blob: 66c049d8e2ce86d904c4906eedb73353717dfdd5 [file] [log] [blame]
Alpana Kumarib17dd3b2020-10-01 00:18:10 -05001#pragma once
2
3#include "manager.hpp"
4
5#include <sdeventplus/event.hpp>
6
7namespace openpower
8{
9namespace vpd
10{
11namespace manager
12{
13/** @class GpioEventHandler
14 * @brief Responsible for catching the event and handle it.
15 * This keeps checking for the FRU's presence.
16 * If any attachment or de-attachment found, it enables/disables that
17 * fru's output gpio and bind/unbind the driver, respectively.
18 */
19class GpioEventHandler
20{
21 public:
22 GpioEventHandler() = default;
23 ~GpioEventHandler() = default;
24 GpioEventHandler(const GpioEventHandler&) = default;
25 GpioEventHandler& operator=(const GpioEventHandler&) = delete;
26 GpioEventHandler(GpioEventHandler&&) = delete;
27 GpioEventHandler& operator=(GpioEventHandler&&) = delete;
28
29 GpioEventHandler(std::string& presPin, Byte& presValue, std::string& outPin,
30 Byte& outValue, std::string& devAddr, std::string& driver,
31 std::string& bus, std::string& objPath,
32 sdeventplus::Event& event) :
33 presencePin(presPin),
34 presenceValue(presValue), outputPin(outPin), outputValue(outValue),
35 devNameAddr(devAddr), driverType(driver), busType(bus),
36 objectPath(objPath)
37 {
38 doEventAndTimerSetup(event);
39 }
40
41 private:
42 /** @brief GPIO informations to get parsed from vpd json*/
43
44 // gpio pin indicates presence/absence of fru
45 const std::string presencePin;
46 // value which means fru is present
47 const Byte presenceValue;
48 // gpio pin to enable If fru is present
49 const std::string outputPin;
50 // Value to set, to enable the output pin
51 const Byte outputValue;
52
53 // FRU address on bus
54 const std::string devNameAddr;
55 // Driver type
56 const std::string driverType;
57 // Bus type
58 const std::string busType;
59 // object path of FRU
60 const std::string objectPath;
61
62 /** Preserves the GPIO pin value to compare it next time. Default init by
63 * false*/
64 bool prevPresPinValue = false;
65
66 /** @brief This is a helper function to read the
67 * current value of Presence GPIO
68 *
69 * @returns The GPIO value
70 */
71 bool getPresencePinValue();
72
73 /** @brief This function will toggle the output gpio as per the presence
74 * state of fru.
75 */
76 void toggleGpio();
77
78 /** @brief This function checks for fru's presence pin and detects change of
79 * value on that pin, (in case of fru gets attached or de-attached).
80 *
81 * @returns true if presence pin value changed
82 * false otherwise
83 */
84 inline bool hasEventOccurred()
85 {
86 return getPresencePinValue() != prevPresPinValue;
87 }
88
89 /** @brief This function runs a timer , which keeps checking for if an event
90 * happened, if event occured then takes action.
91 * @param[in] timer- Shared pointer of Timer to do event setup for each
92 * object.
93 * @param[in] event- Event which needs to be tagged with the timer.
94 */
95 void doEventAndTimerSetup(sdeventplus::Event& event);
96};
97
98/** @class GpioMonitor
99 * @brief Responsible for initialising the private variables containing gpio
100 * infos. These informations will be fetched from vpd json.
101 */
102class GpioMonitor
103{
104 public:
105 GpioMonitor() = delete;
106 ~GpioMonitor() = default;
107 GpioMonitor(const GpioMonitor&) = delete;
108 GpioMonitor& operator=(const GpioMonitor&) = delete;
109 GpioMonitor(GpioMonitor&&) = delete;
110 GpioMonitor& operator=(GpioMonitor&&) = delete;
111
112 GpioMonitor(nlohmann::json& js, sdeventplus::Event& event) : jsonFile(js)
113 {
114 initGpioInfos(event);
115 }
116
117 private:
118 // Json file to get the datas
119 nlohmann::json& jsonFile;
120 // Array of event handlers for all the attachable FRUs
121 std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects;
122
123 /** @brief This function will extract the gpio informations from vpd json
124 * and store it in GpioEventHandler's private variables
125 * @param[in] gpioObj - shared object to initialise it's data and it's
126 * Timer setup
127 * @param[in] requestedGpioPin - Which GPIO's informations need to be
128 * stored
129 * @param[in] timer - shared object of timer to do the event setup
130 * @param[in] event - event to be tagged with timer.
131 */
132 void initGpioInfos(sdeventplus::Event& event);
133};
134
135} // namespace manager
136} // namespace vpd
137} // namespace openpower