xref: /openbmc/phosphor-buttons/inc/hostSelector_switch.hpp (revision 5f3c2e5b1115d0b532c8dc5330009fea42d4f15b)
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 #include <sdeventplus/event.hpp>
15 #include <sdeventplus/utility/timer.hpp>
16 
17 #include <chrono>
18 #include <fstream>
19 #include <iostream>
20 #include <optional>
21 
22 using sdeventplus::ClockId;
23 using sdeventplus::Event;
24 using Timer = sdeventplus::utility::Timer<ClockId::Monotonic>;
25 
26 static constexpr auto HOST_SELECTOR = "HOST_SELECTOR";
27 
28 static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
29 
30 class HostSelector final :
31     public sdbusplus::server::object_t<
32         sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::
33             HostSelector>,
34     public ButtonIface
35 {
36   public:
HostSelector(sdbusplus::bus_t & bus,const char * path,EventPtr & event,ButtonConfig & buttonCfg)37     HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
38                  ButtonConfig& buttonCfg) :
39         sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Chassis::
40                                         Buttons::server::HostSelector>(
41             bus, path, action::defer_emit),
42         ButtonIface(bus, event, buttonCfg)
43     {
44         init();
45         // read and store the host selector position Map
46         if (buttonCfg.type == ConfigType::gpio)
47         {
48             hsPosMap = buttonCfg.extraJsonInfo.at("host_selector_map")
49                            .get<std::map<std::string, int>>();
50             gpioLineCount = buttonCfg.gpios.size();
51         }
52         setInitialHostSelectorValue();
53         maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
54         emit_object_added();
55     }
56 
~HostSelector()57     ~HostSelector()
58     {
59         deInit();
60     }
61 
getFormFactorName()62     static constexpr std::string getFormFactorName()
63     {
64         return HOST_SELECTOR;
65     }
66 
getDbusObjectPath()67     static constexpr std::string getDbusObjectPath()
68     {
69         return HS_DBUS_OBJECT_NAME;
70     }
71     void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
72     size_t getMappedHSConfig(size_t hsPosition);
73     size_t getGpioIndex(int fd);
74     void setInitialHostSelectorValue(void);
75     void setHostSelectorValue(int fd, GpioState state);
76     char getValueFromFd(int fd);
77     void pollGpioState();
78 
79   private:
80     std::optional<Timer> pollTimer;
81 
82   protected:
83     size_t hostSelectorPosition = 0;
84     size_t gpioLineCount;
85     size_t previousPos = INVALID_INDEX;
86 
87     // map of read Host selector switch value and corresponding host number
88     // value.
89     std::map<std::string, int> hsPosMap;
90 };
91