blob: bf3fbddf4fec888adf45ce7f9e58bdeeb13d00bd [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
Patrick Williams5adc2d42022-04-07 15:53:44 -050021namespace
22{
23using SoftPowerOffInherit = sdbusplus::server::object_t<Base::SoftPowerOff>;
24}
25
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053026/** @class SoftPowerOff
27 * @brief Responsible for coordinating Host SoftPowerOff operation
28 */
Patrick Williams5adc2d42022-04-07 15:53:44 -050029class SoftPowerOff : public SoftPowerOffInherit
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053030{
Patrick Venture0b02be92018-08-31 11:55:55 -070031 public:
32 /** @brief Constructs SoftPowerOff object.
33 *
34 * @param[in] bus - system dbus handler
35 * @param[in] event - sd_event handler
36 * @param[in] objPath - The Dbus path hosting SoftPowerOff function
37 */
38 SoftPowerOff(sdbusplus::bus::bus& bus, sd_event* event,
39 const char* objPath) :
Patrick Williams5adc2d42022-04-07 15:53:44 -050040 SoftPowerOffInherit(bus, objPath,
41 SoftPowerOffInherit::action::defer_emit),
Patrick Venture0b02be92018-08-31 11:55:55 -070042 bus(bus), timer(event),
43 hostControlSignal(
44 bus,
45 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
46 sdbusRule::path("/xyz/openbmc_project/control/host0") +
47 sdbusRule::interface(CONTROL_HOST_BUSNAME) +
48 sdbusRule::argN(0, convertForMessage(Host::Command::SoftOff)),
49 std::bind(std::mem_fn(&SoftPowerOff::hostControlEvent), this,
50 std::placeholders::_1))
51 {
52 // Need to announce since we may get the response
53 // very quickly on host shutdown command
54 emit_object_added();
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053055
Patrick Venture0b02be92018-08-31 11:55:55 -070056 // The whole purpose of this application is to send a host shutdown
57 // command and watch for the soft power off to go through. We need
58 // the interface added signal emitted before we send the shutdown
59 // command just to attend to lightning fast response from host
60 sendHostShutDownCmd();
61 }
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053062
Patrick Venture0b02be92018-08-31 11:55:55 -070063 /** @brief Tells if the objective of this application is completed */
64 inline auto isCompleted()
65 {
66 return completed;
67 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053068
Patrick Venture0b02be92018-08-31 11:55:55 -070069 /** @brief Tells if the referenced timer is expired or not */
70 inline auto isTimerExpired()
71 {
72 return timer.isExpired();
73 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053074
Patrick Venture0b02be92018-08-31 11:55:55 -070075 /** @brief overloaded property setter function
76 *
77 * @param[in] value - One of SoftOffReceived / HostShutdown
78 *
79 * @return Success or exception thrown
80 */
81 HostResponse responseReceived(HostResponse value) override;
Vishwanatha Subbanna31272b82017-02-01 18:53:14 +053082
Patrick Venture0b02be92018-08-31 11:55:55 -070083 /** @brief Using the base class's getter method */
84 using Base::SoftPowerOff::responseReceived;
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053085
Patrick Venture0b02be92018-08-31 11:55:55 -070086 /** @brief Calls to start a timer
87 *
88 * @param[in] usec - Time in microseconds
89 *
90 * @return Success or exception thrown
91 */
92 int startTimer(const std::chrono::microseconds& usec);
Vishwanatha Subbanna917454b2017-02-24 00:16:05 +053093
Patrick Venture0b02be92018-08-31 11:55:55 -070094 private:
95 // Need this to send SMS_ATTN
96 // TODO : Switch over to using mapper service in a different patch
97 static constexpr auto HOST_IPMI_BUS = "org.openbmc.HostIpmi";
98 static constexpr auto HOST_IPMI_OBJ = "/org/openbmc/HostIpmi/1";
99 static constexpr auto HOST_IPMI_INTF = "org.openbmc.HostIpmi";
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530100
Patrick Venture0b02be92018-08-31 11:55:55 -0700101 /* @brief sdbusplus handle */
102 sdbusplus::bus::bus& bus;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530103
Patrick Venture0b02be92018-08-31 11:55:55 -0700104 /** @brief Reference to Timer object */
105 Timer timer;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530106
Patrick Venture0b02be92018-08-31 11:55:55 -0700107 /** @brief Marks the end of life of this application.
108 *
109 * This is set to true if host gives appropriate responses
110 * for the sequence of commands.
111 */
112 bool completed = false;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530113
Patrick Venture0b02be92018-08-31 11:55:55 -0700114 /** @brief Subscribe to host control signals
115 *
116 * Protocol is to send the host power off request to the host
117 * control interface and then wait for a signal indicating pass/fail
118 **/
119 sdbusplus::bus::match_t hostControlSignal;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500120
Patrick Venture0b02be92018-08-31 11:55:55 -0700121 /** @brief Sends host control command to tell host to shut down
122 *
123 * After sending the command, wait for a signal indicating the status
124 * of the command.
125 *
126 * After receiving the initial response, start a timer for 30 minutes
127 * to let host do a clean shutdown of partitions. When the response is
128 * received from the host, it indicates that BMC can do a power off.
129 * If BMC fails to get any response, then a hard power off would
130 * be forced.
131 *
132 * @return - Does not return anything. Error will result in exception
133 * being thrown
134 */
135 void sendHostShutDownCmd();
Andrew Geisslerf0f496c2017-05-02 13:54:35 -0500136
Patrick Venture0b02be92018-08-31 11:55:55 -0700137 /** @brief Callback function on host control signals
138 *
139 * @param[in] msg - Data associated with subscribed signal
140 *
141 */
142 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +0530143};
144} // namespace ipmi
145} // namespace phosphor