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),
35         presenceValue(presValue), outputPin(outPin), outputValue(outValue),
36         devNameAddr(devAddr), driverType(driver), busType(bus),
37         objectPath(objPath)
38     {
39         doEventAndTimerSetup(ioCon);
40     }
41 
42   private:
43     /** @brief GPIO information 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      */
hasEventOccurred()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 occurred then takes action.
92      *
93      *  @param[in] ioContext - Pointer to io context object.
94      */
95     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);
106 };
107 
108 /** @class GpioMonitor
109  *  @brief Responsible for initialising the private variables containing gpio
110  *         infos. These information will be fetched from vpd json.
111  */
112 class 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 
GpioMonitor(nlohmann::json & js,std::shared_ptr<boost::asio::io_context> & ioCon)122     GpioMonitor(nlohmann::json& js,
123                 std::shared_ptr<boost::asio::io_context>& ioCon) :
124         jsonFile(js)
125     {
126         initGpioInfos(ioCon);
127     }
128 
129   private:
130     // Json file to get the data
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 information from vpd json
136      * and store it in GpioEventHandler's private variables
137      *
138      * @param[in] ioContext - Pointer to io context object.
139      */
140     void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext);
141 };
142 
143 } // namespace manager
144 } // namespace vpd
145 } // namespace openpower
146