blob: 806da062b2889c50ae5c8d5d770d60e270dec348 [file] [log] [blame]
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05301#pragma once
George Liud6a1bae2022-06-20 13:47:31 +08002#include "config.h"
3
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05304#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
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053013#include <nlohmann/json.hpp>
14#include <phosphor-logging/elog-errors.hpp>
15
George Liu5b98f4d2022-06-20 13:31:14 +080016#include <fstream>
17#include <iostream>
18
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053019static constexpr std::string_view HOST_SELECTOR = "HOST_SELECTOR";
20
21static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
22
George Liu5b98f4d2022-06-20 13:31:14 +080023class HostSelector final :
24 public sdbusplus::server::object_t<
25 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::
26 HostSelector>,
27 public ButtonIface
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053028{
29 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050030 HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080031 ButtonConfig& buttonCfg) :
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053032 sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::Chassis::
33 Buttons::server::HostSelector>(
34 bus, path, action::defer_emit),
35 ButtonIface(bus, event, buttonCfg)
36 {
37 init();
38 // read and store the host selector position Map
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080039 if (buttonCfg.type == ConfigType::gpio)
40 {
41 hsPosMap = buttonCfg.extraJsonInfo.at("host_selector_map")
42 .get<std::map<std::string, int>>();
43 gpioLineCount = buttonCfg.gpios.size();
44 }
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053045 setInitialHostSelectorValue();
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080046 maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053047 emit_object_added();
48 }
49
50 ~HostSelector()
51 {
52 deInit();
53 }
54
55 static constexpr std::string_view getFormFactorName()
56 {
57 return HOST_SELECTOR;
58 }
59
60 static const char* getDbusObjectPath()
61 {
62 return HS_DBUS_OBJECT_NAME;
63 }
64 void handleEvent(sd_event_source* es, int fd, uint32_t revents) override;
65 size_t getMappedHSConfig(size_t hsPosition);
66 size_t getGpioIndex(int fd);
67 void setInitialHostSelectorValue(void);
68 void setHostSelectorValue(int fd, GpioState state);
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080069 char getValueFromFd(int fd);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053070
71 protected:
72 size_t hostSelectorPosition = 0;
73 size_t gpioLineCount;
74
75 // map of read Host selector switch value and corresponding host number
76 // value.
77 std::map<std::string, int> hsPosMap;
78};