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