1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/bus/match.hpp>
5 
6 namespace phosphor
7 {
8 namespace button
9 {
10 enum class PowerEvent
11 {
12     powerPressed,
13     longPowerPressed,
14     resetPressed
15 };
16 /**
17  * @class Handler
18  *
19  * This class acts on the signals generated by the
20  * xyz.openbmc_project.Chassis.Buttons code when
21  * it detects button presses.
22  *
23  * There are 3 buttons supported - Power, ID, and Reset.
24  * As not all systems may implement each button, this class will
25  * check for that button on D-Bus before listening for its signals.
26  */
27 class Handler
28 {
29   public:
30     Handler() = delete;
31     ~Handler() = default;
32     Handler(const Handler&) = delete;
33     Handler& operator=(const Handler&) = delete;
34     Handler(Handler&&) = delete;
35     Handler& operator=(Handler&&) = delete;
36 
37     /**
38      * @brief Constructor
39      *
40      * @param[in] bus - sdbusplus connection object
41      */
42     Handler(sdbusplus::bus::bus& bus);
43 
44   private:
45     /**
46      * @brief The handler for a power button press
47      *
48      * It will power on the system if it's currently off,
49      * else it will soft power it off.
50      *
51      * @param[in] msg - sdbusplus message from signal
52      */
53     void powerPressed(sdbusplus::message::message& msg);
54 
55     /**
56      * @brief The handler for a long power button press
57      *
58      * If the system is currently powered on, it will
59      * perform an immediate power off.
60      *
61      * @param[in] msg - sdbusplus message from signal
62      */
63     void longPowerPressed(sdbusplus::message::message& msg);
64 
65     /**
66      * @brief The handler for an ID button press
67      *
68      * Toggles the ID LED group
69      *
70      * @param[in] msg - sdbusplus message from signal
71      */
72     void idPressed(sdbusplus::message::message& msg);
73 
74     /**
75      * @brief The handler for a reset button press
76      *
77      * Reboots the host if it is powered on.
78      *
79      * @param[in] msg - sdbusplus message from signal
80      */
81     void resetPressed(sdbusplus::message::message& msg);
82 
83     /**
84      * @brief Checks if system is powered on
85      *
86      * @return true if powered on, false else
87      */
88     bool poweredOn(size_t hostNumber) const;
89 
90     /*
91      * @return std::string - the D-Bus service name if found, else
92      *                       an empty string
93      */
94     std::string getService(const std::string& path,
95                            const std::string& interface) const;
96 
97     /**
98      * @brief gets the valid host selector value in multi host
99      * system
100      *
101      * @return size_t throws exception if host selector position is
102      * invalid or not available.
103      */
104 
105     size_t getHostSelectorValue();
106 
107     /**
108      * @brief checks if the system has multi host
109      * based on the host selector property availability
110      *
111      * @return bool returns true if multi host system
112      * else returns false.
113      */
114     bool isMultiHost();
115     /**
116      * @brief trigger the power ctrl event based on the
117      *  button press event type.
118      *
119      * @return void
120      */
121     void handlePowerEvent(PowerEvent powerEventType);
122 
123     /**
124      * @brief sdbusplus connection object
125      */
126     sdbusplus::bus::bus& bus;
127 
128     /**
129      * @brief Matches on the power button released signal
130      */
131     std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
132 
133     /**
134      * @brief Matches on the power button long press released signal
135      */
136     std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressReleased;
137 
138     /**
139      * @brief Matches on the ID button released signal
140      */
141     std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
142 
143     /**
144      * @brief Matches on the reset button released signal
145      */
146     std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
147 };
148 
149 } // namespace button
150 } // namespace phosphor
151