blob: 9e3992dc7bc0b4b39e5d909910262d4b76a33ac9 [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>
6#include "file.hpp"
7namespace phosphor
8{
9namespace gpio
10{
11/** @class Monitor
12 * @brief Responsible for catching GPIO state change
13 * condition and taking actions
14 */
15class Monitor
16{
17 public:
18 Monitor() = delete;
19 Monitor(const Monitor&) = delete;
20 Monitor& operator=(const Monitor&) = delete;
21 Monitor(Monitor&&) = delete;
22 Monitor& operator=(Monitor&&) = delete;
23
24 /** @brief Constructs Monitor object.
25 *
26 * @param[in] path - Path to gpio input device
27 * @param[in] key - GPIO key to monitor
28 * @param[in] polarity - GPIO assertion polarity to look for
29 * @param[in] target - systemd unit to be started on GPIO
30 * value change
31 */
32 Monitor(const std::string& path,
33 decltype(input_event::code) key,
34 decltype(input_event::value) polarity,
35 const std::string& target)
36 : path(path),
37 key(key),
38 polarity(polarity),
39 target(target),
40 fd(openDevice())
41 {
42 // Nothing
43 }
44
45 private:
46 /** @brief Absolute path of GPIO input device */
47 const std::string& path;
48
49 /** @brief GPIO key code that is of interest */
50 decltype(input_event::code) key;
51
52 /** @brief GPIO key value that is of interest */
53 decltype(input_event::value) polarity;
54
55 /** @brief Systemd unit to be started when the condition is met */
56 const std::string& target;
57
58 /** @brief Manages File descriptor */
59 FileDescriptor fd;
60
61 /** @brief Opens the device and populates the descriptor */
62 int openDevice();
63};
64
65} // namespace gpio
66} // namespace phosphor