Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | #include "button_factory.hpp" |
| 3 | #include "button_interface.hpp" |
| 4 | #include "common.hpp" |
Delphine CC Chiu | 15c60e2 | 2024-04-12 13:01:32 -0500 | [diff] [blame] | 5 | #include "config.hpp" |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 6 | #include "gpio.hpp" |
| 7 | #include "xyz/openbmc_project/Chassis/Buttons/HostSelector/server.hpp" |
| 8 | #include "xyz/openbmc_project/Chassis/Common/error.hpp" |
| 9 | |
| 10 | #include <unistd.h> |
| 11 | |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
| 14 | |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 15 | #include <fstream> |
| 16 | #include <iostream> |
| 17 | |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 18 | static constexpr std::string_view HOST_SELECTOR = "HOST_SELECTOR"; |
| 19 | |
| 20 | static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max(); |
| 21 | |
George Liu | 5b98f4d | 2022-06-20 13:31:14 +0800 | [diff] [blame] | 22 | class HostSelector final : |
| 23 | public sdbusplus::server::object_t< |
| 24 | sdbusplus::xyz::openbmc_project::Chassis::Buttons::server:: |
| 25 | HostSelector>, |
| 26 | public ButtonIface |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 27 | { |
| 28 | public: |
Patrick Williams | 9a529a6 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 29 | HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event, |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 30 | ButtonConfig& buttonCfg) : |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 31 | sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Chassis:: |
| 32 | Buttons::server::HostSelector>( |
| 33 | bus, path, action::defer_emit), |
| 34 | ButtonIface(bus, event, buttonCfg) |
| 35 | { |
| 36 | init(); |
| 37 | // read and store the host selector position Map |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 38 | if (buttonCfg.type == ConfigType::gpio) |
| 39 | { |
| 40 | hsPosMap = buttonCfg.extraJsonInfo.at("host_selector_map") |
| 41 | .get<std::map<std::string, int>>(); |
| 42 | gpioLineCount = buttonCfg.gpios.size(); |
| 43 | } |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 44 | setInitialHostSelectorValue(); |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 45 | maxPosition(buttonCfg.extraJsonInfo["max_position"], true); |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 46 | emit_object_added(); |
| 47 | } |
| 48 | |
| 49 | ~HostSelector() |
| 50 | { |
| 51 | deInit(); |
| 52 | } |
| 53 | |
| 54 | static constexpr std::string_view getFormFactorName() |
| 55 | { |
| 56 | return HOST_SELECTOR; |
| 57 | } |
| 58 | |
| 59 | static const char* getDbusObjectPath() |
| 60 | { |
| 61 | return HS_DBUS_OBJECT_NAME; |
| 62 | } |
| 63 | void handleEvent(sd_event_source* es, int fd, uint32_t revents) override; |
| 64 | size_t getMappedHSConfig(size_t hsPosition); |
| 65 | size_t getGpioIndex(int fd); |
| 66 | void setInitialHostSelectorValue(void); |
| 67 | void setHostSelectorValue(int fd, GpioState state); |
Delphine CC Chiu | ccd7db0 | 2023-02-09 14:48:53 +0800 | [diff] [blame] | 68 | char getValueFromFd(int fd); |
Naveen Moses | 3bd1cfc | 2022-02-14 18:04:20 +0530 | [diff] [blame] | 69 | |
| 70 | protected: |
| 71 | size_t hostSelectorPosition = 0; |
| 72 | size_t gpioLineCount; |
| 73 | |
| 74 | // map of read Host selector switch value and corresponding host number |
| 75 | // value. |
| 76 | std::map<std::string, int> hsPosMap; |
| 77 | }; |