1 #pragma once 2 3 #include "evdev.hpp" 4 5 #include <linux/input.h> 6 #include <systemd/sd-event.h> 7 #include <unistd.h> 8 9 #include <sdbusplus/bus.hpp> 10 11 #include <string> 12 13 namespace phosphor 14 { 15 namespace gpio 16 { 17 18 /** @class Monitor 19 * @brief Responsible for catching GPIO state change 20 * condition and starting systemd targets. 21 */ 22 class Monitor : public Evdev 23 { 24 public: 25 Monitor() = delete; 26 ~Monitor() = default; 27 Monitor(const Monitor&) = delete; 28 Monitor& operator=(const Monitor&) = delete; 29 Monitor(Monitor&&) = delete; 30 Monitor& operator=(Monitor&&) = delete; 31 32 /** @brief Constructs Monitor object. 33 * 34 * @param[in] path - Path to gpio input device 35 * @param[in] key - GPIO key to monitor 36 * @param[in] polarity - GPIO assertion polarity to look for 37 * @param[in] target - systemd unit to be started on GPIO 38 * value change 39 * @param[in] event - sd_event handler 40 * @param[in] continueRun - Whether to continue after key pressed 41 * @param[in] handler - IO callback handler. Defaults to one in this 42 * class 43 * @param[in] useEvDev - Whether to use EvDev to retrieve events 44 */ Monitor(const std::string & path,decltype(input_event::code)key,decltype(input_event::value)polarity,const std::string & target,EventPtr & event,bool continueRun,sd_event_io_handler_t handler=Monitor::processEvents,bool useEvDev=true)45 Monitor(const std::string& path, decltype(input_event::code) key, 46 decltype(input_event::value) polarity, const std::string& target, 47 EventPtr& event, bool continueRun, 48 sd_event_io_handler_t handler = Monitor::processEvents, 49 bool useEvDev = true) : 50 Evdev(path, key, event, handler, useEvDev), polarity(polarity), 51 target(target), continueAfterKeyPress(continueRun) {}; 52 53 /** @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); 65 66 /** @brief Returns the completion state of this handler */ completed() const67 inline auto completed() const 68 { 69 return complete; 70 } 71 72 private: 73 /** @brief GPIO key value that is of interest */ 74 decltype(input_event::value) polarity; 75 76 /** @brief Systemd unit to be started when the condition is met */ 77 const std::string& target; 78 79 /** @brief If the monitor should continue after key press */ 80 bool continueAfterKeyPress; 81 82 /** @brief Completion indicator */ 83 bool complete = false; 84 85 /** @brief Analyzes the GPIO event and starts configured target */ 86 void analyzeEvent(); 87 }; 88 89 } // namespace gpio 90 } // namespace phosphor 91