blob: 1c9341b92e4ef5178dcbcd78841938f75df5595a [file] [log] [blame]
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05301#pragma once
2
Patrick Venture0b02be92018-08-31 11:55:55 -07003#include "config.h"
4
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <functional>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05306#include <sdbusplus/bus.hpp>
7#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -07008#include <sdbusplus/timer.hpp>
Andrew Geisslerf0f496c2017-05-02 13:54:35 -05009#include <xyz/openbmc_project/Control/Host/server.hpp>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053010#include <xyz/openbmc_project/Ipmi/Internal/SoftPowerOff/server.hpp>
11namespace phosphor
12{
13namespace ipmi
14{
15
16namespace Base = sdbusplus::xyz::openbmc_project::Ipmi::Internal::server;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050017using namespace sdbusplus::xyz::openbmc_project::Control::server;
18
19namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053020
21/** @class SoftPowerOff
22 * @brief Responsible for coordinating Host SoftPowerOff operation
23 */
Patrick Venture0b02be92018-08-31 11:55:55 -070024class SoftPowerOff
25 : public sdbusplus::server::object::object<Base::SoftPowerOff>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053026{
Patrick Venture0b02be92018-08-31 11:55:55 -070027 public:
28 /** @brief Constructs SoftPowerOff object.
29 *
30 * @param[in] bus - system dbus handler
31 * @param[in] event - sd_event handler
32 * @param[in] objPath - The Dbus path hosting SoftPowerOff function
33 */
34 SoftPowerOff(sdbusplus::bus::bus& bus, sd_event* event,
35 const char* objPath) :
36 sdbusplus::server::object::object<Base::SoftPowerOff>(bus, objPath,
37 false),
38 bus(bus), timer(event),
39 hostControlSignal(
40 bus,
41 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
42 sdbusRule::path("/xyz/openbmc_project/control/host0") +
43 sdbusRule::interface(CONTROL_HOST_BUSNAME) +
44 sdbusRule::argN(0, convertForMessage(Host::Command::SoftOff)),
45 std::bind(std::mem_fn(&SoftPowerOff::hostControlEvent), this,
46 std::placeholders::_1))
47 {
48 // Need to announce since we may get the response
49 // very quickly on host shutdown command
50 emit_object_added();
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053051
Patrick Venture0b02be92018-08-31 11:55:55 -070052 // The whole purpose of this application is to send a host shutdown
53 // command and watch for the soft power off to go through. We need
54 // the interface added signal emitted before we send the shutdown
55 // command just to attend to lightning fast response from host
56 sendHostShutDownCmd();
57 }
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053058
Patrick Venture0b02be92018-08-31 11:55:55 -070059 /** @brief Tells if the objective of this application is completed */
60 inline auto isCompleted()
61 {
62 return completed;
63 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053064
Patrick Venture0b02be92018-08-31 11:55:55 -070065 /** @brief Tells if the referenced timer is expired or not */
66 inline auto isTimerExpired()
67 {
68 return timer.isExpired();
69 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053070
Patrick Venture0b02be92018-08-31 11:55:55 -070071 /** @brief overloaded property setter function
72 *
73 * @param[in] value - One of SoftOffReceived / HostShutdown
74 *
75 * @return Success or exception thrown
76 */
77 HostResponse responseReceived(HostResponse value) override;
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053078
Patrick Venture0b02be92018-08-31 11:55:55 -070079 /** @brief Using the base class's getter method */
80 using Base::SoftPowerOff::responseReceived;
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053081
Patrick Venture0b02be92018-08-31 11:55:55 -070082 /** @brief Calls to start a timer
83 *
84 * @param[in] usec - Time in microseconds
85 *
86 * @return Success or exception thrown
87 */
88 int startTimer(const std::chrono::microseconds& usec);
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053089
Patrick Venture0b02be92018-08-31 11:55:55 -070090 private:
91 // Need this to send SMS_ATTN
92 // TODO : Switch over to using mapper service in a different patch
93 static constexpr auto HOST_IPMI_BUS = "org.openbmc.HostIpmi";
94 static constexpr auto HOST_IPMI_OBJ = "/org/openbmc/HostIpmi/1";
95 static constexpr auto HOST_IPMI_INTF = "org.openbmc.HostIpmi";
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053096
Patrick Venture0b02be92018-08-31 11:55:55 -070097 /* @brief sdbusplus handle */
98 sdbusplus::bus::bus& bus;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053099
Patrick Venture0b02be92018-08-31 11:55:55 -0700100 /** @brief Reference to Timer object */
101 Timer timer;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530102
Patrick Venture0b02be92018-08-31 11:55:55 -0700103 /** @brief Marks the end of life of this application.
104 *
105 * This is set to true if host gives appropriate responses
106 * for the sequence of commands.
107 */
108 bool completed = false;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530109
Patrick Venture0b02be92018-08-31 11:55:55 -0700110 /** @brief Subscribe to host control signals
111 *
112 * Protocol is to send the host power off request to the host
113 * control interface and then wait for a signal indicating pass/fail
114 **/
115 sdbusplus::bus::match_t hostControlSignal;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500116
Patrick Venture0b02be92018-08-31 11:55:55 -0700117 /** @brief Sends host control command to tell host to shut down
118 *
119 * After sending the command, wait for a signal indicating the status
120 * of the command.
121 *
122 * After receiving the initial response, start a timer for 30 minutes
123 * to let host do a clean shutdown of partitions. When the response is
124 * received from the host, it indicates that BMC can do a power off.
125 * If BMC fails to get any response, then a hard power off would
126 * be forced.
127 *
128 * @return - Does not return anything. Error will result in exception
129 * being thrown
130 */
131 void sendHostShutDownCmd();
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500132
Patrick Venture0b02be92018-08-31 11:55:55 -0700133 /** @brief Callback function on host control signals
134 *
135 * @param[in] msg - Data associated with subscribed signal
136 *
137 */
138 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +0530139};
140} // namespace ipmi
141} // namespace phosphor