| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "ExitAirTempSensor.hpp" |
| 18 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "SensorPaths.hpp" |
| 20 | #include "Thresholds.hpp" |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 21 | #include "Utils.hpp" |
| 22 | #include "VariantVisitors.hpp" |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 23 | #include "sensor.hpp" |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 24 | |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 25 | #include <boost/algorithm/string/replace.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 26 | #include <boost/asio/error.hpp> |
| 27 | #include <boost/asio/io_context.hpp> |
| 28 | #include <boost/asio/post.hpp> |
| 29 | #include <boost/asio/steady_timer.hpp> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <boost/container/flat_map.hpp> |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 31 | #include <phosphor-logging/lg2.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 32 | #include <sdbusplus/asio/connection.hpp> |
| 33 | #include <sdbusplus/asio/object_server.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 34 | #include <sdbusplus/bus.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 35 | #include <sdbusplus/bus/match.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 36 | #include <sdbusplus/message.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 37 | |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 38 | #include <algorithm> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 39 | #include <array> |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 40 | #include <chrono> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 41 | #include <cmath> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 42 | #include <cstddef> |
| 43 | #include <cstdint> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 44 | #include <functional> |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 45 | #include <limits> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 46 | #include <memory> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 47 | #include <stdexcept> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 48 | #include <string> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 49 | #include <utility> |
| 50 | #include <variant> |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 51 | #include <vector> |
| 52 | |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 53 | constexpr const double altitudeFactor = 1.14; |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 54 | constexpr const char* exitAirType = "ExitAirTempSensor"; |
| 55 | constexpr const char* cfmType = "CFMSensor"; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 56 | |
| 57 | // todo: this *might* need to be configurable |
| 58 | constexpr const char* inletTemperatureSensor = "temperature/Front_Panel_Temp"; |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 59 | constexpr const char* pidConfigurationType = |
| 60 | "xyz.openbmc_project.Configuration.Pid"; |
| 61 | constexpr const char* settingsDaemon = "xyz.openbmc_project.Settings"; |
| 62 | constexpr const char* cfmSettingPath = "/xyz/openbmc_project/control/cfm_limit"; |
| 63 | constexpr const char* cfmSettingIface = "xyz.openbmc_project.Control.CFMLimit"; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 64 | |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 65 | static constexpr double cfmMaxReading = 255; |
| 66 | static constexpr double cfmMinReading = 0; |
| 67 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 68 | static constexpr size_t minSystemCfm = 50; |
| 69 | |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 70 | constexpr const auto monitorTypes{ |
| 71 | std::to_array<const char*>({exitAirType, cfmType})}; |
| James Feist | 655f376 | 2020-10-05 15:28:15 -0700 | [diff] [blame] | 72 | |
| James Feist | 9a25ed4 | 2019-10-15 15:43:44 -0700 | [diff] [blame] | 73 | static std::vector<std::shared_ptr<CFMSensor>> cfmSensors; |
| 74 | |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 75 | static void setupSensorMatch( |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 76 | std::vector<sdbusplus::bus::match_t>& matches, sdbusplus::bus_t& connection, |
| 77 | const std::string& type, |
| 78 | std::function<void(const double&, sdbusplus::message_t&)>&& callback) |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 79 | { |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 80 | std::function<void(sdbusplus::message_t & message)> eventHandler = |
| 81 | [callback{std::move(callback)}](sdbusplus::message_t& message) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 82 | std::string objectName; |
| 83 | boost::container::flat_map<std::string, |
| 84 | std::variant<double, int64_t>> |
| 85 | values; |
| 86 | message.read(objectName, values); |
| 87 | auto findValue = values.find("Value"); |
| 88 | if (findValue == values.end()) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | double value = |
| 93 | std::visit(VariantToDoubleVisitor(), findValue->second); |
| 94 | if (std::isnan(value)) |
| 95 | { |
| 96 | return; |
| 97 | } |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 98 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 99 | callback(value, message); |
| 100 | }; |
| 101 | matches.emplace_back( |
| 102 | connection, |
| 103 | "type='signal'," |
| 104 | "member='PropertiesChanged',interface='org." |
| 105 | "freedesktop.DBus.Properties',path_" |
| 106 | "namespace='/xyz/openbmc_project/sensors/" + |
| 107 | std::string(type) + "',arg0='xyz.openbmc_project.Sensor.Value'", |
| 108 | std::move(eventHandler)); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 111 | static void setMaxPWM(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 112 | double value) |
| 113 | { |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 114 | conn->async_method_call( |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 115 | [conn, |
| 116 | value](const boost::system::error_code ec, const GetSubTreeType& ret) { |
| 117 | if (ec) |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 118 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 119 | lg2::error("Error calling mapper"); |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 120 | return; |
| 121 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 122 | for (const auto& [path, objDict] : ret) |
| 123 | { |
| 124 | if (objDict.empty()) |
| 125 | { |
| 126 | return; |
| 127 | } |
| 128 | const std::string& owner = objDict.begin()->first; |
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 129 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 130 | conn->async_method_call( |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 131 | [conn, value, owner, |
| 132 | path{path}](const boost::system::error_code ec, |
| 133 | const std::variant<std::string>& classType) { |
| 134 | if (ec) |
| 135 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 136 | lg2::error("Error getting pid class"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | const auto* classStr = |
| 140 | std::get_if<std::string>(&classType); |
| 141 | if (classStr == nullptr || *classStr != "fan") |
| 142 | { |
| 143 | return; |
| 144 | } |
| 145 | conn->async_method_call( |
| 146 | [](boost::system::error_code& ec) { |
| 147 | if (ec) |
| 148 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 149 | lg2::error("Error setting pid class"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | }, |
| 153 | owner, path, "org.freedesktop.DBus.Properties", |
| 154 | "Set", pidConfigurationType, "OutLimitMax", |
| 155 | std::variant<double>(value)); |
| 156 | }, |
| 157 | owner, path, "org.freedesktop.DBus.Properties", "Get", |
| 158 | pidConfigurationType, "Class"); |
| 159 | } |
| 160 | }, |
| James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 161 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/", |
| 162 | 0, std::array<std::string, 1>{pidConfigurationType}); |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 165 | CFMSensor::CFMSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 166 | const std::string& sensorName, |
| 167 | const std::string& sensorConfiguration, |
| 168 | sdbusplus::asio::object_server& objectServer, |
| James Feist | b839c05 | 2019-05-15 10:25:24 -0700 | [diff] [blame] | 169 | std::vector<thresholds::Threshold>&& thresholdData, |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 170 | std::shared_ptr<ExitAirTempSensor>& parent) : |
| Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 171 | Sensor(escapeName(sensorName), std::move(thresholdData), |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 172 | sensorConfiguration, "CFMSensor", false, false, cfmMaxReading, |
| 173 | cfmMinReading, conn, PowerState::on), |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 174 | parent(parent), objServer(objectServer) |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 175 | { |
| Basheer Ahmed Muddebihal | e5b867b | 2021-07-26 08:32:19 -0700 | [diff] [blame] | 176 | sensorInterface = objectServer.add_interface( |
| 177 | "/xyz/openbmc_project/sensors/airflow/" + name, |
| 178 | "xyz.openbmc_project.Sensor.Value"); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 179 | |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 180 | for (const auto& threshold : thresholds) |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 181 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 182 | std::string interface = thresholds::getInterface(threshold.level); |
| 183 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 184 | objectServer.add_interface( |
| 185 | "/xyz/openbmc_project/sensors/airflow/" + name, interface); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 186 | } |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 187 | |
| 188 | association = objectServer.add_interface( |
| Basheer Ahmed Muddebihal | e5b867b | 2021-07-26 08:32:19 -0700 | [diff] [blame] | 189 | "/xyz/openbmc_project/sensors/airflow/" + name, association::interface); |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 190 | |
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 191 | setInitialProperties(sensor_paths::unitCFM); |
| James Feist | 9a25ed4 | 2019-10-15 15:43:44 -0700 | [diff] [blame] | 192 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 193 | pwmLimitIface = |
| 194 | objectServer.add_interface("/xyz/openbmc_project/control/pwm_limit", |
| 195 | "xyz.openbmc_project.Control.PWMLimit"); |
| 196 | cfmLimitIface = |
| 197 | objectServer.add_interface("/xyz/openbmc_project/control/MaxCFM", |
| 198 | "xyz.openbmc_project.Control.CFMLimit"); |
| James Feist | 9a25ed4 | 2019-10-15 15:43:44 -0700 | [diff] [blame] | 199 | } |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 200 | |
| James Feist | 9a25ed4 | 2019-10-15 15:43:44 -0700 | [diff] [blame] | 201 | void CFMSensor::setupMatches() |
| 202 | { |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 203 | std::weak_ptr<CFMSensor> weakRef = weak_from_this(); |
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 204 | setupSensorMatch( |
| 205 | matches, *dbusConnection, "fan_tach", |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 206 | [weakRef](const double& value, sdbusplus::message_t& message) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 207 | auto self = weakRef.lock(); |
| 208 | if (!self) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | self->tachReadings[message.get_path()] = value; |
| 213 | if (self->tachRanges.find(message.get_path()) == |
| 214 | self->tachRanges.end()) |
| 215 | { |
| 216 | // calls update reading after updating ranges |
| 217 | self->addTachRanges(message.get_sender(), message.get_path()); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | self->updateReading(); |
| 222 | } |
| 223 | }); |
| James Feist | 9a25ed4 | 2019-10-15 15:43:44 -0700 | [diff] [blame] | 224 | |
| 225 | dbusConnection->async_method_call( |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 226 | [weakRef](const boost::system::error_code ec, |
| 227 | const std::variant<double> cfmVariant) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 228 | auto self = weakRef.lock(); |
| 229 | if (!self) |
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 230 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 231 | return; |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 232 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 233 | |
| 234 | uint64_t maxRpm = 100; |
| 235 | if (!ec) |
| 236 | { |
| 237 | const auto* cfm = std::get_if<double>(&cfmVariant); |
| 238 | if (cfm != nullptr && *cfm >= minSystemCfm) |
| 239 | { |
| 240 | maxRpm = self->getMaxRpm(*cfm); |
| 241 | } |
| 242 | } |
| 243 | self->pwmLimitIface->register_property("Limit", maxRpm); |
| 244 | self->pwmLimitIface->initialize(); |
| 245 | setMaxPWM(self->dbusConnection, maxRpm); |
| 246 | }, |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 247 | settingsDaemon, cfmSettingPath, "org.freedesktop.DBus.Properties", |
| 248 | "Get", cfmSettingIface, "Limit"); |
| 249 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 250 | matches.emplace_back( |
| 251 | *dbusConnection, |
| 252 | "type='signal'," |
| 253 | "member='PropertiesChanged',interface='org." |
| 254 | "freedesktop.DBus.Properties',path='" + |
| 255 | std::string(cfmSettingPath) + "',arg0='" + |
| 256 | std::string(cfmSettingIface) + "'", |
| 257 | [weakRef](sdbusplus::message_t& message) { |
| 258 | auto self = weakRef.lock(); |
| 259 | if (!self) |
| 260 | { |
| 261 | return; |
| 262 | } |
| 263 | boost::container::flat_map<std::string, std::variant<double>> |
| 264 | values; |
| 265 | std::string objectName; |
| 266 | message.read(objectName, values); |
| 267 | const auto findValue = values.find("Limit"); |
| 268 | if (findValue == values.end()) |
| 269 | { |
| 270 | return; |
| 271 | } |
| 272 | auto* const reading = std::get_if<double>(&(findValue->second)); |
| 273 | if (reading == nullptr) |
| 274 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 275 | lg2::error("Got CFM Limit of wrong type"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | if (*reading < minSystemCfm && *reading != 0) |
| 279 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 280 | lg2::error("Illegal CFM setting detected"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 281 | return; |
| 282 | } |
| 283 | uint64_t maxRpm = self->getMaxRpm(*reading); |
| 284 | self->pwmLimitIface->set_property("Limit", maxRpm); |
| 285 | setMaxPWM(self->dbusConnection, maxRpm); |
| 286 | }); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 289 | CFMSensor::~CFMSensor() |
| 290 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 291 | for (const auto& iface : thresholdInterfaces) |
| 292 | { |
| 293 | objServer.remove_interface(iface); |
| 294 | } |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 295 | objServer.remove_interface(sensorInterface); |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 296 | objServer.remove_interface(association); |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 297 | objServer.remove_interface(cfmLimitIface); |
| 298 | objServer.remove_interface(pwmLimitIface); |
| 299 | } |
| 300 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 301 | void CFMSensor::createMaxCFMIface() |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 302 | { |
| James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 303 | cfmLimitIface->register_property("Limit", c2 * maxCFM * tachs.size()); |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 304 | cfmLimitIface->initialize(); |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 307 | void CFMSensor::addTachRanges(const std::string& serviceName, |
| 308 | const std::string& path) |
| 309 | { |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 310 | std::weak_ptr<CFMSensor> weakRef = weak_from_this(); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 311 | dbusConnection->async_method_call( |
| Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 312 | [weakRef, path](const boost::system::error_code ec, |
| 313 | const SensorBaseConfigMap& data) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 314 | if (ec) |
| 315 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 316 | lg2::error("Error getting properties from '{PATH}'", "PATH", |
| 317 | path); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 318 | return; |
| 319 | } |
| 320 | auto self = weakRef.lock(); |
| 321 | if (!self) |
| 322 | { |
| 323 | return; |
| 324 | } |
| 325 | double max = loadVariant<double>(data, "MaxValue"); |
| 326 | double min = loadVariant<double>(data, "MinValue"); |
| 327 | self->tachRanges[path] = std::make_pair(min, max); |
| 328 | self->updateReading(); |
| 329 | }, |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 330 | serviceName, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 331 | "xyz.openbmc_project.Sensor.Value"); |
| 332 | } |
| 333 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 334 | void CFMSensor::checkThresholds() |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 335 | { |
| 336 | thresholds::checkThresholds(this); |
| 337 | } |
| 338 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 339 | void CFMSensor::updateReading() |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 340 | { |
| 341 | double val = 0.0; |
| 342 | if (calculate(val)) |
| 343 | { |
| 344 | if (value != val && parent) |
| 345 | { |
| 346 | parent->updateReading(); |
| 347 | } |
| 348 | updateValue(val); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 353 | } |
| 354 | } |
| 355 | |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 356 | uint64_t CFMSensor::getMaxRpm(uint64_t cfmMaxSetting) const |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 357 | { |
| 358 | uint64_t pwmPercent = 100; |
| 359 | double totalCFM = std::numeric_limits<double>::max(); |
| 360 | if (cfmMaxSetting == 0) |
| 361 | { |
| 362 | return pwmPercent; |
| 363 | } |
| 364 | |
| James Feist | 5242795 | 2019-04-05 14:23:35 -0700 | [diff] [blame] | 365 | bool firstLoop = true; |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 366 | while (totalCFM > cfmMaxSetting) |
| 367 | { |
| James Feist | 5242795 | 2019-04-05 14:23:35 -0700 | [diff] [blame] | 368 | if (firstLoop) |
| 369 | { |
| 370 | firstLoop = false; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | pwmPercent--; |
| 375 | } |
| 376 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 377 | double ci = 0; |
| 378 | if (pwmPercent == 0) |
| 379 | { |
| 380 | ci = 0; |
| 381 | } |
| 382 | else if (pwmPercent < tachMinPercent) |
| 383 | { |
| 384 | ci = c1; |
| 385 | } |
| 386 | else if (pwmPercent > tachMaxPercent) |
| 387 | { |
| 388 | ci = c2; |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | ci = c1 + (((c2 - c1) * (pwmPercent - tachMinPercent)) / |
| 393 | (tachMaxPercent - tachMinPercent)); |
| 394 | } |
| 395 | |
| 396 | // Now calculate the CFM for this tach |
| 397 | // CFMi = Ci * Qmaxi * TACHi |
| 398 | totalCFM = ci * maxCFM * pwmPercent; |
| 399 | totalCFM *= tachs.size(); |
| 400 | // divide by 100 since pwm is in percent |
| 401 | totalCFM /= 100; |
| 402 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 403 | if (pwmPercent <= 0) |
| 404 | { |
| 405 | break; |
| 406 | } |
| 407 | } |
| James Feist | 5242795 | 2019-04-05 14:23:35 -0700 | [diff] [blame] | 408 | |
| James Feist | 1345209 | 2019-03-07 16:38:12 -0800 | [diff] [blame] | 409 | return pwmPercent; |
| 410 | } |
| 411 | |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 412 | bool CFMSensor::calculate(double& value) |
| 413 | { |
| 414 | double totalCFM = 0; |
| 415 | for (const std::string& tachName : tachs) |
| 416 | { |
| 417 | auto findReading = std::find_if( |
| Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 418 | tachReadings.begin(), tachReadings.end(), |
| 419 | [&](const auto& item) { return item.first.ends_with(tachName); }); |
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 420 | auto findRange = std::find_if( |
| 421 | tachRanges.begin(), tachRanges.end(), |
| 422 | [&](const auto& item) { return item.first.ends_with(tachName); }); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 423 | if (findReading == tachReadings.end()) |
| 424 | { |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 425 | lg2::debug("Can't find '{NAME}' in readings", "NAME", tachName); |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 426 | continue; // haven't gotten a reading |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (findRange == tachRanges.end()) |
| 430 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 431 | lg2::error("Can't find '{NAME}' in ranges", "NAME", tachName); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 432 | return false; // haven't gotten a max / min |
| 433 | } |
| 434 | |
| 435 | // avoid divide by 0 |
| 436 | if (findRange->second.second == 0) |
| 437 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 438 | lg2::error("Tach Max Set to 0, tachName: '{NAME}'", "NAME", |
| 439 | tachName); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 440 | return false; |
| 441 | } |
| 442 | |
| 443 | double rpm = findReading->second; |
| 444 | |
| 445 | // for now assume the min for a fan is always 0, divide by max to get |
| 446 | // percent and mult by 100 |
| 447 | rpm /= findRange->second.second; |
| 448 | rpm *= 100; |
| 449 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 450 | lg2::debug("Tach '{NAME}' at '{RPM}'", "NAME", tachName, "RPM", rpm); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 451 | |
| 452 | // Do a linear interpolation to get Ci |
| 453 | // Ci = C1 + (C2 - C1)/(RPM2 - RPM1) * (TACHi - TACH1) |
| 454 | |
| 455 | double ci = 0; |
| 456 | if (rpm == 0) |
| 457 | { |
| 458 | ci = 0; |
| 459 | } |
| 460 | else if (rpm < tachMinPercent) |
| 461 | { |
| 462 | ci = c1; |
| 463 | } |
| 464 | else if (rpm > tachMaxPercent) |
| 465 | { |
| 466 | ci = c2; |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | ci = c1 + (((c2 - c1) * (rpm - tachMinPercent)) / |
| 471 | (tachMaxPercent - tachMinPercent)); |
| 472 | } |
| 473 | |
| 474 | // Now calculate the CFM for this tach |
| 475 | // CFMi = Ci * Qmaxi * TACHi |
| 476 | totalCFM += ci * maxCFM * rpm; |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 477 | lg2::debug( |
| 478 | "totalCFM = {CFM}, Ci = {CI}, MaxCFM = {MAXCFM}, rpm = {RPM}, c1 = {C1}" |
| 479 | ", c2 = {C2}, max = {MAX}, min = {MIN}", |
| 480 | "CFM", totalCFM, "CI", ci, "MAXCFM", maxCFM, "RPM", rpm, "C1", c1, |
| 481 | "C2", c2, "MAX", tachMaxPercent, "MIN", tachMinPercent); |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // divide by 100 since rpm is in percent |
| 485 | value = totalCFM / 100; |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 486 | lg2::debug("cfm value = {VALUE}", "VALUE", value); |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 487 | return true; |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static constexpr double exitAirMaxReading = 127; |
| 491 | static constexpr double exitAirMinReading = -128; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 492 | ExitAirTempSensor::ExitAirTempSensor( |
| 493 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 494 | const std::string& sensorName, const std::string& sensorConfiguration, |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 495 | sdbusplus::asio::object_server& objectServer, |
| James Feist | b839c05 | 2019-05-15 10:25:24 -0700 | [diff] [blame] | 496 | std::vector<thresholds::Threshold>&& thresholdData) : |
| Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 497 | Sensor(escapeName(sensorName), std::move(thresholdData), |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 498 | sensorConfiguration, "ExitAirTemp", false, false, exitAirMaxReading, |
| 499 | exitAirMinReading, conn, PowerState::on), |
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 500 | objServer(objectServer) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 501 | { |
| 502 | sensorInterface = objectServer.add_interface( |
| 503 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 504 | "xyz.openbmc_project.Sensor.Value"); |
| 505 | |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 506 | for (const auto& threshold : thresholds) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 507 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 508 | std::string interface = thresholds::getInterface(threshold.level); |
| 509 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 510 | objectServer.add_interface( |
| 511 | "/xyz/openbmc_project/sensors/temperature/" + name, interface); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 512 | } |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 513 | association = objectServer.add_interface( |
| 514 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 515 | association::interface); |
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 516 | setInitialProperties(sensor_paths::unitDegreesC); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | ExitAirTempSensor::~ExitAirTempSensor() |
| 520 | { |
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 521 | for (const auto& iface : thresholdInterfaces) |
| 522 | { |
| 523 | objServer.remove_interface(iface); |
| 524 | } |
| James Feist | 523828e | 2019-03-04 14:38:37 -0800 | [diff] [blame] | 525 | objServer.remove_interface(sensorInterface); |
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 526 | objServer.remove_interface(association); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 529 | void ExitAirTempSensor::setupMatches() |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 530 | { |
| Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 531 | constexpr const auto matchTypes{ |
| 532 | std::to_array<const char*>({"power", inletTemperatureSensor})}; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 533 | |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 534 | std::weak_ptr<ExitAirTempSensor> weakRef = weak_from_this(); |
| Ed Tanous | 13b63f8 | 2021-05-11 16:12:52 -0700 | [diff] [blame] | 535 | for (const std::string type : matchTypes) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 536 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 537 | setupSensorMatch( |
| 538 | matches, *dbusConnection, type, |
| 539 | [weakRef, |
| 540 | type](const double& value, sdbusplus::message_t& message) { |
| 541 | auto self = weakRef.lock(); |
| 542 | if (!self) |
| 543 | { |
| 544 | return; |
| 545 | } |
| 546 | if (type == "power") |
| 547 | { |
| 548 | std::string path = message.get_path(); |
| 549 | if (path.find("PS") != std::string::npos && |
| 550 | path.ends_with("Input_Power")) |
| 551 | { |
| 552 | self->powerReadings[message.get_path()] = value; |
| 553 | } |
| 554 | } |
| 555 | else if (type == inletTemperatureSensor) |
| 556 | { |
| 557 | self->inletTemp = value; |
| 558 | } |
| 559 | self->updateReading(); |
| 560 | }); |
| 561 | } |
| 562 | dbusConnection->async_method_call( |
| 563 | [weakRef](boost::system::error_code ec, |
| 564 | const std::variant<double>& value) { |
| 565 | if (ec) |
| 566 | { |
| 567 | // sensor not ready yet |
| 568 | return; |
| 569 | } |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 570 | auto self = weakRef.lock(); |
| 571 | if (!self) |
| 572 | { |
| 573 | return; |
| 574 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 575 | self->inletTemp = std::visit(VariantToDoubleVisitor(), value); |
| 576 | }, |
| James Feist | 9566bfa | 2019-01-29 15:31:23 -0800 | [diff] [blame] | 577 | "xyz.openbmc_project.HwmonTempSensor", |
| 578 | std::string("/xyz/openbmc_project/sensors/") + inletTemperatureSensor, |
| James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 579 | properties::interface, properties::get, sensorValueInterface, "Value"); |
| 580 | dbusConnection->async_method_call( |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 581 | [weakRef](boost::system::error_code ec, const GetSubTreeType& subtree) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 582 | if (ec) |
| James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 583 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 584 | lg2::error("Error contacting mapper"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 585 | return; |
| James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 586 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 587 | auto self = weakRef.lock(); |
| 588 | if (!self) |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 589 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 590 | return; |
| Zhikui Ren | dbb73aa | 2021-04-02 13:39:04 -0700 | [diff] [blame] | 591 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 592 | for (const auto& [path, matches] : subtree) |
| 593 | { |
| 594 | size_t lastSlash = path.rfind('/'); |
| 595 | if (lastSlash == std::string::npos || |
| 596 | lastSlash == path.size() || matches.empty()) |
| 597 | { |
| 598 | continue; |
| 599 | } |
| 600 | std::string sensorName = path.substr(lastSlash + 1); |
| 601 | if (sensorName.starts_with("PS") && |
| 602 | sensorName.ends_with("Input_Power")) |
| 603 | { |
| 604 | // lambda capture requires a proper variable (not a |
| 605 | // structured binding) |
| 606 | const std::string& cbPath = path; |
| 607 | self->dbusConnection->async_method_call( |
| 608 | [weakRef, cbPath](boost::system::error_code ec, |
| 609 | const std::variant<double>& value) { |
| 610 | if (ec) |
| 611 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 612 | lg2::error("Error getting value from '{PATH}'", |
| 613 | "PATH", cbPath); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 614 | } |
| 615 | auto self = weakRef.lock(); |
| 616 | if (!self) |
| 617 | { |
| 618 | return; |
| 619 | } |
| 620 | double reading = |
| 621 | std::visit(VariantToDoubleVisitor(), value); |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 622 | lg2::debug("'{PATH}' reading '{VALUE}'", "PATH", |
| 623 | cbPath, "VALUE", reading); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 624 | self->powerReadings[cbPath] = reading; |
| 625 | }, |
| 626 | matches[0].first, cbPath, properties::interface, |
| 627 | properties::get, sensorValueInterface, "Value"); |
| 628 | } |
| 629 | } |
| 630 | }, |
| James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 631 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, |
| 632 | "/xyz/openbmc_project/sensors/power", 0, |
| 633 | std::array<const char*, 1>{sensorValueInterface}); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 636 | void ExitAirTempSensor::updateReading() |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 637 | { |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 638 | double val = 0.0; |
| 639 | if (calculate(val)) |
| 640 | { |
| James Feist | 18af423 | 2019-03-13 11:14:00 -0700 | [diff] [blame] | 641 | val = std::floor(val + 0.5); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 642 | updateValue(val); |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 647 | } |
| 648 | } |
| 649 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 650 | double ExitAirTempSensor::getTotalCFM() |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 651 | { |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 652 | double sum = 0; |
| 653 | for (auto& sensor : cfmSensors) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 654 | { |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 655 | double reading = 0; |
| 656 | if (!sensor->calculate(reading)) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 657 | { |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 658 | return -1; |
| 659 | } |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 660 | sum += reading; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 661 | } |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 662 | |
| 663 | return sum; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | bool ExitAirTempSensor::calculate(double& val) |
| 667 | { |
| Zhikui Ren | 12e3d67 | 2020-12-03 15:14:49 -0800 | [diff] [blame] | 668 | constexpr size_t maxErrorPrint = 5; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 669 | static bool firstRead = false; |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 670 | static size_t errorPrint = maxErrorPrint; |
| 671 | |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 672 | double cfm = getTotalCFM(); |
| 673 | if (cfm <= 0) |
| 674 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 675 | lg2::error("Error getting cfm"); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
| Zhikui Ren | 12e3d67 | 2020-12-03 15:14:49 -0800 | [diff] [blame] | 679 | // Though cfm is not expected to be less than qMin normally, |
| 680 | // it is not a hard limit for exit air temp calculation. |
| 681 | // 50% qMin is chosen as a generic limit between providing |
| 682 | // a valid derived exit air temp and reporting exit air temp not available. |
| 683 | constexpr const double cfmLimitFactor = 0.5; |
| 684 | if (cfm < (qMin * cfmLimitFactor)) |
| 685 | { |
| 686 | if (errorPrint > 0) |
| 687 | { |
| 688 | errorPrint--; |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 689 | lg2::error("cfm '{CFM}' is too low, expected qMin '{QMIN}'", "CFM", |
| 690 | cfm, "QMIN", qMin); |
| Zhikui Ren | 12e3d67 | 2020-12-03 15:14:49 -0800 | [diff] [blame] | 691 | } |
| 692 | val = 0; |
| 693 | return false; |
| 694 | } |
| 695 | |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 696 | // if there is an error getting inlet temp, return error |
| 697 | if (std::isnan(inletTemp)) |
| 698 | { |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 699 | if (errorPrint > 0) |
| 700 | { |
| 701 | errorPrint--; |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 702 | lg2::error("Cannot get inlet temp"); |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 703 | } |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 704 | val = 0; |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | // if fans are off, just make the exit temp equal to inlet |
| James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 709 | if (!isPowerOn()) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 710 | { |
| 711 | val = inletTemp; |
| 712 | return true; |
| 713 | } |
| 714 | |
| 715 | double totalPower = 0; |
| Zev Weiss | 72f322f | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 716 | for (const auto& [path, reading] : powerReadings) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 717 | { |
| Zev Weiss | 72f322f | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 718 | if (std::isnan(reading)) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 719 | { |
| 720 | continue; |
| 721 | } |
| Zev Weiss | 72f322f | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 722 | totalPower += reading; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Calculate power correction factor |
| 726 | // Ci = CL + (CH - CL)/(QMax - QMin) * (CFM - QMin) |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 727 | double powerFactor = 0.0; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 728 | if (cfm <= qMin) |
| 729 | { |
| 730 | powerFactor = powerFactorMin; |
| 731 | } |
| 732 | else if (cfm >= qMax) |
| 733 | { |
| 734 | powerFactor = powerFactorMax; |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | powerFactor = powerFactorMin + ((powerFactorMax - powerFactorMin) / |
| 739 | (qMax - qMin) * (cfm - qMin)); |
| 740 | } |
| 741 | |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 742 | totalPower *= powerFactor; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 743 | totalPower += pOffset; |
| 744 | |
| 745 | if (totalPower == 0) |
| 746 | { |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 747 | if (errorPrint > 0) |
| 748 | { |
| 749 | errorPrint--; |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 750 | lg2::error("total power 0"); |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 751 | } |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 752 | val = 0; |
| 753 | return false; |
| 754 | } |
| 755 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 756 | lg2::debug( |
| 757 | "Power Factor: {POWER_FACTOR}, Inlet Temp: {INLET_TEMP}, Total Power: {TOTAL_POWER}", |
| 758 | "POWER_FACTOR", powerFactor, "INLET_TEMP", inletTemp, "TOTAL_POWER", |
| 759 | totalPower); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 760 | |
| 761 | // Calculate the exit air temp |
| 762 | // Texit = Tfp + (1.76 * TotalPower / CFM * Faltitude) |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 763 | double reading = 1.76 * totalPower * altitudeFactor; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 764 | reading /= cfm; |
| 765 | reading += inletTemp; |
| 766 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 767 | lg2::debug("Reading 1: '{VALUE}'", "VALUE", reading); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 768 | |
| 769 | // Now perform the exponential average |
| 770 | // Calculate alpha based on SDR values and CFM |
| 771 | // Ai = As + (Af - As)/(QMax - QMin) * (CFM - QMin) |
| 772 | |
| 773 | double alpha = 0.0; |
| 774 | if (cfm < qMin) |
| 775 | { |
| 776 | alpha = alphaS; |
| 777 | } |
| 778 | else if (cfm >= qMax) |
| 779 | { |
| 780 | alpha = alphaF; |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | alpha = alphaS + ((alphaF - alphaS) * (cfm - qMin) / (qMax - qMin)); |
| 785 | } |
| 786 | |
| Zhikui Ren | 12e3d67 | 2020-12-03 15:14:49 -0800 | [diff] [blame] | 787 | auto time = std::chrono::steady_clock::now(); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 788 | if (!firstRead) |
| 789 | { |
| 790 | firstRead = true; |
| 791 | lastTime = time; |
| 792 | lastReading = reading; |
| 793 | } |
| 794 | double alphaDT = |
| 795 | std::chrono::duration_cast<std::chrono::seconds>(time - lastTime) |
| 796 | .count() * |
| 797 | alpha; |
| 798 | |
| 799 | // cap at 1.0 or the below fails |
| 800 | if (alphaDT > 1.0) |
| 801 | { |
| 802 | alphaDT = 1.0; |
| 803 | } |
| 804 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 805 | lg2::debug("AlphaDT: '{ADT}'", "ADT", alphaDT); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 806 | |
| 807 | reading = ((reading * alphaDT) + (lastReading * (1.0 - alphaDT))); |
| 808 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame] | 809 | lg2::debug("Reading 2: '{VALUE}'", "VALUE", reading); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 810 | |
| 811 | val = reading; |
| 812 | lastReading = reading; |
| 813 | lastTime = time; |
| James Feist | ae11cfc | 2019-05-07 15:01:20 -0700 | [diff] [blame] | 814 | errorPrint = maxErrorPrint; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 815 | return true; |
| 816 | } |
| 817 | |
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 818 | void ExitAirTempSensor::checkThresholds() |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 819 | { |
| 820 | thresholds::checkThresholds(this); |
| 821 | } |
| 822 | |
| Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 823 | static void loadVariantPathArray(const SensorBaseConfigMap& data, |
| 824 | const std::string& key, |
| 825 | std::vector<std::string>& resp) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 826 | { |
| 827 | auto it = data.find(key); |
| 828 | if (it == data.end()) |
| 829 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 830 | lg2::error("Configuration missing '{KEY}'", "KEY", key); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 831 | throw std::invalid_argument("Key Missing"); |
| 832 | } |
| 833 | BasicVariantType copy = it->second; |
| James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 834 | std::vector<std::string> config = std::get<std::vector<std::string>>(copy); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 835 | for (auto& str : config) |
| 836 | { |
| 837 | boost::replace_all(str, " ", "_"); |
| 838 | } |
| 839 | resp = std::move(config); |
| 840 | } |
| 841 | |
| 842 | void createSensor(sdbusplus::asio::object_server& objectServer, |
| James Feist | b2eb3f5 | 2018-12-04 16:17:50 -0800 | [diff] [blame] | 843 | std::shared_ptr<ExitAirTempSensor>& exitAirSensor, |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 844 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 845 | { |
| 846 | if (!dbusConnection) |
| 847 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 848 | lg2::error("Connection not created"); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 849 | return; |
| 850 | } |
| James Feist | 655f376 | 2020-10-05 15:28:15 -0700 | [diff] [blame] | 851 | auto getter = std::make_shared<GetSensorConfiguration>( |
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 852 | dbusConnection, [&objectServer, &dbusConnection, |
| 853 | &exitAirSensor](const ManagedObjectType& resp) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 854 | cfmSensors.clear(); |
| 855 | for (const auto& [path, interfaces] : resp) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 856 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 857 | for (const auto& [intf, cfg] : interfaces) |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 858 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 859 | if (intf == configInterfaceName(exitAirType)) |
| 860 | { |
| 861 | // thresholds should be under the same path |
| 862 | std::vector<thresholds::Threshold> sensorThresholds; |
| 863 | parseThresholdsFromConfig(interfaces, sensorThresholds); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 864 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 865 | std::string name = |
| 866 | loadVariant<std::string>(cfg, "Name"); |
| 867 | exitAirSensor = nullptr; |
| 868 | exitAirSensor = std::make_shared<ExitAirTempSensor>( |
| 869 | dbusConnection, name, path.str, objectServer, |
| 870 | std::move(sensorThresholds)); |
| 871 | exitAirSensor->powerFactorMin = |
| 872 | loadVariant<double>(cfg, "PowerFactorMin"); |
| 873 | exitAirSensor->powerFactorMax = |
| 874 | loadVariant<double>(cfg, "PowerFactorMax"); |
| 875 | exitAirSensor->qMin = loadVariant<double>(cfg, "QMin"); |
| 876 | exitAirSensor->qMax = loadVariant<double>(cfg, "QMax"); |
| 877 | exitAirSensor->alphaS = |
| 878 | loadVariant<double>(cfg, "AlphaS"); |
| 879 | exitAirSensor->alphaF = |
| 880 | loadVariant<double>(cfg, "AlphaF"); |
| 881 | } |
| 882 | else if (intf == configInterfaceName(cfmType)) |
| 883 | { |
| 884 | // thresholds should be under the same path |
| 885 | std::vector<thresholds::Threshold> sensorThresholds; |
| 886 | parseThresholdsFromConfig(interfaces, sensorThresholds); |
| 887 | std::string name = |
| 888 | loadVariant<std::string>(cfg, "Name"); |
| 889 | auto sensor = std::make_shared<CFMSensor>( |
| 890 | dbusConnection, name, path.str, objectServer, |
| 891 | std::move(sensorThresholds), exitAirSensor); |
| 892 | loadVariantPathArray(cfg, "Tachs", sensor->tachs); |
| 893 | sensor->maxCFM = loadVariant<double>(cfg, "MaxCFM"); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 894 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 895 | // change these into percent upon getting the data |
| 896 | sensor->c1 = loadVariant<double>(cfg, "C1") / 100; |
| 897 | sensor->c2 = loadVariant<double>(cfg, "C2") / 100; |
| 898 | sensor->tachMinPercent = |
| 899 | loadVariant<double>(cfg, "TachMinPercent"); |
| 900 | sensor->tachMaxPercent = |
| 901 | loadVariant<double>(cfg, "TachMaxPercent"); |
| 902 | sensor->createMaxCFMIface(); |
| 903 | sensor->setupMatches(); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 904 | |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 905 | cfmSensors.emplace_back(std::move(sensor)); |
| 906 | } |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 909 | if (exitAirSensor) |
| 910 | { |
| 911 | exitAirSensor->setupMatches(); |
| 912 | exitAirSensor->updateReading(); |
| 913 | } |
| 914 | }); |
| James Feist | 655f376 | 2020-10-05 15:28:15 -0700 | [diff] [blame] | 915 | getter->getConfiguration( |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 916 | std::vector<std::string>(monitorTypes.begin(), monitorTypes.end())); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 919 | int main() |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 920 | { |
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 921 | boost::asio::io_context io; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 922 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| Johnathan Mantey | 661d437 | 2022-10-27 09:00:59 -0700 | [diff] [blame] | 923 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 924 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 925 | systemBus->request_name("xyz.openbmc_project.ExitAirTempSensor"); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 926 | std::shared_ptr<ExitAirTempSensor> sensor = |
| 927 | nullptr; // wait until we find the config |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 928 | |
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 929 | boost::asio::post(io, |
| 930 | [&]() { createSensor(objectServer, sensor, systemBus); }); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 931 | |
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 932 | boost::asio::steady_timer configTimer(io); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 933 | |
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 934 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 935 | [&](sdbusplus::message_t&) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 936 | configTimer.expires_after(std::chrono::seconds(1)); |
| 937 | // create a timer because normally multiple properties change |
| 938 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 939 | if (ec == boost::asio::error::operation_aborted) |
| 940 | { |
| 941 | return; // we're being canceled |
| 942 | } |
| 943 | createSensor(objectServer, sensor, systemBus); |
| 944 | if (!sensor) |
| 945 | { |
| George Liu | ce1abb6 | 2025-02-20 10:44:47 +0800 | [diff] [blame] | 946 | lg2::info("Configuration not detected"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 947 | } |
| 948 | }); |
| 949 | }; |
| Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 950 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 951 | setupPropertiesChangedMatches(*systemBus, monitorTypes, eventHandler); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 952 | |
| Bruce Lee | 913d4d0 | 2021-07-22 10:18:42 +0800 | [diff] [blame] | 953 | setupManufacturingModeMatch(*systemBus); |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 954 | io.run(); |
| Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 955 | return 0; |
| James Feist | bc896df | 2018-11-26 16:28:17 -0800 | [diff] [blame] | 956 | } |