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