xref: /openbmc/dbus-sensors/src/fan/TachSensor.hpp (revision d7be555ee0d885418e9a862b16565a0474c68d14)
1 #pragma once
2 
3 #include "PresenceGpio.hpp"
4 #include "Thresholds.hpp"
5 #include "sensor.hpp"
6 
7 #include <boost/asio/random_access_file.hpp>
8 #include <boost/container/flat_map.hpp>
9 #include <boost/container/flat_set.hpp>
10 #include <gpiod.hpp>
11 #include <phosphor-logging/lg2.hpp>
12 #include <sdbusplus/asio/object_server.hpp>
13 
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace redundancy
21 {
22 constexpr const char* full = "Full";
23 constexpr const char* degraded = "Degraded";
24 constexpr const char* failed = "Failed";
25 } // namespace redundancy
26 
27 class RedundancySensor
28 {
29   public:
30     RedundancySensor(size_t count, const std::vector<std::string>& children,
31                      sdbusplus::asio::object_server& objectServer,
32                      const std::string& sensorConfiguration);
33     ~RedundancySensor();
34 
35     void update(const std::string& name, bool failed);
36 
37   private:
38     size_t count;
39     std::string state = redundancy::full;
40     std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
41     std::shared_ptr<sdbusplus::asio::dbus_interface> association;
42     sdbusplus::asio::object_server& objectServer;
43     boost::container::flat_map<std::string, bool> statuses;
44 
logFanRedundancyLost()45     static void logFanRedundancyLost()
46     {
47         const auto* msg = "OpenBMC.0.1.FanRedundancyLost";
48         lg2::error("Fan Inserted", "REDFISH_MESSAGE_ID", msg);
49     }
50 
logFanRedundancyRestored()51     static void logFanRedundancyRestored()
52     {
53         const auto* msg = "OpenBMC.0.1.FanRedundancyRegained";
54         lg2::error("Fan Removed", "REDFISH_MESSAGE_ID", msg);
55     }
56 };
57 
58 class TachSensor :
59     public Sensor,
60     public std::enable_shared_from_this<TachSensor>
61 {
62   public:
63     TachSensor(const std::string& path, const std::string& objectType,
64                sdbusplus::asio::object_server& objectServer,
65                std::shared_ptr<sdbusplus::asio::connection>& conn,
66                std::shared_ptr<PresenceGpio>& presence,
67                std::optional<RedundancySensor>* redundancy,
68                boost::asio::io_context& io, const std::string& fanName,
69                std::vector<thresholds::Threshold>&& thresholds,
70                const std::string& sensorConfiguration,
71                const std::pair<double, double>& limits,
72                const PowerState& powerState,
73                const std::optional<std::string>& led);
74     ~TachSensor() override;
75     void setupRead();
76 
77   private:
78     // Ordering is important here; readBuf is first so that it's not destroyed
79     // while async operations from other member fields might still be using it.
80     std::array<char, 128> readBuf{};
81     sdbusplus::asio::object_server& objServer;
82     std::optional<RedundancySensor>* redundancy;
83     std::shared_ptr<PresenceGpio> presence;
84     std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
85     std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
86     boost::asio::random_access_file inputDev;
87     boost::asio::steady_timer waitTimer;
88     std::string path;
89     std::optional<std::string> led;
90     bool ledState = false;
91 
92     void handleResponse(const boost::system::error_code& err, size_t bytesRead);
93     void restartRead(size_t pollTime);
94     void checkThresholds() override;
95 };
96