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