blob: 19757853bc50949ce01373724f5bae963cb88d35 [file] [log] [blame]
Chris Cain83929002024-03-06 14:20:09 -06001#pragma once
2
Ed Tanous18b61862025-01-30 10:56:28 -08003#include <boost/asio/io_context.hpp>
4#include <boost/asio/posix/stream_descriptor.hpp>
5#include <boost/asio/steady_timer.hpp>
Chris Cain83929002024-03-06 14:20:09 -06006#include <gpiod.hpp>
7#include <phosphor-logging/lg2.hpp>
8
Ed Tanous18b61862025-01-30 10:56:28 -08009#include <memory>
10#include <string>
11
Chris Cain83929002024-03-06 14:20:09 -060012class PresenceGpio
13{
14 public:
Chris Cainc45e18f2024-07-24 15:58:00 -050015 PresenceGpio(const std::string& deviceType, const std::string& deviceName,
16 const std::string& gpioName);
Chris Cain83929002024-03-06 14:20:09 -060017 PresenceGpio(const PresenceGpio&) = delete;
18 PresenceGpio& operator=(const PresenceGpio&) = delete;
19 virtual ~PresenceGpio() = 0;
20
Chris Cainc45e18f2024-07-24 15:58:00 -050021 virtual void monitorPresence() = 0;
Chris Cain83929002024-03-06 14:20:09 -060022 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;
Chris Cainc45e18f2024-07-24 15:58:00 -050032 std::string gpioName;
Chris Cain83929002024-03-06 14:20:09 -060033
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
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
Chris Cainc45e18f2024-07-24 15:58:00 -050050 void updateAndTracePresence(int newValue);
Chris Cain83929002024-03-06 14:20:09 -060051};
52
53class EventPresenceGpio :
54 public PresenceGpio,
55 public std::enable_shared_from_this<EventPresenceGpio>
56{
57 public:
Chris Cainc45e18f2024-07-24 15:58:00 -050058 EventPresenceGpio(const std::string& deviceType,
59 const std::string& deviceName,
Chris Cain83929002024-03-06 14:20:09 -060060 const std::string& gpioName, bool inverted,
61 boost::asio::io_context& io);
62
Chris Cainc45e18f2024-07-24 15:58:00 -050063 void monitorPresence() override;
64
Chris Cain83929002024-03-06 14:20:09 -060065 private:
66 boost::asio::posix::stream_descriptor gpioFd;
67
Chris Cain83929002024-03-06 14:20:09 -060068 void read();
69};
Chris Cainc45e18f2024-07-24 15:58:00 -050070
71class 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);
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
Patrick Williams556e04b2025-02-01 08:22:22 -050090 static inline void pollTimerHandler(
91 const std::weak_ptr<PollingPresenceGpio>& weakRef,
92 const boost::system::error_code& ec);
Chris Cainc45e18f2024-07-24 15:58:00 -050093};