blob: a5154397a6f650435dd7a4335adfc894f199200c [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 Feist1738e2a2019-02-04 15:57:03 -080033#include <regex>
James Feist7136a5a2018-07-19 09:52:05 -070034#include <set>
35#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080036#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070037
Patrick Venturea0764872020-08-08 07:48:43 -070038namespace pid_control
39{
40
James Feist7136a5a2018-07-19 09:52:05 -070041static constexpr bool DEBUG = false; // enable to print found configuration
42
James Feistf81f2882019-02-26 11:26:36 -080043extern std::map<std::string, struct conf::SensorConfig> sensorConfig;
44extern std::map<int64_t, conf::PIDConf> zoneConfig;
45extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig;
James Feist7136a5a2018-07-19 09:52:05 -070046
Patrick Venturee2ec0f62018-09-04 12:30:27 -070047constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070048 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070049constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070050 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070051constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070052 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070053constexpr const char* stepwiseConfigurationInterface =
54 "xyz.openbmc_project.Configuration.Stepwise";
James Feistf0096a02019-02-21 11:25:22 -080055constexpr const char* thermalControlIface =
56 "xyz.openbmc_project.Control.ThermalMode";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070057constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
Patrick Venture0911bfe2020-08-10 12:51:40 -070058constexpr const char* defaultPwmInterface =
59 "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070060
James Feist991ebd82020-07-21 11:14:52 -070061using Association = std::tuple<std::string, std::string, std::string>;
62using Associations = std::vector<Association>;
63
James Feist5ec20272019-07-10 11:59:57 -070064namespace thresholds
65{
66constexpr const char* warningInterface =
67 "xyz.openbmc_project.Sensor.Threshold.Warning";
68constexpr const char* criticalInterface =
69 "xyz.openbmc_project.Sensor.Threshold.Critical";
70const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh",
71 "WarningLow", "WarningHigh"};
72
73} // namespace thresholds
74
James Feist7136a5a2018-07-19 09:52:05 -070075namespace dbus_configuration
76{
Jason Lingf3b04fd2020-07-24 09:33:04 -070077using SensorInterfaceType = std::pair<std::string, std::string>;
78
79inline std::string getSensorNameFromPath(const std::string& dbusPath)
80{
81 return dbusPath.substr(dbusPath.find_last_of("/") + 1);
82}
83
84inline std::string sensorNameToDbusName(const std::string& sensorName)
85{
86 std::string retString = sensorName;
87 std::replace(retString.begin(), retString.end(), ' ', '_');
88 return retString;
89}
James Feist5ec20272019-07-10 11:59:57 -070090
James Feist1738e2a2019-02-04 15:57:03 -080091bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
92 const std::string& search,
93 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070094{
James Feist1738e2a2019-02-04 15:57:03 -080095 std::smatch match;
Jae Hyun Yoo1a704dc2020-06-04 15:00:10 -070096 std::regex reg(search + '$');
James Feist1738e2a2019-02-04 15:57:03 -080097 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070098 {
James Feist1738e2a2019-02-04 15:57:03 -080099 if (std::regex_search(sensor.first, match, reg))
100 {
101 matches.push_back(sensor);
102 }
James Feist7136a5a2018-07-19 09:52:05 -0700103 }
James Feist1738e2a2019-02-04 15:57:03 -0800104 return matches.size() > 0;
James Feist7136a5a2018-07-19 09:52:05 -0700105}
106
107// this function prints the configuration into a form similar to the cpp
108// generated code to help in verification, should be turned off during normal
109// use
110void debugPrint(void)
111{
112 // print sensor config
113 std::cout << "sensor config:\n";
114 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700115 for (const auto& pair : sensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700116 {
117
118 std::cout << "\t{" << pair.first << ",\n\t\t{";
119 std::cout << pair.second.type << ", ";
Patrick Venture69c51062019-02-11 09:46:03 -0800120 std::cout << pair.second.readPath << ", ";
121 std::cout << pair.second.writePath << ", ";
James Feist7136a5a2018-07-19 09:52:05 -0700122 std::cout << pair.second.min << ", ";
123 std::cout << pair.second.max << ", ";
124 std::cout << pair.second.timeout << "},\n\t},\n";
125 }
126 std::cout << "}\n\n";
127 std::cout << "ZoneDetailsConfig\n";
128 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700129 for (const auto& zone : zoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700130 {
131 std::cout << "\t{" << zone.first << ",\n";
James Feist3484bed2019-02-25 13:28:18 -0800132 std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800133 std::cout << zone.second.failsafePercent << "}\n\t},\n";
James Feist7136a5a2018-07-19 09:52:05 -0700134 }
135 std::cout << "}\n\n";
136 std::cout << "ZoneConfig\n";
137 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700138 for (const auto& zone : zoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700139 {
140 std::cout << "\t{" << zone.first << "\n";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700141 for (const auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700142 {
143 std::cout << "\t\t{" << pidconf.first << ",\n";
144 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
145 std::cout << "\t\t\t{";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700146 for (const auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700147 {
148 std::cout << "\n\t\t\t" << input << ",\n";
149 }
150 std::cout << "\t\t\t}\n";
151 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700152 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
Patrick Venture7442c372019-02-11 10:21:05 -0800153 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
154 << ",\n";
155 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
156 << ",\n";
157 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
158 << ",\n";
159 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
160 << ",\n";
161 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
162 << "," << pidconf.second.pidInfo.integralLimit.max
163 << "},\n";
164 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
165 << pidconf.second.pidInfo.outLim.max << "},\n";
166 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
167 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700168 std::cout << "\t\t\t}\n\t\t}\n";
169 }
170 std::cout << "\t},\n";
171 }
172 std::cout << "}\n\n";
173}
174
James Feistffd418b2018-11-15 14:46:36 -0800175size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
176{
177 auto it = std::find(zones.begin(), zones.end(), name);
178 if (it == zones.end())
179 {
180 zones.emplace_back(name);
181 it = zones.end() - 1;
182 }
183
184 return it - zones.begin();
185}
186
James Feistf0096a02019-02-21 11:25:22 -0800187std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus)
188{
189 std::vector<std::string> ret;
190 auto mapper =
191 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
192 "/xyz/openbmc_project/object_mapper",
193 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
194 mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface});
195 std::unordered_map<
196 std::string, std::unordered_map<std::string, std::vector<std::string>>>
197 respData;
198
199 try
200 {
201 auto resp = bus.call(mapper);
202 resp.read(respData);
203 }
204 catch (sdbusplus::exception_t&)
205 {
206 // can't do anything without mapper call data
207 throw std::runtime_error("ObjectMapper Call Failure");
208 }
209 if (respData.empty())
210 {
211 // if the user has profiles but doesn't expose the interface to select
212 // one, just go ahead without using profiles
213 return ret;
214 }
215
216 // assumption is that we should only have a small handful of selected
217 // profiles at a time (probably only 1), so calling each individually should
218 // not incur a large cost
219 for (const auto& objectPair : respData)
220 {
221 const std::string& path = objectPair.first;
222 for (const auto& ownerPair : objectPair.second)
223 {
224 const std::string& busName = ownerPair.first;
225 auto getProfile =
226 bus.new_method_call(busName.c_str(), path.c_str(),
227 "org.freedesktop.DBus.Properties", "Get");
228 getProfile.append(thermalControlIface, "Current");
229 std::variant<std::string> variantResp;
230 try
231 {
232 auto resp = bus.call(getProfile);
233 resp.read(variantResp);
234 }
235 catch (sdbusplus::exception_t&)
236 {
237 throw std::runtime_error("Failure getting profile");
238 }
239 std::string mode = std::get<std::string>(variantResp);
240 ret.emplace_back(std::move(mode));
241 }
242 }
243 if constexpr (DEBUG)
244 {
245 std::cout << "Profiles selected: ";
246 for (const auto& profile : ret)
247 {
248 std::cout << profile << " ";
249 }
250 std::cout << "\n";
251 }
252 return ret;
253}
254
James Feist991ebd82020-07-21 11:14:52 -0700255int eventHandler(sd_bus_message* m, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700256{
James Feist1fe08952019-05-07 09:17:16 -0700257
James Feist991ebd82020-07-21 11:14:52 -0700258 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700259 {
260 throw std::runtime_error("Invalid match");
261 }
James Feist991ebd82020-07-21 11:14:52 -0700262
263 // we skip associations because the mapper populates these, not the sensors
264 const std::array<const char*, 1> skipList = {
265 "xyz.openbmc_project.Association"};
266
267 sdbusplus::message::message message(m);
268 if (std::string(message.get_member()) == "InterfacesAdded")
269 {
270 sdbusplus::message::object_path path;
271 std::unordered_map<
272 std::string,
273 std::unordered_map<std::string, std::variant<Associations, bool>>>
274 data;
275
276 message.read(path, data);
277
278 for (const char* skip : skipList)
279 {
280 auto find = data.find(skip);
281 if (find != data.end())
282 {
283 data.erase(find);
284 if (data.empty())
285 {
286 return 1;
287 }
288 }
289 }
290 }
291
James Feist1fe08952019-05-07 09:17:16 -0700292 boost::asio::steady_timer* timer =
293 static_cast<boost::asio::steady_timer*>(context);
294
295 // do a brief sleep as we tend to get a bunch of these events at
296 // once
297 timer->expires_after(std::chrono::seconds(2));
298 timer->async_wait([](const boost::system::error_code ec) {
299 if (ec == boost::asio::error::operation_aborted)
300 {
301 /* another timer started*/
302 return;
303 }
304
305 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800306 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700307 });
308
309 return 1;
310}
311
312void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
313{
314 // this is a list because the matches can't be moved
315 static std::list<sdbusplus::bus::match::match> matches;
316
James Feist3987c8b2019-05-13 10:43:17 -0700317 const std::array<std::string, 4> interfaces = {
318 thermalControlIface, pidConfigurationInterface,
319 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700320
321 // this list only needs to be created once
322 if (!matches.empty())
323 {
324 return;
325 }
326
327 // we restart when the configuration changes or there are new sensors
328 for (const auto& interface : interfaces)
329 {
330 matches.emplace_back(
331 bus,
332 "type='signal',member='PropertiesChanged',arg0namespace='" +
333 interface + "'",
334 eventHandler, &timer);
335 }
336 matches.emplace_back(
337 bus,
338 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
339 "sensors/'",
340 eventHandler, &timer);
341}
342
Jason Ling6fc301f2020-07-23 12:39:57 -0700343/**
344 * retrieve an attribute from the pid configuration map
345 * @param[in] base - the PID configuration map, keys are the attributes and
346 * value is the variant associated with that attribute.
347 * @param attributeName - the name of the attribute
348 * @return a variant holding the value associated with a key
349 * @throw runtime_error : attributeName is not in base
350 */
351inline DbusVariantType getPIDAttribute(
352 const std::unordered_map<std::string, DbusVariantType>& base,
353 const std::string& attributeName)
354{
355 auto search = base.find(attributeName);
356 if (search == base.end())
357 {
358 throw std::runtime_error("missing attribute " + attributeName);
359 }
360 return search->second;
361}
362
James Feist5ec20272019-07-10 11:59:57 -0700363void populatePidInfo(
364 sdbusplus::bus::bus& bus,
365 const std::unordered_map<std::string, DbusVariantType>& base,
366 struct conf::ControllerInfo& info, const std::string* thresholdProperty)
367{
Jason Ling6fc301f2020-07-23 12:39:57 -0700368 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700369 if (info.type == "fan")
370 {
371 info.setpoint = 0;
372 }
373 else
374 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700375 info.setpoint = std::visit(VariantToDoubleVisitor(),
376 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700377 }
378
379 if (thresholdProperty != nullptr)
380 {
381 std::string interface;
382 if (*thresholdProperty == "WarningHigh" ||
383 *thresholdProperty == "WarningLow")
384 {
385 interface = thresholds::warningInterface;
386 }
387 else
388 {
389 interface = thresholds::criticalInterface;
390 }
391 const std::string& path = sensorConfig[info.inputs.front()].readPath;
392
Patrick Venture8729eb92020-08-10 10:38:44 -0700393 DbusHelper helper(sdbusplus::bus::new_system());
Patrick Venture9b936922020-08-10 11:28:39 -0700394 std::string service = helper.getService(interface, path);
James Feist5ec20272019-07-10 11:59:57 -0700395 double reading = 0;
396 try
397 {
Patrick Venture9b936922020-08-10 11:28:39 -0700398 helper.getProperty(service, path, interface, *thresholdProperty,
399 reading);
James Feist5ec20272019-07-10 11:59:57 -0700400 }
401 catch (const sdbusplus::exception::SdBusError& ex)
402 {
403 // unsupported threshold, leaving reading at 0
404 }
405
406 info.setpoint += reading;
407 }
408
409 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700410 info.pidInfo.proportionalCoeff = std::visit(
411 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
412 info.pidInfo.integralCoeff = std::visit(
413 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
414 info.pidInfo.feedFwdOffset = std::visit(
415 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
416 info.pidInfo.feedFwdGain = std::visit(
417 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
418 info.pidInfo.integralLimit.max = std::visit(
419 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
420 info.pidInfo.integralLimit.min = std::visit(
421 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
422 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
423 getPIDAttribute(base, "OutLimitMax"));
424 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
425 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700426 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700427 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700428 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700429 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
James Feist5ec20272019-07-10 11:59:57 -0700430 double negativeHysteresis = 0;
431 double positiveHysteresis = 0;
432
433 auto findNeg = base.find("NegativeHysteresis");
434 auto findPos = base.find("PositiveHysteresis");
435
436 if (findNeg != base.end())
437 {
438 negativeHysteresis =
439 std::visit(VariantToDoubleVisitor(), findNeg->second);
440 }
441
442 if (findPos != base.end())
443 {
444 positiveHysteresis =
445 std::visit(VariantToDoubleVisitor(), findPos->second);
446 }
James Feist5ec20272019-07-10 11:59:57 -0700447 info.pidInfo.negativeHysteresis = negativeHysteresis;
448 info.pidInfo.positiveHysteresis = positiveHysteresis;
449}
450
James Feist1fe08952019-05-07 09:17:16 -0700451bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
452{
453
454 sensorConfig.clear();
455 zoneConfig.clear();
456 zoneDetailsConfig.clear();
457
458 createMatches(bus, timer);
459
James Feist7136a5a2018-07-19 09:52:05 -0700460 auto mapper =
461 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
462 "/xyz/openbmc_project/object_mapper",
463 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700464 mapper.append("/", 0,
Patrick Venture0911bfe2020-08-10 12:51:40 -0700465 std::array<const char*, 6>{
466 objectManagerInterface, pidConfigurationInterface,
467 pidZoneConfigurationInterface,
468 stepwiseConfigurationInterface, sensorInterface,
469 defaultPwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700470 std::unordered_map<
471 std::string, std::unordered_map<std::string, std::vector<std::string>>>
472 respData;
James Feist22c257a2018-08-31 14:07:12 -0700473 try
474 {
475 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700476 resp.read(respData);
477 }
478 catch (sdbusplus::exception_t&)
479 {
480 // can't do anything without mapper call data
481 throw std::runtime_error("ObjectMapper Call Failure");
482 }
James Feist7136a5a2018-07-19 09:52:05 -0700483
James Feist7136a5a2018-07-19 09:52:05 -0700484 if (respData.empty())
485 {
James Feist22c257a2018-08-31 14:07:12 -0700486 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700487 throw std::runtime_error("No configuration data available from Mapper");
488 }
489 // create a map of pair of <has pid configuration, ObjectManager path>
490 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
491 // and a map of <path, interface> for sensors
492 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700493 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700494 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700495 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700496 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700497 auto& owner = owners[ownerPair.first];
498 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700499 {
500
501 if (interface == objectManagerInterface)
502 {
503 owner.second = objectPair.first;
504 }
505 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700506 interface == pidZoneConfigurationInterface ||
507 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700508 {
509 owner.first = true;
510 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700511 if (interface == sensorInterface ||
512 interface == defaultPwmInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700513 {
514 // we're not interested in pwm sensors, just pwm control
515 if (interface == sensorInterface &&
516 objectPair.first.find("pwm") != std::string::npos)
517 {
518 continue;
519 }
520 sensors[objectPair.first] = interface;
521 }
522 }
523 }
524 }
525 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700526 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700527 {
528 // skip if no pid configuration (means probably a sensor)
529 if (!owner.second.first)
530 {
531 continue;
532 }
533 auto endpoint = bus.new_method_call(
534 owner.first.c_str(), owner.second.second.c_str(),
535 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700536 ManagedObjectType configuration;
537 try
James Feist7136a5a2018-07-19 09:52:05 -0700538 {
James Feist22c257a2018-08-31 14:07:12 -0700539 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700540 responce.read(configuration);
541 }
542 catch (sdbusplus::exception_t&)
543 {
544 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700545 throw std::runtime_error("Error getting managed objects from " +
546 owner.first);
547 }
James Feist22c257a2018-08-31 14:07:12 -0700548
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700549 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700550 {
551 if (pathPair.second.find(pidConfigurationInterface) !=
552 pathPair.second.end() ||
553 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700554 pathPair.second.end() ||
555 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700556 pathPair.second.end())
557 {
558 configurations.emplace(pathPair);
559 }
James Feistf0096a02019-02-21 11:25:22 -0800560 }
561 }
562
563 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700564 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
565 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800566 {
James Feist3987c8b2019-05-13 10:43:17 -0700567 for (auto pathIt = configurations.begin();
568 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800569 {
James Feist3987c8b2019-05-13 10:43:17 -0700570 for (auto confIt = pathIt->second.begin();
571 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800572 {
James Feist3987c8b2019-05-13 10:43:17 -0700573 auto profilesFind = confIt->second.find("Profiles");
574 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800575 {
James Feist3987c8b2019-05-13 10:43:17 -0700576 confIt++;
577 continue; // if no profiles selected, apply always
578 }
579 auto profiles =
580 std::get<std::vector<std::string>>(profilesFind->second);
581 if (profiles.empty())
582 {
583 confIt++;
584 continue;
585 }
586
587 bool found = false;
588 for (const std::string& profile : profiles)
589 {
590 if (std::find(selectedProfiles.begin(),
591 selectedProfiles.end(),
592 profile) != selectedProfiles.end())
593 {
594 found = true;
595 break;
596 }
597 }
598 if (found)
599 {
600 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800601 }
602 else
603 {
James Feist3987c8b2019-05-13 10:43:17 -0700604 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800605 }
606 }
James Feist3987c8b2019-05-13 10:43:17 -0700607 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800608 {
James Feist3987c8b2019-05-13 10:43:17 -0700609 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800610 }
James Feist3987c8b2019-05-13 10:43:17 -0700611 else
James Feistf0096a02019-02-21 11:25:22 -0800612 {
James Feist3987c8b2019-05-13 10:43:17 -0700613 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800614 }
James Feist7136a5a2018-07-19 09:52:05 -0700615 }
616 }
James Feist8c3c51e2018-08-08 16:31:43 -0700617
618 // on dbus having an index field is a bit strange, so randomly
619 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800620 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700621 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700622 {
623 auto findZone =
624 configuration.second.find(pidZoneConfigurationInterface);
625 if (findZone != configuration.second.end())
626 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700627 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800628
James Feist1f802f52019-02-08 13:51:43 -0800629 const std::string& name = std::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800630 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700631
Patrick Venturec54fbd82018-10-30 19:40:05 -0700632 auto& details = zoneDetailsConfig[index];
James Feist3484bed2019-02-25 13:28:18 -0800633 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
634 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800635 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800636 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700637 }
638 auto findBase = configuration.second.find(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700639 // loop through all the PID configurations and fill out a sensor config
James Feist22c257a2018-08-31 14:07:12 -0700640 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700641 {
James Feist8c3c51e2018-08-08 16:31:43 -0700642
James Feist22c257a2018-08-31 14:07:12 -0700643 const auto& base =
644 configuration.second.at(pidConfigurationInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700645 const std::string pidName = std::get<std::string>(base.at("Name"));
646 const std::string pidClass =
647 std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700648 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800649 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700650 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700651 {
James Feistffd418b2018-11-15 14:46:36 -0800652 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800653 conf::PIDConf& conf = zoneConfig[index];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700654 std::vector<std::string> inputSensorNames(
655 std::get<std::vector<std::string>>(base.at("Inputs")));
656 std::vector<std::string> outputSensorNames;
James Feist50fdfe32018-09-24 15:51:09 -0700657
Jason Lingf3b04fd2020-07-24 09:33:04 -0700658 // assumption: all fan pids must have at least one output
659 if (pidClass == "fan")
James Feist50fdfe32018-09-24 15:51:09 -0700660 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700661 outputSensorNames = std::get<std::vector<std::string>>(
662 getPIDAttribute(base, "Outputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700663 }
James Feist1738e2a2019-02-04 15:57:03 -0800664
Jason Lingf3b04fd2020-07-24 09:33:04 -0700665 std::vector<SensorInterfaceType> inputSensorInterfaces;
666 std::vector<SensorInterfaceType> outputSensorInterfaces;
667 /* populate an interface list for different sensor direction
668 * types (input,output)
669 */
670 /* take the Inputs from the configuration and generate
671 * a list of dbus descriptors (path, interface).
672 * Mapping can be many-to-one since an element of Inputs can be
673 * a regex
674 */
675 for (const std::string& sensorName : inputSensorNames)
James Feist50fdfe32018-09-24 15:51:09 -0700676 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700677 findSensors(sensors, sensorNameToDbusName(sensorName),
678 inputSensorInterfaces);
679 }
680 for (const std::string& sensorName : outputSensorNames)
681 {
682 findSensors(sensors, sensorNameToDbusName(sensorName),
683 outputSensorInterfaces);
James Feist1738e2a2019-02-04 15:57:03 -0800684 }
James Feist50fdfe32018-09-24 15:51:09 -0700685
Jason Lingf3b04fd2020-07-24 09:33:04 -0700686 inputSensorNames.clear();
687 for (const SensorInterfaceType& inputSensorInterface :
688 inputSensorInterfaces)
James Feist1738e2a2019-02-04 15:57:03 -0800689 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700690 const std::string& dbusInterface =
691 inputSensorInterface.second;
692 const std::string& inputSensorPath =
693 inputSensorInterface.first;
694 std::string inputSensorName =
695 getSensorNameFromPath(inputSensorPath);
696 auto& config = sensorConfig[inputSensorName];
697 inputSensorNames.push_back(inputSensorName);
698 config.type = pidClass;
699 config.readPath = inputSensorInterface.first;
700 // todo: maybe un-hardcode this if we run into slower
701 // timeouts with sensors
702 if (config.type == "temp")
James Feist50fdfe32018-09-24 15:51:09 -0700703 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700704 config.timeout = 0;
705 config.ignoreDbusMinMax = true;
James Feist50fdfe32018-09-24 15:51:09 -0700706 }
Jason Lingf3b04fd2020-07-24 09:33:04 -0700707 if (dbusInterface != sensorInterface)
James Feist50fdfe32018-09-24 15:51:09 -0700708 {
Jason Lingf3b04fd2020-07-24 09:33:04 -0700709 /* all expected inputs in the configuration are expected
710 * to be sensor interfaces
711 */
712 throw std::runtime_error(
713 "sensor at dbus path [" + inputSensorPath +
714 "] has an interface [" + dbusInterface +
715 "] that does not match the expected interface of " +
716 sensorInterface);
James Feist50fdfe32018-09-24 15:51:09 -0700717 }
718 }
James Feist1738e2a2019-02-04 15:57:03 -0800719
Jason Lingf3b04fd2020-07-24 09:33:04 -0700720 /* fan pids need to pair up tach sensors with their pwm
721 * counterparts
722 */
723 if (pidClass == "fan")
724 {
725 /* If a PID is a fan there should be either
726 * (1) one output(pwm) per input(tach)
727 * OR
728 * (2) one putput(pwm) for all inputs(tach)
729 * everything else indicates a bad configuration.
730 */
731 bool singlePwm = false;
732 if (outputSensorInterfaces.size() == 1)
733 {
734 /* one pwm, set write paths for all fan sensors to it */
735 singlePwm = true;
736 }
737 else if (inputSensorInterfaces.size() ==
738 outputSensorInterfaces.size())
739 {
740 /* one to one mapping, each fan sensor gets its own pwm
741 * control */
742 singlePwm = false;
743 }
744 else
745 {
746 throw std::runtime_error(
747 "fan PID has invalid number of Outputs");
748 }
749 std::string fanSensorName;
750 std::string pwmPath;
751 std::string pwmInterface;
752 if (singlePwm)
753 {
754 /* if just a single output(pwm) is provided then use
755 * that pwm control path for all the fan sensor write
756 * path configs
757 */
758 pwmPath = outputSensorInterfaces.at(0).first;
759 pwmInterface = outputSensorInterfaces.at(0).second;
760 }
761 for (uint32_t idx = 0; idx < inputSensorInterfaces.size();
762 idx++)
763 {
764 if (!singlePwm)
765 {
766 pwmPath = outputSensorInterfaces.at(idx).first;
767 pwmInterface =
768 outputSensorInterfaces.at(idx).second;
769 }
Patrick Venture0911bfe2020-08-10 12:51:40 -0700770 if (defaultPwmInterface != pwmInterface)
Jason Lingf3b04fd2020-07-24 09:33:04 -0700771 {
772 throw std::runtime_error(
773 "fan pwm control at dbus path [" + pwmPath +
774 "] has an interface [" + pwmInterface +
775 "] that does not match the expected interface "
776 "of " +
Patrick Venture0911bfe2020-08-10 12:51:40 -0700777 defaultPwmInterface);
Jason Lingf3b04fd2020-07-24 09:33:04 -0700778 }
779 const std::string& fanPath =
780 inputSensorInterfaces.at(idx).first;
781 fanSensorName = getSensorNameFromPath(fanPath);
782 auto& fanConfig = sensorConfig[fanSensorName];
783 fanConfig.writePath = pwmPath;
784 // todo: un-hardcode this if there are fans with
785 // different ranges
786 fanConfig.max = 255;
787 fanConfig.min = 0;
788 }
789 }
James Feist11d243d2019-06-24 16:18:40 -0700790 // if the sensors aren't available in the current state, don't
791 // add them to the configuration.
Jason Lingf3b04fd2020-07-24 09:33:04 -0700792 if (inputSensorNames.empty())
James Feist11d243d2019-06-24 16:18:40 -0700793 {
794 continue;
795 }
796
James Feist5ec20272019-07-10 11:59:57 -0700797 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700798
James Feist5ec20272019-07-10 11:59:57 -0700799 // SetPointOffset is a threshold value to pull from the sensor
800 // to apply an offset. For upper thresholds this means the
801 // setpoint is usually negative.
802 auto findSetpointOffset = base.find("SetPointOffset");
803 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700804 {
James Feist5ec20272019-07-10 11:59:57 -0700805 offsetType =
806 std::get<std::string>(findSetpointOffset->second);
807 if (std::find(thresholds::types.begin(),
808 thresholds::types.end(),
809 offsetType) == thresholds::types.end())
810 {
811 throw std::runtime_error("Unsupported type: " +
812 offsetType);
813 }
814 }
815
816 if (offsetType.empty())
817 {
818 struct conf::ControllerInfo& info =
819 conf[std::get<std::string>(base.at("Name"))];
Jason Lingf3b04fd2020-07-24 09:33:04 -0700820 info.inputs = std::move(inputSensorNames);
James Feist5ec20272019-07-10 11:59:57 -0700821 populatePidInfo(bus, base, info, nullptr);
James Feist22c257a2018-08-31 14:07:12 -0700822 }
823 else
824 {
James Feist5ec20272019-07-10 11:59:57 -0700825 // we have to split up the inputs, as in practice t-control
826 // values will differ, making setpoints differ
Jason Lingf3b04fd2020-07-24 09:33:04 -0700827 for (const std::string& input : inputSensorNames)
James Feist5ec20272019-07-10 11:59:57 -0700828 {
829 struct conf::ControllerInfo& info = conf[input];
830 info.inputs.emplace_back(input);
831 populatePidInfo(bus, base, info, &offsetType);
832 }
James Feist22c257a2018-08-31 14:07:12 -0700833 }
James Feist22c257a2018-08-31 14:07:12 -0700834 }
835 }
836 auto findStepwise =
837 configuration.second.find(stepwiseConfigurationInterface);
838 if (findStepwise != configuration.second.end())
839 {
840 const auto& base = findStepwise->second;
841 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800842 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700843 for (const std::string& zone : zones)
844 {
James Feistffd418b2018-11-15 14:46:36 -0800845 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800846 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700847
848 std::vector<std::string> inputs;
849 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800850 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700851
James Feist1738e2a2019-02-04 15:57:03 -0800852 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700853 for (const std::string& sensorName : sensorNames)
854 {
James Feist1738e2a2019-02-04 15:57:03 -0800855 std::vector<std::pair<std::string, std::string>>
856 sensorPathIfacePairs;
Jason Lingf3b04fd2020-07-24 09:33:04 -0700857 if (!findSensors(sensors, sensorNameToDbusName(sensorName),
858 sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700859 {
James Feist50fdfe32018-09-24 15:51:09 -0700860 break;
861 }
862
James Feist1738e2a2019-02-04 15:57:03 -0800863 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
864 {
865 size_t idx =
866 sensorPathIfacePair.first.find_last_of("/") + 1;
867 std::string shortName =
868 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700869
James Feist1738e2a2019-02-04 15:57:03 -0800870 inputs.push_back(shortName);
871 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800872 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800873 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800874 config.ignoreDbusMinMax = true;
James Feist1738e2a2019-02-04 15:57:03 -0800875 // todo: maybe un-hardcode this if we run into slower
876 // timeouts with sensors
877
James Feist2642cb52019-02-25 13:00:16 -0800878 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800879 sensorFound = true;
880 }
James Feist50fdfe32018-09-24 15:51:09 -0700881 }
882 if (!sensorFound)
883 {
884 continue;
885 }
James Feistf81f2882019-02-26 11:26:36 -0800886 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800887 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700888 info.inputs = std::move(inputs);
889
James Feist22c257a2018-08-31 14:07:12 -0700890 info.type = "stepwise";
891 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700892 info.stepwiseInfo.positiveHysteresis = 0.0;
893 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800894 std::string subtype = std::get<std::string>(base.at("Class"));
895
896 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700897 auto findPosHyst = base.find("PositiveHysteresis");
898 auto findNegHyst = base.find("NegativeHysteresis");
899 if (findPosHyst != base.end())
900 {
James Feist1f802f52019-02-08 13:51:43 -0800901 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800902 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700903 }
904 if (findNegHyst != base.end())
905 {
James Feist5782ab82019-04-02 08:38:48 -0700906 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800907 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700908 }
James Feist22c257a2018-08-31 14:07:12 -0700909 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800910 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700911 if (readings.size() > ec::maxStepwisePoints)
912 {
913 throw std::invalid_argument("Too many stepwise points.");
914 }
915 if (readings.empty())
916 {
917 throw std::invalid_argument(
918 "Must have one stepwise point.");
919 }
920 std::copy(readings.begin(), readings.end(),
921 info.stepwiseInfo.reading);
922 if (readings.size() < ec::maxStepwisePoints)
923 {
924 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800925 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700926 }
927 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800928 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700929 if (readings.size() != outputs.size())
930 {
931 throw std::invalid_argument(
932 "Outputs size must match readings");
933 }
934 std::copy(outputs.begin(), outputs.end(),
935 info.stepwiseInfo.output);
936 if (outputs.size() < ec::maxStepwisePoints)
937 {
938 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800939 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700940 }
James Feist7136a5a2018-07-19 09:52:05 -0700941 }
942 }
943 }
James Feistf0096a02019-02-21 11:25:22 -0800944 if constexpr (DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700945 {
946 debugPrint();
947 }
James Feistc959c422018-11-01 12:33:40 -0700948 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700949 {
James Feist1fe08952019-05-07 09:17:16 -0700950 std::cerr
951 << "No fan zones, application pausing until new configuration\n";
952 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700953 }
James Feist1fe08952019-05-07 09:17:16 -0700954 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700955}
Patrick Venturea0764872020-08-08 07:48:43 -0700956
James Feist7136a5a2018-07-19 09:52:05 -0700957} // namespace dbus_configuration
Patrick Venturea0764872020-08-08 07:48:43 -0700958} // namespace pid_control