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