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