blob: 51772abf8026d15da2799ba0817ca711336e6058 [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>
7
8namespace phosphor
9{
10namespace gpio
11{
12
13/** @class GpioMonitor
14 * @brief Responsible for catching GPIO state change
15 * condition and starting systemd targets.
16 */
17class GpioMonitor
18{
19 public:
20 GpioMonitor() = delete;
21 ~GpioMonitor() = default;
22 GpioMonitor(const GpioMonitor&) = delete;
23 GpioMonitor& operator=(const GpioMonitor&) = delete;
24 GpioMonitor(GpioMonitor&&) = delete;
25 GpioMonitor& operator=(GpioMonitor&&) = delete;
26
27 /** @brief Constructs GpioMonitor object.
28 *
29 * @param[in] line - GPIO line from libgpiod
30 * @param[in] config - configuration of line with event
31 * @param[in] io - io service
32 * @param[in] target - systemd unit to be started on GPIO
33 * value change
34 * @param[in] lineMsg - GPIO line message to be used for log
35 * @param[in] continueRun - Whether to continue after event occur
36 */
37 GpioMonitor(gpiod_line* line, gpiod_line_request_config& config,
Ed Tanous854404e2023-02-28 13:37:51 -080038 boost::asio::io_context& io, const std::string& target,
Vijay Khemka939a6432019-10-09 17:45:45 -070039 const std::string& lineMsg, bool continueRun) :
40 gpioLine(line),
41 gpioConfig(config), gpioEventDescriptor(io), target(target),
42 gpioLineMsg(lineMsg), continueAfterEvent(continueRun)
43 {
44 requestGPIOEvents();
45 };
46
47 private:
48 /** @brief GPIO line */
49 gpiod_line* gpioLine;
50
51 /** @brief GPIO line configuration */
52 gpiod_line_request_config gpioConfig;
53
54 /** @brief GPIO event descriptor */
55 boost::asio::posix::stream_descriptor gpioEventDescriptor;
56
57 /** @brief Systemd unit to be started when the condition is met */
58 const std::string target;
59
60 /** @brief GPIO line name message */
61 std::string gpioLineMsg;
62
63 /** @brief If the monitor should continue after event */
64 bool continueAfterEvent;
65
66 /** @brief register handler for gpio event
67 *
68 * @return - 0 on success and -1 otherwise
69 */
70 int requestGPIOEvents();
71
72 /** @brief Schedule an event handler for GPIO event to trigger */
73 void scheduleEventHandler();
74
75 /** @brief Handle the GPIO event and starts configured target */
76 void gpioEventHandler();
77};
78
79} // namespace gpio
80} // namespace phosphor