blob: 14742e1812b0d4b275a41b5414ff04897b707e90 [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
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05305#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -07007#include <sdbusplus/timer.hpp>
Andrew Geisslerf0f496c2017-05-02 13:54:35 -05008#include <xyz/openbmc_project/Control/Host/server.hpp>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05309#include <xyz/openbmc_project/Ipmi/Internal/SoftPowerOff/server.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050010
11#include <functional>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053012namespace phosphor
13{
14namespace ipmi
15{
16
Willy Tu523e2d12023-09-05 11:36:48 -070017namespace Base = sdbusplus::server::xyz::openbmc_project::ipmi::internal;
18using namespace sdbusplus::server::xyz::openbmc_project::control;
Andrew Geisslerf0f496c2017-05-02 13:54:35 -050019
20namespace sdbusRule = sdbusplus::bus::match::rules;
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053021
Patrick Williams5adc2d42022-04-07 15:53:44 -050022namespace
23{
24using SoftPowerOffInherit = sdbusplus::server::object_t<Base::SoftPowerOff>;
25}
26
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053027/** @class SoftPowerOff
28 * @brief Responsible for coordinating Host SoftPowerOff operation
29 */
Patrick Williams5adc2d42022-04-07 15:53:44 -050030class SoftPowerOff : public SoftPowerOffInherit
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053031{
Patrick Venture0b02be92018-08-31 11:55:55 -070032 public:
33 /** @brief Constructs SoftPowerOff object.
34 *
35 * @param[in] bus - system dbus handler
36 * @param[in] event - sd_event handler
37 * @param[in] objPath - The Dbus path hosting SoftPowerOff function
38 */
Patrick Williams5d82f472022-07-22 19:26:53 -050039 SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event, 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 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500102 sdbusplus::bus_t& bus;
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +0530103
Patrick Venture0b02be92018-08-31 11:55:55 -0700104 /** @brief Reference to Timer object */
Patrick Williamsba0da6b2024-01-22 15:48:38 -0600105 sdbusplus::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 */
Patrick Williams5d82f472022-07-22 19:26:53 -0500142 void hostControlEvent(sdbusplus::message_t& msg);
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +0530143};
144} // namespace ipmi
145} // namespace phosphor