blob: 99053040f032040198a31813d2a217e391b4a928 [file] [log] [blame]
Naveen Mosesa1af3292021-12-15 11:47:01 +05301#pragma once
2
3#include "common.hpp"
4#include "gpio.hpp"
5#include "xyz/openbmc_project/Chassis/Common/error.hpp"
6
7#include <phosphor-logging/elog-errors.hpp>
8// This is the base class for all the button interface types
9//
10class ButtonIface
11{
Naveen Mosesa1af3292021-12-15 11:47:01 +053012 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050013 ButtonIface(sdbusplus::bus_t& bus, EventPtr& event, buttonConfig& buttonCfg,
Naveen Mosesa1af3292021-12-15 11:47:01 +053014 sd_event_io_handler_t handler = ButtonIface::EventHandler) :
15 bus(bus),
16 event(event), config(buttonCfg), callbackHandler(handler)
17 {
18 int ret = -1;
19
20 // config group gpio based on the gpio defs read from the json file
21 ret = configGroupGpio(config);
22
23 if (ret < 0)
24 {
25 phosphor::logging::log<phosphor::logging::level::ERR>(
26 (getFormFactorType() + " : failed to config GPIO").c_str());
27 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
28 IOError();
29 }
30 }
Patrick Williams0d038f52023-05-10 07:50:40 -050031 virtual ~ButtonIface() {}
Naveen Mosesa1af3292021-12-15 11:47:01 +053032
33 /**
34 * @brief This method is called from sd-event provided callback function
35 * callbackHandler if platform specific event handling is needed then a
36 * derived class instance with its specific evend handling logic along with
37 * init() function can be created to override the default event handling.
38 */
39
40 virtual void handleEvent(sd_event_source* es, int fd, uint32_t revents) = 0;
41 static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
42 void* userdata)
43 {
44 if (userdata)
45 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053046 ButtonIface* buttonIface = static_cast<ButtonIface*>(userdata);
47 buttonIface->handleEvent(es, fd, revents);
Naveen Mosesa1af3292021-12-15 11:47:01 +053048 }
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053049
50 return 0;
Naveen Mosesa1af3292021-12-15 11:47:01 +053051 }
52
53 std::string getFormFactorType() const
54 {
55 return config.formFactorName;
56 }
57
58 protected:
59 /**
60 * @brief oem specific initialization can be done under init function.
61 * if platform specific initialization is needed then
62 * a derived class instance with its own init function to override the
63 * default init() method can be added.
64 */
65
66 virtual void init()
67 {
Naveen Mosesa1af3292021-12-15 11:47:01 +053068 // initialize the button io fd from the buttonConfig
69 // which has fd stored when configGroupGpio is called
70 for (auto gpioCfg : config.gpios)
71 {
72 char buf;
73 int fd = gpioCfg.fd;
74
75 int ret = ::read(fd, &buf, sizeof(buf));
76 if (ret < 0)
77 {
78 phosphor::logging::log<phosphor::logging::level::ERR>(
79 (getFormFactorType() + " : read error!").c_str());
80 }
81
82 ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
83 callbackHandler, this);
84 if (ret < 0)
85 {
86 phosphor::logging::log<phosphor::logging::level::ERR>(
87 (getFormFactorType() + " : failed to add to event loop")
88 .c_str());
89 ::closeGpio(fd);
90 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
91 IOError();
92 }
93 }
94 }
95 /**
96 * @brief similar to init() oem specific deinitialization can be done under
97 * deInit function. if platform specific deinitialization is needed then a
98 * derived class instance with its own init function to override the default
99 * deinit() method can be added.
100 */
101 virtual void deInit()
102 {
103 for (auto gpioCfg : config.gpios)
104 {
105 ::closeGpio(gpioCfg.fd);
106 }
107 }
108
Patrick Williams9a529a62022-07-22 19:26:54 -0500109 sdbusplus::bus_t& bus;
Naveen Mosesa1af3292021-12-15 11:47:01 +0530110 EventPtr& event;
George Liu94afa4b2022-06-20 13:36:43 +0800111 buttonConfig config;
Naveen Mosesa1af3292021-12-15 11:47:01 +0530112 sd_event_io_handler_t callbackHandler;
113};