blob: 1fd0d1afd080877b3f9de3fb986ec96ee8048d04 [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 // if the sensors aren't available in the current state, don't
511 // add them to the configuration.
512 if (sensorInterfaces.empty())
513 {
514 continue;
515 }
516 for (const auto& sensorPathIfacePair : sensorInterfaces)
517 {
518
James Feist50fdfe32018-09-24 15:51:09 -0700519 if (sensorPathIfacePair.second == sensorInterface)
520 {
James Feist1738e2a2019-02-04 15:57:03 -0800521 size_t idx =
522 sensorPathIfacePair.first.find_last_of("/") + 1;
523 std::string shortName =
524 sensorPathIfacePair.first.substr(idx);
525
526 inputs.push_back(shortName);
527 auto& config = sensorConfig[shortName];
James Feist1f802f52019-02-08 13:51:43 -0800528 config.type = std::get<std::string>(base.at("Class"));
Patrick Venture69c51062019-02-11 09:46:03 -0800529 config.readPath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700530 // todo: maybe un-hardcode this if we run into slower
531 // timeouts with sensors
532 if (config.type == "temp")
533 {
James Feist2642cb52019-02-25 13:00:16 -0800534 config.timeout = 0;
James Feist50fdfe32018-09-24 15:51:09 -0700535 }
James Feist75eb7692019-02-25 12:50:02 -0800536 else if (config.type == "fan")
537 {
538 config.max = conf::inheritValueFromDbus;
539 config.min = conf::inheritValueFromDbus;
540 }
James Feist50fdfe32018-09-24 15:51:09 -0700541 }
542 else if (sensorPathIfacePair.second == pwmInterface)
543 {
544 // copy so we can modify it
545 for (std::string otherSensor : sensorNames)
546 {
James Feist1738e2a2019-02-04 15:57:03 -0800547 std::replace(otherSensor.begin(), otherSensor.end(),
548 ' ', '_');
549 if (sensorPathIfacePair.first.find(otherSensor) !=
550 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700551 {
552 continue;
553 }
James Feist1738e2a2019-02-04 15:57:03 -0800554
Patrick Venturec54fbd82018-10-30 19:40:05 -0700555 auto& config = sensorConfig[otherSensor];
Patrick Venture69c51062019-02-11 09:46:03 -0800556 config.writePath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700557 // todo: un-hardcode this if there are fans with
558 // different ranges
559 config.max = 255;
560 config.min = 0;
561 }
562 }
563 }
James Feist1738e2a2019-02-04 15:57:03 -0800564
James Feistf81f2882019-02-26 11:26:36 -0800565 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800566 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700567 info.inputs = std::move(inputs);
568
James Feist1f802f52019-02-08 13:51:43 -0800569 info.type = std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700570 // todo: auto generation yaml -> c script seems to discard this
571 // value for fans, verify this is okay
572 if (info.type == "fan")
573 {
574 info.setpoint = 0;
575 }
576 else
577 {
James Feist1f802f52019-02-08 13:51:43 -0800578 info.setpoint = std::visit(VariantToDoubleVisitor(),
579 base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700580 }
581 info.pidInfo.ts = 1.0; // currently unused
Patrick Venture7442c372019-02-11 10:21:05 -0800582 info.pidInfo.proportionalCoeff = std::visit(
583 VariantToDoubleVisitor(), base.at("PCoefficient"));
584 info.pidInfo.integralCoeff = std::visit(
585 VariantToDoubleVisitor(), base.at("ICoefficient"));
586 info.pidInfo.feedFwdOffset = std::visit(
587 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
588 info.pidInfo.feedFwdGain = std::visit(
589 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
590 info.pidInfo.integralLimit.max =
James Feist1f802f52019-02-08 13:51:43 -0800591 std::visit(VariantToDoubleVisitor(), base.at("ILimitMax"));
Patrick Venture7442c372019-02-11 10:21:05 -0800592 info.pidInfo.integralLimit.min =
James Feist1f802f52019-02-08 13:51:43 -0800593 std::visit(VariantToDoubleVisitor(), base.at("ILimitMin"));
Patrick Venture7442c372019-02-11 10:21:05 -0800594 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
595 base.at("OutLimitMax"));
596 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
597 base.at("OutLimitMin"));
598 info.pidInfo.slewNeg =
James Feist1f802f52019-02-08 13:51:43 -0800599 std::visit(VariantToDoubleVisitor(), base.at("SlewNeg"));
Patrick Venture7442c372019-02-11 10:21:05 -0800600 info.pidInfo.slewPos =
James Feist1f802f52019-02-08 13:51:43 -0800601 std::visit(VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist572c43d2019-01-31 15:52:22 -0800602 double negativeHysteresis = 0;
603 double positiveHysteresis = 0;
604
605 auto findNeg = base.find("NegativeHysteresis");
606 auto findPos = base.find("PositiveHysteresis");
607 if (findNeg != base.end())
608 {
James Feist1f802f52019-02-08 13:51:43 -0800609 negativeHysteresis =
610 std::visit(VariantToDoubleVisitor(), findNeg->second);
James Feist572c43d2019-01-31 15:52:22 -0800611 }
612
613 if (findPos != base.end())
614 {
James Feist1f802f52019-02-08 13:51:43 -0800615 positiveHysteresis =
616 std::visit(VariantToDoubleVisitor(), findPos->second);
James Feist572c43d2019-01-31 15:52:22 -0800617 }
618 info.pidInfo.negativeHysteresis = negativeHysteresis;
619 info.pidInfo.positiveHysteresis = positiveHysteresis;
James Feist22c257a2018-08-31 14:07:12 -0700620 }
621 }
622 auto findStepwise =
623 configuration.second.find(stepwiseConfigurationInterface);
624 if (findStepwise != configuration.second.end())
625 {
626 const auto& base = findStepwise->second;
627 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800628 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700629 for (const std::string& zone : zones)
630 {
James Feistffd418b2018-11-15 14:46:36 -0800631 size_t index = getZoneIndex(zone, foundZones);
James Feistf81f2882019-02-26 11:26:36 -0800632 conf::PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700633
634 std::vector<std::string> inputs;
635 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800636 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700637
James Feist1738e2a2019-02-04 15:57:03 -0800638 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700639 for (const std::string& sensorName : sensorNames)
640 {
641 std::string name = sensorName;
642 // replace spaces with underscores to be legal on dbus
643 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800644 std::vector<std::pair<std::string, std::string>>
645 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700646
James Feist1738e2a2019-02-04 15:57:03 -0800647 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700648 {
James Feist50fdfe32018-09-24 15:51:09 -0700649 break;
650 }
651
James Feist1738e2a2019-02-04 15:57:03 -0800652 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
653 {
654 size_t idx =
655 sensorPathIfacePair.first.find_last_of("/") + 1;
656 std::string shortName =
657 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700658
James Feist1738e2a2019-02-04 15:57:03 -0800659 inputs.push_back(shortName);
660 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800661 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800662 config.type = "temp";
663 // todo: maybe un-hardcode this if we run into slower
664 // timeouts with sensors
665
James Feist2642cb52019-02-25 13:00:16 -0800666 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800667 sensorFound = true;
668 }
James Feist50fdfe32018-09-24 15:51:09 -0700669 }
670 if (!sensorFound)
671 {
672 continue;
673 }
James Feistf81f2882019-02-26 11:26:36 -0800674 struct conf::ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800675 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700676 info.inputs = std::move(inputs);
677
James Feist22c257a2018-08-31 14:07:12 -0700678 info.type = "stepwise";
679 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700680 info.stepwiseInfo.positiveHysteresis = 0.0;
681 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800682 std::string subtype = std::get<std::string>(base.at("Class"));
683
684 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700685 auto findPosHyst = base.find("PositiveHysteresis");
686 auto findNegHyst = base.find("NegativeHysteresis");
687 if (findPosHyst != base.end())
688 {
James Feist1f802f52019-02-08 13:51:43 -0800689 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800690 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700691 }
692 if (findNegHyst != base.end())
693 {
James Feist5782ab82019-04-02 08:38:48 -0700694 info.stepwiseInfo.negativeHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800695 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700696 }
James Feist22c257a2018-08-31 14:07:12 -0700697 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800698 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700699 if (readings.size() > ec::maxStepwisePoints)
700 {
701 throw std::invalid_argument("Too many stepwise points.");
702 }
703 if (readings.empty())
704 {
705 throw std::invalid_argument(
706 "Must have one stepwise point.");
707 }
708 std::copy(readings.begin(), readings.end(),
709 info.stepwiseInfo.reading);
710 if (readings.size() < ec::maxStepwisePoints)
711 {
712 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800713 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700714 }
715 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800716 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700717 if (readings.size() != outputs.size())
718 {
719 throw std::invalid_argument(
720 "Outputs size must match readings");
721 }
722 std::copy(outputs.begin(), outputs.end(),
723 info.stepwiseInfo.output);
724 if (outputs.size() < ec::maxStepwisePoints)
725 {
726 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800727 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700728 }
James Feist7136a5a2018-07-19 09:52:05 -0700729 }
730 }
731 }
James Feistf0096a02019-02-21 11:25:22 -0800732 if constexpr (DEBUG)
James Feist7136a5a2018-07-19 09:52:05 -0700733 {
734 debugPrint();
735 }
James Feistc959c422018-11-01 12:33:40 -0700736 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700737 {
James Feist1fe08952019-05-07 09:17:16 -0700738 std::cerr
739 << "No fan zones, application pausing until new configuration\n";
740 return false;
James Feist50fdfe32018-09-24 15:51:09 -0700741 }
James Feist1fe08952019-05-07 09:17:16 -0700742 return true;
James Feist7136a5a2018-07-19 09:52:05 -0700743}
744} // namespace dbus_configuration