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