blob: 1dcef79c55c4107249f918576097ce044030963e [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
37 * @param[in] handler - IO callback handler. Defaults to one in this
38 * class
Vishwanatha Subbannaba8de422017-06-19 15:51:01 +053039 * @param[in] useEvDev - Whether to use EvDev to retrieve events
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053040 */
41 Monitor(const std::string& path,
42 decltype(input_event::code) key,
43 decltype(input_event::value) polarity,
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053044 const std::string& target,
45 EventPtr& event,
Vishwanatha Subbannaba8de422017-06-19 15:51:01 +053046 sd_event_io_handler_t handler = Monitor::processEvents,
47 bool useEvDev = true)
Gunnar Mills99258572017-07-27 15:22:42 -050048 : Evdev(path, key, event, handler, useEvDev),
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053049 polarity(polarity),
Gunnar Mills99258572017-07-27 15:22:42 -050050 target(target) {};
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053051
Vishwanatha Subbanna0b956032017-04-04 14:07:25 +053052 /** @brief Callback handler when the FD has some activity on it
53 *
54 * @param[in] es - Populated event source
55 * @param[in] fd - Associated File descriptor
56 * @param[in] revents - Type of event
57 * @param[in] userData - User data that was passed during registration
58 *
59 * @return - 0 or positive number on success and negative
60 * errno otherwise
61 */
62 static int processEvents(sd_event_source* es, int fd,
63 uint32_t revents, void* userData);
64
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053065 /** @brief Returns the completion state of this handler */
66 inline auto completed() const
67 {
68 return complete;
69 }
70
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053071 private:
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053072 /** @brief GPIO key value that is of interest */
73 decltype(input_event::value) polarity;
74
75 /** @brief Systemd unit to be started when the condition is met */
76 const std::string& target;
77
Vishwanatha Subbannaba730132017-04-04 14:08:26 +053078 /** @brief Completion indicator */
79 bool complete = false;
80
Vishwanatha Subbanna77ec4792017-04-10 15:43:47 +053081 /** @brief Analyzes the GPIO event and starts configured target */
82 void analyzeEvent();
Vishwanatha Subbanna4902a102017-04-04 14:05:09 +053083};
84
85} // namespace gpio
86} // namespace phosphor