1fb35a325SMatt Spinler #pragma once
2fb35a325SMatt Spinler #include <sdbusplus/bus.hpp>
3fb35a325SMatt Spinler #include <sdbusplus/bus/match.hpp>
4fb35a325SMatt Spinler 
5fb35a325SMatt Spinler namespace phosphor
6fb35a325SMatt Spinler {
7fb35a325SMatt Spinler namespace button
8fb35a325SMatt Spinler {
93bd1cfcbSNaveen Moses enum class PowerEvent
103bd1cfcbSNaveen Moses {
113bd1cfcbSNaveen Moses     powerPressed,
123bd1cfcbSNaveen Moses     longPowerPressed,
13ab8dac51SNaveen Moses     resetPressed,
14ab8dac51SNaveen Moses     powerReleased,
15ab8dac51SNaveen Moses     longPowerReleased,
16ab8dac51SNaveen Moses     resetReleased
173bd1cfcbSNaveen Moses };
18fb35a325SMatt Spinler /**
19fb35a325SMatt Spinler  * @class Handler
20fb35a325SMatt Spinler  *
21fb35a325SMatt Spinler  * This class acts on the signals generated by the
22fb35a325SMatt Spinler  * xyz.openbmc_project.Chassis.Buttons code when
23fb35a325SMatt Spinler  * it detects button presses.
24fb35a325SMatt Spinler  *
25fb35a325SMatt Spinler  * There are 3 buttons supported - Power, ID, and Reset.
26fb35a325SMatt Spinler  * As not all systems may implement each button, this class will
27fb35a325SMatt Spinler  * check for that button on D-Bus before listening for its signals.
28fb35a325SMatt Spinler  */
29fb35a325SMatt Spinler class Handler
30fb35a325SMatt Spinler {
31fb35a325SMatt Spinler   public:
32fb35a325SMatt Spinler     Handler() = delete;
33fb35a325SMatt Spinler     ~Handler() = default;
34fb35a325SMatt Spinler     Handler(const Handler&) = delete;
35fb35a325SMatt Spinler     Handler& operator=(const Handler&) = delete;
36fb35a325SMatt Spinler     Handler(Handler&&) = delete;
37fb35a325SMatt Spinler     Handler& operator=(Handler&&) = delete;
38fb35a325SMatt Spinler 
39fb35a325SMatt Spinler     /**
40fb35a325SMatt Spinler      * @brief Constructor
41fb35a325SMatt Spinler      *
42fb35a325SMatt Spinler      * @param[in] bus - sdbusplus connection object
43fb35a325SMatt Spinler      */
449a529a69SPatrick Williams     explicit Handler(sdbusplus::bus_t& bus);
45fb35a325SMatt Spinler 
46fb35a325SMatt Spinler   private:
47fb35a325SMatt Spinler     /**
48963c65f3SMatt Spinler      * @brief The handler for a power button press
49963c65f3SMatt Spinler      *
50963c65f3SMatt Spinler      * It will power on the system if it's currently off,
51963c65f3SMatt Spinler      * else it will soft power it off.
52963c65f3SMatt Spinler      *
53963c65f3SMatt Spinler      * @param[in] msg - sdbusplus message from signal
54963c65f3SMatt Spinler      */
559a529a69SPatrick Williams     void powerReleased(sdbusplus::message_t& msg);
56963c65f3SMatt Spinler 
57963c65f3SMatt Spinler     /**
58963c65f3SMatt Spinler      * @brief The handler for a long power button press
59963c65f3SMatt Spinler      *
60963c65f3SMatt Spinler      * If the system is currently powered on, it will
61963c65f3SMatt Spinler      * perform an immediate power off.
62963c65f3SMatt Spinler      *
63963c65f3SMatt Spinler      * @param[in] msg - sdbusplus message from signal
64963c65f3SMatt Spinler      */
659a529a69SPatrick Williams     void longPowerPressed(sdbusplus::message_t& msg);
66963c65f3SMatt Spinler 
67963c65f3SMatt Spinler     /**
6869f93512SMatt Spinler      * @brief The handler for an ID button press
6969f93512SMatt Spinler      *
7069f93512SMatt Spinler      * Toggles the ID LED group
7169f93512SMatt Spinler      *
7269f93512SMatt Spinler      * @param[in] msg - sdbusplus message from signal
7369f93512SMatt Spinler      */
749a529a69SPatrick Williams     void idReleased(sdbusplus::message_t& msg);
7569f93512SMatt Spinler 
7669f93512SMatt Spinler     /**
7706a5bddfSMatt Spinler      * @brief The handler for a reset button press
7806a5bddfSMatt Spinler      *
7906a5bddfSMatt Spinler      * Reboots the host if it is powered on.
8006a5bddfSMatt Spinler      *
8106a5bddfSMatt Spinler      * @param[in] msg - sdbusplus message from signal
8206a5bddfSMatt Spinler      */
839a529a69SPatrick Williams     void resetReleased(sdbusplus::message_t& msg);
8406a5bddfSMatt Spinler 
8506a5bddfSMatt Spinler     /**
86a6d4e65dSNaveen Moses      * @brief The handler for a OCP debug card host selector button press
87a6d4e65dSNaveen Moses      *
88a6d4e65dSNaveen Moses      * In multi host system increases host position by 1 up to max host
89a6d4e65dSNaveen Moses      * position.
90a6d4e65dSNaveen Moses      *
91a6d4e65dSNaveen Moses      * @param[in] msg - sdbusplus message from signal
92a6d4e65dSNaveen Moses      */
93a6d4e65dSNaveen Moses 
94*e3b4e11fSPatrick Williams     void debugHostSelectorReleased(sdbusplus::message_t& msg);
95a6d4e65dSNaveen Moses 
96a6d4e65dSNaveen Moses     /**
97963c65f3SMatt Spinler      * @brief Checks if system is powered on
98963c65f3SMatt Spinler      *
99963c65f3SMatt Spinler      * @return true if powered on, false else
100963c65f3SMatt Spinler      */
1013bd1cfcbSNaveen Moses     bool poweredOn(size_t hostNumber) const;
102963c65f3SMatt Spinler 
1033bd1cfcbSNaveen Moses     /*
104963c65f3SMatt Spinler      * @return std::string - the D-Bus service name if found, else
105963c65f3SMatt Spinler      *                       an empty string
106963c65f3SMatt Spinler      */
107963c65f3SMatt Spinler     std::string getService(const std::string& path,
108963c65f3SMatt Spinler                            const std::string& interface) const;
109963c65f3SMatt Spinler 
110963c65f3SMatt Spinler     /**
1113bd1cfcbSNaveen Moses      * @brief gets the valid host selector value in multi host
1123bd1cfcbSNaveen Moses      * system
1133bd1cfcbSNaveen Moses      *
1143bd1cfcbSNaveen Moses      * @return size_t throws exception if host selector position is
1153bd1cfcbSNaveen Moses      * invalid or not available.
1163bd1cfcbSNaveen Moses      */
1173bd1cfcbSNaveen Moses 
1183bd1cfcbSNaveen Moses     size_t getHostSelectorValue();
119a6d4e65dSNaveen Moses     /**
120a6d4e65dSNaveen Moses      * @brief increases the host selector position property
121a6d4e65dSNaveen Moses      * by 1 upto max host selector position
122a6d4e65dSNaveen Moses      *
123a6d4e65dSNaveen Moses      * @return void
124a6d4e65dSNaveen Moses      */
1253bd1cfcbSNaveen Moses 
126a6d4e65dSNaveen Moses     void increaseHostSelectorPosition();
1273bd1cfcbSNaveen Moses     /**
1283bd1cfcbSNaveen Moses      * @brief checks if the system has multi host
1293bd1cfcbSNaveen Moses      * based on the host selector property availability
1303bd1cfcbSNaveen Moses      *
1313bd1cfcbSNaveen Moses      * @return bool returns true if multi host system
1323bd1cfcbSNaveen Moses      * else returns false.
1333bd1cfcbSNaveen Moses      */
1343bd1cfcbSNaveen Moses     bool isMultiHost();
1353bd1cfcbSNaveen Moses     /**
1363bd1cfcbSNaveen Moses      * @brief trigger the power ctrl event based on the
1373bd1cfcbSNaveen Moses      *  button press event type.
1383bd1cfcbSNaveen Moses      *
1393bd1cfcbSNaveen Moses      * @return void
1403bd1cfcbSNaveen Moses      */
1413bd1cfcbSNaveen Moses     void handlePowerEvent(PowerEvent powerEventType);
1423bd1cfcbSNaveen Moses 
1433bd1cfcbSNaveen Moses     /**
144fb35a325SMatt Spinler      * @brief sdbusplus connection object
145fb35a325SMatt Spinler      */
1469a529a69SPatrick Williams     sdbusplus::bus_t& bus;
147963c65f3SMatt Spinler 
148963c65f3SMatt Spinler     /**
149963c65f3SMatt Spinler      * @brief Matches on the power button released signal
150963c65f3SMatt Spinler      */
151963c65f3SMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
152963c65f3SMatt Spinler 
153963c65f3SMatt Spinler     /**
154963c65f3SMatt Spinler      * @brief Matches on the power button long press released signal
155963c65f3SMatt Spinler      */
156ab8dac51SNaveen Moses     std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
15706a5bddfSMatt Spinler 
15806a5bddfSMatt Spinler     /**
15969f93512SMatt Spinler      * @brief Matches on the ID button released signal
16069f93512SMatt Spinler      */
16169f93512SMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
16269f93512SMatt Spinler 
16369f93512SMatt Spinler     /**
16406a5bddfSMatt Spinler      * @brief Matches on the reset button released signal
16506a5bddfSMatt Spinler      */
16606a5bddfSMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
167a6d4e65dSNaveen Moses 
168a6d4e65dSNaveen Moses     /**
169a6d4e65dSNaveen Moses      * @brief Matches on the ocp debug host selector  button released signal
170a6d4e65dSNaveen Moses      */
171a6d4e65dSNaveen Moses     std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased;
172fb35a325SMatt Spinler };
173fb35a325SMatt Spinler 
174fb35a325SMatt Spinler } // namespace button
175fb35a325SMatt Spinler } // namespace phosphor
176