xref: /openbmc/pldm/softoff/softoff.hpp (revision fe6f0d40)
1 #pragma once
2 
3 #include "libpldm/requester/pldm.h"
4 
5 #include "common/types.hpp"
6 
7 #include <sdbusplus/bus.hpp>
8 #include <sdbusplus/server.hpp>
9 #include <sdbusplus/server/object.hpp>
10 #include <sdbusplus/timer.hpp>
11 #include <sdeventplus/event.hpp>
12 
13 namespace pldm
14 {
15 
16 /** @class SoftPowerOff
17  *  @brief Responsible for coordinating Host SoftPowerOff operation
18  */
19 class SoftPowerOff
20 {
21   public:
22     /** @brief Constructs SoftPowerOff object.
23      *
24      *  @param[in] bus       - system D-Bus handler
25      *  @param[in] event     - sd_event handler
26      */
27     SoftPowerOff(sdbusplus::bus::bus& bus, sd_event* event);
28 
29     /** @brief Is the pldm-softpoweroff has error.
30      * if hasError is true, that means the pldm-softpoweroff failed to
31      * trigger the host soft off,so the pldm-softpoweroff will exit.
32      */
33     inline bool isError()
34     {
35         return hasError;
36     }
37 
38     /** @brief Is the timer expired.
39      */
40     inline bool isTimerExpired()
41     {
42         return timer.isExpired();
43     }
44 
45     /** @brief Is the host soft off completed.
46      */
47     inline bool isCompleted()
48     {
49         return completed;
50     }
51 
52     /** @brief Is receive the response for the PLDM request msg.
53      */
54     inline bool isReceiveResponse()
55     {
56         return responseReceived;
57     }
58 
59     /** @brief Send PLDM Set State Effecter States command and
60      * wait the host gracefully shutdown.
61      *
62      *  @param[in] event - The event loop.
63      *
64      *  @return PLDM_SUCCESS or PLDM_ERROR.
65      */
66     int hostSoftOff(sdeventplus::Event& event);
67 
68   private:
69     /** @brief Getting the host current state.
70      */
71     int getHostState();
72 
73     /** @brief Stop the timer.
74      */
75     inline auto stopTimer()
76     {
77         return timer.stop();
78     }
79 
80     /** @brief When host soft off completed, stop the timer and
81      *         set the completed to true.
82      *
83      *  @param[in] msg - Data associated with subscribed signal
84      */
85     void hostSoftOffComplete(sdbusplus::message::message& msg);
86 
87     /** @brief Start the timer.
88      *
89      *  @param[in] usec - Time to wait for the Host to gracefully shutdown.
90      *
91      *  @return Success or exception thrown
92      */
93     int startTimer(const std::chrono::microseconds& usec);
94 
95     /** @brief Get effecterID from PDRs.
96      *
97      *  @return PLDM_SUCCESS or PLDM_ERROR
98      */
99     int getEffecterID();
100 
101     /** @brief Get VMM/SystemFirmware Sensor info from PDRs.
102      *
103      *  @return PLDM_SUCCESS or PLDM_ERROR
104      */
105     int getSensorInfo();
106 
107     /** @brief effecterID
108      */
109     uint16_t effecterID;
110 
111     /** @brief sensorID.
112      */
113     pldm::pdr::SensorID sensorID;
114 
115     /** @brief sensorOffset.
116      */
117     pldm::pdr::SensorOffset sensorOffset;
118 
119     /** @brief Failed to send host soft off command flag.
120      */
121     bool hasError = false;
122 
123     /** @brief Host soft off completed flag.
124      */
125     bool completed = false;
126 
127     /** @brief The response for the PLDM request msg is received flag.
128      */
129     bool responseReceived = false;
130 
131     /** @brief Is the Virtual Machine Manager/VMM state effecter available.
132      */
133     bool VMMPdrExist = true;
134 
135     /* @brief sdbusplus handle */
136     sdbusplus::bus::bus& bus;
137 
138     /** @brief Reference to Timer object */
139     phosphor::Timer timer;
140 
141     /** @brief Used to subscribe to dbus pldm StateSensorEvent signal
142      * When the host soft off is complete, it sends an platform event message
143      * to BMC's pldmd, and the pldmd will emit the StateSensorEvent signal.
144      **/
145     std::unique_ptr<sdbusplus::bus::match_t> pldmEventSignal;
146 };
147 
148 } // namespace pldm
149