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