xref: /openbmc/dbus-sensors/src/GPIOInterface.hpp (revision 15dde8641baa1cb902d17f9857effb081e384944)
1 #pragma once
2 
3 #include <gpiod.hpp>
4 #include <sdbusplus/async.hpp>
5 
6 #include <functional>
7 #include <memory>
8 #include <string>
9 
10 namespace gpio
11 {
12 
13 class GPIOInterface
14 {
15   public:
16     using Callback_t = std::function<sdbusplus::async::task<>(bool)>;
17 
18     GPIOInterface() = delete;
19 
20     GPIOInterface(sdbusplus::async::context& ctx,
21                   const std::string& consumerName, const std::string& pinName,
22                   bool activeLow, Callback_t updateStateCallback);
23 
24     /** @brief Start the GPIO Interface */
25     auto start() -> sdbusplus::async::task<>;
26 
27   private:
28     /** @brief Read the gpio state asynchronously */
29     auto readGPIOAsync() -> sdbusplus::async::task<>;
30 
31     /** @brief Read the gpio state asynchronously based on gpio event */
32     auto readGPIOAsyncEvent() -> sdbusplus::async::task<>;
33 
34     sdbusplus::async::context& ctx;
35     const std::string& pinName;
36     Callback_t updateStateCallback;
37     gpiod::line line;
38     /** File descriptor based async event handler */
39     std::unique_ptr<sdbusplus::async::fdio> fdioInstance;
40 };
41 
42 } // namespace gpio
43