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