Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame] | 3 | #include <boost/asio/io_context.hpp> |
| 4 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 5 | #include <boost/asio/steady_timer.hpp> |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 6 | #include <gpiod.hpp> |
| 7 | #include <phosphor-logging/lg2.hpp> |
| 8 | |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 12 | class PresenceGpio |
| 13 | { |
| 14 | public: |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 15 | PresenceGpio(const std::string& deviceType, const std::string& deviceName, |
| 16 | const std::string& gpioName); |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 17 | PresenceGpio(const PresenceGpio&) = delete; |
| 18 | PresenceGpio& operator=(const PresenceGpio&) = delete; |
| 19 | virtual ~PresenceGpio() = 0; |
| 20 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 21 | virtual void monitorPresence() = 0; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 22 | 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 Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 32 | std::string gpioName; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 33 | |
| 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 Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 50 | void updateAndTracePresence(int newValue); |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class EventPresenceGpio : |
| 54 | public PresenceGpio, |
| 55 | public std::enable_shared_from_this<EventPresenceGpio> |
| 56 | { |
| 57 | public: |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 58 | EventPresenceGpio(const std::string& deviceType, |
| 59 | const std::string& deviceName, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 60 | const std::string& gpioName, bool inverted, |
| 61 | boost::asio::io_context& io); |
| 62 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 63 | void monitorPresence() override; |
| 64 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 65 | private: |
| 66 | boost::asio::posix::stream_descriptor gpioFd; |
| 67 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 68 | void read(); |
| 69 | }; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 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); |
| 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 Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame] | 90 | static inline void pollTimerHandler( |
| 91 | const std::weak_ptr<PollingPresenceGpio>& weakRef, |
| 92 | const boost::system::error_code& ec); |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 93 | }; |