1 #pragma once
2 #include "types.hpp"
3 
4 #include <boost/asio/steady_timer.hpp>
5 #include <nlohmann/json.hpp>
6 #include <sdbusplus/asio/connection.hpp>
7 
8 namespace openpower
9 {
10 namespace vpd
11 {
12 namespace 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  */
20 class 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 
GpioEventHandler(std::string & presPin,Byte & presValue,std::string & outPin,Byte & outValue,std::string & devAddr,std::string & driver,std::string & bus,std::string & objPath,std::shared_ptr<boost::asio::io_context> & ioCon)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,
33                      std::shared_ptr<boost::asio::io_context>& ioCon) :
34         presencePin(presPin), presenceValue(presValue), outputPin(outPin),
35         outputValue(outValue), devNameAddr(devAddr), driverType(driver),
36         busType(bus), objectPath(objPath)
37     {
38         doEventAndTimerSetup(ioCon);
39     }
40 
41   private:
42     /** @brief GPIO information 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      */
hasEventOccurred()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 occurred then takes action.
91      *
92      *  @param[in] ioContext - Pointer to io context object.
93      */
94     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);
105 };
106 
107 /** @class GpioMonitor
108  *  @brief Responsible for initialising the private variables containing gpio
109  *         infos. These information will be fetched from vpd json.
110  */
111 class 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 
GpioMonitor(nlohmann::json & js,std::shared_ptr<boost::asio::io_context> & ioCon)121     GpioMonitor(nlohmann::json& js,
122                 std::shared_ptr<boost::asio::io_context>& ioCon) : jsonFile(js)
123     {
124         initGpioInfos(ioCon);
125     }
126 
127   private:
128     // Json file to get the data
129     nlohmann::json& jsonFile;
130     // Array of event handlers for all the attachable FRUs
131     std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects;
132 
133     /** @brief This function will extract the gpio information from vpd json
134      * and store it in GpioEventHandler's private variables
135      *
136      * @param[in] ioContext - Pointer to io context object.
137      */
138     void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext);
139 };
140 
141 } // namespace manager
142 } // namespace vpd
143 } // namespace openpower
144