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