James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 2 | |
| 3 | #include "Thresholds.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 6 | #include <boost/asio/random_access_file.hpp> |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 7 | #include <boost/container/flat_map.hpp> |
| 8 | #include <boost/container/flat_set.hpp> |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 9 | #include <gpiod.hpp> |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 11 | #include <sdbusplus/asio/object_server.hpp> |
| 12 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <optional> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 18 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 19 | class PresenceSensor |
| 20 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 21 | public: |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 22 | PresenceSensor(const std::string& gpioName, bool inverted, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 23 | boost::asio::io_context& io, const std::string& name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 24 | ~PresenceSensor(); |
| 25 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame^] | 26 | void monitorPresence(); |
| 27 | void read(); |
| 28 | bool getValue() const; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 29 | |
| 30 | private: |
| 31 | bool status = true; |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 32 | gpiod::line gpioLine; |
| 33 | boost::asio::posix::stream_descriptor gpioFd; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 34 | std::string name; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 35 | }; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 36 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 37 | namespace redundancy |
| 38 | { |
| 39 | constexpr const char* full = "Full"; |
| 40 | constexpr const char* degraded = "Degraded"; |
| 41 | constexpr const char* failed = "Failed"; |
| 42 | } // namespace redundancy |
| 43 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 44 | class RedundancySensor |
| 45 | { |
| 46 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 47 | RedundancySensor(size_t count, const std::vector<std::string>& children, |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 48 | sdbusplus::asio::object_server& objectServer, |
| 49 | const std::string& sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 50 | ~RedundancySensor(); |
| 51 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 52 | void update(const std::string& name, bool failed); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | size_t count; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 56 | std::string state = redundancy::full; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 57 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 58 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 59 | sdbusplus::asio::object_server& objectServer; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 60 | boost::container::flat_map<std::string, bool> statuses; |
| 61 | }; |
| 62 | |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 63 | class TachSensor : |
| 64 | public Sensor, |
| 65 | public std::enable_shared_from_this<TachSensor> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 66 | { |
| 67 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 68 | TachSensor(const std::string& path, const std::string& objectType, |
| 69 | sdbusplus::asio::object_server& objectServer, |
| 70 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 71 | std::unique_ptr<PresenceSensor>&& presence, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 72 | std::optional<RedundancySensor>* redundancy, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 73 | boost::asio::io_context& io, const std::string& fanName, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 74 | std::vector<thresholds::Threshold>&& thresholds, |
| 75 | const std::string& sensorConfiguration, |
Ed Tanous | f69fbf9 | 2022-01-14 10:15:33 -0800 | [diff] [blame] | 76 | const std::pair<double, double>& limits, |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 77 | const PowerState& powerState, |
| 78 | const std::optional<std::string>& led); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 79 | ~TachSensor() override; |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 80 | void setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 83 | // Ordering is important here; readBuf is first so that it's not destroyed |
| 84 | // while async operations from other member fields might still be using it. |
| 85 | std::array<char, 128> readBuf{}; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 86 | sdbusplus::asio::object_server& objServer; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 87 | std::optional<RedundancySensor>* redundancy; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 88 | std::unique_ptr<PresenceSensor> presence; |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 89 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface; |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 90 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc; |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 91 | boost::asio::random_access_file inputDev; |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 92 | boost::asio::steady_timer waitTimer; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 93 | std::string path; |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 94 | std::optional<std::string> led; |
| 95 | bool ledState = false; |
Josh Lehan | 5170fe6 | 2022-08-03 13:17:41 -0700 | [diff] [blame] | 96 | |
| 97 | void handleResponse(const boost::system::error_code& err, size_t bytesRead); |
| 98 | void restartRead(size_t pollTime); |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame^] | 99 | void checkThresholds() override; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 100 | }; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 101 | |
| 102 | inline void logFanInserted(const std::string& device) |
| 103 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 104 | const auto* msg = "OpenBMC.0.1.FanInserted"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 105 | lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg, |
| 106 | "REDFISH_MESSAGE_ARGS", device); |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | inline void logFanRemoved(const std::string& device) |
| 110 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 111 | const auto* msg = "OpenBMC.0.1.FanRemoved"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 112 | lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg, "REDFISH_MESSAGE_ARGS", |
| 113 | device); |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame^] | 116 | inline void logFanRedundancyLost() |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 117 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 118 | const auto* msg = "OpenBMC.0.1.FanRedundancyLost"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 119 | lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg); |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame^] | 122 | inline void logFanRedundancyRestored() |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 123 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 124 | const auto* msg = "OpenBMC.0.1.FanRedundancyRegained"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 125 | lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg); |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 126 | } |