| Alexander Hansen | 46a755f | 2025-10-27 16:31:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Google Inc |
| 3 | |
| Pete O_o | 765a6d8 | 2025-07-23 21:44:14 -0700 | [diff] [blame] | 4 | #include "config.h" |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 5 | |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 6 | #include "dbuspassive.hpp" |
| 7 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 8 | #include "conf.hpp" |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 9 | #include "dbushelper_interface.hpp" |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 10 | #include "dbuspassiveredundancy.hpp" |
| Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 11 | #include "dbusutil.hpp" |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 12 | #include "failsafeloggers/failsafe_logger_utility.hpp" |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 13 | #include "interfaces.hpp" |
| James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 14 | #include "util.hpp" |
| Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 15 | |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 16 | #include <systemd/sd-bus.h> |
| 17 | |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 18 | #include <sdbusplus/bus.hpp> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 19 | #include <sdbusplus/message.hpp> |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 20 | #include <xyz/openbmc_project/Sensor/Threshold/Critical/common.hpp> |
| 21 | #include <xyz/openbmc_project/Sensor/Threshold/Warning/common.hpp> |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Sensor/Value/client.hpp> |
| Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 23 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <cmath> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 26 | #include <cstdint> |
| 27 | #include <exception> |
| 28 | #include <limits> |
| 29 | #include <map> |
| Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 30 | #include <memory> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 31 | #include <mutex> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 32 | #include <set> |
| Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 33 | #include <string> |
| Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 34 | #include <utility> |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 35 | #include <variant> |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 36 | |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 37 | #include "failsafeloggers/failsafe_logger.cpp" |
| 38 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 39 | using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value; |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 40 | using SensorThresholdWarning = |
| 41 | sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning; |
| 42 | using SensorThresholdCritical = |
| 43 | sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical; |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 44 | |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 45 | namespace pid_control |
| 46 | { |
| 47 | |
| Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 48 | std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive( |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 49 | sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 50 | std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info, |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 51 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) |
| Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 52 | { |
| 53 | if (helper == nullptr) |
| 54 | { |
| 55 | return nullptr; |
| 56 | } |
| Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 57 | if (!validType(type)) |
| Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 58 | { |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 62 | /* Need to get the scale and initial value */ |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 63 | /* service == busname */ |
| Harvey.Wu | f2efcbb | 2022-02-09 10:24:30 +0800 | [diff] [blame] | 64 | std::string path; |
| 65 | if (info->readPath.empty()) |
| 66 | { |
| 67 | path = getSensorPath(type, id); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | path = info->readPath; |
| 72 | } |
| Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 73 | |
| Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 74 | SensorProperties settings; |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 75 | bool failed; |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 76 | bool objectMissing = false; |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 77 | std::string service; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 78 | |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 79 | try |
| 80 | { |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 81 | service = helper->getService(SensorValue::interface, path); |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 82 | } |
| 83 | catch (const std::exception& e) |
| 84 | { |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 85 | #ifndef HANDLE_MISSING_OBJECT_PATHS |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 86 | return nullptr; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 87 | #else |
| 88 | // CASE1: The sensor is not on DBus, but as it is not in the |
| 89 | // MissingIsAcceptable list, the sensor should be built with a failed |
| 90 | // state to send the zone to failsafe mode. Everything will recover if |
| 91 | // all important sensors are back to DBus. swampd will be informed |
| 92 | // through InterfacesAdded signals and the sensors will be built again. |
| 93 | |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 94 | // CASE2: The sensor is on D-Bus (getService succeeds) but getProperties |
| 95 | // fails (e.g., D-Bus error or property fetch failure). In this case, |
| 96 | // handle-missing-object-paths does not apply. The sensor build fails, |
| 97 | // and the control loop will keep restarting until getProperties |
| 98 | // succeeds. |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 99 | |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 100 | // Only CASE1 may send the zone to failsafe mode if the sensor is not |
| 101 | // in MissingIsAcceptable. CASE2 results in continuous restart until |
| 102 | // recovery. |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 103 | |
| 104 | failed = true; |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 105 | objectMissing = true; |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 106 | settings.value = std::numeric_limits<double>::quiet_NaN(); |
| 107 | settings.unit = getSensorUnit(type); |
| 108 | settings.available = false; |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 109 | settings.unavailableAsFailed = true; |
| 110 | if (info->ignoreDbusMinMax) |
| 111 | { |
| 112 | settings.min = 0; |
| 113 | settings.max = 0; |
| 114 | } |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 115 | std::cerr << "DbusPassive: Sensor " << path |
| 116 | << " is missing from D-Bus, build this sensor as failed\n"; |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 117 | return std::make_unique<DbusPassive>( |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 118 | bus, type, id, std::move(helper), settings, failed, objectMissing, |
| 119 | path, redundancy); |
| Chaul Ly | a552fe2 | 2024-11-15 10:20:28 +0000 | [diff] [blame] | 120 | #endif |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| Eric Yang | 897f31c | 2025-05-16 20:40:56 +0800 | [diff] [blame] | 123 | try |
| 124 | { |
| 125 | helper->getProperties(service, path, &settings); |
| 126 | failed = helper->thresholdsAsserted(service, path); |
| 127 | } |
| 128 | catch (const std::exception& e) |
| 129 | { |
| 130 | return nullptr; |
| 131 | } |
| 132 | |
| Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 133 | /* if these values are zero, they're ignored. */ |
| 134 | if (info->ignoreDbusMinMax) |
| 135 | { |
| 136 | settings.min = 0; |
| 137 | settings.max = 0; |
| 138 | } |
| 139 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 140 | settings.unavailableAsFailed = info->unavailableAsFailed; |
| 141 | |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 142 | return std::make_unique<DbusPassive>( |
| 143 | bus, type, id, std::move(helper), settings, failed, objectMissing, path, |
| 144 | redundancy); |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 147 | DbusPassive::DbusPassive( |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 148 | sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
| Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 149 | std::unique_ptr<DbusHelperInterface> helper, |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 150 | const SensorProperties& settings, bool failed, bool objectMissing, |
| 151 | const std::string& path, |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 152 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) : |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 153 | ReadInterface(), _signal(bus, getMatch(path), dbusHandleSignal, this), |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 154 | _id(id), _helper(std::move(helper)), _failed(failed), |
| 155 | _objectMissing(objectMissing), path(path), redundancy(redundancy) |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 156 | |
| Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 157 | { |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 158 | _scale = settings.scale; |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 159 | _min = settings.min * std::pow(10.0, _scale); |
| 160 | _max = settings.max * std::pow(10.0, _scale); |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 161 | _available = settings.available; |
| 162 | _unavailableAsFailed = settings.unavailableAsFailed; |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 163 | |
| 164 | // Cache this type knowledge, to avoid repeated string comparison |
| 165 | _typeMargin = (type == "margin"); |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 166 | _typeFan = (type == "fan"); |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 167 | |
| 168 | // Force value to be stored, otherwise member would be uninitialized |
| 169 | updateValue(settings.value, true); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ReadReturn DbusPassive::read(void) |
| 173 | { |
| 174 | std::lock_guard<std::mutex> guard(_lock); |
| 175 | |
| Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 176 | ReadReturn r = {_value, _updated, _unscaled}; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 177 | |
| 178 | return r; |
| 179 | } |
| 180 | |
| Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 181 | void DbusPassive::setValue(double value, double unscaled) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 182 | { |
| 183 | std::lock_guard<std::mutex> guard(_lock); |
| 184 | |
| 185 | _value = value; |
| Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 186 | _unscaled = unscaled; |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 187 | _updated = std::chrono::high_resolution_clock::now(); |
| 188 | } |
| 189 | |
| Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 190 | void DbusPassive::setValue(double value) |
| 191 | { |
| 192 | // First param is scaled, second param is unscaled, assume same here |
| 193 | setValue(value, value); |
| 194 | } |
| 195 | |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 196 | bool DbusPassive::getFailed(void) const |
| 197 | { |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 198 | if (redundancy) |
| 199 | { |
| 200 | const std::set<std::string>& failures = redundancy->getFailed(); |
| 201 | if (failures.find(path) != failures.end()) |
| 202 | { |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 203 | outputFailsafeLogWithSensor(_id, true, _id, |
| 204 | "The sensor path is marked redundant."); |
| James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | } |
| James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 208 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 209 | /* |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 210 | * If handle-missing-object-paths is enabled, and the expected D-Bus object |
| 211 | * path is not exported, this sensor is created to represent that condition. |
| 212 | * Indicate this sensor has failed so the zone enters failSafe mode. |
| 213 | */ |
| 214 | if (_objectMissing) |
| 215 | { |
| 216 | outputFailsafeLogWithSensor(_id, true, _id, |
| 217 | "The sensor D-Bus object is missing."); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | /* |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 222 | * Unavailable thermal sensors, who are not present or |
| 223 | * power-state-not-matching, should not trigger the failSafe mode. For |
| 224 | * example, when a system stays at a powered-off state, its CPU Temp |
| 225 | * sensors will be unavailable, these unavailable sensors should not be |
| 226 | * treated as failed and trigger failSafe. |
| 227 | * This is important for systems whose Fans are always on. |
| 228 | */ |
| 229 | if (!_typeFan && !_available && !_unavailableAsFailed) |
| 230 | { |
| 231 | return false; |
| 232 | } |
| 233 | |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 234 | // If a reading has came in, |
| 235 | // but its value bad in some way (determined by sensor type), |
| 236 | // indicate this sensor has failed, |
| 237 | // until another value comes in that is no longer bad. |
| 238 | // This is different from the overall _failed flag, |
| 239 | // which is set and cleared by other causes. |
| 240 | if (_badReading) |
| 241 | { |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 242 | outputFailsafeLogWithSensor(_id, true, _id, |
| 243 | "The sensor has bad readings."); |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 244 | return true; |
| 245 | } |
| 246 | |
| 247 | // If a reading has came in, and it is not a bad reading, |
| 248 | // but it indicates there is no more thermal margin left, |
| 249 | // that is bad, something is wrong with the PID loops, |
| 250 | // they are not cooling the system, enable failsafe mode also. |
| 251 | if (_marginHot) |
| 252 | { |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 253 | outputFailsafeLogWithSensor(_id, true, _id, |
| 254 | "The sensor has no thermal margin left."); |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
| James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 258 | if (_failed) |
| 259 | { |
| 260 | outputFailsafeLogWithSensor( |
| 261 | _id, true, _id, "The sensor has failed with a critical issue."); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | if (!_available) |
| 266 | { |
| 267 | outputFailsafeLogWithSensor(_id, true, _id, |
| 268 | "The sensor is unavailable."); |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | if (!_functional) |
| 273 | { |
| 274 | outputFailsafeLogWithSensor(_id, true, _id, |
| 275 | "The sensor is not functional."); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | outputFailsafeLogWithSensor(_id, false, _id, "The sensor has recovered."); |
| 280 | |
| 281 | return false; |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| Harvey Wu | a427007 | 2024-05-29 16:11:13 +0800 | [diff] [blame] | 284 | std::string DbusPassive::getFailReason(void) const |
| 285 | { |
| Eric Yang | 3bfece8 | 2025-08-26 10:20:01 +0000 | [diff] [blame] | 286 | if (_objectMissing) |
| 287 | { |
| 288 | return "Sensor D-Bus object missing"; |
| 289 | } |
| Harvey Wu | a427007 | 2024-05-29 16:11:13 +0800 | [diff] [blame] | 290 | if (_badReading) |
| 291 | { |
| 292 | return "Sensor reading bad"; |
| 293 | } |
| 294 | if (_marginHot) |
| 295 | { |
| 296 | return "Margin hot"; |
| 297 | } |
| 298 | if (_failed) |
| 299 | { |
| 300 | return "Sensor threshold asserted"; |
| 301 | } |
| 302 | if (!_available) |
| 303 | { |
| 304 | return "Sensor unavailable"; |
| 305 | } |
| 306 | if (!_functional) |
| 307 | { |
| 308 | return "Sensor not functional"; |
| 309 | } |
| 310 | return "Unknown"; |
| 311 | } |
| 312 | |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 313 | void DbusPassive::setFailed(bool value) |
| 314 | { |
| 315 | _failed = value; |
| 316 | } |
| 317 | |
| James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 318 | void DbusPassive::setFunctional(bool value) |
| 319 | { |
| 320 | _functional = value; |
| 321 | } |
| 322 | |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 323 | void DbusPassive::setAvailable(bool value) |
| 324 | { |
| 325 | _available = value; |
| 326 | } |
| 327 | |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 328 | int64_t DbusPassive::getScale(void) |
| 329 | { |
| 330 | return _scale; |
| 331 | } |
| 332 | |
| Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 333 | std::string DbusPassive::getID(void) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 334 | { |
| 335 | return _id; |
| 336 | } |
| 337 | |
| James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 338 | double DbusPassive::getMax(void) |
| 339 | { |
| 340 | return _max; |
| 341 | } |
| 342 | |
| 343 | double DbusPassive::getMin(void) |
| 344 | { |
| 345 | return _min; |
| 346 | } |
| 347 | |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 348 | void DbusPassive::updateValue(double value, bool force) |
| 349 | { |
| 350 | _badReading = false; |
| 351 | |
| 352 | // Do not let a NAN, or other floating-point oddity, be used to update |
| 353 | // the value, as that indicates the sensor has no valid reading. |
| 354 | if (!(std::isfinite(value))) |
| 355 | { |
| 356 | _badReading = true; |
| 357 | |
| 358 | // Do not continue with a bad reading, unless caller forcing |
| 359 | if (!force) |
| 360 | { |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | value *= std::pow(10.0, _scale); |
| 366 | |
| 367 | auto unscaled = value; |
| 368 | scaleSensorReading(_min, _max, value); |
| 369 | |
| 370 | if (_typeMargin) |
| 371 | { |
| 372 | _marginHot = false; |
| 373 | |
| 374 | // Unlike an absolute temperature sensor, |
| 375 | // where 0 degrees C is a good reading, |
| 376 | // a value received of 0 (or negative) margin is worrisome, |
| 377 | // and should be flagged. |
| 378 | // Either it indicates margin not calculated properly, |
| 379 | // or somebody forgot to set the margin-zero setpoint, |
| 380 | // or the system is really overheating that much. |
| 381 | // This is a different condition from _failed |
| 382 | // and _badReading, so it merits its own flag. |
| 383 | // The sensor has not failed, the reading is good, but the zone |
| 384 | // still needs to know that it should go to failsafe mode. |
| 385 | if (unscaled <= 0.0) |
| 386 | { |
| 387 | _marginHot = true; |
| 388 | } |
| 389 | } |
| 390 | |
| Josh Lehan | b300575 | 2022-02-22 20:48:07 -0800 | [diff] [blame] | 391 | setValue(value, unscaled); |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 394 | int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 395 | { |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 396 | std::string msgSensor; |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 397 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
| Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 398 | |
| 399 | msg.read(msgSensor, msgData); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 400 | |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 401 | if (msgSensor == SensorValue::interface) |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 402 | { |
| Alexander Hansen | dae4a3a | 2025-11-11 17:12:01 +0100 | [diff] [blame] | 403 | auto valPropMap = msgData.find(SensorValue::property_names::value); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 404 | if (valPropMap != msgData.end()) |
| 405 | { |
| Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 406 | double value = |
| 407 | std::visit(VariantToDoubleVisitor(), valPropMap->second); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 408 | |
| Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 409 | owner->updateValue(value, false); |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 412 | else if (msgSensor == SensorThresholdCritical::interface) |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 413 | { |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 414 | auto criticalAlarmLow = msgData.find( |
| 415 | SensorThresholdCritical::property_names::critical_alarm_low); |
| 416 | auto criticalAlarmHigh = msgData.find( |
| 417 | SensorThresholdCritical::property_names::critical_alarm_high); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 418 | if (criticalAlarmHigh == msgData.end() && |
| 419 | criticalAlarmLow == msgData.end()) |
| 420 | { |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | bool asserted = false; |
| 425 | if (criticalAlarmLow != msgData.end()) |
| 426 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 427 | asserted = std::get<bool>(criticalAlarmLow->second); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // checking both as in theory you could de-assert one threshold and |
| 431 | // assert the other at the same moment |
| 432 | if (!asserted && criticalAlarmHigh != msgData.end()) |
| 433 | { |
| James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 434 | asserted = std::get<bool>(criticalAlarmHigh->second); |
| James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 435 | } |
| 436 | owner->setFailed(asserted); |
| 437 | } |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 438 | #ifdef UNC_FAILSAFE |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 439 | else if (msgSensor == SensorThresholdWarning::interface) |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 440 | { |
| Alexander Hansen | d3e71ce | 2025-11-12 16:20:53 +0100 | [diff] [blame] | 441 | auto warningAlarmHigh = msgData.find( |
| 442 | SensorThresholdWarning::property_names::warning_alarm_high); |
| Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 443 | if (warningAlarmHigh == msgData.end()) |
| 444 | { |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | bool asserted = false; |
| 449 | if (warningAlarmHigh != msgData.end()) |
| 450 | { |
| 451 | asserted = std::get<bool>(warningAlarmHigh->second); |
| 452 | } |
| 453 | owner->setFailed(asserted); |
| 454 | } |
| 455 | #endif |
| Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 456 | else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability") |
| 457 | { |
| 458 | auto available = msgData.find("Available"); |
| 459 | if (available == msgData.end()) |
| 460 | { |
| 461 | return 0; |
| 462 | } |
| 463 | bool asserted = std::get<bool>(available->second); |
| 464 | owner->setAvailable(asserted); |
| 465 | if (!asserted) |
| 466 | { |
| 467 | // A thermal controller will continue its PID calculation and not |
| 468 | // trigger a 'failsafe' when some inputs are unavailable. |
| 469 | // So, forced to clear the value here to prevent a historical |
| 470 | // value to participate in a latter PID calculation. |
| 471 | owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true); |
| 472 | } |
| 473 | } |
| James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 474 | else if (msgSensor == |
| 475 | "xyz.openbmc_project.State.Decorator.OperationalStatus") |
| 476 | { |
| 477 | auto functional = msgData.find("Functional"); |
| 478 | if (functional == msgData.end()) |
| 479 | { |
| 480 | return 0; |
| 481 | } |
| 482 | bool asserted = std::get<bool>(functional->second); |
| 483 | owner->setFunctional(asserted); |
| 484 | } |
| Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 488 | |
| Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 489 | int dbusHandleSignal(sd_bus_message* msg, void* usrData, |
| 490 | [[maybe_unused]] sd_bus_error* err) |
| Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 491 | { |
| Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 492 | auto sdbpMsg = sdbusplus::message_t(msg); |
| Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 493 | DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| 494 | |
| Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 495 | return handleSensorValue(sdbpMsg, obj); |
| Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 496 | } |
| Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 497 | |
| 498 | } // namespace pid_control |