blob: a645e02cb1a53d00bc84a443dac1401a3065f7e2 [file] [log] [blame]
Vijay Khemka939a6432019-10-09 17:45:45 -07001#pragma once
2
3#include <gpiod.h>
4
Ed Tanous854404e2023-02-28 13:37:51 -08005#include <boost/asio/io_context.hpp>
Vijay Khemka939a6432019-10-09 17:45:45 -07006#include <boost/asio/posix/stream_descriptor.hpp>
Patrick Williams39084b42023-05-10 07:50:58 -05007
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +08008#include <map>
9#include <vector>
Vijay Khemka939a6432019-10-09 17:45:45 -070010
11namespace phosphor
12{
13namespace gpio
14{
15
16/** @class GpioMonitor
17 * @brief Responsible for catching GPIO state change
18 * condition and starting systemd targets.
19 */
20class GpioMonitor
21{
22 public:
23 GpioMonitor() = delete;
24 ~GpioMonitor() = default;
25 GpioMonitor(const GpioMonitor&) = delete;
26 GpioMonitor& operator=(const GpioMonitor&) = delete;
27 GpioMonitor(GpioMonitor&&) = delete;
28 GpioMonitor& operator=(GpioMonitor&&) = delete;
29
30 /** @brief Constructs GpioMonitor object.
31 *
32 * @param[in] line - GPIO line from libgpiod
33 * @param[in] config - configuration of line with event
34 * @param[in] io - io service
35 * @param[in] target - systemd unit to be started on GPIO
36 * value change
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080037 * @param[in] targets - systemd units to be started on GPIO
38 * value change
Vijay Khemka939a6432019-10-09 17:45:45 -070039 * @param[in] lineMsg - GPIO line message to be used for log
40 * @param[in] continueRun - Whether to continue after event occur
41 */
42 GpioMonitor(gpiod_line* line, gpiod_line_request_config& config,
Ed Tanous854404e2023-02-28 13:37:51 -080043 boost::asio::io_context& io, const std::string& target,
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080044 const std::map<std::string, std::vector<std::string>>& targets,
Vijay Khemka939a6432019-10-09 17:45:45 -070045 const std::string& lineMsg, bool continueRun) :
46 gpioLine(line),
47 gpioConfig(config), gpioEventDescriptor(io), target(target),
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080048 targets(targets), gpioLineMsg(lineMsg), continueAfterEvent(continueRun)
Vijay Khemka939a6432019-10-09 17:45:45 -070049 {
50 requestGPIOEvents();
51 };
52
53 private:
54 /** @brief GPIO line */
55 gpiod_line* gpioLine;
56
57 /** @brief GPIO line configuration */
58 gpiod_line_request_config gpioConfig;
59
60 /** @brief GPIO event descriptor */
61 boost::asio::posix::stream_descriptor gpioEventDescriptor;
62
63 /** @brief Systemd unit to be started when the condition is met */
64 const std::string target;
65
Delphine CC Chiua66ac0f2023-01-09 17:12:23 +080066 /** @brief Multi systemd units to be started when the condition is met */
67 std::map<std::string, std::vector<std::string>> targets;
68
Vijay Khemka939a6432019-10-09 17:45:45 -070069 /** @brief GPIO line name message */
70 std::string gpioLineMsg;
71
72 /** @brief If the monitor should continue after event */
73 bool continueAfterEvent;
74
75 /** @brief register handler for gpio event
76 *
77 * @return - 0 on success and -1 otherwise
78 */
79 int requestGPIOEvents();
80
81 /** @brief Schedule an event handler for GPIO event to trigger */
82 void scheduleEventHandler();
83
84 /** @brief Handle the GPIO event and starts configured target */
85 void gpioEventHandler();
86};
87
88} // namespace gpio
89} // namespace phosphor