blob: 31742f5f07c672ce40f108d0196d446ad26969a6 [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
Patrick Venturec54fbd82018-10-30 19:40:05 -070033std::map<std::string, struct SensorConfig> sensorConfig = {};
34std::map<int64_t, PIDConf> zoneConfig = {};
35std::map<int64_t, struct ZoneConfig> zoneDetailsConfig = {};
James Feist7136a5a2018-07-19 09:52:05 -070036
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 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 << ", ";
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 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";
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 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";
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
James Feistffd418b2018-11-15 14:46:36 -0800143size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
144{
145 auto it = std::find(zones.begin(), zones.end(), name);
146 if (it == zones.end())
147 {
148 zones.emplace_back(name);
149 it = zones.end() - 1;
150 }
151
152 return it - zones.begin();
153}
154
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700155void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700156{
James Feist22c257a2018-08-31 14:07:12 -0700157 using DbusVariantType =
158 sdbusplus::message::variant<uint64_t, int64_t, double, std::string,
159 std::vector<std::string>,
160 std::vector<double>>;
161
James Feist7136a5a2018-07-19 09:52:05 -0700162 using ManagedObjectType = std::unordered_map<
163 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700164 std::unordered_map<std::string,
165 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700166
James Feist50fdfe32018-09-24 15:51:09 -0700167 // restart on configuration properties changed
168 static sdbusplus::bus::match::match configMatch(
James Feist64f072a2018-08-10 16:39:24 -0700169 bus,
170 "type='signal',member='PropertiesChanged',arg0namespace='" +
171 std::string(pidConfigurationInterface) + "'",
172 eventHandler);
173
James Feist50fdfe32018-09-24 15:51:09 -0700174 // restart on sensors changed
175 static sdbusplus::bus::match::match sensorAdded(
176 bus,
177 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
178 "sensors/'",
179 eventHandler);
180
James Feist7136a5a2018-07-19 09:52:05 -0700181 auto mapper =
182 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
183 "/xyz/openbmc_project/object_mapper",
184 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700185 mapper.append("/", 0,
James Feist22c257a2018-08-31 14:07:12 -0700186 std::array<const char*, 6>{objectManagerInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700187 pidConfigurationInterface,
188 pidZoneConfigurationInterface,
James Feist22c257a2018-08-31 14:07:12 -0700189 stepwiseConfigurationInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700190 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700191 std::unordered_map<
192 std::string, std::unordered_map<std::string, std::vector<std::string>>>
193 respData;
James Feist22c257a2018-08-31 14:07:12 -0700194 try
195 {
196 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700197 resp.read(respData);
198 }
199 catch (sdbusplus::exception_t&)
200 {
201 // can't do anything without mapper call data
202 throw std::runtime_error("ObjectMapper Call Failure");
203 }
James Feist7136a5a2018-07-19 09:52:05 -0700204
James Feist7136a5a2018-07-19 09:52:05 -0700205 if (respData.empty())
206 {
James Feist22c257a2018-08-31 14:07:12 -0700207 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700208 throw std::runtime_error("No configuration data available from Mapper");
209 }
210 // create a map of pair of <has pid configuration, ObjectManager path>
211 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
212 // and a map of <path, interface> for sensors
213 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700214 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700215 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700216 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700217 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700218 auto& owner = owners[ownerPair.first];
219 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700220 {
221
222 if (interface == objectManagerInterface)
223 {
224 owner.second = objectPair.first;
225 }
226 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700227 interface == pidZoneConfigurationInterface ||
228 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700229 {
230 owner.first = true;
231 }
232 if (interface == sensorInterface || interface == pwmInterface)
233 {
234 // we're not interested in pwm sensors, just pwm control
235 if (interface == sensorInterface &&
236 objectPair.first.find("pwm") != std::string::npos)
237 {
238 continue;
239 }
240 sensors[objectPair.first] = interface;
241 }
242 }
243 }
244 }
245 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700246 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700247 {
248 // skip if no pid configuration (means probably a sensor)
249 if (!owner.second.first)
250 {
251 continue;
252 }
253 auto endpoint = bus.new_method_call(
254 owner.first.c_str(), owner.second.second.c_str(),
255 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700256 ManagedObjectType configuration;
257 try
James Feist7136a5a2018-07-19 09:52:05 -0700258 {
James Feist22c257a2018-08-31 14:07:12 -0700259 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700260 responce.read(configuration);
261 }
262 catch (sdbusplus::exception_t&)
263 {
264 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700265 throw std::runtime_error("Error getting managed objects from " +
266 owner.first);
267 }
James Feist22c257a2018-08-31 14:07:12 -0700268
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700269 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700270 {
271 if (pathPair.second.find(pidConfigurationInterface) !=
272 pathPair.second.end() ||
273 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700274 pathPair.second.end() ||
275 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700276 pathPair.second.end())
277 {
278 configurations.emplace(pathPair);
279 }
280 }
281 }
James Feist8c3c51e2018-08-08 16:31:43 -0700282
283 // on dbus having an index field is a bit strange, so randomly
284 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800285 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700286 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700287 {
288 auto findZone =
289 configuration.second.find(pidZoneConfigurationInterface);
290 if (findZone != configuration.second.end())
291 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700292 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800293
James Feist8c3c51e2018-08-08 16:31:43 -0700294 const std::string& name =
James Feist50fdfe32018-09-24 15:51:09 -0700295 variant_ns::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800296 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700297
Patrick Venturec54fbd82018-10-30 19:40:05 -0700298 auto& details = zoneDetailsConfig[index];
James Feistd7a55bf2018-10-11 14:40:07 -0700299 details.minthermalrpm = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800300 VariantToDoubleVisitor(), zone.at("MinThermalRpm"));
James Feistd7a55bf2018-10-11 14:40:07 -0700301 details.failsafepercent = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800302 VariantToDoubleVisitor(), zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700303 }
304 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700305 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700306 {
James Feist8c3c51e2018-08-08 16:31:43 -0700307
James Feist22c257a2018-08-31 14:07:12 -0700308 const auto& base =
309 configuration.second.at(pidConfigurationInterface);
310 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700311 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700312 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700313 {
James Feistffd418b2018-11-15 14:46:36 -0800314 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700315 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700316
317 std::vector<std::string> sensorNames =
318 variant_ns::get<std::vector<std::string>>(
319 base.at("Inputs"));
320 auto findOutputs =
321 base.find("Outputs"); // currently only fans have outputs
322 if (findOutputs != base.end())
323 {
324 std::vector<std::string> outputs =
325 variant_ns::get<std::vector<std::string>>(
326 findOutputs->second);
327 sensorNames.insert(sensorNames.end(), outputs.begin(),
328 outputs.end());
329 }
330 bool sensorsAvailable = sensorNames.size();
331 std::vector<std::string> inputs;
332 for (const std::string& sensorName : sensorNames)
333 {
334 std::string name = sensorName;
335 // replace spaces with underscores to be legal on dbus
336 std::replace(name.begin(), name.end(), ' ', '_');
337 std::pair<std::string, std::string> sensorPathIfacePair;
338
339 if (!findSensor(sensors, name, sensorPathIfacePair))
340 {
341 sensorsAvailable = false;
342 break;
343 }
344 if (sensorPathIfacePair.second == sensorInterface)
345 {
346 inputs.push_back(name);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700347 auto& config = sensorConfig[name];
James Feist50fdfe32018-09-24 15:51:09 -0700348 config.type =
349 variant_ns::get<std::string>(base.at("Class"));
350 config.readpath = sensorPathIfacePair.first;
351 // todo: maybe un-hardcode this if we run into slower
352 // timeouts with sensors
353 if (config.type == "temp")
354 {
355 config.timeout = 500;
356 }
357 }
358 else if (sensorPathIfacePair.second == pwmInterface)
359 {
360 // copy so we can modify it
361 for (std::string otherSensor : sensorNames)
362 {
363 if (otherSensor == sensorName)
364 {
365 continue;
366 }
367 std::replace(otherSensor.begin(), otherSensor.end(),
368 ' ', '_');
Patrick Venturec54fbd82018-10-30 19:40:05 -0700369 auto& config = sensorConfig[otherSensor];
James Feist50fdfe32018-09-24 15:51:09 -0700370 config.writepath = sensorPathIfacePair.first;
371 // todo: un-hardcode this if there are fans with
372 // different ranges
373 config.max = 255;
374 config.min = 0;
375 }
376 }
377 }
378 // if the sensors aren't available in the current state, don't
379 // add them to the configuration.
380 if (!sensorsAvailable)
381 {
382 continue;
383 }
Patrick Venturef3252312018-10-30 08:42:53 -0700384 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700385 conf[variant_ns::get<std::string>(base.at("Name"))];
386 info.inputs = std::move(inputs);
387
388 info.type = variant_ns::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700389 // todo: auto generation yaml -> c script seems to discard this
390 // value for fans, verify this is okay
391 if (info.type == "fan")
392 {
393 info.setpoint = 0;
394 }
395 else
396 {
James Feistd7a55bf2018-10-11 14:40:07 -0700397 info.setpoint = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800398 VariantToDoubleVisitor(), base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700399 }
400 info.pidInfo.ts = 1.0; // currently unused
James Feistd7a55bf2018-10-11 14:40:07 -0700401 info.pidInfo.p_c = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800402 VariantToDoubleVisitor(), base.at("PCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700403 info.pidInfo.i_c = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800404 VariantToDoubleVisitor(), base.at("ICoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700405 info.pidInfo.ff_off = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800406 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700407 info.pidInfo.ff_gain = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800408 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700409 info.pidInfo.i_lim.max = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800410 VariantToDoubleVisitor(), base.at("ILimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700411 info.pidInfo.i_lim.min = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800412 VariantToDoubleVisitor(), base.at("ILimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700413 info.pidInfo.out_lim.max = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800414 VariantToDoubleVisitor(), base.at("OutLimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700415 info.pidInfo.out_lim.min = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800416 VariantToDoubleVisitor(), base.at("OutLimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700417 info.pidInfo.slew_neg = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800418 VariantToDoubleVisitor(), base.at("SlewNeg"));
James Feistd7a55bf2018-10-11 14:40:07 -0700419 info.pidInfo.slew_pos = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800420 VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist22c257a2018-08-31 14:07:12 -0700421 }
422 }
423 auto findStepwise =
424 configuration.second.find(stepwiseConfigurationInterface);
425 if (findStepwise != configuration.second.end())
426 {
427 const auto& base = findStepwise->second;
428 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700429 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700430 for (const std::string& zone : zones)
431 {
James Feistffd418b2018-11-15 14:46:36 -0800432 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700433 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700434
435 std::vector<std::string> inputs;
436 std::vector<std::string> sensorNames =
437 variant_ns::get<std::vector<std::string>>(
438 base.at("Inputs"));
439
440 bool sensorFound = sensorNames.size();
441 for (const std::string& sensorName : sensorNames)
442 {
443 std::string name = sensorName;
444 // replace spaces with underscores to be legal on dbus
445 std::replace(name.begin(), name.end(), ' ', '_');
446 std::pair<std::string, std::string> sensorPathIfacePair;
447
448 if (!findSensor(sensors, name, sensorPathIfacePair))
449 {
450 sensorFound = false;
451 break;
452 }
453
454 inputs.push_back(name);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700455 auto& config = sensorConfig[name];
James Feist50fdfe32018-09-24 15:51:09 -0700456 config.readpath = sensorPathIfacePair.first;
457 config.type = "temp";
458 // todo: maybe un-hardcode this if we run into slower
459 // timeouts with sensors
460
461 config.timeout = 500;
462 }
463 if (!sensorFound)
464 {
465 continue;
466 }
Patrick Venturef3252312018-10-30 08:42:53 -0700467 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700468 conf[variant_ns::get<std::string>(base.at("Name"))];
469 info.inputs = std::move(inputs);
470
James Feist22c257a2018-08-31 14:07:12 -0700471 info.type = "stepwise";
472 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700473 info.stepwiseInfo.positiveHysteresis = 0.0;
474 info.stepwiseInfo.negativeHysteresis = 0.0;
475 auto findPosHyst = base.find("PositiveHysteresis");
476 auto findNegHyst = base.find("NegativeHysteresis");
477 if (findPosHyst != base.end())
478 {
479 info.stepwiseInfo.positiveHysteresis =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800480 variant_ns::apply_visitor(VariantToDoubleVisitor(),
James Feistd7a55bf2018-10-11 14:40:07 -0700481 findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700482 }
483 if (findNegHyst != base.end())
484 {
485 info.stepwiseInfo.positiveHysteresis =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800486 variant_ns::apply_visitor(VariantToDoubleVisitor(),
James Feistd7a55bf2018-10-11 14:40:07 -0700487 findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700488 }
James Feist22c257a2018-08-31 14:07:12 -0700489 std::vector<double> readings =
James Feist50fdfe32018-09-24 15:51:09 -0700490 variant_ns::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700491 if (readings.size() > ec::maxStepwisePoints)
492 {
493 throw std::invalid_argument("Too many stepwise points.");
494 }
495 if (readings.empty())
496 {
497 throw std::invalid_argument(
498 "Must have one stepwise point.");
499 }
500 std::copy(readings.begin(), readings.end(),
501 info.stepwiseInfo.reading);
502 if (readings.size() < ec::maxStepwisePoints)
503 {
504 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800505 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700506 }
507 std::vector<double> outputs =
James Feist50fdfe32018-09-24 15:51:09 -0700508 variant_ns::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700509 if (readings.size() != outputs.size())
510 {
511 throw std::invalid_argument(
512 "Outputs size must match readings");
513 }
514 std::copy(outputs.begin(), outputs.end(),
515 info.stepwiseInfo.output);
516 if (outputs.size() < ec::maxStepwisePoints)
517 {
518 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800519 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700520 }
James Feist7136a5a2018-07-19 09:52:05 -0700521 }
522 }
523 }
524 if (DEBUG)
525 {
526 debugPrint();
527 }
James Feistc959c422018-11-01 12:33:40 -0700528 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700529 {
530 std::cerr << "No fan zones, application pausing until reboot\n";
531 while (1)
532 {
533 bus.process_discard();
James Feist65ea92e2018-10-26 16:21:37 -0700534 bus.wait();
James Feist50fdfe32018-09-24 15:51:09 -0700535 }
536 }
James Feist7136a5a2018-07-19 09:52:05 -0700537}
538} // namespace dbus_configuration