blob: e1ca91c9cd89b6b9749193ba8932d6eec64a3954 [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*/
16
Patrick Venture07716592018-10-14 11:46:40 -070017#include "conf.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070018#include "util.hpp"
Patrick Venture07716592018-10-14 11:46:40 -070019
James Feist1fe08952019-05-07 09:17:16 -070020#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070021#include <sdbusplus/bus.hpp>
22#include <sdbusplus/bus/match.hpp>
23#include <sdbusplus/exception.hpp>
24
25#include <algorithm>
James Feist64f072a2018-08-10 16:39:24 -070026#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070027#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070028#include <iostream>
James Feist1fe08952019-05-07 09:17:16 -070029#include <list>
James Feist1738e2a2019-02-04 15:57:03 -080030#include <regex>
James Feist7136a5a2018-07-19 09:52:05 -070031#include <set>
32#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080033#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070034
35static constexpr bool DEBUG = false; // enable to print found configuration
36
James Feistf81f2882019-02-26 11:26:36 -080037extern std::map<std::string, struct conf::SensorConfig> sensorConfig;
38extern std::map<int64_t, conf::PIDConf> zoneConfig;
39extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig;
James Feist7136a5a2018-07-19 09:52:05 -070040
Patrick Venturee2ec0f62018-09-04 12:30:27 -070041constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070042 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070043constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070044 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070045constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070046 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070047constexpr const char* stepwiseConfigurationInterface =
48 "xyz.openbmc_project.Configuration.Stepwise";
James Feistf0096a02019-02-21 11:25:22 -080049constexpr const char* thermalControlIface =
50 "xyz.openbmc_project.Control.ThermalMode";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070051constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
52constexpr const char* pwmInterface = "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{
70
James Feist5ec20272019-07-10 11:59:57 -070071using DbusVariantType =
72 std::variant<uint64_t, int64_t, double, std::string,
73 std::vector<std::string>, std::vector<double>>;
74
James Feist1738e2a2019-02-04 15:57:03 -080075bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
76 const std::string& search,
77 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070078{
James Feist1738e2a2019-02-04 15:57:03 -080079 std::smatch match;
Jae Hyun Yoo1a704dc2020-06-04 15:00:10 -070080 std::regex reg(search + '$');
James Feist1738e2a2019-02-04 15:57:03 -080081 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070082 {
James Feist1738e2a2019-02-04 15:57:03 -080083 if (std::regex_search(sensor.first, match, reg))
84 {
85 matches.push_back(sensor);
86 }
James Feist7136a5a2018-07-19 09:52:05 -070087 }
James Feist1738e2a2019-02-04 15:57:03 -080088 return matches.size() > 0;
James Feist7136a5a2018-07-19 09:52:05 -070089}
90
91// this function prints the configuration into a form similar to the cpp
92// generated code to help in verification, should be turned off during normal
93// use
94void debugPrint(void)
95{
96 // print sensor config
97 std::cout << "sensor config:\n";
98 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070099 for (const auto& pair : sensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700100 {
101
102 std::cout << "\t{" << pair.first << ",\n\t\t{";
103 std::cout << pair.second.type << ", ";
Patrick Venture69c51062019-02-11 09:46:03 -0800104 std::cout << pair.second.readPath << ", ";
105 std::cout << pair.second.writePath << ", ";
James Feist7136a5a2018-07-19 09:52:05 -0700106 std::cout << pair.second.min << ", ";
107 std::cout << pair.second.max << ", ";
108 std::cout << pair.second.timeout << "},\n\t},\n";
109 }
110 std::cout << "}\n\n";
111 std::cout << "ZoneDetailsConfig\n";
112 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700113 for (const auto& zone : zoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700114 {
115 std::cout << "\t{" << zone.first << ",\n";
James Feist3484bed2019-02-25 13:28:18 -0800116 std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800117 std::cout << zone.second.failsafePercent << "}\n\t},\n";
James Feist7136a5a2018-07-19 09:52:05 -0700118 }
119 std::cout << "}\n\n";
120 std::cout << "ZoneConfig\n";
121 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700122 for (const auto& zone : zoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700123 {
124 std::cout << "\t{" << zone.first << "\n";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700125 for (const auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700126 {
127 std::cout << "\t\t{" << pidconf.first << ",\n";
128 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
129 std::cout << "\t\t\t{";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700130 for (const auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700131 {
132 std::cout << "\n\t\t\t" << input << ",\n";
133 }
134 std::cout << "\t\t\t}\n";
135 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700136 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
Patrick Venture7442c372019-02-11 10:21:05 -0800137 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
138 << ",\n";
139 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
140 << ",\n";
141 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
142 << ",\n";
143 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
144 << ",\n";
145 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
146 << "," << pidconf.second.pidInfo.integralLimit.max
147 << "},\n";
148 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
149 << pidconf.second.pidInfo.outLim.max << "},\n";
150 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
151 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700152 std::cout << "\t\t\t}\n\t\t}\n";
153 }
154 std::cout << "\t},\n";
155 }
156 std::cout << "}\n\n";
157}
158
James Feistffd418b2018-11-15 14:46:36 -0800159size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
160{
161 auto it = std::find(zones.begin(), zones.end(), name);
162 if (it == zones.end())
163 {
164 zones.emplace_back(name);
165 it = zones.end() - 1;
166 }
167
168 return it - zones.begin();
169}
170
James Feistf0096a02019-02-21 11:25:22 -0800171std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus)
172{
173 std::vector<std::string> ret;
174 auto mapper =
175 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
176 "/xyz/openbmc_project/object_mapper",
177 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
178 mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface});
179 std::unordered_map<
180 std::string, std::unordered_map<std::string, std::vector<std::string>>>
181 respData;
182
183 try
184 {
185 auto resp = bus.call(mapper);
186 resp.read(respData);
187 }
188 catch (sdbusplus::exception_t&)
189 {
190 // can't do anything without mapper call data
191 throw std::runtime_error("ObjectMapper Call Failure");
192 }
193 if (respData.empty())
194 {
195 // if the user has profiles but doesn't expose the interface to select
196 // one, just go ahead without using profiles
197 return ret;
198 }
199
200 // assumption is that we should only have a small handful of selected
201 // profiles at a time (probably only 1), so calling each individually should
202 // not incur a large cost
203 for (const auto& objectPair : respData)
204 {
205 const std::string& path = objectPair.first;
206 for (const auto& ownerPair : objectPair.second)
207 {
208 const std::string& busName = ownerPair.first;
209 auto getProfile =
210 bus.new_method_call(busName.c_str(), path.c_str(),
211 "org.freedesktop.DBus.Properties", "Get");
212 getProfile.append(thermalControlIface, "Current");
213 std::variant<std::string> variantResp;
214 try
215 {
216 auto resp = bus.call(getProfile);
217 resp.read(variantResp);
218 }
219 catch (sdbusplus::exception_t&)
220 {
221 throw std::runtime_error("Failure getting profile");
222 }
223 std::string mode = std::get<std::string>(variantResp);
224 ret.emplace_back(std::move(mode));
225 }
226 }
227 if constexpr (DEBUG)
228 {
229 std::cout << "Profiles selected: ";
230 for (const auto& profile : ret)
231 {
232 std::cout << profile << " ";
233 }
234 std::cout << "\n";
235 }
236 return ret;
237}
238
James Feist991ebd82020-07-21 11:14:52 -0700239int eventHandler(sd_bus_message* m, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700240{
James Feist1fe08952019-05-07 09:17:16 -0700241
James Feist991ebd82020-07-21 11:14:52 -0700242 if (context == nullptr || m == nullptr)
James Feist1fe08952019-05-07 09:17:16 -0700243 {
244 throw std::runtime_error("Invalid match");
245 }
James Feist991ebd82020-07-21 11:14:52 -0700246
247 // we skip associations because the mapper populates these, not the sensors
248 const std::array<const char*, 1> skipList = {
249 "xyz.openbmc_project.Association"};
250
251 sdbusplus::message::message message(m);
252 if (std::string(message.get_member()) == "InterfacesAdded")
253 {
254 sdbusplus::message::object_path path;
255 std::unordered_map<
256 std::string,
257 std::unordered_map<std::string, std::variant<Associations, bool>>>
258 data;
259
260 message.read(path, data);
261
262 for (const char* skip : skipList)
263 {
264 auto find = data.find(skip);
265 if (find != data.end())
266 {
267 data.erase(find);
268 if (data.empty())
269 {
270 return 1;
271 }
272 }
273 }
274 }
275
James Feist1fe08952019-05-07 09:17:16 -0700276 boost::asio::steady_timer* timer =
277 static_cast<boost::asio::steady_timer*>(context);
278
279 // do a brief sleep as we tend to get a bunch of these events at
280 // once
281 timer->expires_after(std::chrono::seconds(2));
282 timer->async_wait([](const boost::system::error_code ec) {
283 if (ec == boost::asio::error::operation_aborted)
284 {
285 /* another timer started*/
286 return;
287 }
288
289 std::cout << "New configuration detected, reloading\n.";
Yong Li298a95c2020-04-07 15:11:02 +0800290 tryRestartControlLoops();
James Feist1fe08952019-05-07 09:17:16 -0700291 });
292
293 return 1;
294}
295
296void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
297{
298 // this is a list because the matches can't be moved
299 static std::list<sdbusplus::bus::match::match> matches;
300
James Feist3987c8b2019-05-13 10:43:17 -0700301 const std::array<std::string, 4> interfaces = {
302 thermalControlIface, pidConfigurationInterface,
303 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700304
305 // this list only needs to be created once
306 if (!matches.empty())
307 {
308 return;
309 }
310
311 // we restart when the configuration changes or there are new sensors
312 for (const auto& interface : interfaces)
313 {
314 matches.emplace_back(
315 bus,
316 "type='signal',member='PropertiesChanged',arg0namespace='" +
317 interface + "'",
318 eventHandler, &timer);
319 }
320 matches.emplace_back(
321 bus,
322 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
323 "sensors/'",
324 eventHandler, &timer);
325}
326
Jason Ling6fc301f2020-07-23 12:39:57 -0700327/**
328 * retrieve an attribute from the pid configuration map
329 * @param[in] base - the PID configuration map, keys are the attributes and
330 * value is the variant associated with that attribute.
331 * @param attributeName - the name of the attribute
332 * @return a variant holding the value associated with a key
333 * @throw runtime_error : attributeName is not in base
334 */
335inline DbusVariantType getPIDAttribute(
336 const std::unordered_map<std::string, DbusVariantType>& base,
337 const std::string& attributeName)
338{
339 auto search = base.find(attributeName);
340 if (search == base.end())
341 {
342 throw std::runtime_error("missing attribute " + attributeName);
343 }
344 return search->second;
345}
346
James Feist5ec20272019-07-10 11:59:57 -0700347void populatePidInfo(
348 sdbusplus::bus::bus& bus,
349 const std::unordered_map<std::string, DbusVariantType>& base,
350 struct conf::ControllerInfo& info, const std::string* thresholdProperty)
351{
Jason Ling6fc301f2020-07-23 12:39:57 -0700352 info.type = std::get<std::string>(getPIDAttribute(base, "Class"));
James Feist5ec20272019-07-10 11:59:57 -0700353 if (info.type == "fan")
354 {
355 info.setpoint = 0;
356 }
357 else
358 {
Jason Ling6fc301f2020-07-23 12:39:57 -0700359 info.setpoint = std::visit(VariantToDoubleVisitor(),
360 getPIDAttribute(base, "SetPoint"));
James Feist5ec20272019-07-10 11:59:57 -0700361 }
362
363 if (thresholdProperty != nullptr)
364 {
365 std::string interface;
366 if (*thresholdProperty == "WarningHigh" ||
367 *thresholdProperty == "WarningLow")
368 {
369 interface = thresholds::warningInterface;
370 }
371 else
372 {
373 interface = thresholds::criticalInterface;
374 }
375 const std::string& path = sensorConfig[info.inputs.front()].readPath;
376
377 DbusHelper helper;
378 std::string service = helper.getService(bus, interface, path);
379 double reading = 0;
380 try
381 {
382 helper.getProperty(bus, service, path, interface,
383 *thresholdProperty, reading);
384 }
385 catch (const sdbusplus::exception::SdBusError& ex)
386 {
387 // unsupported threshold, leaving reading at 0
388 }
389
390 info.setpoint += reading;
391 }
392
393 info.pidInfo.ts = 1.0; // currently unused
Jason Ling6fc301f2020-07-23 12:39:57 -0700394 info.pidInfo.proportionalCoeff = std::visit(
395 VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient"));
396 info.pidInfo.integralCoeff = std::visit(
397 VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient"));
398 info.pidInfo.feedFwdOffset = std::visit(
399 VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient"));
400 info.pidInfo.feedFwdGain = std::visit(
401 VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient"));
402 info.pidInfo.integralLimit.max = std::visit(
403 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax"));
404 info.pidInfo.integralLimit.min = std::visit(
405 VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin"));
406 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
407 getPIDAttribute(base, "OutLimitMax"));
408 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
409 getPIDAttribute(base, "OutLimitMin"));
James Feist5ec20272019-07-10 11:59:57 -0700410 info.pidInfo.slewNeg =
Jason Ling6fc301f2020-07-23 12:39:57 -0700411 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg"));
James Feist5ec20272019-07-10 11:59:57 -0700412 info.pidInfo.slewPos =
Jason Ling6fc301f2020-07-23 12:39:57 -0700413 std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos"));
James Feist5ec20272019-07-10 11:59:57 -0700414 double negativeHysteresis = 0;
415 double positiveHysteresis = 0;
416
417 auto findNeg = base.find("NegativeHysteresis");
418 auto findPos = base.find("PositiveHysteresis");
419
420 if (findNeg != base.end())
421 {
422 negativeHysteresis =
423 std::visit(VariantToDoubleVisitor(), findNeg->second);
424 }
425
426 if (findPos != base.end())
427 {
428 positiveHysteresis =
429 std::visit(VariantToDoubleVisitor(), findPos->second);
430 }
James Feist5ec20272019-07-10 11:59:57 -0700431 info.pidInfo.negativeHysteresis = negativeHysteresis;
432 info.pidInfo.positiveHysteresis = positiveHysteresis;
433}
434
James Feist1fe08952019-05-07 09:17:16 -0700435bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
436{
437
438 sensorConfig.clear();
439 zoneConfig.clear();
440 zoneDetailsConfig.clear();
441
442 createMatches(bus, timer);
443
James Feist22c257a2018-08-31 14:07:12 -0700444 using DbusVariantType =
James Feist1f802f52019-02-08 13:51:43 -0800445 std::variant<uint64_t, int64_t, double, std::string,
446 std::vector<std::string>, std::vector<double>>;
James Feist22c257a2018-08-31 14:07:12 -0700447
James Feist7136a5a2018-07-19 09:52:05 -0700448 using ManagedObjectType = std::unordered_map<
449 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700450 std::unordered_map<std::string,
451 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700452
James Feist7136a5a2018-07-19 09:52:05 -0700453 auto mapper =
454 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
455 "/xyz/openbmc_project/object_mapper",
456 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700457 mapper.append("/", 0,
James Feist3987c8b2019-05-13 10:43:17 -0700458 std::array<const char*, 6>{objectManagerInterface,
459 pidConfigurationInterface,
460 pidZoneConfigurationInterface,
461 stepwiseConfigurationInterface,
462 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700463 std::unordered_map<
464 std::string, std::unordered_map<std::string, std::vector<std::string>>>
465 respData;
James Feist22c257a2018-08-31 14:07:12 -0700466 try
467 {
468 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700469 resp.read(respData);
470 }
471 catch (sdbusplus::exception_t&)
472 {
473 // can't do anything without mapper call data
474 throw std::runtime_error("ObjectMapper Call Failure");
475 }
James Feist7136a5a2018-07-19 09:52:05 -0700476
James Feist7136a5a2018-07-19 09:52:05 -0700477 if (respData.empty())
478 {
James Feist22c257a2018-08-31 14:07:12 -0700479 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700480 throw std::runtime_error("No configuration data available from Mapper");
481 }
482 // create a map of pair of <has pid configuration, ObjectManager path>
483 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
484 // and a map of <path, interface> for sensors
485 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700486 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700487 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700488 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700489 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700490 auto& owner = owners[ownerPair.first];
491 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700492 {
493
494 if (interface == objectManagerInterface)
495 {
496 owner.second = objectPair.first;
497 }
498 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700499 interface == pidZoneConfigurationInterface ||
500 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700501 {
502 owner.first = true;
503 }
504 if (interface == sensorInterface || interface == pwmInterface)
505 {
506 // we're not interested in pwm sensors, just pwm control
507 if (interface == sensorInterface &&
508 objectPair.first.find("pwm") != std::string::npos)
509 {
510 continue;
511 }
512 sensors[objectPair.first] = interface;
513 }
514 }
515 }
516 }
517 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700518 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700519 {
520 // skip if no pid configuration (means probably a sensor)
521 if (!owner.second.first)
522 {
523 continue;
524 }
525 auto endpoint = bus.new_method_call(
526 owner.first.c_str(), owner.second.second.c_str(),
527 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700528 ManagedObjectType configuration;
529 try
James Feist7136a5a2018-07-19 09:52:05 -0700530 {
James Feist22c257a2018-08-31 14:07:12 -0700531 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700532 responce.read(configuration);
533 }
534 catch (sdbusplus::exception_t&)
535 {
536 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700537 throw std::runtime_error("Error getting managed objects from " +
538 owner.first);
539 }
James Feist22c257a2018-08-31 14:07:12 -0700540
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700541 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700542 {
543 if (pathPair.second.find(pidConfigurationInterface) !=
544 pathPair.second.end() ||
545 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700546 pathPair.second.end() ||
547 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700548 pathPair.second.end())
549 {
550 configurations.emplace(pathPair);
551 }
James Feistf0096a02019-02-21 11:25:22 -0800552 }
553 }
554
555 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700556 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
557 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800558 {
James Feist3987c8b2019-05-13 10:43:17 -0700559 for (auto pathIt = configurations.begin();
560 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800561 {
James Feist3987c8b2019-05-13 10:43:17 -0700562 for (auto confIt = pathIt->second.begin();
563 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800564 {
James Feist3987c8b2019-05-13 10:43:17 -0700565 auto profilesFind = confIt->second.find("Profiles");
566 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800567 {
James Feist3987c8b2019-05-13 10:43:17 -0700568 confIt++;
569 continue; // if no profiles selected, apply always
570 }
571 auto profiles =
572 std::get<std::vector<std::string>>(profilesFind->second);
573 if (profiles.empty())
574 {
575 confIt++;
576 continue;
577 }
578
579 bool found = false;
580 for (const std::string& profile : profiles)
581 {
582 if (std::find(selectedProfiles.begin(),
583 selectedProfiles.end(),
584 profile) != selectedProfiles.end())
585 {
586 found = true;
587 break;
588 }
589 }
590 if (found)
591 {
592 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800593 }
594 else
595 {
James Feist3987c8b2019-05-13 10:43:17 -0700596 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800597 }
598 }
James Feist3987c8b2019-05-13 10:43:17 -0700599 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800600 {
James Feist3987c8b2019-05-13 10:43:17 -0700601 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800602 }
James Feist3987c8b2019-05-13 10:43:17 -0700603 else
James Feistf0096a02019-02-21 11:25:22 -0800604 {
James Feist3987c8b2019-05-13 10:43:17 -0700605 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800606 }
James Feist7136a5a2018-07-19 09:52:05 -0700607 }
608 }
James Feist8c3c51e2018-08-08 16:31:43 -0700609
610 // on dbus having an index field is a bit strange, so randomly
611 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800612 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700613 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700614 {
615 auto findZone =
616 configuration.second.find(pidZoneConfigurationInterface);
617 if (findZone != configuration.second.end())
618 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700619 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800620
James Feist1f802f52019-02-08 13:51:43 -0800621 const std::string& name = std::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800622 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700623
Patrick Venturec54fbd82018-10-30 19:40:05 -0700624 auto& details = zoneDetailsConfig[index];
James Feist3484bed2019-02-25 13:28:18 -0800625 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
626 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800627 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800628 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700629 }
630 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700631 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700632 {
James Feist8c3c51e2018-08-08 16:31:43 -0700633
James Feist22c257a2018-08-31 14:07:12 -0700634 const auto& base =
635 configuration.second.at(pidConfigurationInterface);
636 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800637 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700638 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700639 {
James Feistffd418b2018-11-15 14:46:36 -0800640 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800641 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700642
643 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800644 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700645 auto findOutputs =
646 base.find("Outputs"); // currently only fans have outputs
647 if (findOutputs != base.end())
648 {
649 std::vector<std::string> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800650 std::get<std::vector<std::string>>(findOutputs->second);
James Feist50fdfe32018-09-24 15:51:09 -0700651 sensorNames.insert(sensorNames.end(), outputs.begin(),
652 outputs.end());
653 }
James Feist1738e2a2019-02-04 15:57:03 -0800654
James Feist50fdfe32018-09-24 15:51:09 -0700655 std::vector<std::string> inputs;
James Feist1738e2a2019-02-04 15:57:03 -0800656 std::vector<std::pair<std::string, std::string>>
657 sensorInterfaces;
James Feist50fdfe32018-09-24 15:51:09 -0700658 for (const std::string& sensorName : sensorNames)
659 {
660 std::string name = sensorName;
661 // replace spaces with underscores to be legal on dbus
662 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800663 findSensors(sensors, name, sensorInterfaces);
664 }
James Feist50fdfe32018-09-24 15:51:09 -0700665
James Feist1738e2a2019-02-04 15:57:03 -0800666 for (const auto& sensorPathIfacePair : sensorInterfaces)
667 {
668
James Feist50fdfe32018-09-24 15:51:09 -0700669 if (sensorPathIfacePair.second == sensorInterface)
670 {
James Feist1738e2a2019-02-04 15:57:03 -0800671 size_t idx =
672 sensorPathIfacePair.first.find_last_of("/") + 1;
673 std::string shortName =
674 sensorPathIfacePair.first.substr(idx);
675
676 inputs.push_back(shortName);
677 auto& config = sensorConfig[shortName];
James Feist1f802f52019-02-08 13:51:43 -0800678 config.type = std::get<std::string>(base.at("Class"));
Patrick Venture69c51062019-02-11 09:46:03 -0800679 config.readPath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700680 // todo: maybe un-hardcode this if we run into slower
681 // timeouts with sensors
682 if (config.type == "temp")
683 {
James Feist2642cb52019-02-25 13:00:16 -0800684 config.timeout = 0;
James Feist3433cb62019-11-11 16:12:45 -0800685 config.ignoreDbusMinMax = true;
James Feist50fdfe32018-09-24 15:51:09 -0700686 }
687 }
688 else if (sensorPathIfacePair.second == pwmInterface)
689 {
690 // copy so we can modify it
691 for (std::string otherSensor : sensorNames)
692 {
James Feist1738e2a2019-02-04 15:57:03 -0800693 std::replace(otherSensor.begin(), otherSensor.end(),
694 ' ', '_');
695 if (sensorPathIfacePair.first.find(otherSensor) !=
696 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700697 {
698 continue;
699 }
James Feist1738e2a2019-02-04 15:57:03 -0800700
Patrick Venturec54fbd82018-10-30 19:40:05 -0700701 auto& config = sensorConfig[otherSensor];
Patrick Venture69c51062019-02-11 09:46:03 -0800702 config.writePath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700703 // todo: un-hardcode this if there are fans with
704 // different ranges
705 config.max = 255;
706 config.min = 0;
707 }
708 }
709 }
James Feist1738e2a2019-02-04 15:57:03 -0800710
James Feist11d243d2019-06-24 16:18:40 -0700711 // if the sensors aren't available in the current state, don't
712 // add them to the configuration.
713 if (inputs.empty())
714 {
715 continue;
716 }
717
James Feist5ec20272019-07-10 11:59:57 -0700718 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700719
James Feist5ec20272019-07-10 11:59:57 -0700720 // SetPointOffset is a threshold value to pull from the sensor
721 // to apply an offset. For upper thresholds this means the
722 // setpoint is usually negative.
723 auto findSetpointOffset = base.find("SetPointOffset");
724 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700725 {
James Feist5ec20272019-07-10 11:59:57 -0700726 offsetType =
727 std::get<std::string>(findSetpointOffset->second);
728 if (std::find(thresholds::types.begin(),
729 thresholds::types.end(),
730 offsetType) == thresholds::types.end())
731 {
732 throw std::runtime_error("Unsupported type: " +
733 offsetType);
734 }
735 }
736
737 if (offsetType.empty())
738 {
739 struct conf::ControllerInfo& info =
740 conf[std::get<std::string>(base.at("Name"))];
741 info.inputs = std::move(inputs);
742 populatePidInfo(bus, base, info, nullptr);
James Feist22c257a2018-08-31 14:07:12 -0700743 }
744 else
745 {
James Feist5ec20272019-07-10 11:59:57 -0700746 // we have to split up the inputs, as in practice t-control
747 // values will differ, making setpoints differ
748 for (const std::string& input : inputs)
749 {
750 struct conf::ControllerInfo& info = conf[input];
751 info.inputs.emplace_back(input);
752 populatePidInfo(bus, base, info, &offsetType);
753 }
James Feist22c257a2018-08-31 14:07:12 -0700754 }
James Feist22c257a2018-08-31 14:07:12 -0700755 }
756 }
757 auto findStepwise =
758 configuration.second.find(stepwiseConfigurationInterface);
759 if (findStepwise != configuration.second.end())
760 {
761 const auto& base = findStepwise->second;
762 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800763 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700764 for (const std::string& zone : zones)
765 {
James Feistffd418b2018-11-15 14:46:36 -0800766 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800767 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700768
769 std::vector<std::string> inputs;
770 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800771 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700772
James Feist1738e2a2019-02-04 15:57:03 -0800773 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700774 for (const std::string& sensorName : sensorNames)
775 {
776 std::string name = sensorName;
777 // replace spaces with underscores to be legal on dbus
778 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800779 std::vector<std::pair<std::string, std::string>>
780 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700781
James Feist1738e2a2019-02-04 15:57:03 -0800782 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700783 {
James Feist50fdfe32018-09-24 15:51:09 -0700784 break;
785 }
786
James Feist1738e2a2019-02-04 15:57:03 -0800787 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
788 {
789 size_t idx =
790 sensorPathIfacePair.first.find_last_of("/") + 1;
791 std::string shortName =
792 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700793
James Feist1738e2a2019-02-04 15:57:03 -0800794 inputs.push_back(shortName);
795 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800796 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800797 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800798 config.ignoreDbusMinMax = true;
James Feist1738e2a2019-02-04 15:57:03 -0800799 // todo: maybe un-hardcode this if we run into slower
800 // timeouts with sensors
801
James Feist2642cb52019-02-25 13:00:16 -0800802 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800803 sensorFound = true;
804 }
James Feist50fdfe32018-09-24 15:51:09 -0700805 }
806 if (!sensorFound)
807 {
808 continue;
809 }
James Feistf81f2882019-02-26 11:26:36 -0800810 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800811 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700812 info.inputs = std::move(inputs);
813
James Feist22c257a2018-08-31 14:07:12 -0700814 info.type = "stepwise";
815 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700816 info.stepwiseInfo.positiveHysteresis = 0.0;
817 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800818 std::string subtype = std::get<std::string>(base.at("Class"));
819
820 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700821 auto findPosHyst = base.find("PositiveHysteresis");
822 auto findNegHyst = base.find("NegativeHysteresis");
823 if (findPosHyst != base.end())
824 {
James Feist1f802f52019-02-08 13:51:43 -0800825 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800826 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700827 }
828 if (findNegHyst != base.end())
829 {
James Feist5782ab82019-04-02 08:38:48 -0700830 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800831 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700832 }
James Feist22c257a2018-08-31 14:07:12 -0700833 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800834 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700835 if (readings.size() > ec::maxStepwisePoints)
836 {
837 throw std::invalid_argument("Too many stepwise points.");
838 }
839 if (readings.empty())
840 {
841 throw std::invalid_argument(
842 "Must have one stepwise point.");
843 }
844 std::copy(readings.begin(), readings.end(),
845 info.stepwiseInfo.reading);
846 if (readings.size() < ec::maxStepwisePoints)
847 {
848 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800849 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700850 }
851 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800852 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700853 if (readings.size() != outputs.size())
854 {
855 throw std::invalid_argument(
856 "Outputs size must match readings");
857 }
858 std::copy(outputs.begin(), outputs.end(),
859 info.stepwiseInfo.output);
860 if (outputs.size() < ec::maxStepwisePoints)
861 {
862 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800863 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700864 }
James Feist7136a5a2018-07-19 09:52:05 -0700865 }
866 }
867 }
James Feistf0096a02019-02-21 11:25:22 -0800868 if constexpr (DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700869 {
870 debugPrint();
871 }
James Feistc959c422018-11-01 12:33:40 -0700872 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700873 {
James Feist1fe08952019-05-07 09:17:16 -0700874 std::cerr
875 << "No fan zones, application pausing until new configuration\n";
876 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700877 }
James Feist1fe08952019-05-07 09:17:16 -0700878 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700879}
880} // namespace dbus_configuration