blob: baa74002199b82e839fd62002117d4af470108ae [file] [log] [blame]
Matt Spinlerfb35a322018-11-26 14:30:30 -06001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/bus/match.hpp>
5
6namespace phosphor
7{
8namespace button
9{
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053010enum class PowerEvent
11{
12 powerPressed,
13 longPowerPressed,
14 resetPressed
15};
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 */
George Liu94afa4b2022-06-20 13:36:43 +080042 explicit Handler(sdbusplus::bus::bus& 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 *
48 * It will power on the system if it's currently off,
49 * else it will soft power it off.
50 *
51 * @param[in] msg - sdbusplus message from signal
52 */
53 void powerPressed(sdbusplus::message::message& msg);
54
55 /**
56 * @brief The handler for a long power button press
57 *
58 * If the system is currently powered on, it will
59 * perform an immediate power off.
60 *
61 * @param[in] msg - sdbusplus message from signal
62 */
63 void longPowerPressed(sdbusplus::message::message& msg);
64
65 /**
Matt Spinler69f93512018-11-26 14:55:58 -060066 * @brief The handler for an ID button press
67 *
68 * Toggles the ID LED group
69 *
70 * @param[in] msg - sdbusplus message from signal
71 */
72 void idPressed(sdbusplus::message::message& msg);
73
74 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -060075 * @brief The handler for a reset button press
76 *
77 * Reboots the host if it is powered on.
78 *
79 * @param[in] msg - sdbusplus message from signal
80 */
81 void resetPressed(sdbusplus::message::message& msg);
82
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();
106
107 /**
108 * @brief checks if the system has multi host
109 * based on the host selector property availability
110 *
111 * @return bool returns true if multi host system
112 * else returns false.
113 */
114 bool isMultiHost();
115 /**
116 * @brief trigger the power ctrl event based on the
117 * button press event type.
118 *
119 * @return void
120 */
121 void handlePowerEvent(PowerEvent powerEventType);
122
123 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600124 * @brief sdbusplus connection object
125 */
126 sdbusplus::bus::bus& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600127
128 /**
129 * @brief Matches on the power button released signal
130 */
131 std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
132
133 /**
134 * @brief Matches on the power button long press released signal
135 */
136 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressReleased;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600137
138 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600139 * @brief Matches on the ID button released signal
140 */
141 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
142
143 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600144 * @brief Matches on the reset button released signal
145 */
146 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600147};
148
149} // namespace button
150} // namespace phosphor