blob: 58fbf910622707864a2c805aee016cb73b1ed3b9 [file] [log] [blame]
Alpana Kumarib17dd3b2020-10-01 00:18:10 -05001#pragma once
Sunny Srivastava523af2e2022-02-14 07:30:10 -06002#include "types.hpp"
Alpana Kumarib17dd3b2020-10-01 00:18:10 -05003
Sunny Srivastava523af2e2022-02-14 07:30:10 -06004#include <boost/asio/steady_timer.hpp>
5#include <nlohmann/json.hpp>
6#include <sdbusplus/asio/connection.hpp>
Alpana Kumarib17dd3b2020-10-01 00:18:10 -05007
8namespace openpower
9{
10namespace vpd
11{
12namespace manager
13{
14/** @class GpioEventHandler
15 * @brief Responsible for catching the event and handle it.
16 * This keeps checking for the FRU's presence.
17 * If any attachment or de-attachment found, it enables/disables that
18 * fru's output gpio and bind/unbind the driver, respectively.
19 */
20class GpioEventHandler
21{
22 public:
23 GpioEventHandler() = default;
24 ~GpioEventHandler() = default;
25 GpioEventHandler(const GpioEventHandler&) = default;
26 GpioEventHandler& operator=(const GpioEventHandler&) = delete;
27 GpioEventHandler(GpioEventHandler&&) = delete;
28 GpioEventHandler& operator=(GpioEventHandler&&) = delete;
29
30 GpioEventHandler(std::string& presPin, Byte& presValue, std::string& outPin,
31 Byte& outValue, std::string& devAddr, std::string& driver,
32 std::string& bus, std::string& objPath,
Sunny Srivastava523af2e2022-02-14 07:30:10 -060033 std::shared_ptr<boost::asio::io_context>& ioCon) :
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050034 presencePin(presPin),
35 presenceValue(presValue), outputPin(outPin), outputValue(outValue),
36 devNameAddr(devAddr), driverType(driver), busType(bus),
37 objectPath(objPath)
38 {
Sunny Srivastava523af2e2022-02-14 07:30:10 -060039 doEventAndTimerSetup(ioCon);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050040 }
41
42 private:
43 /** @brief GPIO informations to get parsed from vpd json*/
44
45 // gpio pin indicates presence/absence of fru
46 const std::string presencePin;
47 // value which means fru is present
48 const Byte presenceValue;
49 // gpio pin to enable If fru is present
50 const std::string outputPin;
51 // Value to set, to enable the output pin
52 const Byte outputValue;
53
54 // FRU address on bus
55 const std::string devNameAddr;
56 // Driver type
57 const std::string driverType;
58 // Bus type
59 const std::string busType;
60 // object path of FRU
61 const std::string objectPath;
62
63 /** Preserves the GPIO pin value to compare it next time. Default init by
64 * false*/
65 bool prevPresPinValue = false;
66
67 /** @brief This is a helper function to read the
68 * current value of Presence GPIO
69 *
70 * @returns The GPIO value
71 */
72 bool getPresencePinValue();
73
74 /** @brief This function will toggle the output gpio as per the presence
75 * state of fru.
76 */
77 void toggleGpio();
78
79 /** @brief This function checks for fru's presence pin and detects change of
80 * value on that pin, (in case of fru gets attached or de-attached).
81 *
82 * @returns true if presence pin value changed
83 * false otherwise
84 */
85 inline bool hasEventOccurred()
86 {
87 return getPresencePinValue() != prevPresPinValue;
88 }
89
90 /** @brief This function runs a timer , which keeps checking for if an event
91 * happened, if event occured then takes action.
Sunny Srivastava523af2e2022-02-14 07:30:10 -060092 *
93 * @param[in] ioContext - Pointer to io context object.
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050094 */
Sunny Srivastava523af2e2022-02-14 07:30:10 -060095 void doEventAndTimerSetup(
96 std::shared_ptr<boost::asio::io_context>& ioContext);
97
98 /**
99 * @brief Api to handle timer expiry.
100 *
101 * @param ec - Error code.
102 * @param timer - Pointer to timer object.
103 */
104 void handleTimerExpiry(const boost::system::error_code& ec,
105 std::shared_ptr<boost::asio::steady_timer>& timer);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500106};
107
108/** @class GpioMonitor
109 * @brief Responsible for initialising the private variables containing gpio
110 * infos. These informations will be fetched from vpd json.
111 */
112class GpioMonitor
113{
114 public:
115 GpioMonitor() = delete;
116 ~GpioMonitor() = default;
117 GpioMonitor(const GpioMonitor&) = delete;
118 GpioMonitor& operator=(const GpioMonitor&) = delete;
119 GpioMonitor(GpioMonitor&&) = delete;
120 GpioMonitor& operator=(GpioMonitor&&) = delete;
121
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600122 GpioMonitor(nlohmann::json& js,
123 std::shared_ptr<boost::asio::io_context>& ioCon) :
124 jsonFile(js)
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500125 {
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600126 initGpioInfos(ioCon);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500127 }
128
129 private:
130 // Json file to get the datas
131 nlohmann::json& jsonFile;
132 // Array of event handlers for all the attachable FRUs
133 std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects;
134
135 /** @brief This function will extract the gpio informations from vpd json
136 * and store it in GpioEventHandler's private variables
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600137 *
138 * @param[in] ioContext - Pointer to io context object.
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500139 */
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600140 void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500141};
142
143} // namespace manager
144} // namespace vpd
145} // namespace openpower