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 | */ |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 16 | #include "dbuspassive.hpp" |
| 17 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 18 | #include "dbushelper_interface.hpp" |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 19 | #include "dbuspassiveredundancy.hpp" |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 20 | #include "dbusutil.hpp" |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 21 | #include "util.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 23 | #include <sdbusplus/bus.hpp> |
| 24 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 25 | #include <chrono> |
| 26 | #include <cmath> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 27 | #include <memory> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 28 | #include <mutex> |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 29 | #include <string> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 30 | #include <variant> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 31 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 32 | namespace pid_control |
| 33 | { |
| 34 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 35 | std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 36 | sdbusplus::bus::bus& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 37 | std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 38 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 39 | { |
| 40 | if (helper == nullptr) |
| 41 | { |
| 42 | return nullptr; |
| 43 | } |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 44 | if (!validType(type)) |
Patrick Venture | 0ef1faf | 2018-06-13 12:50:53 -0700 | [diff] [blame] | 45 | { |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 49 | /* Need to get the scale and initial value */ |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 50 | /* service == busname */ |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 51 | std::string path = getSensorPath(type, id); |
Patrick Venture | 34ddc90 | 2018-10-30 11:05:17 -0700 | [diff] [blame] | 52 | |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame^] | 53 | SensorProperties settings; |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 54 | bool failed; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 55 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 56 | try |
| 57 | { |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 58 | std::string service = helper->getService(sensorintf, path); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 59 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 60 | helper->getProperties(service, path, &settings); |
| 61 | failed = helper->thresholdsAsserted(service, path); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 62 | } |
| 63 | catch (const std::exception& e) |
| 64 | { |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
Patrick Venture | 6b9f599 | 2019-09-10 09:18:28 -0700 | [diff] [blame] | 68 | /* if these values are zero, they're ignored. */ |
| 69 | if (info->ignoreDbusMinMax) |
| 70 | { |
| 71 | settings.min = 0; |
| 72 | settings.max = 0; |
| 73 | } |
| 74 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 75 | return std::make_unique<DbusPassive>(bus, type, id, std::move(helper), |
| 76 | settings, failed, path, redundancy); |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 77 | } |
| 78 | |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 79 | DbusPassive::DbusPassive( |
| 80 | sdbusplus::bus::bus& bus, const std::string& type, const std::string& id, |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 81 | std::unique_ptr<DbusHelperInterface> helper, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame^] | 82 | const SensorProperties& settings, bool failed, const std::string& path, |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 83 | const std::shared_ptr<DbusPassiveRedundancy>& redundancy) : |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 84 | ReadInterface(), |
Patrick Venture | d057130 | 2020-08-10 12:42:23 -0700 | [diff] [blame] | 85 | _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this), _id(id), |
| 86 | _helper(std::move(helper)), _failed(failed), path(path), |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 87 | redundancy(redundancy) |
| 88 | |
Patrick Venture | f8cb464 | 2018-10-30 12:02:53 -0700 | [diff] [blame] | 89 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 90 | _scale = settings.scale; |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 91 | _min = settings.min * std::pow(10.0, _scale); |
| 92 | _max = settings.max * std::pow(10.0, _scale); |
| 93 | |
| 94 | // Cache this type knowledge, to avoid repeated string comparison |
| 95 | _typeMargin = (type == "margin"); |
| 96 | |
| 97 | // Force value to be stored, otherwise member would be uninitialized |
| 98 | updateValue(settings.value, true); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | ReadReturn DbusPassive::read(void) |
| 102 | { |
| 103 | std::lock_guard<std::mutex> guard(_lock); |
| 104 | |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame^] | 105 | ReadReturn r = {_value, _updated}; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 106 | |
| 107 | return r; |
| 108 | } |
| 109 | |
| 110 | void DbusPassive::setValue(double value) |
| 111 | { |
| 112 | std::lock_guard<std::mutex> guard(_lock); |
| 113 | |
| 114 | _value = value; |
| 115 | _updated = std::chrono::high_resolution_clock::now(); |
| 116 | } |
| 117 | |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 118 | bool DbusPassive::getFailed(void) const |
| 119 | { |
James Feist | 98b704e | 2019-06-03 16:24:53 -0700 | [diff] [blame] | 120 | if (redundancy) |
| 121 | { |
| 122 | const std::set<std::string>& failures = redundancy->getFailed(); |
| 123 | if (failures.find(path) != failures.end()) |
| 124 | { |
| 125 | return true; |
| 126 | } |
| 127 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 128 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 129 | // If a reading has came in, |
| 130 | // but its value bad in some way (determined by sensor type), |
| 131 | // indicate this sensor has failed, |
| 132 | // until another value comes in that is no longer bad. |
| 133 | // This is different from the overall _failed flag, |
| 134 | // which is set and cleared by other causes. |
| 135 | if (_badReading) |
| 136 | { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // If a reading has came in, and it is not a bad reading, |
| 141 | // but it indicates there is no more thermal margin left, |
| 142 | // that is bad, something is wrong with the PID loops, |
| 143 | // they are not cooling the system, enable failsafe mode also. |
| 144 | if (_marginHot) |
| 145 | { |
| 146 | return true; |
| 147 | } |
| 148 | |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 149 | return _failed || !_functional; |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void DbusPassive::setFailed(bool value) |
| 153 | { |
| 154 | _failed = value; |
| 155 | } |
| 156 | |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 157 | void DbusPassive::setFunctional(bool value) |
| 158 | { |
| 159 | _functional = value; |
| 160 | } |
| 161 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 162 | int64_t DbusPassive::getScale(void) |
| 163 | { |
| 164 | return _scale; |
| 165 | } |
| 166 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 167 | std::string DbusPassive::getID(void) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 168 | { |
| 169 | return _id; |
| 170 | } |
| 171 | |
James Feist | 75eb769 | 2019-02-25 12:50:02 -0800 | [diff] [blame] | 172 | double DbusPassive::getMax(void) |
| 173 | { |
| 174 | return _max; |
| 175 | } |
| 176 | |
| 177 | double DbusPassive::getMin(void) |
| 178 | { |
| 179 | return _min; |
| 180 | } |
| 181 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 182 | void DbusPassive::updateValue(double value, bool force) |
| 183 | { |
| 184 | _badReading = false; |
| 185 | |
| 186 | // Do not let a NAN, or other floating-point oddity, be used to update |
| 187 | // the value, as that indicates the sensor has no valid reading. |
| 188 | if (!(std::isfinite(value))) |
| 189 | { |
| 190 | _badReading = true; |
| 191 | |
| 192 | // Do not continue with a bad reading, unless caller forcing |
| 193 | if (!force) |
| 194 | { |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | value *= std::pow(10.0, _scale); |
| 200 | |
| 201 | auto unscaled = value; |
| 202 | scaleSensorReading(_min, _max, value); |
| 203 | |
| 204 | if (_typeMargin) |
| 205 | { |
| 206 | _marginHot = false; |
| 207 | |
| 208 | // Unlike an absolute temperature sensor, |
| 209 | // where 0 degrees C is a good reading, |
| 210 | // a value received of 0 (or negative) margin is worrisome, |
| 211 | // and should be flagged. |
| 212 | // Either it indicates margin not calculated properly, |
| 213 | // or somebody forgot to set the margin-zero setpoint, |
| 214 | // or the system is really overheating that much. |
| 215 | // This is a different condition from _failed |
| 216 | // and _badReading, so it merits its own flag. |
| 217 | // The sensor has not failed, the reading is good, but the zone |
| 218 | // still needs to know that it should go to failsafe mode. |
| 219 | if (unscaled <= 0.0) |
| 220 | { |
| 221 | _marginHot = true; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | setValue(value); |
| 226 | } |
| 227 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 228 | int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 229 | { |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 230 | std::string msgSensor; |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 231 | std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 232 | |
| 233 | msg.read(msgSensor, msgData); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 234 | |
| 235 | if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| 236 | { |
| 237 | auto valPropMap = msgData.find("Value"); |
| 238 | if (valPropMap != msgData.end()) |
| 239 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 240 | double value = |
| 241 | std::visit(VariantToDoubleVisitor(), valPropMap->second); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 242 | |
Josh Lehan | 3e2f758 | 2020-09-20 22:06:03 -0700 | [diff] [blame] | 243 | owner->updateValue(value, false); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 246 | else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical") |
| 247 | { |
| 248 | auto criticalAlarmLow = msgData.find("CriticalAlarmLow"); |
| 249 | auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh"); |
| 250 | if (criticalAlarmHigh == msgData.end() && |
| 251 | criticalAlarmLow == msgData.end()) |
| 252 | { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | bool asserted = false; |
| 257 | if (criticalAlarmLow != msgData.end()) |
| 258 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 259 | asserted = std::get<bool>(criticalAlarmLow->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // checking both as in theory you could de-assert one threshold and |
| 263 | // assert the other at the same moment |
| 264 | if (!asserted && criticalAlarmHigh != msgData.end()) |
| 265 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 266 | asserted = std::get<bool>(criticalAlarmHigh->second); |
James Feist | 36b7d8e | 2018-10-05 15:39:01 -0700 | [diff] [blame] | 267 | } |
| 268 | owner->setFailed(asserted); |
| 269 | } |
James Feist | 4b36f26 | 2020-07-07 16:56:41 -0700 | [diff] [blame] | 270 | else if (msgSensor == |
| 271 | "xyz.openbmc_project.State.Decorator.OperationalStatus") |
| 272 | { |
| 273 | auto functional = msgData.find("Functional"); |
| 274 | if (functional == msgData.end()) |
| 275 | { |
| 276 | return 0; |
| 277 | } |
| 278 | bool asserted = std::get<bool>(functional->second); |
| 279 | owner->setFunctional(asserted); |
| 280 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 281 | |
| 282 | return 0; |
| 283 | } |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 284 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 285 | int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err) |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 286 | { |
| 287 | auto sdbpMsg = sdbusplus::message::message(msg); |
| 288 | DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| 289 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 290 | return handleSensorValue(sdbpMsg, obj); |
Patrick Venture | d0c7566 | 2018-06-12 19:03:21 -0700 | [diff] [blame] | 291 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 292 | |
| 293 | } // namespace pid_control |