blob: c33264c367941480fd1a6e3c61274fc29780e9c5 [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
53namespace dbus_configuration
54{
55
James Feist1738e2a2019-02-04 15:57:03 -080056bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
57 const std::string& search,
58 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070059{
James Feist1738e2a2019-02-04 15:57:03 -080060 std::smatch match;
61 std::regex reg(search);
62 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070063 {
James Feist1738e2a2019-02-04 15:57:03 -080064 if (std::regex_search(sensor.first, match, reg))
65 {
66 matches.push_back(sensor);
67 }
James Feist7136a5a2018-07-19 09:52:05 -070068 }
Patrick Venture107a25d2018-10-13 14:08:09 -070069
James Feist1738e2a2019-02-04 15:57:03 -080070 return matches.size() > 0;
James Feist7136a5a2018-07-19 09:52:05 -070071}
72
73// this function prints the configuration into a form similar to the cpp
74// generated code to help in verification, should be turned off during normal
75// use
76void debugPrint(void)
77{
78 // print sensor config
79 std::cout << "sensor config:\n";
80 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070081 for (const auto& pair : sensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -070082 {
83
84 std::cout << "\t{" << pair.first << ",\n\t\t{";
85 std::cout << pair.second.type << ", ";
Patrick Venture69c51062019-02-11 09:46:03 -080086 std::cout << pair.second.readPath << ", ";
87 std::cout << pair.second.writePath << ", ";
James Feist7136a5a2018-07-19 09:52:05 -070088 std::cout << pair.second.min << ", ";
89 std::cout << pair.second.max << ", ";
90 std::cout << pair.second.timeout << "},\n\t},\n";
91 }
92 std::cout << "}\n\n";
93 std::cout << "ZoneDetailsConfig\n";
94 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070095 for (const auto& zone : zoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070096 {
97 std::cout << "\t{" << zone.first << ",\n";
James Feist3484bed2019-02-25 13:28:18 -080098 std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
Patrick Venture8e2fdb32019-02-11 09:39:59 -080099 std::cout << zone.second.failsafePercent << "}\n\t},\n";
James Feist7136a5a2018-07-19 09:52:05 -0700100 }
101 std::cout << "}\n\n";
102 std::cout << "ZoneConfig\n";
103 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700104 for (const auto& zone : zoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700105 {
106 std::cout << "\t{" << zone.first << "\n";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700107 for (const auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700108 {
109 std::cout << "\t\t{" << pidconf.first << ",\n";
110 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
111 std::cout << "\t\t\t{";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700112 for (const auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700113 {
114 std::cout << "\n\t\t\t" << input << ",\n";
115 }
116 std::cout << "\t\t\t}\n";
117 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700118 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
Patrick Venture7442c372019-02-11 10:21:05 -0800119 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
120 << ",\n";
121 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
122 << ",\n";
123 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
124 << ",\n";
125 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
126 << ",\n";
127 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
128 << "," << pidconf.second.pidInfo.integralLimit.max
129 << "},\n";
130 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
131 << pidconf.second.pidInfo.outLim.max << "},\n";
132 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
133 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700134 std::cout << "\t\t\t}\n\t\t}\n";
135 }
136 std::cout << "\t},\n";
137 }
138 std::cout << "}\n\n";
139}
140
James Feistffd418b2018-11-15 14:46:36 -0800141size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
142{
143 auto it = std::find(zones.begin(), zones.end(), name);
144 if (it == zones.end())
145 {
146 zones.emplace_back(name);
147 it = zones.end() - 1;
148 }
149
150 return it - zones.begin();
151}
152
James Feistf0096a02019-02-21 11:25:22 -0800153std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus)
154{
155 std::vector<std::string> ret;
156 auto mapper =
157 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
158 "/xyz/openbmc_project/object_mapper",
159 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
160 mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface});
161 std::unordered_map<
162 std::string, std::unordered_map<std::string, std::vector<std::string>>>
163 respData;
164
165 try
166 {
167 auto resp = bus.call(mapper);
168 resp.read(respData);
169 }
170 catch (sdbusplus::exception_t&)
171 {
172 // can't do anything without mapper call data
173 throw std::runtime_error("ObjectMapper Call Failure");
174 }
175 if (respData.empty())
176 {
177 // if the user has profiles but doesn't expose the interface to select
178 // one, just go ahead without using profiles
179 return ret;
180 }
181
182 // assumption is that we should only have a small handful of selected
183 // profiles at a time (probably only 1), so calling each individually should
184 // not incur a large cost
185 for (const auto& objectPair : respData)
186 {
187 const std::string& path = objectPair.first;
188 for (const auto& ownerPair : objectPair.second)
189 {
190 const std::string& busName = ownerPair.first;
191 auto getProfile =
192 bus.new_method_call(busName.c_str(), path.c_str(),
193 "org.freedesktop.DBus.Properties", "Get");
194 getProfile.append(thermalControlIface, "Current");
195 std::variant<std::string> variantResp;
196 try
197 {
198 auto resp = bus.call(getProfile);
199 resp.read(variantResp);
200 }
201 catch (sdbusplus::exception_t&)
202 {
203 throw std::runtime_error("Failure getting profile");
204 }
205 std::string mode = std::get<std::string>(variantResp);
206 ret.emplace_back(std::move(mode));
207 }
208 }
209 if constexpr (DEBUG)
210 {
211 std::cout << "Profiles selected: ";
212 for (const auto& profile : ret)
213 {
214 std::cout << profile << " ";
215 }
216 std::cout << "\n";
217 }
218 return ret;
219}
220
James Feist1fe08952019-05-07 09:17:16 -0700221int eventHandler(sd_bus_message*, void* context, sd_bus_error*)
James Feist7136a5a2018-07-19 09:52:05 -0700222{
James Feist1fe08952019-05-07 09:17:16 -0700223
224 if (context == nullptr)
225 {
226 throw std::runtime_error("Invalid match");
227 }
228 boost::asio::steady_timer* timer =
229 static_cast<boost::asio::steady_timer*>(context);
230
231 // do a brief sleep as we tend to get a bunch of these events at
232 // once
233 timer->expires_after(std::chrono::seconds(2));
234 timer->async_wait([](const boost::system::error_code ec) {
235 if (ec == boost::asio::error::operation_aborted)
236 {
237 /* another timer started*/
238 return;
239 }
240
241 std::cout << "New configuration detected, reloading\n.";
242 restartControlLoops();
243 });
244
245 return 1;
246}
247
248void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
249{
250 // this is a list because the matches can't be moved
251 static std::list<sdbusplus::bus::match::match> matches;
252
James Feist3987c8b2019-05-13 10:43:17 -0700253 const std::array<std::string, 4> interfaces = {
254 thermalControlIface, pidConfigurationInterface,
255 pidZoneConfigurationInterface, stepwiseConfigurationInterface};
James Feist1fe08952019-05-07 09:17:16 -0700256
257 // this list only needs to be created once
258 if (!matches.empty())
259 {
260 return;
261 }
262
263 // we restart when the configuration changes or there are new sensors
264 for (const auto& interface : interfaces)
265 {
266 matches.emplace_back(
267 bus,
268 "type='signal',member='PropertiesChanged',arg0namespace='" +
269 interface + "'",
270 eventHandler, &timer);
271 }
272 matches.emplace_back(
273 bus,
274 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
275 "sensors/'",
276 eventHandler, &timer);
277}
278
279bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer)
280{
281
282 sensorConfig.clear();
283 zoneConfig.clear();
284 zoneDetailsConfig.clear();
285
286 createMatches(bus, timer);
287
James Feist22c257a2018-08-31 14:07:12 -0700288 using DbusVariantType =
James Feist1f802f52019-02-08 13:51:43 -0800289 std::variant<uint64_t, int64_t, double, std::string,
290 std::vector<std::string>, std::vector<double>>;
James Feist22c257a2018-08-31 14:07:12 -0700291
James Feist7136a5a2018-07-19 09:52:05 -0700292 using ManagedObjectType = std::unordered_map<
293 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700294 std::unordered_map<std::string,
295 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700296
James Feist7136a5a2018-07-19 09:52:05 -0700297 auto mapper =
298 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
299 "/xyz/openbmc_project/object_mapper",
300 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700301 mapper.append("/", 0,
James Feist3987c8b2019-05-13 10:43:17 -0700302 std::array<const char*, 6>{objectManagerInterface,
303 pidConfigurationInterface,
304 pidZoneConfigurationInterface,
305 stepwiseConfigurationInterface,
306 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700307 std::unordered_map<
308 std::string, std::unordered_map<std::string, std::vector<std::string>>>
309 respData;
James Feist22c257a2018-08-31 14:07:12 -0700310 try
311 {
312 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700313 resp.read(respData);
314 }
315 catch (sdbusplus::exception_t&)
316 {
317 // can't do anything without mapper call data
318 throw std::runtime_error("ObjectMapper Call Failure");
319 }
James Feist7136a5a2018-07-19 09:52:05 -0700320
James Feist7136a5a2018-07-19 09:52:05 -0700321 if (respData.empty())
322 {
James Feist22c257a2018-08-31 14:07:12 -0700323 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700324 throw std::runtime_error("No configuration data available from Mapper");
325 }
326 // create a map of pair of <has pid configuration, ObjectManager path>
327 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
328 // and a map of <path, interface> for sensors
329 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700330 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700331 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700332 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700333 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700334 auto& owner = owners[ownerPair.first];
335 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700336 {
337
338 if (interface == objectManagerInterface)
339 {
340 owner.second = objectPair.first;
341 }
342 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700343 interface == pidZoneConfigurationInterface ||
344 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700345 {
346 owner.first = true;
347 }
348 if (interface == sensorInterface || interface == pwmInterface)
349 {
350 // we're not interested in pwm sensors, just pwm control
351 if (interface == sensorInterface &&
352 objectPair.first.find("pwm") != std::string::npos)
353 {
354 continue;
355 }
356 sensors[objectPair.first] = interface;
357 }
358 }
359 }
360 }
361 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700362 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700363 {
364 // skip if no pid configuration (means probably a sensor)
365 if (!owner.second.first)
366 {
367 continue;
368 }
369 auto endpoint = bus.new_method_call(
370 owner.first.c_str(), owner.second.second.c_str(),
371 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700372 ManagedObjectType configuration;
373 try
James Feist7136a5a2018-07-19 09:52:05 -0700374 {
James Feist22c257a2018-08-31 14:07:12 -0700375 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700376 responce.read(configuration);
377 }
378 catch (sdbusplus::exception_t&)
379 {
380 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700381 throw std::runtime_error("Error getting managed objects from " +
382 owner.first);
383 }
James Feist22c257a2018-08-31 14:07:12 -0700384
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700385 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700386 {
387 if (pathPair.second.find(pidConfigurationInterface) !=
388 pathPair.second.end() ||
389 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700390 pathPair.second.end() ||
391 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700392 pathPair.second.end())
393 {
394 configurations.emplace(pathPair);
395 }
James Feistf0096a02019-02-21 11:25:22 -0800396 }
397 }
398
399 // remove controllers from config that aren't in the current profile(s)
James Feist3987c8b2019-05-13 10:43:17 -0700400 std::vector<std::string> selectedProfiles = getSelectedProfiles(bus);
401 if (selectedProfiles.size())
James Feistf0096a02019-02-21 11:25:22 -0800402 {
James Feist3987c8b2019-05-13 10:43:17 -0700403 for (auto pathIt = configurations.begin();
404 pathIt != configurations.end();)
James Feistf0096a02019-02-21 11:25:22 -0800405 {
James Feist3987c8b2019-05-13 10:43:17 -0700406 for (auto confIt = pathIt->second.begin();
407 confIt != pathIt->second.end();)
James Feistf0096a02019-02-21 11:25:22 -0800408 {
James Feist3987c8b2019-05-13 10:43:17 -0700409 auto profilesFind = confIt->second.find("Profiles");
410 if (profilesFind == confIt->second.end())
James Feistf0096a02019-02-21 11:25:22 -0800411 {
James Feist3987c8b2019-05-13 10:43:17 -0700412 confIt++;
413 continue; // if no profiles selected, apply always
414 }
415 auto profiles =
416 std::get<std::vector<std::string>>(profilesFind->second);
417 if (profiles.empty())
418 {
419 confIt++;
420 continue;
421 }
422
423 bool found = false;
424 for (const std::string& profile : profiles)
425 {
426 if (std::find(selectedProfiles.begin(),
427 selectedProfiles.end(),
428 profile) != selectedProfiles.end())
429 {
430 found = true;
431 break;
432 }
433 }
434 if (found)
435 {
436 confIt++;
James Feistf0096a02019-02-21 11:25:22 -0800437 }
438 else
439 {
James Feist3987c8b2019-05-13 10:43:17 -0700440 confIt = pathIt->second.erase(confIt);
James Feistf0096a02019-02-21 11:25:22 -0800441 }
442 }
James Feist3987c8b2019-05-13 10:43:17 -0700443 if (pathIt->second.empty())
James Feistf0096a02019-02-21 11:25:22 -0800444 {
James Feist3987c8b2019-05-13 10:43:17 -0700445 pathIt = configurations.erase(pathIt);
James Feistf0096a02019-02-21 11:25:22 -0800446 }
James Feist3987c8b2019-05-13 10:43:17 -0700447 else
James Feistf0096a02019-02-21 11:25:22 -0800448 {
James Feist3987c8b2019-05-13 10:43:17 -0700449 pathIt++;
James Feistf0096a02019-02-21 11:25:22 -0800450 }
James Feist7136a5a2018-07-19 09:52:05 -0700451 }
452 }
James Feist8c3c51e2018-08-08 16:31:43 -0700453
454 // on dbus having an index field is a bit strange, so randomly
455 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800456 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700457 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700458 {
459 auto findZone =
460 configuration.second.find(pidZoneConfigurationInterface);
461 if (findZone != configuration.second.end())
462 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700463 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800464
James Feist1f802f52019-02-08 13:51:43 -0800465 const std::string& name = std::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800466 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700467
Patrick Venturec54fbd82018-10-30 19:40:05 -0700468 auto& details = zoneDetailsConfig[index];
James Feist3484bed2019-02-25 13:28:18 -0800469 details.minThermalOutput = std::visit(VariantToDoubleVisitor(),
470 zone.at("MinThermalOutput"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800471 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800472 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700473 }
474 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700475 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700476 {
James Feist8c3c51e2018-08-08 16:31:43 -0700477
James Feist22c257a2018-08-31 14:07:12 -0700478 const auto& base =
479 configuration.second.at(pidConfigurationInterface);
480 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800481 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700482 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700483 {
James Feistffd418b2018-11-15 14:46:36 -0800484 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800485 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700486
487 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800488 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700489 auto findOutputs =
490 base.find("Outputs"); // currently only fans have outputs
491 if (findOutputs != base.end())
492 {
493 std::vector<std::string> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800494 std::get<std::vector<std::string>>(findOutputs->second);
James Feist50fdfe32018-09-24 15:51:09 -0700495 sensorNames.insert(sensorNames.end(), outputs.begin(),
496 outputs.end());
497 }
James Feist1738e2a2019-02-04 15:57:03 -0800498
James Feist50fdfe32018-09-24 15:51:09 -0700499 std::vector<std::string> inputs;
James Feist1738e2a2019-02-04 15:57:03 -0800500 std::vector<std::pair<std::string, std::string>>
501 sensorInterfaces;
James Feist50fdfe32018-09-24 15:51:09 -0700502 for (const std::string& sensorName : sensorNames)
503 {
504 std::string name = sensorName;
505 // replace spaces with underscores to be legal on dbus
506 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800507 findSensors(sensors, name, sensorInterfaces);
508 }
James Feist50fdfe32018-09-24 15:51:09 -0700509
James Feist1738e2a2019-02-04 15:57:03 -0800510 for (const auto& sensorPathIfacePair : sensorInterfaces)
511 {
512
James Feist50fdfe32018-09-24 15:51:09 -0700513 if (sensorPathIfacePair.second == sensorInterface)
514 {
James Feist1738e2a2019-02-04 15:57:03 -0800515 size_t idx =
516 sensorPathIfacePair.first.find_last_of("/") + 1;
517 std::string shortName =
518 sensorPathIfacePair.first.substr(idx);
519
520 inputs.push_back(shortName);
521 auto& config = sensorConfig[shortName];
James Feist1f802f52019-02-08 13:51:43 -0800522 config.type = std::get<std::string>(base.at("Class"));
Patrick Venture69c51062019-02-11 09:46:03 -0800523 config.readPath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700524 // todo: maybe un-hardcode this if we run into slower
525 // timeouts with sensors
526 if (config.type == "temp")
527 {
James Feist2642cb52019-02-25 13:00:16 -0800528 config.timeout = 0;
James Feist50fdfe32018-09-24 15:51:09 -0700529 }
James Feist75eb7692019-02-25 12:50:02 -0800530 else if (config.type == "fan")
531 {
532 config.max = conf::inheritValueFromDbus;
533 config.min = conf::inheritValueFromDbus;
534 }
James Feist50fdfe32018-09-24 15:51:09 -0700535 }
536 else if (sensorPathIfacePair.second == pwmInterface)
537 {
538 // copy so we can modify it
539 for (std::string otherSensor : sensorNames)
540 {
James Feist1738e2a2019-02-04 15:57:03 -0800541 std::replace(otherSensor.begin(), otherSensor.end(),
542 ' ', '_');
543 if (sensorPathIfacePair.first.find(otherSensor) !=
544 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700545 {
546 continue;
547 }
James Feist1738e2a2019-02-04 15:57:03 -0800548
Patrick Venturec54fbd82018-10-30 19:40:05 -0700549 auto& config = sensorConfig[otherSensor];
Patrick Venture69c51062019-02-11 09:46:03 -0800550 config.writePath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700551 // todo: un-hardcode this if there are fans with
552 // different ranges
553 config.max = 255;
554 config.min = 0;
555 }
556 }
557 }
James Feist1738e2a2019-02-04 15:57:03 -0800558
James Feist11d243d2019-06-24 16:18:40 -0700559 // if the sensors aren't available in the current state, don't
560 // add them to the configuration.
561 if (inputs.empty())
562 {
563 continue;
564 }
565
James Feistf81f2882019-02-26 11:26:36 -0800566 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800567 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700568 info.inputs = std::move(inputs);
569
James Feist1f802f52019-02-08 13:51:43 -0800570 info.type = std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700571 // todo: auto generation yaml -> c script seems to discard this
572 // value for fans, verify this is okay
573 if (info.type == "fan")
574 {
575 info.setpoint = 0;
576 }
577 else
578 {
James Feist1f802f52019-02-08 13:51:43 -0800579 info.setpoint = std::visit(VariantToDoubleVisitor(),
580 base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700581 }
582 info.pidInfo.ts = 1.0; // currently unused
Patrick Venture7442c372019-02-11 10:21:05 -0800583 info.pidInfo.proportionalCoeff = std::visit(
584 VariantToDoubleVisitor(), base.at("PCoefficient"));
585 info.pidInfo.integralCoeff = std::visit(
586 VariantToDoubleVisitor(), base.at("ICoefficient"));
587 info.pidInfo.feedFwdOffset = std::visit(
588 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
589 info.pidInfo.feedFwdGain = std::visit(
590 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
591 info.pidInfo.integralLimit.max =
James Feist1f802f52019-02-08 13:51:43 -0800592 std::visit(VariantToDoubleVisitor(), base.at("ILimitMax"));
Patrick Venture7442c372019-02-11 10:21:05 -0800593 info.pidInfo.integralLimit.min =
James Feist1f802f52019-02-08 13:51:43 -0800594 std::visit(VariantToDoubleVisitor(), base.at("ILimitMin"));
Patrick Venture7442c372019-02-11 10:21:05 -0800595 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
596 base.at("OutLimitMax"));
597 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
598 base.at("OutLimitMin"));
599 info.pidInfo.slewNeg =
James Feist1f802f52019-02-08 13:51:43 -0800600 std::visit(VariantToDoubleVisitor(), base.at("SlewNeg"));
Patrick Venture7442c372019-02-11 10:21:05 -0800601 info.pidInfo.slewPos =
James Feist1f802f52019-02-08 13:51:43 -0800602 std::visit(VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist572c43d2019-01-31 15:52:22 -0800603 double negativeHysteresis = 0;
604 double positiveHysteresis = 0;
605
606 auto findNeg = base.find("NegativeHysteresis");
607 auto findPos = base.find("PositiveHysteresis");
608 if (findNeg != base.end())
609 {
James Feist1f802f52019-02-08 13:51:43 -0800610 negativeHysteresis =
611 std::visit(VariantToDoubleVisitor(), findNeg->second);
James Feist572c43d2019-01-31 15:52:22 -0800612 }
613
614 if (findPos != base.end())
615 {
James Feist1f802f52019-02-08 13:51:43 -0800616 positiveHysteresis =
617 std::visit(VariantToDoubleVisitor(), findPos->second);
James Feist572c43d2019-01-31 15:52:22 -0800618 }
619 info.pidInfo.negativeHysteresis = negativeHysteresis;
620 info.pidInfo.positiveHysteresis = positiveHysteresis;
James Feist22c257a2018-08-31 14:07:12 -0700621 }
622 }
623 auto findStepwise =
624 configuration.second.find(stepwiseConfigurationInterface);
625 if (findStepwise != configuration.second.end())
626 {
627 const auto& base = findStepwise->second;
628 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800629 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700630 for (const std::string& zone : zones)
631 {
James Feistffd418b2018-11-15 14:46:36 -0800632 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800633 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700634
635 std::vector<std::string> inputs;
636 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800637 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700638
James Feist1738e2a2019-02-04 15:57:03 -0800639 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700640 for (const std::string& sensorName : sensorNames)
641 {
642 std::string name = sensorName;
643 // replace spaces with underscores to be legal on dbus
644 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800645 std::vector<std::pair<std::string, std::string>>
646 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700647
James Feist1738e2a2019-02-04 15:57:03 -0800648 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700649 {
James Feist50fdfe32018-09-24 15:51:09 -0700650 break;
651 }
652
James Feist1738e2a2019-02-04 15:57:03 -0800653 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
654 {
655 size_t idx =
656 sensorPathIfacePair.first.find_last_of("/") + 1;
657 std::string shortName =
658 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700659
James Feist1738e2a2019-02-04 15:57:03 -0800660 inputs.push_back(shortName);
661 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800662 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800663 config.type = "temp";
664 // todo: maybe un-hardcode this if we run into slower
665 // timeouts with sensors
666
James Feist2642cb52019-02-25 13:00:16 -0800667 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800668 sensorFound = true;
669 }
James Feist50fdfe32018-09-24 15:51:09 -0700670 }
671 if (!sensorFound)
672 {
673 continue;
674 }
James Feistf81f2882019-02-26 11:26:36 -0800675 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800676 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700677 info.inputs = std::move(inputs);
678
James Feist22c257a2018-08-31 14:07:12 -0700679 info.type = "stepwise";
680 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700681 info.stepwiseInfo.positiveHysteresis = 0.0;
682 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800683 std::string subtype = std::get<std::string>(base.at("Class"));
684
685 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700686 auto findPosHyst = base.find("PositiveHysteresis");
687 auto findNegHyst = base.find("NegativeHysteresis");
688 if (findPosHyst != base.end())
689 {
James Feist1f802f52019-02-08 13:51:43 -0800690 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800691 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700692 }
693 if (findNegHyst != base.end())
694 {
James Feist5782ab82019-04-02 08:38:48 -0700695 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800696 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700697 }
James Feist22c257a2018-08-31 14:07:12 -0700698 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800699 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700700 if (readings.size() > ec::maxStepwisePoints)
701 {
702 throw std::invalid_argument("Too many stepwise points.");
703 }
704 if (readings.empty())
705 {
706 throw std::invalid_argument(
707 "Must have one stepwise point.");
708 }
709 std::copy(readings.begin(), readings.end(),
710 info.stepwiseInfo.reading);
711 if (readings.size() < ec::maxStepwisePoints)
712 {
713 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800714 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700715 }
716 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800717 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700718 if (readings.size() != outputs.size())
719 {
720 throw std::invalid_argument(
721 "Outputs size must match readings");
722 }
723 std::copy(outputs.begin(), outputs.end(),
724 info.stepwiseInfo.output);
725 if (outputs.size() < ec::maxStepwisePoints)
726 {
727 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800728 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700729 }
James Feist7136a5a2018-07-19 09:52:05 -0700730 }
731 }
732 }
James Feistf0096a02019-02-21 11:25:22 -0800733 if constexpr (DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700734 {
735 debugPrint();
736 }
James Feistc959c422018-11-01 12:33:40 -0700737 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700738 {
James Feist1fe08952019-05-07 09:17:16 -0700739 std::cerr
740 << "No fan zones, application pausing until new configuration\n";
741 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700742 }
James Feist1fe08952019-05-07 09:17:16 -0700743 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700744}
745} // namespace dbus_configuration