blob: c6dc127d8033e96661a0125c34bc683b9a9d04e8 [file] [log] [blame]
Matt Spinlerfb35a322018-11-26 14:30:30 -06001#pragma once
Matt Spinlerfb35a322018-11-26 14:30:30 -06002#include <sdbusplus/bus.hpp>
3#include <sdbusplus/bus/match.hpp>
4
5namespace phosphor
6{
7namespace button
8{
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05309enum class PowerEvent
10{
11 powerPressed,
12 longPowerPressed,
13 resetPressed
14};
Matt Spinlerfb35a322018-11-26 14:30:30 -060015/**
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 */
26class 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 */
George Liu94afa4b2022-06-20 13:36:43 +080041 explicit Handler(sdbusplus::bus::bus& bus);
Matt Spinlerfb35a322018-11-26 14:30:30 -060042
43 private:
44 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060045 * @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 /**
Matt Spinler69f93512018-11-26 14:55:58 -060065 * @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 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060074 * @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 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060083 * @brief Checks if system is powered on
84 *
85 * @return true if powered on, false else
86 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053087 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -060088
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053089 /*
Matt Spinler963c65f2018-11-26 14:46:41 -060090 * @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 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053097 * @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 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600123 * @brief sdbusplus connection object
124 */
125 sdbusplus::bus::bus& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600126
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;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600136
137 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600138 * @brief Matches on the ID button released signal
139 */
140 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
141
142 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600143 * @brief Matches on the reset button released signal
144 */
145 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600146};
147
148} // namespace button
149} // namespace phosphor