blob: 5f3433600b5b82a5bb261a6d8f39b333303ff1ef [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) :
Patrick Williams08dc31c2024-08-16 15:21:06 -040034 presencePin(presPin), presenceValue(presValue), outputPin(outPin),
35 outputValue(outValue), devNameAddr(devAddr), driverType(driver),
36 busType(bus), objectPath(objPath)
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050037 {
Sunny Srivastava523af2e2022-02-14 07:30:10 -060038 doEventAndTimerSetup(ioCon);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050039 }
40
41 private:
Manojkiran Edaaf921752024-06-17 15:10:21 +053042 /** @brief GPIO information to get parsed from vpd json*/
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050043
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
Manojkiran Edaaf921752024-06-17 15:10:21 +053090 * happened, if event occurred then takes action.
Sunny Srivastava523af2e2022-02-14 07:30:10 -060091 *
92 * @param[in] ioContext - Pointer to io context object.
Alpana Kumarib17dd3b2020-10-01 00:18:10 -050093 */
Sunny Srivastava523af2e2022-02-14 07:30:10 -060094 void doEventAndTimerSetup(
95 std::shared_ptr<boost::asio::io_context>& ioContext);
96
97 /**
98 * @brief Api to handle timer expiry.
99 *
100 * @param ec - Error code.
101 * @param timer - Pointer to timer object.
102 */
103 void handleTimerExpiry(const boost::system::error_code& ec,
104 std::shared_ptr<boost::asio::steady_timer>& timer);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500105};
106
107/** @class GpioMonitor
108 * @brief Responsible for initialising the private variables containing gpio
Manojkiran Edaaf921752024-06-17 15:10:21 +0530109 * infos. These information will be fetched from vpd json.
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500110 */
111class GpioMonitor
112{
113 public:
114 GpioMonitor() = delete;
115 ~GpioMonitor() = default;
116 GpioMonitor(const GpioMonitor&) = delete;
117 GpioMonitor& operator=(const GpioMonitor&) = delete;
118 GpioMonitor(GpioMonitor&&) = delete;
119 GpioMonitor& operator=(GpioMonitor&&) = delete;
120
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600121 GpioMonitor(nlohmann::json& js,
Patrick Williams08dc31c2024-08-16 15:21:06 -0400122 std::shared_ptr<boost::asio::io_context>& ioCon) : jsonFile(js)
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500123 {
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600124 initGpioInfos(ioCon);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500125 }
126
127 private:
Manojkiran Edaaf921752024-06-17 15:10:21 +0530128 // Json file to get the data
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500129 nlohmann::json& jsonFile;
130 // Array of event handlers for all the attachable FRUs
131 std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects;
132
Manojkiran Edaaf921752024-06-17 15:10:21 +0530133 /** @brief This function will extract the gpio information from vpd json
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500134 * and store it in GpioEventHandler's private variables
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600135 *
136 * @param[in] ioContext - Pointer to io context object.
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500137 */
Sunny Srivastava523af2e2022-02-14 07:30:10 -0600138 void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext);
Alpana Kumarib17dd3b2020-10-01 00:18:10 -0500139};
140
141} // namespace manager
142} // namespace vpd
Patrick Williamsc78d8872023-05-10 07:50:56 -0500143} // namespace openpower