blob: fb0c2a3f9d4104ccc29c2e4773ff78b157f8bc05 [file] [log] [blame]
Andrew Geisslera90a31a2016-12-13 16:16:28 -06001#include <log.hpp>
2#include "chassis_state_manager.hpp"
3
4namespace phosphor
5{
6namespace state
7{
8namespace manager
9{
10
11// When you see server:: you know we're referencing our base class
12namespace server = sdbusplus::xyz::openbmc_project::State::server;
13
14using namespace phosphor::logging;
15
Andrew Geisslerce80f242017-01-24 13:25:33 -060016/* Map a transition to it's systemd target */
17const std::map<server::Chassis::Transition,std::string> SYSTEMD_TARGET_TABLE =
18{
19 {server::Chassis::Transition::Off, "obmc-power-chassis-off@0.target"},
20 {server::Chassis::Transition::On, "obmc-power-chassis-on@0.target"}
21};
22
23constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
24constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
25constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
26
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060027/* TODO:Issue 774 - Use systemd target signals to control chassis state */
28int Chassis::handlePgoodOn(sd_bus_message* /*msg*/, void* usrData,
29 sd_bus_error* retError)
30{
31 log<level::INFO>("Pgood has turned on",
32 entry("CHASSIS_CURRENT_POWER_STATE=%s",
33 convertForMessage(PowerState::On).c_str()));
34 auto chassisInst = static_cast<Chassis*>(usrData);
35 chassisInst->currentPowerState(PowerState::On);
36
37 return 0;
38}
39
40int Chassis::handlePgoodOff(sd_bus_message* /*msg*/, void* usrData,
41 sd_bus_error* retError)
42{
43 log<level::INFO>("Pgood has turned off",
44 entry("CHASSIS_CURRENT_POWER_STATE=%s",
45 convertForMessage(PowerState::Off).c_str()));
46 auto chassisInst = static_cast<Chassis*>(usrData);
47 chassisInst->currentPowerState(PowerState::Off);
48
49 return 0;
50}
51
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060052// TODO - Will be rewritten once sdbusplus client bindings are in place
53// and persistent storage design is in place and sdbusplus
54// has read property function
55void Chassis::determineInitialState()
56{
57 sdbusplus::message::variant<int> pgood = -1;
58 auto method = this->bus.new_method_call("org.openbmc.control.Power",
59 "/org/openbmc/control/power0",
60 "org.freedesktop.DBus.Properties",
61 "Get");
62
63 method.append("org.openbmc.control.Power", "pgood");
64 auto reply = this->bus.call(method);
65 reply.read(pgood);
66
67 if(pgood == 1)
68 {
69 log<level::INFO>("Initial Chassis State will be On",
70 entry("CHASSIS_CURRENT_POWER_STATE=%s",
71 convertForMessage(PowerState::On).c_str()));
72 server::Chassis::currentPowerState(PowerState::On);
73 server::Chassis::requestedPowerTransition(Transition::On);
74 }
75 else
76 {
77 log<level::INFO>("Initial Chassis State will be Off",
78 entry("CHASSIS_CURRENT_POWER_STATE=%s",
79 convertForMessage(PowerState::Off).c_str()));
80 server::Chassis::currentPowerState(PowerState::Off);
81 server::Chassis::requestedPowerTransition(Transition::Off);
82 }
83
84 return;
85}
86
Andrew Geisslerce80f242017-01-24 13:25:33 -060087void Chassis::executeTransition(Transition tranReq)
88{
89 auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second;
90
91 auto method = this->bus.new_method_call(SYSTEMD_SERVICE,
92 SYSTEMD_OBJ_PATH,
93 SYSTEMD_INTERFACE,
94 "StartUnit");
95
96 method.append(sysdTarget);
97 method.append("replace");
98
99 this->bus.call_noreply(method);
100
101 return;
102}
103
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600104Chassis::Transition Chassis::requestedPowerTransition(Transition value)
105{
106
107 log<level::INFO>("Change to Chassis Requested Power State",
108 entry("CHASSIS_REQUESTED_POWER_STATE=%s",
109 convertForMessage(value).c_str()));
Andrew Geisslerce80f242017-01-24 13:25:33 -0600110 executeTransition(value);
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600111 return server::Chassis::requestedPowerTransition(value);
112}
113
114Chassis::PowerState Chassis::currentPowerState(PowerState value)
115{
116 log<level::INFO>("Change to Chassis Power State",
117 entry("CHASSIS_CURRENT_POWER_STATE=%s",
118 convertForMessage(value).c_str()));
119 return server::Chassis::currentPowerState(value);
120}
121
122} // namespace manager
123} // namespace state
124} // namepsace phosphor