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)), |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 23 | hysteresisTrigger((max - min) * 0.01), |
| 24 | hysteresisPublish((max - min) * 0.0001) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 25 | { |
| 26 | } |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 27 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 28 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 29 | std::string name; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 30 | std::string configurationPath; |
| 31 | std::string objectType; |
| 32 | double maxValue; |
| 33 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 34 | std::vector<thresholds::Threshold> thresholds; |
| 35 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 36 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 37 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 38 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 39 | double value = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 40 | bool overriddenState = false; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 41 | bool internalSet = false; |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 42 | double hysteresisTrigger; |
| 43 | double hysteresisPublish; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 44 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 45 | int setSensorValue(const double& newValue, double& oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 46 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 47 | if (!internalSet) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 48 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 49 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 50 | overriddenState = true; |
| 51 | // check thresholds for external set |
| 52 | value = newValue; |
| 53 | checkThresholds(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 54 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 55 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 56 | { |
| 57 | oldValue = newValue; |
| 58 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 59 | return 1; |
| 60 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 61 | |
| 62 | void |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 63 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 64 | const std::string label = std::string(), |
| 65 | size_t thresholdSize = 0) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 66 | { |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 67 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 68 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 69 | sensorInterface->register_property("MaxValue", maxValue); |
| 70 | sensorInterface->register_property("MinValue", minValue); |
| 71 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 72 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 73 | return setSensorValue(newValue, oldValue); |
| 74 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 75 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 76 | { |
| 77 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 78 | std::string level; |
| 79 | std::string alarm; |
| 80 | if (threshold.level == thresholds::Level::CRITICAL) |
| 81 | { |
| 82 | iface = thresholdInterfaceCritical; |
| 83 | if (threshold.direction == thresholds::Direction::HIGH) |
| 84 | { |
| 85 | level = "CriticalHigh"; |
| 86 | alarm = "CriticalAlarmHigh"; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | level = "CriticalLow"; |
| 91 | alarm = "CriticalAlarmLow"; |
| 92 | } |
| 93 | } |
| 94 | else if (threshold.level == thresholds::Level::WARNING) |
| 95 | { |
| 96 | iface = thresholdInterfaceWarning; |
| 97 | if (threshold.direction == thresholds::Direction::HIGH) |
| 98 | { |
| 99 | level = "WarningHigh"; |
| 100 | alarm = "WarningAlarmHigh"; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | level = "WarningLow"; |
| 105 | alarm = "WarningAlarmLow"; |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | std::cerr << "Unknown threshold level" << threshold.level |
| 111 | << "\n"; |
| 112 | continue; |
| 113 | } |
| 114 | if (!iface) |
| 115 | { |
| 116 | std::cout << "trying to set uninitialized interface\n"; |
| 117 | continue; |
| 118 | } |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 119 | |
| 120 | size_t thresSize = |
| 121 | label.empty() ? thresholds.size() : thresholdSize; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 122 | iface->register_property( |
| 123 | level, threshold.value, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 124 | [&, label, thresSize](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 125 | oldValue = request; // todo, just let the config do this? |
| 126 | threshold.value = request; |
| 127 | thresholds::persistThreshold(configurationPath, objectType, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame^] | 128 | threshold, conn, thresSize, |
| 129 | label); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 130 | // Invalidate previously remembered value, |
| 131 | // so new thresholds will be checked during next update, |
| 132 | // even if sensor reading remains unchanged. |
| 133 | value = std::numeric_limits<double>::quiet_NaN(); |
| 134 | |
| 135 | // Although tempting, don't call checkThresholds() from here |
| 136 | // directly. Let the regular sensor monitor call the same |
| 137 | // using updateValue(), which can check conditions like |
| 138 | // poweron, etc., before raising any event. |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 139 | return 1; |
| 140 | }); |
| 141 | iface->register_property(alarm, false); |
| 142 | } |
| 143 | if (!sensorInterface->initialize()) |
| 144 | { |
| 145 | std::cerr << "error initializing value interface\n"; |
| 146 | } |
| 147 | if (thresholdInterfaceWarning && |
| 148 | !thresholdInterfaceWarning->initialize()) |
| 149 | { |
| 150 | std::cerr << "error initializing warning threshold interface\n"; |
| 151 | } |
| 152 | |
| 153 | if (thresholdInterfaceCritical && |
| 154 | !thresholdInterfaceCritical->initialize()) |
| 155 | { |
| 156 | std::cerr << "error initializing critical threshold interface\n"; |
| 157 | } |
| 158 | } |
| 159 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 160 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 161 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 162 | // Ignore if overriding is enabled |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 163 | if (overriddenState) |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 164 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 165 | return; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 166 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 167 | |
| 168 | bool isChanged = false; |
| 169 | |
| 170 | // Avoid floating-point equality comparison, |
| 171 | // by instead comparing against a very small hysteresis range. |
| 172 | if (std::isnan(value) || std::isnan(newValue)) |
| 173 | { |
| 174 | // If one or the other is NAN, |
| 175 | // either we are intentionally invalidating a sensor reading, |
| 176 | // or initializing for the very first time, |
| 177 | // either way we should always publish this. |
| 178 | isChanged = true; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | // This essentially does "if (value != newValue)", |
| 183 | // but safely against floating-point background noise. |
| 184 | double diff = std::abs(value - newValue); |
| 185 | if (diff > hysteresisPublish) |
| 186 | { |
| 187 | isChanged = true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Ignore if the change is so small as to be deemed unchanged |
| 192 | if (!isChanged) |
| 193 | { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // The value will be changed, keep track of it for next time |
| 198 | value = newValue; |
| 199 | |
| 200 | // Indicate that it is internal set call |
| 201 | internalSet = true; |
| 202 | if (!(sensorInterface->set_property("Value", newValue))) |
| 203 | { |
| 204 | std::cerr << "error setting property to " << newValue << "\n"; |
| 205 | } |
| 206 | internalSet = false; |
| 207 | |
| 208 | // Always check thresholds after changing the value, |
| 209 | // as the test against hysteresisTrigger now takes place in |
| 210 | // the thresholds::checkThresholds() method, |
| 211 | // which is called by checkThresholds() below, |
| 212 | // in all current implementations of sensors that have thresholds. |
| 213 | checkThresholds(); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 214 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 215 | }; |