Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 3 | #include "button_config.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 4 | #include "common.hpp" |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 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 | // |
| 10 | class ButtonIface |
| 11 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 12 | public: |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 13 | ButtonIface(sdbusplus::bus_t& bus, EventPtr& event, ButtonConfig& buttonCfg, |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 14 | sd_event_io_handler_t handler = ButtonIface::EventHandler) : |
| 15 | bus(bus), |
| 16 | event(event), config(buttonCfg), callbackHandler(handler) |
| 17 | { |
| 18 | int ret = -1; |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 19 | std::string configType; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 20 | |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 21 | // config group gpio or cpld based on the defs read from the json file |
| 22 | if (buttonCfg.type == ConfigType::gpio) |
| 23 | { |
| 24 | configType = "GPIO"; |
| 25 | ret = configGroupGpio(config); |
| 26 | } |
| 27 | else if (buttonCfg.type == ConfigType::cpld) |
| 28 | { |
| 29 | configType = "CPLD"; |
| 30 | ret = configCpld(config); |
| 31 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 32 | |
| 33 | if (ret < 0) |
| 34 | { |
| 35 | phosphor::logging::log<phosphor::logging::level::ERR>( |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 36 | (getFormFactorType() + " : failed to config " + configType) |
| 37 | .c_str()); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 38 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 39 | IOError(); |
| 40 | } |
| 41 | } |
Patrick Williams | 0d038f5 | 2023-05-10 07:50:40 -0500 | [diff] [blame] | 42 | virtual ~ButtonIface() {} |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @brief This method is called from sd-event provided callback function |
| 46 | * callbackHandler if platform specific event handling is needed then a |
| 47 | * derived class instance with its specific evend handling logic along with |
| 48 | * init() function can be created to override the default event handling. |
| 49 | */ |
| 50 | |
| 51 | virtual void handleEvent(sd_event_source* es, int fd, uint32_t revents) = 0; |
| 52 | static int EventHandler(sd_event_source* es, int fd, uint32_t revents, |
| 53 | void* userdata) |
| 54 | { |
| 55 | if (userdata) |
| 56 | { |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 57 | ButtonIface* buttonIface = static_cast<ButtonIface*>(userdata); |
| 58 | buttonIface->handleEvent(es, fd, revents); |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 59 | } |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 60 | |
| 61 | return 0; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | std::string getFormFactorType() const |
| 65 | { |
| 66 | return config.formFactorName; |
| 67 | } |
| 68 | |
| 69 | protected: |
| 70 | /** |
| 71 | * @brief oem specific initialization can be done under init function. |
| 72 | * if platform specific initialization is needed then |
| 73 | * a derived class instance with its own init function to override the |
| 74 | * default init() method can be added. |
| 75 | */ |
| 76 | |
| 77 | virtual void init() |
| 78 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 79 | // initialize the button io fd from the ButtonConfig |
| 80 | // which has fd stored when configGroupGpio or configCpld is called |
| 81 | for (auto fd : config.fds) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 82 | { |
| 83 | char buf; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 84 | |
| 85 | int ret = ::read(fd, &buf, sizeof(buf)); |
| 86 | if (ret < 0) |
| 87 | { |
| 88 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 89 | (getFormFactorType() + " : read error!").c_str()); |
| 90 | } |
| 91 | |
| 92 | ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI, |
| 93 | callbackHandler, this); |
| 94 | if (ret < 0) |
| 95 | { |
| 96 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 97 | (getFormFactorType() + " : failed to add to event loop") |
| 98 | .c_str()); |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 99 | if (fd > 0) |
| 100 | { |
| 101 | ::close(fd); |
| 102 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 103 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 104 | IOError(); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | /** |
| 109 | * @brief similar to init() oem specific deinitialization can be done under |
| 110 | * deInit function. if platform specific deinitialization is needed then a |
| 111 | * derived class instance with its own init function to override the default |
| 112 | * deinit() method can be added. |
| 113 | */ |
| 114 | virtual void deInit() |
| 115 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 116 | for (auto fd : config.fds) |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 117 | { |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 118 | if (fd > 0) |
| 119 | { |
| 120 | ::close(fd); |
| 121 | } |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 125 | sdbusplus::bus_t& bus; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 126 | EventPtr& event; |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame^] | 127 | ButtonConfig config; |
Naveen Moses | a1af329 | 2021-12-15 11:47:01 +0530 | [diff] [blame] | 128 | sd_event_io_handler_t callbackHandler; |
| 129 | }; |