blob: 1f74f18f2d591e5c4a664840555bea63faee8009 [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,
Naveen Mosesab8dac52022-07-15 19:32:27 +053013 resetPressed,
14 powerReleased,
15 longPowerReleased,
16 resetReleased
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053017};
Matt Spinlerfb35a322018-11-26 14:30:30 -060018/**
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 */
29class 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 */
Patrick Williams9a529a62022-07-22 19:26:54 -050044 explicit Handler(sdbusplus::bus_t& bus);
Matt Spinlerfb35a322018-11-26 14:30:30 -060045
46 private:
47 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060048 * @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 */
Patrick Williams9a529a62022-07-22 19:26:54 -050055 void powerReleased(sdbusplus::message_t& msg);
Matt Spinler963c65f2018-11-26 14:46:41 -060056
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 */
Patrick Williams9a529a62022-07-22 19:26:54 -050065 void longPowerPressed(sdbusplus::message_t& msg);
Matt Spinler963c65f2018-11-26 14:46:41 -060066
67 /**
Matt Spinler69f93512018-11-26 14:55:58 -060068 * @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 */
Patrick Williams9a529a62022-07-22 19:26:54 -050074 void idReleased(sdbusplus::message_t& msg);
Matt Spinler69f93512018-11-26 14:55:58 -060075
76 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060077 * @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 */
Patrick Williams9a529a62022-07-22 19:26:54 -050083 void resetReleased(sdbusplus::message_t& msg);
Matt Spinler06a5bdd2018-11-26 14:50:48 -060084
85 /**
Naveen Mosesa6d4e652022-04-13 19:27:25 +053086 * @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
Patrick Williamse3b4e112022-11-26 09:41:58 -060094 void debugHostSelectorReleased(sdbusplus::message_t& msg);
Naveen Mosesa6d4e652022-04-13 19:27:25 +053095
96 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060097 * @brief Checks if system is powered on
98 *
99 * @return true if powered on, false else
100 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530101 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -0600102
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530103 /*
Matt Spinler963c65f2018-11-26 14:46:41 -0600104 * @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 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530111 * @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();
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530119 /**
120 * @brief increases the host selector position property
121 * by 1 upto max host selector position
122 *
123 * @return void
124 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530125
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530126 void increaseHostSelectorPosition();
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530127 /**
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 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600144 * @brief sdbusplus connection object
145 */
Patrick Williams9a529a62022-07-22 19:26:54 -0500146 sdbusplus::bus_t& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600147
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 */
Naveen Mosesab8dac52022-07-15 19:32:27 +0530156 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600157
158 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600159 * @brief Matches on the ID button released signal
160 */
161 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
162
163 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600164 * @brief Matches on the reset button released signal
165 */
166 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530167
168 /**
169 * @brief Matches on the ocp debug host selector button released signal
170 */
171 std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600172};
173
174} // namespace button
175} // namespace phosphor