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