James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 3 | #include "Thresholds.hpp" |
| 4 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 5 | #include <limits> |
| 6 | #include <memory> |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 7 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 10 | |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 11 | constexpr size_t sensorFailedPollTimeMs = 5000; |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 12 | |
| 13 | constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 14 | struct Sensor |
| 15 | { |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 16 | Sensor(const std::string& name, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 17 | std::vector<thresholds::Threshold>&& thresholdData, |
| 18 | const std::string& configurationPath, const std::string& objectType, |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 19 | const double max, const double min) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 20 | name(name), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 21 | configurationPath(configurationPath), objectType(objectType), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 22 | maxValue(max), minValue(min), thresholds(std::move(thresholdData)), |
James Feist | 9f16b4b | 2019-06-04 14:59:42 -0700 | [diff] [blame] | 23 | hysteresis((max - min) * 0.01) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 24 | { |
| 25 | } |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 26 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 27 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 28 | std::string name; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 29 | std::string configurationPath; |
| 30 | std::string objectType; |
| 31 | double maxValue; |
| 32 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 33 | std::vector<thresholds::Threshold> thresholds; |
| 34 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 35 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 36 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 37 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 38 | double value = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 39 | bool overriddenState = false; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 40 | bool internalSet = false; |
James Feist | 9f16b4b | 2019-06-04 14:59:42 -0700 | [diff] [blame] | 41 | double hysteresis; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 42 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 43 | int setSensorValue(const double& newValue, double& oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 44 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 45 | if (!internalSet) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 46 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 47 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 48 | overriddenState = true; |
| 49 | // check thresholds for external set |
| 50 | value = newValue; |
| 51 | checkThresholds(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 52 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 53 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 54 | { |
| 55 | oldValue = newValue; |
| 56 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 57 | return 1; |
| 58 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 59 | |
| 60 | void |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 61 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 62 | { |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 63 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame^] | 64 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 65 | sensorInterface->register_property("MaxValue", maxValue); |
| 66 | sensorInterface->register_property("MinValue", minValue); |
| 67 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 68 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 69 | return setSensorValue(newValue, oldValue); |
| 70 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 71 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 72 | { |
| 73 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 74 | std::string level; |
| 75 | std::string alarm; |
| 76 | if (threshold.level == thresholds::Level::CRITICAL) |
| 77 | { |
| 78 | iface = thresholdInterfaceCritical; |
| 79 | if (threshold.direction == thresholds::Direction::HIGH) |
| 80 | { |
| 81 | level = "CriticalHigh"; |
| 82 | alarm = "CriticalAlarmHigh"; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | level = "CriticalLow"; |
| 87 | alarm = "CriticalAlarmLow"; |
| 88 | } |
| 89 | } |
| 90 | else if (threshold.level == thresholds::Level::WARNING) |
| 91 | { |
| 92 | iface = thresholdInterfaceWarning; |
| 93 | if (threshold.direction == thresholds::Direction::HIGH) |
| 94 | { |
| 95 | level = "WarningHigh"; |
| 96 | alarm = "WarningAlarmHigh"; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | level = "WarningLow"; |
| 101 | alarm = "WarningAlarmLow"; |
| 102 | } |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | std::cerr << "Unknown threshold level" << threshold.level |
| 107 | << "\n"; |
| 108 | continue; |
| 109 | } |
| 110 | if (!iface) |
| 111 | { |
| 112 | std::cout << "trying to set uninitialized interface\n"; |
| 113 | continue; |
| 114 | } |
| 115 | iface->register_property( |
| 116 | level, threshold.value, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 117 | [&](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 118 | oldValue = request; // todo, just let the config do this? |
| 119 | threshold.value = request; |
| 120 | thresholds::persistThreshold(configurationPath, objectType, |
James Feist | a222ba7 | 2019-03-01 15:57:51 -0800 | [diff] [blame] | 121 | threshold, conn, |
| 122 | thresholds.size()); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 123 | return 1; |
| 124 | }); |
| 125 | iface->register_property(alarm, false); |
| 126 | } |
| 127 | if (!sensorInterface->initialize()) |
| 128 | { |
| 129 | std::cerr << "error initializing value interface\n"; |
| 130 | } |
| 131 | if (thresholdInterfaceWarning && |
| 132 | !thresholdInterfaceWarning->initialize()) |
| 133 | { |
| 134 | std::cerr << "error initializing warning threshold interface\n"; |
| 135 | } |
| 136 | |
| 137 | if (thresholdInterfaceCritical && |
| 138 | !thresholdInterfaceCritical->initialize()) |
| 139 | { |
| 140 | std::cerr << "error initializing critical threshold interface\n"; |
| 141 | } |
| 142 | } |
| 143 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 144 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 145 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 146 | // Ignore if overriding is enabled |
| 147 | if (!overriddenState) |
| 148 | { |
| 149 | // Indicate that it is internal set call |
| 150 | internalSet = true; |
Josh Lehan | 432d1ed | 2019-10-16 12:23:31 -0700 | [diff] [blame] | 151 | if (!(sensorInterface->set_property("Value", newValue))) |
| 152 | { |
| 153 | std::cerr << "error setting property to " << newValue << "\n"; |
| 154 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 155 | internalSet = false; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 156 | double diff = std::abs(value - newValue); |
| 157 | if (std::isnan(diff) || diff > hysteresis) |
James Feist | 9f16b4b | 2019-06-04 14:59:42 -0700 | [diff] [blame] | 158 | { |
| 159 | value = newValue; |
James Feist | 9f16b4b | 2019-06-04 14:59:42 -0700 | [diff] [blame] | 160 | } |
James Feist | 19cb01d | 2019-09-16 13:43:52 -0700 | [diff] [blame] | 161 | checkThresholds(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 162 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 163 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 164 | }; |