Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 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 | #include <string> |
Matthew Barth | 0961fae | 2019-11-15 09:00:27 -0600 | [diff] [blame] | 17 | #include <filesystem> |
| 18 | #include <fstream> |
| 19 | #include <nlohmann/json.hpp> |
| 20 | #include <phosphor-logging/log.hpp> |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 21 | |
| 22 | #include "json_config.hpp" |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 23 | #include "tach.hpp" |
| 24 | #include "gpio.hpp" |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 25 | #include "anyof.hpp" |
| 26 | #include "fallback.hpp" |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 27 | |
| 28 | namespace phosphor |
| 29 | { |
| 30 | namespace fan |
| 31 | { |
| 32 | namespace presence |
| 33 | { |
| 34 | |
Matthew Barth | 0961fae | 2019-11-15 09:00:27 -0600 | [diff] [blame] | 35 | using json = nlohmann::json; |
| 36 | namespace fs = std::filesystem; |
| 37 | using namespace phosphor::logging; |
| 38 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 39 | policies JsonConfig::_policies; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 40 | const std::map<std::string, methodHandler> JsonConfig::_methods = |
| 41 | { |
| 42 | {"tach", method::getTach}, |
| 43 | {"gpio", method::getGpio} |
| 44 | }; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 45 | const std::map<std::string, rpolicyHandler> JsonConfig::_rpolicies = |
| 46 | { |
| 47 | {"anyof", rpolicy::getAnyof}, |
| 48 | {"fallback", rpolicy::getFallback} |
| 49 | }; |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 50 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 51 | JsonConfig::JsonConfig(const std::string& jsonFile) : |
| 52 | _defaultFile(fs::path(jsonFile)) |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 53 | { |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 54 | // Load and process the json configuration |
| 55 | load(); |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | const policies& JsonConfig::get() |
| 59 | { |
| 60 | return _policies; |
| 61 | } |
| 62 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 63 | void JsonConfig::sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 64 | const struct signalfd_siginfo* sigInfo) |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | // Load and process the json configuration |
| 69 | load(); |
| 70 | for (auto& p: _policies) |
| 71 | { |
| 72 | p->monitor(); |
| 73 | } |
| 74 | log<level::INFO>("Configuration loaded successfully"); |
| 75 | } |
| 76 | catch (std::runtime_error& re) |
| 77 | { |
| 78 | log<level::ERR>("Error loading config, no config changes made", |
| 79 | entry("LOAD_ERROR=%s", re.what())); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void JsonConfig::load() |
| 84 | { |
| 85 | std::ifstream file; |
| 86 | json jsonConf; |
| 87 | |
| 88 | if (fs::exists(_defaultFile)) |
| 89 | { |
| 90 | file.open(_defaultFile); |
| 91 | try |
| 92 | { |
| 93 | jsonConf = json::parse(file); |
| 94 | } |
| 95 | catch (std::exception& e) |
| 96 | { |
| 97 | log<level::ERR>("Failed to parse JSON config file", |
| 98 | entry("JSON_FILE=%s", _defaultFile.c_str()), |
| 99 | entry("JSON_ERROR=%s", e.what())); |
| 100 | throw std::runtime_error("Failed to parse JSON config file"); |
| 101 | } |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | log<level::ERR>("Unable to open JSON config file", |
| 106 | entry("JSON_FILE=%s", _defaultFile.c_str())); |
| 107 | throw std::runtime_error("Unable to open JSON config file"); |
| 108 | } |
| 109 | |
| 110 | process(jsonConf); |
| 111 | } |
| 112 | |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 113 | void JsonConfig::process(const json& jsonConf) |
| 114 | { |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 115 | policies policies; |
| 116 | std::vector<fanPolicy> fans; |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 117 | // Set the expected number of fan entries |
| 118 | // to be size of the list of fan json config entries |
| 119 | // (Must be done to eliminate vector reallocation of fan references) |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 120 | fans.reserve(jsonConf.size()); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 121 | for (auto& member : jsonConf) |
| 122 | { |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 123 | if (!member.contains("name") || !member.contains("path") || |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 124 | !member.contains("methods") || !member.contains("rpolicy")) |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 125 | { |
| 126 | log<level::ERR>( |
| 127 | "Missing required fan presence properties", |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 128 | entry("REQUIRED_PROPERTIES=%s", |
| 129 | "{name, path, methods, rpolicy}")); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 130 | throw std::runtime_error( |
| 131 | "Missing required fan presence properties"); |
| 132 | } |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 133 | |
| 134 | // Loop thru the configured methods of presence detection |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 135 | std::vector<std::unique_ptr<PresenceSensor>> sensors; |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 136 | for (auto& method : member["methods"].items()) |
| 137 | { |
| 138 | if (!method.value().contains("type")) |
| 139 | { |
| 140 | log<level::ERR>( |
| 141 | "Missing required fan presence method type", |
| 142 | entry("FAN_NAME=%s", |
| 143 | member["name"].get<std::string>().c_str())); |
| 144 | throw std::runtime_error( |
| 145 | "Missing required fan presence method type"); |
| 146 | } |
| 147 | // The method type of fan presence detection |
| 148 | // (Must have a supported function within the method namespace) |
| 149 | auto type = method.value()["type"].get<std::string>(); |
| 150 | std::transform(type.begin(), type.end(), type.begin(), tolower); |
| 151 | auto func = _methods.find(type); |
| 152 | if (func != _methods.end()) |
| 153 | { |
| 154 | // Call function for method type |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 155 | auto sensor = func->second(fans.size(), method.value()); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 156 | if (sensor) |
| 157 | { |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 158 | sensors.emplace_back(std::move(sensor)); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | log<level::ERR>("Invalid fan presence method type", |
| 164 | entry("FAN_NAME=%s", |
| 165 | member["name"].get<std::string>().c_str()), |
| 166 | entry("METHOD_TYPE=%s", type.c_str())); |
| 167 | throw std::runtime_error("Invalid fan presence method type"); |
| 168 | } |
| 169 | } |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 170 | auto fan = std::make_tuple(member["name"], member["path"]); |
| 171 | // Create a fan object |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 172 | fans.emplace_back(std::make_tuple(fan, std::move(sensors))); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 173 | |
| 174 | // Add fan presence policy |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 175 | auto policy = getPolicy(member["rpolicy"], fans.back()); |
| 176 | if (policy) |
| 177 | { |
| 178 | policies.emplace_back(std::move(policy)); |
| 179 | } |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 180 | } |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 181 | |
| 182 | // Success, refresh fans and policies lists |
| 183 | _fans.clear(); |
| 184 | _fans.swap(fans); |
| 185 | |
| 186 | _policies.clear(); |
| 187 | _policies.swap(policies); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 190 | std::unique_ptr<RedundancyPolicy> JsonConfig::getPolicy( |
| 191 | const json& rpolicy, |
| 192 | const fanPolicy& fpolicy) |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 193 | { |
| 194 | if (!rpolicy.contains("type")) |
| 195 | { |
| 196 | log<level::ERR>("Missing required fan presence policy type", |
| 197 | entry("FAN_NAME=%s", |
| 198 | std::get<fanPolicyFanPos>( |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 199 | std::get<Fan>(fpolicy)).c_str()), |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 200 | entry("REQUIRED_PROPERTIES=%s", "{type}")); |
| 201 | throw std::runtime_error("Missing required fan presence policy type"); |
| 202 | } |
| 203 | |
| 204 | // The redundancy policy type for fan presence detection |
| 205 | // (Must have a supported function within the rpolicy namespace) |
| 206 | auto type = rpolicy["type"].get<std::string>(); |
| 207 | std::transform(type.begin(), type.end(), type.begin(), tolower); |
| 208 | auto func = _rpolicies.find(type); |
| 209 | if (func != _rpolicies.end()) |
| 210 | { |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 211 | // Call function for redundancy policy type and return the policy |
| 212 | return func->second(fpolicy); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | { |
| 216 | log<level::ERR>("Invalid fan presence policy type", |
| 217 | entry("FAN_NAME=%s", |
Matthew Barth | f3e7047 | 2019-12-03 13:33:20 -0600 | [diff] [blame^] | 218 | std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)).c_str()), |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 219 | entry("RPOLICY_TYPE=%s", type.c_str())); |
| 220 | throw std::runtime_error("Invalid fan presence methods policy type"); |
Matthew Barth | 4a94dec | 2019-11-15 10:40:47 -0600 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 224 | /** |
| 225 | * Methods of fan presence detection function definitions |
| 226 | */ |
| 227 | namespace method |
| 228 | { |
| 229 | // Get a constructed presence sensor for fan presence detection by tach |
| 230 | std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method) |
| 231 | { |
Matthew Barth | e0d98a3 | 2019-11-19 15:40:57 -0600 | [diff] [blame] | 232 | if (!method.contains("sensors") || |
| 233 | method["sensors"].size() == 0) |
| 234 | { |
| 235 | log<level::ERR>( |
| 236 | "Missing required tach method properties", |
| 237 | entry("FAN_ENTRY=%d", fanIndex), |
| 238 | entry("REQUIRED_PROPERTIES=%s", "{sensors}")); |
| 239 | throw std::runtime_error("Missing required tach method properties"); |
| 240 | } |
| 241 | |
| 242 | std::vector<std::string> sensors; |
| 243 | for (auto& sensor : method["sensors"]) |
| 244 | { |
| 245 | sensors.emplace_back(sensor.get<std::string>()); |
| 246 | } |
| 247 | |
| 248 | return std::make_unique<PolicyAccess<Tach, JsonConfig>>( |
| 249 | fanIndex, std::move(sensors)); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Get a constructed presence sensor for fan presence detection by gpio |
| 253 | std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method) |
| 254 | { |
Matthew Barth | e0d98a3 | 2019-11-19 15:40:57 -0600 | [diff] [blame] | 255 | if (!method.contains("physpath") || |
| 256 | !method.contains("devpath") || |
| 257 | !method.contains("key")) |
| 258 | { |
| 259 | log<level::ERR>( |
| 260 | "Missing required gpio method properties", |
| 261 | entry("FAN_ENTRY=%d", fanIndex), |
| 262 | entry("REQUIRED_PROPERTIES=%s", "{physpath, devpath, key}")); |
| 263 | throw std::runtime_error("Missing required gpio method properties"); |
| 264 | } |
| 265 | |
| 266 | auto physpath = method["physpath"].get<std::string>(); |
| 267 | auto devpath = method["devpath"].get<std::string>(); |
| 268 | auto key = method["key"].get<unsigned int>(); |
| 269 | |
| 270 | return std::make_unique<PolicyAccess<Gpio, JsonConfig>>( |
| 271 | fanIndex, physpath, devpath, key); |
Matthew Barth | e756663 | 2019-11-18 16:13:04 -0600 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | } // namespace method |
| 275 | |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 276 | /** |
| 277 | * Redundancy policies for fan presence detection function definitions |
| 278 | */ |
| 279 | namespace rpolicy |
| 280 | { |
| 281 | // Get an `Anyof` redundancy policy for the fan |
| 282 | std::unique_ptr<RedundancyPolicy> getAnyof(const fanPolicy& fan) |
| 283 | { |
Matthew Barth | 26ad44a | 2019-11-22 15:37:14 -0600 | [diff] [blame] | 284 | std::vector<std::reference_wrapper<PresenceSensor>> pSensors; |
| 285 | for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan)) |
| 286 | { |
| 287 | pSensors.emplace_back(*fanSensor); |
| 288 | } |
| 289 | |
| 290 | return std::make_unique<AnyOf>( |
| 291 | std::get<fanPolicyFanPos>(fan), pSensors); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Get a `Fallback` redundancy policy for the fan |
| 295 | std::unique_ptr<RedundancyPolicy> getFallback(const fanPolicy& fan) |
| 296 | { |
Matthew Barth | 26ad44a | 2019-11-22 15:37:14 -0600 | [diff] [blame] | 297 | std::vector<std::reference_wrapper<PresenceSensor>> pSensors; |
| 298 | for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan)) |
| 299 | { |
| 300 | // Place in the order given to fallback correctly |
| 301 | pSensors.emplace_back(*fanSensor); |
| 302 | } |
| 303 | |
| 304 | return std::make_unique<Fallback>( |
| 305 | std::get<fanPolicyFanPos>(fan), pSensors); |
Matthew Barth | aa8d81d | 2019-11-21 14:07:31 -0600 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | } // namespace policy |
| 309 | |
Matthew Barth | fd05d64 | 2019-11-14 15:01:57 -0600 | [diff] [blame] | 310 | } // namespace presence |
| 311 | } // namespace fan |
| 312 | } // namespace phosphor |