xref: /openbmc/dbus-sensors/src/PresenceGpio.hpp (revision 83929002)
1 #pragma once
2 
3 #include "sensor.hpp"
4 
5 #include <gpiod.hpp>
6 #include <phosphor-logging/lg2.hpp>
7 
8 class PresenceGpio
9 {
10   public:
PresenceGpio(const std::string & type,const std::string & name)11     PresenceGpio(const std::string& type, const std::string& name) :
12         deviceType(type), deviceName(name) {};
13     PresenceGpio(const PresenceGpio&) = delete;
14     PresenceGpio& operator=(const PresenceGpio&) = delete;
15     virtual ~PresenceGpio() = 0;
16 
isPresent() const17     bool isPresent() const
18     {
19         return status;
20     }
21 
22   protected:
23     gpiod::line gpioLine;
24     bool status = false;
25     std::string deviceType;
26     std::string deviceName;
27 
28     virtual void monitorPresence() = 0;
29 
logPresent(const std::string & device)30     void logPresent(const std::string& device)
31     {
32         std::string summary = deviceType + " " + deviceName + " Inserted";
33         std::string msg = "OpenBMC.0.1." + deviceType + "Inserted";
34         lg2::info(summary.c_str(), "REDFISH_MESSAGE_ID", msg.c_str(),
35                   "REDFISH_MESSAGE_ARGS", device);
36     }
37 
logRemoved(const std::string & device)38     void logRemoved(const std::string& device)
39     {
40         std::string summary = deviceType + " " + deviceName + " Removed";
41         std::string msg = "OpenBMC.0.1." + deviceType + "Removed";
42         lg2::error(summary.c_str(), "REDFISH_MESSAGE_ID", msg.c_str(),
43                    "REDFISH_MESSAGE_ARGS", device);
44     }
45 
46     void updateAndTracePresence();
47 };
48 
49 class EventPresenceGpio :
50     public PresenceGpio,
51     public std::enable_shared_from_this<EventPresenceGpio>
52 {
53   public:
54     EventPresenceGpio(const std::string& iDeviceType,
55                       const std::string& iDeviceName,
56                       const std::string& gpioName, bool inverted,
57                       boost::asio::io_context& io);
58 
59   private:
60     boost::asio::posix::stream_descriptor gpioFd;
61 
62     void monitorPresence() override;
63     void read();
64 };
65