blob: 7578d9d721cc426ff12e2db302a72aae90f99a6f [file] [log] [blame]
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05301#pragma once
2
Patrick Venturedace6802018-11-01 16:52:10 -07003#include "evdev.hpp"
4
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05305#include <linux/input.h>
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +05306#include <systemd/sd-event.h>
Patrick Venturedace6802018-11-01 16:52:10 -07007#include <unistd.h>
8
Vishwanatha Subbannaba730132017-04-04 14:08:26 +05309#include <sdbusplus/bus.hpp>
Patrick Williams39084b42023-05-10 07:50:58 -050010
Patrick Venturedace6802018-11-01 16:52:10 -070011#include <string>
Gunnar Mills99258572017-07-27 15:22:42 -050012
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053013namespace phosphor
14{
15namespace gpio
16{
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053017
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053018/** @class Monitor
19 * @brief Responsible for catching GPIO state change
Gunnar Mills99258572017-07-27 15:22:42 -050020 * condition and starting systemd targets.
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053021 */
Gunnar Mills99258572017-07-27 15:22:42 -050022class Monitor : public Evdev
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053023{
Patrick Venturedace6802018-11-01 16:52:10 -070024 public:
25 Monitor() = delete;
26 ~Monitor() = default;
27 Monitor(const Monitor&) = delete;
28 Monitor& operator=(const Monitor&) = delete;
29 Monitor(Monitor&&) = delete;
30 Monitor& operator=(Monitor&&) = delete;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053031
Patrick Venturedace6802018-11-01 16:52:10 -070032 /** @brief Constructs Monitor object.
33 *
34 * @param[in] path - Path to gpio input device
35 * @param[in] key - GPIO key to monitor
36 * @param[in] polarity - GPIO assertion polarity to look for
37 * @param[in] target - systemd unit to be started on GPIO
38 * value change
39 * @param[in] event - sd_event handler
40 * @param[in] continueRun - Whether to continue after key pressed
41 * @param[in] handler - IO callback handler. Defaults to one in this
42 * class
43 * @param[in] useEvDev - Whether to use EvDev to retrieve events
44 */
45 Monitor(const std::string& path, decltype(input_event::code) key,
46 decltype(input_event::value) polarity, const std::string& target,
47 EventPtr& event, bool continueRun,
48 sd_event_io_handler_t handler = Monitor::processEvents,
49 bool useEvDev = true) :
50 Evdev(path, key, event, handler, useEvDev),
51 polarity(polarity), target(target),
52 continueAfterKeyPress(continueRun){};
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053053
Patrick Venturedace6802018-11-01 16:52:10 -070054 /** @brief Callback handler when the FD has some activity on it
55 *
56 * @param[in] es - Populated event source
57 * @param[in] fd - Associated File descriptor
58 * @param[in] revents - Type of event
59 * @param[in] userData - User data that was passed during registration
60 *
61 * @return - 0 or positive number on success and negative
62 * errno otherwise
63 */
64 static int processEvents(sd_event_source* es, int fd, uint32_t revents,
65 void* userData);
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053066
Patrick Venturedace6802018-11-01 16:52:10 -070067 /** @brief Returns the completion state of this handler */
68 inline auto completed() const
69 {
70 return complete;
71 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053072
Patrick Venturedace6802018-11-01 16:52:10 -070073 private:
74 /** @brief GPIO key value that is of interest */
75 decltype(input_event::value) polarity;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053076
Patrick Venturedace6802018-11-01 16:52:10 -070077 /** @brief Systemd unit to be started when the condition is met */
78 const std::string& target;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053079
Patrick Venturedace6802018-11-01 16:52:10 -070080 /** @brief If the monitor should continue after key press */
81 bool continueAfterKeyPress;
Lei YUbc4a4ff2018-04-11 13:33:25 +080082
Patrick Venturedace6802018-11-01 16:52:10 -070083 /** @brief Completion indicator */
84 bool complete = false;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053085
Patrick Venturedace6802018-11-01 16:52:10 -070086 /** @brief Analyzes the GPIO event and starts configured target */
87 void analyzeEvent();
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053088};
89
90} // namespace gpio
91} // namespace phosphor