| /** |
| * Copyright 2017 Google Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| #include "dbuspassive.hpp" |
| |
| #include "dbushelper_interface.hpp" |
| #include "dbuspassiveredundancy.hpp" |
| #include "dbusutil.hpp" |
| #include "util.hpp" |
| |
| #include <sdbusplus/bus.hpp> |
| |
| #include <chrono> |
| #include <cmath> |
| #include <memory> |
| #include <mutex> |
| #include <string> |
| #include <variant> |
| |
| namespace pid_control |
| { |
| |
| std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive( |
| sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
| std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info, |
| const std::shared_ptr<DbusPassiveRedundancy>& redundancy) |
| { |
| if (helper == nullptr) |
| { |
| return nullptr; |
| } |
| if (!validType(type)) |
| { |
| return nullptr; |
| } |
| |
| /* Need to get the scale and initial value */ |
| /* service == busname */ |
| std::string path; |
| if (info->readPath.empty()) |
| { |
| path = getSensorPath(type, id); |
| } |
| else |
| { |
| path = info->readPath; |
| } |
| |
| SensorProperties settings; |
| bool failed; |
| |
| try |
| { |
| std::string service = helper->getService(sensorintf, path); |
| |
| helper->getProperties(service, path, &settings); |
| failed = helper->thresholdsAsserted(service, path); |
| } |
| catch (const std::exception& e) |
| { |
| return nullptr; |
| } |
| |
| /* if these values are zero, they're ignored. */ |
| if (info->ignoreDbusMinMax) |
| { |
| settings.min = 0; |
| settings.max = 0; |
| } |
| |
| settings.unavailableAsFailed = info->unavailableAsFailed; |
| |
| return std::make_unique<DbusPassive>(bus, type, id, std::move(helper), |
| settings, failed, path, redundancy); |
| } |
| |
| DbusPassive::DbusPassive( |
| sdbusplus::bus_t& bus, const std::string& type, const std::string& id, |
| std::unique_ptr<DbusHelperInterface> helper, |
| const SensorProperties& settings, bool failed, const std::string& path, |
| const std::shared_ptr<DbusPassiveRedundancy>& redundancy) : |
| ReadInterface(), |
| _signal(bus, getMatch(path).c_str(), dbusHandleSignal, this), _id(id), |
| _helper(std::move(helper)), _failed(failed), path(path), |
| redundancy(redundancy) |
| |
| { |
| _scale = settings.scale; |
| _min = settings.min * std::pow(10.0, _scale); |
| _max = settings.max * std::pow(10.0, _scale); |
| _available = settings.available; |
| _unavailableAsFailed = settings.unavailableAsFailed; |
| |
| // Cache this type knowledge, to avoid repeated string comparison |
| _typeMargin = (type == "margin"); |
| _typeFan = (type == "fan"); |
| |
| // Force value to be stored, otherwise member would be uninitialized |
| updateValue(settings.value, true); |
| } |
| |
| ReadReturn DbusPassive::read(void) |
| { |
| std::lock_guard<std::mutex> guard(_lock); |
| |
| ReadReturn r = {_value, _updated, _unscaled}; |
| |
| return r; |
| } |
| |
| void DbusPassive::setValue(double value, double unscaled) |
| { |
| std::lock_guard<std::mutex> guard(_lock); |
| |
| _value = value; |
| _unscaled = unscaled; |
| _updated = std::chrono::high_resolution_clock::now(); |
| } |
| |
| void DbusPassive::setValue(double value) |
| { |
| // First param is scaled, second param is unscaled, assume same here |
| setValue(value, value); |
| } |
| |
| bool DbusPassive::getFailed(void) const |
| { |
| if (redundancy) |
| { |
| const std::set<std::string>& failures = redundancy->getFailed(); |
| if (failures.find(path) != failures.end()) |
| { |
| return true; |
| } |
| } |
| |
| /* |
| * Unavailable thermal sensors, who are not present or |
| * power-state-not-matching, should not trigger the failSafe mode. For |
| * example, when a system stays at a powered-off state, its CPU Temp |
| * sensors will be unavailable, these unavailable sensors should not be |
| * treated as failed and trigger failSafe. |
| * This is important for systems whose Fans are always on. |
| */ |
| if (!_typeFan && !_available && !_unavailableAsFailed) |
| { |
| return false; |
| } |
| |
| // If a reading has came in, |
| // but its value bad in some way (determined by sensor type), |
| // indicate this sensor has failed, |
| // until another value comes in that is no longer bad. |
| // This is different from the overall _failed flag, |
| // which is set and cleared by other causes. |
| if (_badReading) |
| { |
| return true; |
| } |
| |
| // If a reading has came in, and it is not a bad reading, |
| // but it indicates there is no more thermal margin left, |
| // that is bad, something is wrong with the PID loops, |
| // they are not cooling the system, enable failsafe mode also. |
| if (_marginHot) |
| { |
| return true; |
| } |
| |
| return _failed || !_available || !_functional; |
| } |
| |
| void DbusPassive::setFailed(bool value) |
| { |
| _failed = value; |
| } |
| |
| void DbusPassive::setFunctional(bool value) |
| { |
| _functional = value; |
| } |
| |
| void DbusPassive::setAvailable(bool value) |
| { |
| _available = value; |
| } |
| |
| int64_t DbusPassive::getScale(void) |
| { |
| return _scale; |
| } |
| |
| std::string DbusPassive::getID(void) |
| { |
| return _id; |
| } |
| |
| double DbusPassive::getMax(void) |
| { |
| return _max; |
| } |
| |
| double DbusPassive::getMin(void) |
| { |
| return _min; |
| } |
| |
| void DbusPassive::updateValue(double value, bool force) |
| { |
| _badReading = false; |
| |
| // Do not let a NAN, or other floating-point oddity, be used to update |
| // the value, as that indicates the sensor has no valid reading. |
| if (!(std::isfinite(value))) |
| { |
| _badReading = true; |
| |
| // Do not continue with a bad reading, unless caller forcing |
| if (!force) |
| { |
| return; |
| } |
| } |
| |
| value *= std::pow(10.0, _scale); |
| |
| auto unscaled = value; |
| scaleSensorReading(_min, _max, value); |
| |
| if (_typeMargin) |
| { |
| _marginHot = false; |
| |
| // Unlike an absolute temperature sensor, |
| // where 0 degrees C is a good reading, |
| // a value received of 0 (or negative) margin is worrisome, |
| // and should be flagged. |
| // Either it indicates margin not calculated properly, |
| // or somebody forgot to set the margin-zero setpoint, |
| // or the system is really overheating that much. |
| // This is a different condition from _failed |
| // and _badReading, so it merits its own flag. |
| // The sensor has not failed, the reading is good, but the zone |
| // still needs to know that it should go to failsafe mode. |
| if (unscaled <= 0.0) |
| { |
| _marginHot = true; |
| } |
| } |
| |
| setValue(value, unscaled); |
| } |
| |
| int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner) |
| { |
| std::string msgSensor; |
| std::map<std::string, std::variant<int64_t, double, bool>> msgData; |
| |
| msg.read(msgSensor, msgData); |
| |
| if (msgSensor == "xyz.openbmc_project.Sensor.Value") |
| { |
| auto valPropMap = msgData.find("Value"); |
| if (valPropMap != msgData.end()) |
| { |
| double value = std::visit(VariantToDoubleVisitor(), |
| valPropMap->second); |
| |
| owner->updateValue(value, false); |
| } |
| } |
| else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical") |
| { |
| auto criticalAlarmLow = msgData.find("CriticalAlarmLow"); |
| auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh"); |
| if (criticalAlarmHigh == msgData.end() && |
| criticalAlarmLow == msgData.end()) |
| { |
| return 0; |
| } |
| |
| bool asserted = false; |
| if (criticalAlarmLow != msgData.end()) |
| { |
| asserted = std::get<bool>(criticalAlarmLow->second); |
| } |
| |
| // checking both as in theory you could de-assert one threshold and |
| // assert the other at the same moment |
| if (!asserted && criticalAlarmHigh != msgData.end()) |
| { |
| asserted = std::get<bool>(criticalAlarmHigh->second); |
| } |
| owner->setFailed(asserted); |
| } |
| else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability") |
| { |
| auto available = msgData.find("Available"); |
| if (available == msgData.end()) |
| { |
| return 0; |
| } |
| bool asserted = std::get<bool>(available->second); |
| owner->setAvailable(asserted); |
| if (!asserted) |
| { |
| // A thermal controller will continue its PID calculation and not |
| // trigger a 'failsafe' when some inputs are unavailable. |
| // So, forced to clear the value here to prevent a historical |
| // value to participate in a latter PID calculation. |
| owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true); |
| } |
| } |
| else if (msgSensor == |
| "xyz.openbmc_project.State.Decorator.OperationalStatus") |
| { |
| auto functional = msgData.find("Functional"); |
| if (functional == msgData.end()) |
| { |
| return 0; |
| } |
| bool asserted = std::get<bool>(functional->second); |
| owner->setFunctional(asserted); |
| } |
| |
| return 0; |
| } |
| |
| int dbusHandleSignal(sd_bus_message* msg, void* usrData, |
| [[maybe_unused]] sd_bus_error* err) |
| { |
| auto sdbpMsg = sdbusplus::message_t(msg); |
| DbusPassive* obj = static_cast<DbusPassive*>(usrData); |
| |
| return handleSensorValue(sdbpMsg, obj); |
| } |
| |
| } // namespace pid_control |