blob: c3603956c672a3397591bc2448d6c758c629ccad [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Ed Tanous8a57ec02020-10-09 12:46:52 -07002#include <Thresholds.hpp>
Josh Lehan5170fe62022-08-03 13:17:41 -07003#include <boost/asio/random_access_file.hpp>
James Feistdc6c55f2018-10-31 12:53:20 -07004#include <boost/container/flat_map.hpp>
5#include <boost/container/flat_set.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -08006#include <gpiod.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -05007#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -07008#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07009#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070010
Patrick Venturefd6ba732019-10-31 14:27:39 -070011#include <memory>
12#include <optional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <string>
14#include <utility>
15#include <vector>
James Feist6714a252018-09-10 15:26:18 -070016
James Feist7bc2bab2018-10-26 14:09:45 -070017class PresenceSensor
18{
James Feist7bc2bab2018-10-26 14:09:45 -070019 public:
Ed Tanous2049bd22022-07-09 07:20:26 -070020 PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -070021 boost::asio::io_service& io, const std::string& name);
James Feist7bc2bab2018-10-26 14:09:45 -070022 ~PresenceSensor();
23
24 void monitorPresence(void);
25 void read(void);
Ed Tanous2049bd22022-07-09 07:20:26 -070026 bool getValue(void) const;
James Feist7bc2bab2018-10-26 14:09:45 -070027
28 private:
29 bool status = true;
Zhikui Ren347dd4e2019-12-12 13:39:50 -080030 gpiod::line gpioLine;
31 boost::asio::posix::stream_descriptor gpioFd;
James Feist7b18b1e2019-05-14 13:42:09 -070032 std::string name;
James Feist7bc2bab2018-10-26 14:09:45 -070033};
James Feistdc6c55f2018-10-31 12:53:20 -070034
James Feist7b18b1e2019-05-14 13:42:09 -070035namespace redundancy
36{
37constexpr const char* full = "Full";
38constexpr const char* degraded = "Degraded";
39constexpr const char* failed = "Failed";
40} // namespace redundancy
41
James Feistdc6c55f2018-10-31 12:53:20 -070042class RedundancySensor
43{
44 public:
James Feistd8705872019-02-08 13:26:09 -080045 RedundancySensor(size_t count, const std::vector<std::string>& children,
James Feist9bb67462019-03-15 15:09:50 -070046 sdbusplus::asio::object_server& objectServer,
47 const std::string& sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -070048 ~RedundancySensor();
49
James Feistd8705872019-02-08 13:26:09 -080050 void update(const std::string& name, bool failed);
James Feistdc6c55f2018-10-31 12:53:20 -070051
52 private:
53 size_t count;
James Feist7b18b1e2019-05-14 13:42:09 -070054 std::string state = redundancy::full;
James Feistdc6c55f2018-10-31 12:53:20 -070055 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
James Feist9bb67462019-03-15 15:09:50 -070056 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feistd8705872019-02-08 13:26:09 -080057 sdbusplus::asio::object_server& objectServer;
James Feistdc6c55f2018-10-31 12:53:20 -070058 boost::container::flat_map<std::string, bool> statuses;
59};
60
Josh Lehan5170fe62022-08-03 13:17:41 -070061class TachSensor :
62 public Sensor,
63 public std::enable_shared_from_this<TachSensor>
James Feist6714a252018-09-10 15:26:18 -070064{
65 public:
James Feistd8705872019-02-08 13:26:09 -080066 TachSensor(const std::string& path, const std::string& objectType,
67 sdbusplus::asio::object_server& objectServer,
68 std::shared_ptr<sdbusplus::asio::connection>& conn,
69 std::unique_ptr<PresenceSensor>&& presence,
James Feist7b18b1e2019-05-14 13:42:09 -070070 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080071 boost::asio::io_service& io, const std::string& fanName,
72 std::vector<thresholds::Threshold>&& thresholds,
73 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080074 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070075 const PowerState& powerState,
76 const std::optional<std::string>& led);
Ed Tanous8a57ec02020-10-09 12:46:52 -070077 ~TachSensor() override;
Josh Lehan5170fe62022-08-03 13:17:41 -070078 void setupRead();
James Feist6714a252018-09-10 15:26:18 -070079
80 private:
Josh Lehan5170fe62022-08-03 13:17:41 -070081 // Ordering is important here; readBuf is first so that it's not destroyed
82 // while async operations from other member fields might still be using it.
83 std::array<char, 128> readBuf{};
James Feistd8705872019-02-08 13:26:09 -080084 sdbusplus::asio::object_server& objServer;
James Feist7b18b1e2019-05-14 13:42:09 -070085 std::optional<RedundancySensor>* redundancy;
James Feist7bc2bab2018-10-26 14:09:45 -070086 std::unique_ptr<PresenceSensor> presence;
James Feist60c0ec72019-03-18 15:08:43 -070087 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
James Feistd8bd5622019-06-26 12:09:05 -070088 std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
Josh Lehan5170fe62022-08-03 13:17:41 -070089 boost::asio::random_access_file inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070090 boost::asio::steady_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070091 std::string path;
James Feist49a8ccd2020-09-16 16:09:52 -070092 std::optional<std::string> led;
93 bool ledState = false;
Josh Lehan5170fe62022-08-03 13:17:41 -070094
95 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
96 void restartRead(size_t pollTime);
James Feistce3fca42018-11-21 12:58:24 -080097 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070098};
James Feist7b18b1e2019-05-14 13:42:09 -070099
100inline void logFanInserted(const std::string& device)
101{
Ed Tanous2049bd22022-07-09 07:20:26 -0700102 const auto* msg = "OpenBMC.0.1.FanInserted";
Patrick Williams0c42f402021-08-27 16:05:45 -0500103 lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg,
104 "REDFISH_MESSAGE_ARGS", device);
James Feist7b18b1e2019-05-14 13:42:09 -0700105}
106
107inline void logFanRemoved(const std::string& device)
108{
Ed Tanous2049bd22022-07-09 07:20:26 -0700109 const auto* msg = "OpenBMC.0.1.FanRemoved";
Patrick Williams0c42f402021-08-27 16:05:45 -0500110 lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg, "REDFISH_MESSAGE_ARGS",
111 device);
James Feist7b18b1e2019-05-14 13:42:09 -0700112}
113
114inline void logFanRedundancyLost(void)
115{
Ed Tanous2049bd22022-07-09 07:20:26 -0700116 const auto* msg = "OpenBMC.0.1.FanRedundancyLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500117 lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg);
James Feist7b18b1e2019-05-14 13:42:09 -0700118}
119
120inline void logFanRedundancyRestored(void)
121{
Ed Tanous2049bd22022-07-09 07:20:26 -0700122 const auto* msg = "OpenBMC.0.1.FanRedundancyRegained";
Patrick Williams0c42f402021-08-27 16:05:45 -0500123 lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg);
James Feist7b18b1e2019-05-14 13:42:09 -0700124}