blob: 771492a9ec9aa00b038c8152584cb0a6f9ba421d [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 Venturedace6802018-11-01 16:52:10 -070010#include <string>
Gunnar Mills99258572017-07-27 15:22:42 -050011
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053012namespace phosphor
13{
14namespace gpio
15{
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053016
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053017/** @class Monitor
18 * @brief Responsible for catching GPIO state change
Gunnar Mills99258572017-07-27 15:22:42 -050019 * condition and starting systemd targets.
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053020 */
Gunnar Mills99258572017-07-27 15:22:42 -050021class Monitor : public Evdev
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053022{
Patrick Venturedace6802018-11-01 16:52:10 -070023 public:
24 Monitor() = delete;
25 ~Monitor() = default;
26 Monitor(const Monitor&) = delete;
27 Monitor& operator=(const Monitor&) = delete;
28 Monitor(Monitor&&) = delete;
29 Monitor& operator=(Monitor&&) = delete;
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053030
Patrick Venturedace6802018-11-01 16:52:10 -070031 /** @brief Constructs Monitor object.
32 *
33 * @param[in] path - Path to gpio input device
34 * @param[in] key - GPIO key to monitor
35 * @param[in] polarity - GPIO assertion polarity to look for
36 * @param[in] target - systemd unit to be started on GPIO
37 * value change
38 * @param[in] event - sd_event handler
39 * @param[in] continueRun - Whether to continue after key pressed
40 * @param[in] handler - IO callback handler. Defaults to one in this
41 * class
42 * @param[in] useEvDev - Whether to use EvDev to retrieve events
43 */
44 Monitor(const std::string& path, decltype(input_event::code) key,
45 decltype(input_event::value) polarity, const std::string& target,
46 EventPtr& event, bool continueRun,
47 sd_event_io_handler_t handler = Monitor::processEvents,
48 bool useEvDev = true) :
49 Evdev(path, key, event, handler, useEvDev),
50 polarity(polarity), target(target),
51 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