blob: fa9de07aa45864f0e387d5cac44ff511a7096b35 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
James Feist7b18b1e2019-05-14 13:42:09 -07002#include <systemd/sd-journal.h>
3
Ed Tanous8a57ec02020-10-09 12:46:52 -07004#include <Thresholds.hpp>
James Feist8086aba2020-08-25 16:00:59 -07005#include <boost/asio/streambuf.hpp>
James Feistdc6c55f2018-10-31 12:53:20 -07006#include <boost/container/flat_map.hpp>
7#include <boost/container/flat_set.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -08008#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -07009#include <sdbusplus/asio/object_server.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070010#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070011
Patrick Venturefd6ba732019-10-31 14:27:39 -070012#include <memory>
13#include <optional>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <string>
15#include <utility>
16#include <vector>
James Feist6714a252018-09-10 15:26:18 -070017
James Feist7bc2bab2018-10-26 14:09:45 -070018class PresenceSensor
19{
James Feist7bc2bab2018-10-26 14:09:45 -070020 public:
Zhikui Ren347dd4e2019-12-12 13:39:50 -080021 PresenceSensor(const std::string& pinName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -070022 boost::asio::io_service& io, const std::string& name);
James Feist7bc2bab2018-10-26 14:09:45 -070023 ~PresenceSensor();
24
25 void monitorPresence(void);
26 void read(void);
27 bool getValue(void);
28
29 private:
30 bool status = true;
31 bool inverted;
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
James Feist251c7822018-09-12 12:54:15 -070063class TachSensor : public Sensor
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,
Josh Lehanf920e092020-08-07 00:12:54 -070074 const std::pair<size_t, size_t>& 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;
James Feist6714a252018-09-10 15:26:18 -070078
79 private:
James Feistd8705872019-02-08 13:26:09 -080080 sdbusplus::asio::object_server& objServer;
James Feist7b18b1e2019-05-14 13:42:09 -070081 std::optional<RedundancySensor>* redundancy;
James Feist7bc2bab2018-10-26 14:09:45 -070082 std::unique_ptr<PresenceSensor> presence;
James Feist60c0ec72019-03-18 15:08:43 -070083 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
James Feistd8bd5622019-06-26 12:09:05 -070084 std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085 boost::asio::posix::stream_descriptor inputDev;
86 boost::asio::deadline_timer waitTimer;
87 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070088 std::string path;
James Feist49a8ccd2020-09-16 16:09:52 -070089 std::optional<std::string> led;
90 bool ledState = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080092 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080093 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070094};
James Feist7b18b1e2019-05-14 13:42:09 -070095
96inline void logFanInserted(const std::string& device)
97{
James Feist7b18b1e2019-05-14 13:42:09 -070098 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
99 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanInserted",
100 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
101}
102
103inline void logFanRemoved(const std::string& device)
104{
James Feist7b18b1e2019-05-14 13:42:09 -0700105 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
106 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRemoved",
107 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
108}
109
110inline void logFanRedundancyLost(void)
111{
James Feist7b18b1e2019-05-14 13:42:09 -0700112 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
113 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRedundancyLost",
114 NULL);
115}
116
117inline void logFanRedundancyRestored(void)
118{
James Feist7b18b1e2019-05-14 13:42:09 -0700119 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
120 "REDFISH_MESSAGE_ID=%s",
121 "OpenBMC.0.1.FanRedundancyRegained", NULL);
122}