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