Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 1 | #pragma once |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 2 | #include "types.hpp" |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 3 | |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 4 | #include <boost/asio/steady_timer.hpp> |
| 5 | #include <nlohmann/json.hpp> |
| 6 | #include <sdbusplus/asio/connection.hpp> |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 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 | |
| 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, |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 33 | std::shared_ptr<boost::asio::io_context>& ioCon) : |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 34 | presencePin(presPin), |
| 35 | presenceValue(presValue), outputPin(outPin), outputValue(outValue), |
| 36 | devNameAddr(devAddr), driverType(driver), busType(bus), |
| 37 | objectPath(objPath) |
| 38 | { |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 39 | doEventAndTimerSetup(ioCon); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | private: |
| 43 | /** @brief GPIO informations 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 | */ |
| 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 occured then takes action. |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 92 | * |
| 93 | * @param[in] ioContext - Pointer to io context object. |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 94 | */ |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 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); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /** @class GpioMonitor |
| 109 | * @brief Responsible for initialising the private variables containing gpio |
| 110 | * infos. These informations 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 | |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 122 | GpioMonitor(nlohmann::json& js, |
| 123 | std::shared_ptr<boost::asio::io_context>& ioCon) : |
| 124 | jsonFile(js) |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 125 | { |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 126 | initGpioInfos(ioCon); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
| 130 | // Json file to get the datas |
| 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 informations from vpd json |
| 136 | * and store it in GpioEventHandler's private variables |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 137 | * |
| 138 | * @param[in] ioContext - Pointer to io context object. |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 139 | */ |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 140 | void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace manager |
| 144 | } // namespace vpd |
| 145 | } // namespace openpower |