1 #pragma once
2 #include "button_factory.hpp"
3 #include "button_interface.hpp"
4 #include "common.hpp"
5 #include "config.hpp"
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 
12 #include <nlohmann/json.hpp>
13 #include <phosphor-logging/elog-errors.hpp>
14 
15 #include <fstream>
16 #include <iostream>
17 
18 static constexpr std::string_view HOST_SELECTOR = "HOST_SELECTOR";
19 
20 static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
21 
22 class HostSelector final :
23     public sdbusplus::server::object_t<
24         sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::
25             HostSelector>,
26     public ButtonIface
27 {
28   public:
HostSelector(sdbusplus::bus_t & bus,const char * path,EventPtr & event,ButtonConfig & buttonCfg)29     HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
30                  ButtonConfig& buttonCfg) :
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
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         }
44         setInitialHostSelectorValue();
45         maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
46         emit_object_added();
47     }
48 
~HostSelector()49     ~HostSelector()
50     {
51         deInit();
52     }
53 
getFormFactorName()54     static constexpr std::string_view getFormFactorName()
55     {
56         return HOST_SELECTOR;
57     }
58 
getDbusObjectPath()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);
68     char getValueFromFd(int fd);
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 };
78