Matthew Barth | d87f89f | 2020-07-30 10:41:32 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 18 | #include "json_config.hpp" |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 19 | #include "types.hpp" |
| 20 | |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 21 | #include <nlohmann/json.hpp> |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
| 23 | |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <memory> |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 26 | #include <utility> |
| 27 | #include <vector> |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 28 | |
Matthew Barth | d87f89f | 2020-07-30 10:41:32 -0500 | [diff] [blame] | 29 | namespace phosphor::fan::control |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 30 | { |
| 31 | |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 32 | /* Application name to be appended to the path for loading a JSON config file */ |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 33 | constexpr auto confAppName = "control"; |
| 34 | |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 35 | /** |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 36 | * Configuration object key to uniquely map to the configuration object |
| 37 | * Pair constructed of: |
| 38 | * std::string = Configuration object's name |
| 39 | * std::vector<std::string> = List of profiles the configuration object |
| 40 | * is included in |
| 41 | */ |
| 42 | using configKey = std::pair<std::string, std::vector<std::string>>; |
| 43 | |
| 44 | /** |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 45 | * @brief Load the configuration of a given JSON class object type |
| 46 | * |
| 47 | * @param[in] bus - The dbus bus object |
| 48 | * @param[in] isOptional - JSON configuration file is optional or not |
| 49 | * Defaults to false |
| 50 | * |
| 51 | * @return Map of configuration entries |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 52 | * Map of configuration keys to their corresponding configuration object |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 53 | */ |
| 54 | template <typename T> |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 55 | std::map<configKey, std::unique_ptr<T>> getConfig(sdbusplus::bus::bus& bus, |
| 56 | bool isOptional = false) |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 57 | { |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 58 | std::map<configKey, std::unique_ptr<T>> config; |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 59 | |
| 60 | auto confFile = fan::JsonConfig::getConfFile(bus, confAppName, |
| 61 | T::confFileName, isOptional); |
| 62 | if (!confFile.empty()) |
| 63 | { |
| 64 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 65 | { |
| 66 | auto obj = std::make_unique<T>(bus, entry); |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 67 | config.emplace(std::make_pair(obj->getName(), obj->getProfiles()), |
| 68 | std::move(obj)); |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 71 | return config; |
| 72 | } |
| 73 | |
| 74 | /** |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 75 | * @brief Helper function to determine when a configuration entry is included |
| 76 | * based on the list of active profiles and its list of profiles |
| 77 | * |
| 78 | * A configuration entry may include a list of profiles that when any one of |
| 79 | * the profiles are active, the entry should be included. When the list of |
| 80 | * profiles for a configuration entry is empty, that results in always |
| 81 | * including the entry. An empty list of active profiles results in including |
| 82 | * only those entries configured without a list of profiles. |
| 83 | * |
| 84 | * i.e.) No profiles configured results in always being included, whereas |
| 85 | * providing a list of profiles on an entry results only in that entry being |
| 86 | * included when a profile in the list is active. |
| 87 | * |
| 88 | * @param[in] activeProfiles - List of active system profiles |
| 89 | * @param[in] entryProfiles - List of the configuration entry's profiles |
| 90 | * |
| 91 | * @return Whether the configuration entry should be included or not |
| 92 | */ |
| 93 | bool checkEntry(const std::vector<std::string>& activeProfiles, |
| 94 | const std::vector<std::string>& entryProfiles); |
| 95 | |
| 96 | /** |
Matthew Barth | 23ac24c | 2020-08-04 13:55:43 -0500 | [diff] [blame] | 97 | * @brief Get the configuration definitions for zone groups |
| 98 | * |
| 99 | * @param[in] bus - The dbus bus object |
| 100 | * |
| 101 | * @return List of zone group objects |
| 102 | * Generated list of zone groups including their control configurations |
| 103 | */ |
| 104 | const std::vector<ZoneGroup> getZoneGroups(sdbusplus::bus::bus& bus); |
| 105 | |
Matthew Barth | 2dc5aba | 2020-08-04 14:23:34 -0500 | [diff] [blame] | 106 | /** |
| 107 | * @brief Get the delay(in seconds) to allow the fans to ramp up to the defined |
| 108 | * power on speed |
| 109 | * |
| 110 | * @param[in] bus - The dbus bus object |
| 111 | * |
| 112 | * @return Time to delay in seconds |
| 113 | * Amount of time to delay in seconds after a power on |
| 114 | */ |
| 115 | const unsigned int getPowerOnDelay(sdbusplus::bus::bus& bus); |
| 116 | |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 117 | } // namespace phosphor::fan::control |