| Christopher Meis | 75ff167 | 2025-09-23 11:40:06 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 4 | #pragma once |
| 5 | |
| 6 | #include <gpiod.h> |
| 7 | |
| Ed Tanous | 854404e | 2023-02-28 13:37:51 -0800 | [diff] [blame] | 8 | #include <boost/asio/io_context.hpp> |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 9 | #include <boost/asio/posix/stream_descriptor.hpp> |
| Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 10 | |
| Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <vector> |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace gpio |
| 17 | { |
| 18 | |
| 19 | /** @class GpioMonitor |
| 20 | * @brief Responsible for catching GPIO state change |
| 21 | * condition and starting systemd targets. |
| 22 | */ |
| 23 | class GpioMonitor |
| 24 | { |
| 25 | public: |
| 26 | GpioMonitor() = delete; |
| 27 | ~GpioMonitor() = default; |
| 28 | GpioMonitor(const GpioMonitor&) = delete; |
| 29 | GpioMonitor& operator=(const GpioMonitor&) = delete; |
| 30 | GpioMonitor(GpioMonitor&&) = delete; |
| 31 | GpioMonitor& operator=(GpioMonitor&&) = delete; |
| 32 | |
| 33 | /** @brief Constructs GpioMonitor object. |
| 34 | * |
| 35 | * @param[in] line - GPIO line from libgpiod |
| 36 | * @param[in] config - configuration of line with event |
| 37 | * @param[in] io - io service |
| 38 | * @param[in] target - systemd unit to be started on GPIO |
| 39 | * value change |
| Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 40 | * @param[in] targets - systemd units to be started on GPIO |
| 41 | * value change |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 42 | * @param[in] lineMsg - GPIO line message to be used for log |
| 43 | * @param[in] continueRun - Whether to continue after event occur |
| 44 | */ |
| 45 | GpioMonitor(gpiod_line* line, gpiod_line_request_config& config, |
| Ed Tanous | 854404e | 2023-02-28 13:37:51 -0800 | [diff] [blame] | 46 | boost::asio::io_context& io, const std::string& target, |
| Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 47 | const std::map<std::string, std::vector<std::string>>& targets, |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 48 | const std::string& lineMsg, bool continueRun) : |
| Patrick Williams | 8377d59 | 2024-08-16 15:21:08 -0400 | [diff] [blame] | 49 | gpioLine(line), gpioConfig(config), gpioEventDescriptor(io), |
| 50 | target(target), targets(targets), gpioLineMsg(lineMsg), |
| 51 | continueAfterEvent(continueRun) |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 52 | { |
| 53 | requestGPIOEvents(); |
| 54 | }; |
| 55 | |
| 56 | private: |
| 57 | /** @brief GPIO line */ |
| 58 | gpiod_line* gpioLine; |
| 59 | |
| 60 | /** @brief GPIO line configuration */ |
| 61 | gpiod_line_request_config gpioConfig; |
| 62 | |
| 63 | /** @brief GPIO event descriptor */ |
| 64 | boost::asio::posix::stream_descriptor gpioEventDescriptor; |
| 65 | |
| 66 | /** @brief Systemd unit to be started when the condition is met */ |
| 67 | const std::string target; |
| 68 | |
| Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 69 | /** @brief Multi systemd units to be started when the condition is met */ |
| 70 | std::map<std::string, std::vector<std::string>> targets; |
| 71 | |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 72 | /** @brief GPIO line name message */ |
| 73 | std::string gpioLineMsg; |
| 74 | |
| 75 | /** @brief If the monitor should continue after event */ |
| 76 | bool continueAfterEvent; |
| 77 | |
| 78 | /** @brief register handler for gpio event |
| 79 | * |
| 80 | * @return - 0 on success and -1 otherwise |
| 81 | */ |
| 82 | int requestGPIOEvents(); |
| 83 | |
| 84 | /** @brief Schedule an event handler for GPIO event to trigger */ |
| 85 | void scheduleEventHandler(); |
| 86 | |
| 87 | /** @brief Handle the GPIO event and starts configured target */ |
| 88 | void gpioEventHandler(); |
| Amithash Prasad | ba917cf | 2025-04-21 14:52:43 -0700 | [diff] [blame] | 89 | |
| 90 | /** @brief handle current gpio value */ |
| 91 | void gpioHandleInitialState(bool value); |
| Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace gpio |
| 95 | } // namespace phosphor |