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