blob: efbccd2bc6c9ab4d5d19e381a17e582c5f13a8bd [file] [log] [blame]
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05301
2#include "hostSelector_switch.hpp"
3
George Liu9fb15972022-06-20 14:54:38 +08004#include <error.h>
5
6#include <phosphor-logging/lg2.hpp>
7
Naveen Moses3bd1cfc2022-02-14 18:04:20 +05308// add the button iface class to registry
9static ButtonIFRegister<HostSelector> buttonRegister;
George Liu9fb15972022-06-20 14:54:38 +080010
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053011size_t HostSelector::getMappedHSConfig(size_t hsPosition)
12{
13 size_t adjustedPosition = INVALID_INDEX; // set bmc as default value
14 std::string hsPosStr;
15 hsPosStr = std::to_string(hsPosition);
16
17 if (hsPosMap.find(hsPosStr) != hsPosMap.end())
18 {
19 adjustedPosition = hsPosMap[hsPosStr];
20 }
21 else
22 {
George Liu9fb15972022-06-20 14:54:38 +080023 lg2::debug("getMappedHSConfig : {TYPE}: no valid value in map.", "TYPE",
24 getFormFactorType());
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053025 }
26 return adjustedPosition;
27}
28
29size_t HostSelector::getGpioIndex(int fd)
30{
31 for (size_t index = 0; index < gpioLineCount; index++)
32 {
33 if (config.gpios[index].fd == fd)
34 {
35 return index;
36 }
37 }
38 return INVALID_INDEX;
39}
40void HostSelector::setInitialHostSelectorValue()
41{
42 char buf;
George Liu94afa4b2022-06-20 13:36:43 +080043 for (size_t index = 0; index < gpioLineCount; index++)
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053044 {
45 auto result = ::lseek(config.gpios[index].fd, 0, SEEK_SET);
46
47 if (result < 0)
48 {
George Liu9fb15972022-06-20 14:54:38 +080049 lg2::error("{TYPE}: Gpio fd lseek error: {ERROR}", "TYPE",
50 getFormFactorType(), "ERROR", errno);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053051 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
52 IOError();
53 }
54
55 result = ::read(config.gpios[index].fd, &buf, sizeof(buf));
56 if (result < 0)
57 {
George Liu9fb15972022-06-20 14:54:38 +080058 lg2::error("{TYPE}: Gpio fd read error: {ERROR}", "TYPE",
59 getFormFactorType(), "ERROR", errno);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053060 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
61 IOError();
62 }
63 GpioState gpioState =
Naveen Mosesd219fa32022-07-20 00:01:46 +053064 (buf == '0') ? (GpioState::deassert) : (GpioState::assert);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053065 setHostSelectorValue(config.gpios[index].fd, gpioState);
66 size_t hsPosMapped = getMappedHSConfig(hostSelectorPosition);
67 if (hsPosMapped != INVALID_INDEX)
68 {
69 position(hsPosMapped, true);
70 }
71 }
72}
73
74void HostSelector::setHostSelectorValue(int fd, GpioState state)
75{
76 size_t pos = getGpioIndex(fd);
77
78 if (pos == INVALID_INDEX)
79 {
80 return;
81 }
82 auto set_bit = [](size_t& val, size_t n) { val |= 0xff & (1 << n); };
83
84 auto clr_bit = [](size_t& val, size_t n) { val &= ~(0xff & (1 << n)); };
85
Naveen Mosesd219fa32022-07-20 00:01:46 +053086 auto bit_op = (state == GpioState::deassert) ? set_bit : clr_bit;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053087
88 bit_op(hostSelectorPosition, pos);
89 return;
90}
91/**
92 * @brief This method is called from sd-event provided callback function
93 * callbackHandler if platform specific event handling is needed then a
94 * derived class instance with its specific event handling logic along with
95 * init() function can be created to override the default event handling
96 */
97
George Liu94afa4b2022-06-20 13:36:43 +080098void HostSelector::handleEvent(sd_event_source* /* es */, int fd,
99 uint32_t /* revents */)
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530100{
101 int n = -1;
102 char buf = '0';
103
104 n = ::lseek(fd, 0, SEEK_SET);
105
106 if (n < 0)
107 {
George Liu9fb15972022-06-20 14:54:38 +0800108 lg2::error("{TYPE}: Gpio fd lseek error: {ERROR}", "TYPE",
109 getFormFactorType(), "ERROR", errno);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530110 return;
111 }
112
113 n = ::read(fd, &buf, sizeof(buf));
114 if (n < 0)
115 {
George Liu9fb15972022-06-20 14:54:38 +0800116 lg2::error("{TYPE}: Gpio fd read error: {ERROR}", "TYPE",
117 getFormFactorType(), "ERROR", errno);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530118 throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
119 IOError();
120 }
121
122 // read the gpio state for the io event received
Naveen Mosesd219fa32022-07-20 00:01:46 +0530123 GpioState gpioState =
124 (buf == '0') ? (GpioState::deassert) : (GpioState::assert);
Naveen Moses3bd1cfc2022-02-14 18:04:20 +0530125
126 setHostSelectorValue(fd, gpioState);
127
128 size_t hsPosMapped = getMappedHSConfig(hostSelectorPosition);
129
130 if (hsPosMapped != INVALID_INDEX)
131 {
132 position(hsPosMapped);
133 }
134}