blob: f4b07dec45d51511958cff55857317097d30cbe2 [file] [log] [blame]
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +05301#pragma once
2
3#include <unistd.h>
4#include <string>
5#include <linux/input.h>
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +05306#include <systemd/sd-event.h>
Vishwanatha Subbannaba730132017-04-04 14:08:26 +05307#include <sdbusplus/bus.hpp>
Gunnar Mills99258572017-07-27 15:22:42 -05008#include "evdev.hpp"
9
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053010namespace phosphor
11{
12namespace gpio
13{
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053014
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053015/** @class Monitor
16 * @brief Responsible for catching GPIO state change
Gunnar Mills99258572017-07-27 15:22:42 -050017 * condition and starting systemd targets.
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053018 */
Gunnar Mills99258572017-07-27 15:22:42 -050019class Monitor : public Evdev
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053020{
21 public:
22 Monitor() = delete;
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053023 ~Monitor() = default;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053024 Monitor(const Monitor&) = delete;
25 Monitor& operator=(const Monitor&) = delete;
26 Monitor(Monitor&&) = delete;
27 Monitor& operator=(Monitor&&) = delete;
28
29 /** @brief Constructs Monitor object.
30 *
31 * @param[in] path - Path to gpio input device
32 * @param[in] key - GPIO key to monitor
33 * @param[in] polarity - GPIO assertion polarity to look for
34 * @param[in] target - systemd unit to be started on GPIO
35 * value change
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053036 * @param[in] event - sd_event handler
Lei YUbc4a4ff2018-04-11 13:33:25 +080037 * @param[in] continueRun - Whether to continue after key pressed
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053038 * @param[in] handler - IO callback handler. Defaults to one in this
39 * class
Vishwanatha Subbannaba8de422017-06-19 15:51:01 +053040 * @param[in] useEvDev - Whether to use EvDev to retrieve events
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053041 */
42 Monitor(const std::string& path,
43 decltype(input_event::code) key,
44 decltype(input_event::value) polarity,
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053045 const std::string& target,
46 EventPtr& event,
Lei YUbc4a4ff2018-04-11 13:33:25 +080047 bool continueRun,
Vishwanatha Subbannaba8de422017-06-19 15:51:01 +053048 sd_event_io_handler_t handler = Monitor::processEvents,
49 bool useEvDev = true)
Gunnar Mills99258572017-07-27 15:22:42 -050050 : Evdev(path, key, event, handler, useEvDev),
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053051 polarity(polarity),
Lei YUbc4a4ff2018-04-11 13:33:25 +080052 target(target),
53 continueAfterKeyPress(continueRun) {};
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053054
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053055 /** @brief Callback handler when the FD has some activity on it
56 *
57 * @param[in] es - Populated event source
58 * @param[in] fd - Associated File descriptor
59 * @param[in] revents - Type of event
60 * @param[in] userData - User data that was passed during registration
61 *
62 * @return - 0 or positive number on success and negative
63 * errno otherwise
64 */
65 static int processEvents(sd_event_source* es, int fd,
66 uint32_t revents, void* userData);
67
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053068 /** @brief Returns the completion state of this handler */
69 inline auto completed() const
70 {
71 return complete;
72 }
73
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053074 private:
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053075 /** @brief GPIO key value that is of interest */
76 decltype(input_event::value) polarity;
77
78 /** @brief Systemd unit to be started when the condition is met */
79 const std::string& target;
80
Lei YUbc4a4ff2018-04-11 13:33:25 +080081 /** @brief If the monitor should continue after key press */
82 bool continueAfterKeyPress;
83
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053084 /** @brief Completion indicator */
85 bool complete = false;
86
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053087 /** @brief Analyzes the GPIO event and starts configured target */
88 void analyzeEvent();
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053089};
90
91} // namespace gpio
92} // namespace phosphor