blob: 66ef7f8a9729cd1ce07c4875bb350833a4d2f217 [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
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*/
Patrick Venture7e952d92020-10-05 15:58:52 -070016#include "dbusconfiguration.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070017
Patrick Venture07716592018-10-14 11:46:40 -070018#include "conf.hpp"
Patrick Ventureef1f8862020-08-17 09:34:35 -070019#include "dbushelper.hpp"
20#include "dbusutil.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070021#include "util.hpp"
Patrick Venture07716592018-10-14 11:46:40 -070022
James Feist1fe08952019-05-07 09:17:16 -070023#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070024#include <sdbusplus/bus.hpp>
25#include <sdbusplus/bus/match.hpp>
26#include <sdbusplus/exception.hpp>
27
28#include <algorithm>
James Feist64f072a2018-08-10 16:39:24 -070029#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070030#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070031#include <iostream>
James Feist1fe08952019-05-07 09:17:16 -070032#include <list>
James Feist7136a5a2018-07-19 09:52:05 -070033#include <set>
34#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080035#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070036
Patrick Venturea0764872020-08-08 07:48:43 -070037namespace pid_control
38{
39
Patrick Venturee2ec0f62018-09-04 12:30:27 -070040constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070041 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070042constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070043 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070044constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070045 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070046constexpr const char* stepwiseConfigurationInterface =
47 "xyz.openbmc_project.Configuration.Stepwise";
James Feistf0096a02019-02-21 11:25:22 -080048constexpr const char* thermalControlIface =
49 "xyz.openbmc_project.Control.ThermalMode";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070050constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
Patrick Venture0911bfe2020-08-10 12:51:40 -070051constexpr const char* defaultPwmInterface =
52 "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070053
James Feist991ebd82020-07-21 11:14:52 -070054using Association = std::tuple<std::string, std::string, std::string>;
55using Associations = std::vector<Association>;
56
James Feist5ec20272019-07-10 11:59:57 -070057namespace thresholds
58{
59constexpr const char* warningInterface =
60 "xyz.openbmc_project.Sensor.Threshold.Warning";
61constexpr const char* criticalInterface =
62 "xyz.openbmc_project.Sensor.Threshold.Critical";
63const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh",
64 "WarningLow", "WarningHigh"};
65
66} // namespace thresholds
67
James Feist7136a5a2018-07-19 09:52:05 -070068namespace dbus_configuration
69{
Jason Lingf3b04fd2020-07-24 09:33:04 -070070using SensorInterfaceType = std::pair<std::string, std::string>;
71
72inline std::string getSensorNameFromPath(const std::string& dbusPath)
73{
74 return dbusPath.substr(dbusPath.find_last_of("/") + 1);
75}
76
77inline std::string sensorNameToDbusName(const std::string& sensorName)
78{
79 std::string retString = sensorName;
80 std::replace(retString.begin(), retString.end(), ' ', '_');
81 return retString;
82}
James Feist5ec20272019-07-10 11:59:57 -070083
Patrick Williamsb228bc32022-07-22 19:26:56 -050084std::vector<std::string> getSelectedProfiles(sdbusplus::bus_t& bus)
James Feistf0096a02019-02-21 11:25:22 -080085{
86 std::vector<std::string> ret;
87 auto mapper =
88 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
89 "/xyz/openbmc_project/object_mapper",
90 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
91 mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface});
92 std::unordered_map<
93 std::string, std::unordered_map<std::string, std::vector<std::string>>>
94 respData;
95
96 try
97 {
98 auto resp = bus.call(mapper);
99 resp.read(respData);
100 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500101 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800102 {
103 // can't do anything without mapper call data
104 throw std::runtime_error("ObjectMapper Call Failure");
105 }
106 if (respData.empty())
107 {
108 // if the user has profiles but doesn't expose the interface to select
109 // one, just go ahead without using profiles
110 return ret;
111 }
112
113 // assumption is that we should only have a small handful of selected
114 // profiles at a time (probably only 1), so calling each individually should
115 // not incur a large cost
116 for (const auto& objectPair : respData)
117 {
118 const std::string& path = objectPair.first;
119 for (const auto& ownerPair : objectPair.second)
120 {
121 const std::string& busName = ownerPair.first;
122 auto getProfile =
123 bus.new_method_call(busName.c_str(), path.c_str(),
124 "org.freedesktop.DBus.Properties", "Get");
125 getProfile.append(thermalControlIface, "Current");
126 std::variant<std::string> variantResp;
127 try
128 {
129 auto resp = bus.call(getProfile);
130 resp.read(variantResp);
131 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500132 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800133 {
134 throw std::runtime_error("Failure getting profile");
135 }
136 std::string mode = std::get<std::string>(variantResp);
137 ret.emplace_back(std::move(mode));
138 }
139 }
Patrick Venture39199b42020-10-08 14:40:29 -0700140 if constexpr (pid_control::conf::DEBUG)
James Feistf0096a02019-02-21 11:25:22 -0800141 {
142 std::cout << "Profiles selected: ";
143 for (const auto& profile : ret)
144 {
145 std::cout << profile << " ";
146 }
147 std::cout << "\n";
148 }
149 return ret;
150}
151
James Feist991ebd82020-07-21 11:14:52 -0700152int eventHandler(sd_bus_message* m, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700153{
James Feist1fe08952019-05-07 09:17:16 -0700154
James Feist991ebd82020-07-21 11:14:52 -0700155 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700156 {
157 throw std::runtime_error("Invalid match");
158 }
James Feist991ebd82020-07-21 11:14:52 -0700159
160 // we skip associations because the mapper populates these, not the sensors
161 const std::array<const char*, 1> skipList = {
162 "xyz.openbmc_project.Association"};
163
Patrick Williamsb228bc32022-07-22 19:26:56 -0500164 sdbusplus::message_t message(m);
James Feist991ebd82020-07-21 11:14:52 -0700165 if (std::string(message.get_member()) == "InterfacesAdded")
166 {
167 sdbusplus::message::object_path path;
168 std::unordered_map<
169 std::string,
170 std::unordered_map<std::string, std::variant<Associations, bool>>>
171 data;
172
173 message.read(path, data);
174
175 for (const char* skip : skipList)
176 {
177 auto find = data.find(skip);
178 if (find != data.end())
179 {
180 data.erase(find);
181 if (data.empty())
182 {
183 return 1;
184 }
185 }
186 }
187 }
188
James Feist1fe08952019-05-07 09:17:16 -0700189 boost::asio::steady_timer* timer =
190 static_cast<boost::asio::steady_timer*>(context);
191
192 // do a brief sleep as we tend to get a bunch of these events at
193 // once
194 timer->expires_after(std::chrono::seconds(2));
195 timer->async_wait([](const boost::system::error_code ec) {
196 if (ec == boost::asio::error::operation_aborted)
197 {
198 /* another timer started*/
199 return;
200 }
201
202 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800203 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700204 });
205
206 return 1;
207}
208
Patrick Williamsb228bc32022-07-22 19:26:56 -0500209void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer)
James Feist1fe08952019-05-07 09:17:16 -0700210{
211 // this is a list because the matches can't be moved
Patrick Williamsb228bc32022-07-22 19:26:56 -0500212 static std::list<sdbusplus::bus::match_t> matches;
James Feist1fe08952019-05-07 09:17:16 -0700213
James Feist3987c8b2019-05-13 10:43:17 -0700214 const std::array<std::string, 4> interfaces = {
215 thermalControlIface, pidConfigurationInterface,
216 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700217
218 // this list only needs to be created once
219 if (!matches.empty())
220 {
221 return;
222 }
223
224 // we restart when the configuration changes or there are new sensors
225 for (const auto& interface : interfaces)
226 {
227 matches.emplace_back(
228 bus,
229 "type='signal',member='PropertiesChanged',arg0namespace='" +
230 interface + "'",
231 eventHandler, &timer);
232 }
233 matches.emplace_back(
234 bus,
235 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
236 "sensors/'",
237 eventHandler, &timer);
238}
239
Jason Ling6fc301f2020-07-23 12:39:57 -0700240/**
241 * retrieve an attribute from the pid configuration map
242 * @param[in] base - the PID configuration map, keys are the attributes and
243 * value is the variant associated with that attribute.
244 * @param attributeName - the name of the attribute
245 * @return a variant holding the value associated with a key
246 * @throw runtime_error : attributeName is not in base
247 */
248inline DbusVariantType getPIDAttribute(
249 const std::unordered_map<std::string, DbusVariantType>& base,
250 const std::string& attributeName)
251{
252 auto search = base.find(attributeName);
253 if (search == base.end())
254 {
255 throw std::runtime_error("missing attribute " + attributeName);
256 }
257 return search->second;
258}
259
Harvey Wu239aa7d2022-11-18 08:43:34 +0800260inline void getCycleTimeSetting(
261 const std::unordered_map<std::string, DbusVariantType>& zone,
262 const int zoneIndex, const std::string& attributeName, uint64_t& value)
263{
264 auto findAttributeName = zone.find(attributeName);
265 if (findAttributeName != zone.end())
266 {
267 double tmpAttributeValue =
268 std::visit(VariantToDoubleVisitor(), zone.at(attributeName));
269 if (tmpAttributeValue >= 1.0)
270 {
271 value = static_cast<uint64_t>(tmpAttributeValue);
272 }
273 else
274 {
275 std::cerr << "Zone " << zoneIndex << ": " << attributeName
276 << " is invalid. Use default " << value << " ms\n";
277 }
278 }
279 else
280 {
281 std::cerr << "Zone " << zoneIndex << ": " << attributeName
282 << " cannot find setting. Use default " << value << " ms\n";
283 }
284}
285
James Feist5ec20272019-07-10 11:59:57 -0700286void populatePidInfo(
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800287 [[maybe_unused]] sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700288 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700289 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700290 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700291{
Jason Ling6fc301f2020-07-23 12:39:57 -0700292 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700293 if (info.type == "fan")
294 {
295 info.setpoint = 0;
296 }
297 else
298 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700299 info.setpoint = std::visit(VariantToDoubleVisitor(),
300 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700301 }
302
303 if (thresholdProperty != nullptr)
304 {
305 std::string interface;
306 if (*thresholdProperty == "WarningHigh" ||
307 *thresholdProperty == "WarningLow")
308 {
309 interface = thresholds::warningInterface;
310 }
311 else
312 {
313 interface = thresholds::criticalInterface;
314 }
Patrick Venture73823182020-10-08 15:12:51 -0700315 const std::string& path = sensorConfig.at(info.inputs.front()).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700316
Patrick Venture8729eb92020-08-10 10:38:44 -0700317 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700318 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700319 double reading = 0;
320 try
321 {
Patrick Venture9b936922020-08-10 11:28:39 -0700322 helper.getProperty(service, path, interface, *thresholdProperty,
323 reading);
James Feist5ec20272019-07-10 11:59:57 -0700324 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500325 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700326 {
327 // unsupported threshold, leaving reading at 0
328 }
329
330 info.setpoint += reading;
331 }
332
333 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700334 info.pidInfo.proportionalCoeff = std::visit(
335 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
336 info.pidInfo.integralCoeff = std::visit(
337 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
Josh Lehanc612c052022-12-12 09:56:47 -0800338 // DCoefficient is below, it is optional, same reason as in buildjson.cpp
Jason Ling6fc301f2020-07-23 12:39:57 -0700339 info.pidInfo.feedFwdOffset = std::visit(
340 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
341 info.pidInfo.feedFwdGain = std::visit(
342 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
343 info.pidInfo.integralLimit.max = std::visit(
344 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
345 info.pidInfo.integralLimit.min = std::visit(
346 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
347 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
348 getPIDAttribute(base, "OutLimitMax"));
349 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
350 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700351 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700352 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700353 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700354 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
Josh Lehanc612c052022-12-12 09:56:47 -0800355
James Feist5ec20272019-07-10 11:59:57 -0700356 double negativeHysteresis = 0;
357 double positiveHysteresis = 0;
Josh Lehanc612c052022-12-12 09:56:47 -0800358 double derivativeCoeff = 0;
James Feist5ec20272019-07-10 11:59:57 -0700359
360 auto findNeg = base.find("NegativeHysteresis");
361 auto findPos = base.find("PositiveHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -0800362 auto findDerivative = base.find("DCoefficient");
James Feist5ec20272019-07-10 11:59:57 -0700363
364 if (findNeg != base.end())
365 {
366 negativeHysteresis =
367 std::visit(VariantToDoubleVisitor(), findNeg->second);
368 }
James Feist5ec20272019-07-10 11:59:57 -0700369 if (findPos != base.end())
370 {
371 positiveHysteresis =
372 std::visit(VariantToDoubleVisitor(), findPos->second);
373 }
Josh Lehanc612c052022-12-12 09:56:47 -0800374 if (findDerivative != base.end())
375 {
376 derivativeCoeff =
377 std::visit(VariantToDoubleVisitor(), findDerivative->second);
378 }
379
James Feist5ec20272019-07-10 11:59:57 -0700380 info.pidInfo.negativeHysteresis = negativeHysteresis;
381 info.pidInfo.positiveHysteresis = positiveHysteresis;
Josh Lehanc612c052022-12-12 09:56:47 -0800382 info.pidInfo.derivativeCoeff = derivativeCoeff;
James Feist5ec20272019-07-10 11:59:57 -0700383}
384
Patrick Williamsb228bc32022-07-22 19:26:56 -0500385bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700386 std::map<std::string, conf::SensorConfig>& sensorConfig,
387 std::map<int64_t, conf::PIDConf>& zoneConfig,
388 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700389{
390
391 sensorConfig.clear();
392 zoneConfig.clear();
393 zoneDetailsConfig.clear();
394
395 createMatches(bus, timer);
396
James Feist7136a5a2018-07-19 09:52:05 -0700397 auto mapper =
398 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
399 "/xyz/openbmc_project/object_mapper",
400 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700401 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700402 std::array<const char*, 6>{
403 objectManagerInterface, pidConfigurationInterface,
404 pidZoneConfigurationInterface,
405 stepwiseConfigurationInterface, sensorInterface,
406 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700407 std::unordered_map<
408 std::string, std::unordered_map<std::string, std::vector<std::string>>>
409 respData;
James Feist22c257a2018-08-31 14:07:12 -0700410 try
411 {
412 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700413 resp.read(respData);
414 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500415 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700416 {
417 // can't do anything without mapper call data
418 throw std::runtime_error("ObjectMapper Call Failure");
419 }
James Feist7136a5a2018-07-19 09:52:05 -0700420
James Feist7136a5a2018-07-19 09:52:05 -0700421 if (respData.empty())
422 {
James Feist22c257a2018-08-31 14:07:12 -0700423 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700424 throw std::runtime_error("No configuration data available from Mapper");
425 }
426 // create a map of pair of <has pid configuration, ObjectManager path>
427 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
428 // and a map of <path, interface> for sensors
429 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700430 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700431 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700432 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700433 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700434 auto& owner = owners[ownerPair.first];
435 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700436 {
437
438 if (interface == objectManagerInterface)
439 {
440 owner.second = objectPair.first;
441 }
442 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700443 interface == pidZoneConfigurationInterface ||
444 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700445 {
446 owner.first = true;
447 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700448 if (interface == sensorInterface ||
449 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700450 {
451 // we're not interested in pwm sensors, just pwm control
452 if (interface == sensorInterface &&
453 objectPair.first.find("pwm") != std::string::npos)
454 {
455 continue;
456 }
457 sensors[objectPair.first] = interface;
458 }
459 }
460 }
461 }
462 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700463 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700464 {
465 // skip if no pid configuration (means probably a sensor)
466 if (!owner.second.first)
467 {
468 continue;
469 }
470 auto endpoint = bus.new_method_call(
471 owner.first.c_str(), owner.second.second.c_str(),
472 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700473 ManagedObjectType configuration;
474 try
James Feist7136a5a2018-07-19 09:52:05 -0700475 {
James Feist22c257a2018-08-31 14:07:12 -0700476 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700477 responce.read(configuration);
478 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500479 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700480 {
481 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700482 throw std::runtime_error("Error getting managed objects from " +
483 owner.first);
484 }
James Feist22c257a2018-08-31 14:07:12 -0700485
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700486 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700487 {
488 if (pathPair.second.find(pidConfigurationInterface) !=
489 pathPair.second.end() ||
490 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700491 pathPair.second.end() ||
492 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700493 pathPair.second.end())
494 {
495 configurations.emplace(pathPair);
496 }
James Feistf0096a02019-02-21 11:25:22 -0800497 }
498 }
499
500 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700501 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
502 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800503 {
James Feist3987c8b2019-05-13 10:43:17 -0700504 for (auto pathIt = configurations.begin();
505 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800506 {
James Feist3987c8b2019-05-13 10:43:17 -0700507 for (auto confIt = pathIt->second.begin();
508 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800509 {
James Feist3987c8b2019-05-13 10:43:17 -0700510 auto profilesFind = confIt->second.find("Profiles");
511 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800512 {
James Feist3987c8b2019-05-13 10:43:17 -0700513 confIt++;
514 continue; // if no profiles selected, apply always
515 }
516 auto profiles =
517 std::get<std::vector<std::string>>(profilesFind->second);
518 if (profiles.empty())
519 {
520 confIt++;
521 continue;
522 }
523
524 bool found = false;
525 for (const std::string& profile : profiles)
526 {
527 if (std::find(selectedProfiles.begin(),
528 selectedProfiles.end(),
529 profile) != selectedProfiles.end())
530 {
531 found = true;
532 break;
533 }
534 }
535 if (found)
536 {
537 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800538 }
539 else
540 {
James Feist3987c8b2019-05-13 10:43:17 -0700541 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800542 }
543 }
James Feist3987c8b2019-05-13 10:43:17 -0700544 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800545 {
James Feist3987c8b2019-05-13 10:43:17 -0700546 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800547 }
James Feist3987c8b2019-05-13 10:43:17 -0700548 else
James Feistf0096a02019-02-21 11:25:22 -0800549 {
James Feist3987c8b2019-05-13 10:43:17 -0700550 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800551 }
James Feist7136a5a2018-07-19 09:52:05 -0700552 }
553 }
James Feist8c3c51e2018-08-08 16:31:43 -0700554
Josh Lehan998fbe62020-09-20 21:21:05 -0700555 // On D-Bus, although not necessary,
556 // having the "zoneID" field can still be useful,
557 // as it is used for diagnostic messages,
558 // logging file names, and so on.
559 // Accept optional "ZoneIndex" parameter to explicitly specify.
560 // If not present, or not unique, auto-assign index,
561 // using 0-based numbering, ensuring uniqueness.
562 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700563 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700564 {
565 auto findZone =
566 configuration.second.find(pidZoneConfigurationInterface);
567 if (findZone != configuration.second.end())
568 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700569 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800570
James Feist1f802f52019-02-08 13:51:43 -0800571 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700572
573 auto findZoneIndex = zone.find("ZoneIndex");
574 if (findZoneIndex == zone.end())
575 {
576 continue;
577 }
578
579 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
580 if (!ptrZoneIndex)
581 {
582 continue;
583 }
584
585 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
586 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
587 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
588 << "\n";
589 }
590 }
591
592 for (const auto& configuration : configurations)
593 {
594 auto findZone =
595 configuration.second.find(pidZoneConfigurationInterface);
596 if (findZone != configuration.second.end())
597 {
598 const auto& zone = findZone->second;
599
600 const std::string& name = std::get<std::string>(zone.at("Name"));
601
602 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700603
Patrick Venturec54fbd82018-10-30 19:40:05 -0700604 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700605
James Feist3484bed2019-02-25 13:28:18 -0800606 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
607 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800608 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800609 zone.at("FailSafePercent"));
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800610
Harvey Wu239aa7d2022-11-18 08:43:34 +0800611 getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
612 details.cycleTime.cycleIntervalTimeMS);
613 getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
614 details.cycleTime.updateThermalsTimeMS);
James Feist7136a5a2018-07-19 09:52:05 -0700615 }
616 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700617 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700618 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700619 {
James Feist22c257a2018-08-31 14:07:12 -0700620 const auto& base =
621 configuration.second.at(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700622 const std::string pidName = std::get<std::string>(base.at("Name"));
623 const std::string pidClass =
624 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700625 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800626 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700627 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700628 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700629 auto index = getZoneIndex(zone, foundZones);
630
James Feistf81f2882019-02-26 11:26:36 -0800631 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700632 std::vector<std::string> inputSensorNames(
633 std::get<std::vector<std::string>>(base.at("Inputs")));
634 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700635
Jason Lingf3b04fd2020-07-24 09:33:04 -0700636 // assumption: all fan pids must have at least one output
637 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700638 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700639 outputSensorNames = std::get<std::vector<std::string>>(
640 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700641 }
James Feist1738e2a2019-02-04 15:57:03 -0800642
Alex.Song8f73ad72021-10-07 00:18:27 +0800643 bool unavailableAsFailed = true;
644 auto findUnavailableAsFailed =
645 base.find("InputUnavailableAsFailed");
646 if (findUnavailableAsFailed != base.end())
647 {
648 unavailableAsFailed =
649 std::get<bool>(findUnavailableAsFailed->second);
650 }
651
Jason Lingf3b04fd2020-07-24 09:33:04 -0700652 std::vector<SensorInterfaceType> inputSensorInterfaces;
653 std::vector<SensorInterfaceType> outputSensorInterfaces;
654 /* populate an interface list for different sensor direction
655 * types (input,output)
656 */
657 /* take the Inputs from the configuration and generate
658 * a list of dbus descriptors (path, interface).
659 * Mapping can be many-to-one since an element of Inputs can be
660 * a regex
661 */
662 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700663 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700664 findSensors(sensors, sensorNameToDbusName(sensorName),
665 inputSensorInterfaces);
666 }
667 for (const std::string& sensorName : outputSensorNames)
668 {
669 findSensors(sensors, sensorNameToDbusName(sensorName),
670 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800671 }
James Feist50fdfe32018-09-24 15:51:09 -0700672
Jason Lingf3b04fd2020-07-24 09:33:04 -0700673 inputSensorNames.clear();
674 for (const SensorInterfaceType& inputSensorInterface :
675 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800676 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700677 const std::string& dbusInterface =
678 inputSensorInterface.second;
679 const std::string& inputSensorPath =
680 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700681
682 // Setting timeout to 0 is intentional, as D-Bus passive
683 // sensor updates are pushed in, not pulled by timer poll.
684 // Setting ignoreDbusMinMax is intentional, as this
685 // prevents normalization of values to [0.0, 1.0] range,
686 // which would mess up the PID loop math.
687 // All non-fan PID classes should be initialized this way.
688 // As for why a fan should not use this code path, see
689 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
Josh Lehan23e22b92022-11-12 22:37:58 -0800690 if ((pidClass == "temp") || (pidClass == "margin") ||
691 (pidClass == "power") || (pidClass == "powersum"))
James Feist50fdfe32018-09-24 15:51:09 -0700692 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800693 std::string inputSensorName =
694 getSensorNameFromPath(inputSensorPath);
695 auto& config = sensorConfig[inputSensorName];
696 inputSensorNames.push_back(inputSensorName);
697 config.type = pidClass;
698 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700699 config.timeout = 0;
700 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800701 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700702 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700703
Jason Lingf3b04fd2020-07-24 09:33:04 -0700704 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700705 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700706 /* all expected inputs in the configuration are expected
707 * to be sensor interfaces
708 */
709 throw std::runtime_error(
710 "sensor at dbus path [" + inputSensorPath +
711 "] has an interface [" + dbusInterface +
712 "] that does not match the expected interface of " +
713 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700714 }
715 }
James Feist1738e2a2019-02-04 15:57:03 -0800716
Jason Lingf3b04fd2020-07-24 09:33:04 -0700717 /* fan pids need to pair up tach sensors with their pwm
718 * counterparts
719 */
720 if (pidClass == "fan")
721 {
722 /* If a PID is a fan there should be either
723 * (1) one output(pwm) per input(tach)
724 * OR
725 * (2) one putput(pwm) for all inputs(tach)
726 * everything else indicates a bad configuration.
727 */
728 bool singlePwm = false;
729 if (outputSensorInterfaces.size() == 1)
730 {
731 /* one pwm, set write paths for all fan sensors to it */
732 singlePwm = true;
733 }
734 else if (inputSensorInterfaces.size() ==
735 outputSensorInterfaces.size())
736 {
737 /* one to one mapping, each fan sensor gets its own pwm
738 * control */
739 singlePwm = false;
740 }
741 else
742 {
743 throw std::runtime_error(
744 "fan PID has invalid number of Outputs");
745 }
746 std::string fanSensorName;
747 std::string pwmPath;
748 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800749 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700750 if (singlePwm)
751 {
752 /* if just a single output(pwm) is provided then use
753 * that pwm control path for all the fan sensor write
754 * path configs
755 */
756 pwmPath = outputSensorInterfaces.at(0).first;
757 pwmInterface = outputSensorInterfaces.at(0).second;
758 }
759 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
760 idx++)
761 {
762 if (!singlePwm)
763 {
764 pwmPath = outputSensorInterfaces.at(idx).first;
765 pwmInterface =
766 outputSensorInterfaces.at(idx).second;
767 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700768 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700769 {
770 throw std::runtime_error(
771 "fan pwm control at dbus path [" + pwmPath +
772 "] has an interface [" + pwmInterface +
773 "] that does not match the expected interface "
774 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700775 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700776 }
777 const std::string& fanPath =
778 inputSensorInterfaces.at(idx).first;
779 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800780 pwmSensorName = getSensorNameFromPath(pwmPath);
781 std::string fanPwmIndex = fanSensorName + pwmSensorName;
782 inputSensorNames.push_back(fanPwmIndex);
783 auto& fanConfig = sensorConfig[fanPwmIndex];
784 fanConfig.type = pidClass;
785 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700786 fanConfig.writePath = pwmPath;
787 // todo: un-hardcode this if there are fans with
788 // different ranges
789 fanConfig.max = 255;
790 fanConfig.min = 0;
791 }
792 }
James Feist11d243d2019-06-24 16:18:40 -0700793 // if the sensors aren't available in the current state, don't
794 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700795 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700796 {
797 continue;
798 }
799
James Feist5ec20272019-07-10 11:59:57 -0700800 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700801
James Feist5ec20272019-07-10 11:59:57 -0700802 // SetPointOffset is a threshold value to pull from the sensor
803 // to apply an offset. For upper thresholds this means the
804 // setpoint is usually negative.
805 auto findSetpointOffset = base.find("SetPointOffset");
806 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700807 {
James Feist5ec20272019-07-10 11:59:57 -0700808 offsetType =
809 std::get<std::string>(findSetpointOffset->second);
810 if (std::find(thresholds::types.begin(),
811 thresholds::types.end(),
812 offsetType) == thresholds::types.end())
813 {
814 throw std::runtime_error("Unsupported type: " +
815 offsetType);
816 }
817 }
818
819 if (offsetType.empty())
820 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700821 conf::ControllerInfo& info =
James Feist5ec20272019-07-10 11:59:57 -0700822 conf[std::get<std::string>(base.at("Name"))];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700823 info.inputs = std::move(inputSensorNames);
Patrick Venture73823182020-10-08 15:12:51 -0700824 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -0700825 }
826 else
827 {
James Feist5ec20272019-07-10 11:59:57 -0700828 // we have to split up the inputs, as in practice t-control
829 // values will differ, making setpoints differ
Jason Lingf3b04fd2020-07-24 09:33:04 -0700830 for (const std::string& input : inputSensorNames)
James Feist5ec20272019-07-10 11:59:57 -0700831 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700832 conf::ControllerInfo& info = conf[input];
James Feist5ec20272019-07-10 11:59:57 -0700833 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -0700834 populatePidInfo(bus, base, info, &offsetType,
835 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -0700836 }
James Feist22c257a2018-08-31 14:07:12 -0700837 }
James Feist22c257a2018-08-31 14:07:12 -0700838 }
839 }
840 auto findStepwise =
841 configuration.second.find(stepwiseConfigurationInterface);
842 if (findStepwise != configuration.second.end())
843 {
844 const auto& base = findStepwise->second;
845 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800846 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700847 for (const std::string& zone : zones)
848 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700849 auto index = getZoneIndex(zone, foundZones);
850
James Feistf81f2882019-02-26 11:26:36 -0800851 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700852
853 std::vector<std::string> inputs;
854 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800855 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700856
Alex.Song8f73ad72021-10-07 00:18:27 +0800857 bool unavailableAsFailed = true;
858 auto findUnavailableAsFailed =
859 base.find("InputUnavailableAsFailed");
860 if (findUnavailableAsFailed != base.end())
861 {
862 unavailableAsFailed =
863 std::get<bool>(findUnavailableAsFailed->second);
864 }
865
James Feist1738e2a2019-02-04 15:57:03 -0800866 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700867 for (const std::string& sensorName : sensorNames)
868 {
James Feist1738e2a2019-02-04 15:57:03 -0800869 std::vector<std::pair<std::string, std::string>>
870 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700871 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
872 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700873 {
James Feist50fdfe32018-09-24 15:51:09 -0700874 break;
875 }
876
James Feist1738e2a2019-02-04 15:57:03 -0800877 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
878 {
879 size_t idx =
880 sensorPathIfacePair.first.find_last_of("/") + 1;
881 std::string shortName =
882 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700883
James Feist1738e2a2019-02-04 15:57:03 -0800884 inputs.push_back(shortName);
885 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800886 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800887 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800888 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800889 config.unavailableAsFailed = unavailableAsFailed;
James Feist1738e2a2019-02-04 15:57:03 -0800890 // todo: maybe un-hardcode this if we run into slower
891 // timeouts with sensors
892
James Feist2642cb52019-02-25 13:00:16 -0800893 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800894 sensorFound = true;
895 }
James Feist50fdfe32018-09-24 15:51:09 -0700896 }
897 if (!sensorFound)
898 {
899 continue;
900 }
Patrick Venture1df9e872020-10-08 15:35:01 -0700901 conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800902 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700903 info.inputs = std::move(inputs);
904
James Feist22c257a2018-08-31 14:07:12 -0700905 info.type = "stepwise";
906 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700907 info.stepwiseInfo.positiveHysteresis = 0.0;
908 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800909 std::string subtype = std::get<std::string>(base.at("Class"));
910
911 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700912 auto findPosHyst = base.find("PositiveHysteresis");
913 auto findNegHyst = base.find("NegativeHysteresis");
914 if (findPosHyst != base.end())
915 {
James Feist1f802f52019-02-08 13:51:43 -0800916 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800917 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700918 }
919 if (findNegHyst != base.end())
920 {
James Feist5782ab82019-04-02 08:38:48 -0700921 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800922 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700923 }
James Feist22c257a2018-08-31 14:07:12 -0700924 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800925 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700926 if (readings.size() > ec::maxStepwisePoints)
927 {
928 throw std::invalid_argument("Too many stepwise points.");
929 }
930 if (readings.empty())
931 {
932 throw std::invalid_argument(
933 "Must have one stepwise point.");
934 }
935 std::copy(readings.begin(), readings.end(),
936 info.stepwiseInfo.reading);
937 if (readings.size() < ec::maxStepwisePoints)
938 {
939 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800940 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700941 }
942 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800943 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700944 if (readings.size() != outputs.size())
945 {
946 throw std::invalid_argument(
947 "Outputs size must match readings");
948 }
949 std::copy(outputs.begin(), outputs.end(),
950 info.stepwiseInfo.output);
951 if (outputs.size() < ec::maxStepwisePoints)
952 {
953 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800954 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700955 }
James Feist7136a5a2018-07-19 09:52:05 -0700956 }
957 }
958 }
Patrick Venture39199b42020-10-08 14:40:29 -0700959 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700960 {
Patrick Venture39199b42020-10-08 14:40:29 -0700961 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -0700962 }
James Feistc959c422018-11-01 12:33:40 -0700963 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700964 {
James Feist1fe08952019-05-07 09:17:16 -0700965 std::cerr
966 << "No fan zones, application pausing until new configuration\n";
967 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700968 }
James Feist1fe08952019-05-07 09:17:16 -0700969 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700970}
Patrick Venturea0764872020-08-08 07:48:43 -0700971
James Feist7136a5a2018-07-19 09:52:05 -0700972} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -0700973} // namespace pid_control