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