blob: 460e9e365f0b72abaf8c778f28c1e284bf6322c1 [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Patrick Venture7e952d92020-10-05 15:58:52 -070016#include "dbusconfiguration.hpp"
James Feist7136a5a2018-07-19 09:52:05 -070017
Patrick Venture07716592018-10-14 11:46:40 -070018#include "conf.hpp"
Patrick Ventureef1f8862020-08-17 09:34:35 -070019#include "dbushelper.hpp"
20#include "dbusutil.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070021#include "util.hpp"
Patrick Venture07716592018-10-14 11:46:40 -070022
James Feist1fe08952019-05-07 09:17:16 -070023#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070024#include <sdbusplus/bus.hpp>
25#include <sdbusplus/bus/match.hpp>
26#include <sdbusplus/exception.hpp>
27
28#include <algorithm>
James Feist64f072a2018-08-10 16:39:24 -070029#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070030#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070031#include <iostream>
James Feist1fe08952019-05-07 09:17:16 -070032#include <list>
James Feist7136a5a2018-07-19 09:52:05 -070033#include <set>
34#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080035#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070036
Patrick Venturea0764872020-08-08 07:48:43 -070037namespace pid_control
38{
39
Patrick Venturee2ec0f62018-09-04 12:30:27 -070040constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070041 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070042constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070043 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070044constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070045 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070046constexpr const char* stepwiseConfigurationInterface =
47 "xyz.openbmc_project.Configuration.Stepwise";
James Feistf0096a02019-02-21 11:25:22 -080048constexpr const char* thermalControlIface =
49 "xyz.openbmc_project.Control.ThermalMode";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070050constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
Patrick Venture0911bfe2020-08-10 12:51:40 -070051constexpr const char* defaultPwmInterface =
52 "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070053
James Feist991ebd82020-07-21 11:14:52 -070054using Association = std::tuple<std::string, std::string, std::string>;
55using Associations = std::vector<Association>;
56
James Feist5ec20272019-07-10 11:59:57 -070057namespace thresholds
58{
59constexpr const char* warningInterface =
60 "xyz.openbmc_project.Sensor.Threshold.Warning";
61constexpr const char* criticalInterface =
62 "xyz.openbmc_project.Sensor.Threshold.Critical";
63const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh",
64 "WarningLow", "WarningHigh"};
65
66} // namespace thresholds
67
James Feist7136a5a2018-07-19 09:52:05 -070068namespace dbus_configuration
69{
Jason Lingf3b04fd2020-07-24 09:33:04 -070070using SensorInterfaceType = std::pair<std::string, std::string>;
71
72inline std::string getSensorNameFromPath(const std::string& dbusPath)
73{
74 return dbusPath.substr(dbusPath.find_last_of("/") + 1);
75}
76
77inline std::string sensorNameToDbusName(const std::string& sensorName)
78{
79 std::string retString = sensorName;
80 std::replace(retString.begin(), retString.end(), ' ', '_');
81 return retString;
82}
James Feist5ec20272019-07-10 11:59:57 -070083
Patrick Williamsb228bc32022-07-22 19:26:56 -050084std::vector<std::string> getSelectedProfiles(sdbusplus::bus_t& bus)
James Feistf0096a02019-02-21 11:25:22 -080085{
86 std::vector<std::string> ret;
87 auto mapper =
88 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
89 "/xyz/openbmc_project/object_mapper",
90 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
91 mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface});
92 std::unordered_map<
93 std::string, std::unordered_map<std::string, std::vector<std::string>>>
94 respData;
95
96 try
97 {
98 auto resp = bus.call(mapper);
99 resp.read(respData);
100 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500101 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800102 {
103 // can't do anything without mapper call data
104 throw std::runtime_error("ObjectMapper Call Failure");
105 }
106 if (respData.empty())
107 {
108 // if the user has profiles but doesn't expose the interface to select
109 // one, just go ahead without using profiles
110 return ret;
111 }
112
113 // assumption is that we should only have a small handful of selected
114 // profiles at a time (probably only 1), so calling each individually should
115 // not incur a large cost
116 for (const auto& objectPair : respData)
117 {
118 const std::string& path = objectPair.first;
119 for (const auto& ownerPair : objectPair.second)
120 {
121 const std::string& busName = ownerPair.first;
122 auto getProfile =
123 bus.new_method_call(busName.c_str(), path.c_str(),
124 "org.freedesktop.DBus.Properties", "Get");
125 getProfile.append(thermalControlIface, "Current");
126 std::variant<std::string> variantResp;
127 try
128 {
129 auto resp = bus.call(getProfile);
130 resp.read(variantResp);
131 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500132 catch (const sdbusplus::exception_t&)
James Feistf0096a02019-02-21 11:25:22 -0800133 {
134 throw std::runtime_error("Failure getting profile");
135 }
136 std::string mode = std::get<std::string>(variantResp);
137 ret.emplace_back(std::move(mode));
138 }
139 }
Patrick Venture39199b42020-10-08 14:40:29 -0700140 if constexpr (pid_control::conf::DEBUG)
James Feistf0096a02019-02-21 11:25:22 -0800141 {
142 std::cout << "Profiles selected: ";
143 for (const auto& profile : ret)
144 {
145 std::cout << profile << " ";
146 }
147 std::cout << "\n";
148 }
149 return ret;
150}
151
James Feist991ebd82020-07-21 11:14:52 -0700152int eventHandler(sd_bus_message* m, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700153{
James Feist1fe08952019-05-07 09:17:16 -0700154
James Feist991ebd82020-07-21 11:14:52 -0700155 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700156 {
157 throw std::runtime_error("Invalid match");
158 }
James Feist991ebd82020-07-21 11:14:52 -0700159
160 // we skip associations because the mapper populates these, not the sensors
161 const std::array<const char*, 1> skipList = {
162 "xyz.openbmc_project.Association"};
163
Patrick Williamsb228bc32022-07-22 19:26:56 -0500164 sdbusplus::message_t message(m);
James Feist991ebd82020-07-21 11:14:52 -0700165 if (std::string(message.get_member()) == "InterfacesAdded")
166 {
167 sdbusplus::message::object_path path;
168 std::unordered_map<
169 std::string,
170 std::unordered_map<std::string, std::variant<Associations, bool>>>
171 data;
172
173 message.read(path, data);
174
175 for (const char* skip : skipList)
176 {
177 auto find = data.find(skip);
178 if (find != data.end())
179 {
180 data.erase(find);
181 if (data.empty())
182 {
183 return 1;
184 }
185 }
186 }
187 }
188
James Feist1fe08952019-05-07 09:17:16 -0700189 boost::asio::steady_timer* timer =
190 static_cast<boost::asio::steady_timer*>(context);
191
192 // do a brief sleep as we tend to get a bunch of these events at
193 // once
194 timer->expires_after(std::chrono::seconds(2));
195 timer->async_wait([](const boost::system::error_code ec) {
196 if (ec == boost::asio::error::operation_aborted)
197 {
198 /* another timer started*/
199 return;
200 }
201
202 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800203 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700204 });
205
206 return 1;
207}
208
Patrick Williamsb228bc32022-07-22 19:26:56 -0500209void createMatches(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer)
James Feist1fe08952019-05-07 09:17:16 -0700210{
211 // this is a list because the matches can't be moved
Patrick Williamsb228bc32022-07-22 19:26:56 -0500212 static std::list<sdbusplus::bus::match_t> matches;
James Feist1fe08952019-05-07 09:17:16 -0700213
James Feist3987c8b2019-05-13 10:43:17 -0700214 const std::array<std::string, 4> interfaces = {
215 thermalControlIface, pidConfigurationInterface,
216 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700217
218 // this list only needs to be created once
219 if (!matches.empty())
220 {
221 return;
222 }
223
224 // we restart when the configuration changes or there are new sensors
225 for (const auto& interface : interfaces)
226 {
227 matches.emplace_back(
228 bus,
229 "type='signal',member='PropertiesChanged',arg0namespace='" +
230 interface + "'",
231 eventHandler, &timer);
232 }
233 matches.emplace_back(
234 bus,
235 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
236 "sensors/'",
237 eventHandler, &timer);
238}
239
Jason Ling6fc301f2020-07-23 12:39:57 -0700240/**
241 * retrieve an attribute from the pid configuration map
242 * @param[in] base - the PID configuration map, keys are the attributes and
243 * value is the variant associated with that attribute.
244 * @param attributeName - the name of the attribute
245 * @return a variant holding the value associated with a key
246 * @throw runtime_error : attributeName is not in base
247 */
248inline DbusVariantType getPIDAttribute(
249 const std::unordered_map<std::string, DbusVariantType>& base,
250 const std::string& attributeName)
251{
252 auto search = base.find(attributeName);
253 if (search == base.end())
254 {
255 throw std::runtime_error("missing attribute " + attributeName);
256 }
257 return search->second;
258}
259
James Feist5ec20272019-07-10 11:59:57 -0700260void populatePidInfo(
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800261 [[maybe_unused]] sdbusplus::bus_t& bus,
James Feist5ec20272019-07-10 11:59:57 -0700262 const std::unordered_map<std::string, DbusVariantType>& base,
Patrick Venture1df9e872020-10-08 15:35:01 -0700263 conf::ControllerInfo& info, const std::string* thresholdProperty,
Patrick Venture73823182020-10-08 15:12:51 -0700264 const std::map<std::string, conf::SensorConfig>& sensorConfig)
James Feist5ec20272019-07-10 11:59:57 -0700265{
Jason Ling6fc301f2020-07-23 12:39:57 -0700266 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700267 if (info.type == "fan")
268 {
269 info.setpoint = 0;
270 }
271 else
272 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700273 info.setpoint = std::visit(VariantToDoubleVisitor(),
274 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700275 }
276
277 if (thresholdProperty != nullptr)
278 {
279 std::string interface;
280 if (*thresholdProperty == "WarningHigh" ||
281 *thresholdProperty == "WarningLow")
282 {
283 interface = thresholds::warningInterface;
284 }
285 else
286 {
287 interface = thresholds::criticalInterface;
288 }
Patrick Venture73823182020-10-08 15:12:51 -0700289 const std::string& path = sensorConfig.at(info.inputs.front()).readPath;
James Feist5ec20272019-07-10 11:59:57 -0700290
Patrick Venture8729eb92020-08-10 10:38:44 -0700291 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700292 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700293 double reading = 0;
294 try
295 {
Patrick Venture9b936922020-08-10 11:28:39 -0700296 helper.getProperty(service, path, interface, *thresholdProperty,
297 reading);
James Feist5ec20272019-07-10 11:59:57 -0700298 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500299 catch (const sdbusplus::exception_t& ex)
James Feist5ec20272019-07-10 11:59:57 -0700300 {
301 // unsupported threshold, leaving reading at 0
302 }
303
304 info.setpoint += reading;
305 }
306
307 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700308 info.pidInfo.proportionalCoeff = std::visit(
309 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
310 info.pidInfo.integralCoeff = std::visit(
311 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
312 info.pidInfo.feedFwdOffset = std::visit(
313 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
314 info.pidInfo.feedFwdGain = std::visit(
315 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
316 info.pidInfo.integralLimit.max = std::visit(
317 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
318 info.pidInfo.integralLimit.min = std::visit(
319 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
320 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
321 getPIDAttribute(base, "OutLimitMax"));
322 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
323 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700324 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700325 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700326 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700327 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
James Feist5ec20272019-07-10 11:59:57 -0700328 double negativeHysteresis = 0;
329 double positiveHysteresis = 0;
330
331 auto findNeg = base.find("NegativeHysteresis");
332 auto findPos = base.find("PositiveHysteresis");
333
334 if (findNeg != base.end())
335 {
336 negativeHysteresis =
337 std::visit(VariantToDoubleVisitor(), findNeg->second);
338 }
339
340 if (findPos != base.end())
341 {
342 positiveHysteresis =
343 std::visit(VariantToDoubleVisitor(), findPos->second);
344 }
James Feist5ec20272019-07-10 11:59:57 -0700345 info.pidInfo.negativeHysteresis = negativeHysteresis;
346 info.pidInfo.positiveHysteresis = positiveHysteresis;
347}
348
Patrick Williamsb228bc32022-07-22 19:26:56 -0500349bool init(sdbusplus::bus_t& bus, boost::asio::steady_timer& timer,
Patrick Venture73823182020-10-08 15:12:51 -0700350 std::map<std::string, conf::SensorConfig>& sensorConfig,
351 std::map<int64_t, conf::PIDConf>& zoneConfig,
352 std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
James Feist1fe08952019-05-07 09:17:16 -0700353{
354
355 sensorConfig.clear();
356 zoneConfig.clear();
357 zoneDetailsConfig.clear();
358
359 createMatches(bus, timer);
360
James Feist7136a5a2018-07-19 09:52:05 -0700361 auto mapper =
362 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
363 "/xyz/openbmc_project/object_mapper",
364 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700365 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700366 std::array<const char*, 6>{
367 objectManagerInterface, pidConfigurationInterface,
368 pidZoneConfigurationInterface,
369 stepwiseConfigurationInterface, sensorInterface,
370 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700371 std::unordered_map<
372 std::string, std::unordered_map<std::string, std::vector<std::string>>>
373 respData;
James Feist22c257a2018-08-31 14:07:12 -0700374 try
375 {
376 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700377 resp.read(respData);
378 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500379 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700380 {
381 // can't do anything without mapper call data
382 throw std::runtime_error("ObjectMapper Call Failure");
383 }
James Feist7136a5a2018-07-19 09:52:05 -0700384
James Feist7136a5a2018-07-19 09:52:05 -0700385 if (respData.empty())
386 {
James Feist22c257a2018-08-31 14:07:12 -0700387 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700388 throw std::runtime_error("No configuration data available from Mapper");
389 }
390 // create a map of pair of <has pid configuration, ObjectManager path>
391 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
392 // and a map of <path, interface> for sensors
393 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700394 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700395 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700396 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700397 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700398 auto& owner = owners[ownerPair.first];
399 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700400 {
401
402 if (interface == objectManagerInterface)
403 {
404 owner.second = objectPair.first;
405 }
406 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700407 interface == pidZoneConfigurationInterface ||
408 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700409 {
410 owner.first = true;
411 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700412 if (interface == sensorInterface ||
413 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700414 {
415 // we're not interested in pwm sensors, just pwm control
416 if (interface == sensorInterface &&
417 objectPair.first.find("pwm") != std::string::npos)
418 {
419 continue;
420 }
421 sensors[objectPair.first] = interface;
422 }
423 }
424 }
425 }
426 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700427 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700428 {
429 // skip if no pid configuration (means probably a sensor)
430 if (!owner.second.first)
431 {
432 continue;
433 }
434 auto endpoint = bus.new_method_call(
435 owner.first.c_str(), owner.second.second.c_str(),
436 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700437 ManagedObjectType configuration;
438 try
James Feist7136a5a2018-07-19 09:52:05 -0700439 {
James Feist22c257a2018-08-31 14:07:12 -0700440 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700441 responce.read(configuration);
442 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500443 catch (const sdbusplus::exception_t&)
James Feist22c257a2018-08-31 14:07:12 -0700444 {
445 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700446 throw std::runtime_error("Error getting managed objects from " +
447 owner.first);
448 }
James Feist22c257a2018-08-31 14:07:12 -0700449
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700450 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700451 {
452 if (pathPair.second.find(pidConfigurationInterface) !=
453 pathPair.second.end() ||
454 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700455 pathPair.second.end() ||
456 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700457 pathPair.second.end())
458 {
459 configurations.emplace(pathPair);
460 }
James Feistf0096a02019-02-21 11:25:22 -0800461 }
462 }
463
464 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700465 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
466 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800467 {
James Feist3987c8b2019-05-13 10:43:17 -0700468 for (auto pathIt = configurations.begin();
469 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800470 {
James Feist3987c8b2019-05-13 10:43:17 -0700471 for (auto confIt = pathIt->second.begin();
472 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800473 {
James Feist3987c8b2019-05-13 10:43:17 -0700474 auto profilesFind = confIt->second.find("Profiles");
475 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800476 {
James Feist3987c8b2019-05-13 10:43:17 -0700477 confIt++;
478 continue; // if no profiles selected, apply always
479 }
480 auto profiles =
481 std::get<std::vector<std::string>>(profilesFind->second);
482 if (profiles.empty())
483 {
484 confIt++;
485 continue;
486 }
487
488 bool found = false;
489 for (const std::string& profile : profiles)
490 {
491 if (std::find(selectedProfiles.begin(),
492 selectedProfiles.end(),
493 profile) != selectedProfiles.end())
494 {
495 found = true;
496 break;
497 }
498 }
499 if (found)
500 {
501 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800502 }
503 else
504 {
James Feist3987c8b2019-05-13 10:43:17 -0700505 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800506 }
507 }
James Feist3987c8b2019-05-13 10:43:17 -0700508 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800509 {
James Feist3987c8b2019-05-13 10:43:17 -0700510 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800511 }
James Feist3987c8b2019-05-13 10:43:17 -0700512 else
James Feistf0096a02019-02-21 11:25:22 -0800513 {
James Feist3987c8b2019-05-13 10:43:17 -0700514 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800515 }
James Feist7136a5a2018-07-19 09:52:05 -0700516 }
517 }
James Feist8c3c51e2018-08-08 16:31:43 -0700518
Josh Lehan998fbe62020-09-20 21:21:05 -0700519 // On D-Bus, although not necessary,
520 // having the "zoneID" field can still be useful,
521 // as it is used for diagnostic messages,
522 // logging file names, and so on.
523 // Accept optional "ZoneIndex" parameter to explicitly specify.
524 // If not present, or not unique, auto-assign index,
525 // using 0-based numbering, ensuring uniqueness.
526 std::map<std::string, int64_t> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700527 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700528 {
529 auto findZone =
530 configuration.second.find(pidZoneConfigurationInterface);
531 if (findZone != configuration.second.end())
532 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700533 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800534
James Feist1f802f52019-02-08 13:51:43 -0800535 const std::string& name = std::get<std::string>(zone.at("Name"));
Josh Lehan998fbe62020-09-20 21:21:05 -0700536
537 auto findZoneIndex = zone.find("ZoneIndex");
538 if (findZoneIndex == zone.end())
539 {
540 continue;
541 }
542
543 auto ptrZoneIndex = std::get_if<double>(&(findZoneIndex->second));
544 if (!ptrZoneIndex)
545 {
546 continue;
547 }
548
549 auto desiredIndex = static_cast<int64_t>(*ptrZoneIndex);
550 auto grantedIndex = setZoneIndex(name, foundZones, desiredIndex);
551 std::cout << "Zone " << name << " is at ZoneIndex " << grantedIndex
552 << "\n";
553 }
554 }
555
556 for (const auto& configuration : configurations)
557 {
558 auto findZone =
559 configuration.second.find(pidZoneConfigurationInterface);
560 if (findZone != configuration.second.end())
561 {
562 const auto& zone = findZone->second;
563
564 const std::string& name = std::get<std::string>(zone.at("Name"));
565
566 auto index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700567
Patrick Venturec54fbd82018-10-30 19:40:05 -0700568 auto& details = zoneDetailsConfig[index];
Josh Lehan998fbe62020-09-20 21:21:05 -0700569
James Feist3484bed2019-02-25 13:28:18 -0800570 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
571 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800572 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800573 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700574 }
575 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700576 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700577 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700578 {
James Feist22c257a2018-08-31 14:07:12 -0700579 const auto& base =
580 configuration.second.at(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700581 const std::string pidName = std::get<std::string>(base.at("Name"));
582 const std::string pidClass =
583 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700584 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800585 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700586 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700587 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700588 auto index = getZoneIndex(zone, foundZones);
589
James Feistf81f2882019-02-26 11:26:36 -0800590 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700591 std::vector<std::string> inputSensorNames(
592 std::get<std::vector<std::string>>(base.at("Inputs")));
593 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700594
Jason Lingf3b04fd2020-07-24 09:33:04 -0700595 // assumption: all fan pids must have at least one output
596 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700597 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700598 outputSensorNames = std::get<std::vector<std::string>>(
599 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700600 }
James Feist1738e2a2019-02-04 15:57:03 -0800601
Alex.Song8f73ad72021-10-07 00:18:27 +0800602 bool unavailableAsFailed = true;
603 auto findUnavailableAsFailed =
604 base.find("InputUnavailableAsFailed");
605 if (findUnavailableAsFailed != base.end())
606 {
607 unavailableAsFailed =
608 std::get<bool>(findUnavailableAsFailed->second);
609 }
610
Jason Lingf3b04fd2020-07-24 09:33:04 -0700611 std::vector<SensorInterfaceType> inputSensorInterfaces;
612 std::vector<SensorInterfaceType> outputSensorInterfaces;
613 /* populate an interface list for different sensor direction
614 * types (input,output)
615 */
616 /* take the Inputs from the configuration and generate
617 * a list of dbus descriptors (path, interface).
618 * Mapping can be many-to-one since an element of Inputs can be
619 * a regex
620 */
621 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700622 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700623 findSensors(sensors, sensorNameToDbusName(sensorName),
624 inputSensorInterfaces);
625 }
626 for (const std::string& sensorName : outputSensorNames)
627 {
628 findSensors(sensors, sensorNameToDbusName(sensorName),
629 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800630 }
James Feist50fdfe32018-09-24 15:51:09 -0700631
Jason Lingf3b04fd2020-07-24 09:33:04 -0700632 inputSensorNames.clear();
633 for (const SensorInterfaceType& inputSensorInterface :
634 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800635 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700636 const std::string& dbusInterface =
637 inputSensorInterface.second;
638 const std::string& inputSensorPath =
639 inputSensorInterface.first;
Josh Lehanfb82a872020-09-20 21:48:22 -0700640
641 // Setting timeout to 0 is intentional, as D-Bus passive
642 // sensor updates are pushed in, not pulled by timer poll.
643 // Setting ignoreDbusMinMax is intentional, as this
644 // prevents normalization of values to [0.0, 1.0] range,
645 // which would mess up the PID loop math.
646 // All non-fan PID classes should be initialized this way.
647 // As for why a fan should not use this code path, see
648 // the ed1dafdf168def37c65bfb7a5efd18d9dbe04727 commit.
649 if ((pidClass == "temp") || (pidClass == "margin"))
James Feist50fdfe32018-09-24 15:51:09 -0700650 {
Harvey.Wued1dafd2022-02-09 13:53:20 +0800651 std::string inputSensorName =
652 getSensorNameFromPath(inputSensorPath);
653 auto& config = sensorConfig[inputSensorName];
654 inputSensorNames.push_back(inputSensorName);
655 config.type = pidClass;
656 config.readPath = inputSensorInterface.first;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700657 config.timeout = 0;
658 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800659 config.unavailableAsFailed = unavailableAsFailed;
James Feist50fdfe32018-09-24 15:51:09 -0700660 }
Josh Lehanfb82a872020-09-20 21:48:22 -0700661
Jason Lingf3b04fd2020-07-24 09:33:04 -0700662 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700663 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700664 /* all expected inputs in the configuration are expected
665 * to be sensor interfaces
666 */
667 throw std::runtime_error(
668 "sensor at dbus path [" + inputSensorPath +
669 "] has an interface [" + dbusInterface +
670 "] that does not match the expected interface of " +
671 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700672 }
673 }
James Feist1738e2a2019-02-04 15:57:03 -0800674
Jason Lingf3b04fd2020-07-24 09:33:04 -0700675 /* fan pids need to pair up tach sensors with their pwm
676 * counterparts
677 */
678 if (pidClass == "fan")
679 {
680 /* If a PID is a fan there should be either
681 * (1) one output(pwm) per input(tach)
682 * OR
683 * (2) one putput(pwm) for all inputs(tach)
684 * everything else indicates a bad configuration.
685 */
686 bool singlePwm = false;
687 if (outputSensorInterfaces.size() == 1)
688 {
689 /* one pwm, set write paths for all fan sensors to it */
690 singlePwm = true;
691 }
692 else if (inputSensorInterfaces.size() ==
693 outputSensorInterfaces.size())
694 {
695 /* one to one mapping, each fan sensor gets its own pwm
696 * control */
697 singlePwm = false;
698 }
699 else
700 {
701 throw std::runtime_error(
702 "fan PID has invalid number of Outputs");
703 }
704 std::string fanSensorName;
705 std::string pwmPath;
706 std::string pwmInterface;
Harvey.Wued1dafd2022-02-09 13:53:20 +0800707 std::string pwmSensorName;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700708 if (singlePwm)
709 {
710 /* if just a single output(pwm) is provided then use
711 * that pwm control path for all the fan sensor write
712 * path configs
713 */
714 pwmPath = outputSensorInterfaces.at(0).first;
715 pwmInterface = outputSensorInterfaces.at(0).second;
716 }
717 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
718 idx++)
719 {
720 if (!singlePwm)
721 {
722 pwmPath = outputSensorInterfaces.at(idx).first;
723 pwmInterface =
724 outputSensorInterfaces.at(idx).second;
725 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700726 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700727 {
728 throw std::runtime_error(
729 "fan pwm control at dbus path [" + pwmPath +
730 "] has an interface [" + pwmInterface +
731 "] that does not match the expected interface "
732 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700733 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700734 }
735 const std::string& fanPath =
736 inputSensorInterfaces.at(idx).first;
737 fanSensorName = getSensorNameFromPath(fanPath);
Harvey.Wued1dafd2022-02-09 13:53:20 +0800738 pwmSensorName = getSensorNameFromPath(pwmPath);
739 std::string fanPwmIndex = fanSensorName + pwmSensorName;
740 inputSensorNames.push_back(fanPwmIndex);
741 auto& fanConfig = sensorConfig[fanPwmIndex];
742 fanConfig.type = pidClass;
743 fanConfig.readPath = fanPath;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700744 fanConfig.writePath = pwmPath;
745 // todo: un-hardcode this if there are fans with
746 // different ranges
747 fanConfig.max = 255;
748 fanConfig.min = 0;
749 }
750 }
James Feist11d243d2019-06-24 16:18:40 -0700751 // if the sensors aren't available in the current state, don't
752 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700753 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700754 {
755 continue;
756 }
757
James Feist5ec20272019-07-10 11:59:57 -0700758 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700759
James Feist5ec20272019-07-10 11:59:57 -0700760 // SetPointOffset is a threshold value to pull from the sensor
761 // to apply an offset. For upper thresholds this means the
762 // setpoint is usually negative.
763 auto findSetpointOffset = base.find("SetPointOffset");
764 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700765 {
James Feist5ec20272019-07-10 11:59:57 -0700766 offsetType =
767 std::get<std::string>(findSetpointOffset->second);
768 if (std::find(thresholds::types.begin(),
769 thresholds::types.end(),
770 offsetType) == thresholds::types.end())
771 {
772 throw std::runtime_error("Unsupported type: " +
773 offsetType);
774 }
775 }
776
777 if (offsetType.empty())
778 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700779 conf::ControllerInfo& info =
James Feist5ec20272019-07-10 11:59:57 -0700780 conf[std::get<std::string>(base.at("Name"))];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700781 info.inputs = std::move(inputSensorNames);
Patrick Venture73823182020-10-08 15:12:51 -0700782 populatePidInfo(bus, base, info, nullptr, sensorConfig);
James Feist22c257a2018-08-31 14:07:12 -0700783 }
784 else
785 {
James Feist5ec20272019-07-10 11:59:57 -0700786 // we have to split up the inputs, as in practice t-control
787 // values will differ, making setpoints differ
Jason Lingf3b04fd2020-07-24 09:33:04 -0700788 for (const std::string& input : inputSensorNames)
James Feist5ec20272019-07-10 11:59:57 -0700789 {
Patrick Venture1df9e872020-10-08 15:35:01 -0700790 conf::ControllerInfo& info = conf[input];
James Feist5ec20272019-07-10 11:59:57 -0700791 info.inputs.emplace_back(input);
Patrick Venture73823182020-10-08 15:12:51 -0700792 populatePidInfo(bus, base, info, &offsetType,
793 sensorConfig);
James Feist5ec20272019-07-10 11:59:57 -0700794 }
James Feist22c257a2018-08-31 14:07:12 -0700795 }
James Feist22c257a2018-08-31 14:07:12 -0700796 }
797 }
798 auto findStepwise =
799 configuration.second.find(stepwiseConfigurationInterface);
800 if (findStepwise != configuration.second.end())
801 {
802 const auto& base = findStepwise->second;
803 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800804 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700805 for (const std::string& zone : zones)
806 {
Josh Lehan998fbe62020-09-20 21:21:05 -0700807 auto index = getZoneIndex(zone, foundZones);
808
James Feistf81f2882019-02-26 11:26:36 -0800809 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700810
811 std::vector<std::string> inputs;
812 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800813 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700814
Alex.Song8f73ad72021-10-07 00:18:27 +0800815 bool unavailableAsFailed = true;
816 auto findUnavailableAsFailed =
817 base.find("InputUnavailableAsFailed");
818 if (findUnavailableAsFailed != base.end())
819 {
820 unavailableAsFailed =
821 std::get<bool>(findUnavailableAsFailed->second);
822 }
823
James Feist1738e2a2019-02-04 15:57:03 -0800824 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700825 for (const std::string& sensorName : sensorNames)
826 {
James Feist1738e2a2019-02-04 15:57:03 -0800827 std::vector<std::pair<std::string, std::string>>
828 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700829 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
830 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700831 {
James Feist50fdfe32018-09-24 15:51:09 -0700832 break;
833 }
834
James Feist1738e2a2019-02-04 15:57:03 -0800835 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
836 {
837 size_t idx =
838 sensorPathIfacePair.first.find_last_of("/") + 1;
839 std::string shortName =
840 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700841
James Feist1738e2a2019-02-04 15:57:03 -0800842 inputs.push_back(shortName);
843 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800844 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800845 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800846 config.ignoreDbusMinMax = true;
Alex.Song8f73ad72021-10-07 00:18:27 +0800847 config.unavailableAsFailed = unavailableAsFailed;
James Feist1738e2a2019-02-04 15:57:03 -0800848 // todo: maybe un-hardcode this if we run into slower
849 // timeouts with sensors
850
James Feist2642cb52019-02-25 13:00:16 -0800851 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800852 sensorFound = true;
853 }
James Feist50fdfe32018-09-24 15:51:09 -0700854 }
855 if (!sensorFound)
856 {
857 continue;
858 }
Patrick Venture1df9e872020-10-08 15:35:01 -0700859 conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800860 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700861 info.inputs = std::move(inputs);
862
James Feist22c257a2018-08-31 14:07:12 -0700863 info.type = "stepwise";
864 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700865 info.stepwiseInfo.positiveHysteresis = 0.0;
866 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800867 std::string subtype = std::get<std::string>(base.at("Class"));
868
869 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700870 auto findPosHyst = base.find("PositiveHysteresis");
871 auto findNegHyst = base.find("NegativeHysteresis");
872 if (findPosHyst != base.end())
873 {
James Feist1f802f52019-02-08 13:51:43 -0800874 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800875 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700876 }
877 if (findNegHyst != base.end())
878 {
James Feist5782ab82019-04-02 08:38:48 -0700879 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800880 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700881 }
James Feist22c257a2018-08-31 14:07:12 -0700882 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800883 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700884 if (readings.size() > ec::maxStepwisePoints)
885 {
886 throw std::invalid_argument("Too many stepwise points.");
887 }
888 if (readings.empty())
889 {
890 throw std::invalid_argument(
891 "Must have one stepwise point.");
892 }
893 std::copy(readings.begin(), readings.end(),
894 info.stepwiseInfo.reading);
895 if (readings.size() < ec::maxStepwisePoints)
896 {
897 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800898 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700899 }
900 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800901 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700902 if (readings.size() != outputs.size())
903 {
904 throw std::invalid_argument(
905 "Outputs size must match readings");
906 }
907 std::copy(outputs.begin(), outputs.end(),
908 info.stepwiseInfo.output);
909 if (outputs.size() < ec::maxStepwisePoints)
910 {
911 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800912 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700913 }
James Feist7136a5a2018-07-19 09:52:05 -0700914 }
915 }
916 }
Patrick Venture39199b42020-10-08 14:40:29 -0700917 if constexpr (pid_control::conf::DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700918 {
Patrick Venture39199b42020-10-08 14:40:29 -0700919 debugPrint(sensorConfig, zoneConfig, zoneDetailsConfig);
James Feist7136a5a2018-07-19 09:52:05 -0700920 }
James Feistc959c422018-11-01 12:33:40 -0700921 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700922 {
James Feist1fe08952019-05-07 09:17:16 -0700923 std::cerr
924 << "No fan zones, application pausing until new configuration\n";
925 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700926 }
James Feist1fe08952019-05-07 09:17:16 -0700927 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700928}
Patrick Venturea0764872020-08-08 07:48:43 -0700929
James Feist7136a5a2018-07-19 09:52:05 -0700930} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -0700931} // namespace pid_control