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