blob: 845fbecffbc0e37cc014f38b1822919089b45f28 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10302
3#include "Thresholds.hpp"
4#include "sensor.hpp"
5
Josh Lehan5170fe62022-08-03 13:17:41 -07006#include <boost/asio/random_access_file.hpp>
James Feistdc6c55f2018-10-31 12:53:20 -07007#include <boost/container/flat_map.hpp>
8#include <boost/container/flat_set.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -08009#include <gpiod.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050010#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070011#include <sdbusplus/asio/object_server.hpp>
12
Patrick Venturefd6ba732019-10-31 14:27:39 -070013#include <memory>
14#include <optional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070015#include <string>
16#include <utility>
17#include <vector>
James Feist6714a252018-09-10 15:26:18 -070018
James Feist7bc2bab2018-10-26 14:09:45 -070019class PresenceSensor
20{
James Feist7bc2bab2018-10-26 14:09:45 -070021 public:
Ed Tanous2049bd22022-07-09 07:20:26 -070022 PresenceSensor(const std::string& gpioName, bool inverted,
Ed Tanous1f978632023-02-28 18:16:39 -080023 boost::asio::io_context& io, const std::string& name);
James Feist7bc2bab2018-10-26 14:09:45 -070024 ~PresenceSensor();
25
Ed Tanous201a1012024-04-03 18:07:28 -070026 void monitorPresence();
27 void read();
28 bool getValue() const;
James Feist7bc2bab2018-10-26 14:09:45 -070029
30 private:
31 bool status = true;
Zhikui Ren347dd4e2019-12-12 13:39:50 -080032 gpiod::line gpioLine;
33 boost::asio::posix::stream_descriptor gpioFd;
James Feist7b18b1e2019-05-14 13:42:09 -070034 std::string name;
James Feist7bc2bab2018-10-26 14:09:45 -070035};
James Feistdc6c55f2018-10-31 12:53:20 -070036
James Feist7b18b1e2019-05-14 13:42:09 -070037namespace redundancy
38{
39constexpr const char* full = "Full";
40constexpr const char* degraded = "Degraded";
41constexpr const char* failed = "Failed";
42} // namespace redundancy
43
James Feistdc6c55f2018-10-31 12:53:20 -070044class RedundancySensor
45{
46 public:
James Feistd8705872019-02-08 13:26:09 -080047 RedundancySensor(size_t count, const std::vector<std::string>& children,
James Feist9bb67462019-03-15 15:09:50 -070048 sdbusplus::asio::object_server& objectServer,
49 const std::string& sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -070050 ~RedundancySensor();
51
James Feistd8705872019-02-08 13:26:09 -080052 void update(const std::string& name, bool failed);
James Feistdc6c55f2018-10-31 12:53:20 -070053
54 private:
55 size_t count;
James Feist7b18b1e2019-05-14 13:42:09 -070056 std::string state = redundancy::full;
James Feistdc6c55f2018-10-31 12:53:20 -070057 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
James Feist9bb67462019-03-15 15:09:50 -070058 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feistd8705872019-02-08 13:26:09 -080059 sdbusplus::asio::object_server& objectServer;
James Feistdc6c55f2018-10-31 12:53:20 -070060 boost::container::flat_map<std::string, bool> statuses;
61};
62
Josh Lehan5170fe62022-08-03 13:17:41 -070063class TachSensor :
64 public Sensor,
65 public std::enable_shared_from_this<TachSensor>
James Feist6714a252018-09-10 15:26:18 -070066{
67 public:
James Feistd8705872019-02-08 13:26:09 -080068 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 Feist7b18b1e2019-05-14 13:42:09 -070072 std::optional<RedundancySensor>* redundancy,
Ed Tanous1f978632023-02-28 18:16:39 -080073 boost::asio::io_context& io, const std::string& fanName,
James Feistd8705872019-02-08 13:26:09 -080074 std::vector<thresholds::Threshold>&& thresholds,
75 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080076 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070077 const PowerState& powerState,
78 const std::optional<std::string>& led);
Ed Tanous8a57ec02020-10-09 12:46:52 -070079 ~TachSensor() override;
Josh Lehan5170fe62022-08-03 13:17:41 -070080 void setupRead();
James Feist6714a252018-09-10 15:26:18 -070081
82 private:
Josh Lehan5170fe62022-08-03 13:17:41 -070083 // 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 Feistd8705872019-02-08 13:26:09 -080086 sdbusplus::asio::object_server& objServer;
James Feist7b18b1e2019-05-14 13:42:09 -070087 std::optional<RedundancySensor>* redundancy;
James Feist7bc2bab2018-10-26 14:09:45 -070088 std::unique_ptr<PresenceSensor> presence;
James Feist60c0ec72019-03-18 15:08:43 -070089 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
James Feistd8bd5622019-06-26 12:09:05 -070090 std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
Josh Lehan5170fe62022-08-03 13:17:41 -070091 boost::asio::random_access_file inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070092 boost::asio::steady_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070093 std::string path;
James Feist49a8ccd2020-09-16 16:09:52 -070094 std::optional<std::string> led;
95 bool ledState = false;
Josh Lehan5170fe62022-08-03 13:17:41 -070096
97 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
98 void restartRead(size_t pollTime);
Ed Tanous201a1012024-04-03 18:07:28 -070099 void checkThresholds() override;
James Feist6714a252018-09-10 15:26:18 -0700100};
James Feist7b18b1e2019-05-14 13:42:09 -0700101
102inline void logFanInserted(const std::string& device)
103{
Ed Tanous2049bd22022-07-09 07:20:26 -0700104 const auto* msg = "OpenBMC.0.1.FanInserted";
Patrick Williams0c42f402021-08-27 16:05:45 -0500105 lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg,
106 "REDFISH_MESSAGE_ARGS", device);
James Feist7b18b1e2019-05-14 13:42:09 -0700107}
108
109inline void logFanRemoved(const std::string& device)
110{
Ed Tanous2049bd22022-07-09 07:20:26 -0700111 const auto* msg = "OpenBMC.0.1.FanRemoved";
Patrick Williams0c42f402021-08-27 16:05:45 -0500112 lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg, "REDFISH_MESSAGE_ARGS",
113 device);
James Feist7b18b1e2019-05-14 13:42:09 -0700114}
115
Ed Tanous201a1012024-04-03 18:07:28 -0700116inline void logFanRedundancyLost()
James Feist7b18b1e2019-05-14 13:42:09 -0700117{
Ed Tanous2049bd22022-07-09 07:20:26 -0700118 const auto* msg = "OpenBMC.0.1.FanRedundancyLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500119 lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg);
James Feist7b18b1e2019-05-14 13:42:09 -0700120}
121
Ed Tanous201a1012024-04-03 18:07:28 -0700122inline void logFanRedundancyRestored()
James Feist7b18b1e2019-05-14 13:42:09 -0700123{
Ed Tanous2049bd22022-07-09 07:20:26 -0700124 const auto* msg = "OpenBMC.0.1.FanRedundancyRegained";
Patrick Williams0c42f402021-08-27 16:05:45 -0500125 lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg);
James Feist7b18b1e2019-05-14 13:42:09 -0700126}