blob: 8d1b319b9a2474f2a1a69b3372b94a2496174428 [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 */
George Liu94afa4b2022-06-20 13:36:43 +080044 explicit Handler(sdbusplus::bus::bus& 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 */
Naveen Mosesab8dac52022-07-15 19:32:27 +053055 void powerReleased(sdbusplus::message::message& 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 */
65 void longPowerPressed(sdbusplus::message::message& msg);
66
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 */
Naveen Mosesab8dac52022-07-15 19:32:27 +053074 void idReleased(sdbusplus::message::message& 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 */
Naveen Mosesab8dac52022-07-15 19:32:27 +053083 void resetReleased(sdbusplus::message::message& msg);
Matt Spinler06a5bdd2018-11-26 14:50:48 -060084
85 /**
Matt Spinler963c65f2018-11-26 14:46:41 -060086 * @brief Checks if system is powered on
87 *
88 * @return true if powered on, false else
89 */
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053090 bool poweredOn(size_t hostNumber) const;
Matt Spinler963c65f2018-11-26 14:46:41 -060091
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053092 /*
Matt Spinler963c65f2018-11-26 14:46:41 -060093 * @return std::string - the D-Bus service name if found, else
94 * an empty string
95 */
96 std::string getService(const std::string& path,
97 const std::string& interface) const;
98
99 /**
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530100 * @brief gets the valid host selector value in multi host
101 * system
102 *
103 * @return size_t throws exception if host selector position is
104 * invalid or not available.
105 */
106
107 size_t getHostSelectorValue();
108
109 /**
110 * @brief checks if the system has multi host
111 * based on the host selector property availability
112 *
113 * @return bool returns true if multi host system
114 * else returns false.
115 */
116 bool isMultiHost();
117 /**
118 * @brief trigger the power ctrl event based on the
119 * button press event type.
120 *
121 * @return void
122 */
123 void handlePowerEvent(PowerEvent powerEventType);
124
125 /**
Matt Spinlerfb35a322018-11-26 14:30:30 -0600126 * @brief sdbusplus connection object
127 */
128 sdbusplus::bus::bus& bus;
Matt Spinler963c65f2018-11-26 14:46:41 -0600129
130 /**
131 * @brief Matches on the power button released signal
132 */
133 std::unique_ptr<sdbusplus::bus::match_t> powerButtonReleased;
134
135 /**
136 * @brief Matches on the power button long press released signal
137 */
Naveen Mosesab8dac52022-07-15 19:32:27 +0530138 std::unique_ptr<sdbusplus::bus::match_t> powerButtonLongPressed;
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600139
140 /**
Matt Spinler69f93512018-11-26 14:55:58 -0600141 * @brief Matches on the ID button released signal
142 */
143 std::unique_ptr<sdbusplus::bus::match_t> idButtonReleased;
144
145 /**
Matt Spinler06a5bdd2018-11-26 14:50:48 -0600146 * @brief Matches on the reset button released signal
147 */
148 std::unique_ptr<sdbusplus::bus::match_t> resetButtonReleased;
Matt Spinlerfb35a322018-11-26 14:30:30 -0600149};
150
151} // namespace button
152} // namespace phosphor