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 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 5 | #include <sdbusplus/asio/object_server.hpp> |
| 6 | |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 7 | #include <limits> |
| 8 | #include <memory> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 11 | |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 12 | constexpr size_t sensorFailedPollTimeMs = 5000; |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 13 | |
| 14 | constexpr const char* sensorValueInterface = "xyz.openbmc_project.Sensor.Value"; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame^] | 15 | constexpr const char* availableInterfaceName = |
| 16 | "xyz.openbmc_project.State.Decorator.Availability"; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 17 | struct Sensor |
| 18 | { |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 19 | Sensor(const std::string& name, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 20 | std::vector<thresholds::Threshold>&& thresholdData, |
| 21 | const std::string& configurationPath, const std::string& objectType, |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 22 | const double max, const double min) : |
AppaRao Puli | 54ffb27 | 2020-06-02 19:09:38 +0530 | [diff] [blame] | 23 | name(std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_")), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 24 | configurationPath(configurationPath), objectType(objectType), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 25 | maxValue(max), minValue(min), thresholds(std::move(thresholdData)), |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 26 | hysteresisTrigger((max - min) * 0.01), |
| 27 | hysteresisPublish((max - min) * 0.0001) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 28 | {} |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 29 | virtual ~Sensor() = default; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 30 | virtual void checkThresholds(void) = 0; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 31 | std::string name; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 32 | std::string configurationPath; |
| 33 | std::string objectType; |
| 34 | double maxValue; |
| 35 | double minValue; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 36 | std::vector<thresholds::Threshold> thresholds; |
| 37 | std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface; |
| 38 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning; |
| 39 | std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical; |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 40 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame^] | 41 | std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface; |
James Feist | 8fd8a58 | 2018-11-16 11:10:46 -0800 | [diff] [blame] | 42 | double value = std::numeric_limits<double>::quiet_NaN(); |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 43 | bool overriddenState = false; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 44 | bool internalSet = false; |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame^] | 45 | bool available = true; |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 46 | double hysteresisTrigger; |
| 47 | double hysteresisPublish; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 48 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 49 | int setSensorValue(const double& newValue, double& oldValue) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 50 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 51 | if (!internalSet) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 52 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 53 | oldValue = newValue; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 54 | overriddenState = true; |
| 55 | // check thresholds for external set |
| 56 | value = newValue; |
| 57 | checkThresholds(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 58 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 59 | else if (!overriddenState) |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 60 | { |
| 61 | oldValue = newValue; |
| 62 | } |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 63 | return 1; |
| 64 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 65 | |
| 66 | void |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 67 | setInitialProperties(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 68 | const std::string label = std::string(), |
| 69 | size_t thresholdSize = 0) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 70 | { |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 71 | createAssociation(association, configurationPath); |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 72 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 73 | sensorInterface->register_property("MaxValue", maxValue); |
| 74 | sensorInterface->register_property("MinValue", minValue); |
| 75 | sensorInterface->register_property( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 76 | "Value", value, [&](const double& newValue, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 77 | return setSensorValue(newValue, oldValue); |
| 78 | }); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 79 | for (auto& threshold : thresholds) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 80 | { |
| 81 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 82 | std::string level; |
| 83 | std::string alarm; |
| 84 | if (threshold.level == thresholds::Level::CRITICAL) |
| 85 | { |
| 86 | iface = thresholdInterfaceCritical; |
| 87 | if (threshold.direction == thresholds::Direction::HIGH) |
| 88 | { |
| 89 | level = "CriticalHigh"; |
| 90 | alarm = "CriticalAlarmHigh"; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | level = "CriticalLow"; |
| 95 | alarm = "CriticalAlarmLow"; |
| 96 | } |
| 97 | } |
| 98 | else if (threshold.level == thresholds::Level::WARNING) |
| 99 | { |
| 100 | iface = thresholdInterfaceWarning; |
| 101 | if (threshold.direction == thresholds::Direction::HIGH) |
| 102 | { |
| 103 | level = "WarningHigh"; |
| 104 | alarm = "WarningAlarmHigh"; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | level = "WarningLow"; |
| 109 | alarm = "WarningAlarmLow"; |
| 110 | } |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | std::cerr << "Unknown threshold level" << threshold.level |
| 115 | << "\n"; |
| 116 | continue; |
| 117 | } |
| 118 | if (!iface) |
| 119 | { |
| 120 | std::cout << "trying to set uninitialized interface\n"; |
| 121 | continue; |
| 122 | } |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 123 | |
| 124 | size_t thresSize = |
| 125 | label.empty() ? thresholds.size() : thresholdSize; |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 126 | iface->register_property( |
| 127 | level, threshold.value, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 128 | [&, label, thresSize](const double& request, double& oldValue) { |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 129 | oldValue = request; // todo, just let the config do this? |
| 130 | threshold.value = request; |
| 131 | thresholds::persistThreshold(configurationPath, objectType, |
Cheng C Yang | 6b1247a | 2020-03-09 23:48:39 +0800 | [diff] [blame] | 132 | threshold, conn, thresSize, |
| 133 | label); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 134 | // Invalidate previously remembered value, |
| 135 | // so new thresholds will be checked during next update, |
| 136 | // even if sensor reading remains unchanged. |
| 137 | value = std::numeric_limits<double>::quiet_NaN(); |
| 138 | |
| 139 | // Although tempting, don't call checkThresholds() from here |
| 140 | // directly. Let the regular sensor monitor call the same |
| 141 | // using updateValue(), which can check conditions like |
| 142 | // poweron, etc., before raising any event. |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 143 | return 1; |
| 144 | }); |
| 145 | iface->register_property(alarm, false); |
| 146 | } |
| 147 | if (!sensorInterface->initialize()) |
| 148 | { |
| 149 | std::cerr << "error initializing value interface\n"; |
| 150 | } |
| 151 | if (thresholdInterfaceWarning && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 152 | !thresholdInterfaceWarning->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 153 | { |
| 154 | std::cerr << "error initializing warning threshold interface\n"; |
| 155 | } |
| 156 | |
| 157 | if (thresholdInterfaceCritical && |
Yong Li | f902c05 | 2020-05-07 17:13:53 +0800 | [diff] [blame] | 158 | !thresholdInterfaceCritical->initialize(true)) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 159 | { |
| 160 | std::cerr << "error initializing critical threshold interface\n"; |
| 161 | } |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame^] | 162 | |
| 163 | if (!availableInterface) |
| 164 | { |
| 165 | availableInterface = |
| 166 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 167 | conn, sensorInterface->get_object_path(), |
| 168 | availableInterfaceName); |
| 169 | availableInterface->register_property( |
| 170 | "Available", true, [this](const bool propIn, bool& old) { |
| 171 | if (propIn == old) |
| 172 | { |
| 173 | return 1; |
| 174 | } |
| 175 | if (!propIn) |
| 176 | { |
| 177 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 178 | } |
| 179 | old = propIn; |
| 180 | available = propIn; |
| 181 | return 1; |
| 182 | }); |
| 183 | availableInterface->initialize(); |
| 184 | } |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 185 | } |
| 186 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 187 | void updateValue(const double& newValue) |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 188 | { |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 189 | // Ignore if overriding is enabled |
James Feist | 67601bd | 2020-06-16 17:14:44 -0700 | [diff] [blame^] | 190 | if (overriddenState || !available) |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 191 | { |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 192 | return; |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 193 | } |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 194 | |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 195 | // Indicate that it is internal set call |
| 196 | internalSet = true; |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 197 | updateProperty(sensorInterface, value, newValue, "Value"); |
Josh Lehan | 883fb3a | 2020-02-27 14:41:39 -0800 | [diff] [blame] | 198 | internalSet = false; |
| 199 | |
| 200 | // Always check thresholds after changing the value, |
| 201 | // as the test against hysteresisTrigger now takes place in |
| 202 | // the thresholds::checkThresholds() method, |
| 203 | // which is called by checkThresholds() below, |
| 204 | // in all current implementations of sensors that have thresholds. |
| 205 | checkThresholds(); |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 206 | } |
Zbigniew Kurzynski | 4f45e42 | 2020-06-09 12:42:15 +0200 | [diff] [blame] | 207 | |
| 208 | void updateProperty( |
| 209 | std::shared_ptr<sdbusplus::asio::dbus_interface>& interface, |
| 210 | double& oldValue, const double& newValue, const char* dbusPropertyName) |
| 211 | { |
| 212 | if (requiresUpdate(oldValue, newValue)) |
| 213 | { |
| 214 | oldValue = newValue; |
| 215 | if (!(interface->set_property(dbusPropertyName, newValue))) |
| 216 | { |
| 217 | std::cerr << "error setting property " << dbusPropertyName |
| 218 | << " to " << newValue << "\n"; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | bool requiresUpdate(const double& lVal, const double& rVal) |
| 224 | { |
| 225 | if (std::isnan(lVal) || std::isnan(rVal)) |
| 226 | { |
| 227 | return true; |
| 228 | } |
| 229 | double diff = std::abs(lVal - rVal); |
| 230 | if (diff > hysteresisPublish) |
| 231 | { |
| 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | } |
Richard Marian Thomaiyar | c0ca7ee | 2019-04-24 21:22:52 +0530 | [diff] [blame] | 236 | }; |