Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <string> |
| 5 | #include <linux/input.h> |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 6 | #include <libevdev/libevdev.h> |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 7 | #include <systemd/sd-event.h> |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 9 | #include "file.hpp" |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace gpio |
| 13 | { |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 14 | |
| 15 | /* Need a custom deleter for freeing up sd_event */ |
| 16 | struct EventDeleter |
| 17 | { |
| 18 | void operator()(sd_event* event) const |
| 19 | { |
| 20 | event = sd_event_unref(event); |
| 21 | } |
| 22 | }; |
| 23 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 24 | |
| 25 | /* Need a custom deleter for freeing up sd_event_source */ |
| 26 | struct EventSourceDeleter |
| 27 | { |
| 28 | void operator()(sd_event_source* eventSource) const |
| 29 | { |
| 30 | eventSource = sd_event_source_unref(eventSource); |
| 31 | } |
| 32 | }; |
| 33 | using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>; |
| 34 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 35 | /* Need a custom deleter for freeing up evdev struct */ |
| 36 | struct EvdevDeleter |
| 37 | { |
| 38 | void operator()(struct libevdev* device) const |
| 39 | { |
| 40 | libevdev_free(device); |
| 41 | } |
| 42 | }; |
| 43 | using EvdevPtr = std::unique_ptr<struct libevdev, EvdevDeleter>; |
| 44 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 45 | /** @class Monitor |
| 46 | * @brief Responsible for catching GPIO state change |
| 47 | * condition and taking actions |
| 48 | */ |
| 49 | class Monitor |
| 50 | { |
| 51 | public: |
| 52 | Monitor() = delete; |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 53 | ~Monitor() = default; |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 54 | Monitor(const Monitor&) = delete; |
| 55 | Monitor& operator=(const Monitor&) = delete; |
| 56 | Monitor(Monitor&&) = delete; |
| 57 | Monitor& operator=(Monitor&&) = delete; |
| 58 | |
| 59 | /** @brief Constructs Monitor object. |
| 60 | * |
| 61 | * @param[in] path - Path to gpio input device |
| 62 | * @param[in] key - GPIO key to monitor |
| 63 | * @param[in] polarity - GPIO assertion polarity to look for |
| 64 | * @param[in] target - systemd unit to be started on GPIO |
| 65 | * value change |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 66 | * @param[in] event - sd_event handler |
| 67 | * @param[in] handler - IO callback handler. Defaults to one in this |
| 68 | * class |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 69 | */ |
| 70 | Monitor(const std::string& path, |
| 71 | decltype(input_event::code) key, |
| 72 | decltype(input_event::value) polarity, |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 73 | const std::string& target, |
| 74 | EventPtr& event, |
| 75 | sd_event_io_handler_t handler = Monitor::processEvents) |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 76 | : path(path), |
| 77 | key(key), |
| 78 | polarity(polarity), |
| 79 | target(target), |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 80 | event(event), |
| 81 | callbackHandler(handler), |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 82 | fd(openDevice()) |
| 83 | { |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 84 | // And register callback handler when FD has some data |
| 85 | registerCallback(); |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 86 | } |
| 87 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 88 | /** @brief Callback handler when the FD has some activity on it |
| 89 | * |
| 90 | * @param[in] es - Populated event source |
| 91 | * @param[in] fd - Associated File descriptor |
| 92 | * @param[in] revents - Type of event |
| 93 | * @param[in] userData - User data that was passed during registration |
| 94 | * |
| 95 | * @return - 0 or positive number on success and negative |
| 96 | * errno otherwise |
| 97 | */ |
| 98 | static int processEvents(sd_event_source* es, int fd, |
| 99 | uint32_t revents, void* userData); |
| 100 | |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 101 | /** @brief Returns the completion state of this handler */ |
| 102 | inline auto completed() const |
| 103 | { |
| 104 | return complete; |
| 105 | } |
| 106 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 107 | private: |
| 108 | /** @brief Absolute path of GPIO input device */ |
| 109 | const std::string& path; |
| 110 | |
| 111 | /** @brief GPIO key code that is of interest */ |
| 112 | decltype(input_event::code) key; |
| 113 | |
| 114 | /** @brief GPIO key value that is of interest */ |
| 115 | decltype(input_event::value) polarity; |
| 116 | |
| 117 | /** @brief Systemd unit to be started when the condition is met */ |
| 118 | const std::string& target; |
| 119 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 120 | /** @brief Monitor to sd_event */ |
| 121 | EventPtr& event; |
| 122 | |
| 123 | /** @brief event source */ |
| 124 | EventSourcePtr eventSource; |
| 125 | |
| 126 | /** @brief Callback handler when the FD has some data */ |
| 127 | sd_event_io_handler_t callbackHandler; |
| 128 | |
| 129 | /** @brief File descriptor manager */ |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 130 | FileDescriptor fd; |
| 131 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 132 | /** event structure */ |
| 133 | EvdevPtr device; |
| 134 | |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 135 | /** @brief Completion indicator */ |
| 136 | bool complete = false; |
| 137 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 138 | /** @brief Opens the device and populates the descriptor */ |
| 139 | int openDevice(); |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 140 | |
| 141 | /** @brief attaches FD to events and sets up callback handler */ |
| 142 | void registerCallback(); |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 143 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 144 | /** @brief Analyzes the GPIO event and starts configured target */ |
| 145 | void analyzeEvent(); |
| 146 | |
| 147 | /** @brief Initializes evdev handle with the fd */ |
| 148 | void initEvDev(); |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace gpio |
| 152 | } // namespace phosphor |