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