xref: /openbmc/phosphor-gpio-monitor/gpioMon.hpp (revision 75ff16717de9a7b3beeda9f3cace9456cad98156)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 
4 #pragma once
5 
6 #include <gpiod.h>
7 
8 #include <boost/asio/io_context.hpp>
9 #include <boost/asio/posix/stream_descriptor.hpp>
10 
11 #include <map>
12 #include <vector>
13 
14 namespace phosphor
15 {
16 namespace gpio
17 {
18 
19 /** @class GpioMonitor
20  *  @brief Responsible for catching GPIO state change
21  *  condition and starting systemd targets.
22  */
23 class GpioMonitor
24 {
25   public:
26     GpioMonitor() = delete;
27     ~GpioMonitor() = default;
28     GpioMonitor(const GpioMonitor&) = delete;
29     GpioMonitor& operator=(const GpioMonitor&) = delete;
30     GpioMonitor(GpioMonitor&&) = delete;
31     GpioMonitor& operator=(GpioMonitor&&) = delete;
32 
33     /** @brief Constructs GpioMonitor object.
34      *
35      *  @param[in] line        - GPIO line from libgpiod
36      *  @param[in] config      - configuration of line with event
37      *  @param[in] io          - io service
38      *  @param[in] target      - systemd unit to be started on GPIO
39      *                           value change
40      *  @param[in] targets     - systemd units to be started on GPIO
41      *                           value change
42      *  @param[in] lineMsg     - GPIO line message to be used for log
43      *  @param[in] continueRun - Whether to continue after event occur
44      */
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)45     GpioMonitor(gpiod_line* line, gpiod_line_request_config& config,
46                 boost::asio::io_context& io, const std::string& target,
47                 const std::map<std::string, std::vector<std::string>>& targets,
48                 const std::string& lineMsg, bool continueRun) :
49         gpioLine(line), gpioConfig(config), gpioEventDescriptor(io),
50         target(target), targets(targets), gpioLineMsg(lineMsg),
51         continueAfterEvent(continueRun)
52     {
53         requestGPIOEvents();
54     };
55 
56   private:
57     /** @brief GPIO line */
58     gpiod_line* gpioLine;
59 
60     /** @brief GPIO line configuration */
61     gpiod_line_request_config gpioConfig;
62 
63     /** @brief GPIO event descriptor */
64     boost::asio::posix::stream_descriptor gpioEventDescriptor;
65 
66     /** @brief Systemd unit to be started when the condition is met */
67     const std::string target;
68 
69     /** @brief Multi systemd units to be started when the condition is met */
70     std::map<std::string, std::vector<std::string>> targets;
71 
72     /** @brief GPIO line name message */
73     std::string gpioLineMsg;
74 
75     /** @brief If the monitor should continue after event */
76     bool continueAfterEvent;
77 
78     /** @brief register handler for gpio event
79      *
80      *  @return  - 0 on success and -1 otherwise
81      */
82     int requestGPIOEvents();
83 
84     /** @brief Schedule an event handler for GPIO event to trigger */
85     void scheduleEventHandler();
86 
87     /** @brief Handle the GPIO event and starts configured target */
88     void gpioEventHandler();
89 
90     /** @brief handle current gpio value */
91     void gpioHandleInitialState(bool value);
92 };
93 
94 } // namespace gpio
95 } // namespace phosphor
96