blob: 1332a0fb47117154288f69edc260cbd052e80727 [file] [log] [blame]
Matt Spinlerfb35a322018-11-26 14:30:30 -06001#pragma once
Matt Spinler1a309f72023-04-04 13:12:19 -05002
3#include "power_button_profile.hpp"
4
Matt Spinlerfb35a322018-11-26 14:30:30 -06005#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
7
8namespace phosphor
9{
10namespace button
11{
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053012enum class PowerEvent
13{
14 powerPressed,
Naveen Mosesab8dac52022-07-15 19:32:27 +053015 resetPressed,
16 powerReleased,
Naveen Mosesab8dac52022-07-15 19:32:27 +053017 resetReleased
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053018};
Matt Spinlerfb35a322018-11-26 14:30:30 -060019/**
20 * @class Handler
21 *
22 * This class acts on the signals generated by the
23 * xyz.openbmc_project.Chassis.Buttons code when
24 * it detects button presses.
25 *
26 * There are 3 buttons supported - Power, ID, and Reset.
27 * As not all systems may implement each button, this class will
28 * check for that button on D-Bus before listening for its signals.
29 */
30class Handler
31{
32 public:
33 Handler() = delete;
34 ~Handler() = default;
35 Handler(const Handler&) = delete;
36 Handler& operator=(const Handler&) = delete;
37 Handler(Handler&&) = delete;
38 Handler& operator=(Handler&&) = delete;
39
40 /**
41 * @brief Constructor
42 *
43 * @param[in] bus - sdbusplus connection object
44 */
Patrick Williams9a529a62022-07-22 19:26:54 -050045 explicit Handler(sdbusplus::bus_t& bus);
Matt Spinlerfb35a322018-11-26 14:30:30 -060046
47 private:
48 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060049 * @brief The handler for a power button press
50 *
DelphineCCChiu395c7642023-04-19 10:46:47 +080051 * It will do power action according to the pressing duration.
Matt Spinler963c65f2018-11-26 14:46:41 -060052 *
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 /**
Matt Spinler69f93512018-11-26 14:55:58 -060058 * @brief The handler for an ID button press
59 *
60 * Toggles the ID LED group
61 *
62 * @param[in] msg - sdbusplus message from signal
63 */
Patrick Williams9a529a62022-07-22 19:26:54 -050064 void idReleased(sdbusplus::message_t& msg);
Matt Spinler69f93512018-11-26 14:55:58 -060065
66 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060067 * @brief The handler for a reset button press
68 *
69 * Reboots the host if it is powered on.
70 *
71 * @param[in] msg - sdbusplus message from signal
72 */
Patrick Williams9a529a62022-07-22 19:26:54 -050073 void resetReleased(sdbusplus::message_t& msg);
Matt Spinler06a5bdd2018-11-26 14:50:48 -060074
75 /**
Naveen Mosesa6d4e652022-04-13 19:27:25 +053076 * @brief The handler for a OCP debug card host selector button press
77 *
78 * In multi host system increases host position by 1 up to max host
79 * position.
80 *
81 * @param[in] msg - sdbusplus message from signal
82 */
83
Patrick Williamse3b4e112022-11-26 09:41:58 -060084 void debugHostSelectorReleased(sdbusplus::message_t& msg);
Naveen Mosesa6d4e652022-04-13 19:27:25 +053085
86 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060087 * @brief Checks if system is powered on
88 *
89 * @return true if powered on, false else
90 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053091 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -060092
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053093 /*
Matt Spinler963c65f2018-11-26 14:46:41 -060094 * @return std::string - the D-Bus service name if found, else
95 * an empty string
96 */
97 std::string getService(const std::string& path,
98 const std::string& interface) const;
99
100 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530101 * @brief gets the valid host selector value in multi host
102 * system
103 *
104 * @return size_t throws exception if host selector position is
105 * invalid or not available.
106 */
107
108 size_t getHostSelectorValue();
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530109 /**
110 * @brief increases the host selector position property
111 * by 1 upto max host selector position
112 *
113 * @return void
114 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530115
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530116 void increaseHostSelectorPosition();
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530117 /**
118 * @brief checks if the system has multi host
119 * based on the host selector property availability
120 *
121 * @return bool returns true if multi host system
122 * else returns false.
123 */
124 bool isMultiHost();
125 /**
126 * @brief trigger the power ctrl event based on the
127 * button press event type.
128 *
129 * @return void
130 */
DelphineCCChiu395c7642023-04-19 10:46:47 +0800131 void handlePowerEvent(PowerEvent powerEventType,
132 std::chrono::microseconds duration);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530133
134 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600135 * @brief sdbusplus connection object
136 */
Patrick Williams9a529a62022-07-22 19:26:54 -0500137 sdbusplus::bus_t& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600138
139 /**
140 * @brief Matches on the power button released signal
141 */
142 std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
143
144 /**
145 * @brief Matches on the power button long press released signal
146 */
Naveen Mosesab8dac52022-07-15 19:32:27 +0530147 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600148
149 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600150 * @brief Matches on the ID button released signal
151 */
152 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
153
154 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600155 * @brief Matches on the reset button released signal
156 */
157 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530158
159 /**
160 * @brief Matches on the ocp debug host selector button released signal
161 */
162 std::unique_ptr<sdbusplus::bus::match_t> debugHSButtonReleased;
Matt Spinler1a309f72023-04-04 13:12:19 -0500163
164 /**
165 * @brief The custom power handler profile object.
166 */
167 std::unique_ptr<PowerButtonProfile> powerButtonProfile;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600168};
169
170} // namespace button
171} // namespace phosphor