blob: 960b9b49e03a7987df76eea11a16955c66720571 [file] [log] [blame]
Alpana Kumarib17dd3b2020-10-01 00:18:10 -05001#include "gpioMonitor.hpp"
2
3#include "common_utility.hpp"
4#include "ibm_vpd_utils.hpp"
5
6#include <systemd/sd-event.h>
7
8#include <chrono>
9#include <gpiod.hpp>
10#include <sdeventplus/clock.hpp>
11#include <sdeventplus/utility/timer.hpp>
12
13using namespace std;
14using namespace openpower::vpd::constants;
15using sdeventplus::ClockId;
16using sdeventplus::Event;
17using Timer = sdeventplus::utility::Timer<ClockId::Monotonic>;
18using namespace std::chrono_literals;
19
20namespace openpower
21{
22namespace vpd
23{
24namespace manager
25{
26
27bool GpioEventHandler::getPresencePinValue()
28{
29 Byte gpioData = 1;
30 gpiod::line presenceLine = gpiod::find_line(presencePin);
31 if (!presenceLine)
32 {
33 cerr << "Error getPresencePinValue: couldn't find presence line:"
34 << presencePin << " on GPIO \n";
35 // return previous state as we couldn't read current state
36 return prevPresPinValue;
37 }
38
39 presenceLine.request(
40 {"Op-panel presence line", gpiod::line_request::DIRECTION_INPUT, 0});
41
42 gpioData = presenceLine.get_value();
43
44 return gpioData;
45}
46
47void GpioMonitor::initGpioInfos(Event& event)
48{
49 Byte outputValue = 0;
50 Byte presenceValue = 0;
51 string presencePinName{}, outputPinName{};
52 string devNameAddr{}, driverType{}, busType{}, objectPath{};
53
54 for (const auto& eachFRU : jsonFile["frus"].items())
55 {
56 for (const auto& eachInventory : eachFRU.value())
57 {
58 objectPath = eachInventory["inventoryPath"];
59
60 if ((eachInventory.find("presence") != eachInventory.end()) &&
61 (eachInventory.find("preAction") != eachInventory.end()))
62 {
Alpana Kumari187bc272022-02-10 23:18:09 -060063 if (!eachInventory["presence"].value("pollingRequired", false))
64 {
65 // Polling not required for this FRU , skip.
66 continue;
67 }
68
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050069 for (const auto& presStatus : eachInventory["presence"].items())
70 {
71 if (presStatus.key() == "pin")
72 {
73 presencePinName = presStatus.value();
74 }
75 else if (presStatus.key() == "value")
76 {
77 presenceValue = presStatus.value();
78 }
79 }
80
81 // Based on presence pin value, preAction pin will be set/reset
82 // This action will be taken before vpd collection.
83 for (const auto& preAction : eachInventory["preAction"].items())
84 {
85 if (preAction.key() == "pin")
86 {
87 outputPinName = preAction.value();
88 }
89 else if (preAction.key() == "value")
90 {
91 outputValue = preAction.value();
92 }
93 }
94
95 devNameAddr = eachInventory["devAddress"];
96 driverType = eachInventory["driverType"];
97 busType = eachInventory["busType"];
98
99 // Init all Gpio info variables
100 std::shared_ptr<GpioEventHandler> gpioObj =
101 make_shared<GpioEventHandler>(
102 presencePinName, presenceValue, outputPinName,
103 outputValue, devNameAddr, driverType, busType,
104 objectPath, event);
105
106 gpioObjects.push_back(gpioObj);
107 }
108 }
109 }
110}
111
112void GpioEventHandler::toggleGpio()
113{
114 bool presPinVal = getPresencePinValue();
115 bool isPresent = false;
116
117 // preserve the new value
118 prevPresPinValue = presPinVal;
119
120 if (presPinVal == presenceValue)
121 {
122 isPresent = true;
123 }
124
125 // if FRU went away set the present property to false
126 if (!isPresent)
127 {
128 inventory::ObjectMap objects;
129 inventory::InterfaceMap interfaces;
130 inventory::PropertyMap presProp;
131
132 presProp.emplace("Present", false);
133 interfaces.emplace("xyz.openbmc_project.Inventory.Item", presProp);
134 objects.emplace(objectPath, move(interfaces));
135
136 common::utility::callPIM(move(objects));
137 }
138
139 gpiod::line outputLine = gpiod::find_line(outputPin);
140 if (!outputLine)
141 {
142 cerr << "Error: toggleGpio: couldn't find output line:" << outputPin
143 << ". Skipping update\n";
144
145 return;
146 }
147
148 outputLine.request({"FRU presence: update the output GPIO pin",
149 gpiod::line_request::DIRECTION_OUTPUT, 0},
150 isPresent ? outputValue : (!outputValue));
151
152 string cmnd = createBindUnbindDriverCmnd(devNameAddr, busType, driverType,
153 isPresent ? "bind" : "unbind");
154
155 cout << cmnd << endl;
156 executeCmd(cmnd);
157}
158
159void GpioEventHandler::doEventAndTimerSetup(sdeventplus::Event& event)
160{
161 prevPresPinValue = getPresencePinValue();
162
163 static vector<shared_ptr<Timer>> timers;
164 shared_ptr<Timer> timer = make_shared<Timer>(
165 event,
166 [this](Timer&) {
167 if (hasEventOccurred())
168 {
169 toggleGpio();
170 }
171 },
172 std::chrono::seconds{5s});
173
174 timers.push_back(timer);
175}
176
177} // namespace manager
178} // namespace vpd
179} // namespace openpower