blob: 866784bb234b78f2888b7ca907bce110302214a6 [file] [log] [blame]
Chicago Duan184f6022020-04-17 11:30:49 +08001#pragma once
2
Manojkiran Eda3fcfaa12024-02-26 15:17:58 +05303#include "common/instance_id.hpp"
Rashmica Gupta1ed5f7a2023-05-22 13:56:42 +10004#include "common/transport.hpp"
Chicago Duan184f6022020-04-17 11:30:49 +08005#include "common/types.hpp"
6
Sagar Srinivas4d99c312024-03-28 06:50:13 -05007#include <nlohmann/json.hpp>
Chicago Duan184f6022020-04-17 11:30:49 +08008#include <sdbusplus/bus.hpp>
9#include <sdbusplus/server.hpp>
10#include <sdbusplus/server/object.hpp>
11#include <sdbusplus/timer.hpp>
12#include <sdeventplus/event.hpp>
13
14namespace pldm
15{
Sagar Srinivas4d99c312024-03-28 06:50:13 -050016using Json = nlohmann::json;
Chicago Duan184f6022020-04-17 11:30:49 +080017
18/** @class SoftPowerOff
19 * @brief Responsible for coordinating Host SoftPowerOff operation
20 */
21class SoftPowerOff
22{
23 public:
24 /** @brief Constructs SoftPowerOff object.
25 *
26 * @param[in] bus - system D-Bus handler
27 * @param[in] event - sd_event handler
Manojkiran Eda3fcfaa12024-02-26 15:17:58 +053028 * @param[in] instanceDb - pldm instance database
Chicago Duan184f6022020-04-17 11:30:49 +080029 */
Manojkiran Eda3fcfaa12024-02-26 15:17:58 +053030 SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event,
31 InstanceIdDb& instanceIdDb);
Chicago Duan184f6022020-04-17 11:30:49 +080032
33 /** @brief Is the pldm-softpoweroff has error.
34 * if hasError is true, that means the pldm-softpoweroff failed to
35 * trigger the host soft off,so the pldm-softpoweroff will exit.
36 */
37 inline bool isError()
38 {
39 return hasError;
40 }
41
42 /** @brief Is the timer expired.
43 */
44 inline bool isTimerExpired()
45 {
46 return timer.isExpired();
47 }
48
49 /** @brief Is the host soft off completed.
50 */
51 inline bool isCompleted()
52 {
53 return completed;
54 }
55
56 /** @brief Is receive the response for the PLDM request msg.
57 */
58 inline bool isReceiveResponse()
59 {
60 return responseReceived;
61 }
62
63 /** @brief Send PLDM Set State Effecter States command and
64 * wait the host gracefully shutdown.
65 *
66 * @param[in] event - The event loop.
67 *
68 * @return PLDM_SUCCESS or PLDM_ERROR.
69 */
70 int hostSoftOff(sdeventplus::Event& event);
71
72 private:
73 /** @brief Getting the host current state.
74 */
75 int getHostState();
76
77 /** @brief Stop the timer.
78 */
79 inline auto stopTimer()
80 {
81 return timer.stop();
82 }
83
Sagar Srinivas4d99c312024-03-28 06:50:13 -050084 /** @brief method to parse the config Json file for softoff
85 *
86 * @return Json - Json object of
87 */
88 Json parseConfig();
89
Chicago Duan184f6022020-04-17 11:30:49 +080090 /** @brief When host soft off completed, stop the timer and
91 * set the completed to true.
92 *
93 * @param[in] msg - Data associated with subscribed signal
94 */
Patrick Williams84b790c2022-07-22 19:26:56 -050095 void hostSoftOffComplete(sdbusplus::message_t& msg);
Chicago Duan184f6022020-04-17 11:30:49 +080096
97 /** @brief Start the timer.
98 *
99 * @param[in] usec - Time to wait for the Host to gracefully shutdown.
100 *
101 * @return Success or exception thrown
102 */
103 int startTimer(const std::chrono::microseconds& usec);
104
105 /** @brief Get effecterID from PDRs.
106 *
Sagar Srinivas4d99c312024-03-28 06:50:13 -0500107 * @param[in] entityType - entity type of the entity hosting
108 * hosting softoff PDR
109 * @param[in] stateSetId - state set ID of the softoff PDR
110 *
111 * @return true or false
Chicago Duan184f6022020-04-17 11:30:49 +0800112 */
Sagar Srinivas4d99c312024-03-28 06:50:13 -0500113 bool getEffecterID(pldm::pdr::EntityType& entityType,
114 pldm::pdr::StateSetId& stateSetId);
Chicago Duan184f6022020-04-17 11:30:49 +0800115
116 /** @brief Get VMM/SystemFirmware Sensor info from PDRs.
117 *
Sagar Srinivas4d99c312024-03-28 06:50:13 -0500118 * @param[in] entityType - entity type of the entity hosting
119 * hosting softoff PDR
120 * @param[in] stateSetId - state set ID of the softoff PDR
121 *
Chicago Duan184f6022020-04-17 11:30:49 +0800122 * @return PLDM_SUCCESS or PLDM_ERROR
123 */
Sagar Srinivas4d99c312024-03-28 06:50:13 -0500124 int getSensorInfo(pldm::pdr::EntityType& entityType,
125 pldm::pdr::StateSetId& stateSetId);
Chicago Duan184f6022020-04-17 11:30:49 +0800126
127 /** @brief effecterID
128 */
129 uint16_t effecterID;
130
131 /** @brief sensorID.
132 */
133 pldm::pdr::SensorID sensorID;
134
135 /** @brief sensorOffset.
136 */
137 pldm::pdr::SensorOffset sensorOffset;
138
139 /** @brief Failed to send host soft off command flag.
140 */
141 bool hasError = false;
142
143 /** @brief Host soft off completed flag.
144 */
145 bool completed = false;
146
147 /** @brief The response for the PLDM request msg is received flag.
148 */
149 bool responseReceived = false;
150
Chicago Duan184f6022020-04-17 11:30:49 +0800151 /* @brief sdbusplus handle */
Patrick Williams84b790c2022-07-22 19:26:56 -0500152 sdbusplus::bus_t& bus;
Chicago Duan184f6022020-04-17 11:30:49 +0800153
154 /** @brief Reference to Timer object */
Patrick Williams35535cf2023-12-05 12:45:02 -0600155 sdbusplus::Timer timer;
Chicago Duan184f6022020-04-17 11:30:49 +0800156
157 /** @brief Used to subscribe to dbus pldm StateSensorEvent signal
158 * When the host soft off is complete, it sends an platform event message
159 * to BMC's pldmd, and the pldmd will emit the StateSensorEvent signal.
160 **/
161 std::unique_ptr<sdbusplus::bus::match_t> pldmEventSignal;
Manojkiran Eda3fcfaa12024-02-26 15:17:58 +0530162
163 /** @brief Reference to the instance database
164 */
165 InstanceIdDb& instanceIdDb;
Chicago Duan184f6022020-04-17 11:30:49 +0800166};
167
168} // namespace pldm