blob: be372af2507e3166a1dce4f8abca4a07ca8913db [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>
24#include <sdbusplus/bus.hpp>
James Feist64f072a2018-08-10 16:39:24 -070025#include <sdbusplus/bus/match.hpp>
James Feist22c257a2018-08-31 14:07:12 -070026#include <sdbusplus/exception.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070027#include <set>
James Feist64f072a2018-08-10 16:39:24 -070028#include <thread>
James Feist7136a5a2018-07-19 09:52:05 -070029#include <unordered_map>
30
31static constexpr bool DEBUG = false; // enable to print found configuration
32
33std::map<std::string, struct sensor> SensorConfig = {};
34std::map<int64_t, PIDConf> ZoneConfig = {};
35std::map<int64_t, struct zone> ZoneDetailsConfig = {};
36
Patrick Venturee2ec0f62018-09-04 12:30:27 -070037constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070038 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070039constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070040 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070041constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070042 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070043constexpr const char* stepwiseConfigurationInterface =
44 "xyz.openbmc_project.Configuration.Stepwise";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070045constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
46constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070047
48namespace dbus_configuration
49{
50
James Feist50fdfe32018-09-24 15:51:09 -070051namespace variant_ns = sdbusplus::message::variant_ns;
52
Patrick Venturee2ec0f62018-09-04 12:30:27 -070053bool findSensor(const std::unordered_map<std::string, std::string>& sensors,
54 const std::string& search,
55 std::pair<std::string, std::string>& sensor)
James Feist7136a5a2018-07-19 09:52:05 -070056{
Patrick Venture107a25d2018-10-13 14:08:09 -070057 auto found =
58 std::find_if(sensors.begin(), sensors.end(), [&search](const auto& s) {
59 return (s.first.find(search) != std::string::npos);
60 });
61 if (found != sensors.end())
James Feist7136a5a2018-07-19 09:52:05 -070062 {
Patrick Venture107a25d2018-10-13 14:08:09 -070063 sensor = *found;
64 return true;
James Feist7136a5a2018-07-19 09:52:05 -070065 }
Patrick Venture107a25d2018-10-13 14:08:09 -070066
James Feist7136a5a2018-07-19 09:52:05 -070067 return false;
68}
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 Venturee2ec0f62018-09-04 12:30:27 -070078 for (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 << ", ";
83 std::cout << pair.second.readpath << ", ";
84 std::cout << pair.second.writepath << ", ";
85 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 Venturee2ec0f62018-09-04 12:30:27 -070092 for (auto& zone : ZoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070093 {
94 std::cout << "\t{" << zone.first << ",\n";
95 std::cout << "\t\t{" << zone.second.minthermalrpm << ", ";
96 std::cout << zone.second.failsafepercent << "}\n\t},\n";
97 }
98 std::cout << "}\n\n";
99 std::cout << "ZoneConfig\n";
100 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700101 for (auto& zone : ZoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700102 {
103 std::cout << "\t{" << zone.first << "\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700104 for (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 Venturee2ec0f62018-09-04 12:30:27 -0700109 for (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";
116 std::cout << "\t\t\t" << pidconf.second.pidInfo.p_c << ",\n";
117 std::cout << "\t\t\t" << pidconf.second.pidInfo.i_c << ",\n";
118 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_off << ",\n";
119 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_gain << ",\n";
120 std::cout << "\t\t\t{" << pidconf.second.pidInfo.i_lim.min << ","
121 << pidconf.second.pidInfo.i_lim.max << "},\n";
122 std::cout << "\t\t\t{" << pidconf.second.pidInfo.out_lim.min << ","
123 << pidconf.second.pidInfo.out_lim.max << "},\n";
124 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_neg << ",\n";
125 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_pos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700126 std::cout << "\t\t\t}\n\t\t}\n";
127 }
128 std::cout << "\t},\n";
129 }
130 std::cout << "}\n\n";
131}
132
James Feist50fdfe32018-09-24 15:51:09 -0700133int eventHandler(sd_bus_message*, void*, sd_bus_error*)
134{
135 // do a brief sleep as we tend to get a bunch of these events at
136 // once
137 std::this_thread::sleep_for(std::chrono::seconds(5));
138 std::cout << "New configuration detected, restarting\n.";
139 std::exit(EXIT_SUCCESS); // service file should make us restart
140 return 1;
141}
142
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700143void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700144{
James Feist22c257a2018-08-31 14:07:12 -0700145 using DbusVariantType =
146 sdbusplus::message::variant<uint64_t, int64_t, double, std::string,
147 std::vector<std::string>,
148 std::vector<double>>;
149
James Feist7136a5a2018-07-19 09:52:05 -0700150 using ManagedObjectType = std::unordered_map<
151 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700152 std::unordered_map<std::string,
153 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700154
James Feist50fdfe32018-09-24 15:51:09 -0700155 // restart on configuration properties changed
156 static sdbusplus::bus::match::match configMatch(
James Feist64f072a2018-08-10 16:39:24 -0700157 bus,
158 "type='signal',member='PropertiesChanged',arg0namespace='" +
159 std::string(pidConfigurationInterface) + "'",
160 eventHandler);
161
James Feist50fdfe32018-09-24 15:51:09 -0700162 // restart on sensors changed
163 static sdbusplus::bus::match::match sensorAdded(
164 bus,
165 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
166 "sensors/'",
167 eventHandler);
168
James Feist7136a5a2018-07-19 09:52:05 -0700169 auto mapper =
170 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
171 "/xyz/openbmc_project/object_mapper",
172 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
173 mapper.append("", 0,
James Feist22c257a2018-08-31 14:07:12 -0700174 std::array<const char*, 6>{objectManagerInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700175 pidConfigurationInterface,
176 pidZoneConfigurationInterface,
James Feist22c257a2018-08-31 14:07:12 -0700177 stepwiseConfigurationInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700178 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700179 std::unordered_map<
180 std::string, std::unordered_map<std::string, std::vector<std::string>>>
181 respData;
James Feist22c257a2018-08-31 14:07:12 -0700182 try
183 {
184 auto resp = bus.call(mapper);
185 if (resp.is_method_error())
186 {
187 throw std::runtime_error("ObjectMapper Call Failure");
188 }
189 resp.read(respData);
190 }
191 catch (sdbusplus::exception_t&)
192 {
193 // can't do anything without mapper call data
194 throw std::runtime_error("ObjectMapper Call Failure");
195 }
James Feist7136a5a2018-07-19 09:52:05 -0700196
James Feist7136a5a2018-07-19 09:52:05 -0700197 if (respData.empty())
198 {
James Feist22c257a2018-08-31 14:07:12 -0700199 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700200 throw std::runtime_error("No configuration data available from Mapper");
201 }
202 // create a map of pair of <has pid configuration, ObjectManager path>
203 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
204 // and a map of <path, interface> for sensors
205 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700206 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700207 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700208 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700209 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700210 auto& owner = owners[ownerPair.first];
211 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700212 {
213
214 if (interface == objectManagerInterface)
215 {
216 owner.second = objectPair.first;
217 }
218 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700219 interface == pidZoneConfigurationInterface ||
220 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700221 {
222 owner.first = true;
223 }
224 if (interface == sensorInterface || interface == pwmInterface)
225 {
226 // we're not interested in pwm sensors, just pwm control
227 if (interface == sensorInterface &&
228 objectPair.first.find("pwm") != std::string::npos)
229 {
230 continue;
231 }
232 sensors[objectPair.first] = interface;
233 }
234 }
235 }
236 }
237 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700238 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700239 {
240 // skip if no pid configuration (means probably a sensor)
241 if (!owner.second.first)
242 {
243 continue;
244 }
245 auto endpoint = bus.new_method_call(
246 owner.first.c_str(), owner.second.second.c_str(),
247 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700248 ManagedObjectType configuration;
249 try
James Feist7136a5a2018-07-19 09:52:05 -0700250 {
James Feist22c257a2018-08-31 14:07:12 -0700251 auto responce = bus.call(endpoint);
252 if (responce.is_method_error())
253 {
254 throw std::runtime_error("Error getting managed objects from " +
255 owner.first);
256 }
257 responce.read(configuration);
258 }
259 catch (sdbusplus::exception_t&)
260 {
261 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700262 throw std::runtime_error("Error getting managed objects from " +
263 owner.first);
264 }
James Feist22c257a2018-08-31 14:07:12 -0700265
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700266 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700267 {
268 if (pathPair.second.find(pidConfigurationInterface) !=
269 pathPair.second.end() ||
270 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700271 pathPair.second.end() ||
272 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700273 pathPair.second.end())
274 {
275 configurations.emplace(pathPair);
276 }
277 }
278 }
James Feist8c3c51e2018-08-08 16:31:43 -0700279
280 // on dbus having an index field is a bit strange, so randomly
281 // assign index based on name property
282 std::vector<std::string> zoneIndex;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700283 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700284 {
285 auto findZone =
286 configuration.second.find(pidZoneConfigurationInterface);
287 if (findZone != configuration.second.end())
288 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700289 const auto& zone = findZone->second;
James Feist8c3c51e2018-08-08 16:31:43 -0700290 size_t index = 1;
291 const std::string& name =
James Feist50fdfe32018-09-24 15:51:09 -0700292 variant_ns::get<std::string>(zone.at("Name"));
James Feist8c3c51e2018-08-08 16:31:43 -0700293 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), name);
294 if (it == zoneIndex.end())
295 {
296 zoneIndex.emplace_back(name);
297 index = zoneIndex.size();
298 }
299 else
300 {
301 index = zoneIndex.end() - it;
302 }
303
304 auto& details = ZoneDetailsConfig[index];
James Feistd7a55bf2018-10-11 14:40:07 -0700305 details.minthermalrpm = variant_ns::apply_visitor(
James Feist7136a5a2018-07-19 09:52:05 -0700306 VariantToFloatVisitor(), zone.at("MinThermalRpm"));
James Feistd7a55bf2018-10-11 14:40:07 -0700307 details.failsafepercent = variant_ns::apply_visitor(
James Feist7136a5a2018-07-19 09:52:05 -0700308 VariantToFloatVisitor(), zone.at("FailSafePercent"));
309 }
310 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700311 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700312 {
James Feist8c3c51e2018-08-08 16:31:43 -0700313
James Feist22c257a2018-08-31 14:07:12 -0700314 const auto& base =
315 configuration.second.at(pidConfigurationInterface);
316 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700317 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700318 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700319 {
James Feist22c257a2018-08-31 14:07:12 -0700320 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
321 size_t index = 1;
322 if (it == zoneIndex.end())
James Feist7136a5a2018-07-19 09:52:05 -0700323 {
James Feist22c257a2018-08-31 14:07:12 -0700324 zoneIndex.emplace_back(zone);
325 index = zoneIndex.size();
James Feist7136a5a2018-07-19 09:52:05 -0700326 }
James Feist22c257a2018-08-31 14:07:12 -0700327 else
James Feist7136a5a2018-07-19 09:52:05 -0700328 {
James Feist22c257a2018-08-31 14:07:12 -0700329 index = zoneIndex.end() - it;
330 }
331 PIDConf& conf = ZoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700332
333 std::vector<std::string> sensorNames =
334 variant_ns::get<std::vector<std::string>>(
335 base.at("Inputs"));
336 auto findOutputs =
337 base.find("Outputs"); // currently only fans have outputs
338 if (findOutputs != base.end())
339 {
340 std::vector<std::string> outputs =
341 variant_ns::get<std::vector<std::string>>(
342 findOutputs->second);
343 sensorNames.insert(sensorNames.end(), outputs.begin(),
344 outputs.end());
345 }
346 bool sensorsAvailable = sensorNames.size();
347 std::vector<std::string> inputs;
348 for (const std::string& sensorName : sensorNames)
349 {
350 std::string name = sensorName;
351 // replace spaces with underscores to be legal on dbus
352 std::replace(name.begin(), name.end(), ' ', '_');
353 std::pair<std::string, std::string> sensorPathIfacePair;
354
355 if (!findSensor(sensors, name, sensorPathIfacePair))
356 {
357 sensorsAvailable = false;
358 break;
359 }
360 if (sensorPathIfacePair.second == sensorInterface)
361 {
362 inputs.push_back(name);
363 auto& config = SensorConfig[name];
364 config.type =
365 variant_ns::get<std::string>(base.at("Class"));
366 config.readpath = sensorPathIfacePair.first;
367 // todo: maybe un-hardcode this if we run into slower
368 // timeouts with sensors
369 if (config.type == "temp")
370 {
371 config.timeout = 500;
372 }
373 }
374 else if (sensorPathIfacePair.second == pwmInterface)
375 {
376 // copy so we can modify it
377 for (std::string otherSensor : sensorNames)
378 {
379 if (otherSensor == sensorName)
380 {
381 continue;
382 }
383 std::replace(otherSensor.begin(), otherSensor.end(),
384 ' ', '_');
385 auto& config = SensorConfig[otherSensor];
386 config.writepath = sensorPathIfacePair.first;
387 // todo: un-hardcode this if there are fans with
388 // different ranges
389 config.max = 255;
390 config.min = 0;
391 }
392 }
393 }
394 // if the sensors aren't available in the current state, don't
395 // add them to the configuration.
396 if (!sensorsAvailable)
397 {
398 continue;
399 }
James Feist22c257a2018-08-31 14:07:12 -0700400 struct controller_info& info =
James Feist50fdfe32018-09-24 15:51:09 -0700401 conf[variant_ns::get<std::string>(base.at("Name"))];
402 info.inputs = std::move(inputs);
403
404 info.type = variant_ns::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700405 // todo: auto generation yaml -> c script seems to discard this
406 // value for fans, verify this is okay
407 if (info.type == "fan")
408 {
409 info.setpoint = 0;
410 }
411 else
412 {
James Feistd7a55bf2018-10-11 14:40:07 -0700413 info.setpoint = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700414 VariantToFloatVisitor(), base.at("SetPoint"));
415 }
416 info.pidInfo.ts = 1.0; // currently unused
James Feistd7a55bf2018-10-11 14:40:07 -0700417 info.pidInfo.p_c = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700418 VariantToFloatVisitor(), base.at("PCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700419 info.pidInfo.i_c = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700420 VariantToFloatVisitor(), base.at("ICoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700421 info.pidInfo.ff_off = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700422 VariantToFloatVisitor(), base.at("FFOffCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700423 info.pidInfo.ff_gain = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700424 VariantToFloatVisitor(), base.at("FFGainCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700425 info.pidInfo.i_lim.max = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700426 VariantToFloatVisitor(), base.at("ILimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700427 info.pidInfo.i_lim.min = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700428 VariantToFloatVisitor(), base.at("ILimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700429 info.pidInfo.out_lim.max = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700430 VariantToFloatVisitor(), base.at("OutLimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700431 info.pidInfo.out_lim.min = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700432 VariantToFloatVisitor(), base.at("OutLimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700433 info.pidInfo.slew_neg = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700434 VariantToFloatVisitor(), base.at("SlewNeg"));
James Feistd7a55bf2018-10-11 14:40:07 -0700435 info.pidInfo.slew_pos = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700436 VariantToFloatVisitor(), base.at("SlewPos"));
James Feist22c257a2018-08-31 14:07:12 -0700437 }
438 }
439 auto findStepwise =
440 configuration.second.find(stepwiseConfigurationInterface);
441 if (findStepwise != configuration.second.end())
442 {
443 const auto& base = findStepwise->second;
444 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700445 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700446 for (const std::string& zone : zones)
447 {
448 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
449 size_t index = 1;
450 if (it == zoneIndex.end())
451 {
452 zoneIndex.emplace_back(zone);
453 index = zoneIndex.size();
454 }
455 else
456 {
457 index = zoneIndex.end() - it;
458 }
459 PIDConf& conf = ZoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700460
461 std::vector<std::string> inputs;
462 std::vector<std::string> sensorNames =
463 variant_ns::get<std::vector<std::string>>(
464 base.at("Inputs"));
465
466 bool sensorFound = sensorNames.size();
467 for (const std::string& sensorName : sensorNames)
468 {
469 std::string name = sensorName;
470 // replace spaces with underscores to be legal on dbus
471 std::replace(name.begin(), name.end(), ' ', '_');
472 std::pair<std::string, std::string> sensorPathIfacePair;
473
474 if (!findSensor(sensors, name, sensorPathIfacePair))
475 {
476 sensorFound = false;
477 break;
478 }
479
480 inputs.push_back(name);
481 auto& config = SensorConfig[name];
482 config.readpath = sensorPathIfacePair.first;
483 config.type = "temp";
484 // todo: maybe un-hardcode this if we run into slower
485 // timeouts with sensors
486
487 config.timeout = 500;
488 }
489 if (!sensorFound)
490 {
491 continue;
492 }
James Feist22c257a2018-08-31 14:07:12 -0700493 struct controller_info& info =
James Feist50fdfe32018-09-24 15:51:09 -0700494 conf[variant_ns::get<std::string>(base.at("Name"))];
495 info.inputs = std::move(inputs);
496
James Feist22c257a2018-08-31 14:07:12 -0700497 info.type = "stepwise";
498 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700499 info.stepwiseInfo.positiveHysteresis = 0.0;
500 info.stepwiseInfo.negativeHysteresis = 0.0;
501 auto findPosHyst = base.find("PositiveHysteresis");
502 auto findNegHyst = base.find("NegativeHysteresis");
503 if (findPosHyst != base.end())
504 {
505 info.stepwiseInfo.positiveHysteresis =
James Feistd7a55bf2018-10-11 14:40:07 -0700506 variant_ns::apply_visitor(VariantToFloatVisitor(),
507 findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700508 }
509 if (findNegHyst != base.end())
510 {
511 info.stepwiseInfo.positiveHysteresis =
James Feistd7a55bf2018-10-11 14:40:07 -0700512 variant_ns::apply_visitor(VariantToFloatVisitor(),
513 findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700514 }
James Feist22c257a2018-08-31 14:07:12 -0700515 std::vector<double> readings =
James Feist50fdfe32018-09-24 15:51:09 -0700516 variant_ns::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700517 if (readings.size() > ec::maxStepwisePoints)
518 {
519 throw std::invalid_argument("Too many stepwise points.");
520 }
521 if (readings.empty())
522 {
523 throw std::invalid_argument(
524 "Must have one stepwise point.");
525 }
526 std::copy(readings.begin(), readings.end(),
527 info.stepwiseInfo.reading);
528 if (readings.size() < ec::maxStepwisePoints)
529 {
530 info.stepwiseInfo.reading[readings.size()] =
531 std::numeric_limits<float>::quiet_NaN();
532 }
533 std::vector<double> outputs =
James Feist50fdfe32018-09-24 15:51:09 -0700534 variant_ns::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700535 if (readings.size() != outputs.size())
536 {
537 throw std::invalid_argument(
538 "Outputs size must match readings");
539 }
540 std::copy(outputs.begin(), outputs.end(),
541 info.stepwiseInfo.output);
542 if (outputs.size() < ec::maxStepwisePoints)
543 {
544 info.stepwiseInfo.output[outputs.size()] =
545 std::numeric_limits<float>::quiet_NaN();
546 }
James Feist7136a5a2018-07-19 09:52:05 -0700547 }
548 }
549 }
550 if (DEBUG)
551 {
552 debugPrint();
553 }
James Feist50fdfe32018-09-24 15:51:09 -0700554 if (ZoneConfig.empty())
555 {
556 std::cerr << "No fan zones, application pausing until reboot\n";
557 while (1)
558 {
559 bus.process_discard();
560 }
561 }
James Feist7136a5a2018-07-19 09:52:05 -0700562}
563} // namespace dbus_configuration