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