blob: abce90ac1c16bab53228c3453f29aa2be13bf1f3 [file] [log] [blame]
Matt Spinler1a309f72023-04-04 13:12:19 -05001#pragma once
2
Delphine CC Chiu15c60e22024-04-12 13:01:32 -05003#include "config.hpp"
Matt Spinler1a309f72023-04-04 13:12:19 -05004
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
7#include <sdbusplus/message.hpp>
8
9#include <string>
10
11namespace phosphor::button
12{
13
14constexpr auto powerButtonInterface =
15 "xyz.openbmc_project.Chassis.Buttons.Power";
16namespace 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 */
27class PowerButtonProfile
28{
29 public:
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)),
Patrick Williamsd36b6b12024-08-16 15:20:34 -040038 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))
Matt Spinler1a309f72023-04-04 13:12:19 -050045 {}
46
47 virtual ~PowerButtonProfile() = default;
48
49 void pressedHandler(sdbusplus::message_t /* msg*/)
50 {
51 pressed();
52 }
53
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