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