blob: 9ae04e1ed844117f6a03329f1615b2f37e578b17 [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 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 Venturefd6ba732019-10-31 14:27:39 -070010#include <memory>
11#include <optional>
James Feist6714a252018-09-10 15:26:18 -070012#include <sdbusplus/asio/object_server.hpp>
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:
Zhikui Ren347dd4e2019-12-12 13:39:50 -080020 PresenceSensor(const std::string& pinName, 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);
26 bool getValue(void);
27
28 private:
29 bool status = true;
30 bool inverted;
Zhikui Ren347dd4e2019-12-12 13:39:50 -080031 gpiod::line gpioLine;
32 boost::asio::posix::stream_descriptor gpioFd;
James Feist7b18b1e2019-05-14 13:42:09 -070033 std::string name;
James Feist7bc2bab2018-10-26 14:09:45 -070034};
James Feistdc6c55f2018-10-31 12:53:20 -070035
James Feist7b18b1e2019-05-14 13:42:09 -070036namespace redundancy
37{
38constexpr const char* full = "Full";
39constexpr const char* degraded = "Degraded";
40constexpr const char* failed = "Failed";
41} // namespace redundancy
42
James Feistdc6c55f2018-10-31 12:53:20 -070043class RedundancySensor
44{
45 public:
James Feistd8705872019-02-08 13:26:09 -080046 RedundancySensor(size_t count, const std::vector<std::string>& children,
James Feist9bb67462019-03-15 15:09:50 -070047 sdbusplus::asio::object_server& objectServer,
48 const std::string& sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -070049 ~RedundancySensor();
50
James Feistd8705872019-02-08 13:26:09 -080051 void update(const std::string& name, bool failed);
James Feistdc6c55f2018-10-31 12:53:20 -070052
53 private:
54 size_t count;
James Feist7b18b1e2019-05-14 13:42:09 -070055 std::string state = redundancy::full;
James Feistdc6c55f2018-10-31 12:53:20 -070056 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
James Feist9bb67462019-03-15 15:09:50 -070057 std::shared_ptr<sdbusplus::asio::dbus_interface> association;
James Feistd8705872019-02-08 13:26:09 -080058 sdbusplus::asio::object_server& objectServer;
James Feistdc6c55f2018-10-31 12:53:20 -070059 boost::container::flat_map<std::string, bool> statuses;
60};
61
James Feist251c7822018-09-12 12:54:15 -070062class TachSensor : public Sensor
James Feist6714a252018-09-10 15:26:18 -070063{
64 public:
James Feistd8705872019-02-08 13:26:09 -080065 TachSensor(const std::string& path, const std::string& objectType,
66 sdbusplus::asio::object_server& objectServer,
67 std::shared_ptr<sdbusplus::asio::connection>& conn,
68 std::unique_ptr<PresenceSensor>&& presence,
James Feist7b18b1e2019-05-14 13:42:09 -070069 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080070 boost::asio::io_service& io, const std::string& fanName,
71 std::vector<thresholds::Threshold>&& thresholds,
72 const std::string& sensorConfiguration,
73 const std::pair<size_t, size_t>& limits);
James Feist6714a252018-09-10 15:26:18 -070074 ~TachSensor();
75
76 private:
James Feistd8705872019-02-08 13:26:09 -080077 sdbusplus::asio::object_server& objServer;
James Feist7b18b1e2019-05-14 13:42:09 -070078 std::optional<RedundancySensor>* redundancy;
James Feist7bc2bab2018-10-26 14:09:45 -070079 std::unique_ptr<PresenceSensor> presence;
James Feist60c0ec72019-03-18 15:08:43 -070080 std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface;
James Feistd8bd5622019-06-26 12:09:05 -070081 std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 boost::asio::posix::stream_descriptor inputDev;
83 boost::asio::deadline_timer waitTimer;
84 boost::asio::streambuf readBuf;
James Feist930fcde2019-05-28 12:58:43 -070085 std::string path;
Brad Bishopfbb44ad2019-11-08 09:42:37 -050086 size_t errCount;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087 void setupRead(void);
James Feistd8705872019-02-08 13:26:09 -080088 void handleResponse(const boost::system::error_code& err);
James Feistce3fca42018-11-21 12:58:24 -080089 void checkThresholds(void) override;
James Feist6714a252018-09-10 15:26:18 -070090};
James Feist7b18b1e2019-05-14 13:42:09 -070091
92inline void logFanInserted(const std::string& device)
93{
James Feist7b18b1e2019-05-14 13:42:09 -070094 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
95 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanInserted",
96 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
97}
98
99inline void logFanRemoved(const std::string& device)
100{
James Feist7b18b1e2019-05-14 13:42:09 -0700101 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
102 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRemoved",
103 "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL);
104}
105
106inline void logFanRedundancyLost(void)
107{
James Feist7b18b1e2019-05-14 13:42:09 -0700108 sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR,
109 "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRedundancyLost",
110 NULL);
111}
112
113inline void logFanRedundancyRestored(void)
114{
James Feist7b18b1e2019-05-14 13:42:09 -0700115 sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR,
116 "REDFISH_MESSAGE_ID=%s",
117 "OpenBMC.0.1.FanRedundancyRegained", NULL);
118}