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 | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 45 | * @brief Load the configuration of a given JSON class object type that requires |
| 46 | * a dbus object |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 47 | * |
| 48 | * @param[in] bus - The dbus bus object |
| 49 | * @param[in] isOptional - JSON configuration file is optional or not |
| 50 | * Defaults to false |
| 51 | * |
| 52 | * @return Map of configuration entries |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 53 | * Map of configuration keys to their corresponding configuration object |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 54 | */ |
| 55 | template <typename T> |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 56 | std::map<configKey, std::unique_ptr<T>> getConfig(sdbusplus::bus::bus& bus, |
| 57 | bool isOptional = false) |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 58 | { |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 59 | std::map<configKey, std::unique_ptr<T>> config; |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 60 | |
| 61 | auto confFile = fan::JsonConfig::getConfFile(bus, confAppName, |
| 62 | T::confFileName, isOptional); |
| 63 | if (!confFile.empty()) |
| 64 | { |
| 65 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 66 | { |
| 67 | auto obj = std::make_unique<T>(bus, entry); |
Matthew Barth | ce95726 | 2020-08-27 10:35:57 -0500 | [diff] [blame] | 68 | config.emplace(std::make_pair(obj->getName(), obj->getProfiles()), |
| 69 | std::move(obj)); |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
Matthew Barth | 2331d18 | 2020-08-18 15:00:27 -0500 | [diff] [blame] | 72 | return config; |
| 73 | } |
| 74 | |
| 75 | /** |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 76 | * @brief Load the configuration of a given JSON class object type that does not |
| 77 | * require a dbus object |
| 78 | * |
| 79 | * @param[in] isOptional - JSON configuration file is optional or not |
| 80 | * Defaults to false |
| 81 | * |
| 82 | * @return Map of configuration entries |
| 83 | * Map of configuration keys to their corresponding configuration object |
| 84 | */ |
| 85 | template <typename T> |
| 86 | std::map<configKey, std::unique_ptr<T>> getConfig(bool isOptional = false) |
| 87 | { |
| 88 | // Dbus object is needed to retrieve the JSON configuration file |
| 89 | auto bus = sdbusplus::bus::new_default(); |
| 90 | std::map<configKey, std::unique_ptr<T>> config; |
| 91 | |
| 92 | auto confFile = fan::JsonConfig::getConfFile(bus, confAppName, |
| 93 | T::confFileName, isOptional); |
| 94 | if (!confFile.empty()) |
| 95 | { |
| 96 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 97 | { |
| 98 | auto obj = std::make_unique<T>(entry); |
| 99 | config.emplace(std::make_pair(obj->getName(), obj->getProfiles()), |
| 100 | std::move(obj)); |
| 101 | } |
| 102 | } |
| 103 | return config; |
| 104 | } |
| 105 | |
| 106 | /** |
Matthew Barth | 413e4d0 | 2020-09-29 10:44:37 -0500 | [diff] [blame] | 107 | * @brief Helper function to determine when a configuration entry is included |
| 108 | * based on the list of active profiles and its list of profiles |
| 109 | * |
| 110 | * A configuration entry may include a list of profiles that when any one of |
| 111 | * the profiles are active, the entry should be included. When the list of |
| 112 | * profiles for a configuration entry is empty, that results in always |
| 113 | * including the entry. An empty list of active profiles results in including |
| 114 | * only those entries configured without a list of profiles. |
| 115 | * |
| 116 | * i.e.) No profiles configured results in always being included, whereas |
| 117 | * providing a list of profiles on an entry results only in that entry being |
| 118 | * included when a profile in the list is active. |
| 119 | * |
| 120 | * @param[in] activeProfiles - List of active system profiles |
| 121 | * @param[in] entryProfiles - List of the configuration entry's profiles |
| 122 | * |
| 123 | * @return Whether the configuration entry should be included or not |
| 124 | */ |
| 125 | bool checkEntry(const std::vector<std::string>& activeProfiles, |
| 126 | const std::vector<std::string>& entryProfiles); |
| 127 | |
| 128 | /** |
Matthew Barth | 2dc5aba | 2020-08-04 14:23:34 -0500 | [diff] [blame] | 129 | * @brief Get the delay(in seconds) to allow the fans to ramp up to the defined |
| 130 | * power on speed |
| 131 | * |
| 132 | * @param[in] bus - The dbus bus object |
| 133 | * |
| 134 | * @return Time to delay in seconds |
| 135 | * Amount of time to delay in seconds after a power on |
| 136 | */ |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 137 | const unsigned int getPowerOnDelay(sdbusplus::bus::bus& bus, |
| 138 | const sdeventplus::Event& event); |
Matthew Barth | 2dc5aba | 2020-08-04 14:23:34 -0500 | [diff] [blame] | 139 | |
Matthew Barth | 167d2dd | 2020-08-04 12:19:16 -0500 | [diff] [blame] | 140 | } // namespace phosphor::fan::control |