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