blob: 627270bad1533e2176cb29676e2e9014acb4ef10 [file] [log] [blame]
Chris Cain83929002024-03-06 14:20:09 -06001#pragma once
2
3#include "sensor.hpp"
4
5#include <gpiod.hpp>
6#include <phosphor-logging/lg2.hpp>
7
8class PresenceGpio
9{
10 public:
Chris Cainc45e18f2024-07-24 15:58:00 -050011 PresenceGpio(const std::string& deviceType, const std::string& deviceName,
12 const std::string& gpioName);
Chris Cain83929002024-03-06 14:20:09 -060013 PresenceGpio(const PresenceGpio&) = delete;
14 PresenceGpio& operator=(const PresenceGpio&) = delete;
15 virtual ~PresenceGpio() = 0;
16
Chris Cainc45e18f2024-07-24 15:58:00 -050017 virtual void monitorPresence() = 0;
Chris Cain83929002024-03-06 14:20:09 -060018 bool isPresent() const
19 {
20 return status;
21 }
22
23 protected:
24 gpiod::line gpioLine;
25 bool status = false;
26 std::string deviceType;
27 std::string deviceName;
Chris Cainc45e18f2024-07-24 15:58:00 -050028 std::string gpioName;
Chris Cain83929002024-03-06 14:20:09 -060029
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
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
Chris Cainc45e18f2024-07-24 15:58:00 -050046 void updateAndTracePresence(int newValue);
Chris Cain83929002024-03-06 14:20:09 -060047};
48
49class EventPresenceGpio :
50 public PresenceGpio,
51 public std::enable_shared_from_this<EventPresenceGpio>
52{
53 public:
Chris Cainc45e18f2024-07-24 15:58:00 -050054 EventPresenceGpio(const std::string& deviceType,
55 const std::string& deviceName,
Chris Cain83929002024-03-06 14:20:09 -060056 const std::string& gpioName, bool inverted,
57 boost::asio::io_context& io);
58
Chris Cainc45e18f2024-07-24 15:58:00 -050059 void monitorPresence() override;
60
Chris Cain83929002024-03-06 14:20:09 -060061 private:
62 boost::asio::posix::stream_descriptor gpioFd;
63
Chris Cain83929002024-03-06 14:20:09 -060064 void read();
65};
Chris Cainc45e18f2024-07-24 15:58:00 -050066
67class PollingPresenceGpio :
68 public PresenceGpio,
69 public std::enable_shared_from_this<PollingPresenceGpio>
70{
71 public:
72 PollingPresenceGpio(const std::string& deviceType,
73 const std::string& deviceName,
74 const std::string& gpioName, bool inverted,
75 boost::asio::io_context& io);
76 ~PollingPresenceGpio() override
77 {
78 // GPIO no longer being used so release/remove
79 gpioLine.release();
80 }
81 void monitorPresence() override;
82
83 private:
84 boost::asio::steady_timer pollTimer;
85
86 static inline void
87 pollTimerHandler(const std::weak_ptr<PollingPresenceGpio>& weakRef,
88 const boost::system::error_code& ec);
89};