James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 2 | #include "Thresholds.hpp" |
| 3 | #include "sensor.hpp" |
| 4 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 5 | #include <systemd/sd-journal.h> |
| 6 | |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 7 | #include <boost/asio/streambuf.hpp> |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 8 | #include <boost/container/flat_map.hpp> |
| 9 | #include <boost/container/flat_set.hpp> |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 10 | #include <gpiod.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 11 | #include <sdbusplus/asio/object_server.hpp> |
| 12 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <optional> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 18 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 19 | class PresenceSensor |
| 20 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 21 | public: |
Zhikui Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 22 | PresenceSensor(const std::string& pinName, bool inverted, |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 23 | boost::asio::io_service& io, const std::string& name); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 24 | ~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 Ren | 347dd4e | 2019-12-12 13:39:50 -0800 | [diff] [blame] | 33 | gpiod::line gpioLine; |
| 34 | boost::asio::posix::stream_descriptor gpioFd; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 35 | std::string name; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 36 | }; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 37 | |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 38 | namespace redundancy |
| 39 | { |
| 40 | constexpr const char* full = "Full"; |
| 41 | constexpr const char* degraded = "Degraded"; |
| 42 | constexpr const char* failed = "Failed"; |
| 43 | } // namespace redundancy |
| 44 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 45 | class RedundancySensor |
| 46 | { |
| 47 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 48 | RedundancySensor(size_t count, const std::vector<std::string>& children, |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 49 | sdbusplus::asio::object_server& objectServer, |
| 50 | const std::string& sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 51 | ~RedundancySensor(); |
| 52 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 53 | void update(const std::string& name, bool failed); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | size_t count; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 57 | std::string state = redundancy::full; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 58 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 59 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 60 | sdbusplus::asio::object_server& objectServer; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 61 | boost::container::flat_map<std::string, bool> statuses; |
| 62 | }; |
| 63 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 64 | class TachSensor : public Sensor |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
| 66 | public: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 67 | 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 Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 71 | std::optional<RedundancySensor>* redundancy, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 72 | boost::asio::io_service& io, const std::string& fanName, |
| 73 | std::vector<thresholds::Threshold>&& thresholds, |
| 74 | const std::string& sensorConfiguration, |
Josh Lehan | f920e09 | 2020-08-07 00:12:54 -0700 | [diff] [blame] | 75 | const std::pair<size_t, size_t>& limits, |
| 76 | const PowerState& powerState = PowerState::on); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | ~TachSensor(); |
| 78 | |
| 79 | private: |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 80 | sdbusplus::asio::object_server& objServer; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 81 | std::optional<RedundancySensor>* redundancy; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 82 | std::unique_ptr<PresenceSensor> presence; |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 83 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface; |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 84 | std::shared_ptr<sdbusplus::asio::dbus_interface> itemAssoc; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 85 | boost::asio::posix::stream_descriptor inputDev; |
| 86 | boost::asio::deadline_timer waitTimer; |
| 87 | boost::asio::streambuf readBuf; |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 88 | std::string path; |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 89 | size_t errCount; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 90 | void setupRead(void); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 91 | void handleResponse(const boost::system::error_code& err); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 92 | void checkThresholds(void) override; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | }; |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 94 | |
| 95 | inline void logFanInserted(const std::string& device) |
| 96 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 97 | sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR, |
| 98 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanInserted", |
| 99 | "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL); |
| 100 | } |
| 101 | |
| 102 | inline void logFanRemoved(const std::string& device) |
| 103 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 104 | sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR, |
| 105 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRemoved", |
| 106 | "REDFISH_MESSAGE_ARGS=%s", device.c_str(), NULL); |
| 107 | } |
| 108 | |
| 109 | inline void logFanRedundancyLost(void) |
| 110 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 111 | sd_journal_send("MESSAGE=%s", "Fan Inserted", "PRIORITY=%i", LOG_ERR, |
| 112 | "REDFISH_MESSAGE_ID=%s", "OpenBMC.0.1.FanRedundancyLost", |
| 113 | NULL); |
| 114 | } |
| 115 | |
| 116 | inline void logFanRedundancyRestored(void) |
| 117 | { |
James Feist | 7b18b1e | 2019-05-14 13:42:09 -0700 | [diff] [blame] | 118 | sd_journal_send("MESSAGE=%s", "Fan Removed", "PRIORITY=%i", LOG_ERR, |
| 119 | "REDFISH_MESSAGE_ID=%s", |
| 120 | "OpenBMC.0.1.FanRedundancyRegained", NULL); |
| 121 | } |