Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -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 | #include "json_parser.hpp" |
| 17 | |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 18 | #include "conditions.hpp" |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 19 | #include "json_config.hpp" |
| 20 | #include "nonzero_speed_trust.hpp" |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 21 | #include "power_interface.hpp" |
| 22 | #include "power_off_rule.hpp" |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 23 | #include "tach_sensor.hpp" |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 24 | #include "types.hpp" |
| 25 | |
| 26 | #include <nlohmann/json.hpp> |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 27 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | #include <map> |
| 31 | #include <memory> |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 32 | #include <optional> |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
| 35 | namespace phosphor::fan::monitor |
| 36 | { |
| 37 | |
| 38 | using json = nlohmann::json; |
| 39 | using namespace phosphor::logging; |
| 40 | |
| 41 | namespace tClass |
| 42 | { |
| 43 | |
| 44 | // Get a constructed trust group class for a non-zero speed group |
Patrick Williams | 4fa67aa | 2025-02-03 14:28:47 -0500 | [diff] [blame] | 45 | CreateGroupFunction getNonZeroSpeed( |
| 46 | const std::vector<trust::GroupDefinition>& group) |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 47 | { |
| 48 | return [group]() { |
| 49 | return std::make_unique<trust::NonzeroSpeed>(std::move(group)); |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | } // namespace tClass |
| 54 | |
| 55 | const std::map<std::string, trustHandler> trusts = { |
| 56 | {"nonzerospeed", tClass::getNonZeroSpeed}}; |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 57 | const std::map<std::string, condHandler> conditions = { |
| 58 | {"propertiesmatch", condition::getPropertiesMatch}}; |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 59 | const std::map<std::string, size_t> methods = { |
| 60 | {"timebased", MethodMode::timebased}, {"count", MethodMode::count}}; |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 61 | |
| 62 | const std::vector<CreateGroupFunction> getTrustGrps(const json& obj) |
| 63 | { |
| 64 | std::vector<CreateGroupFunction> grpFuncs; |
| 65 | |
| 66 | if (obj.contains("sensor_trust_groups")) |
| 67 | { |
| 68 | for (auto& stg : obj["sensor_trust_groups"]) |
| 69 | { |
| 70 | if (!stg.contains("class") || !stg.contains("group")) |
| 71 | { |
| 72 | // Log error on missing required parameters |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 73 | lg2::error( |
| 74 | "Missing required fan monitor trust group parameters 'class, group'"); |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 75 | throw std::runtime_error( |
| 76 | "Missing required fan trust group parameters"); |
| 77 | } |
| 78 | auto tgClass = stg["class"].get<std::string>(); |
| 79 | std::vector<trust::GroupDefinition> group; |
| 80 | for (auto& member : stg["group"]) |
| 81 | { |
| 82 | // Construct list of group members |
| 83 | if (!member.contains("name")) |
| 84 | { |
| 85 | // Log error on missing required parameter |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 86 | lg2::error( |
| 87 | "Missing required fan monitor trust group member name for class {CLASS}", |
| 88 | "CLASS", tgClass); |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 89 | throw std::runtime_error( |
| 90 | "Missing required fan monitor trust group member name"); |
| 91 | } |
| 92 | auto in_trust = true; |
| 93 | if (member.contains("in_trust")) |
| 94 | { |
| 95 | in_trust = member["in_trust"].get<bool>(); |
| 96 | } |
| 97 | group.emplace_back(trust::GroupDefinition{ |
| 98 | member["name"].get<std::string>(), in_trust}); |
| 99 | } |
| 100 | // The class for fan sensor trust groups |
| 101 | // (Must have a supported function within the tClass namespace) |
| 102 | std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(), |
| 103 | tolower); |
| 104 | auto handler = trusts.find(tgClass); |
| 105 | if (handler != trusts.end()) |
| 106 | { |
| 107 | // Call function for trust group class |
| 108 | grpFuncs.emplace_back(handler->second(group)); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | // Log error on unsupported trust group class |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 113 | lg2::error("Invalid fan monitor trust group class {CLASS}", |
| 114 | "CLASS", tgClass); |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 115 | throw std::runtime_error( |
| 116 | "Invalid fan monitor trust group class"); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return grpFuncs; |
| 122 | } |
| 123 | |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 124 | const std::vector<SensorDefinition> getSensorDefs(const json& sensors) |
| 125 | { |
| 126 | std::vector<SensorDefinition> sensorDefs; |
| 127 | |
| 128 | for (const auto& sensor : sensors) |
| 129 | { |
| 130 | if (!sensor.contains("name") || !sensor.contains("has_target")) |
| 131 | { |
| 132 | // Log error on missing required parameters |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 133 | lg2::error( |
| 134 | "Missing required fan sensor definition parameters 'name, has_target'"); |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 135 | throw std::runtime_error( |
| 136 | "Missing required fan sensor definition parameters"); |
| 137 | } |
| 138 | // Target interface is optional and defaults to |
| 139 | // 'xyz.openbmc_project.Control.FanSpeed' |
| 140 | std::string targetIntf = "xyz.openbmc_project.Control.FanSpeed"; |
| 141 | if (sensor.contains("target_interface")) |
| 142 | { |
| 143 | targetIntf = sensor["target_interface"].get<std::string>(); |
| 144 | } |
Chau Ly | 27cc39f | 2022-09-20 08:16:56 +0000 | [diff] [blame] | 145 | // Target path is optional |
| 146 | std::string targetPath; |
| 147 | if (sensor.contains("target_path")) |
| 148 | { |
| 149 | targetPath = sensor["target_path"].get<std::string>(); |
| 150 | } |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 151 | // Factor is optional and defaults to 1 |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 152 | double factor = 1.0; |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 153 | if (sensor.contains("factor")) |
| 154 | { |
| 155 | factor = sensor["factor"].get<double>(); |
| 156 | } |
| 157 | // Offset is optional and defaults to 0 |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 158 | int64_t offset = 0; |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 159 | if (sensor.contains("offset")) |
| 160 | { |
| 161 | offset = sensor["offset"].get<int64_t>(); |
| 162 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 163 | // Threshold is optional and defaults to 1 |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 164 | size_t threshold = 1; |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 165 | if (sensor.contains("threshold")) |
| 166 | { |
| 167 | threshold = sensor["threshold"].get<size_t>(); |
| 168 | } |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 169 | // Ignore being above the allowed max is optional, defaults to not |
| 170 | bool ignoreAboveMax = false; |
| 171 | if (sensor.contains("ignore_above_max")) |
| 172 | { |
| 173 | ignoreAboveMax = sensor["ignore_above_max"].get<bool>(); |
| 174 | } |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 175 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 176 | SensorDefinition def{ |
| 177 | .name = sensor["name"].get<std::string>(), |
| 178 | .hasTarget = sensor["has_target"].get<bool>(), |
| 179 | .targetInterface = targetIntf, |
| 180 | .targetPath = targetPath, |
| 181 | .factor = factor, |
| 182 | .offset = offset, |
| 183 | .threshold = threshold, |
| 184 | .ignoreAboveMax = ignoreAboveMax}; |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 185 | |
| 186 | sensorDefs.push_back(std::move(def)); |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return sensorDefs; |
| 190 | } |
| 191 | |
| 192 | const std::vector<FanDefinition> getFanDefs(const json& obj) |
| 193 | { |
| 194 | std::vector<FanDefinition> fanDefs; |
| 195 | |
| 196 | for (const auto& fan : obj["fans"]) |
| 197 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 198 | if (!fan.contains("inventory") || !fan.contains("deviation") || |
| 199 | !fan.contains("sensors")) |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 200 | { |
| 201 | // Log error on missing required parameters |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 202 | lg2::error( |
| 203 | "Missing required fan monitor definition parameters 'inventory, deviation, sensors'"); |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 204 | throw std::runtime_error( |
| 205 | "Missing required fan monitor definition parameters"); |
| 206 | } |
Matthew Barth | 4c4de26 | 2021-02-17 13:03:02 -0600 | [diff] [blame] | 207 | // Valid deviation range is 0 - 100% |
| 208 | auto deviation = fan["deviation"].get<size_t>(); |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 209 | if (100 < deviation) |
Matthew Barth | 4c4de26 | 2021-02-17 13:03:02 -0600 | [diff] [blame] | 210 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 211 | lg2::error( |
| 212 | "Invalid deviation of {DEVIATION} found, must be between 0 and 100", |
| 213 | "DEVIATION", deviation); |
| 214 | throw std::runtime_error( |
| 215 | "Invalid deviation found, must be between 0 and 100"); |
Matthew Barth | 4c4de26 | 2021-02-17 13:03:02 -0600 | [diff] [blame] | 216 | } |
| 217 | |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 218 | // Upper deviation defaults to the deviation value and |
| 219 | // can also be separately specified. |
| 220 | size_t upperDeviation = deviation; |
| 221 | if (fan.contains("upper_deviation")) |
| 222 | { |
| 223 | upperDeviation = fan["upper_deviation"].get<size_t>(); |
| 224 | if (100 < upperDeviation) |
| 225 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 226 | lg2::error( |
| 227 | "Invalid upper_deviation of {UPPER_DEVIATION} found, must be between 0 and 100", |
| 228 | "UPPER_DEVIATION", upperDeviation); |
| 229 | throw std::runtime_error( |
| 230 | "Invalid upper_deviation found, must be between 0 and 100"); |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 234 | // Construct the sensor definitions for this fan |
| 235 | auto sensorDefs = getSensorDefs(fan["sensors"]); |
| 236 | |
| 237 | // Functional delay is optional and defaults to 0 |
| 238 | size_t funcDelay = 0; |
| 239 | if (fan.contains("functional_delay")) |
| 240 | { |
| 241 | funcDelay = fan["functional_delay"].get<size_t>(); |
| 242 | } |
| 243 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 244 | // Method is optional and defaults to time based functional |
| 245 | // determination |
| 246 | size_t method = MethodMode::timebased; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 247 | size_t countInterval = 1; |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 248 | if (fan.contains("method")) |
| 249 | { |
| 250 | auto methodConf = fan["method"].get<std::string>(); |
| 251 | auto methodFunc = methods.find(methodConf); |
| 252 | if (methodFunc != methods.end()) |
| 253 | { |
| 254 | method = methodFunc->second; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | // Log error on unsupported method parameter |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 259 | lg2::error("Invalid fan method"); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 260 | throw std::runtime_error("Invalid fan method"); |
| 261 | } |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 262 | |
| 263 | // Read the count interval value used with the count method. |
| 264 | if (method == MethodMode::count) |
| 265 | { |
| 266 | if (fan.contains("count_interval")) |
| 267 | { |
| 268 | countInterval = fan["count_interval"].get<size_t>(); |
| 269 | } |
| 270 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Timeout defaults to 0 |
| 274 | size_t timeout = 0; |
| 275 | if (method == MethodMode::timebased) |
| 276 | { |
| 277 | if (!fan.contains("allowed_out_of_range_time")) |
| 278 | { |
| 279 | // Log error on missing required parameter |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 280 | lg2::error( |
| 281 | "Missing required fan monitor definition parameters 'allowed_out_of_range_time'"); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 282 | throw std::runtime_error( |
| 283 | "Missing required fan monitor definition parameters"); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | timeout = fan["allowed_out_of_range_time"].get<size_t>(); |
| 288 | } |
| 289 | } |
| 290 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 291 | // Monitor start delay is optional and defaults to 0 |
| 292 | size_t monitorDelay = 0; |
| 293 | if (fan.contains("monitor_start_delay")) |
| 294 | { |
| 295 | monitorDelay = fan["monitor_start_delay"].get<size_t>(); |
| 296 | } |
| 297 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 298 | // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults |
| 299 | // to zero if not present, meaning the code will not set the |
| 300 | // parent fan to nonfunctional based on sensors. |
| 301 | size_t nonfuncSensorsCount = 0; |
| 302 | if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc")) |
| 303 | { |
| 304 | nonfuncSensorsCount = |
| 305 | fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>(); |
| 306 | } |
| 307 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 308 | // nonfunc_rotor_error_delay is optional, though it will |
| 309 | // default to zero if 'fault_handling' is present. |
| 310 | std::optional<size_t> nonfuncRotorErrorDelay; |
| 311 | if (fan.contains("nonfunc_rotor_error_delay")) |
| 312 | { |
| 313 | nonfuncRotorErrorDelay = |
| 314 | fan["nonfunc_rotor_error_delay"].get<size_t>(); |
| 315 | } |
| 316 | else if (obj.contains("fault_handling")) |
| 317 | { |
| 318 | nonfuncRotorErrorDelay = 0; |
| 319 | } |
| 320 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 321 | // fan_missing_error_delay is optional. |
| 322 | std::optional<size_t> fanMissingErrorDelay; |
| 323 | if (fan.contains("fan_missing_error_delay")) |
| 324 | { |
| 325 | fanMissingErrorDelay = |
| 326 | fan.at("fan_missing_error_delay").get<size_t>(); |
| 327 | } |
| 328 | |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 329 | // Handle optional conditions |
Matthew Barth | 8a0c232 | 2020-06-17 09:53:10 -0500 | [diff] [blame] | 330 | auto cond = std::optional<Condition>(); |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 331 | if (fan.contains("condition")) |
| 332 | { |
| 333 | if (!fan["condition"].contains("name")) |
| 334 | { |
| 335 | // Log error on missing required parameter |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 336 | lg2::error( |
| 337 | "Missing required fan monitor condition parameter 'name'"); |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 338 | throw std::runtime_error( |
| 339 | "Missing required fan monitor condition parameter"); |
| 340 | } |
| 341 | auto name = fan["condition"]["name"].get<std::string>(); |
| 342 | // The function for fan monitoring condition |
| 343 | // (Must have a supported function within the condition namespace) |
| 344 | std::transform(name.begin(), name.end(), name.begin(), tolower); |
| 345 | auto handler = conditions.find(name); |
| 346 | if (handler != conditions.end()) |
| 347 | { |
| 348 | cond = handler->second(fan["condition"]); |
| 349 | } |
| 350 | else |
| 351 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 352 | lg2::info( |
| 353 | "No handler found for configured condition {CONDITION_NAME}", |
| 354 | "CONDITION_NAME", name, "JSON_DUMP", |
| 355 | fan["condition"].dump()); |
Matthew Barth | 3ad1434 | 2020-06-08 16:17:42 -0500 | [diff] [blame] | 356 | } |
| 357 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 358 | |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 359 | // if the fan should be set to functional when plugged in |
| 360 | bool setFuncOnPresent = false; |
| 361 | if (fan.contains("set_func_on_present")) |
| 362 | { |
| 363 | setFuncOnPresent = fan["set_func_on_present"].get<bool>(); |
| 364 | } |
| 365 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 366 | FanDefinition def{ |
| 367 | .name = fan["inventory"].get<std::string>(), |
| 368 | .method = method, |
| 369 | .funcDelay = funcDelay, |
| 370 | .timeout = timeout, |
| 371 | .deviation = deviation, |
| 372 | .upperDeviation = upperDeviation, |
| 373 | .numSensorFailsForNonfunc = nonfuncSensorsCount, |
| 374 | .monitorStartDelay = monitorDelay, |
| 375 | .countInterval = countInterval, |
| 376 | .nonfuncRotorErrDelay = nonfuncRotorErrorDelay, |
| 377 | .fanMissingErrDelay = fanMissingErrorDelay, |
| 378 | .sensorList = std::move(sensorDefs), |
| 379 | .condition = cond, |
| 380 | .funcOnPresent = setFuncOnPresent}; |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 381 | |
| 382 | fanDefs.push_back(std::move(def)); |
Matthew Barth | 22ab93b | 2020-06-08 10:47:56 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return fanDefs; |
| 386 | } |
| 387 | |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 388 | PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig) |
| 389 | { |
| 390 | // The state is optional and defaults to runtime |
| 391 | PowerRuleState ruleState{PowerRuleState::runtime}; |
| 392 | |
| 393 | if (powerOffConfig.contains("state")) |
| 394 | { |
| 395 | auto state = powerOffConfig.at("state").get<std::string>(); |
| 396 | if (state == "at_pgood") |
| 397 | { |
| 398 | ruleState = PowerRuleState::atPgood; |
| 399 | } |
| 400 | else if (state != "runtime") |
| 401 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 402 | lg2::error("Invalid power off state entry {STATE}", "STATE", state); |
| 403 | throw std::runtime_error("Invalid power off state entry"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | return ruleState; |
| 408 | } |
| 409 | |
| 410 | std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig) |
| 411 | { |
| 412 | std::unique_ptr<PowerOffCause> cause; |
| 413 | |
| 414 | if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause")) |
| 415 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 416 | lg2::error("Missing 'count' or 'cause' entries in power off config"); |
| 417 | throw std::runtime_error( |
| 418 | "Missing 'count' or 'cause' entries in power off config"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | auto count = powerOffConfig.at("count").get<size_t>(); |
| 422 | auto powerOffCause = powerOffConfig.at("cause").get<std::string>(); |
| 423 | |
| 424 | const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>> |
| 425 | causes{ |
| 426 | {"missing_fan_frus", |
| 427 | [count]() { return std::make_unique<MissingFanFRUCause>(count); }}, |
Matt Spinler | 4c62fc7 | 2024-02-14 16:10:19 -0600 | [diff] [blame] | 428 | {"nonfunc_fan_rotors", |
| 429 | [count]() { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 430 | return std::make_unique<NonfuncFanRotorCause>(count); |
| 431 | }}, |
Matt Spinler | 4c62fc7 | 2024-02-14 16:10:19 -0600 | [diff] [blame] | 432 | {"fan_frus_with_nonfunc_rotors", [count]() { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 433 | return std::make_unique<FanFRUsWithNonfuncRotorsCause>(count); |
| 434 | }}}; |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 435 | |
| 436 | auto it = causes.find(powerOffCause); |
| 437 | if (it != causes.end()) |
| 438 | { |
| 439 | cause = it->second(); |
| 440 | } |
| 441 | else |
| 442 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 443 | lg2::error( |
| 444 | "Invalid power off cause {POWER_OFF_CAUSE} in power off config JSON", |
| 445 | "POWER_OFF_CAUSE", powerOffCause); |
| 446 | throw std::runtime_error( |
| 447 | "Invalid power off cause in power off config JSON"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return cause; |
| 451 | } |
| 452 | |
Patrick Williams | 4fa67aa | 2025-02-03 14:28:47 -0500 | [diff] [blame] | 453 | std::unique_ptr<PowerOffAction> getPowerOffAction( |
| 454 | const json& powerOffConfig, |
| 455 | std::shared_ptr<PowerInterfaceBase>& powerInterface, |
| 456 | PowerOffAction::PrePowerOffFunc& func) |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 457 | { |
| 458 | std::unique_ptr<PowerOffAction> action; |
| 459 | if (!powerOffConfig.contains("type")) |
| 460 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 461 | lg2::error("Missing 'type' entry in power off config"); |
| 462 | throw std::runtime_error("Missing 'type' entry in power off config"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | auto type = powerOffConfig.at("type").get<std::string>(); |
| 466 | |
| 467 | if (((type == "hard") || (type == "soft")) && |
| 468 | !powerOffConfig.contains("delay")) |
| 469 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 470 | lg2::error("Missing 'delay' entry in power off config"); |
| 471 | throw std::runtime_error("Missing 'delay' entry in power off config"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 472 | } |
| 473 | else if ((type == "epow") && |
| 474 | (!powerOffConfig.contains("service_mode_delay") || |
| 475 | !powerOffConfig.contains("meltdown_delay"))) |
| 476 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 477 | lg2::error( |
| 478 | "Missing 'service_mode_delay' or 'meltdown_delay' entry in power off config"); |
| 479 | throw std::runtime_error( |
| 480 | "Missing 'service_mode_delay' or 'meltdown_delay' entry in power off config"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (type == "hard") |
| 484 | { |
| 485 | action = std::make_unique<HardPowerOff>( |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 486 | powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 487 | } |
| 488 | else if (type == "soft") |
| 489 | { |
| 490 | action = std::make_unique<SoftPowerOff>( |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 491 | powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 492 | } |
| 493 | else if (type == "epow") |
| 494 | { |
| 495 | action = std::make_unique<EpowPowerOff>( |
| 496 | powerOffConfig.at("service_mode_delay").get<uint32_t>(), |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 497 | powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface, |
| 498 | func); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 499 | } |
| 500 | else |
| 501 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 502 | lg2::error("Invalid 'type' entry {TYPE} in power off config", "TYPE", |
| 503 | type); |
| 504 | throw std::runtime_error("Invalid 'type' entry in power off config"); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return action; |
| 508 | } |
| 509 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 510 | std::vector<std::unique_ptr<PowerOffRule>> getPowerOffRules( |
| 511 | const json& obj, std::shared_ptr<PowerInterfaceBase>& powerInterface, |
| 512 | PowerOffAction::PrePowerOffFunc& func) |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 513 | { |
| 514 | std::vector<std::unique_ptr<PowerOffRule>> rules; |
| 515 | |
| 516 | if (!(obj.contains("fault_handling") && |
| 517 | obj.at("fault_handling").contains("power_off_config"))) |
| 518 | { |
| 519 | return rules; |
| 520 | } |
| 521 | |
| 522 | for (const auto& config : obj.at("fault_handling").at("power_off_config")) |
| 523 | { |
| 524 | auto state = getPowerOffPowerRuleState(config); |
| 525 | auto cause = getPowerOffCause(config); |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 526 | auto action = getPowerOffAction(config, powerInterface, func); |
Matt Spinler | f06ab07 | 2020-10-14 12:58:22 -0500 | [diff] [blame] | 527 | |
| 528 | auto rule = std::make_unique<PowerOffRule>( |
| 529 | std::move(state), std::move(cause), std::move(action)); |
| 530 | rules.push_back(std::move(rule)); |
| 531 | } |
| 532 | |
| 533 | return rules; |
| 534 | } |
| 535 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 536 | std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj) |
| 537 | { |
| 538 | std::optional<size_t> num; |
| 539 | |
| 540 | if (obj.contains("fault_handling")) |
| 541 | { |
| 542 | // Defaults to 1 if not present inside of 'fault_handling'. |
| 543 | num = obj.at("fault_handling") |
| 544 | .value("num_nonfunc_rotors_before_error", 1); |
| 545 | } |
| 546 | |
| 547 | return num; |
| 548 | } |
| 549 | |
Matthew Barth | 9ea8bee | 2020-06-04 14:27:19 -0500 | [diff] [blame] | 550 | } // namespace phosphor::fan::monitor |