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) : |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 34 | presencePin(presPin), presenceValue(presValue), outputPin(outPin), |
| 35 | outputValue(outValue), devNameAddr(devAddr), driverType(driver), |
| 36 | busType(bus), objectPath(objPath) |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 37 | { |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 38 | doEventAndTimerSetup(ioCon); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | private: |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 42 | /** @brief GPIO information to get parsed from vpd json*/ |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 43 | |
| 44 | // gpio pin indicates presence/absence of fru |
| 45 | const std::string presencePin; |
| 46 | // value which means fru is present |
| 47 | const Byte presenceValue; |
| 48 | // gpio pin to enable If fru is present |
| 49 | const std::string outputPin; |
| 50 | // Value to set, to enable the output pin |
| 51 | const Byte outputValue; |
| 52 | |
| 53 | // FRU address on bus |
| 54 | const std::string devNameAddr; |
| 55 | // Driver type |
| 56 | const std::string driverType; |
| 57 | // Bus type |
| 58 | const std::string busType; |
| 59 | // object path of FRU |
| 60 | const std::string objectPath; |
| 61 | |
| 62 | /** Preserves the GPIO pin value to compare it next time. Default init by |
| 63 | * false*/ |
| 64 | bool prevPresPinValue = false; |
| 65 | |
| 66 | /** @brief This is a helper function to read the |
| 67 | * current value of Presence GPIO |
| 68 | * |
| 69 | * @returns The GPIO value |
| 70 | */ |
| 71 | bool getPresencePinValue(); |
| 72 | |
| 73 | /** @brief This function will toggle the output gpio as per the presence |
| 74 | * state of fru. |
| 75 | */ |
| 76 | void toggleGpio(); |
| 77 | |
| 78 | /** @brief This function checks for fru's presence pin and detects change of |
| 79 | * value on that pin, (in case of fru gets attached or de-attached). |
| 80 | * |
| 81 | * @returns true if presence pin value changed |
| 82 | * false otherwise |
| 83 | */ |
| 84 | inline bool hasEventOccurred() |
| 85 | { |
| 86 | return getPresencePinValue() != prevPresPinValue; |
| 87 | } |
| 88 | |
| 89 | /** @brief This function runs a timer , which keeps checking for if an event |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 90 | * happened, if event occurred then takes action. |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 91 | * |
| 92 | * @param[in] ioContext - Pointer to io context object. |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 93 | */ |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 94 | void doEventAndTimerSetup( |
| 95 | std::shared_ptr<boost::asio::io_context>& ioContext); |
| 96 | |
| 97 | /** |
| 98 | * @brief Api to handle timer expiry. |
| 99 | * |
| 100 | * @param ec - Error code. |
| 101 | * @param timer - Pointer to timer object. |
| 102 | */ |
| 103 | void handleTimerExpiry(const boost::system::error_code& ec, |
| 104 | std::shared_ptr<boost::asio::steady_timer>& timer); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** @class GpioMonitor |
| 108 | * @brief Responsible for initialising the private variables containing gpio |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 109 | * infos. These information will be fetched from vpd json. |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 110 | */ |
| 111 | class GpioMonitor |
| 112 | { |
| 113 | public: |
| 114 | GpioMonitor() = delete; |
| 115 | ~GpioMonitor() = default; |
| 116 | GpioMonitor(const GpioMonitor&) = delete; |
| 117 | GpioMonitor& operator=(const GpioMonitor&) = delete; |
| 118 | GpioMonitor(GpioMonitor&&) = delete; |
| 119 | GpioMonitor& operator=(GpioMonitor&&) = delete; |
| 120 | |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 121 | GpioMonitor(nlohmann::json& js, |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 122 | std::shared_ptr<boost::asio::io_context>& ioCon) : jsonFile(js) |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 123 | { |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 124 | initGpioInfos(ioCon); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private: |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 128 | // Json file to get the data |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 129 | nlohmann::json& jsonFile; |
| 130 | // Array of event handlers for all the attachable FRUs |
| 131 | std::vector<std::shared_ptr<GpioEventHandler>> gpioObjects; |
| 132 | |
Manojkiran Eda | af92175 | 2024-06-17 15:10:21 +0530 | [diff] [blame] | 133 | /** @brief This function will extract the gpio information from vpd json |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 134 | * and store it in GpioEventHandler's private variables |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 135 | * |
| 136 | * @param[in] ioContext - Pointer to io context object. |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 137 | */ |
Sunny Srivastava | 523af2e | 2022-02-14 07:30:10 -0600 | [diff] [blame] | 138 | void initGpioInfos(std::shared_ptr<boost::asio::io_context>& ioContext); |
Alpana Kumari | b17dd3b | 2020-10-01 00:18:10 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // namespace manager |
| 142 | } // namespace vpd |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 143 | } // namespace openpower |