blob: f055c1120c6b2fc48f8a448ec5210ce5be5f5c7d [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
Josh Lehan10e46ef2023-02-01 18:25:58 -0800161 const std::array<const char*, 2> skipList = {
162 "xyz.openbmc_project.Association",
163 "xyz.openbmc_project.Association.Definitions"};
James Feist991ebd82020-07-21 11:14:52 -0700164
Patrick Williamsb228bc32022-07-22 19:26:56 -0500165 sdbusplus::message_t message(m);
James Feist991ebd82020-07-21 11:14:52 -0700166 if (std::string(message.get_member()) == "InterfacesAdded")
167 {
168 sdbusplus::message::object_path path;
169 std::unordered_map<
170 std::string,
171 std::unordered_map<std::string, std::variant<Associations, bool>>>
172 data;
173
174 message.read(path, data);
175
176 for (const char* skip : skipList)
177 {
178 auto find = data.find(skip);
179 if (find != data.end())
180 {
181 data.erase(find);
182 if (data.empty())
183 {
184 return 1;
185 }
186 }
187 }
Josh Lehan10e46ef2023-02-01 18:25:58 -0800188
189 if constexpr (pid_control::conf::DEBUG)
190 {
191 std::cout << "New config detected: " << path.str << std::endl;
192 for (auto& d : data)
193 {
194 std::cout << "\tdata is " << d.first << std::endl;
195 for (auto& second : d.second)
196 {
197 std::cout << "\t\tdata is " << second.first << std::endl;
198 }
199 }
200 }
James Feist991ebd82020-07-21 11:14:52 -0700201 }
202
James Feist1fe08952019-05-07 09:17:16 -0700203 boost::asio::steady_timer* timer =
204 static_cast<boost::asio::steady_timer*>(context);
205
206 // do a brief sleep as we tend to get a bunch of these events at
207 // once
208 timer->expires_after(std::chrono::seconds(2));
209 timer->async_wait([](const boost::system::error_code ec) {
210 if (ec == boost::asio::error::operation_aborted)
211 {
212 /* another timer started*/
213 return;
214 }
215
216 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800217 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700218 });
219
220 return 1;
221}
222
Patrick Williamsb228bc32022-07-22 19:26:56 -0500223void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer)
James Feist1fe08952019-05-07 09:17:16 -0700224{
225 // this is a list because the matches can't be moved
Patrick Williamsb228bc32022-07-22 19:26:56 -0500226 static std::list<sdbusplus::bus::match_t> matches;
James Feist1fe08952019-05-07 09:17:16 -0700227
James Feist3987c8b2019-05-13 10:43:17 -0700228 const std::array<std::string, 4> interfaces = {
229 thermalControlIface, pidConfigurationInterface,
230 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700231
232 // this list only needs to be created once
233 if (!matches.empty())
234 {
235 return;
236 }
237
238 // we restart when the configuration changes or there are new sensors
239 for (const auto& interface : interfaces)
240 {
241 matches.emplace_back(
242 bus,
243 "type='signal',member='PropertiesChanged',arg0namespace='" +
244 interface + "'",
245 eventHandler, &timer);
246 }
247 matches.emplace_back(
248 bus,
249 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
250 "sensors/'",
251 eventHandler, &timer);
Jinliang Wangc2a311b2023-04-26 18:36:56 +0000252 matches.emplace_back(bus,
253 "type='signal',member='InterfacesRemoved',arg0path='/"
254 "xyz/openbmc_project/sensors/'",
255 eventHandler, &timer);
James Feist1fe08952019-05-07 09:17:16 -0700256}
257
Jason Ling6fc301f2020-07-23 12:39:57 -0700258/**
259 * retrieve an attribute from the pid configuration map
260 * @param[in] base - the PID configuration map, keys are the attributes and
261 * value is the variant associated with that attribute.
262 * @param attributeName - the name of the attribute
263 * @return a variant holding the value associated with a key
264 * @throw runtime_error : attributeName is not in base
265 */
266inline DbusVariantType getPIDAttribute(
267 const std::unordered_map<std::string, DbusVariantType>& base,
268 const std::string& attributeName)
269{
270 auto search = base.find(attributeName);
271 if (search == base.end())
272 {
273 throw std::runtime_error("missing attribute " + attributeName);
274 }
275 return search->second;
276}
277
Harvey Wu239aa7d2022-11-18 08:43:34 +0800278inline void getCycleTimeSetting(
279 const std::unordered_map<std::string, DbusVariantType>& zone,
280 const int zoneIndex, const std::string& attributeName, uint64_t& value)
281{
282 auto findAttributeName = zone.find(attributeName);
283 if (findAttributeName != zone.end())
284 {
285 double tmpAttributeValue =
286 std::visit(VariantToDoubleVisitor(), zone.at(attributeName));
287 if (tmpAttributeValue >= 1.0)
288 {
289 value = static_cast<uint64_t>(tmpAttributeValue);
290 }
291 else
292 {
293 std::cerr << "Zone " << zoneIndex << ": " << attributeName
294 << " is invalid. Use default " << value << " ms\n";
295 }
296 }
297 else
298 {
299 std::cerr << "Zone " << zoneIndex << ": " << attributeName
300 << " cannot find setting. Use default " << value << " ms\n";
301 }
302}
303
James Feist5ec20272019-07-10 11:59:57 -0700304void populatePidInfo(
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800305 [[maybe_unused]] sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700306 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700307 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700308 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700309{
Jason Ling6fc301f2020-07-23 12:39:57 -0700310 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700311 if (info.type == "fan")
312 {
313 info.setpoint = 0;
314 }
315 else
316 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700317 info.setpoint = std::visit(VariantToDoubleVisitor(),
318 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700319 }
320
321 if (thresholdProperty != nullptr)
322 {
323 std::string interface;
324 if (*thresholdProperty == "WarningHigh" ||
325 *thresholdProperty == "WarningLow")
326 {
327 interface = thresholds::warningInterface;
328 }
329 else
330 {
331 interface = thresholds::criticalInterface;
332 }
Patrick Venture73823182020-10-08 15:12:51 -0700333 const std::string& path = sensorConfig.at(info.inputs.front()).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700334
Patrick Venture8729eb92020-08-10 10:38:44 -0700335 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700336 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700337 double reading = 0;
338 try
339 {
Patrick Venture9b936922020-08-10 11:28:39 -0700340 helper.getProperty(service, path, interface, *thresholdProperty,
341 reading);
James Feist5ec20272019-07-10 11:59:57 -0700342 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500343 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700344 {
345 // unsupported threshold, leaving reading at 0
346 }
347
348 info.setpoint += reading;
349 }
350
351 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700352 info.pidInfo.proportionalCoeff = std::visit(
353 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
354 info.pidInfo.integralCoeff = std::visit(
355 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
Josh Lehanc612c052022-12-12 09:56:47 -0800356 // DCoefficient is below, it is optional, same reason as in buildjson.cpp
Jason Ling6fc301f2020-07-23 12:39:57 -0700357 info.pidInfo.feedFwdOffset = std::visit(
358 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
359 info.pidInfo.feedFwdGain = std::visit(
360 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
361 info.pidInfo.integralLimit.max = std::visit(
362 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
363 info.pidInfo.integralLimit.min = std::visit(
364 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
365 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
366 getPIDAttribute(base, "OutLimitMax"));
367 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
368 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700369 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700370 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700371 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700372 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
Josh Lehanc612c052022-12-12 09:56:47 -0800373
James Feist5ec20272019-07-10 11:59:57 -0700374 double negativeHysteresis = 0;
375 double positiveHysteresis = 0;
Josh Lehanc612c052022-12-12 09:56:47 -0800376 double derivativeCoeff = 0;
James Feist5ec20272019-07-10 11:59:57 -0700377
378 auto findNeg = base.find("NegativeHysteresis");
379 auto findPos = base.find("PositiveHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -0800380 auto findDerivative = base.find("DCoefficient");
James Feist5ec20272019-07-10 11:59:57 -0700381
382 if (findNeg != base.end())
383 {
384 negativeHysteresis =
385 std::visit(VariantToDoubleVisitor(), findNeg->second);
386 }
James Feist5ec20272019-07-10 11:59:57 -0700387 if (findPos != base.end())
388 {
389 positiveHysteresis =
390 std::visit(VariantToDoubleVisitor(), findPos->second);
391 }
Josh Lehanc612c052022-12-12 09:56:47 -0800392 if (findDerivative != base.end())
393 {
394 derivativeCoeff =
395 std::visit(VariantToDoubleVisitor(), findDerivative->second);
396 }
397
James Feist5ec20272019-07-10 11:59:57 -0700398 info.pidInfo.negativeHysteresis = negativeHysteresis;
399 info.pidInfo.positiveHysteresis = positiveHysteresis;
Josh Lehanc612c052022-12-12 09:56:47 -0800400 info.pidInfo.derivativeCoeff = derivativeCoeff;
James Feist5ec20272019-07-10 11:59:57 -0700401}
402
Patrick Williamsb228bc32022-07-22 19:26:56 -0500403bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700404 std::map<std::string, conf::SensorConfig>& sensorConfig,
405 std::map<int64_t, conf::PIDConf>& zoneConfig,
406 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700407{
408
409 sensorConfig.clear();
410 zoneConfig.clear();
411 zoneDetailsConfig.clear();
412
413 createMatches(bus, timer);
414
James Feist7136a5a2018-07-19 09:52:05 -0700415 auto mapper =
416 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
417 "/xyz/openbmc_project/object_mapper",
418 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700419 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700420 std::array<const char*, 6>{
421 objectManagerInterface, pidConfigurationInterface,
422 pidZoneConfigurationInterface,
423 stepwiseConfigurationInterface, sensorInterface,
424 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700425 std::unordered_map<
426 std::string, std::unordered_map<std::string, std::vector<std::string>>>
427 respData;
James Feist22c257a2018-08-31 14:07:12 -0700428 try
429 {
430 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700431 resp.read(respData);
432 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500433 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700434 {
435 // can't do anything without mapper call data
436 throw std::runtime_error("ObjectMapper Call Failure");
437 }
James Feist7136a5a2018-07-19 09:52:05 -0700438
James Feist7136a5a2018-07-19 09:52:05 -0700439 if (respData.empty())
440 {
James Feist22c257a2018-08-31 14:07:12 -0700441 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700442 throw std::runtime_error("No configuration data available from Mapper");
443 }
444 // create a map of pair of <has pid configuration, ObjectManager path>
445 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
446 // and a map of <path, interface> for sensors
447 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700448 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700449 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700450 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700451 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700452 auto& owner = owners[ownerPair.first];
453 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700454 {
455
456 if (interface == objectManagerInterface)
457 {
458 owner.second = objectPair.first;
459 }
460 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700461 interface == pidZoneConfigurationInterface ||
462 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700463 {
464 owner.first = true;
465 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700466 if (interface == sensorInterface ||
467 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700468 {
469 // we're not interested in pwm sensors, just pwm control
470 if (interface == sensorInterface &&
471 objectPair.first.find("pwm") != std::string::npos)
472 {
473 continue;
474 }
475 sensors[objectPair.first] = interface;
476 }
477 }
478 }
479 }
480 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700481 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700482 {
483 // skip if no pid configuration (means probably a sensor)
484 if (!owner.second.first)
485 {
486 continue;
487 }
488 auto endpoint = bus.new_method_call(
489 owner.first.c_str(), owner.second.second.c_str(),
490 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700491 ManagedObjectType configuration;
492 try
James Feist7136a5a2018-07-19 09:52:05 -0700493 {
James Feist22c257a2018-08-31 14:07:12 -0700494 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700495 responce.read(configuration);
496 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500497 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700498 {
499 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700500 throw std::runtime_error("Error getting managed objects from " +
501 owner.first);
502 }
James Feist22c257a2018-08-31 14:07:12 -0700503
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700504 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700505 {
506 if (pathPair.second.find(pidConfigurationInterface) !=
507 pathPair.second.end() ||
508 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700509 pathPair.second.end() ||
510 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700511 pathPair.second.end())
512 {
513 configurations.emplace(pathPair);
514 }
James Feistf0096a02019-02-21 11:25:22 -0800515 }
516 }
517
518 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700519 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
520 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800521 {
James Feist3987c8b2019-05-13 10:43:17 -0700522 for (auto pathIt = configurations.begin();
523 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800524 {
James Feist3987c8b2019-05-13 10:43:17 -0700525 for (auto confIt = pathIt->second.begin();
526 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800527 {
James Feist3987c8b2019-05-13 10:43:17 -0700528 auto profilesFind = confIt->second.find("Profiles");
529 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800530 {
James Feist3987c8b2019-05-13 10:43:17 -0700531 confIt++;
532 continue; // if no profiles selected, apply always
533 }
534 auto profiles =
535 std::get<std::vector<std::string>>(profilesFind->second);
536 if (profiles.empty())
537 {
538 confIt++;
539 continue;
540 }
541
542 bool found = false;
543 for (const std::string& profile : profiles)
544 {
545 if (std::find(selectedProfiles.begin(),
546 selectedProfiles.end(),
547 profile) != selectedProfiles.end())
548 {
549 found = true;
550 break;
551 }
552 }
553 if (found)
554 {
555 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800556 }
557 else
558 {
James Feist3987c8b2019-05-13 10:43:17 -0700559 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800560 }
561 }
James Feist3987c8b2019-05-13 10:43:17 -0700562 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800563 {
James Feist3987c8b2019-05-13 10:43:17 -0700564 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800565 }
James Feist3987c8b2019-05-13 10:43:17 -0700566 else
James Feistf0096a02019-02-21 11:25:22 -0800567 {
James Feist3987c8b2019-05-13 10:43:17 -0700568 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800569 }
James Feist7136a5a2018-07-19 09:52:05 -0700570 }
571 }
James Feist8c3c51e2018-08-08 16:31:43 -0700572
Josh Lehan998fbe62020-09-20 21:21:05 -0700573 // On D-Bus, although not necessary,
574 // having the "zoneID" field can still be useful,
575 // as it is used for diagnostic messages,
576 // logging file names, and so on.
577 // Accept optional "ZoneIndex" parameter to explicitly specify.
578 // If not present, or not unique, auto-assign index,
579 // using 0-based numbering, ensuring uniqueness.
580 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700581 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700582 {
583 auto findZone =
584 configuration.second.find(pidZoneConfigurationInterface);
585 if (findZone != configuration.second.end())
586 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700587 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800588
James Feist1f802f52019-02-08 13:51:43 -0800589 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700590
591 auto findZoneIndex = zone.find("ZoneIndex");
592 if (findZoneIndex == zone.end())
593 {
594 continue;
595 }
596
597 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
598 if (!ptrZoneIndex)
599 {
600 continue;
601 }
602
603 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
604 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
605 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
606 << "\n";
607 }
608 }
609
610 for (const auto& configuration : configurations)
611 {
612 auto findZone =
613 configuration.second.find(pidZoneConfigurationInterface);
614 if (findZone != configuration.second.end())
615 {
616 const auto& zone = findZone->second;
617
618 const std::string& name = std::get<std::string>(zone.at("Name"));
619
620 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700621
Patrick Venturec54fbd82018-10-30 19:40:05 -0700622 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700623
James Feist3484bed2019-02-25 13:28:18 -0800624 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
625 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800626 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800627 zone.at("FailSafePercent"));
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800628
Harvey Wu239aa7d2022-11-18 08:43:34 +0800629 getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
630 details.cycleTime.cycleIntervalTimeMS);
631 getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
632 details.cycleTime.updateThermalsTimeMS);
James Feist7136a5a2018-07-19 09:52:05 -0700633 }
634 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700635 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700636 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700637 {
James Feist22c257a2018-08-31 14:07:12 -0700638 const auto& base =
639 configuration.second.at(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700640 const std::string pidName = std::get<std::string>(base.at("Name"));
641 const std::string pidClass =
642 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700643 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800644 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700645 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700646 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700647 auto index = getZoneIndex(zone, foundZones);
648
James Feistf81f2882019-02-26 11:26:36 -0800649 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700650 std::vector<std::string> inputSensorNames(
651 std::get<std::vector<std::string>>(base.at("Inputs")));
652 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700653
Jason Lingf3b04fd2020-07-24 09:33:04 -0700654 // assumption: all fan pids must have at least one output
655 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700656 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700657 outputSensorNames = std::get<std::vector<std::string>>(
658 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700659 }
James Feist1738e2a2019-02-04 15:57:03 -0800660
Alex.Song8f73ad72021-10-07 00:18:27 +0800661 bool unavailableAsFailed = true;
662 auto findUnavailableAsFailed =
663 base.find("InputUnavailableAsFailed");
664 if (findUnavailableAsFailed != base.end())
665 {
666 unavailableAsFailed =
667 std::get<bool>(findUnavailableAsFailed->second);
668 }
669
Jason Lingf3b04fd2020-07-24 09:33:04 -0700670 std::vector<SensorInterfaceType> inputSensorInterfaces;
671 std::vector<SensorInterfaceType> outputSensorInterfaces;
672 /* populate an interface list for different sensor direction
673 * types (input,output)
674 */
675 /* take the Inputs from the configuration and generate
676 * a list of dbus descriptors (path, interface).
677 * Mapping can be many-to-one since an element of Inputs can be
678 * a regex
679 */
680 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700681 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700682 findSensors(sensors, sensorNameToDbusName(sensorName),
683 inputSensorInterfaces);
684 }
685 for (const std::string& sensorName : outputSensorNames)
686 {
687 findSensors(sensors, sensorNameToDbusName(sensorName),
688 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800689 }
James Feist50fdfe32018-09-24 15:51:09 -0700690
Jason Lingf3b04fd2020-07-24 09:33:04 -0700691 inputSensorNames.clear();
692 for (const SensorInterfaceType& inputSensorInterface :
693 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800694 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700695 const std::string& dbusInterface =
696 inputSensorInterface.second;
697 const std::string& inputSensorPath =
698 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700699
700 // Setting timeout to 0 is intentional, as D-Bus passive
701 // sensor updates are pushed in, not pulled by timer poll.
702 // Setting ignoreDbusMinMax is intentional, as this
703 // prevents normalization of values to [0.0, 1.0] range,
704 // which would mess up the PID loop math.
705 // All non-fan PID classes should be initialized this way.
706 // As for why a fan should not use this code path, see
707 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
Josh Lehan23e22b92022-11-12 22:37:58 -0800708 if ((pidClass == "temp") || (pidClass == "margin") ||
709 (pidClass == "power") || (pidClass == "powersum"))
James Feist50fdfe32018-09-24 15:51:09 -0700710 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800711 std::string inputSensorName =
712 getSensorNameFromPath(inputSensorPath);
713 auto& config = sensorConfig[inputSensorName];
714 inputSensorNames.push_back(inputSensorName);
715 config.type = pidClass;
716 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700717 config.timeout = 0;
718 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800719 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700720 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700721
Jason Lingf3b04fd2020-07-24 09:33:04 -0700722 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700723 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700724 /* all expected inputs in the configuration are expected
725 * to be sensor interfaces
726 */
727 throw std::runtime_error(
728 "sensor at dbus path [" + inputSensorPath +
729 "] has an interface [" + dbusInterface +
730 "] that does not match the expected interface of " +
731 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700732 }
733 }
James Feist1738e2a2019-02-04 15:57:03 -0800734
Jason Lingf3b04fd2020-07-24 09:33:04 -0700735 /* fan pids need to pair up tach sensors with their pwm
736 * counterparts
737 */
738 if (pidClass == "fan")
739 {
740 /* If a PID is a fan there should be either
741 * (1) one output(pwm) per input(tach)
742 * OR
743 * (2) one putput(pwm) for all inputs(tach)
744 * everything else indicates a bad configuration.
745 */
746 bool singlePwm = false;
747 if (outputSensorInterfaces.size() == 1)
748 {
749 /* one pwm, set write paths for all fan sensors to it */
750 singlePwm = true;
751 }
752 else if (inputSensorInterfaces.size() ==
753 outputSensorInterfaces.size())
754 {
755 /* one to one mapping, each fan sensor gets its own pwm
756 * control */
757 singlePwm = false;
758 }
759 else
760 {
761 throw std::runtime_error(
762 "fan PID has invalid number of Outputs");
763 }
764 std::string fanSensorName;
765 std::string pwmPath;
766 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800767 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700768 if (singlePwm)
769 {
770 /* if just a single output(pwm) is provided then use
771 * that pwm control path for all the fan sensor write
772 * path configs
773 */
774 pwmPath = outputSensorInterfaces.at(0).first;
775 pwmInterface = outputSensorInterfaces.at(0).second;
776 }
777 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
778 idx++)
779 {
780 if (!singlePwm)
781 {
782 pwmPath = outputSensorInterfaces.at(idx).first;
783 pwmInterface =
784 outputSensorInterfaces.at(idx).second;
785 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700786 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700787 {
788 throw std::runtime_error(
789 "fan pwm control at dbus path [" + pwmPath +
790 "] has an interface [" + pwmInterface +
791 "] that does not match the expected interface "
792 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700793 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700794 }
795 const std::string& fanPath =
796 inputSensorInterfaces.at(idx).first;
797 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800798 pwmSensorName = getSensorNameFromPath(pwmPath);
799 std::string fanPwmIndex = fanSensorName + pwmSensorName;
800 inputSensorNames.push_back(fanPwmIndex);
801 auto& fanConfig = sensorConfig[fanPwmIndex];
802 fanConfig.type = pidClass;
803 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700804 fanConfig.writePath = pwmPath;
805 // todo: un-hardcode this if there are fans with
806 // different ranges
807 fanConfig.max = 255;
808 fanConfig.min = 0;
809 }
810 }
James Feist11d243d2019-06-24 16:18:40 -0700811 // if the sensors aren't available in the current state, don't
812 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700813 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700814 {
815 continue;
816 }
817
James Feist5ec20272019-07-10 11:59:57 -0700818 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700819
James Feist5ec20272019-07-10 11:59:57 -0700820 // SetPointOffset is a threshold value to pull from the sensor
821 // to apply an offset. For upper thresholds this means the
822 // setpoint is usually negative.
823 auto findSetpointOffset = base.find("SetPointOffset");
824 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700825 {
James Feist5ec20272019-07-10 11:59:57 -0700826 offsetType =
827 std::get<std::string>(findSetpointOffset->second);
828 if (std::find(thresholds::types.begin(),
829 thresholds::types.end(),
830 offsetType) == thresholds::types.end())
831 {
832 throw std::runtime_error("Unsupported type: " +
833 offsetType);
834 }
835 }
836
837 if (offsetType.empty())
838 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700839 conf::ControllerInfo& info =
James Feist5ec20272019-07-10 11:59:57 -0700840 conf[std::get<std::string>(base.at("Name"))];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700841 info.inputs = std::move(inputSensorNames);
Patrick Venture73823182020-10-08 15:12:51 -0700842 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -0700843 }
844 else
845 {
James Feist5ec20272019-07-10 11:59:57 -0700846 // we have to split up the inputs, as in practice t-control
847 // values will differ, making setpoints differ
Jason Lingf3b04fd2020-07-24 09:33:04 -0700848 for (const std::string& input : inputSensorNames)
James Feist5ec20272019-07-10 11:59:57 -0700849 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700850 conf::ControllerInfo& info = conf[input];
James Feist5ec20272019-07-10 11:59:57 -0700851 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -0700852 populatePidInfo(bus, base, info, &offsetType,
853 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -0700854 }
James Feist22c257a2018-08-31 14:07:12 -0700855 }
James Feist22c257a2018-08-31 14:07:12 -0700856 }
857 }
858 auto findStepwise =
859 configuration.second.find(stepwiseConfigurationInterface);
860 if (findStepwise != configuration.second.end())
861 {
862 const auto& base = findStepwise->second;
863 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800864 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700865 for (const std::string& zone : zones)
866 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700867 auto index = getZoneIndex(zone, foundZones);
868
James Feistf81f2882019-02-26 11:26:36 -0800869 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700870
871 std::vector<std::string> inputs;
872 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800873 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700874
Alex.Song8f73ad72021-10-07 00:18:27 +0800875 bool unavailableAsFailed = true;
876 auto findUnavailableAsFailed =
877 base.find("InputUnavailableAsFailed");
878 if (findUnavailableAsFailed != base.end())
879 {
880 unavailableAsFailed =
881 std::get<bool>(findUnavailableAsFailed->second);
882 }
883
James Feist1738e2a2019-02-04 15:57:03 -0800884 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700885 for (const std::string& sensorName : sensorNames)
886 {
James Feist1738e2a2019-02-04 15:57:03 -0800887 std::vector<std::pair<std::string, std::string>>
888 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700889 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
890 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700891 {
James Feist50fdfe32018-09-24 15:51:09 -0700892 break;
893 }
894
James Feist1738e2a2019-02-04 15:57:03 -0800895 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
896 {
897 size_t idx =
898 sensorPathIfacePair.first.find_last_of("/") + 1;
899 std::string shortName =
900 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700901
James Feist1738e2a2019-02-04 15:57:03 -0800902 inputs.push_back(shortName);
903 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800904 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800905 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800906 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800907 config.unavailableAsFailed = unavailableAsFailed;
James Feist1738e2a2019-02-04 15:57:03 -0800908 // todo: maybe un-hardcode this if we run into slower
909 // timeouts with sensors
910
James Feist2642cb52019-02-25 13:00:16 -0800911 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800912 sensorFound = true;
913 }
James Feist50fdfe32018-09-24 15:51:09 -0700914 }
915 if (!sensorFound)
916 {
917 continue;
918 }
Patrick Venture1df9e872020-10-08 15:35:01 -0700919 conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800920 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700921 info.inputs = std::move(inputs);
922
James Feist22c257a2018-08-31 14:07:12 -0700923 info.type = "stepwise";
924 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700925 info.stepwiseInfo.positiveHysteresis = 0.0;
926 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800927 std::string subtype = std::get<std::string>(base.at("Class"));
928
929 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700930 auto findPosHyst = base.find("PositiveHysteresis");
931 auto findNegHyst = base.find("NegativeHysteresis");
932 if (findPosHyst != base.end())
933 {
James Feist1f802f52019-02-08 13:51:43 -0800934 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800935 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700936 }
937 if (findNegHyst != base.end())
938 {
James Feist5782ab82019-04-02 08:38:48 -0700939 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800940 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700941 }
James Feist22c257a2018-08-31 14:07:12 -0700942 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800943 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700944 if (readings.size() > ec::maxStepwisePoints)
945 {
946 throw std::invalid_argument("Too many stepwise points.");
947 }
948 if (readings.empty())
949 {
950 throw std::invalid_argument(
951 "Must have one stepwise point.");
952 }
953 std::copy(readings.begin(), readings.end(),
954 info.stepwiseInfo.reading);
955 if (readings.size() < ec::maxStepwisePoints)
956 {
957 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800958 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700959 }
960 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800961 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700962 if (readings.size() != outputs.size())
963 {
964 throw std::invalid_argument(
965 "Outputs size must match readings");
966 }
967 std::copy(outputs.begin(), outputs.end(),
968 info.stepwiseInfo.output);
969 if (outputs.size() < ec::maxStepwisePoints)
970 {
971 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800972 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700973 }
James Feist7136a5a2018-07-19 09:52:05 -0700974 }
975 }
976 }
Patrick Venture39199b42020-10-08 14:40:29 -0700977 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700978 {
Patrick Venture39199b42020-10-08 14:40:29 -0700979 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -0700980 }
James Feistc959c422018-11-01 12:33:40 -0700981 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700982 {
James Feist1fe08952019-05-07 09:17:16 -0700983 std::cerr
984 << "No fan zones, application pausing until new configuration\n";
985 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700986 }
James Feist1fe08952019-05-07 09:17:16 -0700987 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700988}
Patrick Venturea0764872020-08-08 07:48:43 -0700989
James Feist7136a5a2018-07-19 09:52:05 -0700990} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -0700991} // namespace pid_control