Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "chassis_state_manager.hpp" |
| 4 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 5 | #include "utils.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
| 7 | #include "xyz/openbmc_project/State/Shutdown/Power/error.hpp" |
| 8 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 9 | #include <fmt/format.h> |
| 10 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 11 | #include <cereal/archives/json.hpp> |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 15 | #include <sdbusplus/exception.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 16 | #include <sdeventplus/event.hpp> |
| 17 | #include <sdeventplus/exception.hpp> |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 18 | #include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 19 | |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 20 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 21 | #include <fstream> |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace state |
| 26 | { |
| 27 | namespace manager |
| 28 | { |
| 29 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 30 | PHOSPHOR_LOG2_USING; |
| 31 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 32 | // When you see server:: you know we're referencing our base class |
| 33 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 34 | namespace decoratorServer = |
| 35 | sdbusplus::xyz::openbmc_project::State::Decorator::server; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 36 | |
| 37 | using namespace phosphor::logging; |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 38 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Aatir Manzur | 27115ae | 2019-07-23 16:25:38 -0500 | [diff] [blame] | 39 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout; |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 40 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator; |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 41 | constexpr auto CHASSIS_STATE_POWEROFF_TGT_FMT = |
| 42 | "obmc-chassis-poweroff@{}.target"; |
| 43 | constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT_FMT = |
| 44 | "obmc-chassis-hard-poweroff@{}.target"; |
| 45 | constexpr auto CHASSIS_STATE_POWERON_TGT_FMT = "obmc-chassis-poweron@{}.target"; |
| 46 | constexpr auto RESET_HOST_SENSORS_SVC_FMT = |
| 47 | "phosphor-reset-sensor-states@{}.service"; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 48 | constexpr auto ACTIVE_STATE = "active"; |
| 49 | constexpr auto ACTIVATING_STATE = "activating"; |
| 50 | |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 51 | // Details at https://upower.freedesktop.org/docs/Device.html |
| 52 | constexpr uint TYPE_UPS = 3; |
| 53 | constexpr uint STATE_FULLY_CHARGED = 4; |
| 54 | constexpr uint BATTERY_LVL_FULL = 8; |
| 55 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 56 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 57 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 58 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 59 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 60 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 61 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 62 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 63 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 64 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 65 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 66 | constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device"; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 67 | constexpr auto POWERSYSINPUTS_INTERFACE = |
| 68 | "xyz.openbmc_project.State.Decorator.PowerSystemInputs"; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 69 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 70 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 71 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 72 | { |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 73 | try |
| 74 | { |
| 75 | auto method = this->bus.new_method_call( |
| 76 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 5b95027 | 2019-05-24 12:27:51 -0500 | [diff] [blame] | 77 | this->bus.call(method); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 78 | } |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 79 | catch (const sdbusplus::exception::exception& e) |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 80 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 81 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 82 | elog<InternalFailure>(); |
| 83 | } |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 84 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 85 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 88 | void Chassis::createSystemdTargetTable() |
| 89 | { |
| 90 | systemdTargetTable = { |
| 91 | // Use the hard off target to ensure we shutdown immediately |
| 92 | {Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)}, |
| 93 | {Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)}}; |
| 94 | } |
| 95 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 96 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 97 | // and persistent storage design is in place and sdbusplus |
| 98 | // has read property function |
| 99 | void Chassis::determineInitialState() |
| 100 | { |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 101 | |
| 102 | // Monitor for any properties changed signals on UPower device path |
| 103 | uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 104 | bus, |
| 105 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 106 | "/org/freedesktop/UPower", UPOWER_INTERFACE), |
| 107 | [this](auto& msg) { this->uPowerChangeEvent(msg); }); |
| 108 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 109 | // Monitor for any properties changed signals on PowerSystemInputs |
| 110 | powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 111 | bus, |
| 112 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 113 | fmt::format( |
| 114 | "/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id), |
| 115 | POWERSYSINPUTS_INTERFACE), |
| 116 | [this](auto& msg) { this->powerSysInputsChangeEvent(msg); }); |
| 117 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 118 | determineStatusOfPower(); |
| 119 | |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 120 | std::variant<int> pgood = -1; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 121 | auto method = this->bus.new_method_call( |
| 122 | "org.openbmc.control.Power", "/org/openbmc/control/power0", |
| 123 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 124 | |
| 125 | method.append("org.openbmc.control.Power", "pgood"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 126 | try |
| 127 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 128 | auto reply = this->bus.call(method); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 129 | reply.read(pgood); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 130 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 131 | if (std::get<int>(pgood) == 1) |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 132 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 133 | info("Initial Chassis State will be On"); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 134 | server::Chassis::currentPowerState(PowerState::On); |
| 135 | server::Chassis::requestedPowerTransition(Transition::On); |
| 136 | return; |
| 137 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 138 | else |
| 139 | { |
| 140 | // The system is off. If we think it should be on then |
| 141 | // we probably lost AC while up, so set a new state |
| 142 | // change time. |
| 143 | uint64_t lastTime; |
| 144 | PowerState lastState; |
| 145 | |
| 146 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 147 | { |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 148 | // If power was on before the BMC reboot and the reboot reason |
| 149 | // was not a pinhole reset, log an error |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 150 | if (lastState == PowerState::On) |
| 151 | { |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 152 | info( |
| 153 | "Chassis power was on before the BMC reboot and it is off now"); |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 154 | |
| 155 | // Reset host sensors since system is off now |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 156 | startUnit(fmt::format(RESET_HOST_SENSORS_SVC_FMT, id)); |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 157 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 158 | setStateChangeTime(); |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 159 | |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 160 | // 0 indicates pinhole reset. 1 is NOT pinhole reset |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 161 | if (phosphor::state::manager::utils::getGpioValue( |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 162 | "reset-cause-pinhole") != 0) |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 163 | { |
| 164 | if (standbyVoltageRegulatorFault()) |
| 165 | { |
| 166 | report<Regulator>(); |
| 167 | } |
| 168 | else |
| 169 | { |
Mike Capps | 073fa2b | 2022-03-15 16:06:45 -0400 | [diff] [blame] | 170 | report<Blackout>(Entry::Level::Critical); |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 171 | } |
| 172 | } |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 173 | else |
| 174 | { |
| 175 | info("Pinhole reset"); |
| 176 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 180 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 181 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 182 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 183 | // It's acceptable for the pgood state service to not be available |
| 184 | // since it will notify us of the pgood state when it comes up. |
| 185 | if (e.name() != nullptr && |
| 186 | strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0) |
| 187 | { |
| 188 | goto fail; |
| 189 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 190 | |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 191 | // Only log for unexpected error types. |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 192 | error("Error performing call to get pgood: {ERROR}", "ERROR", e); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 193 | goto fail; |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 194 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 195 | |
| 196 | fail: |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 197 | info("Initial Chassis State will be Off"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 198 | server::Chassis::currentPowerState(PowerState::Off); |
| 199 | server::Chassis::requestedPowerTransition(Transition::Off); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 200 | |
| 201 | return; |
| 202 | } |
| 203 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 204 | void Chassis::determineStatusOfPower() |
| 205 | { |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 206 | // Default PowerStatus to good |
| 207 | server::Chassis::currentPowerStatus(PowerStatus::Good); |
| 208 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 209 | determineStatusOfUPSPower(); |
| 210 | if (server::Chassis::currentPowerStatus() != PowerStatus::Good) |
| 211 | { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | determineStatusOfPSUPower(); |
| 216 | } |
| 217 | |
| 218 | void Chassis::determineStatusOfUPSPower() |
| 219 | { |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 220 | // Find all implementations of the UPower interface |
| 221 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 222 | MAPPER_INTERFACE, "GetSubTree"); |
| 223 | |
| 224 | mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE})); |
| 225 | |
| 226 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 227 | mapperResponse; |
| 228 | |
| 229 | try |
| 230 | { |
| 231 | auto mapperResponseMsg = bus.call(mapper); |
| 232 | mapperResponseMsg.read(mapperResponse); |
| 233 | } |
| 234 | catch (const sdbusplus::exception::exception& e) |
| 235 | { |
| 236 | error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e); |
| 237 | throw; |
| 238 | } |
| 239 | |
| 240 | if (mapperResponse.empty()) |
| 241 | { |
| 242 | debug("No UPower devices found in system"); |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Iterate through all returned Upower interfaces and look for UPS's |
| 246 | for (const auto& [path, services] : mapperResponse) |
| 247 | { |
| 248 | for (const auto& serviceIter : services) |
| 249 | { |
| 250 | const std::string& service = serviceIter.first; |
| 251 | |
| 252 | try |
| 253 | { |
| 254 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 255 | PROPERTY_INTERFACE, "GetAll"); |
| 256 | method.append(UPOWER_INTERFACE); |
| 257 | |
| 258 | auto response = bus.call(method); |
| 259 | using Property = std::string; |
| 260 | using Value = std::variant<bool, uint>; |
| 261 | using PropertyMap = std::map<Property, Value>; |
| 262 | PropertyMap properties; |
| 263 | response.read(properties); |
| 264 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 265 | if (std::get<uint>(properties["Type"]) != TYPE_UPS) |
| 266 | { |
| 267 | info("UPower device {OBJ_PATH} is not a UPS device", |
| 268 | "OBJ_PATH", path); |
| 269 | continue; |
| 270 | } |
| 271 | |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 272 | if (std::get<bool>(properties["IsPresent"]) != true) |
| 273 | { |
| 274 | // There is a UPS detected but it is not officially |
| 275 | // "present" yet. Monitor it for state change. |
| 276 | info("UPower device {OBJ_PATH} is not present", "OBJ_PATH", |
| 277 | path); |
| 278 | continue; |
| 279 | } |
| 280 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 281 | if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED) |
| 282 | { |
| 283 | info("UPS is fully charged"); |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE", |
| 288 | std::get<uint>(properties["State"])); |
| 289 | server::Chassis::currentPowerStatus( |
| 290 | PowerStatus::UninterruptiblePowerSupply); |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | if (std::get<uint>(properties["BatteryLevel"]) == |
| 295 | BATTERY_LVL_FULL) |
| 296 | { |
| 297 | info("UPS Battery Level is Full"); |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 298 | // Only one UPS per system, we've found it and it's all |
| 299 | // good so exit function |
| 300 | return; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
| 304 | info("UPS Battery Level is Low: {UPS_BAT_LEVEL}", |
| 305 | "UPS_BAT_LEVEL", |
| 306 | std::get<uint>(properties["BatteryLevel"])); |
| 307 | server::Chassis::currentPowerStatus( |
| 308 | PowerStatus::UninterruptiblePowerSupply); |
| 309 | return; |
| 310 | } |
| 311 | } |
| 312 | catch (const sdbusplus::exception::exception& e) |
| 313 | { |
| 314 | error("Error reading UPS property, error: {ERROR}, " |
| 315 | "service: {SERVICE} path: {PATH}", |
| 316 | "ERROR", e, "SERVICE", service, "PATH", path); |
| 317 | throw; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | return; |
| 322 | } |
| 323 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 324 | void Chassis::determineStatusOfPSUPower() |
| 325 | { |
| 326 | // Find all implementations of the PowerSystemInputs interface |
| 327 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 328 | MAPPER_INTERFACE, "GetSubTree"); |
| 329 | |
| 330 | mapper.append("/", 0, std::vector<std::string>({POWERSYSINPUTS_INTERFACE})); |
| 331 | |
| 332 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 333 | mapperResponse; |
| 334 | |
| 335 | try |
| 336 | { |
| 337 | auto mapperResponseMsg = bus.call(mapper); |
| 338 | mapperResponseMsg.read(mapperResponse); |
| 339 | } |
| 340 | catch (const sdbusplus::exception::exception& e) |
| 341 | { |
| 342 | error("Error in mapper GetSubTree call for PowerSystemInputs: {ERROR}", |
| 343 | "ERROR", e); |
| 344 | throw; |
| 345 | } |
| 346 | |
| 347 | for (const auto& [path, services] : mapperResponse) |
| 348 | { |
| 349 | for (const auto& serviceIter : services) |
| 350 | { |
| 351 | const std::string& service = serviceIter.first; |
| 352 | |
| 353 | try |
| 354 | { |
| 355 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 356 | PROPERTY_INTERFACE, "GetAll"); |
| 357 | method.append(POWERSYSINPUTS_INTERFACE); |
| 358 | |
| 359 | auto response = bus.call(method); |
| 360 | using Property = std::string; |
| 361 | using Value = std::variant<std::string>; |
| 362 | using PropertyMap = std::map<Property, Value>; |
| 363 | PropertyMap properties; |
| 364 | response.read(properties); |
| 365 | |
| 366 | auto statusStr = std::get<std::string>(properties["Status"]); |
| 367 | auto status = |
| 368 | decoratorServer::PowerSystemInputs::convertStatusFromString( |
| 369 | statusStr); |
| 370 | |
| 371 | if (status == decoratorServer::PowerSystemInputs::Status::Fault) |
| 372 | { |
| 373 | info("Power System Inputs status is in Fault state"); |
| 374 | server::Chassis::currentPowerStatus(PowerStatus::BrownOut); |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | catch (const sdbusplus::exception::exception& e) |
| 379 | { |
| 380 | error( |
| 381 | "Error reading Power System Inputs property, error: {ERROR}, " |
| 382 | "service: {SERVICE} path: {PATH}", |
| 383 | "ERROR", e, "SERVICE", service, "PATH", path); |
| 384 | throw; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return; |
| 389 | } |
| 390 | |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 391 | void Chassis::uPowerChangeEvent(sdbusplus::message::message& msg) |
| 392 | { |
| 393 | debug("UPS Property Change Event Triggered"); |
| 394 | std::string statusInterface; |
| 395 | std::map<std::string, std::variant<uint, bool>> msgData; |
| 396 | msg.read(statusInterface, msgData); |
| 397 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 398 | // If the change is to any of the properties we are interested in, then call |
| 399 | // determineStatusOfPower(), which looks at all the power-related |
| 400 | // interfaces, to see if a power status change is needed |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 401 | auto propertyMap = msgData.find("IsPresent"); |
| 402 | if (propertyMap != msgData.end()) |
| 403 | { |
| 404 | info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO", |
| 405 | std::get<bool>(propertyMap->second)); |
| 406 | determineStatusOfPower(); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | propertyMap = msgData.find("State"); |
| 411 | if (propertyMap != msgData.end()) |
| 412 | { |
| 413 | info("UPS State changed to {UPS_STATE}", "UPS_STATE", |
| 414 | std::get<uint>(propertyMap->second)); |
| 415 | determineStatusOfPower(); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | propertyMap = msgData.find("BatteryLevel"); |
| 420 | if (propertyMap != msgData.end()) |
| 421 | { |
| 422 | info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL", |
| 423 | std::get<uint>(propertyMap->second)); |
| 424 | determineStatusOfPower(); |
| 425 | return; |
| 426 | } |
| 427 | return; |
| 428 | } |
| 429 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame^] | 430 | void Chassis::powerSysInputsChangeEvent(sdbusplus::message::message& msg) |
| 431 | { |
| 432 | debug("Power System Inputs Property Change Event Triggered"); |
| 433 | std::string statusInterface; |
| 434 | std::map<std::string, std::variant<std::string>> msgData; |
| 435 | msg.read(statusInterface, msgData); |
| 436 | |
| 437 | // If the change is to any of the properties we are interested in, then call |
| 438 | // determineStatusOfPower(), which looks at all the power-related |
| 439 | // interfaces, to see if a power status change is needed |
| 440 | auto propertyMap = msgData.find("Status"); |
| 441 | if (propertyMap != msgData.end()) |
| 442 | { |
| 443 | info("Power System Inputs status changed to {POWER_SYS_INPUT_STATUS}", |
| 444 | "POWER_SYS_INPUT_STATUS", |
| 445 | std::get<std::string>(propertyMap->second)); |
| 446 | determineStatusOfPower(); |
| 447 | return; |
| 448 | } |
| 449 | return; |
| 450 | } |
| 451 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 452 | void Chassis::startUnit(const std::string& sysdUnit) |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 453 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 454 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 455 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 456 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 457 | method.append(sysdUnit); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 458 | method.append("replace"); |
| 459 | |
| 460 | this->bus.call_noreply(method); |
| 461 | |
| 462 | return; |
| 463 | } |
| 464 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 465 | bool Chassis::stateActive(const std::string& target) |
| 466 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 467 | std::variant<std::string> currentState; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 468 | sdbusplus::message::object_path unitTargetPath; |
| 469 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 470 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 471 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 472 | |
| 473 | method.append(target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 474 | |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 475 | try |
| 476 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 477 | auto result = this->bus.call(method); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 478 | result.read(unitTargetPath); |
| 479 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 480 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 481 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 482 | error("Error in GetUnit call: {ERROR}", "ERROR", e); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 483 | return false; |
| 484 | } |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 485 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 486 | method = this->bus.new_method_call( |
| 487 | SYSTEMD_SERVICE, |
| 488 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 489 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 490 | |
| 491 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 492 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 493 | try |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 494 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 495 | auto result = this->bus.call(method); |
| 496 | result.read(currentState); |
| 497 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 498 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 499 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 500 | error("Error in ActiveState Get: {ERROR}", "ERROR", e); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 501 | return false; |
| 502 | } |
| 503 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 504 | const auto& currentStateStr = std::get<std::string>(currentState); |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 505 | return currentStateStr == ACTIVE_STATE || |
| 506 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 507 | } |
| 508 | |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 509 | int Chassis::sysStateChange(sdbusplus::message::message& msg) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 510 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 511 | sdbusplus::message::object_path newStateObjPath; |
| 512 | std::string newStateUnit{}; |
| 513 | std::string newStateResult{}; |
| 514 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 515 | // Read the msg and populate each variable |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 516 | try |
| 517 | { |
Andrew Geissler | 9b8af4f | 2019-09-12 14:19:14 -0500 | [diff] [blame] | 518 | // newStateID is a throwaway that is needed in order to read the |
| 519 | // parameters that are useful out of the dbus message |
| 520 | uint32_t newStateID{}; |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 521 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 522 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 523 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 524 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 525 | error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}", |
| 526 | "ERROR", e, "REPLY_SIG", msg.get_signature()); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 529 | |
Allen.Wang | c089562 | 2022-03-23 15:46:47 +0800 | [diff] [blame] | 530 | if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) && |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 531 | (newStateResult == "done") && |
| 532 | (!stateActive(systemdTargetTable[Transition::On]))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 533 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 534 | info("Received signal that power OFF is complete"); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 535 | this->currentPowerState(server::Chassis::PowerState::Off); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 536 | this->setStateChangeTime(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 537 | } |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 538 | else if ((newStateUnit == systemdTargetTable[Transition::On]) && |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 539 | (newStateResult == "done") && |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 540 | (stateActive(systemdTargetTable[Transition::On]))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 541 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 542 | info("Received signal that power ON is complete"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 543 | this->currentPowerState(server::Chassis::PowerState::On); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 544 | this->setStateChangeTime(); |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 545 | |
| 546 | // Remove temporary file which is utilized for scenarios where the |
| 547 | // BMC is rebooted while the chassis power is still on. |
| 548 | // This file is used to indicate to chassis related systemd services |
| 549 | // that the chassis is already on and they should skip running. |
| 550 | // Once the chassis state is back to on we can clear this file. |
| 551 | auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0); |
| 552 | size++; // null |
| 553 | std::unique_ptr<char[]> chassisFile(new char[size]); |
| 554 | std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0); |
| 555 | if (std::filesystem::exists(chassisFile.get())) |
| 556 | { |
| 557 | std::filesystem::remove(chassisFile.get()); |
| 558 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 559 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 564 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 565 | { |
| 566 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 567 | info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}", |
| 568 | "REQ_POWER_TRAN", value); |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 569 | startUnit(systemdTargetTable.find(value)->second); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 570 | return server::Chassis::requestedPowerTransition(value); |
| 571 | } |
| 572 | |
| 573 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 574 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 575 | PowerState chassisPowerState; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 576 | info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE", |
| 577 | value); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 578 | |
| 579 | chassisPowerState = server::Chassis::currentPowerState(value); |
shamim ali | 6d582a8 | 2022-03-15 18:19:32 +0530 | [diff] [blame] | 580 | if (chassisPowerState == PowerState::On) |
| 581 | { |
| 582 | pohTimer.resetRemaining(); |
| 583 | } |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 584 | return chassisPowerState; |
| 585 | } |
| 586 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 587 | uint32_t Chassis::pohCounter(uint32_t value) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 588 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 589 | if (value != pohCounter()) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 590 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 591 | ChassisInherit::pohCounter(value); |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 592 | serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 593 | } |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 594 | return pohCounter(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 595 | } |
| 596 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 597 | void Chassis::pohCallback() |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 598 | { |
| 599 | if (ChassisInherit::currentPowerState() == PowerState::On) |
| 600 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 601 | pohCounter(pohCounter() + 1); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 605 | void Chassis::restorePOHCounter() |
| 606 | { |
| 607 | uint32_t counter; |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 608 | if (!deserializePOH(POH_COUNTER_PERSIST_PATH, counter)) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 609 | { |
| 610 | // set to default value |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 611 | pohCounter(0); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 612 | } |
| 613 | else |
| 614 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 615 | pohCounter(counter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 619 | fs::path Chassis::serializePOH(const fs::path& path) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 620 | { |
| 621 | std::ofstream os(path.c_str(), std::ios::binary); |
| 622 | cereal::JSONOutputArchive oarchive(os); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 623 | oarchive(pohCounter()); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 624 | return path; |
| 625 | } |
| 626 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 627 | bool Chassis::deserializePOH(const fs::path& path, uint32_t& pohCounter) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 628 | { |
| 629 | try |
| 630 | { |
| 631 | if (fs::exists(path)) |
| 632 | { |
| 633 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 634 | cereal::JSONInputArchive iarchive(is); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 635 | iarchive(pohCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 636 | return true; |
| 637 | } |
| 638 | return false; |
| 639 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 640 | catch (const cereal::Exception& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 641 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 642 | error("deserialize exception: {ERROR}", "ERROR", e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 643 | fs::remove(path); |
| 644 | return false; |
| 645 | } |
| 646 | catch (const fs::filesystem_error& e) |
| 647 | { |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | void Chassis::startPOHCounter() |
| 655 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 656 | auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path(); |
| 657 | fs::create_directories(dir); |
| 658 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 659 | try |
| 660 | { |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 661 | auto event = sdeventplus::Event::get_default(); |
| 662 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 663 | event.loop(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 664 | } |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 665 | catch (const sdeventplus::SdEventError& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 666 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 667 | error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR", |
| 668 | e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 669 | phosphor::logging::commit<InternalFailure>(); |
| 670 | } |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 671 | } |
| 672 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 673 | void Chassis::serializeStateChangeTime() |
| 674 | { |
| 675 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 676 | std::ofstream os(path.c_str(), std::ios::binary); |
| 677 | cereal::JSONOutputArchive oarchive(os); |
| 678 | |
| 679 | oarchive(ChassisInherit::lastStateChangeTime(), |
| 680 | ChassisInherit::currentPowerState()); |
| 681 | } |
| 682 | |
| 683 | bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state) |
| 684 | { |
| 685 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 686 | |
| 687 | try |
| 688 | { |
| 689 | if (fs::exists(path)) |
| 690 | { |
| 691 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 692 | cereal::JSONInputArchive iarchive(is); |
| 693 | iarchive(time, state); |
| 694 | return true; |
| 695 | } |
| 696 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 697 | catch (const std::exception& e) |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 698 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 699 | error("deserialize exception: {ERROR}", "ERROR", e); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 700 | fs::remove(path); |
| 701 | } |
| 702 | |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | void Chassis::restoreChassisStateChangeTime() |
| 707 | { |
| 708 | uint64_t time; |
| 709 | PowerState state; |
| 710 | |
| 711 | if (!deserializeStateChangeTime(time, state)) |
| 712 | { |
| 713 | ChassisInherit::lastStateChangeTime(0); |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | ChassisInherit::lastStateChangeTime(time); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | void Chassis::setStateChangeTime() |
| 722 | { |
| 723 | using namespace std::chrono; |
| 724 | uint64_t lastTime; |
| 725 | PowerState lastState; |
| 726 | |
| 727 | auto now = |
| 728 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()) |
| 729 | .count(); |
| 730 | |
| 731 | // If power is on when the BMC is rebooted, this function will get called |
| 732 | // because sysStateChange() runs. Since the power state didn't change |
| 733 | // in this case, neither should the state change time, so check that |
| 734 | // the power state actually did change here. |
| 735 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 736 | { |
| 737 | if (lastState == ChassisInherit::currentPowerState()) |
| 738 | { |
| 739 | return; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | ChassisInherit::lastStateChangeTime(now); |
| 744 | serializeStateChangeTime(); |
| 745 | } |
| 746 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 747 | bool Chassis::standbyVoltageRegulatorFault() |
| 748 | { |
| 749 | bool regulatorFault = false; |
| 750 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 751 | // find standby voltage regulator fault via gpiog |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 752 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 753 | auto gpioval = utils::getGpioValue("regulator-standby-faulted"); |
| 754 | |
| 755 | if (-1 == gpioval) |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 756 | { |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 757 | error("Failed reading regulator-standby-faulted GPIO"); |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 758 | } |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 759 | |
| 760 | if (1 == gpioval) |
| 761 | { |
| 762 | info("Detected standby voltage regulator fault"); |
| 763 | regulatorFault = true; |
| 764 | } |
| 765 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 766 | return regulatorFault; |
| 767 | } |
| 768 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 769 | } // namespace manager |
| 770 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 771 | } // namespace phosphor |