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