xref: /openbmc/openpower-vpd-parser/vpd-manager/gpioMonitor.hpp (revision 08dc31cd99172460b86e7552d0eeb21eb4197446)
1b17dd3b3SAlpana Kumari #pragma once
2523af2e0SSunny Srivastava #include "types.hpp"
3b17dd3b3SAlpana Kumari 
4523af2e0SSunny Srivastava #include <boost/asio/steady_timer.hpp>
5523af2e0SSunny Srivastava #include <nlohmann/json.hpp>
6523af2e0SSunny Srivastava #include <sdbusplus/asio/connection.hpp>
7b17dd3b3SAlpana Kumari 
8b17dd3b3SAlpana Kumari namespace openpower
9b17dd3b3SAlpana Kumari {
10b17dd3b3SAlpana Kumari namespace vpd
11b17dd3b3SAlpana Kumari {
12b17dd3b3SAlpana Kumari namespace manager
13b17dd3b3SAlpana Kumari {
14b17dd3b3SAlpana Kumari /** @class GpioEventHandler
15b17dd3b3SAlpana Kumari  *  @brief Responsible for catching the event and handle it.
16b17dd3b3SAlpana Kumari  *         This keeps checking for the FRU's presence.
17b17dd3b3SAlpana Kumari  *         If any attachment or de-attachment found, it enables/disables that
18b17dd3b3SAlpana Kumari  * fru's output gpio and bind/unbind the driver, respectively.
19b17dd3b3SAlpana Kumari  */
20b17dd3b3SAlpana Kumari class GpioEventHandler
21b17dd3b3SAlpana Kumari {
22b17dd3b3SAlpana Kumari   public:
23b17dd3b3SAlpana Kumari     GpioEventHandler() = default;
24b17dd3b3SAlpana Kumari     ~GpioEventHandler() = default;
25b17dd3b3SAlpana Kumari     GpioEventHandler(const GpioEventHandler&) = default;
26b17dd3b3SAlpana Kumari     GpioEventHandler& operator=(const GpioEventHandler&) = delete;
27b17dd3b3SAlpana Kumari     GpioEventHandler(GpioEventHandler&&) = delete;
28b17dd3b3SAlpana Kumari     GpioEventHandler& operator=(GpioEventHandler&&) = delete;
29b17dd3b3SAlpana Kumari 
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)30b17dd3b3SAlpana Kumari     GpioEventHandler(std::string& presPin, Byte& presValue, std::string& outPin,
31b17dd3b3SAlpana Kumari                      Byte& outValue, std::string& devAddr, std::string& driver,
32b17dd3b3SAlpana Kumari                      std::string& bus, std::string& objPath,
33523af2e0SSunny Srivastava                      std::shared_ptr<boost::asio::io_context>& ioCon) :
34*08dc31cdSPatrick Williams         presencePin(presPin), presenceValue(presValue), outputPin(outPin),
35*08dc31cdSPatrick Williams         outputValue(outValue), devNameAddr(devAddr), driverType(driver),
36*08dc31cdSPatrick Williams         busType(bus), objectPath(objPath)
37b17dd3b3SAlpana Kumari     {
38523af2e0SSunny Srivastava         doEventAndTimerSetup(ioCon);
39b17dd3b3SAlpana Kumari     }
40b17dd3b3SAlpana Kumari 
41b17dd3b3SAlpana Kumari   private:
42af921756SManojkiran Eda     /** @brief GPIO information to get parsed from vpd json*/
43b17dd3b3SAlpana Kumari 
44b17dd3b3SAlpana Kumari     // gpio pin indicates presence/absence of fru
45b17dd3b3SAlpana Kumari     const std::string presencePin;
46b17dd3b3SAlpana Kumari     // value which means fru is present
47b17dd3b3SAlpana Kumari     const Byte presenceValue;
48b17dd3b3SAlpana Kumari     // gpio pin to enable If fru is present
49b17dd3b3SAlpana Kumari     const std::string outputPin;
50b17dd3b3SAlpana Kumari     // Value to set, to enable the output pin
51b17dd3b3SAlpana Kumari     const Byte outputValue;
52b17dd3b3SAlpana Kumari 
53b17dd3b3SAlpana Kumari     // FRU address on bus
54b17dd3b3SAlpana Kumari     const std::string devNameAddr;
55b17dd3b3SAlpana Kumari     // Driver type
56b17dd3b3SAlpana Kumari     const std::string driverType;
57b17dd3b3SAlpana Kumari     // Bus type
58b17dd3b3SAlpana Kumari     const std::string busType;
59b17dd3b3SAlpana Kumari     // object path of FRU
60b17dd3b3SAlpana Kumari     const std::string objectPath;
61b17dd3b3SAlpana Kumari 
62b17dd3b3SAlpana Kumari     /** Preserves the GPIO pin value to compare it next time. Default init by
63b17dd3b3SAlpana Kumari      *  false*/
64b17dd3b3SAlpana Kumari     bool prevPresPinValue = false;
65b17dd3b3SAlpana Kumari 
66b17dd3b3SAlpana Kumari     /** @brief This is a helper function to read the
67b17dd3b3SAlpana Kumari      *        current value of Presence GPIO
68b17dd3b3SAlpana Kumari      *
69b17dd3b3SAlpana Kumari      *  @returns The GPIO value
70b17dd3b3SAlpana Kumari      */
71b17dd3b3SAlpana Kumari     bool getPresencePinValue();
72b17dd3b3SAlpana Kumari 
73b17dd3b3SAlpana Kumari     /** @brief This function will toggle the output gpio as per the presence
74b17dd3b3SAlpana Kumari      *         state of fru.
75b17dd3b3SAlpana Kumari      */
76b17dd3b3SAlpana Kumari     void toggleGpio();
77b17dd3b3SAlpana Kumari 
78b17dd3b3SAlpana Kumari     /** @brief This function checks for fru's presence pin and detects change of
79b17dd3b3SAlpana Kumari      *         value on that pin, (in case of fru gets attached or de-attached).
80b17dd3b3SAlpana Kumari      *
81b17dd3b3SAlpana Kumari      *  @returns true if presence pin value changed
82b17dd3b3SAlpana Kumari      *           false otherwise
83b17dd3b3SAlpana Kumari      */
hasEventOccurred()84b17dd3b3SAlpana Kumari     inline bool hasEventOccurred()
85b17dd3b3SAlpana Kumari     {
86b17dd3b3SAlpana Kumari         return getPresencePinValue() != prevPresPinValue;
87b17dd3b3SAlpana Kumari     }
88b17dd3b3SAlpana Kumari 
89b17dd3b3SAlpana Kumari     /** @brief This function runs a timer , which keeps checking for if an event
90af921756SManojkiran Eda      *         happened, if event occurred then takes action.
91523af2e0SSunny Srivastava      *
92523af2e0SSunny Srivastava      *  @param[in] ioContext - Pointer to io context object.
93b17dd3b3SAlpana Kumari      */
94523af2e0SSunny Srivastava     void doEventAndTimerSetup(
95523af2e0SSunny Srivastava         std::shared_ptr<boost::asio::io_context>& ioContext);
96523af2e0SSunny Srivastava 
97523af2e0SSunny Srivastava     /**
98523af2e0SSunny Srivastava      * @brief Api to handle timer expiry.
99523af2e0SSunny Srivastava      *
100523af2e0SSunny Srivastava      * @param ec - Error code.
101523af2e0SSunny Srivastava      * @param timer - Pointer to timer object.
102523af2e0SSunny Srivastava      */
103523af2e0SSunny Srivastava     void handleTimerExpiry(const boost::system::error_code& ec,
104523af2e0SSunny Srivastava                            std::shared_ptr<boost::asio::steady_timer>& timer);
105b17dd3b3SAlpana Kumari };
106b17dd3b3SAlpana Kumari 
107b17dd3b3SAlpana Kumari /** @class GpioMonitor
108b17dd3b3SAlpana Kumari  *  @brief Responsible for initialising the private variables containing gpio
109af921756SManojkiran Eda  *         infos. These information will be fetched from vpd json.
110b17dd3b3SAlpana Kumari  */
111b17dd3b3SAlpana Kumari class GpioMonitor
112b17dd3b3SAlpana Kumari {
113b17dd3b3SAlpana Kumari   public:
114b17dd3b3SAlpana Kumari     GpioMonitor() = delete;
115b17dd3b3SAlpana Kumari     ~GpioMonitor() = default;
116b17dd3b3SAlpana Kumari     GpioMonitor(const GpioMonitor&) = delete;
117b17dd3b3SAlpana Kumari     GpioMonitor& operator=(const GpioMonitor&) = delete;
118b17dd3b3SAlpana Kumari     GpioMonitor(GpioMonitor&&) = delete;
119b17dd3b3SAlpana Kumari     GpioMonitor& operator=(GpioMonitor&&) = delete;
120b17dd3b3SAlpana Kumari 
GpioMonitor(nlohmann::json & js,std::shared_ptr<boost::asio::io_context> & ioCon)121523af2e0SSunny Srivastava     GpioMonitor(nlohmann::json& js,
122*08dc31cdSPatrick Williams                 std::shared_ptr<boost::asio::io_context>& ioCon) : jsonFile(js)
123b17dd3b3SAlpana Kumari     {
124523af2e0SSunny Srivastava         initGpioInfos(ioCon);
125b17dd3b3SAlpana Kumari     }
126b17dd3b3SAlpana Kumari 
127b17dd3b3SAlpana Kumari   private:
128af921756SManojkiran Eda     // Json file to get the data
129b17dd3b3SAlpana Kumari     nlohmann::json& jsonFile;
130b17dd3b3SAlpana Kumari     // Array of event handlers for all the attachable FRUs
131b17dd3b3SAlpana Kumari     std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects;
132b17dd3b3SAlpana Kumari 
133af921756SManojkiran Eda     /** @brief This function will extract the gpio information from vpd json
134b17dd3b3SAlpana Kumari      * and store it in GpioEventHandler's private variables
135523af2e0SSunny Srivastava      *
136523af2e0SSunny Srivastava      * @param[in] ioContext - Pointer to io context object.
137b17dd3b3SAlpana Kumari      */
138523af2e0SSunny Srivastava     void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext);
139b17dd3b3SAlpana Kumari };
140b17dd3b3SAlpana Kumari 
141b17dd3b3SAlpana Kumari } // namespace manager
142b17dd3b3SAlpana Kumari } // namespace vpd
143b17dd3b3SAlpana Kumari } // namespace openpower
144