blob: 96d2b4b936f6f66477ea402304790a4a6ef5c596 [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);
252}
253
Jason Ling6fc301f2020-07-23 12:39:57 -0700254/**
255 * retrieve an attribute from the pid configuration map
256 * @param[in] base - the PID configuration map, keys are the attributes and
257 * value is the variant associated with that attribute.
258 * @param attributeName - the name of the attribute
259 * @return a variant holding the value associated with a key
260 * @throw runtime_error : attributeName is not in base
261 */
262inline DbusVariantType getPIDAttribute(
263 const std::unordered_map<std::string, DbusVariantType>& base,
264 const std::string& attributeName)
265{
266 auto search = base.find(attributeName);
267 if (search == base.end())
268 {
269 throw std::runtime_error("missing attribute " + attributeName);
270 }
271 return search->second;
272}
273
Harvey Wu239aa7d2022-11-18 08:43:34 +0800274inline void getCycleTimeSetting(
275 const std::unordered_map<std::string, DbusVariantType>& zone,
276 const int zoneIndex, const std::string& attributeName, uint64_t& value)
277{
278 auto findAttributeName = zone.find(attributeName);
279 if (findAttributeName != zone.end())
280 {
281 double tmpAttributeValue =
282 std::visit(VariantToDoubleVisitor(), zone.at(attributeName));
283 if (tmpAttributeValue >= 1.0)
284 {
285 value = static_cast<uint64_t>(tmpAttributeValue);
286 }
287 else
288 {
289 std::cerr << "Zone " << zoneIndex << ": " << attributeName
290 << " is invalid. Use default " << value << " ms\n";
291 }
292 }
293 else
294 {
295 std::cerr << "Zone " << zoneIndex << ": " << attributeName
296 << " cannot find setting. Use default " << value << " ms\n";
297 }
298}
299
James Feist5ec20272019-07-10 11:59:57 -0700300void populatePidInfo(
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800301 [[maybe_unused]] sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700302 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700303 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700304 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700305{
Jason Ling6fc301f2020-07-23 12:39:57 -0700306 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700307 if (info.type == "fan")
308 {
309 info.setpoint = 0;
310 }
311 else
312 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700313 info.setpoint = std::visit(VariantToDoubleVisitor(),
314 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700315 }
316
317 if (thresholdProperty != nullptr)
318 {
319 std::string interface;
320 if (*thresholdProperty == "WarningHigh" ||
321 *thresholdProperty == "WarningLow")
322 {
323 interface = thresholds::warningInterface;
324 }
325 else
326 {
327 interface = thresholds::criticalInterface;
328 }
Patrick Venture73823182020-10-08 15:12:51 -0700329 const std::string& path = sensorConfig.at(info.inputs.front()).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700330
Patrick Venture8729eb92020-08-10 10:38:44 -0700331 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700332 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700333 double reading = 0;
334 try
335 {
Patrick Venture9b936922020-08-10 11:28:39 -0700336 helper.getProperty(service, path, interface, *thresholdProperty,
337 reading);
James Feist5ec20272019-07-10 11:59:57 -0700338 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500339 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700340 {
341 // unsupported threshold, leaving reading at 0
342 }
343
344 info.setpoint += reading;
345 }
346
347 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700348 info.pidInfo.proportionalCoeff = std::visit(
349 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
350 info.pidInfo.integralCoeff = std::visit(
351 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
Josh Lehanc612c052022-12-12 09:56:47 -0800352 // DCoefficient is below, it is optional, same reason as in buildjson.cpp
Jason Ling6fc301f2020-07-23 12:39:57 -0700353 info.pidInfo.feedFwdOffset = std::visit(
354 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
355 info.pidInfo.feedFwdGain = std::visit(
356 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
357 info.pidInfo.integralLimit.max = std::visit(
358 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
359 info.pidInfo.integralLimit.min = std::visit(
360 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
361 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
362 getPIDAttribute(base, "OutLimitMax"));
363 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
364 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700365 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700366 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700367 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700368 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
Josh Lehanc612c052022-12-12 09:56:47 -0800369
James Feist5ec20272019-07-10 11:59:57 -0700370 double negativeHysteresis = 0;
371 double positiveHysteresis = 0;
Josh Lehanc612c052022-12-12 09:56:47 -0800372 double derivativeCoeff = 0;
James Feist5ec20272019-07-10 11:59:57 -0700373
374 auto findNeg = base.find("NegativeHysteresis");
375 auto findPos = base.find("PositiveHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -0800376 auto findDerivative = base.find("DCoefficient");
James Feist5ec20272019-07-10 11:59:57 -0700377
378 if (findNeg != base.end())
379 {
380 negativeHysteresis =
381 std::visit(VariantToDoubleVisitor(), findNeg->second);
382 }
James Feist5ec20272019-07-10 11:59:57 -0700383 if (findPos != base.end())
384 {
385 positiveHysteresis =
386 std::visit(VariantToDoubleVisitor(), findPos->second);
387 }
Josh Lehanc612c052022-12-12 09:56:47 -0800388 if (findDerivative != base.end())
389 {
390 derivativeCoeff =
391 std::visit(VariantToDoubleVisitor(), findDerivative->second);
392 }
393
James Feist5ec20272019-07-10 11:59:57 -0700394 info.pidInfo.negativeHysteresis = negativeHysteresis;
395 info.pidInfo.positiveHysteresis = positiveHysteresis;
Josh Lehanc612c052022-12-12 09:56:47 -0800396 info.pidInfo.derivativeCoeff = derivativeCoeff;
James Feist5ec20272019-07-10 11:59:57 -0700397}
398
Patrick Williamsb228bc32022-07-22 19:26:56 -0500399bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700400 std::map<std::string, conf::SensorConfig>& sensorConfig,
401 std::map<int64_t, conf::PIDConf>& zoneConfig,
402 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700403{
404
405 sensorConfig.clear();
406 zoneConfig.clear();
407 zoneDetailsConfig.clear();
408
409 createMatches(bus, timer);
410
James Feist7136a5a2018-07-19 09:52:05 -0700411 auto mapper =
412 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
413 "/xyz/openbmc_project/object_mapper",
414 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700415 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700416 std::array<const char*, 6>{
417 objectManagerInterface, pidConfigurationInterface,
418 pidZoneConfigurationInterface,
419 stepwiseConfigurationInterface, sensorInterface,
420 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700421 std::unordered_map<
422 std::string, std::unordered_map<std::string, std::vector<std::string>>>
423 respData;
James Feist22c257a2018-08-31 14:07:12 -0700424 try
425 {
426 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700427 resp.read(respData);
428 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500429 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700430 {
431 // can't do anything without mapper call data
432 throw std::runtime_error("ObjectMapper Call Failure");
433 }
James Feist7136a5a2018-07-19 09:52:05 -0700434
James Feist7136a5a2018-07-19 09:52:05 -0700435 if (respData.empty())
436 {
James Feist22c257a2018-08-31 14:07:12 -0700437 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700438 throw std::runtime_error("No configuration data available from Mapper");
439 }
440 // create a map of pair of <has pid configuration, ObjectManager path>
441 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
442 // and a map of <path, interface> for sensors
443 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700444 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700445 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700446 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700447 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700448 auto& owner = owners[ownerPair.first];
449 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700450 {
451
452 if (interface == objectManagerInterface)
453 {
454 owner.second = objectPair.first;
455 }
456 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700457 interface == pidZoneConfigurationInterface ||
458 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700459 {
460 owner.first = true;
461 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700462 if (interface == sensorInterface ||
463 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700464 {
465 // we're not interested in pwm sensors, just pwm control
466 if (interface == sensorInterface &&
467 objectPair.first.find("pwm") != std::string::npos)
468 {
469 continue;
470 }
471 sensors[objectPair.first] = interface;
472 }
473 }
474 }
475 }
476 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700477 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700478 {
479 // skip if no pid configuration (means probably a sensor)
480 if (!owner.second.first)
481 {
482 continue;
483 }
484 auto endpoint = bus.new_method_call(
485 owner.first.c_str(), owner.second.second.c_str(),
486 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700487 ManagedObjectType configuration;
488 try
James Feist7136a5a2018-07-19 09:52:05 -0700489 {
James Feist22c257a2018-08-31 14:07:12 -0700490 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700491 responce.read(configuration);
492 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500493 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700494 {
495 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700496 throw std::runtime_error("Error getting managed objects from " +
497 owner.first);
498 }
James Feist22c257a2018-08-31 14:07:12 -0700499
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700500 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700501 {
502 if (pathPair.second.find(pidConfigurationInterface) !=
503 pathPair.second.end() ||
504 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700505 pathPair.second.end() ||
506 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700507 pathPair.second.end())
508 {
509 configurations.emplace(pathPair);
510 }
James Feistf0096a02019-02-21 11:25:22 -0800511 }
512 }
513
514 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700515 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
516 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800517 {
James Feist3987c8b2019-05-13 10:43:17 -0700518 for (auto pathIt = configurations.begin();
519 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800520 {
James Feist3987c8b2019-05-13 10:43:17 -0700521 for (auto confIt = pathIt->second.begin();
522 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800523 {
James Feist3987c8b2019-05-13 10:43:17 -0700524 auto profilesFind = confIt->second.find("Profiles");
525 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800526 {
James Feist3987c8b2019-05-13 10:43:17 -0700527 confIt++;
528 continue; // if no profiles selected, apply always
529 }
530 auto profiles =
531 std::get<std::vector<std::string>>(profilesFind->second);
532 if (profiles.empty())
533 {
534 confIt++;
535 continue;
536 }
537
538 bool found = false;
539 for (const std::string& profile : profiles)
540 {
541 if (std::find(selectedProfiles.begin(),
542 selectedProfiles.end(),
543 profile) != selectedProfiles.end())
544 {
545 found = true;
546 break;
547 }
548 }
549 if (found)
550 {
551 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800552 }
553 else
554 {
James Feist3987c8b2019-05-13 10:43:17 -0700555 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800556 }
557 }
James Feist3987c8b2019-05-13 10:43:17 -0700558 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800559 {
James Feist3987c8b2019-05-13 10:43:17 -0700560 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800561 }
James Feist3987c8b2019-05-13 10:43:17 -0700562 else
James Feistf0096a02019-02-21 11:25:22 -0800563 {
James Feist3987c8b2019-05-13 10:43:17 -0700564 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800565 }
James Feist7136a5a2018-07-19 09:52:05 -0700566 }
567 }
James Feist8c3c51e2018-08-08 16:31:43 -0700568
Josh Lehan998fbe62020-09-20 21:21:05 -0700569 // On D-Bus, although not necessary,
570 // having the "zoneID" field can still be useful,
571 // as it is used for diagnostic messages,
572 // logging file names, and so on.
573 // Accept optional "ZoneIndex" parameter to explicitly specify.
574 // If not present, or not unique, auto-assign index,
575 // using 0-based numbering, ensuring uniqueness.
576 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700577 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700578 {
579 auto findZone =
580 configuration.second.find(pidZoneConfigurationInterface);
581 if (findZone != configuration.second.end())
582 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700583 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800584
James Feist1f802f52019-02-08 13:51:43 -0800585 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700586
587 auto findZoneIndex = zone.find("ZoneIndex");
588 if (findZoneIndex == zone.end())
589 {
590 continue;
591 }
592
593 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
594 if (!ptrZoneIndex)
595 {
596 continue;
597 }
598
599 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
600 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
601 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
602 << "\n";
603 }
604 }
605
606 for (const auto& configuration : configurations)
607 {
608 auto findZone =
609 configuration.second.find(pidZoneConfigurationInterface);
610 if (findZone != configuration.second.end())
611 {
612 const auto& zone = findZone->second;
613
614 const std::string& name = std::get<std::string>(zone.at("Name"));
615
616 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700617
Patrick Venturec54fbd82018-10-30 19:40:05 -0700618 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700619
James Feist3484bed2019-02-25 13:28:18 -0800620 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
621 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800622 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800623 zone.at("FailSafePercent"));
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800624
Harvey Wu239aa7d2022-11-18 08:43:34 +0800625 getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
626 details.cycleTime.cycleIntervalTimeMS);
627 getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
628 details.cycleTime.updateThermalsTimeMS);
James Feist7136a5a2018-07-19 09:52:05 -0700629 }
630 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700631 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700632 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700633 {
James Feist22c257a2018-08-31 14:07:12 -0700634 const auto& base =
635 configuration.second.at(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700636 const std::string pidName = std::get<std::string>(base.at("Name"));
637 const std::string pidClass =
638 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700639 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800640 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700641 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700642 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700643 auto index = getZoneIndex(zone, foundZones);
644
James Feistf81f2882019-02-26 11:26:36 -0800645 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700646 std::vector<std::string> inputSensorNames(
647 std::get<std::vector<std::string>>(base.at("Inputs")));
648 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700649
Jason Lingf3b04fd2020-07-24 09:33:04 -0700650 // assumption: all fan pids must have at least one output
651 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700652 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700653 outputSensorNames = std::get<std::vector<std::string>>(
654 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700655 }
James Feist1738e2a2019-02-04 15:57:03 -0800656
Alex.Song8f73ad72021-10-07 00:18:27 +0800657 bool unavailableAsFailed = true;
658 auto findUnavailableAsFailed =
659 base.find("InputUnavailableAsFailed");
660 if (findUnavailableAsFailed != base.end())
661 {
662 unavailableAsFailed =
663 std::get<bool>(findUnavailableAsFailed->second);
664 }
665
Jason Lingf3b04fd2020-07-24 09:33:04 -0700666 std::vector<SensorInterfaceType> inputSensorInterfaces;
667 std::vector<SensorInterfaceType> outputSensorInterfaces;
668 /* populate an interface list for different sensor direction
669 * types (input,output)
670 */
671 /* take the Inputs from the configuration and generate
672 * a list of dbus descriptors (path, interface).
673 * Mapping can be many-to-one since an element of Inputs can be
674 * a regex
675 */
676 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700677 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700678 findSensors(sensors, sensorNameToDbusName(sensorName),
679 inputSensorInterfaces);
680 }
681 for (const std::string& sensorName : outputSensorNames)
682 {
683 findSensors(sensors, sensorNameToDbusName(sensorName),
684 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800685 }
James Feist50fdfe32018-09-24 15:51:09 -0700686
Jason Lingf3b04fd2020-07-24 09:33:04 -0700687 inputSensorNames.clear();
688 for (const SensorInterfaceType& inputSensorInterface :
689 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800690 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700691 const std::string& dbusInterface =
692 inputSensorInterface.second;
693 const std::string& inputSensorPath =
694 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700695
696 // Setting timeout to 0 is intentional, as D-Bus passive
697 // sensor updates are pushed in, not pulled by timer poll.
698 // Setting ignoreDbusMinMax is intentional, as this
699 // prevents normalization of values to [0.0, 1.0] range,
700 // which would mess up the PID loop math.
701 // All non-fan PID classes should be initialized this way.
702 // As for why a fan should not use this code path, see
703 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
Josh Lehan23e22b92022-11-12 22:37:58 -0800704 if ((pidClass == "temp") || (pidClass == "margin") ||
705 (pidClass == "power") || (pidClass == "powersum"))
James Feist50fdfe32018-09-24 15:51:09 -0700706 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800707 std::string inputSensorName =
708 getSensorNameFromPath(inputSensorPath);
709 auto& config = sensorConfig[inputSensorName];
710 inputSensorNames.push_back(inputSensorName);
711 config.type = pidClass;
712 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700713 config.timeout = 0;
714 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800715 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700716 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700717
Jason Lingf3b04fd2020-07-24 09:33:04 -0700718 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700719 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700720 /* all expected inputs in the configuration are expected
721 * to be sensor interfaces
722 */
723 throw std::runtime_error(
724 "sensor at dbus path [" + inputSensorPath +
725 "] has an interface [" + dbusInterface +
726 "] that does not match the expected interface of " +
727 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700728 }
729 }
James Feist1738e2a2019-02-04 15:57:03 -0800730
Jason Lingf3b04fd2020-07-24 09:33:04 -0700731 /* fan pids need to pair up tach sensors with their pwm
732 * counterparts
733 */
734 if (pidClass == "fan")
735 {
736 /* If a PID is a fan there should be either
737 * (1) one output(pwm) per input(tach)
738 * OR
739 * (2) one putput(pwm) for all inputs(tach)
740 * everything else indicates a bad configuration.
741 */
742 bool singlePwm = false;
743 if (outputSensorInterfaces.size() == 1)
744 {
745 /* one pwm, set write paths for all fan sensors to it */
746 singlePwm = true;
747 }
748 else if (inputSensorInterfaces.size() ==
749 outputSensorInterfaces.size())
750 {
751 /* one to one mapping, each fan sensor gets its own pwm
752 * control */
753 singlePwm = false;
754 }
755 else
756 {
757 throw std::runtime_error(
758 "fan PID has invalid number of Outputs");
759 }
760 std::string fanSensorName;
761 std::string pwmPath;
762 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800763 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700764 if (singlePwm)
765 {
766 /* if just a single output(pwm) is provided then use
767 * that pwm control path for all the fan sensor write
768 * path configs
769 */
770 pwmPath = outputSensorInterfaces.at(0).first;
771 pwmInterface = outputSensorInterfaces.at(0).second;
772 }
773 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
774 idx++)
775 {
776 if (!singlePwm)
777 {
778 pwmPath = outputSensorInterfaces.at(idx).first;
779 pwmInterface =
780 outputSensorInterfaces.at(idx).second;
781 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700782 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700783 {
784 throw std::runtime_error(
785 "fan pwm control at dbus path [" + pwmPath +
786 "] has an interface [" + pwmInterface +
787 "] that does not match the expected interface "
788 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700789 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700790 }
791 const std::string& fanPath =
792 inputSensorInterfaces.at(idx).first;
793 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800794 pwmSensorName = getSensorNameFromPath(pwmPath);
795 std::string fanPwmIndex = fanSensorName + pwmSensorName;
796 inputSensorNames.push_back(fanPwmIndex);
797 auto& fanConfig = sensorConfig[fanPwmIndex];
798 fanConfig.type = pidClass;
799 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700800 fanConfig.writePath = pwmPath;
801 // todo: un-hardcode this if there are fans with
802 // different ranges
803 fanConfig.max = 255;
804 fanConfig.min = 0;
805 }
806 }
James Feist11d243d2019-06-24 16:18:40 -0700807 // if the sensors aren't available in the current state, don't
808 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700809 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700810 {
811 continue;
812 }
813
James Feist5ec20272019-07-10 11:59:57 -0700814 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700815
James Feist5ec20272019-07-10 11:59:57 -0700816 // SetPointOffset is a threshold value to pull from the sensor
817 // to apply an offset. For upper thresholds this means the
818 // setpoint is usually negative.
819 auto findSetpointOffset = base.find("SetPointOffset");
820 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700821 {
James Feist5ec20272019-07-10 11:59:57 -0700822 offsetType =
823 std::get<std::string>(findSetpointOffset->second);
824 if (std::find(thresholds::types.begin(),
825 thresholds::types.end(),
826 offsetType) == thresholds::types.end())
827 {
828 throw std::runtime_error("Unsupported type: " +
829 offsetType);
830 }
831 }
832
833 if (offsetType.empty())
834 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700835 conf::ControllerInfo& info =
James Feist5ec20272019-07-10 11:59:57 -0700836 conf[std::get<std::string>(base.at("Name"))];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700837 info.inputs = std::move(inputSensorNames);
Patrick Venture73823182020-10-08 15:12:51 -0700838 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -0700839 }
840 else
841 {
James Feist5ec20272019-07-10 11:59:57 -0700842 // we have to split up the inputs, as in practice t-control
843 // values will differ, making setpoints differ
Jason Lingf3b04fd2020-07-24 09:33:04 -0700844 for (const std::string& input : inputSensorNames)
James Feist5ec20272019-07-10 11:59:57 -0700845 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700846 conf::ControllerInfo& info = conf[input];
James Feist5ec20272019-07-10 11:59:57 -0700847 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -0700848 populatePidInfo(bus, base, info, &offsetType,
849 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -0700850 }
James Feist22c257a2018-08-31 14:07:12 -0700851 }
James Feist22c257a2018-08-31 14:07:12 -0700852 }
853 }
854 auto findStepwise =
855 configuration.second.find(stepwiseConfigurationInterface);
856 if (findStepwise != configuration.second.end())
857 {
858 const auto& base = findStepwise->second;
859 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800860 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700861 for (const std::string& zone : zones)
862 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700863 auto index = getZoneIndex(zone, foundZones);
864
James Feistf81f2882019-02-26 11:26:36 -0800865 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700866
867 std::vector<std::string> inputs;
868 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800869 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700870
Alex.Song8f73ad72021-10-07 00:18:27 +0800871 bool unavailableAsFailed = true;
872 auto findUnavailableAsFailed =
873 base.find("InputUnavailableAsFailed");
874 if (findUnavailableAsFailed != base.end())
875 {
876 unavailableAsFailed =
877 std::get<bool>(findUnavailableAsFailed->second);
878 }
879
James Feist1738e2a2019-02-04 15:57:03 -0800880 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700881 for (const std::string& sensorName : sensorNames)
882 {
James Feist1738e2a2019-02-04 15:57:03 -0800883 std::vector<std::pair<std::string, std::string>>
884 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700885 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
886 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700887 {
James Feist50fdfe32018-09-24 15:51:09 -0700888 break;
889 }
890
James Feist1738e2a2019-02-04 15:57:03 -0800891 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
892 {
893 size_t idx =
894 sensorPathIfacePair.first.find_last_of("/") + 1;
895 std::string shortName =
896 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700897
James Feist1738e2a2019-02-04 15:57:03 -0800898 inputs.push_back(shortName);
899 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800900 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800901 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800902 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800903 config.unavailableAsFailed = unavailableAsFailed;
James Feist1738e2a2019-02-04 15:57:03 -0800904 // todo: maybe un-hardcode this if we run into slower
905 // timeouts with sensors
906
James Feist2642cb52019-02-25 13:00:16 -0800907 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800908 sensorFound = true;
909 }
James Feist50fdfe32018-09-24 15:51:09 -0700910 }
911 if (!sensorFound)
912 {
913 continue;
914 }
Patrick Venture1df9e872020-10-08 15:35:01 -0700915 conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800916 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700917 info.inputs = std::move(inputs);
918
James Feist22c257a2018-08-31 14:07:12 -0700919 info.type = "stepwise";
920 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700921 info.stepwiseInfo.positiveHysteresis = 0.0;
922 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800923 std::string subtype = std::get<std::string>(base.at("Class"));
924
925 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700926 auto findPosHyst = base.find("PositiveHysteresis");
927 auto findNegHyst = base.find("NegativeHysteresis");
928 if (findPosHyst != base.end())
929 {
James Feist1f802f52019-02-08 13:51:43 -0800930 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800931 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700932 }
933 if (findNegHyst != base.end())
934 {
James Feist5782ab82019-04-02 08:38:48 -0700935 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800936 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700937 }
James Feist22c257a2018-08-31 14:07:12 -0700938 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800939 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700940 if (readings.size() > ec::maxStepwisePoints)
941 {
942 throw std::invalid_argument("Too many stepwise points.");
943 }
944 if (readings.empty())
945 {
946 throw std::invalid_argument(
947 "Must have one stepwise point.");
948 }
949 std::copy(readings.begin(), readings.end(),
950 info.stepwiseInfo.reading);
951 if (readings.size() < ec::maxStepwisePoints)
952 {
953 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800954 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700955 }
956 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800957 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700958 if (readings.size() != outputs.size())
959 {
960 throw std::invalid_argument(
961 "Outputs size must match readings");
962 }
963 std::copy(outputs.begin(), outputs.end(),
964 info.stepwiseInfo.output);
965 if (outputs.size() < ec::maxStepwisePoints)
966 {
967 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800968 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700969 }
James Feist7136a5a2018-07-19 09:52:05 -0700970 }
971 }
972 }
Patrick Venture39199b42020-10-08 14:40:29 -0700973 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700974 {
Patrick Venture39199b42020-10-08 14:40:29 -0700975 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -0700976 }
James Feistc959c422018-11-01 12:33:40 -0700977 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700978 {
James Feist1fe08952019-05-07 09:17:16 -0700979 std::cerr
980 << "No fan zones, application pausing until new configuration\n";
981 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700982 }
James Feist1fe08952019-05-07 09:17:16 -0700983 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700984}
Patrick Venturea0764872020-08-08 07:48:43 -0700985
James Feist7136a5a2018-07-19 09:52:05 -0700986} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -0700987} // namespace pid_control