1939a6431SVijay Khemka #pragma once
2939a6431SVijay Khemka 
3939a6431SVijay Khemka #include <gpiod.h>
4939a6431SVijay Khemka 
5854404e0SEd Tanous #include <boost/asio/io_context.hpp>
6939a6431SVijay Khemka #include <boost/asio/posix/stream_descriptor.hpp>
7*39084b4aSPatrick Williams 
8a66ac0fcSDelphine CC Chiu #include <map>
9a66ac0fcSDelphine CC Chiu #include <vector>
10939a6431SVijay Khemka 
11939a6431SVijay Khemka namespace phosphor
12939a6431SVijay Khemka {
13939a6431SVijay Khemka namespace gpio
14939a6431SVijay Khemka {
15939a6431SVijay Khemka 
16939a6431SVijay Khemka /** @class GpioMonitor
17939a6431SVijay Khemka  *  @brief Responsible for catching GPIO state change
18939a6431SVijay Khemka  *  condition and starting systemd targets.
19939a6431SVijay Khemka  */
20939a6431SVijay Khemka class GpioMonitor
21939a6431SVijay Khemka {
22939a6431SVijay Khemka   public:
23939a6431SVijay Khemka     GpioMonitor() = delete;
24939a6431SVijay Khemka     ~GpioMonitor() = default;
25939a6431SVijay Khemka     GpioMonitor(const GpioMonitor&) = delete;
26939a6431SVijay Khemka     GpioMonitor& operator=(const GpioMonitor&) = delete;
27939a6431SVijay Khemka     GpioMonitor(GpioMonitor&&) = delete;
28939a6431SVijay Khemka     GpioMonitor& operator=(GpioMonitor&&) = delete;
29939a6431SVijay Khemka 
30939a6431SVijay Khemka     /** @brief Constructs GpioMonitor object.
31939a6431SVijay Khemka      *
32939a6431SVijay Khemka      *  @param[in] line        - GPIO line from libgpiod
33939a6431SVijay Khemka      *  @param[in] config      - configuration of line with event
34939a6431SVijay Khemka      *  @param[in] io          - io service
35939a6431SVijay Khemka      *  @param[in] target      - systemd unit to be started on GPIO
36939a6431SVijay Khemka      *                           value change
37a66ac0fcSDelphine CC Chiu      *  @param[in] targets     - systemd units to be started on GPIO
38a66ac0fcSDelphine CC Chiu      *                           value change
39939a6431SVijay Khemka      *  @param[in] lineMsg     - GPIO line message to be used for log
40939a6431SVijay Khemka      *  @param[in] continueRun - Whether to continue after event occur
41939a6431SVijay Khemka      */
GpioMonitor(gpiod_line * line,gpiod_line_request_config & config,boost::asio::io_context & io,const std::string & target,const std::map<std::string,std::vector<std::string>> & targets,const std::string & lineMsg,bool continueRun)42939a6431SVijay Khemka     GpioMonitor(gpiod_line* line, gpiod_line_request_config& config,
43854404e0SEd Tanous                 boost::asio::io_context& io, const std::string& target,
44a66ac0fcSDelphine CC Chiu                 const std::map<std::string, std::vector<std::string>>& targets,
45939a6431SVijay Khemka                 const std::string& lineMsg, bool continueRun) :
46939a6431SVijay Khemka         gpioLine(line),
47939a6431SVijay Khemka         gpioConfig(config), gpioEventDescriptor(io), target(target),
48a66ac0fcSDelphine CC Chiu         targets(targets), gpioLineMsg(lineMsg), continueAfterEvent(continueRun)
49939a6431SVijay Khemka     {
50939a6431SVijay Khemka         requestGPIOEvents();
51939a6431SVijay Khemka     };
52939a6431SVijay Khemka 
53939a6431SVijay Khemka   private:
54939a6431SVijay Khemka     /** @brief GPIO line */
55939a6431SVijay Khemka     gpiod_line* gpioLine;
56939a6431SVijay Khemka 
57939a6431SVijay Khemka     /** @brief GPIO line configuration */
58939a6431SVijay Khemka     gpiod_line_request_config gpioConfig;
59939a6431SVijay Khemka 
60939a6431SVijay Khemka     /** @brief GPIO event descriptor */
61939a6431SVijay Khemka     boost::asio::posix::stream_descriptor gpioEventDescriptor;
62939a6431SVijay Khemka 
63939a6431SVijay Khemka     /** @brief Systemd unit to be started when the condition is met */
64939a6431SVijay Khemka     const std::string target;
65939a6431SVijay Khemka 
66a66ac0fcSDelphine CC Chiu     /** @brief Multi systemd units to be started when the condition is met */
67a66ac0fcSDelphine CC Chiu     std::map<std::string, std::vector<std::string>> targets;
68a66ac0fcSDelphine CC Chiu 
69939a6431SVijay Khemka     /** @brief GPIO line name message */
70939a6431SVijay Khemka     std::string gpioLineMsg;
71939a6431SVijay Khemka 
72939a6431SVijay Khemka     /** @brief If the monitor should continue after event */
73939a6431SVijay Khemka     bool continueAfterEvent;
74939a6431SVijay Khemka 
75939a6431SVijay Khemka     /** @brief register handler for gpio event
76939a6431SVijay Khemka      *
77939a6431SVijay Khemka      *  @return  - 0 on success and -1 otherwise
78939a6431SVijay Khemka      */
79939a6431SVijay Khemka     int requestGPIOEvents();
80939a6431SVijay Khemka 
81939a6431SVijay Khemka     /** @brief Schedule an event handler for GPIO event to trigger */
82939a6431SVijay Khemka     void scheduleEventHandler();
83939a6431SVijay Khemka 
84939a6431SVijay Khemka     /** @brief Handle the GPIO event and starts configured target */
85939a6431SVijay Khemka     void gpioEventHandler();
86939a6431SVijay Khemka };
87939a6431SVijay Khemka 
88939a6431SVijay Khemka } // namespace gpio
89939a6431SVijay Khemka } // namespace phosphor
90