blob: 70e31e0355f30a756a1939e1bdae75be52953bfa [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"
Delphine CC Chiu15c60e22024-04-12 13:01:32 -05005#include "config.hpp"
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05306#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
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053012#include <nlohmann/json.hpp>
13#include <phosphor-logging/elog-errors.hpp>
14
George Liu5b98f4d2022-06-20 13:31:14 +080015#include <fstream>
16#include <iostream>
17
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053018static constexpr std::string_view HOST_SELECTOR = "HOST_SELECTOR";
19
20static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
21
George Liu5b98f4d2022-06-20 13:31:14 +080022class HostSelector final :
23 public sdbusplus::server::object_t<
24 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::
25 HostSelector>,
26 public ButtonIface
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053027{
28 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050029 HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080030 ButtonConfig& buttonCfg) :
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053031 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
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080038 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 }
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053044 setInitialHostSelectorValue();
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080045 maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053046 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);
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080068 char getValueFromFd(int fd);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053069
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};