Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 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 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 17 | #include "power_control.hpp" |
| 18 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 19 | #include "config_file_parser.hpp" |
| 20 | #include "format_utils.hpp" |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 21 | #include "types.hpp" |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 22 | #include "ucd90160_device.hpp" |
| 23 | #include "ucd90320_device.hpp" |
| 24 | #include "utility.hpp" |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 25 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 26 | #include <exception> |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 27 | #include <format> |
| 28 | #include <functional> |
| 29 | #include <map> |
| 30 | #include <span> |
| 31 | #include <stdexcept> |
| 32 | #include <thread> |
| 33 | #include <utility> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 34 | |
| 35 | namespace phosphor::power::sequencer |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 36 | { |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 37 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 38 | const std::string powerOnTimeoutError = |
| 39 | "xyz.openbmc_project.Power.Error.PowerOnTimeout"; |
| 40 | |
| 41 | const std::string powerOffTimeoutError = |
| 42 | "xyz.openbmc_project.Power.Error.PowerOffTimeout"; |
| 43 | |
| 44 | const std::string shutdownError = "xyz.openbmc_project.Power.Error.Shutdown"; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 45 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 46 | PowerControl::PowerControl(sdbusplus::bus_t& bus, |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 47 | const sdeventplus::Event& event) : |
Patrick Williams | 3fa31a7 | 2022-04-05 21:22:58 -0500 | [diff] [blame] | 48 | PowerObject{bus, POWER_OBJ_PATH, PowerObject::action::defer_emit}, |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 49 | bus{bus}, services{bus}, |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 50 | pgoodWaitTimer{event, std::bind(&PowerControl::onFailureCallback, this)}, |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 51 | powerOnAllowedTime{std::chrono::steady_clock::now() + minimumColdStartTime}, |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 52 | timer{event, std::bind(&PowerControl::pollPgood, this), pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 53 | { |
| 54 | // Obtain dbus service name |
| 55 | bus.request_name(POWER_IFACE); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 56 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 57 | compatSysTypesFinder = std::make_unique<util::CompatibleSystemTypesFinder>( |
| 58 | bus, std::bind_front(&PowerControl::compatibleSystemTypesFound, this)); |
| 59 | |
| 60 | deviceFinder = std::make_unique<DeviceFinder>( |
| 61 | bus, std::bind_front(&PowerControl::deviceFound, this)); |
| 62 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 63 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 64 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 65 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 66 | int PowerControl::getPgood() const |
| 67 | { |
| 68 | return pgood; |
| 69 | } |
| 70 | |
| 71 | int PowerControl::getPgoodTimeout() const |
| 72 | { |
| 73 | return timeout.count(); |
| 74 | } |
| 75 | |
| 76 | int PowerControl::getState() const |
| 77 | { |
| 78 | return state; |
| 79 | } |
| 80 | |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 81 | void PowerControl::onFailureCallback() |
| 82 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 83 | services.logInfoMsg("After onFailure wait"); |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 84 | |
| 85 | onFailure(false); |
| 86 | |
| 87 | // Power good has failed, call for chassis hard power off |
| 88 | auto method = bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT, |
| 89 | util::SYSTEMD_INTERFACE, "StartUnit"); |
| 90 | method.append(util::POWEROFF_TARGET); |
| 91 | method.append("replace"); |
| 92 | bus.call_noreply(method); |
| 93 | } |
| 94 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 95 | void PowerControl::onFailure(bool wasTimeOut) |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 96 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 97 | std::string error; |
| 98 | std::map<std::string, std::string> additionalData{}; |
| 99 | |
| 100 | // Check if pgood fault occurred on rail monitored by power sequencer device |
| 101 | if (device) |
| 102 | { |
| 103 | try |
| 104 | { |
| 105 | error = device->findPgoodFault(services, powerSupplyError, |
| 106 | additionalData); |
| 107 | } |
| 108 | catch (const std::exception& e) |
| 109 | { |
| 110 | services.logErrorMsg(e.what()); |
| 111 | additionalData.emplace("ERROR", e.what()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // If fault was not isolated to a voltage rail, select a more generic error |
| 116 | if (error.empty()) |
| 117 | { |
| 118 | if (!powerSupplyError.empty()) |
| 119 | { |
| 120 | error = powerSupplyError; |
| 121 | } |
| 122 | else if (wasTimeOut) |
| 123 | { |
| 124 | error = powerOnTimeoutError; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | error = shutdownError; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | services.logError(error, Entry::Level::Critical, additionalData); |
| 133 | |
| 134 | if (!wasTimeOut) |
| 135 | { |
| 136 | services.createBMCDump(); |
| 137 | } |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 138 | } |
| 139 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 140 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 141 | { |
| 142 | if (inStateTransition) |
| 143 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 144 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 145 | const auto now = std::chrono::steady_clock::now(); |
| 146 | if (now > pgoodTimeoutTime) |
| 147 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 148 | services.logErrorMsg(std::format( |
| 149 | "Power state transition timeout, state: {}", state)); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 150 | inStateTransition = false; |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 151 | |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 152 | if (state) |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 153 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 154 | // Time out powering on |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 155 | onFailure(true); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 156 | } |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 157 | else |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 158 | { |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 159 | // Time out powering off |
| 160 | std::map<std::string, std::string> additionalData{}; |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 161 | services.logError(powerOffTimeoutError, Entry::Level::Critical, |
| 162 | additionalData); |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 165 | failureFound = true; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | int pgoodState = pgoodLine.get_value(); |
| 171 | if (pgoodState != pgood) |
| 172 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 173 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 174 | pgood = pgoodState; |
| 175 | if (pgoodState == 0) |
| 176 | { |
| 177 | emitPowerLostSignal(); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | emitPowerGoodSignal(); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 182 | // Clear any errors on the transition to power on |
Jim Wright | abd64b9 | 2022-02-11 09:56:56 -0600 | [diff] [blame] | 183 | powerSupplyError.clear(); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 184 | failureFound = false; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 185 | } |
| 186 | emitPropertyChangedSignal("pgood"); |
| 187 | } |
| 188 | if (pgoodState == state) |
| 189 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 190 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 191 | inStateTransition = false; |
| 192 | } |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 193 | else if (!inStateTransition && (pgoodState == 0) && !failureFound) |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 194 | { |
| 195 | // Not in power off state, not changing state, and power good is off |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 196 | services.logErrorMsg("Chassis pgood failure"); |
Jim Wright | 1b63953 | 2022-11-19 17:41:33 -0600 | [diff] [blame] | 197 | pgoodWaitTimer.restartOnce(std::chrono::seconds(7)); |
Jim Wright | 4875262 | 2022-02-28 20:37:53 -0600 | [diff] [blame] | 198 | failureFound = true; |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 199 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 200 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 201 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 202 | void PowerControl::setPgoodTimeout(int t) |
| 203 | { |
| 204 | if (timeout.count() != t) |
| 205 | { |
| 206 | timeout = std::chrono::seconds(t); |
| 207 | emitPropertyChangedSignal("pgood_timeout"); |
| 208 | } |
| 209 | } |
| 210 | |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame] | 211 | void PowerControl::setPowerSupplyError(const std::string& error) |
| 212 | { |
| 213 | powerSupplyError = error; |
| 214 | } |
| 215 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 216 | void PowerControl::setState(int s) |
| 217 | { |
| 218 | if (state == s) |
| 219 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 220 | services.logInfoMsg( |
| 221 | std::format("Power already at requested state: {}", state)); |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 222 | return; |
| 223 | } |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 224 | if (s == 0) |
| 225 | { |
| 226 | // Wait for two seconds when powering down. This is to allow host and |
| 227 | // other BMC applications time to complete power off processing |
| 228 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 229 | } |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 230 | else |
| 231 | { |
| 232 | // If minimum power off time has not passed, wait |
| 233 | if (powerOnAllowedTime > std::chrono::steady_clock::now()) |
| 234 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 235 | services.logInfoMsg(std::format( |
| 236 | "Waiting {} seconds until power on allowed", |
| 237 | std::chrono::duration_cast<std::chrono::seconds>( |
| 238 | powerOnAllowedTime - std::chrono::steady_clock::now()) |
| 239 | .count())); |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 240 | } |
| 241 | std::this_thread::sleep_until(powerOnAllowedTime); |
| 242 | } |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 243 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 244 | services.logInfoMsg(std::format("setState: {}", s)); |
| 245 | services.logInfoMsg(std::format("Powering chassis {}", (s ? "on" : "off"))); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 246 | powerControlLine.request( |
| 247 | {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0}); |
| 248 | powerControlLine.set_value(s); |
| 249 | powerControlLine.release(); |
| 250 | |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 251 | if (s == 0) |
| 252 | { |
| 253 | // Set a minimum amount of time to wait before next power on |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 254 | powerOnAllowedTime = std::chrono::steady_clock::now() + |
| 255 | minimumPowerOffTime; |
Jim Wright | b4ad95d | 2022-03-08 17:24:01 -0600 | [diff] [blame] | 256 | } |
| 257 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 258 | pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout; |
| 259 | inStateTransition = true; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 260 | state = s; |
| 261 | emitPropertyChangedSignal("state"); |
| 262 | } |
| 263 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 264 | void PowerControl::compatibleSystemTypesFound( |
| 265 | const std::vector<std::string>& types) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 266 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 267 | // If we don't already have compatible system types |
| 268 | if (compatibleSystemTypes.empty()) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 269 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 270 | std::string typesStr = format_utils::toString(std::span{types}); |
| 271 | services.logInfoMsg( |
| 272 | std::format("Compatible system types found: {}", typesStr)); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 273 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 274 | // Store compatible system types |
| 275 | compatibleSystemTypes = types; |
Jim Wright | eae2d61 | 2022-12-23 13:14:17 -0600 | [diff] [blame] | 276 | |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 277 | // Load config file and create device object if possible |
| 278 | loadConfigFileAndCreateDevice(); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 279 | } |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void PowerControl::deviceFound(const DeviceProperties& properties) |
| 283 | { |
| 284 | // If we don't already have device properties |
| 285 | if (!deviceProperties) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 286 | { |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 287 | services.logInfoMsg(std::format( |
| 288 | "Power sequencer device found: type={}, name={}, bus={:d}, address={:#02x}", |
| 289 | properties.type, properties.name, properties.bus, |
| 290 | properties.address)); |
| 291 | |
| 292 | // Store device properties |
| 293 | deviceProperties = properties; |
| 294 | |
| 295 | // Load config file and create device object if possible |
| 296 | loadConfigFileAndCreateDevice(); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 300 | void PowerControl::setUpGpio() |
| 301 | { |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 302 | const std::string powerControlLineName = "power-chassis-control"; |
| 303 | const std::string pgoodLineName = "power-chassis-good"; |
| 304 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 305 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 306 | if (!pgoodLine) |
| 307 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 308 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 309 | services.logErrorMsg(errorString); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 310 | throw std::runtime_error(errorString); |
| 311 | } |
| 312 | powerControlLine = gpiod::find_line(powerControlLineName); |
| 313 | if (!powerControlLine) |
| 314 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 315 | std::string errorString{"GPIO line name not found: " + |
| 316 | powerControlLineName}; |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 317 | services.logErrorMsg(errorString); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 318 | throw std::runtime_error(errorString); |
| 319 | } |
| 320 | |
| 321 | pgoodLine.request( |
| 322 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 323 | int pgoodState = pgoodLine.get_value(); |
| 324 | pgood = pgoodState; |
| 325 | state = pgoodState; |
Shawn McCarney | 1f8b110 | 2024-06-21 18:57:46 -0500 | [diff] [blame] | 326 | services.logInfoMsg(std::format("Pgood state: {}", pgoodState)); |
| 327 | } |
| 328 | |
| 329 | void PowerControl::loadConfigFileAndCreateDevice() |
| 330 | { |
| 331 | // If compatible system types and device properties have been found |
| 332 | if (!compatibleSystemTypes.empty() && deviceProperties) |
| 333 | { |
| 334 | // Find the JSON configuration file |
| 335 | std::filesystem::path configFile = findConfigFile(); |
| 336 | if (!configFile.empty()) |
| 337 | { |
| 338 | // Parse the JSON configuration file |
| 339 | std::vector<std::unique_ptr<Rail>> rails; |
| 340 | if (parseConfigFile(configFile, rails)) |
| 341 | { |
| 342 | // Create the power sequencer device object |
| 343 | createDevice(std::move(rails)); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | std::filesystem::path PowerControl::findConfigFile() |
| 350 | { |
| 351 | // Find config file for current system based on compatible system types |
| 352 | std::filesystem::path configFile; |
| 353 | if (!compatibleSystemTypes.empty()) |
| 354 | { |
| 355 | try |
| 356 | { |
| 357 | configFile = config_file_parser::find(compatibleSystemTypes); |
| 358 | if (!configFile.empty()) |
| 359 | { |
| 360 | services.logInfoMsg(std::format( |
| 361 | "JSON configuration file found: {}", configFile.string())); |
| 362 | } |
| 363 | } |
| 364 | catch (const std::exception& e) |
| 365 | { |
| 366 | services.logErrorMsg(std::format( |
| 367 | "Unable to find JSON configuration file: {}", e.what())); |
| 368 | } |
| 369 | } |
| 370 | return configFile; |
| 371 | } |
| 372 | |
| 373 | bool PowerControl::parseConfigFile(const std::filesystem::path& configFile, |
| 374 | std::vector<std::unique_ptr<Rail>>& rails) |
| 375 | { |
| 376 | // Parse JSON configuration file |
| 377 | bool wasParsed{false}; |
| 378 | try |
| 379 | { |
| 380 | rails = config_file_parser::parse(configFile); |
| 381 | wasParsed = true; |
| 382 | } |
| 383 | catch (const std::exception& e) |
| 384 | { |
| 385 | services.logErrorMsg(std::format( |
| 386 | "Unable to parse JSON configuration file: {}", e.what())); |
| 387 | } |
| 388 | return wasParsed; |
| 389 | } |
| 390 | |
| 391 | void PowerControl::createDevice(std::vector<std::unique_ptr<Rail>> rails) |
| 392 | { |
| 393 | // Create power sequencer device based on device properties |
| 394 | if (deviceProperties) |
| 395 | { |
| 396 | try |
| 397 | { |
| 398 | if (deviceProperties->type == UCD90160Device::deviceName) |
| 399 | { |
| 400 | device = std::make_unique<UCD90160Device>( |
| 401 | std::move(rails), services, deviceProperties->bus, |
| 402 | deviceProperties->address); |
| 403 | } |
| 404 | else if (deviceProperties->type == UCD90320Device::deviceName) |
| 405 | { |
| 406 | device = std::make_unique<UCD90320Device>( |
| 407 | std::move(rails), services, deviceProperties->bus, |
| 408 | deviceProperties->address); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | throw std::runtime_error{std::format( |
| 413 | "Unsupported device type: {}", deviceProperties->type)}; |
| 414 | } |
| 415 | services.logInfoMsg(std::format( |
| 416 | "Power sequencer device created: {}", device->getName())); |
| 417 | } |
| 418 | catch (const std::exception& e) |
| 419 | { |
| 420 | services.logErrorMsg( |
| 421 | std::format("Unable to create device object: {}", e.what())); |
| 422 | } |
| 423 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 426 | } // namespace phosphor::power::sequencer |