blob: 6befd35ade0089de8402a8d5be117fba46f41dd7 [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
17#include "ExitAirTempSensor.hpp"
18
19#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
22#include <math.h>
23
24#include <boost/algorithm/string/predicate.hpp>
25#include <boost/algorithm/string/replace.hpp>
26#include <chrono>
27#include <iostream>
28#include <limits>
29#include <numeric>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32#include <vector>
33
34constexpr const float altitudeFactor = 1.14;
35constexpr const char* exitAirIface =
36 "xyz.openbmc_project.Configuration.ExitAirTempSensor";
37constexpr const char* cfmIface = "xyz.openbmc_project.Configuration.CFMSensor";
38
39// todo: this *might* need to be configurable
40constexpr const char* inletTemperatureSensor = "temperature/Front_Panel_Temp";
James Feist13452092019-03-07 16:38:12 -080041constexpr const char* pidConfigurationType =
42 "xyz.openbmc_project.Configuration.Pid";
43constexpr const char* settingsDaemon = "xyz.openbmc_project.Settings";
44constexpr const char* cfmSettingPath = "/xyz/openbmc_project/control/cfm_limit";
45constexpr const char* cfmSettingIface = "xyz.openbmc_project.Control.CFMLimit";
James Feistbc896df2018-11-26 16:28:17 -080046
47static constexpr bool DEBUG = false;
48
James Feistb2eb3f52018-12-04 16:17:50 -080049static constexpr double cfmMaxReading = 255;
50static constexpr double cfmMinReading = 0;
51
James Feist13452092019-03-07 16:38:12 -080052static constexpr size_t minSystemCfm = 50;
53
James Feistb2eb3f52018-12-04 16:17:50 -080054static void setupSensorMatch(
55 std::vector<sdbusplus::bus::match::match>& matches,
56 sdbusplus::bus::bus& connection, const std::string& type,
57 std::function<void(const double&, sdbusplus::message::message&)>&& callback)
58{
59
60 std::function<void(sdbusplus::message::message & message)> eventHandler =
61 [callback{std::move(callback)}](sdbusplus::message::message& message) {
62 std::string objectName;
James Feist3eb82622019-02-08 13:10:22 -080063 boost::container::flat_map<std::string,
64 std::variant<double, int64_t>>
James Feistb2eb3f52018-12-04 16:17:50 -080065 values;
66 message.read(objectName, values);
67 auto findValue = values.find("Value");
68 if (findValue == values.end())
69 {
70 return;
71 }
James Feist3eb82622019-02-08 13:10:22 -080072 double value =
73 std::visit(VariantToDoubleVisitor(), findValue->second);
James Feist9566bfa2019-01-29 15:31:23 -080074 if (std::isnan(value))
75 {
76 return;
77 }
78
James Feistb2eb3f52018-12-04 16:17:50 -080079 callback(value, message);
80 };
81 matches.emplace_back(connection,
82 "type='signal',"
83 "member='PropertiesChanged',interface='org."
84 "freedesktop.DBus.Properties',path_"
85 "namespace='/xyz/openbmc_project/sensors/" +
86 std::string(type) +
87 "',arg0='xyz.openbmc_project.Sensor.Value'",
88 std::move(eventHandler));
89}
90
James Feist13452092019-03-07 16:38:12 -080091static void setMaxPWM(const std::shared_ptr<sdbusplus::asio::connection>& conn,
92 double value)
93{
94 using GetSubTreeType = std::vector<std::pair<
95 std::string,
96 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
97
98 conn->async_method_call(
99 [conn, value](const boost::system::error_code ec,
100 const GetSubTreeType& ret) {
101 if (ec)
102 {
103 std::cerr << "Error calling mapper\n";
104 return;
105 }
106 for (const auto& [path, objDict] : ret)
107 {
108 if (objDict.empty())
109 {
110 return;
111 }
112 const std::string& owner = objDict.begin()->first;
113
114 conn->async_method_call(
115 [conn, value, owner,
116 path](const boost::system::error_code ec,
117 const std::variant<std::string>& classType) {
118 if (ec)
119 {
120 std::cerr << "Error getting pid class\n";
121 return;
122 }
123 auto classStr = std::get_if<std::string>(&classType);
124 if (classStr == nullptr || *classStr != "fan")
125 {
126 return;
127 }
128 conn->async_method_call(
129 [](boost::system::error_code& ec) {
130 if (ec)
131 {
132 std::cerr << "Error setting pid class\n";
133 return;
134 }
135 },
136 owner, path, "org.freedesktop.DBus.Properties",
137 "Set", pidConfigurationType, "OutLimitMax",
138 std::variant<double>(value));
139 },
140 owner, path, "org.freedesktop.DBus.Properties", "Get",
141 pidConfigurationType, "Class");
142 }
143 },
144 "xyz.openbmc_project.ObjectMapper",
145 "/xyz/openbmc_project/object_mapper",
146 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
147 std::array<std::string, 1>{pidConfigurationType});
148}
149
James Feistb2eb3f52018-12-04 16:17:50 -0800150CFMSensor::CFMSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
151 const std::string& sensorName,
152 const std::string& sensorConfiguration,
153 sdbusplus::asio::object_server& objectServer,
154 std::vector<thresholds::Threshold>&& thresholds,
155 std::shared_ptr<ExitAirTempSensor>& parent) :
156 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
157 "" /* todo: remove arg from base*/, std::move(thresholds),
158 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
159 cfmMaxReading, cfmMinReading),
James Feist9566bfa2019-01-29 15:31:23 -0800160 dbusConnection(conn), parent(parent), objServer(objectServer)
James Feistb2eb3f52018-12-04 16:17:50 -0800161{
162 sensorInterface =
163 objectServer.add_interface("/xyz/openbmc_project/sensors/cfm/" + name,
164 "xyz.openbmc_project.Sensor.Value");
165
166 if (thresholds::hasWarningInterface(thresholds))
167 {
168 thresholdInterfaceWarning = objectServer.add_interface(
169 "/xyz/openbmc_project/sensors/cfm/" + name,
170 "xyz.openbmc_project.Sensor.Threshold.Warning");
171 }
172 if (thresholds::hasCriticalInterface(thresholds))
173 {
174 thresholdInterfaceCritical = objectServer.add_interface(
175 "/xyz/openbmc_project/sensors/cfm/" + name,
176 "xyz.openbmc_project.Sensor.Threshold.Critical");
177 }
James Feist078f2322019-03-08 11:09:05 -0800178
179 association = objectServer.add_interface(
180 "/xyz/openbmc_project/sensors/voltage/" + name,
181 "org.openbmc.Associations");
182
James Feistb2eb3f52018-12-04 16:17:50 -0800183 setInitialProperties(conn);
184 setupSensorMatch(
185 matches, *dbusConnection, "fan_tach",
186 std::move(
187 [this](const double& value, sdbusplus::message::message& message) {
188 tachReadings[message.get_path()] = value;
189 if (tachRanges.find(message.get_path()) == tachRanges.end())
190 {
191 // calls update reading after updating ranges
192 addTachRanges(message.get_sender(), message.get_path());
193 }
194 else
195 {
196 updateReading();
197 }
198 }));
James Feist13452092019-03-07 16:38:12 -0800199 pwmLimitIface =
200 objectServer.add_interface("/xyz/openbmc_project/control/pwm_limit",
201 "xyz.openbmc_project.Control.PWMLimit");
202 cfmLimitIface =
203 objectServer.add_interface("/xyz/openbmc_project/control/MaxCFM",
204 "xyz.openbmc_project.Control.CFMLimit");
205
206 conn->async_method_call(
207 [this, conn](const boost::system::error_code ec,
208 const std::variant<double> cfmVariant) {
209 uint64_t maxRpm = 100;
210 if (!ec)
211 {
212
213 auto cfm = std::get_if<double>(&cfmVariant);
214 if (cfm != nullptr || *cfm >= minSystemCfm)
215 {
216 maxRpm = getMaxRpm(*cfm);
217 }
218 }
219 pwmLimitIface->register_property("Limit", maxRpm);
220 pwmLimitIface->initialize();
221 setMaxPWM(conn, maxRpm);
222 },
223 settingsDaemon, cfmSettingPath, "org.freedesktop.DBus.Properties",
224 "Get", cfmSettingIface, "Limit");
225
226 matches.emplace_back(
227 *conn,
228 "type='signal',"
229 "member='PropertiesChanged',interface='org."
230 "freedesktop.DBus.Properties',path='" +
231 std::string(cfmSettingPath) + "',arg0='" +
232 std::string(cfmSettingIface) + "'",
233 [this, conn](sdbusplus::message::message& message) {
234 boost::container::flat_map<std::string, std::variant<double>>
235 values;
236 std::string objectName;
237 message.read(objectName, values);
238 const auto findValue = values.find("Limit");
239 if (findValue == values.end())
240 {
241 return;
242 }
243 const auto reading = std::get_if<double>(&(findValue->second));
244 if (reading == nullptr)
245 {
246 std::cerr << "Got CFM Limit of wrong type\n";
247 return;
248 }
249 if (*reading < minSystemCfm && *reading != 0)
250 {
251 std::cerr << "Illegal CFM setting detected\n";
252 return;
253 }
254 uint64_t maxRpm = getMaxRpm(*reading);
255 pwmLimitIface->set_property("Limit", maxRpm);
256 setMaxPWM(conn, maxRpm);
257 });
James Feistb2eb3f52018-12-04 16:17:50 -0800258}
259
James Feist9566bfa2019-01-29 15:31:23 -0800260CFMSensor::~CFMSensor()
261{
262 objServer.remove_interface(thresholdInterfaceWarning);
263 objServer.remove_interface(thresholdInterfaceCritical);
264 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800265 objServer.remove_interface(association);
James Feist13452092019-03-07 16:38:12 -0800266 objServer.remove_interface(cfmLimitIface);
267 objServer.remove_interface(pwmLimitIface);
268}
269
270void CFMSensor::createMaxCFMIface(void)
271{
272 cfmLimitIface->register_property("Limit", static_cast<double>(c2) * maxCFM *
273 tachs.size());
274 cfmLimitIface->initialize();
James Feist9566bfa2019-01-29 15:31:23 -0800275}
276
James Feistb2eb3f52018-12-04 16:17:50 -0800277void CFMSensor::addTachRanges(const std::string& serviceName,
278 const std::string& path)
279{
280 dbusConnection->async_method_call(
281 [this, path](const boost::system::error_code ec,
282 const boost::container::flat_map<std::string,
283 BasicVariantType>& data) {
284 if (ec)
285 {
286 std::cerr << "Error getting properties from " << path << "\n";
James Feist1ccdb5e2019-01-24 09:44:01 -0800287 return;
James Feistb2eb3f52018-12-04 16:17:50 -0800288 }
289
290 double max = loadVariant<double>(data, "MaxValue");
291 double min = loadVariant<double>(data, "MinValue");
292 tachRanges[path] = std::make_pair(min, max);
293 updateReading();
294 },
295 serviceName, path, "org.freedesktop.DBus.Properties", "GetAll",
296 "xyz.openbmc_project.Sensor.Value");
297}
298
299void CFMSensor::checkThresholds(void)
300{
301 thresholds::checkThresholds(this);
302}
303
304void CFMSensor::updateReading(void)
305{
306 double val = 0.0;
307 if (calculate(val))
308 {
309 if (value != val && parent)
310 {
311 parent->updateReading();
312 }
313 updateValue(val);
314 }
315 else
316 {
317 updateValue(std::numeric_limits<double>::quiet_NaN());
318 }
319}
320
James Feist13452092019-03-07 16:38:12 -0800321uint64_t CFMSensor::getMaxRpm(uint64_t cfmMaxSetting)
322{
323 uint64_t pwmPercent = 100;
324 double totalCFM = std::numeric_limits<double>::max();
325 if (cfmMaxSetting == 0)
326 {
327 return pwmPercent;
328 }
329
330 while (totalCFM > cfmMaxSetting)
331 {
332 double ci = 0;
333 if (pwmPercent == 0)
334 {
335 ci = 0;
336 }
337 else if (pwmPercent < tachMinPercent)
338 {
339 ci = c1;
340 }
341 else if (pwmPercent > tachMaxPercent)
342 {
343 ci = c2;
344 }
345 else
346 {
347 ci = c1 + (((c2 - c1) * (pwmPercent - tachMinPercent)) /
348 (tachMaxPercent - tachMinPercent));
349 }
350
351 // Now calculate the CFM for this tach
352 // CFMi = Ci * Qmaxi * TACHi
353 totalCFM = ci * maxCFM * pwmPercent;
354 totalCFM *= tachs.size();
355 // divide by 100 since pwm is in percent
356 totalCFM /= 100;
357
358 pwmPercent--;
359 if (pwmPercent <= 0)
360 {
361 break;
362 }
363 }
364 return pwmPercent;
365}
366
James Feistb2eb3f52018-12-04 16:17:50 -0800367bool CFMSensor::calculate(double& value)
368{
369 double totalCFM = 0;
370 for (const std::string& tachName : tachs)
371 {
James Feist9566bfa2019-01-29 15:31:23 -0800372
James Feistb2eb3f52018-12-04 16:17:50 -0800373 auto findReading = std::find_if(
374 tachReadings.begin(), tachReadings.end(), [&](const auto& item) {
375 return boost::ends_with(item.first, tachName);
376 });
377 auto findRange = std::find_if(
378 tachRanges.begin(), tachRanges.end(), [&](const auto& item) {
379 return boost::ends_with(item.first, tachName);
380 });
381 if (findReading == tachReadings.end())
382 {
James Feista96329f2019-01-24 10:08:27 -0800383 if (DEBUG)
384 {
385 std::cerr << "Can't find " << tachName << "in readings\n";
386 }
James Feist9566bfa2019-01-29 15:31:23 -0800387 continue; // haven't gotten a reading
James Feistb2eb3f52018-12-04 16:17:50 -0800388 }
389
390 if (findRange == tachRanges.end())
391 {
James Feist523828e2019-03-04 14:38:37 -0800392 std::cerr << "Can't find " << tachName << " in ranges\n";
James Feistb2eb3f52018-12-04 16:17:50 -0800393 return false; // haven't gotten a max / min
394 }
395
396 // avoid divide by 0
397 if (findRange->second.second == 0)
398 {
399 std::cerr << "Tach Max Set to 0 " << tachName << "\n";
400 return false;
401 }
402
403 double rpm = findReading->second;
404
405 // for now assume the min for a fan is always 0, divide by max to get
406 // percent and mult by 100
407 rpm /= findRange->second.second;
408 rpm *= 100;
409
410 if constexpr (DEBUG)
411 {
412 std::cout << "Tach " << tachName << "at " << rpm << "\n";
413 }
414
415 // Do a linear interpolation to get Ci
416 // Ci = C1 + (C2 - C1)/(RPM2 - RPM1) * (TACHi - TACH1)
417
418 double ci = 0;
419 if (rpm == 0)
420 {
421 ci = 0;
422 }
423 else if (rpm < tachMinPercent)
424 {
425 ci = c1;
426 }
427 else if (rpm > tachMaxPercent)
428 {
429 ci = c2;
430 }
431 else
432 {
433 ci = c1 + (((c2 - c1) * (rpm - tachMinPercent)) /
434 (tachMaxPercent - tachMinPercent));
435 }
436
437 // Now calculate the CFM for this tach
438 // CFMi = Ci * Qmaxi * TACHi
439 totalCFM += ci * maxCFM * rpm;
440 }
441
442 // divide by 100 since rpm is in percent
443 value = totalCFM / 100;
James Feist9566bfa2019-01-29 15:31:23 -0800444 return true;
James Feistb2eb3f52018-12-04 16:17:50 -0800445}
446
447static constexpr double exitAirMaxReading = 127;
448static constexpr double exitAirMinReading = -128;
James Feistbc896df2018-11-26 16:28:17 -0800449ExitAirTempSensor::ExitAirTempSensor(
450 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feistb2eb3f52018-12-04 16:17:50 -0800451 const std::string& sensorName, const std::string& sensorConfiguration,
James Feistbc896df2018-11-26 16:28:17 -0800452 sdbusplus::asio::object_server& objectServer,
453 std::vector<thresholds::Threshold>&& thresholds) :
James Feistb2eb3f52018-12-04 16:17:50 -0800454 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
455 "" /* todo: remove arg from base*/, std::move(thresholds),
456 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
457 exitAirMaxReading, exitAirMinReading),
James Feist523828e2019-03-04 14:38:37 -0800458 dbusConnection(conn), objServer(objectServer)
James Feistbc896df2018-11-26 16:28:17 -0800459{
460 sensorInterface = objectServer.add_interface(
461 "/xyz/openbmc_project/sensors/temperature/" + name,
462 "xyz.openbmc_project.Sensor.Value");
463
464 if (thresholds::hasWarningInterface(thresholds))
465 {
466 thresholdInterfaceWarning = objectServer.add_interface(
467 "/xyz/openbmc_project/sensors/temperature/" + name,
468 "xyz.openbmc_project.Sensor.Threshold.Warning");
469 }
470 if (thresholds::hasCriticalInterface(thresholds))
471 {
472 thresholdInterfaceCritical = objectServer.add_interface(
473 "/xyz/openbmc_project/sensors/temperature/" + name,
474 "xyz.openbmc_project.Sensor.Threshold.Critical");
475 }
James Feist078f2322019-03-08 11:09:05 -0800476 association = objectServer.add_interface(
477 "/xyz/openbmc_project/sensors/temperature/" + name,
478 "org.openbmc.Associations");
James Feistbc896df2018-11-26 16:28:17 -0800479 setInitialProperties(conn);
480 setupMatches();
James Feist71d31b22019-01-02 16:57:54 -0800481 setupPowerMatch(conn);
James Feistbc896df2018-11-26 16:28:17 -0800482}
483
484ExitAirTempSensor::~ExitAirTempSensor()
485{
James Feist523828e2019-03-04 14:38:37 -0800486 objServer.remove_interface(thresholdInterfaceWarning);
487 objServer.remove_interface(thresholdInterfaceCritical);
488 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800489 objServer.remove_interface(association);
James Feistbc896df2018-11-26 16:28:17 -0800490}
491
492void ExitAirTempSensor::setupMatches(void)
493{
494
James Feistb2eb3f52018-12-04 16:17:50 -0800495 constexpr const std::array<const char*, 2> matchTypes = {
496 "power", inletTemperatureSensor};
James Feistbc896df2018-11-26 16:28:17 -0800497
James Feistb2eb3f52018-12-04 16:17:50 -0800498 for (const std::string& type : matchTypes)
James Feistbc896df2018-11-26 16:28:17 -0800499 {
James Feistb2eb3f52018-12-04 16:17:50 -0800500 setupSensorMatch(matches, *dbusConnection, type,
501 [this, type](const double& value,
502 sdbusplus::message::message& message) {
503 if (type == "power")
504 {
505 powerReadings[message.get_path()] = value;
506 }
507 else if (type == inletTemperatureSensor)
508 {
509 inletTemp = value;
510 }
511 updateReading();
512 });
James Feistbc896df2018-11-26 16:28:17 -0800513 }
James Feist9566bfa2019-01-29 15:31:23 -0800514 dbusConnection->async_method_call(
515 [this](boost::system::error_code ec,
516 const std::variant<double>& value) {
517 if (ec)
518 {
519 // sensor not ready yet
520 return;
521 }
522
James Feist3eb82622019-02-08 13:10:22 -0800523 inletTemp = std::visit(VariantToDoubleVisitor(), value);
James Feist9566bfa2019-01-29 15:31:23 -0800524 },
525 "xyz.openbmc_project.HwmonTempSensor",
526 std::string("/xyz/openbmc_project/sensors/") + inletTemperatureSensor,
527 "org.freedesktop.DBus.Properties", "Get",
528 "xyz.openbmc_project.Sensor.Value", "Value");
James Feistbc896df2018-11-26 16:28:17 -0800529}
530
531void ExitAirTempSensor::updateReading(void)
532{
533
534 double val = 0.0;
535 if (calculate(val))
536 {
James Feist18af4232019-03-13 11:14:00 -0700537 val = std::floor(val + 0.5);
James Feistbc896df2018-11-26 16:28:17 -0800538 updateValue(val);
539 }
540 else
541 {
542 updateValue(std::numeric_limits<double>::quiet_NaN());
543 }
544}
545
James Feistb2eb3f52018-12-04 16:17:50 -0800546double ExitAirTempSensor::getTotalCFM(void)
James Feistbc896df2018-11-26 16:28:17 -0800547{
James Feistb2eb3f52018-12-04 16:17:50 -0800548 double sum = 0;
549 for (auto& sensor : cfmSensors)
James Feistbc896df2018-11-26 16:28:17 -0800550 {
James Feistb2eb3f52018-12-04 16:17:50 -0800551 double reading = 0;
552 if (!sensor->calculate(reading))
James Feistbc896df2018-11-26 16:28:17 -0800553 {
James Feistbc896df2018-11-26 16:28:17 -0800554 return -1;
555 }
James Feistb2eb3f52018-12-04 16:17:50 -0800556 sum += reading;
James Feistbc896df2018-11-26 16:28:17 -0800557 }
James Feistb2eb3f52018-12-04 16:17:50 -0800558
559 return sum;
James Feistbc896df2018-11-26 16:28:17 -0800560}
561
562bool ExitAirTempSensor::calculate(double& val)
563{
564 static bool firstRead = false;
565 double cfm = getTotalCFM();
566 if (cfm <= 0)
567 {
568 std::cerr << "Error getting cfm\n";
569 return false;
570 }
571
572 // if there is an error getting inlet temp, return error
573 if (std::isnan(inletTemp))
574 {
575 std::cerr << "Cannot get inlet temp\n";
576 val = 0;
577 return false;
578 }
579
580 // if fans are off, just make the exit temp equal to inlet
James Feist71d31b22019-01-02 16:57:54 -0800581 if (!isPowerOn())
James Feistbc896df2018-11-26 16:28:17 -0800582 {
583 val = inletTemp;
584 return true;
585 }
586
587 double totalPower = 0;
588 for (const auto& reading : powerReadings)
589 {
590 if (std::isnan(reading.second))
591 {
592 continue;
593 }
594 totalPower += reading.second;
595 }
596
597 // Calculate power correction factor
598 // Ci = CL + (CH - CL)/(QMax - QMin) * (CFM - QMin)
599 float powerFactor = 0.0;
600 if (cfm <= qMin)
601 {
602 powerFactor = powerFactorMin;
603 }
604 else if (cfm >= qMax)
605 {
606 powerFactor = powerFactorMax;
607 }
608 else
609 {
610 powerFactor = powerFactorMin + ((powerFactorMax - powerFactorMin) /
611 (qMax - qMin) * (cfm - qMin));
612 }
613
614 totalPower *= powerFactor;
615 totalPower += pOffset;
616
617 if (totalPower == 0)
618 {
619 std::cerr << "total power 0\n";
620 val = 0;
621 return false;
622 }
623
624 if constexpr (DEBUG)
625 {
626 std::cout << "Power Factor " << powerFactor << "\n";
627 std::cout << "Inlet Temp " << inletTemp << "\n";
628 std::cout << "Total Power" << totalPower << "\n";
629 }
630
631 // Calculate the exit air temp
632 // Texit = Tfp + (1.76 * TotalPower / CFM * Faltitude)
633 double reading = 1.76 * totalPower * altitudeFactor;
634 reading /= cfm;
635 reading += inletTemp;
636
637 if constexpr (DEBUG)
638 {
639 std::cout << "Reading 1: " << reading << "\n";
640 }
641
642 // Now perform the exponential average
643 // Calculate alpha based on SDR values and CFM
644 // Ai = As + (Af - As)/(QMax - QMin) * (CFM - QMin)
645
646 double alpha = 0.0;
647 if (cfm < qMin)
648 {
649 alpha = alphaS;
650 }
651 else if (cfm >= qMax)
652 {
653 alpha = alphaF;
654 }
655 else
656 {
657 alpha = alphaS + ((alphaF - alphaS) * (cfm - qMin) / (qMax - qMin));
658 }
659
660 auto time = std::chrono::system_clock::now();
661 if (!firstRead)
662 {
663 firstRead = true;
664 lastTime = time;
665 lastReading = reading;
666 }
667 double alphaDT =
668 std::chrono::duration_cast<std::chrono::seconds>(time - lastTime)
669 .count() *
670 alpha;
671
672 // cap at 1.0 or the below fails
673 if (alphaDT > 1.0)
674 {
675 alphaDT = 1.0;
676 }
677
678 if constexpr (DEBUG)
679 {
680 std::cout << "AlphaDT: " << alphaDT << "\n";
681 }
682
683 reading = ((reading * alphaDT) + (lastReading * (1.0 - alphaDT)));
684
685 if constexpr (DEBUG)
686 {
687 std::cout << "Reading 2: " << reading << "\n";
688 }
689
690 val = reading;
691 lastReading = reading;
692 lastTime = time;
693 return true;
694}
695
696void ExitAirTempSensor::checkThresholds(void)
697{
698 thresholds::checkThresholds(this);
699}
700
James Feistbc896df2018-11-26 16:28:17 -0800701static void loadVariantPathArray(
702 const boost::container::flat_map<std::string, BasicVariantType>& data,
703 const std::string& key, std::vector<std::string>& resp)
704{
705 auto it = data.find(key);
706 if (it == data.end())
707 {
708 std::cerr << "Configuration missing " << key << "\n";
709 throw std::invalid_argument("Key Missing");
710 }
711 BasicVariantType copy = it->second;
James Feist3eb82622019-02-08 13:10:22 -0800712 std::vector<std::string> config = std::get<std::vector<std::string>>(copy);
James Feistbc896df2018-11-26 16:28:17 -0800713 for (auto& str : config)
714 {
715 boost::replace_all(str, " ", "_");
716 }
717 resp = std::move(config);
718}
719
720void createSensor(sdbusplus::asio::object_server& objectServer,
James Feistb2eb3f52018-12-04 16:17:50 -0800721 std::shared_ptr<ExitAirTempSensor>& exitAirSensor,
James Feistbc896df2018-11-26 16:28:17 -0800722 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
723{
724 if (!dbusConnection)
725 {
726 std::cerr << "Connection not created\n";
727 return;
728 }
729 dbusConnection->async_method_call(
730 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
731 if (ec)
732 {
733 std::cerr << "Error contacting entity manager\n";
734 return;
735 }
James Feistb2eb3f52018-12-04 16:17:50 -0800736 std::vector<std::unique_ptr<CFMSensor>> cfmSensors;
James Feistbc896df2018-11-26 16:28:17 -0800737 for (const auto& pathPair : resp)
738 {
739 for (const auto& entry : pathPair.second)
740 {
741 if (entry.first == exitAirIface)
742 {
James Feistbc896df2018-11-26 16:28:17 -0800743 // thresholds should be under the same path
744 std::vector<thresholds::Threshold> sensorThresholds;
745 parseThresholdsFromConfig(pathPair.second,
746 sensorThresholds);
James Feistbc896df2018-11-26 16:28:17 -0800747
James Feist523828e2019-03-04 14:38:37 -0800748 std::string name =
749 loadVariant<std::string>(entry.second, "Name");
750 exitAirSensor = std::make_shared<ExitAirTempSensor>(
751 dbusConnection, name, pathPair.first.str,
752 objectServer, std::move(sensorThresholds));
James Feistb2eb3f52018-12-04 16:17:50 -0800753 exitAirSensor->powerFactorMin =
754 loadVariant<double>(entry.second, "PowerFactorMin");
755 exitAirSensor->powerFactorMax =
756 loadVariant<double>(entry.second, "PowerFactorMax");
757 exitAirSensor->qMin =
758 loadVariant<double>(entry.second, "QMin");
759 exitAirSensor->qMax =
760 loadVariant<double>(entry.second, "QMax");
761 exitAirSensor->alphaS =
762 loadVariant<double>(entry.second, "AlphaS");
763 exitAirSensor->alphaF =
764 loadVariant<double>(entry.second, "AlphaF");
James Feistbc896df2018-11-26 16:28:17 -0800765 }
766 else if (entry.first == cfmIface)
767
768 {
James Feistb2eb3f52018-12-04 16:17:50 -0800769 // thresholds should be under the same path
770 std::vector<thresholds::Threshold> sensorThresholds;
771 parseThresholdsFromConfig(pathPair.second,
772 sensorThresholds);
773 std::string name =
774 loadVariant<std::string>(entry.second, "Name");
775 auto sensor = std::make_unique<CFMSensor>(
776 dbusConnection, name, pathPair.first.str,
777 objectServer, std::move(sensorThresholds),
778 exitAirSensor);
779 loadVariantPathArray(entry.second, "Tachs",
780 sensor->tachs);
781 sensor->maxCFM =
782 loadVariant<double>(entry.second, "MaxCFM");
James Feistbc896df2018-11-26 16:28:17 -0800783
784 // change these into percent upon getting the data
James Feistb2eb3f52018-12-04 16:17:50 -0800785 sensor->c1 =
786 loadVariant<double>(entry.second, "C1") / 100;
787 sensor->c2 =
788 loadVariant<double>(entry.second, "C2") / 100;
789 sensor->tachMinPercent =
790 loadVariant<double>(entry.second,
791 "TachMinPercent") /
James Feistbc896df2018-11-26 16:28:17 -0800792 100;
James Feistb2eb3f52018-12-04 16:17:50 -0800793 sensor->tachMaxPercent =
794 loadVariant<double>(entry.second,
795 "TachMaxPercent") /
James Feistbc896df2018-11-26 16:28:17 -0800796 100;
James Feist13452092019-03-07 16:38:12 -0800797 sensor->createMaxCFMIface();
James Feistbc896df2018-11-26 16:28:17 -0800798
James Feistb2eb3f52018-12-04 16:17:50 -0800799 cfmSensors.emplace_back(std::move(sensor));
James Feistbc896df2018-11-26 16:28:17 -0800800 }
801 }
802 }
James Feistb2eb3f52018-12-04 16:17:50 -0800803 if (exitAirSensor)
James Feistbc896df2018-11-26 16:28:17 -0800804 {
James Feistb2eb3f52018-12-04 16:17:50 -0800805 exitAirSensor->cfmSensors = std::move(cfmSensors);
James Feistbc896df2018-11-26 16:28:17 -0800806
James Feistb2eb3f52018-12-04 16:17:50 -0800807 // todo: when power sensors are done delete this fake
808 // reading
809 exitAirSensor->powerReadings["foo"] = 144.0;
James Feistbc896df2018-11-26 16:28:17 -0800810
James Feistb2eb3f52018-12-04 16:17:50 -0800811 exitAirSensor->updateReading();
James Feistbc896df2018-11-26 16:28:17 -0800812 }
813 },
814 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
815 "GetManagedObjects");
816}
817
818int main(int argc, char** argv)
819{
820
821 boost::asio::io_service io;
822 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
823 systemBus->request_name("xyz.openbmc_project.ExitAirTempSensor");
824 sdbusplus::asio::object_server objectServer(systemBus);
825 std::shared_ptr<ExitAirTempSensor> sensor =
826 nullptr; // wait until we find the config
827 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
828
829 io.post([&]() { createSensor(objectServer, sensor, systemBus); });
830
831 boost::asio::deadline_timer configTimer(io);
832
833 std::function<void(sdbusplus::message::message&)> eventHandler =
834 [&](sdbusplus::message::message& message) {
835 configTimer.expires_from_now(boost::posix_time::seconds(1));
836 // create a timer because normally multiple properties change
837 configTimer.async_wait([&](const boost::system::error_code& ec) {
838 if (ec == boost::asio::error::operation_aborted)
839 {
840 return; // we're being canceled
841 }
842 createSensor(objectServer, sensor, systemBus);
843 if (!sensor)
844 {
845 std::cout << "Configuration not detected\n";
846 }
847 });
848 };
849 constexpr const std::array<const char*, 2> monitorIfaces = {exitAirIface,
850 cfmIface};
851 for (const char* type : monitorIfaces)
852 {
853 auto match = std::make_unique<sdbusplus::bus::match::match>(
854 static_cast<sdbusplus::bus::bus&>(*systemBus),
855 "type='signal',member='PropertiesChanged',path_namespace='" +
856 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
857 eventHandler);
858 matches.emplace_back(std::move(match));
859 }
860
861 io.run();
862}