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