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 | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 6 | #include <systemd/sd-event.h> |
Vishwanatha Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
Gunnar Mills | 9925857 | 2017-07-27 15:22:42 -0500 | [diff] [blame] | 8 | #include "evdev.hpp" |
| 9 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace gpio |
| 13 | { |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 14 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 15 | /** @class Monitor |
| 16 | * @brief Responsible for catching GPIO state change |
Gunnar Mills | 9925857 | 2017-07-27 15:22:42 -0500 | [diff] [blame] | 17 | * condition and starting systemd targets. |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 18 | */ |
Gunnar Mills | 9925857 | 2017-07-27 15:22:42 -0500 | [diff] [blame] | 19 | class Monitor : public Evdev |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 20 | { |
| 21 | public: |
| 22 | Monitor() = delete; |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 23 | ~Monitor() = default; |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 24 | 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 Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 36 | * @param[in] event - sd_event handler |
| 37 | * @param[in] handler - IO callback handler. Defaults to one in this |
| 38 | * class |
Vishwanatha Subbanna | ba8de42 | 2017-06-19 15:51:01 +0530 | [diff] [blame] | 39 | * @param[in] useEvDev - Whether to use EvDev to retrieve events |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 40 | */ |
| 41 | Monitor(const std::string& path, |
| 42 | decltype(input_event::code) key, |
| 43 | decltype(input_event::value) polarity, |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 44 | const std::string& target, |
| 45 | EventPtr& event, |
Vishwanatha Subbanna | ba8de42 | 2017-06-19 15:51:01 +0530 | [diff] [blame] | 46 | sd_event_io_handler_t handler = Monitor::processEvents, |
| 47 | bool useEvDev = true) |
Gunnar Mills | 9925857 | 2017-07-27 15:22:42 -0500 | [diff] [blame] | 48 | : Evdev(path, key, event, handler, useEvDev), |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 49 | polarity(polarity), |
Gunnar Mills | 9925857 | 2017-07-27 15:22:42 -0500 | [diff] [blame] | 50 | target(target) {}; |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 51 | |
Vishwanatha Subbanna | 0b95603 | 2017-04-04 14:07:25 +0530 | [diff] [blame] | 52 | /** @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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 65 | /** @brief Returns the completion state of this handler */ |
| 66 | inline auto completed() const |
| 67 | { |
| 68 | return complete; |
| 69 | } |
| 70 | |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 71 | private: |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 72 | /** @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 Subbanna | ba73013 | 2017-04-04 14:08:26 +0530 | [diff] [blame] | 78 | /** @brief Completion indicator */ |
| 79 | bool complete = false; |
| 80 | |
Vishwanatha Subbanna | 77ec479 | 2017-04-10 15:43:47 +0530 | [diff] [blame] | 81 | /** @brief Analyzes the GPIO event and starts configured target */ |
| 82 | void analyzeEvent(); |
Vishwanatha Subbanna | 4902a10 | 2017-04-04 14:05:09 +0530 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace gpio |
| 86 | } // namespace phosphor |