blob: acdae196470c40acdcaf947a8dee7399d55a7d20 [file] [log] [blame]
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05301#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 <fstream>
12#include <iostream>
13#include <nlohmann/json.hpp>
14#include <phosphor-logging/elog-errors.hpp>
15
16static constexpr std::string_view HOST_SELECTOR = "HOST_SELECTOR";
17
18static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
19
20enum class GpioState
21{
22 low,
23 high
24};
25
26class HostSelector final : public sdbusplus::server::object_t<
27 sdbusplus::xyz::openbmc_project::Chassis::
28 Buttons::server::HostSelector>,
29 public ButtonIface
30{
31 public:
32 HostSelector(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
33 buttonConfig& buttonCfg) :
34 sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Chassis::
35 Buttons::server::HostSelector>(
36 bus, path, action::defer_emit),
37 ButtonIface(bus, event, buttonCfg)
38 {
39 init();
40 // read and store the host selector position Map
41 hsPosMap = buttonCfg.extraJsonInfo.at("host_selector_map")
42 .get<std::map<std::string, int>>();
43 maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
44 gpioLineCount = buttonCfg.gpios.size();
45 setInitialHostSelectorValue();
46 emit_object_added();
47 }
48
49 ~HostSelector()
50 {
51 deInit();
52 }
53
54 static constexpr std::string_view getFormFactorName()
55 {
56 return HOST_SELECTOR;
57 }
58
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
69 protected:
70 size_t hostSelectorPosition = 0;
71 size_t gpioLineCount;
72
73 // map of read Host selector switch value and corresponding host number
74 // value.
75 std::map<std::string, int> hsPosMap;
76};