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