xref: /openbmc/openpower-vpd-parser/vpd-manager/include/gpio_monitor.hpp (revision fa5e4d325ef9cea3c841fe89d202c340f92bd8c6)
1 #pragma once
2 
3 #include "worker.hpp"
4 
5 #include <boost/asio/steady_timer.hpp>
6 #include <nlohmann/json.hpp>
7 #include <sdbusplus/asio/connection.hpp>
8 
9 #include <vector>
10 
11 namespace vpd
12 {
13 /**
14  * @brief class for GPIO event handling.
15  *
16  * Responsible for detecting events and handling them. It continuously
17  * monitors the presence of the FRU. If it detects any change, performs
18  * deletion of FRU VPD if FRU is not present, otherwise performs VPD
19  * collection if FRU gets added.
20  */
21 class GpioEventHandler
22 {
23   public:
24     GpioEventHandler() = delete;
25     ~GpioEventHandler() = default;
26     GpioEventHandler(const GpioEventHandler&) = delete;
27     GpioEventHandler& operator=(const GpioEventHandler&) = delete;
28     GpioEventHandler(GpioEventHandler&&) = delete;
29     GpioEventHandler& operator=(GpioEventHandler&&) = delete;
30 
31     /**
32      * @brief Constructor
33      *
34      * @param[in] i_fruPath - EEPROM path of the FRU.
35      * @param[in] i_worker - pointer to the worker object.
36      * @param[in] i_ioContext - pointer to the io context object
37      */
GpioEventHandler(const std::string i_fruPath,const std::shared_ptr<Worker> & i_worker,const std::shared_ptr<boost::asio::io_context> & i_ioContext)38     GpioEventHandler(
39         const std::string i_fruPath, const std::shared_ptr<Worker>& i_worker,
40         const std::shared_ptr<boost::asio::io_context>& i_ioContext) :
41         m_fruPath(i_fruPath), m_worker(i_worker)
42     {
43         setEventHandlerForGpioPresence(i_ioContext);
44     }
45 
46   private:
47     /**
48      * @brief API to take action based on GPIO presence pin value.
49      *
50      * This API takes action based on the change in the presence pin value.
51      * It performs deletion of FRU VPD if FRU is not present, otherwise performs
52      * VPD collection if FRU gets added.
53      *
54      * @param[in] i_isFruPresent - Holds the present status of the FRU.
55      */
56     void handleChangeInGpioPin(const bool& i_isFruPresent);
57 
58     /**
59      * @brief An API to set event handler for FRUs GPIO presence.
60      *
61      * An API to set timer to call event handler to detect GPIO presence
62      * of the FRU.
63      *
64      * @param[in] i_ioContext - pointer to io context object
65      */
66     void setEventHandlerForGpioPresence(
67         const std::shared_ptr<boost::asio::io_context>& i_ioContext);
68 
69     /**
70      * @brief API to handle timer expiry.
71      *
72      * This API handles timer expiry and checks on the GPIO presence state,
73      * takes action if there is any change in the GPIO presence value.
74      *
75      * @param[in] i_errorCode - Error Code
76      * @param[in] i_timerObj - Pointer to timer Object.
77      */
78     void handleTimerExpiry(
79         const boost::system::error_code& i_errorCode,
80         const std::shared_ptr<boost::asio::steady_timer>& i_timerObj);
81 
82     const std::string m_fruPath;
83 
84     const std::shared_ptr<Worker>& m_worker;
85 
86     // Preserves the GPIO pin value to compare. Default value is false.
87     bool m_prevPresencePinValue = false;
88 };
89 
90 class GpioMonitor
91 {
92   public:
93     GpioMonitor() = delete;
94     ~GpioMonitor() = default;
95     GpioMonitor(const GpioMonitor&) = delete;
96     GpioMonitor& operator=(const GpioMonitor&) = delete;
97     GpioMonitor(GpioMonitor&&) = delete;
98     GpioMonitor& operator=(GpioMonitor&&) = delete;
99 
100     /**
101      * @brief constructor
102      *
103      * @param[in] i_sysCfgJsonObj - System config JSON Object.
104      * @param[in] i_worker - pointer to the worker object.
105      * @param[in] i_ioContext - pointer to IO context object.
106      */
GpioMonitor(const nlohmann::json i_sysCfgJsonObj,const std::shared_ptr<Worker> & i_worker,const std::shared_ptr<boost::asio::io_context> & i_ioContext)107     GpioMonitor(const nlohmann::json i_sysCfgJsonObj,
108                 const std::shared_ptr<Worker>& i_worker,
109                 const std::shared_ptr<boost::asio::io_context>& i_ioContext) :
110         m_sysCfgJsonObj(i_sysCfgJsonObj)
111     {
112         if (!m_sysCfgJsonObj.empty())
113         {
114             initHandlerForGpio(i_ioContext, i_worker);
115         }
116         else
117         {
118             throw std::runtime_error(
119                 "Gpio Monitoring can't be instantiated with empty config JSON");
120         }
121     }
122 
123   private:
124     /**
125      * @brief API to instantiate GpioEventHandler for GPIO pins.
126      *
127      * This API will extract the GPIO information from system config JSON
128      * and instantiate event handler for GPIO pins.
129      *
130      * @param[in] i_ioContext - Pointer to IO context object.
131      * @param[in] i_worker - Pointer to worker class.
132      */
133     void initHandlerForGpio(
134         const std::shared_ptr<boost::asio::io_context>& i_ioContext,
135         const std::shared_ptr<Worker>& i_worker);
136 
137     // Array of event handlers for all the attachable FRUs.
138     std::vector<std::shared_ptr<GpioEventHandler>> m_gpioEventHandlerObjects;
139 
140     const nlohmann::json& m_sysCfgJsonObj;
141 };
142 } // namespace vpd
143