blob: 1e5347f300cd2d9d51cbbeff9e0b7c8c463a140c [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
Patrick Ventureca44b2f2019-10-31 11:02:26 -07002#include "Thresholds.hpp"
3#include "sensor.hpp"
4
James Feist7b18b1e2019-05-14 13:42:09 -07005#include <systemd/sd-journal.h>
6
James Feist8086aba2020-08-25 16:00:59 -07007#include <boost/asio/streambuf.hpp>
James Feistdc6c55f2018-10-31 12:53:20 -07008#include <boost/container/flat_map.hpp>
9#include <boost/container/flat_set.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080010#include <gpiod.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:
Zhikui Ren347dd4e2019-12-12 13:39:50 -080022 PresenceSensor(const std::string& pinName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -070023 boost::asio::io_service& io, const std::string& name);
James Feist7bc2bab2018-10-26 14:09:45 -070024 ~PresenceSensor();
25
26 void monitorPresence(void);
27 void read(void);
28 bool getValue(void);
29
30 private:
31 bool status = true;
32 bool inverted;
Zhikui Ren347dd4e2019-12-12 13:39:50 -080033 gpiod::line gpioLine;
34 boost::asio::posix::stream_descriptor gpioFd;
James Feist7b18b1e2019-05-14 13:42:09 -070035 std::string name;
James Feist7bc2bab2018-10-26 14:09:45 -070036};
James Feistdc6c55f2018-10-31 12:53:20 -070037
James Feist7b18b1e2019-05-14 13:42:09 -070038namespace redundancy
39{
40constexpr const char* full = "Full";
41constexpr const char* degraded = "Degraded";
42constexpr const char* failed = "Failed";
43} // namespace redundancy
44
James Feistdc6c55f2018-10-31 12:53:20 -070045class RedundancySensor
46{
47 public:
James Feistd8705872019-02-08 13:26:09 -080048 RedundancySensor(size_t count, const std::vector<std::string>& children,
James Feist9bb67462019-03-15 15:09:50 -070049 sdbusplus::asio::object_server& objectServer,
50 const std::string& sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -070051 ~RedundancySensor();
52
James Feistd8705872019-02-08 13:26:09 -080053 void update(const std::string& name, bool failed);
James Feistdc6c55f2018-10-31 12:53:20 -070054
55 private:
56 size_t count;
James Feist7b18b1e2019-05-14 13:42:09 -070057 std::string state = redundancy::full;
James Feistdc6c55f2018-10-31 12:53:20 -070058 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
James Feist9bb67462019-03-15 15:09:50 -070059 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feistd8705872019-02-08 13:26:09 -080060 sdbusplus::asio::object_server& objectServer;
James Feistdc6c55f2018-10-31 12:53:20 -070061 boost::container::flat_map<std::string, bool> statuses;
62};
63
James Feist251c7822018-09-12 12:54:15 -070064class TachSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070065{
66 public:
James Feistd8705872019-02-08 13:26:09 -080067 TachSensor(const std::string& path, const std::string& objectType,
68 sdbusplus::asio::object_server& objectServer,
69 std::shared_ptr<sdbusplus::asio::connection>& conn,
70 std::unique_ptr<PresenceSensor>&& presence,
James Feist7b18b1e2019-05-14 13:42:09 -070071 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080072 boost::asio::io_service& io, const std::string& fanName,
73 std::vector<thresholds::Threshold>&& thresholds,
74 const std::string& sensorConfiguration,
Josh Lehanf920e092020-08-07 00:12:54 -070075 const std::pair<size_t, size_t>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070076 const PowerState& powerState,
77 const std::optional<std::string>& led);
James Feist6714a252018-09-10 15:26:18 -070078 ~TachSensor();
79
80 private:
James Feistd8705872019-02-08 13:26:09 -080081 sdbusplus::asio::object_server& objServer;
James Feist7b18b1e2019-05-14 13:42:09 -070082 std::optional<RedundancySensor>* redundancy;
James Feist7bc2bab2018-10-26 14:09:45 -070083 std::unique_ptr<PresenceSensor> presence;
James Feist60c0ec72019-03-18 15:08:43 -070084 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
James Feistd8bd5622019-06-26 12:09:05 -070085 std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 boost::asio::posix::stream_descriptor inputDev;
87 boost::asio::deadline_timer waitTimer;
88 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070089 std::string path;
James Feist49a8ccd2020-09-16 16:09:52 -070090 std::optional<std::string> led;
91 bool ledState = false;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050092 size_t errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080094 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080095 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070096};
James Feist7b18b1e2019-05-14 13:42:09 -070097
98inline void logFanInserted(const std::string& device)
99{
James Feist7b18b1e2019-05-14 13:42:09 -0700100 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
101 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanInserted",
102 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
103}
104
105inline void logFanRemoved(const std::string& device)
106{
James Feist7b18b1e2019-05-14 13:42:09 -0700107 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
108 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRemoved",
109 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
110}
111
112inline void logFanRedundancyLost(void)
113{
James Feist7b18b1e2019-05-14 13:42:09 -0700114 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
115 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRedundancyLost",
116 NULL);
117}
118
119inline void logFanRedundancyRestored(void)
120{
James Feist7b18b1e2019-05-14 13:42:09 -0700121 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
122 "REDFISH_MESSAGE_ID=%s",
123 "OpenBMC.0.1.FanRedundancyRegained", NULL);
124}