blob: 06bcdcce8cbd5f46c33010fed520cf172557640a [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"
18#include "dbus/util.hpp"
19
Patrick Venture107a25d2018-10-13 14:08:09 -070020#include <algorithm>
James Feist64f072a2018-08-10 16:39:24 -070021#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070022#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070023#include <iostream>
James Feist1738e2a2019-02-04 15:57:03 -080024#include <regex>
James Feist7136a5a2018-07-19 09:52:05 -070025#include <sdbusplus/bus.hpp>
James Feist64f072a2018-08-10 16:39:24 -070026#include <sdbusplus/bus/match.hpp>
James Feist22c257a2018-08-31 14:07:12 -070027#include <sdbusplus/exception.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070028#include <set>
James Feist64f072a2018-08-10 16:39:24 -070029#include <thread>
James Feist7136a5a2018-07-19 09:52:05 -070030#include <unordered_map>
James Feist1f802f52019-02-08 13:51:43 -080031#include <variant>
James Feist7136a5a2018-07-19 09:52:05 -070032
33static constexpr bool DEBUG = false; // enable to print found configuration
34
Patrick Venture18b13112019-02-14 11:43:59 -080035extern std::map<std::string, struct SensorConfig> sensorConfig;
36extern std::map<int64_t, PIDConf> zoneConfig;
37extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig;
James Feist7136a5a2018-07-19 09:52:05 -070038
Patrick Venturee2ec0f62018-09-04 12:30:27 -070039constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070040 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070041constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070042 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070043constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070044 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070045constexpr const char* stepwiseConfigurationInterface =
46 "xyz.openbmc_project.Configuration.Stepwise";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070047constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
48constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070049
50namespace dbus_configuration
51{
52
James Feist1738e2a2019-02-04 15:57:03 -080053bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
54 const std::string& search,
55 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070056{
James Feist1738e2a2019-02-04 15:57:03 -080057 std::smatch match;
58 std::regex reg(search);
59 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070060 {
James Feist1738e2a2019-02-04 15:57:03 -080061 if (std::regex_search(sensor.first, match, reg))
62 {
63 matches.push_back(sensor);
64 }
James Feist7136a5a2018-07-19 09:52:05 -070065 }
Patrick Venture107a25d2018-10-13 14:08:09 -070066
James Feist1738e2a2019-02-04 15:57:03 -080067 return matches.size() > 0;
James Feist7136a5a2018-07-19 09:52:05 -070068}
69
70// this function prints the configuration into a form similar to the cpp
71// generated code to help in verification, should be turned off during normal
72// use
73void debugPrint(void)
74{
75 // print sensor config
76 std::cout << "sensor config:\n";
77 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070078 for (const auto& pair : sensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -070079 {
80
81 std::cout << "\t{" << pair.first << ",\n\t\t{";
82 std::cout << pair.second.type << ", ";
Patrick Venture69c51062019-02-11 09:46:03 -080083 std::cout << pair.second.readPath << ", ";
84 std::cout << pair.second.writePath << ", ";
James Feist7136a5a2018-07-19 09:52:05 -070085 std::cout << pair.second.min << ", ";
86 std::cout << pair.second.max << ", ";
87 std::cout << pair.second.timeout << "},\n\t},\n";
88 }
89 std::cout << "}\n\n";
90 std::cout << "ZoneDetailsConfig\n";
91 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070092 for (const auto& zone : zoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070093 {
94 std::cout << "\t{" << zone.first << ",\n";
Patrick Venture8e2fdb32019-02-11 09:39:59 -080095 std::cout << "\t\t{" << zone.second.minThermalRpm << ", ";
96 std::cout << zone.second.failsafePercent << "}\n\t},\n";
James Feist7136a5a2018-07-19 09:52:05 -070097 }
98 std::cout << "}\n\n";
99 std::cout << "ZoneConfig\n";
100 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700101 for (const auto& zone : zoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700102 {
103 std::cout << "\t{" << zone.first << "\n";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700104 for (const auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700105 {
106 std::cout << "\t\t{" << pidconf.first << ",\n";
107 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
108 std::cout << "\t\t\t{";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700109 for (const auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700110 {
111 std::cout << "\n\t\t\t" << input << ",\n";
112 }
113 std::cout << "\t\t\t}\n";
114 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700115 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
Patrick Venture7442c372019-02-11 10:21:05 -0800116 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
117 << ",\n";
118 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
119 << ",\n";
120 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
121 << ",\n";
122 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
123 << ",\n";
124 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
125 << "," << pidconf.second.pidInfo.integralLimit.max
126 << "},\n";
127 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
128 << pidconf.second.pidInfo.outLim.max << "},\n";
129 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
130 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700131 std::cout << "\t\t\t}\n\t\t}\n";
132 }
133 std::cout << "\t},\n";
134 }
135 std::cout << "}\n\n";
136}
137
James Feist50fdfe32018-09-24 15:51:09 -0700138int eventHandler(sd_bus_message*, void*, sd_bus_error*)
139{
140 // do a brief sleep as we tend to get a bunch of these events at
141 // once
142 std::this_thread::sleep_for(std::chrono::seconds(5));
143 std::cout << "New configuration detected, restarting\n.";
144 std::exit(EXIT_SUCCESS); // service file should make us restart
145 return 1;
146}
147
James Feistffd418b2018-11-15 14:46:36 -0800148size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
149{
150 auto it = std::find(zones.begin(), zones.end(), name);
151 if (it == zones.end())
152 {
153 zones.emplace_back(name);
154 it = zones.end() - 1;
155 }
156
157 return it - zones.begin();
158}
159
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700160void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700161{
James Feist22c257a2018-08-31 14:07:12 -0700162 using DbusVariantType =
James Feist1f802f52019-02-08 13:51:43 -0800163 std::variant<uint64_t, int64_t, double, std::string,
164 std::vector<std::string>, std::vector<double>>;
James Feist22c257a2018-08-31 14:07:12 -0700165
James Feist7136a5a2018-07-19 09:52:05 -0700166 using ManagedObjectType = std::unordered_map<
167 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700168 std::unordered_map<std::string,
169 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700170
James Feist50fdfe32018-09-24 15:51:09 -0700171 // restart on configuration properties changed
172 static sdbusplus::bus::match::match configMatch(
James Feist64f072a2018-08-10 16:39:24 -0700173 bus,
174 "type='signal',member='PropertiesChanged',arg0namespace='" +
175 std::string(pidConfigurationInterface) + "'",
176 eventHandler);
177
James Feist50fdfe32018-09-24 15:51:09 -0700178 // restart on sensors changed
179 static sdbusplus::bus::match::match sensorAdded(
180 bus,
181 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
182 "sensors/'",
183 eventHandler);
184
James Feist7136a5a2018-07-19 09:52:05 -0700185 auto mapper =
186 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
187 "/xyz/openbmc_project/object_mapper",
188 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700189 mapper.append("/", 0,
James Feist22c257a2018-08-31 14:07:12 -0700190 std::array<const char*, 6>{objectManagerInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700191 pidConfigurationInterface,
192 pidZoneConfigurationInterface,
James Feist22c257a2018-08-31 14:07:12 -0700193 stepwiseConfigurationInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700194 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700195 std::unordered_map<
196 std::string, std::unordered_map<std::string, std::vector<std::string>>>
197 respData;
James Feist22c257a2018-08-31 14:07:12 -0700198 try
199 {
200 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700201 resp.read(respData);
202 }
203 catch (sdbusplus::exception_t&)
204 {
205 // can't do anything without mapper call data
206 throw std::runtime_error("ObjectMapper Call Failure");
207 }
James Feist7136a5a2018-07-19 09:52:05 -0700208
James Feist7136a5a2018-07-19 09:52:05 -0700209 if (respData.empty())
210 {
James Feist22c257a2018-08-31 14:07:12 -0700211 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700212 throw std::runtime_error("No configuration data available from Mapper");
213 }
214 // create a map of pair of <has pid configuration, ObjectManager path>
215 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
216 // and a map of <path, interface> for sensors
217 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700218 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700219 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700220 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700221 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700222 auto& owner = owners[ownerPair.first];
223 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700224 {
225
226 if (interface == objectManagerInterface)
227 {
228 owner.second = objectPair.first;
229 }
230 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700231 interface == pidZoneConfigurationInterface ||
232 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700233 {
234 owner.first = true;
235 }
236 if (interface == sensorInterface || interface == pwmInterface)
237 {
238 // we're not interested in pwm sensors, just pwm control
239 if (interface == sensorInterface &&
240 objectPair.first.find("pwm") != std::string::npos)
241 {
242 continue;
243 }
244 sensors[objectPair.first] = interface;
245 }
246 }
247 }
248 }
249 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700250 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700251 {
252 // skip if no pid configuration (means probably a sensor)
253 if (!owner.second.first)
254 {
255 continue;
256 }
257 auto endpoint = bus.new_method_call(
258 owner.first.c_str(), owner.second.second.c_str(),
259 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700260 ManagedObjectType configuration;
261 try
James Feist7136a5a2018-07-19 09:52:05 -0700262 {
James Feist22c257a2018-08-31 14:07:12 -0700263 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700264 responce.read(configuration);
265 }
266 catch (sdbusplus::exception_t&)
267 {
268 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700269 throw std::runtime_error("Error getting managed objects from " +
270 owner.first);
271 }
James Feist22c257a2018-08-31 14:07:12 -0700272
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700273 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700274 {
275 if (pathPair.second.find(pidConfigurationInterface) !=
276 pathPair.second.end() ||
277 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700278 pathPair.second.end() ||
279 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700280 pathPair.second.end())
281 {
282 configurations.emplace(pathPair);
283 }
284 }
285 }
James Feist8c3c51e2018-08-08 16:31:43 -0700286
287 // on dbus having an index field is a bit strange, so randomly
288 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800289 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700290 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700291 {
292 auto findZone =
293 configuration.second.find(pidZoneConfigurationInterface);
294 if (findZone != configuration.second.end())
295 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700296 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800297
James Feist1f802f52019-02-08 13:51:43 -0800298 const std::string& name = std::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800299 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700300
Patrick Venturec54fbd82018-10-30 19:40:05 -0700301 auto& details = zoneDetailsConfig[index];
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800302 details.minThermalRpm =
James Feist1f802f52019-02-08 13:51:43 -0800303 std::visit(VariantToDoubleVisitor(), zone.at("MinThermalRpm"));
Patrick Venture8e2fdb32019-02-11 09:39:59 -0800304 details.failsafePercent = std::visit(VariantToDoubleVisitor(),
James Feist1f802f52019-02-08 13:51:43 -0800305 zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700306 }
307 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700308 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700309 {
James Feist8c3c51e2018-08-08 16:31:43 -0700310
James Feist22c257a2018-08-31 14:07:12 -0700311 const auto& base =
312 configuration.second.at(pidConfigurationInterface);
313 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800314 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700315 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700316 {
James Feistffd418b2018-11-15 14:46:36 -0800317 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700318 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700319
320 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800321 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700322 auto findOutputs =
323 base.find("Outputs"); // currently only fans have outputs
324 if (findOutputs != base.end())
325 {
326 std::vector<std::string> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800327 std::get<std::vector<std::string>>(findOutputs->second);
James Feist50fdfe32018-09-24 15:51:09 -0700328 sensorNames.insert(sensorNames.end(), outputs.begin(),
329 outputs.end());
330 }
James Feist1738e2a2019-02-04 15:57:03 -0800331
James Feist50fdfe32018-09-24 15:51:09 -0700332 std::vector<std::string> inputs;
James Feist1738e2a2019-02-04 15:57:03 -0800333 std::vector<std::pair<std::string, std::string>>
334 sensorInterfaces;
James Feist50fdfe32018-09-24 15:51:09 -0700335 for (const std::string& sensorName : sensorNames)
336 {
337 std::string name = sensorName;
338 // replace spaces with underscores to be legal on dbus
339 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800340 findSensors(sensors, name, sensorInterfaces);
341 }
James Feist50fdfe32018-09-24 15:51:09 -0700342
James Feist1738e2a2019-02-04 15:57:03 -0800343 // if the sensors aren't available in the current state, don't
344 // add them to the configuration.
345 if (sensorInterfaces.empty())
346 {
347 continue;
348 }
349 for (const auto& sensorPathIfacePair : sensorInterfaces)
350 {
351
James Feist50fdfe32018-09-24 15:51:09 -0700352 if (sensorPathIfacePair.second == sensorInterface)
353 {
James Feist1738e2a2019-02-04 15:57:03 -0800354 size_t idx =
355 sensorPathIfacePair.first.find_last_of("/") + 1;
356 std::string shortName =
357 sensorPathIfacePair.first.substr(idx);
358
359 inputs.push_back(shortName);
360 auto& config = sensorConfig[shortName];
James Feist1f802f52019-02-08 13:51:43 -0800361 config.type = std::get<std::string>(base.at("Class"));
Patrick Venture69c51062019-02-11 09:46:03 -0800362 config.readPath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700363 // todo: maybe un-hardcode this if we run into slower
364 // timeouts with sensors
365 if (config.type == "temp")
366 {
James Feist2642cb52019-02-25 13:00:16 -0800367 config.timeout = 0;
James Feist50fdfe32018-09-24 15:51:09 -0700368 }
James Feist75eb7692019-02-25 12:50:02 -0800369 else if (config.type == "fan")
370 {
371 config.max = conf::inheritValueFromDbus;
372 config.min = conf::inheritValueFromDbus;
373 }
James Feist50fdfe32018-09-24 15:51:09 -0700374 }
375 else if (sensorPathIfacePair.second == pwmInterface)
376 {
377 // copy so we can modify it
378 for (std::string otherSensor : sensorNames)
379 {
James Feist1738e2a2019-02-04 15:57:03 -0800380 std::replace(otherSensor.begin(), otherSensor.end(),
381 ' ', '_');
382 if (sensorPathIfacePair.first.find(otherSensor) !=
383 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700384 {
385 continue;
386 }
James Feist1738e2a2019-02-04 15:57:03 -0800387
Patrick Venturec54fbd82018-10-30 19:40:05 -0700388 auto& config = sensorConfig[otherSensor];
Patrick Venture69c51062019-02-11 09:46:03 -0800389 config.writePath = sensorPathIfacePair.first;
James Feist50fdfe32018-09-24 15:51:09 -0700390 // todo: un-hardcode this if there are fans with
391 // different ranges
392 config.max = 255;
393 config.min = 0;
394 }
395 }
396 }
James Feist1738e2a2019-02-04 15:57:03 -0800397
Patrick Venturef3252312018-10-30 08:42:53 -0700398 struct ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800399 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700400 info.inputs = std::move(inputs);
401
James Feist1f802f52019-02-08 13:51:43 -0800402 info.type = std::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700403 // todo: auto generation yaml -> c script seems to discard this
404 // value for fans, verify this is okay
405 if (info.type == "fan")
406 {
407 info.setpoint = 0;
408 }
409 else
410 {
James Feist1f802f52019-02-08 13:51:43 -0800411 info.setpoint = std::visit(VariantToDoubleVisitor(),
412 base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700413 }
414 info.pidInfo.ts = 1.0; // currently unused
Patrick Venture7442c372019-02-11 10:21:05 -0800415 info.pidInfo.proportionalCoeff = std::visit(
416 VariantToDoubleVisitor(), base.at("PCoefficient"));
417 info.pidInfo.integralCoeff = std::visit(
418 VariantToDoubleVisitor(), base.at("ICoefficient"));
419 info.pidInfo.feedFwdOffset = std::visit(
420 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
421 info.pidInfo.feedFwdGain = std::visit(
422 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
423 info.pidInfo.integralLimit.max =
James Feist1f802f52019-02-08 13:51:43 -0800424 std::visit(VariantToDoubleVisitor(), base.at("ILimitMax"));
Patrick Venture7442c372019-02-11 10:21:05 -0800425 info.pidInfo.integralLimit.min =
James Feist1f802f52019-02-08 13:51:43 -0800426 std::visit(VariantToDoubleVisitor(), base.at("ILimitMin"));
Patrick Venture7442c372019-02-11 10:21:05 -0800427 info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(),
428 base.at("OutLimitMax"));
429 info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(),
430 base.at("OutLimitMin"));
431 info.pidInfo.slewNeg =
James Feist1f802f52019-02-08 13:51:43 -0800432 std::visit(VariantToDoubleVisitor(), base.at("SlewNeg"));
Patrick Venture7442c372019-02-11 10:21:05 -0800433 info.pidInfo.slewPos =
James Feist1f802f52019-02-08 13:51:43 -0800434 std::visit(VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist572c43d2019-01-31 15:52:22 -0800435 double negativeHysteresis = 0;
436 double positiveHysteresis = 0;
437
438 auto findNeg = base.find("NegativeHysteresis");
439 auto findPos = base.find("PositiveHysteresis");
440 if (findNeg != base.end())
441 {
James Feist1f802f52019-02-08 13:51:43 -0800442 negativeHysteresis =
443 std::visit(VariantToDoubleVisitor(), findNeg->second);
James Feist572c43d2019-01-31 15:52:22 -0800444 }
445
446 if (findPos != base.end())
447 {
James Feist1f802f52019-02-08 13:51:43 -0800448 positiveHysteresis =
449 std::visit(VariantToDoubleVisitor(), findPos->second);
James Feist572c43d2019-01-31 15:52:22 -0800450 }
451 info.pidInfo.negativeHysteresis = negativeHysteresis;
452 info.pidInfo.positiveHysteresis = positiveHysteresis;
James Feist22c257a2018-08-31 14:07:12 -0700453 }
454 }
455 auto findStepwise =
456 configuration.second.find(stepwiseConfigurationInterface);
457 if (findStepwise != configuration.second.end())
458 {
459 const auto& base = findStepwise->second;
460 const std::vector<std::string>& zones =
James Feist1f802f52019-02-08 13:51:43 -0800461 std::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700462 for (const std::string& zone : zones)
463 {
James Feistffd418b2018-11-15 14:46:36 -0800464 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700465 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700466
467 std::vector<std::string> inputs;
468 std::vector<std::string> sensorNames =
James Feist1f802f52019-02-08 13:51:43 -0800469 std::get<std::vector<std::string>>(base.at("Inputs"));
James Feist50fdfe32018-09-24 15:51:09 -0700470
James Feist1738e2a2019-02-04 15:57:03 -0800471 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700472 for (const std::string& sensorName : sensorNames)
473 {
474 std::string name = sensorName;
475 // replace spaces with underscores to be legal on dbus
476 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800477 std::vector<std::pair<std::string, std::string>>
478 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700479
James Feist1738e2a2019-02-04 15:57:03 -0800480 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700481 {
James Feist50fdfe32018-09-24 15:51:09 -0700482 break;
483 }
484
James Feist1738e2a2019-02-04 15:57:03 -0800485 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
486 {
487 size_t idx =
488 sensorPathIfacePair.first.find_last_of("/") + 1;
489 std::string shortName =
490 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700491
James Feist1738e2a2019-02-04 15:57:03 -0800492 inputs.push_back(shortName);
493 auto& config = sensorConfig[shortName];
Patrick Venture69c51062019-02-11 09:46:03 -0800494 config.readPath = sensorPathIfacePair.first;
James Feist1738e2a2019-02-04 15:57:03 -0800495 config.type = "temp";
496 // todo: maybe un-hardcode this if we run into slower
497 // timeouts with sensors
498
James Feist2642cb52019-02-25 13:00:16 -0800499 config.timeout = 0;
James Feist1738e2a2019-02-04 15:57:03 -0800500 sensorFound = true;
501 }
James Feist50fdfe32018-09-24 15:51:09 -0700502 }
503 if (!sensorFound)
504 {
505 continue;
506 }
Patrick Venturef3252312018-10-30 08:42:53 -0700507 struct ControllerInfo& info =
James Feist1f802f52019-02-08 13:51:43 -0800508 conf[std::get<std::string>(base.at("Name"))];
James Feist50fdfe32018-09-24 15:51:09 -0700509 info.inputs = std::move(inputs);
510
James Feist22c257a2018-08-31 14:07:12 -0700511 info.type = "stepwise";
512 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700513 info.stepwiseInfo.positiveHysteresis = 0.0;
514 info.stepwiseInfo.negativeHysteresis = 0.0;
James Feist608304d2019-02-25 10:01:42 -0800515 std::string subtype = std::get<std::string>(base.at("Class"));
516
517 info.stepwiseInfo.isCeiling = (subtype == "Ceiling");
James Feist3dfaafd2018-09-20 15:46:58 -0700518 auto findPosHyst = base.find("PositiveHysteresis");
519 auto findNegHyst = base.find("NegativeHysteresis");
520 if (findPosHyst != base.end())
521 {
James Feist1f802f52019-02-08 13:51:43 -0800522 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800523 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700524 }
525 if (findNegHyst != base.end())
526 {
James Feist1f802f52019-02-08 13:51:43 -0800527 info.stepwiseInfo.positiveHysteresis = std::visit(
James Feist208abce2018-12-06 09:59:10 -0800528 VariantToDoubleVisitor(), findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700529 }
James Feist22c257a2018-08-31 14:07:12 -0700530 std::vector<double> readings =
James Feist1f802f52019-02-08 13:51:43 -0800531 std::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700532 if (readings.size() > ec::maxStepwisePoints)
533 {
534 throw std::invalid_argument("Too many stepwise points.");
535 }
536 if (readings.empty())
537 {
538 throw std::invalid_argument(
539 "Must have one stepwise point.");
540 }
541 std::copy(readings.begin(), readings.end(),
542 info.stepwiseInfo.reading);
543 if (readings.size() < ec::maxStepwisePoints)
544 {
545 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800546 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700547 }
548 std::vector<double> outputs =
James Feist1f802f52019-02-08 13:51:43 -0800549 std::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700550 if (readings.size() != outputs.size())
551 {
552 throw std::invalid_argument(
553 "Outputs size must match readings");
554 }
555 std::copy(outputs.begin(), outputs.end(),
556 info.stepwiseInfo.output);
557 if (outputs.size() < ec::maxStepwisePoints)
558 {
559 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800560 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700561 }
James Feist7136a5a2018-07-19 09:52:05 -0700562 }
563 }
564 }
565 if (DEBUG)
566 {
567 debugPrint();
568 }
James Feistc959c422018-11-01 12:33:40 -0700569 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700570 {
571 std::cerr << "No fan zones, application pausing until reboot\n";
572 while (1)
573 {
574 bus.process_discard();
James Feist65ea92e2018-10-26 16:21:37 -0700575 bus.wait();
James Feist50fdfe32018-09-24 15:51:09 -0700576 }
577 }
James Feist7136a5a2018-07-19 09:52:05 -0700578}
579} // namespace dbus_configuration