blob: 128672f3ec330ad4514b4cf64def3e46ed9bfb62 [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
3#include "chassis_state_manager.hpp"
4
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -06005#include "utils.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05006#include "xyz/openbmc_project/Common/error.hpp"
7#include "xyz/openbmc_project/State/Shutdown/Power/error.hpp"
8
9#include <cereal/archives/json.hpp>
10#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050011#include <phosphor-logging/lg2.hpp>
Andrew Geissler0029a5d2017-01-24 14:48:35 -060012#include <sdbusplus/bus.hpp>
William A. Kennington III09568ff2018-05-11 00:03:12 -070013#include <sdbusplus/exception.hpp>
William A. Kennington IIId998f822018-10-17 23:17:57 -070014#include <sdeventplus/event.hpp>
15#include <sdeventplus/exception.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050016
Andrew Geisslerf2b22e82021-03-12 14:47:03 -060017#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050018#include <fstream>
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050019
Andrew Geisslera90a31a2016-12-13 16:16:28 -060020namespace phosphor
21{
22namespace state
23{
24namespace manager
25{
26
Andrew Geissler8ffdb262021-09-20 15:25:19 -050027PHOSPHOR_LOG2_USING;
28
Andrew Geisslera90a31a2016-12-13 16:16:28 -060029// When you see server:: you know we're referencing our base class
30namespace server = sdbusplus::xyz::openbmc_project::State::server;
31
32using namespace phosphor::logging;
William A. Kennington IIId998f822018-10-17 23:17:57 -070033using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Aatir Manzur27115ae2019-07-23 16:25:38 -050034using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout;
Ben Tyner2c36e5a2021-07-12 14:56:49 -050035using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator;
Josh D. King6838ea92017-04-11 13:39:18 -050036constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target";
Andrew Geissler58a18012018-01-19 19:36:05 -080037constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT =
38 "obmc-chassis-hard-poweroff@0.target";
Josh D. King6838ea92017-04-11 13:39:18 -050039constexpr auto CHASSIS_STATE_POWERON_TGT = "obmc-chassis-poweron@0.target";
Matthew Barthbe6efab2022-03-01 13:21:45 -060040constexpr auto RESET_HOST_SENSORS_SVC =
41 "phosphor-reset-sensor-states@0.service";
Andrew Geissler0029a5d2017-01-24 14:48:35 -060042
Josh D. King697474c2017-03-02 11:15:55 -060043constexpr auto ACTIVE_STATE = "active";
44constexpr auto ACTIVATING_STATE = "activating";
45
Andrew Geissler2cf2a262022-02-02 14:38:41 -060046// Details at https://upower.freedesktop.org/docs/Device.html
47constexpr uint TYPE_UPS = 3;
48constexpr uint STATE_FULLY_CHARGED = 4;
49constexpr uint BATTERY_LVL_FULL = 8;
50
Andrew Geisslerce80f242017-01-24 13:25:33 -060051/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080052const std::map<server::Chassis::Transition, std::string> SYSTEMD_TARGET_TABLE =
53 {
Andrew Geissler8cf2f9a2017-07-21 11:58:04 -050054 // Use the hard off target to ensure we shutdown immediately
55 {server::Chassis::Transition::Off, CHASSIS_STATE_HARD_POWEROFF_TGT},
Andrew Geissler58a18012018-01-19 19:36:05 -080056 {server::Chassis::Transition::On, CHASSIS_STATE_POWERON_TGT}};
Andrew Geisslerce80f242017-01-24 13:25:33 -060057
Andrew Geissler58a18012018-01-19 19:36:05 -080058constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
59constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geisslerce80f242017-01-24 13:25:33 -060060constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
61
Josh D. King697474c2017-03-02 11:15:55 -060062constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
63constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
64
Andrew Geissler8b1f8622022-01-28 16:37:07 -060065constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
66constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
67constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
68constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device";
69constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
70
Andrew Geissler0029a5d2017-01-24 14:48:35 -060071void Chassis::subscribeToSystemdSignals()
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060072{
Andrew Geissler3a30b052019-05-14 15:54:37 -050073 try
74 {
75 auto method = this->bus.new_method_call(
76 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe");
Andrew Geissler5b950272019-05-24 12:27:51 -050077 this->bus.call(method);
Andrew Geissler3a30b052019-05-14 15:54:37 -050078 }
Andrew Geissler8ffdb262021-09-20 15:25:19 -050079 catch (const sdbusplus::exception::exception& e)
Andrew Geissler3a30b052019-05-14 15:54:37 -050080 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050081 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler3a30b052019-05-14 15:54:37 -050082 elog<InternalFailure>();
83 }
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060084
Andrew Geissler0029a5d2017-01-24 14:48:35 -060085 return;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060086}
87
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060088// TODO - Will be rewritten once sdbusplus client bindings are in place
89// and persistent storage design is in place and sdbusplus
90// has read property function
91void Chassis::determineInitialState()
92{
Andrew Geissler2cf2a262022-02-02 14:38:41 -060093
94 // Monitor for any properties changed signals on UPower device path
95 uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
96 bus,
97 sdbusplus::bus::match::rules::propertiesChangedNamespace(
98 "/org/freedesktop/UPower", UPOWER_INTERFACE),
99 [this](auto& msg) { this->uPowerChangeEvent(msg); });
100
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600101 determineStatusOfPower();
102
Patrick Williams2975e262020-05-13 18:01:09 -0500103 std::variant<int> pgood = -1;
Andrew Geissler58a18012018-01-19 19:36:05 -0800104 auto method = this->bus.new_method_call(
105 "org.openbmc.control.Power", "/org/openbmc/control/power0",
106 "org.freedesktop.DBus.Properties", "Get");
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600107
108 method.append("org.openbmc.control.Power", "pgood");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700109 try
110 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700111 auto reply = this->bus.call(method);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500112 reply.read(pgood);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700113
Patrick Williams37413dc2020-05-13 11:29:54 -0500114 if (std::get<int>(pgood) == 1)
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700115 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500116 info("Initial Chassis State will be On");
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700117 server::Chassis::currentPowerState(PowerState::On);
118 server::Chassis::requestedPowerTransition(Transition::On);
119 return;
120 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500121 else
122 {
123 // The system is off. If we think it should be on then
124 // we probably lost AC while up, so set a new state
125 // change time.
126 uint64_t lastTime;
127 PowerState lastState;
128
129 if (deserializeStateChangeTime(lastTime, lastState))
130 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600131 // If power was on before the BMC reboot and the reboot reason
132 // was not a pinhole reset, log an error
Matt Spinler9eab9862018-07-11 14:13:52 -0500133 if (lastState == PowerState::On)
134 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600135 info(
136 "Chassis power was on before the BMC reboot and it is off now");
Matthew Barthbe6efab2022-03-01 13:21:45 -0600137
138 // Reset host sensors since system is off now
139 startUnit(RESET_HOST_SENSORS_SVC);
140
Matt Spinler9eab9862018-07-11 14:13:52 -0500141 setStateChangeTime();
Andrew Geissler7ed36232022-01-26 13:49:28 -0600142
143 if (phosphor::state::manager::utils::getGpioValue(
144 "reset-cause-pinhole") != 1)
145 {
146 if (standbyVoltageRegulatorFault())
147 {
148 report<Regulator>();
149 }
150 else
151 {
152 report<Blackout>();
153 }
154 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500155 }
156 }
157 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700158 }
Patrick Williams0a675212021-09-02 09:49:43 -0500159 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700160 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700161 // It's acceptable for the pgood state service to not be available
162 // since it will notify us of the pgood state when it comes up.
163 if (e.name() != nullptr &&
164 strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0)
165 {
166 goto fail;
167 }
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600168
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700169 // Only log for unexpected error types.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500170 error("Error performing call to get pgood: {ERROR}", "ERROR", e);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700171 goto fail;
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600172 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700173
174fail:
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500175 info("Initial Chassis State will be Off");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700176 server::Chassis::currentPowerState(PowerState::Off);
177 server::Chassis::requestedPowerTransition(Transition::Off);
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600178
179 return;
180}
181
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600182void Chassis::determineStatusOfPower()
183{
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600184 // Default PowerStatus to good
185 server::Chassis::currentPowerStatus(PowerStatus::Good);
186
187 // Find all implementations of the UPower interface
188 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
189 MAPPER_INTERFACE, "GetSubTree");
190
191 mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE}));
192
193 std::map<std::string, std::map<std::string, std::vector<std::string>>>
194 mapperResponse;
195
196 try
197 {
198 auto mapperResponseMsg = bus.call(mapper);
199 mapperResponseMsg.read(mapperResponse);
200 }
201 catch (const sdbusplus::exception::exception& e)
202 {
203 error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e);
204 throw;
205 }
206
207 if (mapperResponse.empty())
208 {
209 debug("No UPower devices found in system");
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600210 }
211
212 // Iterate through all returned Upower interfaces and look for UPS's
213 for (const auto& [path, services] : mapperResponse)
214 {
215 for (const auto& serviceIter : services)
216 {
217 const std::string& service = serviceIter.first;
218
219 try
220 {
221 auto method = bus.new_method_call(service.c_str(), path.c_str(),
222 PROPERTY_INTERFACE, "GetAll");
223 method.append(UPOWER_INTERFACE);
224
225 auto response = bus.call(method);
226 using Property = std::string;
227 using Value = std::variant<bool, uint>;
228 using PropertyMap = std::map<Property, Value>;
229 PropertyMap properties;
230 response.read(properties);
231
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600232 if (std::get<uint>(properties["Type"]) != TYPE_UPS)
233 {
234 info("UPower device {OBJ_PATH} is not a UPS device",
235 "OBJ_PATH", path);
236 continue;
237 }
238
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600239 if (std::get<bool>(properties["IsPresent"]) != true)
240 {
241 // There is a UPS detected but it is not officially
242 // "present" yet. Monitor it for state change.
243 info("UPower device {OBJ_PATH} is not present", "OBJ_PATH",
244 path);
245 continue;
246 }
247
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600248 if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED)
249 {
250 info("UPS is fully charged");
251 }
252 else
253 {
254 info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE",
255 std::get<uint>(properties["State"]));
256 server::Chassis::currentPowerStatus(
257 PowerStatus::UninterruptiblePowerSupply);
258 return;
259 }
260
261 if (std::get<uint>(properties["BatteryLevel"]) ==
262 BATTERY_LVL_FULL)
263 {
264 info("UPS Battery Level is Full");
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600265 // Only one UPS per system, we've found it and it's all
266 // good so exit function
267 return;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600268 }
269 else
270 {
271 info("UPS Battery Level is Low: {UPS_BAT_LEVEL}",
272 "UPS_BAT_LEVEL",
273 std::get<uint>(properties["BatteryLevel"]));
274 server::Chassis::currentPowerStatus(
275 PowerStatus::UninterruptiblePowerSupply);
276 return;
277 }
278 }
279 catch (const sdbusplus::exception::exception& e)
280 {
281 error("Error reading UPS property, error: {ERROR}, "
282 "service: {SERVICE} path: {PATH}",
283 "ERROR", e, "SERVICE", service, "PATH", path);
284 throw;
285 }
286 }
287 }
288 return;
289}
290
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600291void Chassis::uPowerChangeEvent(sdbusplus::message::message& msg)
292{
293 debug("UPS Property Change Event Triggered");
294 std::string statusInterface;
295 std::map<std::string, std::variant<uint, bool>> msgData;
296 msg.read(statusInterface, msgData);
297
298 // If the change is to any of the three properties we are interested in
299 // then call determineStatusOfPower() to see if a power status change
300 // is needed
301 auto propertyMap = msgData.find("IsPresent");
302 if (propertyMap != msgData.end())
303 {
304 info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO",
305 std::get<bool>(propertyMap->second));
306 determineStatusOfPower();
307 return;
308 }
309
310 propertyMap = msgData.find("State");
311 if (propertyMap != msgData.end())
312 {
313 info("UPS State changed to {UPS_STATE}", "UPS_STATE",
314 std::get<uint>(propertyMap->second));
315 determineStatusOfPower();
316 return;
317 }
318
319 propertyMap = msgData.find("BatteryLevel");
320 if (propertyMap != msgData.end())
321 {
322 info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL",
323 std::get<uint>(propertyMap->second));
324 determineStatusOfPower();
325 return;
326 }
327 return;
328}
329
Matthew Barthbe6efab2022-03-01 13:21:45 -0600330void Chassis::startUnit(const std::string& sysdUnit)
Andrew Geisslerce80f242017-01-24 13:25:33 -0600331{
Andrew Geissler58a18012018-01-19 19:36:05 -0800332 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
333 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geisslerce80f242017-01-24 13:25:33 -0600334
Matthew Barthbe6efab2022-03-01 13:21:45 -0600335 method.append(sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600336 method.append("replace");
337
338 this->bus.call_noreply(method);
339
340 return;
341}
342
Josh D. King697474c2017-03-02 11:15:55 -0600343bool Chassis::stateActive(const std::string& target)
344{
Patrick Williams2975e262020-05-13 18:01:09 -0500345 std::variant<std::string> currentState;
Josh D. King697474c2017-03-02 11:15:55 -0600346 sdbusplus::message::object_path unitTargetPath;
347
Andrew Geissler58a18012018-01-19 19:36:05 -0800348 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
349 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King697474c2017-03-02 11:15:55 -0600350
351 method.append(target);
Josh D. King697474c2017-03-02 11:15:55 -0600352
William A. Kennington III09568ff2018-05-11 00:03:12 -0700353 try
354 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500355 auto result = this->bus.call(method);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700356 result.read(unitTargetPath);
357 }
Patrick Williams0a675212021-09-02 09:49:43 -0500358 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700359 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500360 error("Error in GetUnit call: {ERROR}", "ERROR", e);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700361 return false;
362 }
Josh D. King697474c2017-03-02 11:15:55 -0600363
Andrew Geissler58a18012018-01-19 19:36:05 -0800364 method = this->bus.new_method_call(
365 SYSTEMD_SERVICE,
366 static_cast<const std::string&>(unitTargetPath).c_str(),
367 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King697474c2017-03-02 11:15:55 -0600368
369 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King697474c2017-03-02 11:15:55 -0600370
Anthony Wilson32c532e2018-10-25 21:56:07 -0500371 try
Josh D. King697474c2017-03-02 11:15:55 -0600372 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500373 auto result = this->bus.call(method);
374 result.read(currentState);
375 }
Patrick Williams0a675212021-09-02 09:49:43 -0500376 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500377 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500378 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King697474c2017-03-02 11:15:55 -0600379 return false;
380 }
381
Patrick Williams37413dc2020-05-13 11:29:54 -0500382 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800383 return currentStateStr == ACTIVE_STATE ||
384 currentStateStr == ACTIVATING_STATE;
Josh D. King697474c2017-03-02 11:15:55 -0600385}
386
Patrick Williams8f8ba392017-05-05 15:47:39 -0500387int Chassis::sysStateChange(sdbusplus::message::message& msg)
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600388{
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600389 sdbusplus::message::object_path newStateObjPath;
390 std::string newStateUnit{};
391 std::string newStateResult{};
392
Andrew Geissler58a18012018-01-19 19:36:05 -0800393 // Read the msg and populate each variable
William A. Kennington III09568ff2018-05-11 00:03:12 -0700394 try
395 {
Andrew Geissler9b8af4f2019-09-12 14:19:14 -0500396 // newStateID is a throwaway that is needed in order to read the
397 // parameters that are useful out of the dbus message
398 uint32_t newStateID{};
William A. Kennington III09568ff2018-05-11 00:03:12 -0700399 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
400 }
Patrick Williams0a675212021-09-02 09:49:43 -0500401 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700402 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500403 error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}",
404 "ERROR", e, "REPLY_SIG", msg.get_signature());
William A. Kennington III09568ff2018-05-11 00:03:12 -0700405 return 0;
406 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600407
Andrew Geissler58a18012018-01-19 19:36:05 -0800408 if ((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) &&
409 (newStateResult == "done") && (!stateActive(CHASSIS_STATE_POWERON_TGT)))
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600410 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500411 info("Received signal that power OFF is complete");
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600412 this->currentPowerState(server::Chassis::PowerState::Off);
Matt Spinler9eab9862018-07-11 14:13:52 -0500413 this->setStateChangeTime();
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600414 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800415 else if ((newStateUnit == CHASSIS_STATE_POWERON_TGT) &&
416 (newStateResult == "done") &&
417 (stateActive(CHASSIS_STATE_POWERON_TGT)))
418 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500419 info("Received signal that power ON is complete");
Andrew Geissler58a18012018-01-19 19:36:05 -0800420 this->currentPowerState(server::Chassis::PowerState::On);
Matt Spinler9eab9862018-07-11 14:13:52 -0500421 this->setStateChangeTime();
Andrew Geisslerf2b22e82021-03-12 14:47:03 -0600422
423 // Remove temporary file which is utilized for scenarios where the
424 // BMC is rebooted while the chassis power is still on.
425 // This file is used to indicate to chassis related systemd services
426 // that the chassis is already on and they should skip running.
427 // Once the chassis state is back to on we can clear this file.
428 auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
429 size++; // null
430 std::unique_ptr<char[]> chassisFile(new char[size]);
431 std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0);
432 if (std::filesystem::exists(chassisFile.get()))
433 {
434 std::filesystem::remove(chassisFile.get());
435 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800436 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600437
438 return 0;
439}
440
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600441Chassis::Transition Chassis::requestedPowerTransition(Transition value)
442{
443
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500444 info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}",
445 "REQ_POWER_TRAN", value);
Matthew Barthbe6efab2022-03-01 13:21:45 -0600446 startUnit(SYSTEMD_TARGET_TABLE.find(value)->second);
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600447 return server::Chassis::requestedPowerTransition(value);
448}
449
450Chassis::PowerState Chassis::currentPowerState(PowerState value)
451{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500452 PowerState chassisPowerState;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500453 info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE",
454 value);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500455
456 chassisPowerState = server::Chassis::currentPowerState(value);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500457 pohTimer.setEnabled(chassisPowerState == PowerState::On);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500458 return chassisPowerState;
459}
460
Patrick Williams45a1ed72021-04-30 21:02:43 -0500461uint32_t Chassis::pohCounter(uint32_t value)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500462{
Patrick Williams45a1ed72021-04-30 21:02:43 -0500463 if (value != pohCounter())
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500464 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500465 ChassisInherit::pohCounter(value);
Matt Spinler81957842018-07-11 10:37:12 -0500466 serializePOH();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500467 }
Patrick Williams45a1ed72021-04-30 21:02:43 -0500468 return pohCounter();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500469}
470
Patrick Williams45a1ed72021-04-30 21:02:43 -0500471void Chassis::pohCallback()
William A. Kennington IIId998f822018-10-17 23:17:57 -0700472{
473 if (ChassisInherit::currentPowerState() == PowerState::On)
474 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500475 pohCounter(pohCounter() + 1);
William A. Kennington IIId998f822018-10-17 23:17:57 -0700476 }
477}
478
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500479void Chassis::restorePOHCounter()
480{
481 uint32_t counter;
Matt Spinler81957842018-07-11 10:37:12 -0500482 if (!deserializePOH(POH_COUNTER_PERSIST_PATH, counter))
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500483 {
484 // set to default value
Patrick Williams45a1ed72021-04-30 21:02:43 -0500485 pohCounter(0);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500486 }
487 else
488 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500489 pohCounter(counter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500490 }
491}
492
Matt Spinler81957842018-07-11 10:37:12 -0500493fs::path Chassis::serializePOH(const fs::path& path)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500494{
495 std::ofstream os(path.c_str(), std::ios::binary);
496 cereal::JSONOutputArchive oarchive(os);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500497 oarchive(pohCounter());
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500498 return path;
499}
500
Patrick Williams45a1ed72021-04-30 21:02:43 -0500501bool Chassis::deserializePOH(const fs::path& path, uint32_t& pohCounter)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500502{
503 try
504 {
505 if (fs::exists(path))
506 {
507 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
508 cereal::JSONInputArchive iarchive(is);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500509 iarchive(pohCounter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500510 return true;
511 }
512 return false;
513 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500514 catch (const cereal::Exception& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500515 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500516 error("deserialize exception: {ERROR}", "ERROR", e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500517 fs::remove(path);
518 return false;
519 }
520 catch (const fs::filesystem_error& e)
521 {
522 return false;
523 }
524
525 return false;
526}
527
528void Chassis::startPOHCounter()
529{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500530 auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path();
531 fs::create_directories(dir);
532
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500533 try
534 {
William A. Kennington IIId998f822018-10-17 23:17:57 -0700535 auto event = sdeventplus::Event::get_default();
536 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
537 event.loop();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500538 }
William A. Kennington IIId998f822018-10-17 23:17:57 -0700539 catch (const sdeventplus::SdEventError& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500540 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500541 error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR",
542 e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500543 phosphor::logging::commit<InternalFailure>();
544 }
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600545}
546
Matt Spinler9eab9862018-07-11 14:13:52 -0500547void Chassis::serializeStateChangeTime()
548{
549 fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH};
550 std::ofstream os(path.c_str(), std::ios::binary);
551 cereal::JSONOutputArchive oarchive(os);
552
553 oarchive(ChassisInherit::lastStateChangeTime(),
554 ChassisInherit::currentPowerState());
555}
556
557bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state)
558{
559 fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH};
560
561 try
562 {
563 if (fs::exists(path))
564 {
565 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
566 cereal::JSONInputArchive iarchive(is);
567 iarchive(time, state);
568 return true;
569 }
570 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500571 catch (const std::exception& e)
Matt Spinler9eab9862018-07-11 14:13:52 -0500572 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500573 error("deserialize exception: {ERROR}", "ERROR", e);
Matt Spinler9eab9862018-07-11 14:13:52 -0500574 fs::remove(path);
575 }
576
577 return false;
578}
579
580void Chassis::restoreChassisStateChangeTime()
581{
582 uint64_t time;
583 PowerState state;
584
585 if (!deserializeStateChangeTime(time, state))
586 {
587 ChassisInherit::lastStateChangeTime(0);
588 }
589 else
590 {
591 ChassisInherit::lastStateChangeTime(time);
592 }
593}
594
595void Chassis::setStateChangeTime()
596{
597 using namespace std::chrono;
598 uint64_t lastTime;
599 PowerState lastState;
600
601 auto now =
602 duration_cast<milliseconds>(system_clock::now().time_since_epoch())
603 .count();
604
605 // If power is on when the BMC is rebooted, this function will get called
606 // because sysStateChange() runs. Since the power state didn't change
607 // in this case, neither should the state change time, so check that
608 // the power state actually did change here.
609 if (deserializeStateChangeTime(lastTime, lastState))
610 {
611 if (lastState == ChassisInherit::currentPowerState())
612 {
613 return;
614 }
615 }
616
617 ChassisInherit::lastStateChangeTime(now);
618 serializeStateChangeTime();
619}
620
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500621bool Chassis::standbyVoltageRegulatorFault()
622{
623 bool regulatorFault = false;
624
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600625 // find standby voltage regulator fault via gpiog
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500626
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600627 auto gpioval = utils::getGpioValue("regulator-standby-faulted");
628
629 if (-1 == gpioval)
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500630 {
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600631 error("Failed reading regulator-standby-faulted GPIO");
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500632 }
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600633
634 if (1 == gpioval)
635 {
636 info("Detected standby voltage regulator fault");
637 regulatorFault = true;
638 }
639
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500640 return regulatorFault;
641}
642
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600643} // namespace manager
644} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700645} // namespace phosphor