blob: 3c1d7e4a29678db01b65392bd2c1c283e82a39ca [file] [log] [blame]
James Feistbc896df2018-11-26 16:28:17 -08001/*
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 Tanous8a57ec02020-10-09 12:46:52 -070017#include <ExitAirTempSensor.hpp>
18#include <Utils.hpp>
19#include <VariantVisitors.hpp>
James Feistbc896df2018-11-26 16:28:17 -080020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
James Feistbc896df2018-11-26 16:28:17 -080028#include <chrono>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <cmath>
30#include <functional>
James Feistbc896df2018-11-26 16:28:17 -080031#include <iostream>
32#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <memory>
James Feistbc896df2018-11-26 16:28:17 -080034#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
36#include <utility>
37#include <variant>
James Feistbc896df2018-11-26 16:28:17 -080038#include <vector>
39
Ed Tanous8a57ec02020-10-09 12:46:52 -070040constexpr const double altitudeFactor = 1.14;
James Feistbc896df2018-11-26 16:28:17 -080041constexpr const char* exitAirIface =
42 "xyz.openbmc_project.Configuration.ExitAirTempSensor";
43constexpr const char* cfmIface = "xyz.openbmc_project.Configuration.CFMSensor";
44
45// todo: this *might* need to be configurable
46constexpr const char* inletTemperatureSensor = "temperature/Front_Panel_Temp";
James Feist13452092019-03-07 16:38:12 -080047constexpr const char* pidConfigurationType =
48 "xyz.openbmc_project.Configuration.Pid";
49constexpr const char* settingsDaemon = "xyz.openbmc_project.Settings";
50constexpr const char* cfmSettingPath = "/xyz/openbmc_project/control/cfm_limit";
51constexpr const char* cfmSettingIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistbc896df2018-11-26 16:28:17 -080052
Ed Tanous8a57ec02020-10-09 12:46:52 -070053static constexpr bool debug = false;
James Feistbc896df2018-11-26 16:28:17 -080054
James Feistb2eb3f52018-12-04 16:17:50 -080055static constexpr double cfmMaxReading = 255;
56static constexpr double cfmMinReading = 0;
57
James Feist13452092019-03-07 16:38:12 -080058static constexpr size_t minSystemCfm = 50;
59
Brandon Kim66558232021-11-09 16:53:08 -080060constexpr const auto monitorIfaces{
61 std::to_array<const char*>({exitAirIface, cfmIface})};
James Feist655f3762020-10-05 15:28:15 -070062
James Feist9a25ed42019-10-15 15:43:44 -070063static std::vector<std::shared_ptr<CFMSensor>> cfmSensors;
64
James Feistb2eb3f52018-12-04 16:17:50 -080065static void setupSensorMatch(
Patrick Williams92f8f512022-07-22 19:26:55 -050066 std::vector<sdbusplus::bus::match_t>& matches, sdbusplus::bus_t& connection,
67 const std::string& type,
68 std::function<void(const double&, sdbusplus::message_t&)>&& callback)
James Feistb2eb3f52018-12-04 16:17:50 -080069{
70
Patrick Williams92f8f512022-07-22 19:26:55 -050071 std::function<void(sdbusplus::message_t & message)> eventHandler =
72 [callback{std::move(callback)}](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -070073 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 Feist9566bfa2019-01-29 15:31:23 -080087
Ed Tanousbb679322022-05-16 16:10:00 -070088 callback(value, message);
89 };
James Feistb2eb3f52018-12-04 16:17:50 -080090 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 Feist13452092019-03-07 16:38:12 -0800100static 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 Tanousbb679322022-05-16 16:10:00 -0700110 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 Feist13452092019-03-07 16:38:12 -0800118 {
James Feist13452092019-03-07 16:38:12 -0800119 return;
120 }
Ed Tanousbb679322022-05-16 16:10:00 -0700121 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 Tanous2049bd22022-07-09 07:20:26 -0700132 const auto* classStr = std::get_if<std::string>(&classType);
Ed Tanousbb679322022-05-16 16:10:00 -0700133 if (classStr == nullptr || *classStr != "fan")
James Feist13452092019-03-07 16:38:12 -0800134 {
135 return;
136 }
James Feist13452092019-03-07 16:38:12 -0800137 conn->async_method_call(
Ed Tanousbb679322022-05-16 16:10:00 -0700138 [](boost::system::error_code& ec) {
139 if (ec)
140 {
141 std::cerr << "Error setting pid class\n";
142 return;
143 }
James Feist13452092019-03-07 16:38:12 -0800144 },
Ed Tanousbb679322022-05-16 16:10:00 -0700145 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 Feist13452092019-03-07 16:38:12 -0800152 },
James Feista5e58722019-04-22 14:43:11 -0700153 mapper::busName, mapper::path, mapper::interface, mapper::subtree, "/",
154 0, std::array<std::string, 1>{pidConfigurationType});
James Feist13452092019-03-07 16:38:12 -0800155}
156
James Feistb2eb3f52018-12-04 16:17:50 -0800157CFMSensor::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 Feistb839c052019-05-15 10:25:24 -0700161 std::vector<thresholds::Threshold>&& thresholdData,
James Feistb2eb3f52018-12-04 16:17:50 -0800162 std::shared_ptr<ExitAirTempSensor>& parent) :
Zhikui Renda98f092021-11-01 09:41:08 -0700163 Sensor(escapeName(sensorName), std::move(thresholdData),
164 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
165 false, false, cfmMaxReading, cfmMinReading, conn, PowerState::on),
Ed Tanous2049bd22022-07-09 07:20:26 -0700166 parent(parent), objServer(objectServer)
James Feistb2eb3f52018-12-04 16:17:50 -0800167{
Basheer Ahmed Muddebihale5b867b2021-07-26 08:32:19 -0700168 sensorInterface = objectServer.add_interface(
169 "/xyz/openbmc_project/sensors/airflow/" + name,
170 "xyz.openbmc_project.Sensor.Value");
James Feistb2eb3f52018-12-04 16:17:50 -0800171
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530172 for (const auto& threshold : thresholds)
James Feistb2eb3f52018-12-04 16:17:50 -0800173 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530174 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 Feistb2eb3f52018-12-04 16:17:50 -0800178 }
James Feist078f2322019-03-08 11:09:05 -0800179
180 association = objectServer.add_interface(
Basheer Ahmed Muddebihale5b867b2021-07-26 08:32:19 -0700181 "/xyz/openbmc_project/sensors/airflow/" + name, association::interface);
James Feist078f2322019-03-08 11:09:05 -0800182
Andrei Kartashev39287412022-02-04 16:04:47 +0300183 setInitialProperties(sensor_paths::unitCFM);
James Feist9a25ed42019-10-15 15:43:44 -0700184
James Feist13452092019-03-07 16:38:12 -0800185 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 Feist9a25ed42019-10-15 15:43:44 -0700191}
James Feist13452092019-03-07 16:38:12 -0800192
James Feist9a25ed42019-10-15 15:43:44 -0700193void CFMSensor::setupMatches()
194{
195
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700196 std::weak_ptr<CFMSensor> weakRef = weak_from_this();
Ed Tanous8a17c302021-09-02 15:07:11 -0700197 setupSensorMatch(
198 matches, *dbusConnection, "fan_tach",
Patrick Williams92f8f512022-07-22 19:26:55 -0500199 [weakRef](const double& value, sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700200 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 Tanous8a17c302021-09-02 15:07:11 -0700215 });
James Feist9a25ed42019-10-15 15:43:44 -0700216
217 dbusConnection->async_method_call(
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700218 [weakRef](const boost::system::error_code ec,
219 const std::variant<double> cfmVariant) {
Ed Tanousbb679322022-05-16 16:10:00 -0700220 auto self = weakRef.lock();
221 if (!self)
222 {
223 return;
224 }
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700225
Ed Tanousbb679322022-05-16 16:10:00 -0700226 uint64_t maxRpm = 100;
227 if (!ec)
228 {
James Feist13452092019-03-07 16:38:12 -0800229
Ed Tanous2049bd22022-07-09 07:20:26 -0700230 const auto* cfm = std::get_if<double>(&cfmVariant);
Ed Tanousbb679322022-05-16 16:10:00 -0700231 if (cfm != nullptr && *cfm >= minSystemCfm)
232 {
233 maxRpm = self->getMaxRpm(*cfm);
James Feist13452092019-03-07 16:38:12 -0800234 }
Ed Tanousbb679322022-05-16 16:10:00 -0700235 }
236 self->pwmLimitIface->register_property("Limit", maxRpm);
237 self->pwmLimitIface->initialize();
238 setMaxPWM(self->dbusConnection, maxRpm);
James Feist13452092019-03-07 16:38:12 -0800239 },
240 settingsDaemon, cfmSettingPath, "org.freedesktop.DBus.Properties",
241 "Get", cfmSettingIface, "Limit");
242
Ed Tanousbb679322022-05-16 16:10:00 -0700243 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) + "'",
Patrick Williams92f8f512022-07-22 19:26:55 -0500249 [weakRef](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700250 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 Tanous2049bd22022-07-09 07:20:26 -0700263 auto* const reading = std::get_if<double>(&(findValue->second));
Ed Tanousbb679322022-05-16 16:10:00 -0700264 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 Feistb2eb3f52018-12-04 16:17:50 -0800278}
279
James Feist9566bfa2019-01-29 15:31:23 -0800280CFMSensor::~CFMSensor()
281{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530282 for (const auto& iface : thresholdInterfaces)
283 {
284 objServer.remove_interface(iface);
285 }
James Feist9566bfa2019-01-29 15:31:23 -0800286 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800287 objServer.remove_interface(association);
James Feist13452092019-03-07 16:38:12 -0800288 objServer.remove_interface(cfmLimitIface);
289 objServer.remove_interface(pwmLimitIface);
290}
291
292void CFMSensor::createMaxCFMIface(void)
293{
James Feistb6c0b912019-07-09 12:21:44 -0700294 cfmLimitIface->register_property("Limit", c2 * maxCFM * tachs.size());
James Feist13452092019-03-07 16:38:12 -0800295 cfmLimitIface->initialize();
James Feist9566bfa2019-01-29 15:31:23 -0800296}
297
James Feistb2eb3f52018-12-04 16:17:50 -0800298void CFMSensor::addTachRanges(const std::string& serviceName,
299 const std::string& path)
300{
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700301 std::weak_ptr<CFMSensor> weakRef = weak_from_this();
James Feistb2eb3f52018-12-04 16:17:50 -0800302 dbusConnection->async_method_call(
Zev Weissafd15042022-07-18 12:28:40 -0700303 [weakRef, path](const boost::system::error_code ec,
304 const SensorBaseConfigMap& data) {
Ed Tanousbb679322022-05-16 16:10:00 -0700305 if (ec)
306 {
307 std::cerr << "Error getting properties from " << path << "\n";
308 return;
309 }
310 auto self = weakRef.lock();
311 if (!self)
312 {
313 return;
314 }
315 double max = loadVariant<double>(data, "MaxValue");
316 double min = loadVariant<double>(data, "MinValue");
317 self->tachRanges[path] = std::make_pair(min, max);
318 self->updateReading();
James Feistb2eb3f52018-12-04 16:17:50 -0800319 },
320 serviceName, path, "org.freedesktop.DBus.Properties", "GetAll",
321 "xyz.openbmc_project.Sensor.Value");
322}
323
324void CFMSensor::checkThresholds(void)
325{
326 thresholds::checkThresholds(this);
327}
328
329void CFMSensor::updateReading(void)
330{
331 double val = 0.0;
332 if (calculate(val))
333 {
334 if (value != val && parent)
335 {
336 parent->updateReading();
337 }
338 updateValue(val);
339 }
340 else
341 {
342 updateValue(std::numeric_limits<double>::quiet_NaN());
343 }
344}
345
Ed Tanous2049bd22022-07-09 07:20:26 -0700346uint64_t CFMSensor::getMaxRpm(uint64_t cfmMaxSetting) const
James Feist13452092019-03-07 16:38:12 -0800347{
348 uint64_t pwmPercent = 100;
349 double totalCFM = std::numeric_limits<double>::max();
350 if (cfmMaxSetting == 0)
351 {
352 return pwmPercent;
353 }
354
James Feist52427952019-04-05 14:23:35 -0700355 bool firstLoop = true;
James Feist13452092019-03-07 16:38:12 -0800356 while (totalCFM > cfmMaxSetting)
357 {
James Feist52427952019-04-05 14:23:35 -0700358 if (firstLoop)
359 {
360 firstLoop = false;
361 }
362 else
363 {
364 pwmPercent--;
365 }
366
James Feist13452092019-03-07 16:38:12 -0800367 double ci = 0;
368 if (pwmPercent == 0)
369 {
370 ci = 0;
371 }
372 else if (pwmPercent < tachMinPercent)
373 {
374 ci = c1;
375 }
376 else if (pwmPercent > tachMaxPercent)
377 {
378 ci = c2;
379 }
380 else
381 {
382 ci = c1 + (((c2 - c1) * (pwmPercent - tachMinPercent)) /
383 (tachMaxPercent - tachMinPercent));
384 }
385
386 // Now calculate the CFM for this tach
387 // CFMi = Ci * Qmaxi * TACHi
388 totalCFM = ci * maxCFM * pwmPercent;
389 totalCFM *= tachs.size();
390 // divide by 100 since pwm is in percent
391 totalCFM /= 100;
392
James Feist13452092019-03-07 16:38:12 -0800393 if (pwmPercent <= 0)
394 {
395 break;
396 }
397 }
James Feist52427952019-04-05 14:23:35 -0700398
James Feist13452092019-03-07 16:38:12 -0800399 return pwmPercent;
400}
401
James Feistb2eb3f52018-12-04 16:17:50 -0800402bool CFMSensor::calculate(double& value)
403{
404 double totalCFM = 0;
405 for (const std::string& tachName : tachs)
406 {
James Feist9566bfa2019-01-29 15:31:23 -0800407
James Feistb2eb3f52018-12-04 16:17:50 -0800408 auto findReading = std::find_if(
409 tachReadings.begin(), tachReadings.end(), [&](const auto& item) {
410 return boost::ends_with(item.first, tachName);
411 });
Ed Tanousbb679322022-05-16 16:10:00 -0700412 auto findRange = std::find_if(tachRanges.begin(), tachRanges.end(),
413 [&](const auto& item) {
414 return boost::ends_with(item.first, tachName);
415 });
James Feistb2eb3f52018-12-04 16:17:50 -0800416 if (findReading == tachReadings.end())
417 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700418 if constexpr (debug)
James Feista96329f2019-01-24 10:08:27 -0800419 {
420 std::cerr << "Can't find " << tachName << "in readings\n";
421 }
James Feist9566bfa2019-01-29 15:31:23 -0800422 continue; // haven't gotten a reading
James Feistb2eb3f52018-12-04 16:17:50 -0800423 }
424
425 if (findRange == tachRanges.end())
426 {
James Feist523828e2019-03-04 14:38:37 -0800427 std::cerr << "Can't find " << tachName << " in ranges\n";
James Feistb2eb3f52018-12-04 16:17:50 -0800428 return false; // haven't gotten a max / min
429 }
430
431 // avoid divide by 0
432 if (findRange->second.second == 0)
433 {
434 std::cerr << "Tach Max Set to 0 " << tachName << "\n";
435 return false;
436 }
437
438 double rpm = findReading->second;
439
440 // for now assume the min for a fan is always 0, divide by max to get
441 // percent and mult by 100
442 rpm /= findRange->second.second;
443 rpm *= 100;
444
Ed Tanous8a57ec02020-10-09 12:46:52 -0700445 if constexpr (debug)
James Feistb2eb3f52018-12-04 16:17:50 -0800446 {
447 std::cout << "Tach " << tachName << "at " << rpm << "\n";
448 }
449
450 // Do a linear interpolation to get Ci
451 // Ci = C1 + (C2 - C1)/(RPM2 - RPM1) * (TACHi - TACH1)
452
453 double ci = 0;
454 if (rpm == 0)
455 {
456 ci = 0;
457 }
458 else if (rpm < tachMinPercent)
459 {
460 ci = c1;
461 }
462 else if (rpm > tachMaxPercent)
463 {
464 ci = c2;
465 }
466 else
467 {
468 ci = c1 + (((c2 - c1) * (rpm - tachMinPercent)) /
469 (tachMaxPercent - tachMinPercent));
470 }
471
472 // Now calculate the CFM for this tach
473 // CFMi = Ci * Qmaxi * TACHi
474 totalCFM += ci * maxCFM * rpm;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700475 if constexpr (debug)
James Feista5e58722019-04-22 14:43:11 -0700476 {
477 std::cerr << "totalCFM = " << totalCFM << "\n";
478 std::cerr << "Ci " << ci << " MaxCFM " << maxCFM << " rpm " << rpm
479 << "\n";
480 std::cerr << "c1 " << c1 << " c2 " << c2 << " max "
481 << tachMaxPercent << " min " << tachMinPercent << "\n";
482 }
James Feistb2eb3f52018-12-04 16:17:50 -0800483 }
484
485 // divide by 100 since rpm is in percent
486 value = totalCFM / 100;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700487 if constexpr (debug)
James Feista5e58722019-04-22 14:43:11 -0700488 {
489 std::cerr << "cfm value = " << value << "\n";
490 }
James Feist9566bfa2019-01-29 15:31:23 -0800491 return true;
James Feistb2eb3f52018-12-04 16:17:50 -0800492}
493
494static constexpr double exitAirMaxReading = 127;
495static constexpr double exitAirMinReading = -128;
James Feistbc896df2018-11-26 16:28:17 -0800496ExitAirTempSensor::ExitAirTempSensor(
497 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feistb2eb3f52018-12-04 16:17:50 -0800498 const std::string& sensorName, const std::string& sensorConfiguration,
James Feistbc896df2018-11-26 16:28:17 -0800499 sdbusplus::asio::object_server& objectServer,
James Feistb839c052019-05-15 10:25:24 -0700500 std::vector<thresholds::Threshold>&& thresholdData) :
Zhikui Renda98f092021-11-01 09:41:08 -0700501 Sensor(escapeName(sensorName), std::move(thresholdData),
502 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
503 false, false, exitAirMaxReading, exitAirMinReading, conn,
504 PowerState::on),
Ed Tanous2049bd22022-07-09 07:20:26 -0700505 objServer(objectServer)
James Feistbc896df2018-11-26 16:28:17 -0800506{
507 sensorInterface = objectServer.add_interface(
508 "/xyz/openbmc_project/sensors/temperature/" + name,
509 "xyz.openbmc_project.Sensor.Value");
510
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530511 for (const auto& threshold : thresholds)
James Feistbc896df2018-11-26 16:28:17 -0800512 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530513 std::string interface = thresholds::getInterface(threshold.level);
514 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
515 objectServer.add_interface(
516 "/xyz/openbmc_project/sensors/temperature/" + name, interface);
James Feistbc896df2018-11-26 16:28:17 -0800517 }
James Feist078f2322019-03-08 11:09:05 -0800518 association = objectServer.add_interface(
519 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -0700520 association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +0300521 setInitialProperties(sensor_paths::unitDegreesC);
James Feistbc896df2018-11-26 16:28:17 -0800522}
523
524ExitAirTempSensor::~ExitAirTempSensor()
525{
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530526 for (const auto& iface : thresholdInterfaces)
527 {
528 objServer.remove_interface(iface);
529 }
James Feist523828e2019-03-04 14:38:37 -0800530 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800531 objServer.remove_interface(association);
James Feistbc896df2018-11-26 16:28:17 -0800532}
533
534void ExitAirTempSensor::setupMatches(void)
535{
Brandon Kim66558232021-11-09 16:53:08 -0800536 constexpr const auto matchTypes{
537 std::to_array<const char*>({"power", inletTemperatureSensor})};
James Feistbc896df2018-11-26 16:28:17 -0800538
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700539 std::weak_ptr<ExitAirTempSensor> weakRef = weak_from_this();
Ed Tanous13b63f82021-05-11 16:12:52 -0700540 for (const std::string type : matchTypes)
James Feistbc896df2018-11-26 16:28:17 -0800541 {
James Feistb2eb3f52018-12-04 16:17:50 -0800542 setupSensorMatch(matches, *dbusConnection, type,
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700543 [weakRef, type](const double& value,
Patrick Williams92f8f512022-07-22 19:26:55 -0500544 sdbusplus::message_t& message) {
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700545 auto self = weakRef.lock();
546 if (!self)
547 {
548 return;
549 }
Ed Tanousbb679322022-05-16 16:10:00 -0700550 if (type == "power")
551 {
552 std::string path = message.get_path();
553 if (path.find("PS") != std::string::npos &&
554 boost::ends_with(path, "Input_Power"))
555 {
556 self->powerReadings[message.get_path()] = value;
557 }
558 }
559 else if (type == inletTemperatureSensor)
560 {
561 self->inletTemp = value;
562 }
563 self->updateReading();
564 });
565 }
566 dbusConnection->async_method_call(
567 [weakRef](boost::system::error_code ec,
568 const std::variant<double>& value) {
569 if (ec)
570 {
571 // sensor not ready yet
572 return;
573 }
574 auto self = weakRef.lock();
575 if (!self)
576 {
577 return;
578 }
579 self->inletTemp = std::visit(VariantToDoubleVisitor(), value);
James Feist9566bfa2019-01-29 15:31:23 -0800580 },
581 "xyz.openbmc_project.HwmonTempSensor",
582 std::string("/xyz/openbmc_project/sensors/") + inletTemperatureSensor,
James Feista5e58722019-04-22 14:43:11 -0700583 properties::interface, properties::get, sensorValueInterface, "Value");
584 dbusConnection->async_method_call(
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700585 [weakRef](boost::system::error_code ec, const GetSubTreeType& subtree) {
Ed Tanousbb679322022-05-16 16:10:00 -0700586 if (ec)
587 {
588 std::cerr << "Error contacting mapper\n";
589 return;
590 }
591 auto self = weakRef.lock();
592 if (!self)
593 {
594 return;
595 }
Zev Weiss72f322f2022-08-12 18:21:01 -0700596 for (const auto& [path, matches] : subtree)
Ed Tanousbb679322022-05-16 16:10:00 -0700597 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700598 size_t lastSlash = path.rfind('/');
599 if (lastSlash == std::string::npos || lastSlash == path.size() ||
600 matches.empty())
James Feista5e58722019-04-22 14:43:11 -0700601 {
Ed Tanousbb679322022-05-16 16:10:00 -0700602 continue;
James Feista5e58722019-04-22 14:43:11 -0700603 }
Zev Weiss72f322f2022-08-12 18:21:01 -0700604 std::string sensorName = path.substr(lastSlash + 1);
Ed Tanousbb679322022-05-16 16:10:00 -0700605 if (boost::starts_with(sensorName, "PS") &&
606 boost::ends_with(sensorName, "Input_Power"))
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700607 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700608 // lambda capture requires a proper variable (not a structured
609 // binding)
610 const std::string& cbPath = path;
Ed Tanousbb679322022-05-16 16:10:00 -0700611 self->dbusConnection->async_method_call(
Zev Weiss72f322f2022-08-12 18:21:01 -0700612 [weakRef, cbPath](boost::system::error_code ec,
613 const std::variant<double>& value) {
Ed Tanousbb679322022-05-16 16:10:00 -0700614 if (ec)
615 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700616 std::cerr << "Error getting value from " << cbPath
Ed Tanousbb679322022-05-16 16:10:00 -0700617 << "\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 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700628 std::cerr << cbPath << "Reading " << reading << "\n";
Ed Tanousbb679322022-05-16 16:10:00 -0700629 }
Zev Weiss72f322f2022-08-12 18:21:01 -0700630 self->powerReadings[cbPath] = reading;
Ed Tanousbb679322022-05-16 16:10:00 -0700631 },
Zev Weiss72f322f2022-08-12 18:21:01 -0700632 matches[0].first, cbPath, properties::interface,
Ed Tanousbb679322022-05-16 16:10:00 -0700633 properties::get, sensorValueInterface, "Value");
Zhikui Rendbb73aa2021-04-02 13:39:04 -0700634 }
Ed Tanousbb679322022-05-16 16:10:00 -0700635 }
James Feista5e58722019-04-22 14:43:11 -0700636 },
637 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
638 "/xyz/openbmc_project/sensors/power", 0,
639 std::array<const char*, 1>{sensorValueInterface});
James Feistbc896df2018-11-26 16:28:17 -0800640}
641
642void ExitAirTempSensor::updateReading(void)
643{
644
645 double val = 0.0;
646 if (calculate(val))
647 {
James Feist18af4232019-03-13 11:14:00 -0700648 val = std::floor(val + 0.5);
James Feistbc896df2018-11-26 16:28:17 -0800649 updateValue(val);
650 }
651 else
652 {
653 updateValue(std::numeric_limits<double>::quiet_NaN());
654 }
655}
656
James Feistb2eb3f52018-12-04 16:17:50 -0800657double ExitAirTempSensor::getTotalCFM(void)
James Feistbc896df2018-11-26 16:28:17 -0800658{
James Feistb2eb3f52018-12-04 16:17:50 -0800659 double sum = 0;
660 for (auto& sensor : cfmSensors)
James Feistbc896df2018-11-26 16:28:17 -0800661 {
James Feistb2eb3f52018-12-04 16:17:50 -0800662 double reading = 0;
663 if (!sensor->calculate(reading))
James Feistbc896df2018-11-26 16:28:17 -0800664 {
James Feistbc896df2018-11-26 16:28:17 -0800665 return -1;
666 }
James Feistb2eb3f52018-12-04 16:17:50 -0800667 sum += reading;
James Feistbc896df2018-11-26 16:28:17 -0800668 }
James Feistb2eb3f52018-12-04 16:17:50 -0800669
670 return sum;
James Feistbc896df2018-11-26 16:28:17 -0800671}
672
673bool ExitAirTempSensor::calculate(double& val)
674{
Zhikui Ren12e3d672020-12-03 15:14:49 -0800675 constexpr size_t maxErrorPrint = 5;
James Feistbc896df2018-11-26 16:28:17 -0800676 static bool firstRead = false;
James Feistae11cfc2019-05-07 15:01:20 -0700677 static size_t errorPrint = maxErrorPrint;
678
James Feistbc896df2018-11-26 16:28:17 -0800679 double cfm = getTotalCFM();
680 if (cfm <= 0)
681 {
682 std::cerr << "Error getting cfm\n";
683 return false;
684 }
685
Zhikui Ren12e3d672020-12-03 15:14:49 -0800686 // 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 Feistbc896df2018-11-26 16:28:17 -0800703 // if there is an error getting inlet temp, return error
704 if (std::isnan(inletTemp))
705 {
James Feistae11cfc2019-05-07 15:01:20 -0700706 if (errorPrint > 0)
707 {
708 errorPrint--;
709 std::cerr << "Cannot get inlet temp\n";
710 }
James Feistbc896df2018-11-26 16:28:17 -0800711 val = 0;
712 return false;
713 }
714
715 // if fans are off, just make the exit temp equal to inlet
James Feist71d31b22019-01-02 16:57:54 -0800716 if (!isPowerOn())
James Feistbc896df2018-11-26 16:28:17 -0800717 {
718 val = inletTemp;
719 return true;
720 }
721
722 double totalPower = 0;
Zev Weiss72f322f2022-08-12 18:21:01 -0700723 for (const auto& [path, reading] : powerReadings)
James Feistbc896df2018-11-26 16:28:17 -0800724 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700725 if (std::isnan(reading))
James Feistbc896df2018-11-26 16:28:17 -0800726 {
727 continue;
728 }
Zev Weiss72f322f2022-08-12 18:21:01 -0700729 totalPower += reading;
James Feistbc896df2018-11-26 16:28:17 -0800730 }
731
732 // Calculate power correction factor
733 // Ci = CL + (CH - CL)/(QMax - QMin) * (CFM - QMin)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700734 double powerFactor = 0.0;
James Feistbc896df2018-11-26 16:28:17 -0800735 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 Tanous8a57ec02020-10-09 12:46:52 -0700749 totalPower *= powerFactor;
James Feistbc896df2018-11-26 16:28:17 -0800750 totalPower += pOffset;
751
752 if (totalPower == 0)
753 {
James Feistae11cfc2019-05-07 15:01:20 -0700754 if (errorPrint > 0)
755 {
756 errorPrint--;
757 std::cerr << "total power 0\n";
758 }
James Feistbc896df2018-11-26 16:28:17 -0800759 val = 0;
760 return false;
761 }
762
Ed Tanous8a57ec02020-10-09 12:46:52 -0700763 if constexpr (debug)
James Feistbc896df2018-11-26 16:28:17 -0800764 {
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 Tanous8a57ec02020-10-09 12:46:52 -0700772 double reading = 1.76 * totalPower * altitudeFactor;
James Feistbc896df2018-11-26 16:28:17 -0800773 reading /= cfm;
774 reading += inletTemp;
775
Ed Tanous8a57ec02020-10-09 12:46:52 -0700776 if constexpr (debug)
James Feistbc896df2018-11-26 16:28:17 -0800777 {
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 Ren12e3d672020-12-03 15:14:49 -0800799 auto time = std::chrono::steady_clock::now();
James Feistbc896df2018-11-26 16:28:17 -0800800 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 Tanous8a57ec02020-10-09 12:46:52 -0700817 if constexpr (debug)
James Feistbc896df2018-11-26 16:28:17 -0800818 {
819 std::cout << "AlphaDT: " << alphaDT << "\n";
820 }
821
822 reading = ((reading * alphaDT) + (lastReading * (1.0 - alphaDT)));
823
Ed Tanous8a57ec02020-10-09 12:46:52 -0700824 if constexpr (debug)
James Feistbc896df2018-11-26 16:28:17 -0800825 {
826 std::cout << "Reading 2: " << reading << "\n";
827 }
828
829 val = reading;
830 lastReading = reading;
831 lastTime = time;
James Feistae11cfc2019-05-07 15:01:20 -0700832 errorPrint = maxErrorPrint;
James Feistbc896df2018-11-26 16:28:17 -0800833 return true;
834}
835
836void ExitAirTempSensor::checkThresholds(void)
837{
838 thresholds::checkThresholds(this);
839}
840
Zev Weissafd15042022-07-18 12:28:40 -0700841static void loadVariantPathArray(const SensorBaseConfigMap& data,
842 const std::string& key,
843 std::vector<std::string>& resp)
James Feistbc896df2018-11-26 16:28:17 -0800844{
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 Feist3eb82622019-02-08 13:10:22 -0800852 std::vector<std::string> config = std::get<std::vector<std::string>>(copy);
James Feistbc896df2018-11-26 16:28:17 -0800853 for (auto& str : config)
854 {
855 boost::replace_all(str, " ", "_");
856 }
857 resp = std::move(config);
858}
859
860void createSensor(sdbusplus::asio::object_server& objectServer,
James Feistb2eb3f52018-12-04 16:17:50 -0800861 std::shared_ptr<ExitAirTempSensor>& exitAirSensor,
James Feistbc896df2018-11-26 16:28:17 -0800862 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
863{
864 if (!dbusConnection)
865 {
866 std::cerr << "Connection not created\n";
867 return;
868 }
James Feist655f3762020-10-05 15:28:15 -0700869 auto getter = std::make_shared<GetSensorConfiguration>(
Ed Tanousbb679322022-05-16 16:10:00 -0700870 dbusConnection,
871 [&objectServer, &dbusConnection,
872 &exitAirSensor](const ManagedObjectType& resp) {
873 cfmSensors.clear();
Zev Weiss72f322f2022-08-12 18:21:01 -0700874 for (const auto& [path, interfaces] : resp)
Ed Tanousbb679322022-05-16 16:10:00 -0700875 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700876 for (const auto& [intf, cfg] : interfaces)
James Feistbc896df2018-11-26 16:28:17 -0800877 {
Zev Weiss72f322f2022-08-12 18:21:01 -0700878 if (intf == exitAirIface)
James Feistbc896df2018-11-26 16:28:17 -0800879 {
Ed Tanousbb679322022-05-16 16:10:00 -0700880 // thresholds should be under the same path
881 std::vector<thresholds::Threshold> sensorThresholds;
Zev Weiss72f322f2022-08-12 18:21:01 -0700882 parseThresholdsFromConfig(interfaces, sensorThresholds);
James Feistbc896df2018-11-26 16:28:17 -0800883
Zev Weiss72f322f2022-08-12 18:21:01 -0700884 std::string name = loadVariant<std::string>(cfg, "Name");
Ed Tanousbb679322022-05-16 16:10:00 -0700885 exitAirSensor = std::make_shared<ExitAirTempSensor>(
Zev Weiss72f322f2022-08-12 18:21:01 -0700886 dbusConnection, name, path.str, objectServer,
Ed Tanousbb679322022-05-16 16:10:00 -0700887 std::move(sensorThresholds));
888 exitAirSensor->powerFactorMin =
Zev Weiss72f322f2022-08-12 18:21:01 -0700889 loadVariant<double>(cfg, "PowerFactorMin");
Ed Tanousbb679322022-05-16 16:10:00 -0700890 exitAirSensor->powerFactorMax =
Zev Weiss72f322f2022-08-12 18:21:01 -0700891 loadVariant<double>(cfg, "PowerFactorMax");
892 exitAirSensor->qMin = loadVariant<double>(cfg, "QMin");
893 exitAirSensor->qMax = loadVariant<double>(cfg, "QMax");
894 exitAirSensor->alphaS = loadVariant<double>(cfg, "AlphaS");
895 exitAirSensor->alphaF = loadVariant<double>(cfg, "AlphaF");
Ed Tanousbb679322022-05-16 16:10:00 -0700896 }
Zev Weiss72f322f2022-08-12 18:21:01 -0700897 else if (intf == cfmIface)
Ed Tanousbb679322022-05-16 16:10:00 -0700898 {
899 // thresholds should be under the same path
900 std::vector<thresholds::Threshold> sensorThresholds;
Zev Weiss72f322f2022-08-12 18:21:01 -0700901 parseThresholdsFromConfig(interfaces, sensorThresholds);
902 std::string name = loadVariant<std::string>(cfg, "Name");
Ed Tanousbb679322022-05-16 16:10:00 -0700903 auto sensor = std::make_shared<CFMSensor>(
Zev Weiss72f322f2022-08-12 18:21:01 -0700904 dbusConnection, name, path.str, objectServer,
Ed Tanousbb679322022-05-16 16:10:00 -0700905 std::move(sensorThresholds), exitAirSensor);
Zev Weiss72f322f2022-08-12 18:21:01 -0700906 loadVariantPathArray(cfg, "Tachs", sensor->tachs);
907 sensor->maxCFM = loadVariant<double>(cfg, "MaxCFM");
James Feistbc896df2018-11-26 16:28:17 -0800908
Ed Tanousbb679322022-05-16 16:10:00 -0700909 // change these into percent upon getting the data
Zev Weiss72f322f2022-08-12 18:21:01 -0700910 sensor->c1 = loadVariant<double>(cfg, "C1") / 100;
911 sensor->c2 = loadVariant<double>(cfg, "C2") / 100;
Ed Tanousbb679322022-05-16 16:10:00 -0700912 sensor->tachMinPercent =
Zev Weiss72f322f2022-08-12 18:21:01 -0700913 loadVariant<double>(cfg, "TachMinPercent");
Ed Tanousbb679322022-05-16 16:10:00 -0700914 sensor->tachMaxPercent =
Zev Weiss72f322f2022-08-12 18:21:01 -0700915 loadVariant<double>(cfg, "TachMaxPercent");
Ed Tanousbb679322022-05-16 16:10:00 -0700916 sensor->createMaxCFMIface();
917 sensor->setupMatches();
James Feistbc896df2018-11-26 16:28:17 -0800918
Ed Tanousbb679322022-05-16 16:10:00 -0700919 cfmSensors.emplace_back(std::move(sensor));
James Feistbc896df2018-11-26 16:28:17 -0800920 }
921 }
Ed Tanousbb679322022-05-16 16:10:00 -0700922 }
923 if (exitAirSensor)
924 {
925 exitAirSensor->setupMatches();
926 exitAirSensor->updateReading();
927 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700928 });
James Feist655f3762020-10-05 15:28:15 -0700929 getter->getConfiguration(
930 std::vector<std::string>(monitorIfaces.begin(), monitorIfaces.end()));
James Feistbc896df2018-11-26 16:28:17 -0800931}
932
James Feistb6c0b912019-07-09 12:21:44 -0700933int main()
James Feistbc896df2018-11-26 16:28:17 -0800934{
935
936 boost::asio::io_service io;
937 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
938 systemBus->request_name("xyz.openbmc_project.ExitAirTempSensor");
939 sdbusplus::asio::object_server objectServer(systemBus);
940 std::shared_ptr<ExitAirTempSensor> sensor =
941 nullptr; // wait until we find the config
James Feistbc896df2018-11-26 16:28:17 -0800942
943 io.post([&]() { createSensor(objectServer, sensor, systemBus); });
944
945 boost::asio::deadline_timer configTimer(io);
946
Patrick Williams92f8f512022-07-22 19:26:55 -0500947 std::function<void(sdbusplus::message_t&)> eventHandler =
948 [&](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700949 configTimer.expires_from_now(boost::posix_time::seconds(1));
950 // create a timer because normally multiple properties change
951 configTimer.async_wait([&](const boost::system::error_code& ec) {
952 if (ec == boost::asio::error::operation_aborted)
953 {
954 return; // we're being canceled
955 }
956 createSensor(objectServer, sensor, systemBus);
957 if (!sensor)
958 {
959 std::cout << "Configuration not detected\n";
960 }
961 });
962 };
Zev Weiss214d9712022-08-12 12:54:31 -0700963 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
964 setupPropertiesChangedMatches(*systemBus, monitorIfaces, eventHandler);
James Feistbc896df2018-11-26 16:28:17 -0800965
Bruce Lee913d4d02021-07-22 10:18:42 +0800966 setupManufacturingModeMatch(*systemBus);
James Feistbc896df2018-11-26 16:28:17 -0800967 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700968 return 0;
James Feistbc896df2018-11-26 16:28:17 -0800969}