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