blob: 671a436fa16d513ff50786aab74b4086bb3f9a7f [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
Patrick Venture107a25d2018-10-13 14:08:09 -070020#include <algorithm>
James Feist1fe08952019-05-07 09:17:16 -070021#include <boost/asio/steady_timer.hpp>
James Feist64f072a2018-08-10 16:39:24 -070022#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070023#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070024#include <iostream>
James Feist1fe08952019-05-07 09:17:16 -070025#include <list>
James Feist1738e2a2019-02-04 15:57:03 -080026#include <regex>
James Feist7136a5a2018-07-19 09:52:05 -070027#include <sdbusplus/bus.hpp>
James Feist64f072a2018-08-10 16:39:24 -070028#include <sdbusplus/bus/match.hpp>
James Feist22c257a2018-08-31 14:07:12 -070029#include <sdbusplus/exception.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070030#include <set>
31#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080032#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070033
34static constexpr bool DEBUG = false; // enable to print found configuration
35
James Feistf81f2882019-02-26 11:26:36 -080036extern std::map<std::string, struct conf::SensorConfig> sensorConfig;
37extern std::map<int64_t, conf::PIDConf> zoneConfig;
38extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig;
James Feist7136a5a2018-07-19 09:52:05 -070039
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";
51constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070052
James Feist991ebd82020-07-21 11:14:52 -070053using Association = std::tuple<std::string, std::string, std::string>;
54using Associations = std::vector<Association>;
55
James Feist5ec20272019-07-10 11:59:57 -070056namespace thresholds
57{
58constexpr const char* warningInterface =
59 "xyz.openbmc_project.Sensor.Threshold.Warning";
60constexpr const char* criticalInterface =
61 "xyz.openbmc_project.Sensor.Threshold.Critical";
62const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh",
63 "WarningLow", "WarningHigh"};
64
65} // namespace thresholds
66
James Feist7136a5a2018-07-19 09:52:05 -070067namespace dbus_configuration
68{
69
James Feist5ec20272019-07-10 11:59:57 -070070using DbusVariantType =
71 std::variant<uint64_t, int64_t, double, std::string,
72 std::vector<std::string>, std::vector<double>>;
73
James Feist1738e2a2019-02-04 15:57:03 -080074bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
75 const std::string& search,
76 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070077{
James Feist1738e2a2019-02-04 15:57:03 -080078 std::smatch match;
Jae Hyun Yoo1a704dc2020-06-04 15:00:10 -070079 std::regex reg(search + '$');
James Feist1738e2a2019-02-04 15:57:03 -080080 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070081 {
James Feist1738e2a2019-02-04 15:57:03 -080082 if (std::regex_search(sensor.first, match, reg))
83 {
84 matches.push_back(sensor);
85 }
James Feist7136a5a2018-07-19 09:52:05 -070086 }
Patrick Venture107a25d2018-10-13 14:08:09 -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
James Feist5ec20272019-07-10 11:59:57 -0700327void populatePidInfo(
328 sdbusplus::bus::bus& bus,
329 const std::unordered_map<std::string, DbusVariantType>& base,
330 struct conf::ControllerInfo& info, const std::string* thresholdProperty)
331{
332
333 info.type = std::get<std::string>(base.at("Class"));
334
335 if (info.type == "fan")
336 {
337 info.setpoint = 0;
338 }
339 else
340 {
341 info.setpoint =
342 std::visit(VariantToDoubleVisitor(), base.at("SetPoint"));
343 }
344
345 if (thresholdProperty != nullptr)
346 {
347 std::string interface;
348 if (*thresholdProperty == "WarningHigh" ||
349 *thresholdProperty == "WarningLow")
350 {
351 interface = thresholds::warningInterface;
352 }
353 else
354 {
355 interface = thresholds::criticalInterface;
356 }
357 const std::string& path = sensorConfig[info.inputs.front()].readPath;
358
359 DbusHelper helper;
360 std::string service = helper.getService(bus, interface, path);
361 double reading = 0;
362 try
363 {
364 helper.getProperty(bus, service, path, interface,
365 *thresholdProperty, reading);
366 }
367 catch (const sdbusplus::exception::SdBusError& ex)
368 {
369 // unsupported threshold, leaving reading at 0
370 }
371
372 info.setpoint += reading;
373 }
374
375 info.pidInfo.ts = 1.0; // currently unused
376 info.pidInfo.proportionalCoeff =
377 std::visit(VariantToDoubleVisitor(), base.at("PCoefficient"));
378 info.pidInfo.integralCoeff =
379 std::visit(VariantToDoubleVisitor(), base.at("ICoefficient"));
380 info.pidInfo.feedFwdOffset =
381 std::visit(VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
382 info.pidInfo.feedFwdGain =
383 std::visit(VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
384 info.pidInfo.integralLimit.max =
385 std::visit(VariantToDoubleVisitor(), base.at("ILimitMax"));
386 info.pidInfo.integralLimit.min =
387 std::visit(VariantToDoubleVisitor(), base.at("ILimitMin"));
388 info.pidInfo.outLim.max =
389 std::visit(VariantToDoubleVisitor(), base.at("OutLimitMax"));
390 info.pidInfo.outLim.min =
391 std::visit(VariantToDoubleVisitor(), base.at("OutLimitMin"));
392 info.pidInfo.slewNeg =
393 std::visit(VariantToDoubleVisitor(), base.at("SlewNeg"));
394 info.pidInfo.slewPos =
395 std::visit(VariantToDoubleVisitor(), base.at("SlewPos"));
396 double negativeHysteresis = 0;
397 double positiveHysteresis = 0;
398
399 auto findNeg = base.find("NegativeHysteresis");
400 auto findPos = base.find("PositiveHysteresis");
401
402 if (findNeg != base.end())
403 {
404 negativeHysteresis =
405 std::visit(VariantToDoubleVisitor(), findNeg->second);
406 }
407
408 if (findPos != base.end())
409 {
410 positiveHysteresis =
411 std::visit(VariantToDoubleVisitor(), findPos->second);
412 }
413
414 info.pidInfo.negativeHysteresis = negativeHysteresis;
415 info.pidInfo.positiveHysteresis = positiveHysteresis;
416}
417
James Feist1fe08952019-05-07 09:17:16 -0700418bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
419{
420
421 sensorConfig.clear();
422 zoneConfig.clear();
423 zoneDetailsConfig.clear();
424
425 createMatches(bus, timer);
426
James Feist22c257a2018-08-31 14:07:12 -0700427 using DbusVariantType =
James Feist1f802f52019-02-08 13:51:43 -0800428 std::variant<uint64_t, int64_t, double, std::string,
429 std::vector<std::string>, std::vector<double>>;
James Feist22c257a2018-08-31 14:07:12 -0700430
James Feist7136a5a2018-07-19 09:52:05 -0700431 using ManagedObjectType = std::unordered_map<
432 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700433 std::unordered_map<std::string,
434 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700435
James Feist7136a5a2018-07-19 09:52:05 -0700436 auto mapper =
437 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
438 "/xyz/openbmc_project/object_mapper",
439 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700440 mapper.append("/", 0,
James Feist3987c8b2019-05-13 10:43:17 -0700441 std::array<const char*, 6>{objectManagerInterface,
442 pidConfigurationInterface,
443 pidZoneConfigurationInterface,
444 stepwiseConfigurationInterface,
445 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700446 std::unordered_map<
447 std::string, std::unordered_map<std::string, std::vector<std::string>>>
448 respData;
James Feist22c257a2018-08-31 14:07:12 -0700449 try
450 {
451 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700452 resp.read(respData);
453 }
454 catch (sdbusplus::exception_t&)
455 {
456 // can't do anything without mapper call data
457 throw std::runtime_error("ObjectMapper Call Failure");
458 }
James Feist7136a5a2018-07-19 09:52:05 -0700459
James Feist7136a5a2018-07-19 09:52:05 -0700460 if (respData.empty())
461 {
James Feist22c257a2018-08-31 14:07:12 -0700462 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700463 throw std::runtime_error("No configuration data available from Mapper");
464 }
465 // create a map of pair of <has pid configuration, ObjectManager path>
466 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
467 // and a map of <path, interface> for sensors
468 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700469 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700470 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700471 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700472 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700473 auto& owner = owners[ownerPair.first];
474 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700475 {
476
477 if (interface == objectManagerInterface)
478 {
479 owner.second = objectPair.first;
480 }
481 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700482 interface == pidZoneConfigurationInterface ||
483 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700484 {
485 owner.first = true;
486 }
487 if (interface == sensorInterface || interface == pwmInterface)
488 {
489 // we're not interested in pwm sensors, just pwm control
490 if (interface == sensorInterface &&
491 objectPair.first.find("pwm") != std::string::npos)
492 {
493 continue;
494 }
495 sensors[objectPair.first] = interface;
496 }
497 }
498 }
499 }
500 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700501 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700502 {
503 // skip if no pid configuration (means probably a sensor)
504 if (!owner.second.first)
505 {
506 continue;
507 }
508 auto endpoint = bus.new_method_call(
509 owner.first.c_str(), owner.second.second.c_str(),
510 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700511 ManagedObjectType configuration;
512 try
James Feist7136a5a2018-07-19 09:52:05 -0700513 {
James Feist22c257a2018-08-31 14:07:12 -0700514 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700515 responce.read(configuration);
516 }
517 catch (sdbusplus::exception_t&)
518 {
519 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700520 throw std::runtime_error("Error getting managed objects from " +
521 owner.first);
522 }
James Feist22c257a2018-08-31 14:07:12 -0700523
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700524 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700525 {
526 if (pathPair.second.find(pidConfigurationInterface) !=
527 pathPair.second.end() ||
528 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700529 pathPair.second.end() ||
530 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700531 pathPair.second.end())
532 {
533 configurations.emplace(pathPair);
534 }
James Feistf0096a02019-02-21 11:25:22 -0800535 }
536 }
537
538 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700539 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
540 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800541 {
James Feist3987c8b2019-05-13 10:43:17 -0700542 for (auto pathIt = configurations.begin();
543 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800544 {
James Feist3987c8b2019-05-13 10:43:17 -0700545 for (auto confIt = pathIt->second.begin();
546 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800547 {
James Feist3987c8b2019-05-13 10:43:17 -0700548 auto profilesFind = confIt->second.find("Profiles");
549 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800550 {
James Feist3987c8b2019-05-13 10:43:17 -0700551 confIt++;
552 continue; // if no profiles selected, apply always
553 }
554 auto profiles =
555 std::get<std::vector<std::string>>(profilesFind->second);
556 if (profiles.empty())
557 {
558 confIt++;
559 continue;
560 }
561
562 bool found = false;
563 for (const std::string& profile : profiles)
564 {
565 if (std::find(selectedProfiles.begin(),
566 selectedProfiles.end(),
567 profile) != selectedProfiles.end())
568 {
569 found = true;
570 break;
571 }
572 }
573 if (found)
574 {
575 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800576 }
577 else
578 {
James Feist3987c8b2019-05-13 10:43:17 -0700579 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800580 }
581 }
James Feist3987c8b2019-05-13 10:43:17 -0700582 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800583 {
James Feist3987c8b2019-05-13 10:43:17 -0700584 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800585 }
James Feist3987c8b2019-05-13 10:43:17 -0700586 else
James Feistf0096a02019-02-21 11:25:22 -0800587 {
James Feist3987c8b2019-05-13 10:43:17 -0700588 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800589 }
James Feist7136a5a2018-07-19 09:52:05 -0700590 }
591 }
James Feist8c3c51e2018-08-08 16:31:43 -0700592
593 // on dbus having an index field is a bit strange, so randomly
594 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800595 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700596 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700597 {
598 auto findZone =
599 configuration.second.find(pidZoneConfigurationInterface);
600 if (findZone != configuration.second.end())
601 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700602 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800603
James Feist1f802f52019-02-08 13:51:43 -0800604 const std::string& name = std::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800605 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700606
Patrick Venturec54fbd82018-10-30 19:40:05 -0700607 auto& details = zoneDetailsConfig[index];
James Feist3484bed2019-02-25 13:28:18 -0800608 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
609 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800610 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800611 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700612 }
613 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700614 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700615 {
James Feist8c3c51e2018-08-08 16:31:43 -0700616
James Feist22c257a2018-08-31 14:07:12 -0700617 const auto& base =
618 configuration.second.at(pidConfigurationInterface);
619 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800620 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700621 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700622 {
James Feistffd418b2018-11-15 14:46:36 -0800623 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800624 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700625
626 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800627 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700628 auto findOutputs =
629 base.find("Outputs"); // currently only fans have outputs
630 if (findOutputs != base.end())
631 {
632 std::vector<std::string> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800633 std::get<std::vector<std::string>>(findOutputs->second);
James Feist50fdfe32018-09-24 15:51:09 -0700634 sensorNames.insert(sensorNames.end(), outputs.begin(),
635 outputs.end());
636 }
James Feist1738e2a2019-02-04 15:57:03 -0800637
James Feist50fdfe32018-09-24 15:51:09 -0700638 std::vector<std::string> inputs;
James Feist1738e2a2019-02-04 15:57:03 -0800639 std::vector<std::pair<std::string, std::string>>
640 sensorInterfaces;
James Feist50fdfe32018-09-24 15:51:09 -0700641 for (const std::string& sensorName : sensorNames)
642 {
643 std::string name = sensorName;
644 // replace spaces with underscores to be legal on dbus
645 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800646 findSensors(sensors, name, sensorInterfaces);
647 }
James Feist50fdfe32018-09-24 15:51:09 -0700648
James Feist1738e2a2019-02-04 15:57:03 -0800649 for (const auto& sensorPathIfacePair : sensorInterfaces)
650 {
651
James Feist50fdfe32018-09-24 15:51:09 -0700652 if (sensorPathIfacePair.second == sensorInterface)
653 {
James Feist1738e2a2019-02-04 15:57:03 -0800654 size_t idx =
655 sensorPathIfacePair.first.find_last_of("/") + 1;
656 std::string shortName =
657 sensorPathIfacePair.first.substr(idx);
658
659 inputs.push_back(shortName);
660 auto& config = sensorConfig[shortName];
James Feist1f802f52019-02-08 13:51:43 -0800661 config.type = std::get<std::string>(base.at("Class"));
Patrick Venture69c51062019-02-11 09:46:03 -0800662 config.readPath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700663 // todo: maybe un-hardcode this if we run into slower
664 // timeouts with sensors
665 if (config.type == "temp")
666 {
James Feist2642cb52019-02-25 13:00:16 -0800667 config.timeout = 0;
James Feist3433cb62019-11-11 16:12:45 -0800668 config.ignoreDbusMinMax = true;
James Feist50fdfe32018-09-24 15:51:09 -0700669 }
670 }
671 else if (sensorPathIfacePair.second == pwmInterface)
672 {
673 // copy so we can modify it
674 for (std::string otherSensor : sensorNames)
675 {
James Feist1738e2a2019-02-04 15:57:03 -0800676 std::replace(otherSensor.begin(), otherSensor.end(),
677 ' ', '_');
678 if (sensorPathIfacePair.first.find(otherSensor) !=
679 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700680 {
681 continue;
682 }
James Feist1738e2a2019-02-04 15:57:03 -0800683
Patrick Venturec54fbd82018-10-30 19:40:05 -0700684 auto& config = sensorConfig[otherSensor];
Patrick Venture69c51062019-02-11 09:46:03 -0800685 config.writePath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700686 // todo: un-hardcode this if there are fans with
687 // different ranges
688 config.max = 255;
689 config.min = 0;
690 }
691 }
692 }
James Feist1738e2a2019-02-04 15:57:03 -0800693
James Feist11d243d2019-06-24 16:18:40 -0700694 // if the sensors aren't available in the current state, don't
695 // add them to the configuration.
696 if (inputs.empty())
697 {
698 continue;
699 }
700
James Feist5ec20272019-07-10 11:59:57 -0700701 std::string offsetType;
James Feist50fdfe32018-09-24 15:51:09 -0700702
James Feist5ec20272019-07-10 11:59:57 -0700703 // SetPointOffset is a threshold value to pull from the sensor
704 // to apply an offset. For upper thresholds this means the
705 // setpoint is usually negative.
706 auto findSetpointOffset = base.find("SetPointOffset");
707 if (findSetpointOffset != base.end())
James Feist22c257a2018-08-31 14:07:12 -0700708 {
James Feist5ec20272019-07-10 11:59:57 -0700709 offsetType =
710 std::get<std::string>(findSetpointOffset->second);
711 if (std::find(thresholds::types.begin(),
712 thresholds::types.end(),
713 offsetType) == thresholds::types.end())
714 {
715 throw std::runtime_error("Unsupported type: " +
716 offsetType);
717 }
718 }
719
720 if (offsetType.empty())
721 {
722 struct conf::ControllerInfo& info =
723 conf[std::get<std::string>(base.at("Name"))];
724 info.inputs = std::move(inputs);
725 populatePidInfo(bus, base, info, nullptr);
James Feist22c257a2018-08-31 14:07:12 -0700726 }
727 else
728 {
James Feist5ec20272019-07-10 11:59:57 -0700729 // we have to split up the inputs, as in practice t-control
730 // values will differ, making setpoints differ
731 for (const std::string& input : inputs)
732 {
733 struct conf::ControllerInfo& info = conf[input];
734 info.inputs.emplace_back(input);
735 populatePidInfo(bus, base, info, &offsetType);
736 }
James Feist22c257a2018-08-31 14:07:12 -0700737 }
James Feist22c257a2018-08-31 14:07:12 -0700738 }
739 }
740 auto findStepwise =
741 configuration.second.find(stepwiseConfigurationInterface);
742 if (findStepwise != configuration.second.end())
743 {
744 const auto& base = findStepwise->second;
745 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800746 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700747 for (const std::string& zone : zones)
748 {
James Feistffd418b2018-11-15 14:46:36 -0800749 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800750 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700751
752 std::vector<std::string> inputs;
753 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800754 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700755
James Feist1738e2a2019-02-04 15:57:03 -0800756 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700757 for (const std::string& sensorName : sensorNames)
758 {
759 std::string name = sensorName;
760 // replace spaces with underscores to be legal on dbus
761 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800762 std::vector<std::pair<std::string, std::string>>
763 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700764
James Feist1738e2a2019-02-04 15:57:03 -0800765 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700766 {
James Feist50fdfe32018-09-24 15:51:09 -0700767 break;
768 }
769
James Feist1738e2a2019-02-04 15:57:03 -0800770 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
771 {
772 size_t idx =
773 sensorPathIfacePair.first.find_last_of("/") + 1;
774 std::string shortName =
775 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700776
James Feist1738e2a2019-02-04 15:57:03 -0800777 inputs.push_back(shortName);
778 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800779 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800780 config.type = "temp";
James Feist3660b382019-11-11 16:29:19 -0800781 config.ignoreDbusMinMax = true;
James Feist1738e2a2019-02-04 15:57:03 -0800782 // todo: maybe un-hardcode this if we run into slower
783 // timeouts with sensors
784
James Feist2642cb52019-02-25 13:00:16 -0800785 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800786 sensorFound = true;
787 }
James Feist50fdfe32018-09-24 15:51:09 -0700788 }
789 if (!sensorFound)
790 {
791 continue;
792 }
James Feistf81f2882019-02-26 11:26:36 -0800793 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800794 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700795 info.inputs = std::move(inputs);
796
James Feist22c257a2018-08-31 14:07:12 -0700797 info.type = "stepwise";
798 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700799 info.stepwiseInfo.positiveHysteresis = 0.0;
800 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800801 std::string subtype = std::get<std::string>(base.at("Class"));
802
803 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700804 auto findPosHyst = base.find("PositiveHysteresis");
805 auto findNegHyst = base.find("NegativeHysteresis");
806 if (findPosHyst != base.end())
807 {
James Feist1f802f52019-02-08 13:51:43 -0800808 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800809 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700810 }
811 if (findNegHyst != base.end())
812 {
James Feist5782ab82019-04-02 08:38:48 -0700813 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800814 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700815 }
James Feist22c257a2018-08-31 14:07:12 -0700816 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800817 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700818 if (readings.size() > ec::maxStepwisePoints)
819 {
820 throw std::invalid_argument("Too many stepwise points.");
821 }
822 if (readings.empty())
823 {
824 throw std::invalid_argument(
825 "Must have one stepwise point.");
826 }
827 std::copy(readings.begin(), readings.end(),
828 info.stepwiseInfo.reading);
829 if (readings.size() < ec::maxStepwisePoints)
830 {
831 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800832 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700833 }
834 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800835 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700836 if (readings.size() != outputs.size())
837 {
838 throw std::invalid_argument(
839 "Outputs size must match readings");
840 }
841 std::copy(outputs.begin(), outputs.end(),
842 info.stepwiseInfo.output);
843 if (outputs.size() < ec::maxStepwisePoints)
844 {
845 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800846 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700847 }
James Feist7136a5a2018-07-19 09:52:05 -0700848 }
849 }
850 }
James Feistf0096a02019-02-21 11:25:22 -0800851 if constexpr (DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700852 {
853 debugPrint();
854 }
James Feistc959c422018-11-01 12:33:40 -0700855 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700856 {
James Feist1fe08952019-05-07 09:17:16 -0700857 std::cerr
858 << "No fan zones, application pausing until new configuration\n";
859 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700860 }
James Feist1fe08952019-05-07 09:17:16 -0700861 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700862}
863} // namespace dbus_configuration