xref: /openbmc/pldm/softoff/softoff.hpp (revision 4d99c311)
1 #pragma once
2 
3 #include "common/instance_id.hpp"
4 #include "common/transport.hpp"
5 #include "common/types.hpp"
6 
7 #include <nlohmann/json.hpp>
8 #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 
14 namespace pldm
15 {
16 using Json = nlohmann::json;
17 
18 /** @class SoftPowerOff
19  *  @brief Responsible for coordinating Host SoftPowerOff operation
20  */
21 class 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
28      *  @param[in] instanceDb - pldm instance database
29      */
30     SoftPowerOff(sdbusplus::bus_t& bus, sd_event* event,
31                  InstanceIdDb& instanceIdDb);
32 
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      */
isError()37     inline bool isError()
38     {
39         return hasError;
40     }
41 
42     /** @brief Is the timer expired.
43      */
isTimerExpired()44     inline bool isTimerExpired()
45     {
46         return timer.isExpired();
47     }
48 
49     /** @brief Is the host soft off completed.
50      */
isCompleted()51     inline bool isCompleted()
52     {
53         return completed;
54     }
55 
56     /** @brief Is receive the response for the PLDM request msg.
57      */
isReceiveResponse()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      */
stopTimer()79     inline auto stopTimer()
80     {
81         return timer.stop();
82     }
83 
84     /** @brief method to parse the config Json file for softoff
85      *
86      *  @return Json - Json object of
87      */
88     Json parseConfig();
89 
90     /** @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      */
95     void hostSoftOffComplete(sdbusplus::message_t& msg);
96 
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      *
107      *  @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
112      */
113     bool getEffecterID(pldm::pdr::EntityType& entityType,
114                        pldm::pdr::StateSetId& stateSetId);
115 
116     /** @brief Get VMM/SystemFirmware Sensor info from PDRs.
117      *
118      *  @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      *
122      *  @return PLDM_SUCCESS or PLDM_ERROR
123      */
124     int getSensorInfo(pldm::pdr::EntityType& entityType,
125                       pldm::pdr::StateSetId& stateSetId);
126 
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 
151     /* @brief sdbusplus handle */
152     sdbusplus::bus_t& bus;
153 
154     /** @brief Reference to Timer object */
155     sdbusplus::Timer timer;
156 
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;
162 
163     /** @brief Reference to the instance database
164      */
165     InstanceIdDb& instanceIdDb;
166 };
167 
168 } // namespace pldm
169