blob: ff8850ba32a5bce1227d14e13bbd7c6ec3f3d787 [file] [log] [blame]
Matt Spinlerfb35a322018-11-26 14:30:30 -06001#pragma once
Matt Spinler1a309f72023-04-04 13:12:19 -05002
Rush Chen31ce3752024-11-08 14:57:27 +08003#include "config.hpp"
Matt Spinler1a309f72023-04-04 13:12:19 -05004#include "power_button_profile.hpp"
5
Matt Spinlerfb35a322018-11-26 14:30:30 -06006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/bus/match.hpp>
8
Rush Chen31ce3752024-11-08 14:57:27 +08009#include <algorithm>
10#include <numeric>
11#include <sstream>
12#include <string>
13
Matt Spinlerfb35a322018-11-26 14:30:30 -060014namespace phosphor
15{
16namespace button
17{
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053018enum class PowerEvent
19{
20 powerPressed,
Naveen Mosesab8dac52022-07-15 19:32:27 +053021 resetPressed,
22 powerReleased,
Naveen Mosesab8dac52022-07-15 19:32:27 +053023 resetReleased
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053024};
Rush Chen31ce3752024-11-08 14:57:27 +080025
26enum class PwrCtl
27{
28 chassisOn,
29 chassisOff,
30 chassisCycle,
31};
32
33inline static size_t numberOfChassis()
34{
35 return instances.size();
36}
37
Matt Spinlerfb35a322018-11-26 14:30:30 -060038/**
39 * @class Handler
40 *
41 * This class acts on the signals generated by the
42 * xyz.openbmc_project.Chassis.Buttons code when
43 * it detects button presses.
44 *
45 * There are 3 buttons supported - Power, ID, and Reset.
46 * As not all systems may implement each button, this class will
47 * check for that button on D-Bus before listening for its signals.
48 */
49class Handler
50{
51 public:
52 Handler() = delete;
53 ~Handler() = default;
54 Handler(const Handler&) = delete;
55 Handler& operator=(const Handler&) = delete;
56 Handler(Handler&&) = delete;
57 Handler& operator=(Handler&&) = delete;
58
59 /**
60 * @brief Constructor
61 *
62 * @param[in] bus - sdbusplus connection object
63 */
Patrick Williams9a529a62022-07-22 19:26:54 -050064 explicit Handler(sdbusplus::bus_t& bus);
Matt Spinlerfb35a322018-11-26 14:30:30 -060065
66 private:
67 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060068 * @brief The handler for a power button press
69 *
DelphineCCChiu395c7642023-04-19 10:46:47 +080070 * It will do power action according to the pressing duration.
Matt Spinler963c65f2018-11-26 14:46:41 -060071 *
72 * @param[in] msg - sdbusplus message from signal
73 */
Patrick Williams9a529a62022-07-22 19:26:54 -050074 void powerReleased(sdbusplus::message_t& msg);
Matt Spinler963c65f2018-11-26 14:46:41 -060075
76 /**
Matt Spinler69f93512018-11-26 14:55:58 -060077 * @brief The handler for an ID button press
78 *
79 * Toggles the ID LED group
80 *
81 * @param[in] msg - sdbusplus message from signal
82 */
Patrick Williams9a529a62022-07-22 19:26:54 -050083 void idReleased(sdbusplus::message_t& msg);
Matt Spinler69f93512018-11-26 14:55:58 -060084
85 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060086 * @brief The handler for a reset button press
87 *
88 * Reboots the host if it is powered on.
89 *
90 * @param[in] msg - sdbusplus message from signal
91 */
Patrick Williams9a529a62022-07-22 19:26:54 -050092 void resetReleased(sdbusplus::message_t& msg);
Matt Spinler06a5bdd2018-11-26 14:50:48 -060093
94 /**
Naveen Mosesa6d4e652022-04-13 19:27:25 +053095 * @brief The handler for a OCP debug card host selector button press
96 *
97 * In multi host system increases host position by 1 up to max host
98 * position.
99 *
100 * @param[in] msg - sdbusplus message from signal
101 */
102
Patrick Williamse3b4e112022-11-26 09:41:58 -0600103 void debugHostSelectorReleased(sdbusplus::message_t& msg);
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530104
105 /**
Matt Spinler963c65f2018-11-26 14:46:41 -0600106 * @brief Checks if system is powered on
107 *
108 * @return true if powered on, false else
109 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530110 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -0600111
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530112 /*
Matt Spinler963c65f2018-11-26 14:46:41 -0600113 * @return std::string - the D-Bus service name if found, else
114 * an empty string
115 */
116 std::string getService(const std::string& path,
117 const std::string& interface) const;
118
119 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530120 * @brief gets the valid host selector value in multi host
121 * system
122 *
123 * @return size_t throws exception if host selector position is
124 * invalid or not available.
125 */
126
127 size_t getHostSelectorValue();
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530128 /**
129 * @brief increases the host selector position property
130 * by 1 upto max host selector position
131 *
132 * @return void
133 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530134
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530135 void increaseHostSelectorPosition();
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530136 /**
137 * @brief checks if the system has multi host
138 * based on the host selector property availability
139 *
140 * @return bool returns true if multi host system
141 * else returns false.
142 */
143 bool isMultiHost();
144 /**
145 * @brief trigger the power ctrl event based on the
146 * button press event type.
147 *
148 * @return void
149 */
DelphineCCChiu395c7642023-04-19 10:46:47 +0800150 void handlePowerEvent(PowerEvent powerEventType,
Rush Chen31ce3752024-11-08 14:57:27 +0800151 const std::string& objectPath,
DelphineCCChiu395c7642023-04-19 10:46:47 +0800152 std::chrono::microseconds duration);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530153
154 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600155 * @brief sdbusplus connection object
156 */
Patrick Williams9a529a62022-07-22 19:26:54 -0500157 sdbusplus::bus_t& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600158
159 /**
160 * @brief Matches on the power button released signal
161 */
162 std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
163
164 /**
165 * @brief Matches on the power button long press released signal
166 */
Naveen Mosesab8dac52022-07-15 19:32:27 +0530167 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600168
169 /**
Rush Chen31ce3752024-11-08 14:57:27 +0800170 * @brief Matches on the multi power button released signal
171 */
172 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
173 multiPowerButtonReleased;
174
175 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600176 * @brief Matches on the ID button released signal
177 */
178 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
179
180 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600181 * @brief Matches on the reset button released signal
182 */
183 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530184
185 /**
186 * @brief Matches on the ocp debug host selector button released signal
187 */
188 std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased;
Matt Spinler1a309f72023-04-04 13:12:19 -0500189
190 /**
191 * @brief The custom power handler profile object.
192 */
193 std::unique_ptr<PowerButtonProfile> powerButtonProfile;
Rush Chen31ce3752024-11-08 14:57:27 +0800194
195 /**
196 * @brief Flag to indicate multi power button mode(false) or host select
197 * button mode(true)
198 */
199 bool hostSelectButtonMode = false;
200
201 /**
202 * @brief Flag to indicate if the button supports multi action
203 */
204 bool isButtonMultiActionSupport = true;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600205};
206
207} // namespace button
208} // namespace phosphor