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