blob: 24947ea43f5e9d2b5b3a2162a421f5ebb7f6426d [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>
LioraGuo-wiwynn5f3c2e52025-07-20 18:04:39 +080014#include <sdeventplus/event.hpp>
15#include <sdeventplus/utility/timer.hpp>
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053016
LioraGuo-wiwynn5f3c2e52025-07-20 18:04:39 +080017#include <chrono>
George Liu5b98f4d2022-06-20 13:31:14 +080018#include <fstream>
19#include <iostream>
LioraGuo-wiwynn5f3c2e52025-07-20 18:04:39 +080020#include <optional>
21
22using sdeventplus::ClockId;
23using sdeventplus::Event;
24using Timer = sdeventplus::utility::Timer<ClockId::Monotonic>;
George Liu5b98f4d2022-06-20 13:31:14 +080025
Rush Chen31ce3752024-11-08 14:57:27 +080026static constexpr auto HOST_SELECTOR = "HOST_SELECTOR";
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053027
28static constexpr auto INVALID_INDEX = std::numeric_limits<size_t>::max();
29
George Liu5b98f4d2022-06-20 13:31:14 +080030class HostSelector final :
31 public sdbusplus::server::object_t<
32 sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::
33 HostSelector>,
34 public ButtonIface
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053035{
36 public:
Patrick Williams9a529a62022-07-22 19:26:54 -050037 HostSelector(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080038 ButtonConfig& buttonCfg) :
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053039 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
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080046 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 }
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053052 setInitialHostSelectorValue();
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080053 maxPosition(buttonCfg.extraJsonInfo["max_position"], true);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053054 emit_object_added();
55 }
56
57 ~HostSelector()
58 {
59 deInit();
60 }
61
Rush Chen31ce3752024-11-08 14:57:27 +080062 static constexpr std::string getFormFactorName()
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053063 {
64 return HOST_SELECTOR;
65 }
66
Rush Chen31ce3752024-11-08 14:57:27 +080067 static constexpr std::string getDbusObjectPath()
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053068 {
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);
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080076 char getValueFromFd(int fd);
LioraGuo-wiwynn5f3c2e52025-07-20 18:04:39 +080077 void pollGpioState();
78
79 private:
80 std::optional<Timer> pollTimer;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053081
82 protected:
83 size_t hostSelectorPosition = 0;
84 size_t gpioLineCount;
LioraGuo-wiwynn5f3c2e52025-07-20 18:04:39 +080085 size_t previousPos = INVALID_INDEX;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053086
87 // map of read Host selector switch value and corresponding host number
88 // value.
89 std::map<std::string, int> hsPosMap;
90};