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