blob: 24732520c583bd639d471092df71b2058c86eb31 [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;
Patrick Williams8c051122023-05-10 07:50:59 -050087 auto mapper = bus.new_method_call("xyz.openbmc_project.ObjectMapper",
88 "/xyz/openbmc_project/object_mapper",
89 "xyz.openbmc_project.ObjectMapper",
90 "GetSubTree");
James Feistf0096a02019-02-21 11:25:22 -080091 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 Feist991ebd82020-07-21 11:14:52 -0700154 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700155 {
156 throw std::runtime_error("Invalid match");
157 }
James Feist991ebd82020-07-21 11:14:52 -0700158
159 // we skip associations because the mapper populates these, not the sensors
Josh Lehan10e46ef2023-02-01 18:25:58 -0800160 const std::array<const char*, 2> skipList = {
161 "xyz.openbmc_project.Association",
162 "xyz.openbmc_project.Association.Definitions"};
James Feist991ebd82020-07-21 11:14:52 -0700163
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 }
Josh Lehan10e46ef2023-02-01 18:25:58 -0800187
188 if constexpr (pid_control::conf::DEBUG)
189 {
190 std::cout << "New config detected: " << path.str << std::endl;
191 for (auto& d : data)
192 {
193 std::cout << "\tdata is " << d.first << std::endl;
194 for (auto& second : d.second)
195 {
196 std::cout << "\t\tdata is " << second.first << std::endl;
197 }
198 }
199 }
James Feist991ebd82020-07-21 11:14:52 -0700200 }
201
James Feist1fe08952019-05-07 09:17:16 -0700202 boost::asio::steady_timer* timer =
203 static_cast<boost::asio::steady_timer*>(context);
204
205 // do a brief sleep as we tend to get a bunch of these events at
206 // once
207 timer->expires_after(std::chrono::seconds(2));
208 timer->async_wait([](const boost::system::error_code ec) {
209 if (ec == boost::asio::error::operation_aborted)
210 {
211 /* another timer started*/
212 return;
213 }
214
215 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800216 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700217 });
218
219 return 1;
220}
221
Patrick Williamsb228bc32022-07-22 19:26:56 -0500222void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer)
James Feist1fe08952019-05-07 09:17:16 -0700223{
224 // this is a list because the matches can't be moved
Patrick Williamsb228bc32022-07-22 19:26:56 -0500225 static std::list<sdbusplus::bus::match_t> matches;
James Feist1fe08952019-05-07 09:17:16 -0700226
James Feist3987c8b2019-05-13 10:43:17 -0700227 const std::array<std::string, 4> interfaces = {
228 thermalControlIface, pidConfigurationInterface,
229 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700230
231 // this list only needs to be created once
232 if (!matches.empty())
233 {
234 return;
235 }
236
237 // we restart when the configuration changes or there are new sensors
238 for (const auto& interface : interfaces)
239 {
240 matches.emplace_back(
241 bus,
242 "type='signal',member='PropertiesChanged',arg0namespace='" +
243 interface + "'",
244 eventHandler, &timer);
245 }
246 matches.emplace_back(
247 bus,
248 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
249 "sensors/'",
250 eventHandler, &timer);
Jinliang Wangc2a311b2023-04-26 18:36:56 +0000251 matches.emplace_back(bus,
252 "type='signal',member='InterfacesRemoved',arg0path='/"
253 "xyz/openbmc_project/sensors/'",
254 eventHandler, &timer);
James Feist1fe08952019-05-07 09:17:16 -0700255}
256
Jason Ling6fc301f2020-07-23 12:39:57 -0700257/**
258 * retrieve an attribute from the pid configuration map
259 * @param[in] base - the PID configuration map, keys are the attributes and
260 * value is the variant associated with that attribute.
261 * @param attributeName - the name of the attribute
262 * @return a variant holding the value associated with a key
263 * @throw runtime_error : attributeName is not in base
264 */
265inline DbusVariantType getPIDAttribute(
266 const std::unordered_map<std::string, DbusVariantType>& base,
267 const std::string& attributeName)
268{
269 auto search = base.find(attributeName);
270 if (search == base.end())
271 {
272 throw std::runtime_error("missing attribute " + attributeName);
273 }
274 return search->second;
275}
276
Harvey Wu239aa7d2022-11-18 08:43:34 +0800277inline void getCycleTimeSetting(
278 const std::unordered_map<std::string, DbusVariantType>& zone,
279 const int zoneIndex, const std::string& attributeName, uint64_t& value)
280{
281 auto findAttributeName = zone.find(attributeName);
282 if (findAttributeName != zone.end())
283 {
Patrick Williams8c051122023-05-10 07:50:59 -0500284 double tmpAttributeValue = std::visit(VariantToDoubleVisitor(),
285 zone.at(attributeName));
Harvey Wu239aa7d2022-11-18 08:43:34 +0800286 if (tmpAttributeValue >= 1.0)
287 {
288 value = static_cast<uint64_t>(tmpAttributeValue);
289 }
290 else
291 {
292 std::cerr << "Zone " << zoneIndex << ": " << attributeName
293 << " is invalid. Use default " << value << " ms\n";
294 }
295 }
296 else
297 {
298 std::cerr << "Zone " << zoneIndex << ": " << attributeName
299 << " cannot find setting. Use default " << value << " ms\n";
300 }
301}
302
James Feist5ec20272019-07-10 11:59:57 -0700303void populatePidInfo(
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800304 [[maybe_unused]] sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700305 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700306 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700307 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700308{
Jason Ling6fc301f2020-07-23 12:39:57 -0700309 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700310 if (info.type == "fan")
311 {
312 info.setpoint = 0;
313 }
314 else
315 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700316 info.setpoint = std::visit(VariantToDoubleVisitor(),
317 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700318 }
319
ykchiu9fe3a3c2023-05-11 13:43:54 +0800320 int failsafepercent = 0;
321 auto findFailSafe = base.find("FailSafePercent");
322 if (findFailSafe != base.end())
323 {
324 failsafepercent = std::visit(VariantToDoubleVisitor(),
325 getPIDAttribute(base, "FailSafePercent"));
326 }
327 info.failSafePercent = failsafepercent;
328
James Feist5ec20272019-07-10 11:59:57 -0700329 if (thresholdProperty != nullptr)
330 {
331 std::string interface;
332 if (*thresholdProperty == "WarningHigh" ||
333 *thresholdProperty == "WarningLow")
334 {
335 interface = thresholds::warningInterface;
336 }
337 else
338 {
339 interface = thresholds::criticalInterface;
340 }
Josh Lehan31058fd2023-01-13 11:06:16 -0800341
342 // Although this checks only the first vector element for the
343 // named threshold, it is OK, because the SetPointOffset parser
344 // splits up the input into individual vectors, each with only a
345 // single element, if it detects that SetPointOffset is in use.
346 const std::string& path =
347 sensorConfig.at(info.inputs.front().name).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700348
Patrick Venture8729eb92020-08-10 10:38:44 -0700349 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700350 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700351 double reading = 0;
352 try
353 {
Patrick Venture9b936922020-08-10 11:28:39 -0700354 helper.getProperty(service, path, interface, *thresholdProperty,
355 reading);
James Feist5ec20272019-07-10 11:59:57 -0700356 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500357 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700358 {
359 // unsupported threshold, leaving reading at 0
360 }
361
362 info.setpoint += reading;
363 }
364
365 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700366 info.pidInfo.proportionalCoeff = std::visit(
367 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
368 info.pidInfo.integralCoeff = std::visit(
369 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
Josh Lehanc612c052022-12-12 09:56:47 -0800370 // DCoefficient is below, it is optional, same reason as in buildjson.cpp
Jason Ling6fc301f2020-07-23 12:39:57 -0700371 info.pidInfo.feedFwdOffset = std::visit(
372 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
373 info.pidInfo.feedFwdGain = std::visit(
374 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
375 info.pidInfo.integralLimit.max = std::visit(
376 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
377 info.pidInfo.integralLimit.min = std::visit(
378 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
379 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
380 getPIDAttribute(base, "OutLimitMax"));
381 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
382 getPIDAttribute(base, "OutLimitMin"));
Patrick Williams8c051122023-05-10 07:50:59 -0500383 info.pidInfo.slewNeg = std::visit(VariantToDoubleVisitor(),
384 getPIDAttribute(base, "SlewNeg"));
385 info.pidInfo.slewPos = std::visit(VariantToDoubleVisitor(),
386 getPIDAttribute(base, "SlewPos"));
Josh Lehanc612c052022-12-12 09:56:47 -0800387
James Feist5ec20272019-07-10 11:59:57 -0700388 double negativeHysteresis = 0;
389 double positiveHysteresis = 0;
Josh Lehanc612c052022-12-12 09:56:47 -0800390 double derivativeCoeff = 0;
James Feist5ec20272019-07-10 11:59:57 -0700391
392 auto findNeg = base.find("NegativeHysteresis");
393 auto findPos = base.find("PositiveHysteresis");
Josh Lehanc612c052022-12-12 09:56:47 -0800394 auto findDerivative = base.find("DCoefficient");
James Feist5ec20272019-07-10 11:59:57 -0700395
396 if (findNeg != base.end())
397 {
Patrick Williams8c051122023-05-10 07:50:59 -0500398 negativeHysteresis = std::visit(VariantToDoubleVisitor(),
399 findNeg->second);
James Feist5ec20272019-07-10 11:59:57 -0700400 }
James Feist5ec20272019-07-10 11:59:57 -0700401 if (findPos != base.end())
402 {
Patrick Williams8c051122023-05-10 07:50:59 -0500403 positiveHysteresis = std::visit(VariantToDoubleVisitor(),
404 findPos->second);
James Feist5ec20272019-07-10 11:59:57 -0700405 }
Josh Lehanc612c052022-12-12 09:56:47 -0800406 if (findDerivative != base.end())
407 {
Patrick Williams8c051122023-05-10 07:50:59 -0500408 derivativeCoeff = std::visit(VariantToDoubleVisitor(),
409 findDerivative->second);
Josh Lehanc612c052022-12-12 09:56:47 -0800410 }
411
James Feist5ec20272019-07-10 11:59:57 -0700412 info.pidInfo.negativeHysteresis = negativeHysteresis;
413 info.pidInfo.positiveHysteresis = positiveHysteresis;
Josh Lehanc612c052022-12-12 09:56:47 -0800414 info.pidInfo.derivativeCoeff = derivativeCoeff;
James Feist5ec20272019-07-10 11:59:57 -0700415}
416
Patrick Williamsb228bc32022-07-22 19:26:56 -0500417bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700418 std::map<std::string, conf::SensorConfig>& sensorConfig,
419 std::map<int64_t, conf::PIDConf>& zoneConfig,
420 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700421{
James Feist1fe08952019-05-07 09:17:16 -0700422 sensorConfig.clear();
423 zoneConfig.clear();
424 zoneDetailsConfig.clear();
425
426 createMatches(bus, timer);
427
Patrick Williams8c051122023-05-10 07:50:59 -0500428 auto mapper = bus.new_method_call("xyz.openbmc_project.ObjectMapper",
429 "/xyz/openbmc_project/object_mapper",
430 "xyz.openbmc_project.ObjectMapper",
431 "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700432 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700433 std::array<const char*, 6>{
434 objectManagerInterface, pidConfigurationInterface,
435 pidZoneConfigurationInterface,
436 stepwiseConfigurationInterface, sensorInterface,
437 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700438 std::unordered_map<
439 std::string, std::unordered_map<std::string, std::vector<std::string>>>
440 respData;
James Feist22c257a2018-08-31 14:07:12 -0700441 try
442 {
443 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700444 resp.read(respData);
445 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500446 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700447 {
448 // can't do anything without mapper call data
449 throw std::runtime_error("ObjectMapper Call Failure");
450 }
James Feist7136a5a2018-07-19 09:52:05 -0700451
James Feist7136a5a2018-07-19 09:52:05 -0700452 if (respData.empty())
453 {
James Feist22c257a2018-08-31 14:07:12 -0700454 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700455 throw std::runtime_error("No configuration data available from Mapper");
456 }
457 // create a map of pair of <has pid configuration, ObjectManager path>
458 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
459 // and a map of <path, interface> for sensors
460 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700461 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700462 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700463 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700464 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700465 auto& owner = owners[ownerPair.first];
466 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700467 {
James Feist7136a5a2018-07-19 09:52:05 -0700468 if (interface == objectManagerInterface)
469 {
470 owner.second = objectPair.first;
471 }
472 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700473 interface == pidZoneConfigurationInterface ||
474 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700475 {
476 owner.first = true;
477 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700478 if (interface == sensorInterface ||
479 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700480 {
481 // we're not interested in pwm sensors, just pwm control
482 if (interface == sensorInterface &&
483 objectPair.first.find("pwm") != std::string::npos)
484 {
485 continue;
486 }
487 sensors[objectPair.first] = interface;
488 }
489 }
490 }
491 }
492 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700493 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700494 {
495 // skip if no pid configuration (means probably a sensor)
496 if (!owner.second.first)
497 {
498 continue;
499 }
500 auto endpoint = bus.new_method_call(
501 owner.first.c_str(), owner.second.second.c_str(),
502 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700503 ManagedObjectType configuration;
504 try
James Feist7136a5a2018-07-19 09:52:05 -0700505 {
James Feist22c257a2018-08-31 14:07:12 -0700506 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700507 responce.read(configuration);
508 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500509 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700510 {
511 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700512 throw std::runtime_error("Error getting managed objects from " +
513 owner.first);
514 }
James Feist22c257a2018-08-31 14:07:12 -0700515
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700516 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700517 {
518 if (pathPair.second.find(pidConfigurationInterface) !=
519 pathPair.second.end() ||
520 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700521 pathPair.second.end() ||
522 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700523 pathPair.second.end())
524 {
525 configurations.emplace(pathPair);
526 }
James Feistf0096a02019-02-21 11:25:22 -0800527 }
528 }
529
530 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700531 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
532 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800533 {
James Feist3987c8b2019-05-13 10:43:17 -0700534 for (auto pathIt = configurations.begin();
535 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800536 {
James Feist3987c8b2019-05-13 10:43:17 -0700537 for (auto confIt = pathIt->second.begin();
538 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800539 {
James Feist3987c8b2019-05-13 10:43:17 -0700540 auto profilesFind = confIt->second.find("Profiles");
541 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800542 {
James Feist3987c8b2019-05-13 10:43:17 -0700543 confIt++;
544 continue; // if no profiles selected, apply always
545 }
546 auto profiles =
547 std::get<std::vector<std::string>>(profilesFind->second);
548 if (profiles.empty())
549 {
550 confIt++;
551 continue;
552 }
553
554 bool found = false;
555 for (const std::string& profile : profiles)
556 {
557 if (std::find(selectedProfiles.begin(),
558 selectedProfiles.end(),
559 profile) != selectedProfiles.end())
560 {
561 found = true;
562 break;
563 }
564 }
565 if (found)
566 {
567 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800568 }
569 else
570 {
James Feist3987c8b2019-05-13 10:43:17 -0700571 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800572 }
573 }
James Feist3987c8b2019-05-13 10:43:17 -0700574 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800575 {
James Feist3987c8b2019-05-13 10:43:17 -0700576 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800577 }
James Feist3987c8b2019-05-13 10:43:17 -0700578 else
James Feistf0096a02019-02-21 11:25:22 -0800579 {
James Feist3987c8b2019-05-13 10:43:17 -0700580 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800581 }
James Feist7136a5a2018-07-19 09:52:05 -0700582 }
583 }
James Feist8c3c51e2018-08-08 16:31:43 -0700584
Josh Lehan998fbe62020-09-20 21:21:05 -0700585 // On D-Bus, although not necessary,
586 // having the "zoneID" field can still be useful,
587 // as it is used for diagnostic messages,
588 // logging file names, and so on.
589 // Accept optional "ZoneIndex" parameter to explicitly specify.
590 // If not present, or not unique, auto-assign index,
591 // using 0-based numbering, ensuring uniqueness.
592 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700593 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700594 {
595 auto findZone =
596 configuration.second.find(pidZoneConfigurationInterface);
597 if (findZone != configuration.second.end())
598 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700599 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800600
James Feist1f802f52019-02-08 13:51:43 -0800601 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700602
603 auto findZoneIndex = zone.find("ZoneIndex");
604 if (findZoneIndex == zone.end())
605 {
606 continue;
607 }
608
609 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
610 if (!ptrZoneIndex)
611 {
612 continue;
613 }
614
615 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
616 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
617 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
618 << "\n";
619 }
620 }
621
622 for (const auto& configuration : configurations)
623 {
624 auto findZone =
625 configuration.second.find(pidZoneConfigurationInterface);
626 if (findZone != configuration.second.end())
627 {
628 const auto& zone = findZone->second;
629
630 const std::string& name = std::get<std::string>(zone.at("Name"));
631
632 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700633
Patrick Venturec54fbd82018-10-30 19:40:05 -0700634 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700635
James Feist3484bed2019-02-25 13:28:18 -0800636 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
637 zone.at("MinThermalOutput"));
ykchiu9fe3a3c2023-05-11 13:43:54 +0800638
639 int failsafepercent = 0;
640 auto findFailSafe = zone.find("FailSafePercent");
641 if (findFailSafe != zone.end())
642 {
643 failsafepercent = std::visit(VariantToDoubleVisitor(),
644 zone.at("FailSafePercent"));
645 }
646 details.failsafePercent = failsafepercent;
Josh Lehan9f9a06a2022-12-14 10:39:45 -0800647
Harvey Wu239aa7d2022-11-18 08:43:34 +0800648 getCycleTimeSetting(zone, index, "CycleIntervalTimeMS",
649 details.cycleTime.cycleIntervalTimeMS);
650 getCycleTimeSetting(zone, index, "UpdateThermalsTimeMS",
651 details.cycleTime.updateThermalsTimeMS);
James Feist7136a5a2018-07-19 09:52:05 -0700652 }
653 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700654 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700655 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700656 {
James Feist22c257a2018-08-31 14:07:12 -0700657 const auto& base =
658 configuration.second.at(pidConfigurationInterface);
ykchiu7c6d35d2023-05-10 17:01:46 +0800659 const std::string pidName =
660 sensorNameToDbusName(std::get<std::string>(base.at("Name")));
Jason Lingf3b04fd2020-07-24 09:33:04 -0700661 const std::string pidClass =
662 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700663 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800664 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700665 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700666 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700667 auto index = getZoneIndex(zone, foundZones);
668
James Feistf81f2882019-02-26 11:26:36 -0800669 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700670 std::vector<std::string> inputSensorNames(
671 std::get<std::vector<std::string>>(base.at("Inputs")));
672 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700673
Jason Lingf3b04fd2020-07-24 09:33:04 -0700674 // assumption: all fan pids must have at least one output
675 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700676 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700677 outputSensorNames = std::get<std::vector<std::string>>(
678 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700679 }
James Feist1738e2a2019-02-04 15:57:03 -0800680
Alex.Song8f73ad72021-10-07 00:18:27 +0800681 bool unavailableAsFailed = true;
682 auto findUnavailableAsFailed =
683 base.find("InputUnavailableAsFailed");
684 if (findUnavailableAsFailed != base.end())
685 {
686 unavailableAsFailed =
687 std::get<bool>(findUnavailableAsFailed->second);
688 }
689
Jason Lingf3b04fd2020-07-24 09:33:04 -0700690 std::vector<SensorInterfaceType> inputSensorInterfaces;
691 std::vector<SensorInterfaceType> outputSensorInterfaces;
692 /* populate an interface list for different sensor direction
693 * types (input,output)
694 */
695 /* take the Inputs from the configuration and generate
696 * a list of dbus descriptors (path, interface).
697 * Mapping can be many-to-one since an element of Inputs can be
698 * a regex
699 */
700 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700701 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700702 findSensors(sensors, sensorNameToDbusName(sensorName),
703 inputSensorInterfaces);
704 }
705 for (const std::string& sensorName : outputSensorNames)
706 {
707 findSensors(sensors, sensorNameToDbusName(sensorName),
708 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800709 }
James Feist50fdfe32018-09-24 15:51:09 -0700710
Jason Lingf3b04fd2020-07-24 09:33:04 -0700711 inputSensorNames.clear();
712 for (const SensorInterfaceType& inputSensorInterface :
713 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800714 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700715 const std::string& dbusInterface =
716 inputSensorInterface.second;
717 const std::string& inputSensorPath =
718 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700719
720 // Setting timeout to 0 is intentional, as D-Bus passive
721 // sensor updates are pushed in, not pulled by timer poll.
722 // Setting ignoreDbusMinMax is intentional, as this
723 // prevents normalization of values to [0.0, 1.0] range,
724 // which would mess up the PID loop math.
725 // All non-fan PID classes should be initialized this way.
726 // As for why a fan should not use this code path, see
727 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
Josh Lehan23e22b92022-11-12 22:37:58 -0800728 if ((pidClass == "temp") || (pidClass == "margin") ||
729 (pidClass == "power") || (pidClass == "powersum"))
James Feist50fdfe32018-09-24 15:51:09 -0700730 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800731 std::string inputSensorName =
732 getSensorNameFromPath(inputSensorPath);
733 auto& config = sensorConfig[inputSensorName];
734 inputSensorNames.push_back(inputSensorName);
735 config.type = pidClass;
736 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700737 config.timeout = 0;
738 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800739 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700740 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700741
Jason Lingf3b04fd2020-07-24 09:33:04 -0700742 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700743 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700744 /* all expected inputs in the configuration are expected
745 * to be sensor interfaces
746 */
747 throw std::runtime_error(
748 "sensor at dbus path [" + inputSensorPath +
749 "] has an interface [" + dbusInterface +
750 "] that does not match the expected interface of " +
751 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700752 }
753 }
James Feist1738e2a2019-02-04 15:57:03 -0800754
Jason Lingf3b04fd2020-07-24 09:33:04 -0700755 /* fan pids need to pair up tach sensors with their pwm
756 * counterparts
757 */
758 if (pidClass == "fan")
759 {
760 /* If a PID is a fan there should be either
761 * (1) one output(pwm) per input(tach)
762 * OR
763 * (2) one putput(pwm) for all inputs(tach)
764 * everything else indicates a bad configuration.
765 */
766 bool singlePwm = false;
767 if (outputSensorInterfaces.size() == 1)
768 {
769 /* one pwm, set write paths for all fan sensors to it */
770 singlePwm = true;
771 }
772 else if (inputSensorInterfaces.size() ==
773 outputSensorInterfaces.size())
774 {
775 /* one to one mapping, each fan sensor gets its own pwm
776 * control */
777 singlePwm = false;
778 }
779 else
780 {
781 throw std::runtime_error(
782 "fan PID has invalid number of Outputs");
783 }
784 std::string fanSensorName;
785 std::string pwmPath;
786 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800787 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700788 if (singlePwm)
789 {
790 /* if just a single output(pwm) is provided then use
791 * that pwm control path for all the fan sensor write
792 * path configs
793 */
794 pwmPath = outputSensorInterfaces.at(0).first;
795 pwmInterface = outputSensorInterfaces.at(0).second;
796 }
797 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
798 idx++)
799 {
800 if (!singlePwm)
801 {
802 pwmPath = outputSensorInterfaces.at(idx).first;
803 pwmInterface =
804 outputSensorInterfaces.at(idx).second;
805 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700806 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700807 {
808 throw std::runtime_error(
809 "fan pwm control at dbus path [" + pwmPath +
810 "] has an interface [" + pwmInterface +
811 "] that does not match the expected interface "
812 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700813 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700814 }
815 const std::string& fanPath =
816 inputSensorInterfaces.at(idx).first;
817 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800818 pwmSensorName = getSensorNameFromPath(pwmPath);
819 std::string fanPwmIndex = fanSensorName + pwmSensorName;
820 inputSensorNames.push_back(fanPwmIndex);
821 auto& fanConfig = sensorConfig[fanPwmIndex];
822 fanConfig.type = pidClass;
823 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700824 fanConfig.writePath = pwmPath;
825 // todo: un-hardcode this if there are fans with
826 // different ranges
827 fanConfig.max = 255;
828 fanConfig.min = 0;
829 }
830 }
James Feist11d243d2019-06-24 16:18:40 -0700831 // if the sensors aren't available in the current state, don't
832 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700833 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700834 {
835 continue;
836 }
837
James Feist5ec20272019-07-10 11:59:57 -0700838 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700839
James Feist5ec20272019-07-10 11:59:57 -0700840 // SetPointOffset is a threshold value to pull from the sensor
841 // to apply an offset. For upper thresholds this means the
842 // setpoint is usually negative.
843 auto findSetpointOffset = base.find("SetPointOffset");
844 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700845 {
James Feist5ec20272019-07-10 11:59:57 -0700846 offsetType =
847 std::get<std::string>(findSetpointOffset->second);
848 if (std::find(thresholds::types.begin(),
849 thresholds::types.end(),
850 offsetType) == thresholds::types.end())
851 {
852 throw std::runtime_error("Unsupported type: " +
853 offsetType);
854 }
855 }
856
Josh Lehan31058fd2023-01-13 11:06:16 -0800857 std::vector<double> inputTempToMargin;
858
859 auto findTempToMargin = base.find("TempToMargin");
860 if (findTempToMargin != base.end())
861 {
862 inputTempToMargin =
863 std::get<std::vector<double>>(findTempToMargin->second);
864 }
865
866 std::vector<pid_control::conf::SensorInput> sensorInputs =
867 spliceInputs(inputSensorNames, inputTempToMargin);
868
James Feist5ec20272019-07-10 11:59:57 -0700869 if (offsetType.empty())
870 {
ykchiu7c6d35d2023-05-10 17:01:46 +0800871 conf::ControllerInfo& info = conf[pidName];
Josh Lehan31058fd2023-01-13 11:06:16 -0800872 info.inputs = std::move(sensorInputs);
Patrick Venture73823182020-10-08 15:12:51 -0700873 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -0700874 }
875 else
876 {
James Feist5ec20272019-07-10 11:59:57 -0700877 // we have to split up the inputs, as in practice t-control
878 // values will differ, making setpoints differ
Josh Lehan31058fd2023-01-13 11:06:16 -0800879 for (const pid_control::conf::SensorInput& input :
880 sensorInputs)
James Feist5ec20272019-07-10 11:59:57 -0700881 {
Josh Lehan31058fd2023-01-13 11:06:16 -0800882 conf::ControllerInfo& info = conf[input.name];
James Feist5ec20272019-07-10 11:59:57 -0700883 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -0700884 populatePidInfo(bus, base, info, &offsetType,
885 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -0700886 }
James Feist22c257a2018-08-31 14:07:12 -0700887 }
James Feist22c257a2018-08-31 14:07:12 -0700888 }
889 }
890 auto findStepwise =
891 configuration.second.find(stepwiseConfigurationInterface);
892 if (findStepwise != configuration.second.end())
893 {
894 const auto& base = findStepwise->second;
ykchiu7c6d35d2023-05-10 17:01:46 +0800895 const std::string pidName =
896 sensorNameToDbusName(std::get<std::string>(base.at("Name")));
James Feist22c257a2018-08-31 14:07:12 -0700897 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800898 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700899 for (const std::string& zone : zones)
900 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700901 auto index = getZoneIndex(zone, foundZones);
902
James Feistf81f2882019-02-26 11:26:36 -0800903 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700904
905 std::vector<std::string> inputs;
906 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800907 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700908
Alex.Song8f73ad72021-10-07 00:18:27 +0800909 bool unavailableAsFailed = true;
910 auto findUnavailableAsFailed =
911 base.find("InputUnavailableAsFailed");
912 if (findUnavailableAsFailed != base.end())
913 {
914 unavailableAsFailed =
915 std::get<bool>(findUnavailableAsFailed->second);
916 }
917
James Feist1738e2a2019-02-04 15:57:03 -0800918 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700919 for (const std::string& sensorName : sensorNames)
920 {
James Feist1738e2a2019-02-04 15:57:03 -0800921 std::vector<std::pair<std::string, std::string>>
922 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700923 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
924 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700925 {
James Feist50fdfe32018-09-24 15:51:09 -0700926 break;
927 }
928
James Feist1738e2a2019-02-04 15:57:03 -0800929 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
930 {
931 size_t idx =
932 sensorPathIfacePair.first.find_last_of("/") + 1;
933 std::string shortName =
934 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700935
James Feist1738e2a2019-02-04 15:57:03 -0800936 inputs.push_back(shortName);
937 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800938 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800939 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800940 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800941 config.unavailableAsFailed = unavailableAsFailed;
James Feist1738e2a2019-02-04 15:57:03 -0800942 // todo: maybe un-hardcode this if we run into slower
943 // timeouts with sensors
944
James Feist2642cb52019-02-25 13:00:16 -0800945 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800946 sensorFound = true;
947 }
James Feist50fdfe32018-09-24 15:51:09 -0700948 }
949 if (!sensorFound)
950 {
951 continue;
952 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800953 conf::ControllerInfo& info = conf[pidName];
Josh Lehan31058fd2023-01-13 11:06:16 -0800954
955 std::vector<double> inputTempToMargin;
956
957 auto findTempToMargin = base.find("TempToMargin");
958 if (findTempToMargin != base.end())
959 {
960 inputTempToMargin =
961 std::get<std::vector<double>>(findTempToMargin->second);
962 }
963
964 info.inputs = spliceInputs(inputs, inputTempToMargin);
James Feist50fdfe32018-09-24 15:51:09 -0700965
James Feist22c257a2018-08-31 14:07:12 -0700966 info.type = "stepwise";
967 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700968 info.stepwiseInfo.positiveHysteresis = 0.0;
969 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800970 std::string subtype = std::get<std::string>(base.at("Class"));
971
972 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700973 auto findPosHyst = base.find("PositiveHysteresis");
974 auto findNegHyst = base.find("NegativeHysteresis");
975 if (findPosHyst != base.end())
976 {
James Feist1f802f52019-02-08 13:51:43 -0800977 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800978 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700979 }
980 if (findNegHyst != base.end())
981 {
James Feist5782ab82019-04-02 08:38:48 -0700982 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800983 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700984 }
James Feist22c257a2018-08-31 14:07:12 -0700985 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800986 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700987 if (readings.size() > ec::maxStepwisePoints)
988 {
989 throw std::invalid_argument("Too many stepwise points.");
990 }
991 if (readings.empty())
992 {
993 throw std::invalid_argument(
994 "Must have one stepwise point.");
995 }
996 std::copy(readings.begin(), readings.end(),
997 info.stepwiseInfo.reading);
998 if (readings.size() < ec::maxStepwisePoints)
999 {
1000 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -08001001 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -07001002 }
1003 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -08001004 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -07001005 if (readings.size() != outputs.size())
1006 {
1007 throw std::invalid_argument(
1008 "Outputs size must match readings");
1009 }
1010 std::copy(outputs.begin(), outputs.end(),
1011 info.stepwiseInfo.output);
1012 if (outputs.size() < ec::maxStepwisePoints)
1013 {
1014 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -08001015 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -07001016 }
James Feist7136a5a2018-07-19 09:52:05 -07001017 }
1018 }
1019 }
Patrick Venture39199b42020-10-08 14:40:29 -07001020 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -07001021 {
Patrick Venture39199b42020-10-08 14:40:29 -07001022 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -07001023 }
James Feistc959c422018-11-01 12:33:40 -07001024 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -07001025 {
James Feist1fe08952019-05-07 09:17:16 -07001026 std::cerr
1027 << "No fan zones, application pausing until new configuration\n";
1028 return false;
James Feist50fdfe32018-09-24 15:51:09 -07001029 }
James Feist1fe08952019-05-07 09:17:16 -07001030 return true;
James Feist7136a5a2018-07-19 09:52:05 -07001031}
Patrick Venturea0764872020-08-08 07:48:43 -07001032
James Feist7136a5a2018-07-19 09:52:05 -07001033} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -07001034} // namespace pid_control