blob: 2d68891a8002698948af8722e5a31781bf1ee5cd [file] [log] [blame]
Christopher Meis75ff1672025-09-23 11:40:06 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3
Vijay Khemka939a6432019-10-09 17:45:45 -07004#pragma once
5
6#include <gpiod.h>
7
Ed Tanous854404e2023-02-28 13:37:51 -08008#include <boost/asio/io_context.hpp>
Vijay Khemka939a6432019-10-09 17:45:45 -07009#include <boost/asio/posix/stream_descriptor.hpp>
Patrick Williams39084b42023-05-10 07:50:58 -050010
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080011#include <map>
12#include <vector>
Vijay Khemka939a6432019-10-09 17:45:45 -070013
14namespace phosphor
15{
16namespace gpio
17{
18
19/** @class GpioMonitor
20 * @brief Responsible for catching GPIO state change
21 * condition and starting systemd targets.
22 */
23class 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 Chiua66ac0f2023-01-09 17:12:23 +080040 * @param[in] targets - systemd units to be started on GPIO
41 * value change
Vijay Khemka939a6432019-10-09 17:45:45 -070042 * @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 Tanous854404e2023-02-28 13:37:51 -080046 boost::asio::io_context& io, const std::string& target,
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080047 const std::map<std::string, std::vector<std::string>>& targets,
Vijay Khemka939a6432019-10-09 17:45:45 -070048 const std::string& lineMsg, bool continueRun) :
Patrick Williams8377d592024-08-16 15:21:08 -040049 gpioLine(line), gpioConfig(config), gpioEventDescriptor(io),
50 target(target), targets(targets), gpioLineMsg(lineMsg),
51 continueAfterEvent(continueRun)
Vijay Khemka939a6432019-10-09 17:45:45 -070052 {
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 Chiua66ac0f2023-01-09 17:12:23 +080069 /** @brief Multi systemd units to be started when the condition is met */
70 std::map<std::string, std::vector<std::string>> targets;
71
Vijay Khemka939a6432019-10-09 17:45:45 -070072 /** @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 Prasadba917cf2025-04-21 14:52:43 -070089
90 /** @brief handle current gpio value */
91 void gpioHandleInitialState(bool value);
Vijay Khemka939a6432019-10-09 17:45:45 -070092};
93
94} // namespace gpio
95} // namespace phosphor