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      */
44*9a529a69SPatrick 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      */
55*9a529a69SPatrick 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      */
65*9a529a69SPatrick 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      */
74*9a529a69SPatrick 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      */
83*9a529a69SPatrick Williams     void resetReleased(sdbusplus::message_t& msg);
8406a5bddfSMatt Spinler 
8506a5bddfSMatt Spinler     /**
86963c65f3SMatt Spinler      * @brief Checks if system is powered on
87963c65f3SMatt Spinler      *
88963c65f3SMatt Spinler      * @return true if powered on, false else
89963c65f3SMatt Spinler      */
903bd1cfcbSNaveen Moses     bool poweredOn(size_t hostNumber) const;
91963c65f3SMatt Spinler 
923bd1cfcbSNaveen Moses     /*
93963c65f3SMatt Spinler      * @return std::string - the D-Bus service name if found, else
94963c65f3SMatt Spinler      *                       an empty string
95963c65f3SMatt Spinler      */
96963c65f3SMatt Spinler     std::string getService(const std::string& path,
97963c65f3SMatt Spinler                            const std::string& interface) const;
98963c65f3SMatt Spinler 
99963c65f3SMatt Spinler     /**
1003bd1cfcbSNaveen Moses      * @brief gets the valid host selector value in multi host
1013bd1cfcbSNaveen Moses      * system
1023bd1cfcbSNaveen Moses      *
1033bd1cfcbSNaveen Moses      * @return size_t throws exception if host selector position is
1043bd1cfcbSNaveen Moses      * invalid or not available.
1053bd1cfcbSNaveen Moses      */
1063bd1cfcbSNaveen Moses 
1073bd1cfcbSNaveen Moses     size_t getHostSelectorValue();
1083bd1cfcbSNaveen Moses 
1093bd1cfcbSNaveen Moses     /**
1103bd1cfcbSNaveen Moses      * @brief checks if the system has multi host
1113bd1cfcbSNaveen Moses      * based on the host selector property availability
1123bd1cfcbSNaveen Moses      *
1133bd1cfcbSNaveen Moses      * @return bool returns true if multi host system
1143bd1cfcbSNaveen Moses      * else returns false.
1153bd1cfcbSNaveen Moses      */
1163bd1cfcbSNaveen Moses     bool isMultiHost();
1173bd1cfcbSNaveen Moses     /**
1183bd1cfcbSNaveen Moses      * @brief trigger the power ctrl event based on the
1193bd1cfcbSNaveen Moses      *  button press event type.
1203bd1cfcbSNaveen Moses      *
1213bd1cfcbSNaveen Moses      * @return void
1223bd1cfcbSNaveen Moses      */
1233bd1cfcbSNaveen Moses     void handlePowerEvent(PowerEvent powerEventType);
1243bd1cfcbSNaveen Moses 
1253bd1cfcbSNaveen Moses     /**
126fb35a325SMatt Spinler      * @brief sdbusplus connection object
127fb35a325SMatt Spinler      */
128*9a529a69SPatrick Williams     sdbusplus::bus_t& bus;
129963c65f3SMatt Spinler 
130963c65f3SMatt Spinler     /**
131963c65f3SMatt Spinler      * @brief Matches on the power button released signal
132963c65f3SMatt Spinler      */
133963c65f3SMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
134963c65f3SMatt Spinler 
135963c65f3SMatt Spinler     /**
136963c65f3SMatt Spinler      * @brief Matches on the power button long press released signal
137963c65f3SMatt Spinler      */
138ab8dac51SNaveen Moses     std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
13906a5bddfSMatt Spinler 
14006a5bddfSMatt Spinler     /**
14169f93512SMatt Spinler      * @brief Matches on the ID button released signal
14269f93512SMatt Spinler      */
14369f93512SMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
14469f93512SMatt Spinler 
14569f93512SMatt Spinler     /**
14606a5bddfSMatt Spinler      * @brief Matches on the reset button released signal
14706a5bddfSMatt Spinler      */
14806a5bddfSMatt Spinler     std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
149fb35a325SMatt Spinler };
150fb35a325SMatt Spinler 
151fb35a325SMatt Spinler } // namespace button
152fb35a325SMatt Spinler } // namespace phosphor
153