Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 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: |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 11 | PresenceGpio(const std::string& deviceType, const std::string& deviceName, |
| 12 | const std::string& gpioName); |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 13 | PresenceGpio(const PresenceGpio&) = delete; |
| 14 | PresenceGpio& operator=(const PresenceGpio&) = delete; |
| 15 | virtual ~PresenceGpio() = 0; |
| 16 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 17 | virtual void monitorPresence() = 0; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 18 | 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 Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 28 | std::string gpioName; |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 29 | |
| 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 Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 46 | void updateAndTracePresence(int newValue); |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | class EventPresenceGpio : |
| 50 | public PresenceGpio, |
| 51 | public std::enable_shared_from_this<EventPresenceGpio> |
| 52 | { |
| 53 | public: |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 54 | EventPresenceGpio(const std::string& deviceType, |
| 55 | const std::string& deviceName, |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 56 | const std::string& gpioName, bool inverted, |
| 57 | boost::asio::io_context& io); |
| 58 | |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 59 | void monitorPresence() override; |
| 60 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 61 | private: |
| 62 | boost::asio::posix::stream_descriptor gpioFd; |
| 63 | |
Chris Cain | 8392900 | 2024-03-06 14:20:09 -0600 | [diff] [blame] | 64 | void read(); |
| 65 | }; |
Chris Cain | c45e18f | 2024-07-24 15:58:00 -0500 | [diff] [blame] | 66 | |
| 67 | class 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 | }; |