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