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