blob: 8efd68b46245dc8025a4ae3f479394e98e483266 [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) :
Patrick Williams8377d592024-08-16 15:21:08 -040050 Evdev(path, key, event, handler, useEvDev), polarity(polarity),
51 target(target), continueAfterKeyPress(continueRun) {};
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053052
Patrick Venturedace6802018-11-01 16:52:10 -070053 /** @brief Callback handler when the FD has some activity on it
54 *
55 * @param[in] es - Populated event source
56 * @param[in] fd - Associated File descriptor
57 * @param[in] revents - Type of event
58 * @param[in] userData - User data that was passed during registration
59 *
60 * @return - 0 or positive number on success and negative
61 * errno otherwise
62 */
63 static int processEvents(sd_event_source* es, int fd, uint32_t revents,
64 void* userData);
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053065
Patrick Venturedace6802018-11-01 16:52:10 -070066 /** @brief Returns the completion state of this handler */
67 inline auto completed() const
68 {
69 return complete;
70 }
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053071
Patrick Venturedace6802018-11-01 16:52:10 -070072 private:
73 /** @brief GPIO key value that is of interest */
74 decltype(input_event::value) polarity;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053075
Patrick Venturedace6802018-11-01 16:52:10 -070076 /** @brief Systemd unit to be started when the condition is met */
77 const std::string& target;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053078
Patrick Venturedace6802018-11-01 16:52:10 -070079 /** @brief If the monitor should continue after key press */
80 bool continueAfterKeyPress;
Lei YUbc4a4ff2018-04-11 13:33:25 +080081
Patrick Venturedace6802018-11-01 16:52:10 -070082 /** @brief Completion indicator */
83 bool complete = false;
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053084
Patrick Venturedace6802018-11-01 16:52:10 -070085 /** @brief Analyzes the GPIO event and starts configured target */
86 void analyzeEvent();
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053087};
88
89} // namespace gpio
90} // namespace phosphor