blob: 45f51b6be2a41a59b8560d90c8a8393b2212ed22 [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
James Feist64f072a2018-08-10 16:39:24 -070017#include <chrono>
James Feist7136a5a2018-07-19 09:52:05 -070018#include <conf.hpp>
19#include <dbus/util.hpp>
James Feist64f072a2018-08-10 16:39:24 -070020#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070021#include <iostream>
22#include <sdbusplus/bus.hpp>
James Feist64f072a2018-08-10 16:39:24 -070023#include <sdbusplus/bus/match.hpp>
James Feist22c257a2018-08-31 14:07:12 -070024#include <sdbusplus/exception.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070025#include <set>
James Feist64f072a2018-08-10 16:39:24 -070026#include <thread>
James Feist7136a5a2018-07-19 09:52:05 -070027#include <unordered_map>
28
29static constexpr bool DEBUG = false; // enable to print found configuration
30
31std::map<std::string, struct sensor> SensorConfig = {};
32std::map<int64_t, PIDConf> ZoneConfig = {};
33std::map<int64_t, struct zone> ZoneDetailsConfig = {};
34
Patrick Venturee2ec0f62018-09-04 12:30:27 -070035constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070036 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070037constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070038 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070039constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070040 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070041constexpr const char* stepwiseConfigurationInterface =
42 "xyz.openbmc_project.Configuration.Stepwise";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070043constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
44constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070045
46namespace dbus_configuration
47{
48
James Feist50fdfe32018-09-24 15:51:09 -070049namespace variant_ns = sdbusplus::message::variant_ns;
50
Patrick Venturee2ec0f62018-09-04 12:30:27 -070051bool findSensor(const std::unordered_map<std::string, std::string>& sensors,
52 const std::string& search,
53 std::pair<std::string, std::string>& sensor)
James Feist7136a5a2018-07-19 09:52:05 -070054{
Patrick Venturee2ec0f62018-09-04 12:30:27 -070055 for (const auto& s : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070056 {
57 if (s.first.find(search) != std::string::npos)
58 {
59 sensor = s;
60 return true;
61 }
62 }
63 return false;
64}
65
66// this function prints the configuration into a form similar to the cpp
67// generated code to help in verification, should be turned off during normal
68// use
69void debugPrint(void)
70{
71 // print sensor config
72 std::cout << "sensor config:\n";
73 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070074 for (auto& pair : SensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -070075 {
76
77 std::cout << "\t{" << pair.first << ",\n\t\t{";
78 std::cout << pair.second.type << ", ";
79 std::cout << pair.second.readpath << ", ";
80 std::cout << pair.second.writepath << ", ";
81 std::cout << pair.second.min << ", ";
82 std::cout << pair.second.max << ", ";
83 std::cout << pair.second.timeout << "},\n\t},\n";
84 }
85 std::cout << "}\n\n";
86 std::cout << "ZoneDetailsConfig\n";
87 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070088 for (auto& zone : ZoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070089 {
90 std::cout << "\t{" << zone.first << ",\n";
91 std::cout << "\t\t{" << zone.second.minthermalrpm << ", ";
92 std::cout << zone.second.failsafepercent << "}\n\t},\n";
93 }
94 std::cout << "}\n\n";
95 std::cout << "ZoneConfig\n";
96 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070097 for (auto& zone : ZoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -070098 {
99 std::cout << "\t{" << zone.first << "\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700100 for (auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700101 {
102 std::cout << "\t\t{" << pidconf.first << ",\n";
103 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
104 std::cout << "\t\t\t{";
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700105 for (auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700106 {
107 std::cout << "\n\t\t\t" << input << ",\n";
108 }
109 std::cout << "\t\t\t}\n";
110 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700111 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
112 std::cout << "\t\t\t" << pidconf.second.pidInfo.p_c << ",\n";
113 std::cout << "\t\t\t" << pidconf.second.pidInfo.i_c << ",\n";
114 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_off << ",\n";
115 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_gain << ",\n";
116 std::cout << "\t\t\t{" << pidconf.second.pidInfo.i_lim.min << ","
117 << pidconf.second.pidInfo.i_lim.max << "},\n";
118 std::cout << "\t\t\t{" << pidconf.second.pidInfo.out_lim.min << ","
119 << pidconf.second.pidInfo.out_lim.max << "},\n";
120 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_neg << ",\n";
121 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_pos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700122 std::cout << "\t\t\t}\n\t\t}\n";
123 }
124 std::cout << "\t},\n";
125 }
126 std::cout << "}\n\n";
127}
128
James Feist50fdfe32018-09-24 15:51:09 -0700129int eventHandler(sd_bus_message*, void*, sd_bus_error*)
130{
131 // do a brief sleep as we tend to get a bunch of these events at
132 // once
133 std::this_thread::sleep_for(std::chrono::seconds(5));
134 std::cout << "New configuration detected, restarting\n.";
135 std::exit(EXIT_SUCCESS); // service file should make us restart
136 return 1;
137}
138
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700139void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700140{
James Feist22c257a2018-08-31 14:07:12 -0700141 using DbusVariantType =
142 sdbusplus::message::variant<uint64_t, int64_t, double, std::string,
143 std::vector<std::string>,
144 std::vector<double>>;
145
James Feist7136a5a2018-07-19 09:52:05 -0700146 using ManagedObjectType = std::unordered_map<
147 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700148 std::unordered_map<std::string,
149 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700150
James Feist50fdfe32018-09-24 15:51:09 -0700151 // restart on configuration properties changed
152 static sdbusplus::bus::match::match configMatch(
James Feist64f072a2018-08-10 16:39:24 -0700153 bus,
154 "type='signal',member='PropertiesChanged',arg0namespace='" +
155 std::string(pidConfigurationInterface) + "'",
156 eventHandler);
157
James Feist50fdfe32018-09-24 15:51:09 -0700158 // restart on sensors changed
159 static sdbusplus::bus::match::match sensorAdded(
160 bus,
161 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
162 "sensors/'",
163 eventHandler);
164
James Feist7136a5a2018-07-19 09:52:05 -0700165 auto mapper =
166 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
167 "/xyz/openbmc_project/object_mapper",
168 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
169 mapper.append("", 0,
James Feist22c257a2018-08-31 14:07:12 -0700170 std::array<const char*, 6>{objectManagerInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700171 pidConfigurationInterface,
172 pidZoneConfigurationInterface,
James Feist22c257a2018-08-31 14:07:12 -0700173 stepwiseConfigurationInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700174 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700175 std::unordered_map<
176 std::string, std::unordered_map<std::string, std::vector<std::string>>>
177 respData;
James Feist22c257a2018-08-31 14:07:12 -0700178 try
179 {
180 auto resp = bus.call(mapper);
181 if (resp.is_method_error())
182 {
183 throw std::runtime_error("ObjectMapper Call Failure");
184 }
185 resp.read(respData);
186 }
187 catch (sdbusplus::exception_t&)
188 {
189 // can't do anything without mapper call data
190 throw std::runtime_error("ObjectMapper Call Failure");
191 }
James Feist7136a5a2018-07-19 09:52:05 -0700192
James Feist7136a5a2018-07-19 09:52:05 -0700193 if (respData.empty())
194 {
James Feist22c257a2018-08-31 14:07:12 -0700195 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700196 throw std::runtime_error("No configuration data available from Mapper");
197 }
198 // create a map of pair of <has pid configuration, ObjectManager path>
199 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
200 // and a map of <path, interface> for sensors
201 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700202 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700203 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700204 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700205 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700206 auto& owner = owners[ownerPair.first];
207 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700208 {
209
210 if (interface == objectManagerInterface)
211 {
212 owner.second = objectPair.first;
213 }
214 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700215 interface == pidZoneConfigurationInterface ||
216 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700217 {
218 owner.first = true;
219 }
220 if (interface == sensorInterface || interface == pwmInterface)
221 {
222 // we're not interested in pwm sensors, just pwm control
223 if (interface == sensorInterface &&
224 objectPair.first.find("pwm") != std::string::npos)
225 {
226 continue;
227 }
228 sensors[objectPair.first] = interface;
229 }
230 }
231 }
232 }
233 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700234 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700235 {
236 // skip if no pid configuration (means probably a sensor)
237 if (!owner.second.first)
238 {
239 continue;
240 }
241 auto endpoint = bus.new_method_call(
242 owner.first.c_str(), owner.second.second.c_str(),
243 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700244 ManagedObjectType configuration;
245 try
James Feist7136a5a2018-07-19 09:52:05 -0700246 {
James Feist22c257a2018-08-31 14:07:12 -0700247 auto responce = bus.call(endpoint);
248 if (responce.is_method_error())
249 {
250 throw std::runtime_error("Error getting managed objects from " +
251 owner.first);
252 }
253 responce.read(configuration);
254 }
255 catch (sdbusplus::exception_t&)
256 {
257 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700258 throw std::runtime_error("Error getting managed objects from " +
259 owner.first);
260 }
James Feist22c257a2018-08-31 14:07:12 -0700261
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700262 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700263 {
264 if (pathPair.second.find(pidConfigurationInterface) !=
265 pathPair.second.end() ||
266 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700267 pathPair.second.end() ||
268 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700269 pathPair.second.end())
270 {
271 configurations.emplace(pathPair);
272 }
273 }
274 }
James Feist8c3c51e2018-08-08 16:31:43 -0700275
276 // on dbus having an index field is a bit strange, so randomly
277 // assign index based on name property
278 std::vector<std::string> zoneIndex;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700279 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700280 {
281 auto findZone =
282 configuration.second.find(pidZoneConfigurationInterface);
283 if (findZone != configuration.second.end())
284 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700285 const auto& zone = findZone->second;
James Feist8c3c51e2018-08-08 16:31:43 -0700286 size_t index = 1;
287 const std::string& name =
James Feist50fdfe32018-09-24 15:51:09 -0700288 variant_ns::get<std::string>(zone.at("Name"));
James Feist8c3c51e2018-08-08 16:31:43 -0700289 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), name);
290 if (it == zoneIndex.end())
291 {
292 zoneIndex.emplace_back(name);
293 index = zoneIndex.size();
294 }
295 else
296 {
297 index = zoneIndex.end() - it;
298 }
299
300 auto& details = ZoneDetailsConfig[index];
James Feistd7a55bf2018-10-11 14:40:07 -0700301 details.minthermalrpm = variant_ns::apply_visitor(
James Feist7136a5a2018-07-19 09:52:05 -0700302 VariantToFloatVisitor(), zone.at("MinThermalRpm"));
James Feistd7a55bf2018-10-11 14:40:07 -0700303 details.failsafepercent = variant_ns::apply_visitor(
James Feist7136a5a2018-07-19 09:52:05 -0700304 VariantToFloatVisitor(), zone.at("FailSafePercent"));
305 }
306 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700307 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700308 {
James Feist8c3c51e2018-08-08 16:31:43 -0700309
James Feist22c257a2018-08-31 14:07:12 -0700310 const auto& base =
311 configuration.second.at(pidConfigurationInterface);
312 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700313 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700314 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700315 {
James Feist22c257a2018-08-31 14:07:12 -0700316 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
317 size_t index = 1;
318 if (it == zoneIndex.end())
James Feist7136a5a2018-07-19 09:52:05 -0700319 {
James Feist22c257a2018-08-31 14:07:12 -0700320 zoneIndex.emplace_back(zone);
321 index = zoneIndex.size();
James Feist7136a5a2018-07-19 09:52:05 -0700322 }
James Feist22c257a2018-08-31 14:07:12 -0700323 else
James Feist7136a5a2018-07-19 09:52:05 -0700324 {
James Feist22c257a2018-08-31 14:07:12 -0700325 index = zoneIndex.end() - it;
326 }
327 PIDConf& conf = ZoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700328
329 std::vector<std::string> sensorNames =
330 variant_ns::get<std::vector<std::string>>(
331 base.at("Inputs"));
332 auto findOutputs =
333 base.find("Outputs"); // currently only fans have outputs
334 if (findOutputs != base.end())
335 {
336 std::vector<std::string> outputs =
337 variant_ns::get<std::vector<std::string>>(
338 findOutputs->second);
339 sensorNames.insert(sensorNames.end(), outputs.begin(),
340 outputs.end());
341 }
342 bool sensorsAvailable = sensorNames.size();
343 std::vector<std::string> inputs;
344 for (const std::string& sensorName : sensorNames)
345 {
346 std::string name = sensorName;
347 // replace spaces with underscores to be legal on dbus
348 std::replace(name.begin(), name.end(), ' ', '_');
349 std::pair<std::string, std::string> sensorPathIfacePair;
350
351 if (!findSensor(sensors, name, sensorPathIfacePair))
352 {
353 sensorsAvailable = false;
354 break;
355 }
356 if (sensorPathIfacePair.second == sensorInterface)
357 {
358 inputs.push_back(name);
359 auto& config = SensorConfig[name];
360 config.type =
361 variant_ns::get<std::string>(base.at("Class"));
362 config.readpath = sensorPathIfacePair.first;
363 // todo: maybe un-hardcode this if we run into slower
364 // timeouts with sensors
365 if (config.type == "temp")
366 {
367 config.timeout = 500;
368 }
369 }
370 else if (sensorPathIfacePair.second == pwmInterface)
371 {
372 // copy so we can modify it
373 for (std::string otherSensor : sensorNames)
374 {
375 if (otherSensor == sensorName)
376 {
377 continue;
378 }
379 std::replace(otherSensor.begin(), otherSensor.end(),
380 ' ', '_');
381 auto& config = SensorConfig[otherSensor];
382 config.writepath = sensorPathIfacePair.first;
383 // todo: un-hardcode this if there are fans with
384 // different ranges
385 config.max = 255;
386 config.min = 0;
387 }
388 }
389 }
390 // if the sensors aren't available in the current state, don't
391 // add them to the configuration.
392 if (!sensorsAvailable)
393 {
394 continue;
395 }
James Feist22c257a2018-08-31 14:07:12 -0700396 struct controller_info& info =
James Feist50fdfe32018-09-24 15:51:09 -0700397 conf[variant_ns::get<std::string>(base.at("Name"))];
398 info.inputs = std::move(inputs);
399
400 info.type = variant_ns::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700401 // todo: auto generation yaml -> c script seems to discard this
402 // value for fans, verify this is okay
403 if (info.type == "fan")
404 {
405 info.setpoint = 0;
406 }
407 else
408 {
James Feistd7a55bf2018-10-11 14:40:07 -0700409 info.setpoint = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700410 VariantToFloatVisitor(), base.at("SetPoint"));
411 }
412 info.pidInfo.ts = 1.0; // currently unused
James Feistd7a55bf2018-10-11 14:40:07 -0700413 info.pidInfo.p_c = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700414 VariantToFloatVisitor(), base.at("PCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700415 info.pidInfo.i_c = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700416 VariantToFloatVisitor(), base.at("ICoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700417 info.pidInfo.ff_off = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700418 VariantToFloatVisitor(), base.at("FFOffCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700419 info.pidInfo.ff_gain = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700420 VariantToFloatVisitor(), base.at("FFGainCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700421 info.pidInfo.i_lim.max = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700422 VariantToFloatVisitor(), base.at("ILimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700423 info.pidInfo.i_lim.min = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700424 VariantToFloatVisitor(), base.at("ILimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700425 info.pidInfo.out_lim.max = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700426 VariantToFloatVisitor(), base.at("OutLimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700427 info.pidInfo.out_lim.min = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700428 VariantToFloatVisitor(), base.at("OutLimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700429 info.pidInfo.slew_neg = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700430 VariantToFloatVisitor(), base.at("SlewNeg"));
James Feistd7a55bf2018-10-11 14:40:07 -0700431 info.pidInfo.slew_pos = variant_ns::apply_visitor(
James Feist22c257a2018-08-31 14:07:12 -0700432 VariantToFloatVisitor(), base.at("SlewPos"));
James Feist22c257a2018-08-31 14:07:12 -0700433 }
434 }
435 auto findStepwise =
436 configuration.second.find(stepwiseConfigurationInterface);
437 if (findStepwise != configuration.second.end())
438 {
439 const auto& base = findStepwise->second;
440 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700441 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700442 for (const std::string& zone : zones)
443 {
444 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
445 size_t index = 1;
446 if (it == zoneIndex.end())
447 {
448 zoneIndex.emplace_back(zone);
449 index = zoneIndex.size();
450 }
451 else
452 {
453 index = zoneIndex.end() - it;
454 }
455 PIDConf& conf = ZoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700456
457 std::vector<std::string> inputs;
458 std::vector<std::string> sensorNames =
459 variant_ns::get<std::vector<std::string>>(
460 base.at("Inputs"));
461
462 bool sensorFound = sensorNames.size();
463 for (const std::string& sensorName : sensorNames)
464 {
465 std::string name = sensorName;
466 // replace spaces with underscores to be legal on dbus
467 std::replace(name.begin(), name.end(), ' ', '_');
468 std::pair<std::string, std::string> sensorPathIfacePair;
469
470 if (!findSensor(sensors, name, sensorPathIfacePair))
471 {
472 sensorFound = false;
473 break;
474 }
475
476 inputs.push_back(name);
477 auto& config = SensorConfig[name];
478 config.readpath = sensorPathIfacePair.first;
479 config.type = "temp";
480 // todo: maybe un-hardcode this if we run into slower
481 // timeouts with sensors
482
483 config.timeout = 500;
484 }
485 if (!sensorFound)
486 {
487 continue;
488 }
James Feist22c257a2018-08-31 14:07:12 -0700489 struct controller_info& info =
James Feist50fdfe32018-09-24 15:51:09 -0700490 conf[variant_ns::get<std::string>(base.at("Name"))];
491 info.inputs = std::move(inputs);
492
James Feist22c257a2018-08-31 14:07:12 -0700493 info.type = "stepwise";
494 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700495 info.stepwiseInfo.positiveHysteresis = 0.0;
496 info.stepwiseInfo.negativeHysteresis = 0.0;
497 auto findPosHyst = base.find("PositiveHysteresis");
498 auto findNegHyst = base.find("NegativeHysteresis");
499 if (findPosHyst != base.end())
500 {
501 info.stepwiseInfo.positiveHysteresis =
James Feistd7a55bf2018-10-11 14:40:07 -0700502 variant_ns::apply_visitor(VariantToFloatVisitor(),
503 findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700504 }
505 if (findNegHyst != base.end())
506 {
507 info.stepwiseInfo.positiveHysteresis =
James Feistd7a55bf2018-10-11 14:40:07 -0700508 variant_ns::apply_visitor(VariantToFloatVisitor(),
509 findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700510 }
James Feist22c257a2018-08-31 14:07:12 -0700511 std::vector<double> readings =
James Feist50fdfe32018-09-24 15:51:09 -0700512 variant_ns::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700513 if (readings.size() > ec::maxStepwisePoints)
514 {
515 throw std::invalid_argument("Too many stepwise points.");
516 }
517 if (readings.empty())
518 {
519 throw std::invalid_argument(
520 "Must have one stepwise point.");
521 }
522 std::copy(readings.begin(), readings.end(),
523 info.stepwiseInfo.reading);
524 if (readings.size() < ec::maxStepwisePoints)
525 {
526 info.stepwiseInfo.reading[readings.size()] =
527 std::numeric_limits<float>::quiet_NaN();
528 }
529 std::vector<double> outputs =
James Feist50fdfe32018-09-24 15:51:09 -0700530 variant_ns::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700531 if (readings.size() != outputs.size())
532 {
533 throw std::invalid_argument(
534 "Outputs size must match readings");
535 }
536 std::copy(outputs.begin(), outputs.end(),
537 info.stepwiseInfo.output);
538 if (outputs.size() < ec::maxStepwisePoints)
539 {
540 info.stepwiseInfo.output[outputs.size()] =
541 std::numeric_limits<float>::quiet_NaN();
542 }
James Feist7136a5a2018-07-19 09:52:05 -0700543 }
544 }
545 }
546 if (DEBUG)
547 {
548 debugPrint();
549 }
James Feist50fdfe32018-09-24 15:51:09 -0700550 if (ZoneConfig.empty())
551 {
552 std::cerr << "No fan zones, application pausing until reboot\n";
553 while (1)
554 {
555 bus.process_discard();
556 }
557 }
James Feist7136a5a2018-07-19 09:52:05 -0700558}
559} // namespace dbus_configuration