blob: 78cc4e481b89cc152784da6a2951316fdc326521 [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
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");
James Feist26e8c6a2018-10-25 10:38:26 -0700173 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);
James Feist22c257a2018-08-31 14:07:12 -0700185 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);
James Feist22c257a2018-08-31 14:07:12 -0700248 responce.read(configuration);
249 }
250 catch (sdbusplus::exception_t&)
251 {
252 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700253 throw std::runtime_error("Error getting managed objects from " +
254 owner.first);
255 }
James Feist22c257a2018-08-31 14:07:12 -0700256
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700257 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700258 {
259 if (pathPair.second.find(pidConfigurationInterface) !=
260 pathPair.second.end() ||
261 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700262 pathPair.second.end() ||
263 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700264 pathPair.second.end())
265 {
266 configurations.emplace(pathPair);
267 }
268 }
269 }
James Feist8c3c51e2018-08-08 16:31:43 -0700270
271 // on dbus having an index field is a bit strange, so randomly
272 // assign index based on name property
273 std::vector<std::string> zoneIndex;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700274 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700275 {
276 auto findZone =
277 configuration.second.find(pidZoneConfigurationInterface);
278 if (findZone != configuration.second.end())
279 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700280 const auto& zone = findZone->second;
James Feist8c3c51e2018-08-08 16:31:43 -0700281 size_t index = 1;
282 const std::string& name =
James Feist50fdfe32018-09-24 15:51:09 -0700283 variant_ns::get<std::string>(zone.at("Name"));
James Feist8c3c51e2018-08-08 16:31:43 -0700284 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), name);
285 if (it == zoneIndex.end())
286 {
287 zoneIndex.emplace_back(name);
288 index = zoneIndex.size();
289 }
290 else
291 {
292 index = zoneIndex.end() - it;
293 }
294
Patrick Venturec54fbd82018-10-30 19:40:05 -0700295 auto& details = zoneDetailsConfig[index];
James Feistd7a55bf2018-10-11 14:40:07 -0700296 details.minthermalrpm = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800297 VariantToDoubleVisitor(), zone.at("MinThermalRpm"));
James Feistd7a55bf2018-10-11 14:40:07 -0700298 details.failsafepercent = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800299 VariantToDoubleVisitor(), zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700300 }
301 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700302 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700303 {
James Feist8c3c51e2018-08-08 16:31:43 -0700304
James Feist22c257a2018-08-31 14:07:12 -0700305 const auto& base =
306 configuration.second.at(pidConfigurationInterface);
307 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700308 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700309 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700310 {
James Feist22c257a2018-08-31 14:07:12 -0700311 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
312 size_t index = 1;
313 if (it == zoneIndex.end())
James Feist7136a5a2018-07-19 09:52:05 -0700314 {
James Feist22c257a2018-08-31 14:07:12 -0700315 zoneIndex.emplace_back(zone);
316 index = zoneIndex.size();
James Feist7136a5a2018-07-19 09:52:05 -0700317 }
James Feist22c257a2018-08-31 14:07:12 -0700318 else
James Feist7136a5a2018-07-19 09:52:05 -0700319 {
James Feist22c257a2018-08-31 14:07:12 -0700320 index = zoneIndex.end() - it;
321 }
Patrick Venturec54fbd82018-10-30 19:40:05 -0700322 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700323
324 std::vector<std::string> sensorNames =
325 variant_ns::get<std::vector<std::string>>(
326 base.at("Inputs"));
327 auto findOutputs =
328 base.find("Outputs"); // currently only fans have outputs
329 if (findOutputs != base.end())
330 {
331 std::vector<std::string> outputs =
332 variant_ns::get<std::vector<std::string>>(
333 findOutputs->second);
334 sensorNames.insert(sensorNames.end(), outputs.begin(),
335 outputs.end());
336 }
337 bool sensorsAvailable = sensorNames.size();
338 std::vector<std::string> inputs;
339 for (const std::string& sensorName : sensorNames)
340 {
341 std::string name = sensorName;
342 // replace spaces with underscores to be legal on dbus
343 std::replace(name.begin(), name.end(), ' ', '_');
344 std::pair<std::string, std::string> sensorPathIfacePair;
345
346 if (!findSensor(sensors, name, sensorPathIfacePair))
347 {
348 sensorsAvailable = false;
349 break;
350 }
351 if (sensorPathIfacePair.second == sensorInterface)
352 {
353 inputs.push_back(name);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700354 auto& config = sensorConfig[name];
James Feist50fdfe32018-09-24 15:51:09 -0700355 config.type =
356 variant_ns::get<std::string>(base.at("Class"));
357 config.readpath = sensorPathIfacePair.first;
358 // todo: maybe un-hardcode this if we run into slower
359 // timeouts with sensors
360 if (config.type == "temp")
361 {
362 config.timeout = 500;
363 }
364 }
365 else if (sensorPathIfacePair.second == pwmInterface)
366 {
367 // copy so we can modify it
368 for (std::string otherSensor : sensorNames)
369 {
370 if (otherSensor == sensorName)
371 {
372 continue;
373 }
374 std::replace(otherSensor.begin(), otherSensor.end(),
375 ' ', '_');
Patrick Venturec54fbd82018-10-30 19:40:05 -0700376 auto& config = sensorConfig[otherSensor];
James Feist50fdfe32018-09-24 15:51:09 -0700377 config.writepath = sensorPathIfacePair.first;
378 // todo: un-hardcode this if there are fans with
379 // different ranges
380 config.max = 255;
381 config.min = 0;
382 }
383 }
384 }
385 // if the sensors aren't available in the current state, don't
386 // add them to the configuration.
387 if (!sensorsAvailable)
388 {
389 continue;
390 }
Patrick Venturef3252312018-10-30 08:42:53 -0700391 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700392 conf[variant_ns::get<std::string>(base.at("Name"))];
393 info.inputs = std::move(inputs);
394
395 info.type = variant_ns::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700396 // todo: auto generation yaml -> c script seems to discard this
397 // value for fans, verify this is okay
398 if (info.type == "fan")
399 {
400 info.setpoint = 0;
401 }
402 else
403 {
James Feistd7a55bf2018-10-11 14:40:07 -0700404 info.setpoint = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800405 VariantToDoubleVisitor(), base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700406 }
407 info.pidInfo.ts = 1.0; // currently unused
James Feistd7a55bf2018-10-11 14:40:07 -0700408 info.pidInfo.p_c = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800409 VariantToDoubleVisitor(), base.at("PCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700410 info.pidInfo.i_c = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800411 VariantToDoubleVisitor(), base.at("ICoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700412 info.pidInfo.ff_off = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800413 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700414 info.pidInfo.ff_gain = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800415 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
James Feistd7a55bf2018-10-11 14:40:07 -0700416 info.pidInfo.i_lim.max = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800417 VariantToDoubleVisitor(), base.at("ILimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700418 info.pidInfo.i_lim.min = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800419 VariantToDoubleVisitor(), base.at("ILimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700420 info.pidInfo.out_lim.max = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800421 VariantToDoubleVisitor(), base.at("OutLimitMax"));
James Feistd7a55bf2018-10-11 14:40:07 -0700422 info.pidInfo.out_lim.min = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800423 VariantToDoubleVisitor(), base.at("OutLimitMin"));
James Feistd7a55bf2018-10-11 14:40:07 -0700424 info.pidInfo.slew_neg = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800425 VariantToDoubleVisitor(), base.at("SlewNeg"));
James Feistd7a55bf2018-10-11 14:40:07 -0700426 info.pidInfo.slew_pos = variant_ns::apply_visitor(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800427 VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist22c257a2018-08-31 14:07:12 -0700428 }
429 }
430 auto findStepwise =
431 configuration.second.find(stepwiseConfigurationInterface);
432 if (findStepwise != configuration.second.end())
433 {
434 const auto& base = findStepwise->second;
435 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700436 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700437 for (const std::string& zone : zones)
438 {
439 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
440 size_t index = 1;
441 if (it == zoneIndex.end())
442 {
443 zoneIndex.emplace_back(zone);
444 index = zoneIndex.size();
445 }
446 else
447 {
448 index = zoneIndex.end() - it;
449 }
Patrick Venturec54fbd82018-10-30 19:40:05 -0700450 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700451
452 std::vector<std::string> inputs;
453 std::vector<std::string> sensorNames =
454 variant_ns::get<std::vector<std::string>>(
455 base.at("Inputs"));
456
457 bool sensorFound = sensorNames.size();
458 for (const std::string& sensorName : sensorNames)
459 {
460 std::string name = sensorName;
461 // replace spaces with underscores to be legal on dbus
462 std::replace(name.begin(), name.end(), ' ', '_');
463 std::pair<std::string, std::string> sensorPathIfacePair;
464
465 if (!findSensor(sensors, name, sensorPathIfacePair))
466 {
467 sensorFound = false;
468 break;
469 }
470
471 inputs.push_back(name);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700472 auto& config = sensorConfig[name];
James Feist50fdfe32018-09-24 15:51:09 -0700473 config.readpath = sensorPathIfacePair.first;
474 config.type = "temp";
475 // todo: maybe un-hardcode this if we run into slower
476 // timeouts with sensors
477
478 config.timeout = 500;
479 }
480 if (!sensorFound)
481 {
482 continue;
483 }
Patrick Venturef3252312018-10-30 08:42:53 -0700484 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700485 conf[variant_ns::get<std::string>(base.at("Name"))];
486 info.inputs = std::move(inputs);
487
James Feist22c257a2018-08-31 14:07:12 -0700488 info.type = "stepwise";
489 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700490 info.stepwiseInfo.positiveHysteresis = 0.0;
491 info.stepwiseInfo.negativeHysteresis = 0.0;
492 auto findPosHyst = base.find("PositiveHysteresis");
493 auto findNegHyst = base.find("NegativeHysteresis");
494 if (findPosHyst != base.end())
495 {
496 info.stepwiseInfo.positiveHysteresis =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800497 variant_ns::apply_visitor(VariantToDoubleVisitor(),
James Feistd7a55bf2018-10-11 14:40:07 -0700498 findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700499 }
500 if (findNegHyst != base.end())
501 {
502 info.stepwiseInfo.positiveHysteresis =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800503 variant_ns::apply_visitor(VariantToDoubleVisitor(),
James Feistd7a55bf2018-10-11 14:40:07 -0700504 findNegHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700505 }
James Feist22c257a2018-08-31 14:07:12 -0700506 std::vector<double> readings =
James Feist50fdfe32018-09-24 15:51:09 -0700507 variant_ns::get<std::vector<double>>(base.at("Reading"));
James Feist22c257a2018-08-31 14:07:12 -0700508 if (readings.size() > ec::maxStepwisePoints)
509 {
510 throw std::invalid_argument("Too many stepwise points.");
511 }
512 if (readings.empty())
513 {
514 throw std::invalid_argument(
515 "Must have one stepwise point.");
516 }
517 std::copy(readings.begin(), readings.end(),
518 info.stepwiseInfo.reading);
519 if (readings.size() < ec::maxStepwisePoints)
520 {
521 info.stepwiseInfo.reading[readings.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800522 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700523 }
524 std::vector<double> outputs =
James Feist50fdfe32018-09-24 15:51:09 -0700525 variant_ns::get<std::vector<double>>(base.at("Output"));
James Feist22c257a2018-08-31 14:07:12 -0700526 if (readings.size() != outputs.size())
527 {
528 throw std::invalid_argument(
529 "Outputs size must match readings");
530 }
531 std::copy(outputs.begin(), outputs.end(),
532 info.stepwiseInfo.output);
533 if (outputs.size() < ec::maxStepwisePoints)
534 {
535 info.stepwiseInfo.output[outputs.size()] =
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800536 std::numeric_limits<double>::quiet_NaN();
James Feist22c257a2018-08-31 14:07:12 -0700537 }
James Feist7136a5a2018-07-19 09:52:05 -0700538 }
539 }
540 }
541 if (DEBUG)
542 {
543 debugPrint();
544 }
James Feistc959c422018-11-01 12:33:40 -0700545 if (zoneConfig.empty() || zoneDetailsConfig.empty())
James Feist50fdfe32018-09-24 15:51:09 -0700546 {
547 std::cerr << "No fan zones, application pausing until reboot\n";
548 while (1)
549 {
550 bus.process_discard();
James Feist65ea92e2018-10-26 16:21:37 -0700551 bus.wait();
James Feist50fdfe32018-09-24 15:51:09 -0700552 }
553 }
James Feist7136a5a2018-07-19 09:52:05 -0700554}
555} // namespace dbus_configuration