blob: e398a0a472b12b98390b050b52d6d3a447c262d7 [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,
Naveen Mosesab8dac52022-07-15 19:32:27 +053012 resetPressed,
13 powerReleased,
Naveen Mosesab8dac52022-07-15 19:32:27 +053014 resetReleased
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053015};
Matt Spinlerfb35a322018-11-26 14:30:30 -060016/**
17 * @class Handler
18 *
19 * This class acts on the signals generated by the
20 * xyz.openbmc_project.Chassis.Buttons code when
21 * it detects button presses.
22 *
23 * There are 3 buttons supported - Power, ID, and Reset.
24 * As not all systems may implement each button, this class will
25 * check for that button on D-Bus before listening for its signals.
26 */
27class Handler
28{
29 public:
30 Handler() = delete;
31 ~Handler() = default;
32 Handler(const Handler&) = delete;
33 Handler& operator=(const Handler&) = delete;
34 Handler(Handler&&) = delete;
35 Handler& operator=(Handler&&) = delete;
36
37 /**
38 * @brief Constructor
39 *
40 * @param[in] bus - sdbusplus connection object
41 */
Patrick Williams9a529a62022-07-22 19:26:54 -050042 explicit Handler(sdbusplus::bus_t& bus);
Matt Spinlerfb35a322018-11-26 14:30:30 -060043
44 private:
45 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060046 * @brief The handler for a power button press
47 *
DelphineCCChiu395c7642023-04-19 10:46:47 +080048 * It will do power action according to the pressing duration.
Matt Spinler963c65f2018-11-26 14:46:41 -060049 *
50 * @param[in] msg - sdbusplus message from signal
51 */
Patrick Williams9a529a62022-07-22 19:26:54 -050052 void powerReleased(sdbusplus::message_t& msg);
Matt Spinler963c65f2018-11-26 14:46:41 -060053
54 /**
Matt Spinler69f93512018-11-26 14:55:58 -060055 * @brief The handler for an ID button press
56 *
57 * Toggles the ID LED group
58 *
59 * @param[in] msg - sdbusplus message from signal
60 */
Patrick Williams9a529a62022-07-22 19:26:54 -050061 void idReleased(sdbusplus::message_t& msg);
Matt Spinler69f93512018-11-26 14:55:58 -060062
63 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060064 * @brief The handler for a reset button press
65 *
66 * Reboots the host if it is powered on.
67 *
68 * @param[in] msg - sdbusplus message from signal
69 */
Patrick Williams9a529a62022-07-22 19:26:54 -050070 void resetReleased(sdbusplus::message_t& msg);
Matt Spinler06a5bdd2018-11-26 14:50:48 -060071
72 /**
Naveen Mosesa6d4e652022-04-13 19:27:25 +053073 * @brief The handler for a OCP debug card host selector button press
74 *
75 * In multi host system increases host position by 1 up to max host
76 * position.
77 *
78 * @param[in] msg - sdbusplus message from signal
79 */
80
Patrick Williamse3b4e112022-11-26 09:41:58 -060081 void debugHostSelectorReleased(sdbusplus::message_t& msg);
Naveen Mosesa6d4e652022-04-13 19:27:25 +053082
83 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060084 * @brief Checks if system is powered on
85 *
86 * @return true if powered on, false else
87 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053088 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -060089
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053090 /*
Matt Spinler963c65f2018-11-26 14:46:41 -060091 * @return std::string - the D-Bus service name if found, else
92 * an empty string
93 */
94 std::string getService(const std::string& path,
95 const std::string& interface) const;
96
97 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053098 * @brief gets the valid host selector value in multi host
99 * system
100 *
101 * @return size_t throws exception if host selector position is
102 * invalid or not available.
103 */
104
105 size_t getHostSelectorValue();
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530106 /**
107 * @brief increases the host selector position property
108 * by 1 upto max host selector position
109 *
110 * @return void
111 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530112
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530113 void increaseHostSelectorPosition();
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530114 /**
115 * @brief checks if the system has multi host
116 * based on the host selector property availability
117 *
118 * @return bool returns true if multi host system
119 * else returns false.
120 */
121 bool isMultiHost();
122 /**
123 * @brief trigger the power ctrl event based on the
124 * button press event type.
125 *
126 * @return void
127 */
DelphineCCChiu395c7642023-04-19 10:46:47 +0800128 void handlePowerEvent(PowerEvent powerEventType,
129 std::chrono::microseconds duration);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530130
131 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600132 * @brief sdbusplus connection object
133 */
Patrick Williams9a529a62022-07-22 19:26:54 -0500134 sdbusplus::bus_t& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600135
136 /**
137 * @brief Matches on the power button released signal
138 */
139 std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
140
141 /**
142 * @brief Matches on the power button long press released signal
143 */
Naveen Mosesab8dac52022-07-15 19:32:27 +0530144 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600145
146 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600147 * @brief Matches on the ID button released signal
148 */
149 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
150
151 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600152 * @brief Matches on the reset button released signal
153 */
154 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530155
156 /**
157 * @brief Matches on the ocp debug host selector button released signal
158 */
159 std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600160};
161
162} // namespace button
163} // namespace phosphor