1 #pragma once
2 
3 #include "config.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 #include <sdbusplus/bus/match.hpp>
7 #include <sdbusplus/message.hpp>
8 
9 #include <string>
10 
11 namespace phosphor::button
12 {
13 
14 constexpr auto powerButtonInterface =
15     "xyz.openbmc_project.Chassis.Buttons.Power";
16 namespace sdbusRule = sdbusplus::bus::match::rules;
17 
18 /**
19  * @class PowerButtonProfile
20  *
21  * Abstract base class for custom power button profiles.
22  *
23  * Calls a derived class's pressed() and released()
24  * functions when the power button is pressed and
25  * released.
26  */
27 class PowerButtonProfile
28 {
29   public:
PowerButtonProfile(sdbusplus::bus_t & bus)30     PowerButtonProfile(sdbusplus::bus_t& bus) :
31         bus(bus),
32         pressedMatch(bus,
33                      sdbusRule::type::signal() + sdbusRule::member("Pressed") +
34                          sdbusRule::path(POWER_DBUS_OBJECT_NAME) +
35                          sdbusRule::interface(powerButtonInterface),
36                      std::bind(&PowerButtonProfile::pressedHandler, this,
37                                std::placeholders::_1)),
38         releasedMatch(
39             bus,
40             sdbusRule::type::signal() + sdbusRule::member("Released") +
41                 sdbusRule::path(POWER_DBUS_OBJECT_NAME) +
42                 sdbusRule::interface(powerButtonInterface),
43             std::bind(&PowerButtonProfile::releasedHandler, this,
44                       std::placeholders::_1))
45     {}
46 
47     virtual ~PowerButtonProfile() = default;
48 
pressedHandler(sdbusplus::message_t)49     void pressedHandler(sdbusplus::message_t /* msg*/)
50     {
51         pressed();
52     }
53 
releasedHandler(sdbusplus::message_t msg)54     void releasedHandler(sdbusplus::message_t msg)
55     {
56         auto time = msg.unpack<uint64_t>();
57         released(time);
58     }
59 
60     virtual void pressed() = 0;
61 
62     virtual void released(uint64_t pressTimeMS) = 0;
63 
64   protected:
65     sdbusplus::bus_t& bus;
66 
67   private:
68     sdbusplus::bus::match_t pressedMatch;
69     sdbusplus::bus::match_t releasedMatch;
70 };
71 
72 } // namespace phosphor::button
73