xref: /openbmc/dbus-sensors/src/fan/PresenceGpio.hpp (revision 556e04b8f374a9eb8cf32bf0e36ac46c14873eba)
1 #pragma once
2 
3 #include <boost/asio/io_context.hpp>
4 #include <boost/asio/posix/stream_descriptor.hpp>
5 #include <boost/asio/steady_timer.hpp>
6 #include <gpiod.hpp>
7 #include <phosphor-logging/lg2.hpp>
8 
9 #include <memory>
10 #include <string>
11 
12 class PresenceGpio
13 {
14   public:
15     PresenceGpio(const std::string& deviceType, const std::string& deviceName,
16                  const std::string& gpioName);
17     PresenceGpio(const PresenceGpio&) = delete;
18     PresenceGpio& operator=(const PresenceGpio&) = delete;
19     virtual ~PresenceGpio() = 0;
20 
21     virtual void monitorPresence() = 0;
isPresent() const22     bool isPresent() const
23     {
24         return status;
25     }
26 
27   protected:
28     gpiod::line gpioLine;
29     bool status = false;
30     std::string deviceType;
31     std::string deviceName;
32     std::string gpioName;
33 
logPresent(const std::string & device)34     void logPresent(const std::string& device)
35     {
36         std::string summary = deviceType + " " + deviceName + " Inserted";
37         std::string msg = "OpenBMC.0.1." + deviceType + "Inserted";
38         lg2::info(summary.c_str(), "REDFISH_MESSAGE_ID", msg.c_str(),
39                   "REDFISH_MESSAGE_ARGS", device);
40     }
41 
logRemoved(const std::string & device)42     void logRemoved(const std::string& device)
43     {
44         std::string summary = deviceType + " " + deviceName + " Removed";
45         std::string msg = "OpenBMC.0.1." + deviceType + "Removed";
46         lg2::error(summary.c_str(), "REDFISH_MESSAGE_ID", msg.c_str(),
47                    "REDFISH_MESSAGE_ARGS", device);
48     }
49 
50     void updateAndTracePresence(int newValue);
51 };
52 
53 class EventPresenceGpio :
54     public PresenceGpio,
55     public std::enable_shared_from_this<EventPresenceGpio>
56 {
57   public:
58     EventPresenceGpio(const std::string& deviceType,
59                       const std::string& deviceName,
60                       const std::string& gpioName, bool inverted,
61                       boost::asio::io_context& io);
62 
63     void monitorPresence() override;
64 
65   private:
66     boost::asio::posix::stream_descriptor gpioFd;
67 
68     void read();
69 };
70 
71 class PollingPresenceGpio :
72     public PresenceGpio,
73     public std::enable_shared_from_this<PollingPresenceGpio>
74 {
75   public:
76     PollingPresenceGpio(const std::string& deviceType,
77                         const std::string& deviceName,
78                         const std::string& gpioName, bool inverted,
79                         boost::asio::io_context& io);
~PollingPresenceGpio()80     ~PollingPresenceGpio() override
81     {
82         // GPIO no longer being used so release/remove
83         gpioLine.release();
84     }
85     void monitorPresence() override;
86 
87   private:
88     boost::asio::steady_timer pollTimer;
89 
90     static inline void pollTimerHandler(
91         const std::weak_ptr<PollingPresenceGpio>& weakRef,
92         const boost::system::error_code& ec);
93 };
94