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