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