blob: 3121623862d30b1db1f49d524659ae5e2adf6eaf [file] [log] [blame]
James Feist7136a5a2018-07-19 09:52:05 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Patrick Venture07716592018-10-14 11:46:40 -070017#include "conf.hpp"
18#include "dbus/util.hpp"
19
Patrick Venture107a25d2018-10-13 14:08:09 -070020#include <algorithm>
James Feist64f072a2018-08-10 16:39:24 -070021#include <chrono>
James Feist64f072a2018-08-10 16:39:24 -070022#include <functional>
James Feist7136a5a2018-07-19 09:52:05 -070023#include <iostream>
James Feist1738e2a2019-02-04 15:57:03 -080024#include <regex>
James Feist7136a5a2018-07-19 09:52:05 -070025#include <sdbusplus/bus.hpp>
James Feist64f072a2018-08-10 16:39:24 -070026#include <sdbusplus/bus/match.hpp>
James Feist22c257a2018-08-31 14:07:12 -070027#include <sdbusplus/exception.hpp>
James Feist7136a5a2018-07-19 09:52:05 -070028#include <set>
James Feist64f072a2018-08-10 16:39:24 -070029#include <thread>
James Feist7136a5a2018-07-19 09:52:05 -070030#include <unordered_map>
31
32static constexpr bool DEBUG = false; // enable to print found configuration
33
Patrick Venturec54fbd82018-10-30 19:40:05 -070034std::map<std::string, struct SensorConfig> sensorConfig = {};
35std::map<int64_t, PIDConf> zoneConfig = {};
36std::map<int64_t, struct ZoneConfig> zoneDetailsConfig = {};
James Feist7136a5a2018-07-19 09:52:05 -070037
Patrick Venturee2ec0f62018-09-04 12:30:27 -070038constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070039 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070040constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070041 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070042constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070043 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feist22c257a2018-08-31 14:07:12 -070044constexpr const char* stepwiseConfigurationInterface =
45 "xyz.openbmc_project.Configuration.Stepwise";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070046constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
47constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070048
49namespace dbus_configuration
50{
51
James Feist50fdfe32018-09-24 15:51:09 -070052namespace variant_ns = sdbusplus::message::variant_ns;
53
James Feist1738e2a2019-02-04 15:57:03 -080054bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
55 const std::string& search,
56 std::vector<std::pair<std::string, std::string>>& matches)
James Feist7136a5a2018-07-19 09:52:05 -070057{
James Feist1738e2a2019-02-04 15:57:03 -080058 std::smatch match;
59 std::regex reg(search);
60 for (const auto& sensor : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070061 {
James Feist1738e2a2019-02-04 15:57:03 -080062 if (std::regex_search(sensor.first, match, reg))
63 {
64 matches.push_back(sensor);
65 }
James Feist7136a5a2018-07-19 09:52:05 -070066 }
Patrick Venture107a25d2018-10-13 14:08:09 -070067
James Feist1738e2a2019-02-04 15:57:03 -080068 return matches.size() > 0;
James Feist7136a5a2018-07-19 09:52:05 -070069}
70
71// this function prints the configuration into a form similar to the cpp
72// generated code to help in verification, should be turned off during normal
73// use
74void debugPrint(void)
75{
76 // print sensor config
77 std::cout << "sensor config:\n";
78 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070079 for (const auto& pair : sensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -070080 {
81
82 std::cout << "\t{" << pair.first << ",\n\t\t{";
83 std::cout << pair.second.type << ", ";
84 std::cout << pair.second.readpath << ", ";
85 std::cout << pair.second.writepath << ", ";
86 std::cout << pair.second.min << ", ";
87 std::cout << pair.second.max << ", ";
88 std::cout << pair.second.timeout << "},\n\t},\n";
89 }
90 std::cout << "}\n\n";
91 std::cout << "ZoneDetailsConfig\n";
92 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -070093 for (const auto& zone : zoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070094 {
95 std::cout << "\t{" << zone.first << ",\n";
96 std::cout << "\t\t{" << zone.second.minthermalrpm << ", ";
97 std::cout << zone.second.failsafepercent << "}\n\t},\n";
98 }
99 std::cout << "}\n\n";
100 std::cout << "ZoneConfig\n";
101 std::cout << "{\n";
Patrick Venturec54fbd82018-10-30 19:40:05 -0700102 for (const auto& zone : zoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -0700103 {
104 std::cout << "\t{" << zone.first << "\n";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700105 for (const auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -0700106 {
107 std::cout << "\t\t{" << pidconf.first << ",\n";
108 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
109 std::cout << "\t\t\t{";
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700110 for (const auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700111 {
112 std::cout << "\n\t\t\t" << input << ",\n";
113 }
114 std::cout << "\t\t\t}\n";
115 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
James Feist22c257a2018-08-31 14:07:12 -0700116 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
117 std::cout << "\t\t\t" << pidconf.second.pidInfo.p_c << ",\n";
118 std::cout << "\t\t\t" << pidconf.second.pidInfo.i_c << ",\n";
119 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_off << ",\n";
120 std::cout << "\t\t\t" << pidconf.second.pidInfo.ff_gain << ",\n";
121 std::cout << "\t\t\t{" << pidconf.second.pidInfo.i_lim.min << ","
122 << pidconf.second.pidInfo.i_lim.max << "},\n";
123 std::cout << "\t\t\t{" << pidconf.second.pidInfo.out_lim.min << ","
124 << pidconf.second.pidInfo.out_lim.max << "},\n";
125 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_neg << ",\n";
126 std::cout << "\t\t\t" << pidconf.second.pidInfo.slew_pos << ",\n";
James Feist7136a5a2018-07-19 09:52:05 -0700127 std::cout << "\t\t\t}\n\t\t}\n";
128 }
129 std::cout << "\t},\n";
130 }
131 std::cout << "}\n\n";
132}
133
James Feist50fdfe32018-09-24 15:51:09 -0700134int eventHandler(sd_bus_message*, void*, sd_bus_error*)
135{
136 // do a brief sleep as we tend to get a bunch of these events at
137 // once
138 std::this_thread::sleep_for(std::chrono::seconds(5));
139 std::cout << "New configuration detected, restarting\n.";
140 std::exit(EXIT_SUCCESS); // service file should make us restart
141 return 1;
142}
143
James Feistffd418b2018-11-15 14:46:36 -0800144size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones)
145{
146 auto it = std::find(zones.begin(), zones.end(), name);
147 if (it == zones.end())
148 {
149 zones.emplace_back(name);
150 it = zones.end() - 1;
151 }
152
153 return it - zones.begin();
154}
155
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700156void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700157{
James Feist22c257a2018-08-31 14:07:12 -0700158 using DbusVariantType =
159 sdbusplus::message::variant<uint64_t, int64_t, double, std::string,
160 std::vector<std::string>,
161 std::vector<double>>;
162
James Feist7136a5a2018-07-19 09:52:05 -0700163 using ManagedObjectType = std::unordered_map<
164 sdbusplus::message::object_path,
James Feist22c257a2018-08-31 14:07:12 -0700165 std::unordered_map<std::string,
166 std::unordered_map<std::string, DbusVariantType>>>;
James Feist64f072a2018-08-10 16:39:24 -0700167
James Feist50fdfe32018-09-24 15:51:09 -0700168 // restart on configuration properties changed
169 static sdbusplus::bus::match::match configMatch(
James Feist64f072a2018-08-10 16:39:24 -0700170 bus,
171 "type='signal',member='PropertiesChanged',arg0namespace='" +
172 std::string(pidConfigurationInterface) + "'",
173 eventHandler);
174
James Feist50fdfe32018-09-24 15:51:09 -0700175 // restart on sensors changed
176 static sdbusplus::bus::match::match sensorAdded(
177 bus,
178 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
179 "sensors/'",
180 eventHandler);
181
James Feist7136a5a2018-07-19 09:52:05 -0700182 auto mapper =
183 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
184 "/xyz/openbmc_project/object_mapper",
185 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
James Feist26e8c6a2018-10-25 10:38:26 -0700186 mapper.append("/", 0,
James Feist22c257a2018-08-31 14:07:12 -0700187 std::array<const char*, 6>{objectManagerInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700188 pidConfigurationInterface,
189 pidZoneConfigurationInterface,
James Feist22c257a2018-08-31 14:07:12 -0700190 stepwiseConfigurationInterface,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700191 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700192 std::unordered_map<
193 std::string, std::unordered_map<std::string, std::vector<std::string>>>
194 respData;
James Feist22c257a2018-08-31 14:07:12 -0700195 try
196 {
197 auto resp = bus.call(mapper);
James Feist22c257a2018-08-31 14:07:12 -0700198 resp.read(respData);
199 }
200 catch (sdbusplus::exception_t&)
201 {
202 // can't do anything without mapper call data
203 throw std::runtime_error("ObjectMapper Call Failure");
204 }
James Feist7136a5a2018-07-19 09:52:05 -0700205
James Feist7136a5a2018-07-19 09:52:05 -0700206 if (respData.empty())
207 {
James Feist22c257a2018-08-31 14:07:12 -0700208 // can't do anything without mapper call data
James Feist7136a5a2018-07-19 09:52:05 -0700209 throw std::runtime_error("No configuration data available from Mapper");
210 }
211 // create a map of pair of <has pid configuration, ObjectManager path>
212 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
213 // and a map of <path, interface> for sensors
214 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700215 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700216 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700217 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700218 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700219 auto& owner = owners[ownerPair.first];
220 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700221 {
222
223 if (interface == objectManagerInterface)
224 {
225 owner.second = objectPair.first;
226 }
227 if (interface == pidConfigurationInterface ||
James Feist22c257a2018-08-31 14:07:12 -0700228 interface == pidZoneConfigurationInterface ||
229 interface == stepwiseConfigurationInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700230 {
231 owner.first = true;
232 }
233 if (interface == sensorInterface || interface == pwmInterface)
234 {
235 // we're not interested in pwm sensors, just pwm control
236 if (interface == sensorInterface &&
237 objectPair.first.find("pwm") != std::string::npos)
238 {
239 continue;
240 }
241 sensors[objectPair.first] = interface;
242 }
243 }
244 }
245 }
246 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700247 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700248 {
249 // skip if no pid configuration (means probably a sensor)
250 if (!owner.second.first)
251 {
252 continue;
253 }
254 auto endpoint = bus.new_method_call(
255 owner.first.c_str(), owner.second.second.c_str(),
256 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist22c257a2018-08-31 14:07:12 -0700257 ManagedObjectType configuration;
258 try
James Feist7136a5a2018-07-19 09:52:05 -0700259 {
James Feist22c257a2018-08-31 14:07:12 -0700260 auto responce = bus.call(endpoint);
James Feist22c257a2018-08-31 14:07:12 -0700261 responce.read(configuration);
262 }
263 catch (sdbusplus::exception_t&)
264 {
265 // this shouldn't happen, probably means daemon crashed
James Feist7136a5a2018-07-19 09:52:05 -0700266 throw std::runtime_error("Error getting managed objects from " +
267 owner.first);
268 }
James Feist22c257a2018-08-31 14:07:12 -0700269
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700270 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700271 {
272 if (pathPair.second.find(pidConfigurationInterface) !=
273 pathPair.second.end() ||
274 pathPair.second.find(pidZoneConfigurationInterface) !=
James Feist22c257a2018-08-31 14:07:12 -0700275 pathPair.second.end() ||
276 pathPair.second.find(stepwiseConfigurationInterface) !=
James Feist7136a5a2018-07-19 09:52:05 -0700277 pathPair.second.end())
278 {
279 configurations.emplace(pathPair);
280 }
281 }
282 }
James Feist8c3c51e2018-08-08 16:31:43 -0700283
284 // on dbus having an index field is a bit strange, so randomly
285 // assign index based on name property
James Feistffd418b2018-11-15 14:46:36 -0800286 std::vector<std::string> foundZones;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700287 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700288 {
289 auto findZone =
290 configuration.second.find(pidZoneConfigurationInterface);
291 if (findZone != configuration.second.end())
292 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700293 const auto& zone = findZone->second;
James Feistffd418b2018-11-15 14:46:36 -0800294
James Feist8c3c51e2018-08-08 16:31:43 -0700295 const std::string& name =
James Feist50fdfe32018-09-24 15:51:09 -0700296 variant_ns::get<std::string>(zone.at("Name"));
James Feistffd418b2018-11-15 14:46:36 -0800297 size_t index = getZoneIndex(name, foundZones);
James Feist8c3c51e2018-08-08 16:31:43 -0700298
Patrick Venturec54fbd82018-10-30 19:40:05 -0700299 auto& details = zoneDetailsConfig[index];
James Feist208abce2018-12-06 09:59:10 -0800300 details.minthermalrpm = variant_ns::visit(VariantToDoubleVisitor(),
301 zone.at("MinThermalRpm"));
302 details.failsafepercent = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800303 VariantToDoubleVisitor(), zone.at("FailSafePercent"));
James Feist7136a5a2018-07-19 09:52:05 -0700304 }
305 auto findBase = configuration.second.find(pidConfigurationInterface);
James Feist22c257a2018-08-31 14:07:12 -0700306 if (findBase != configuration.second.end())
James Feist7136a5a2018-07-19 09:52:05 -0700307 {
James Feist8c3c51e2018-08-08 16:31:43 -0700308
James Feist22c257a2018-08-31 14:07:12 -0700309 const auto& base =
310 configuration.second.at(pidConfigurationInterface);
311 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700312 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700313 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700314 {
James Feistffd418b2018-11-15 14:46:36 -0800315 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700316 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700317
318 std::vector<std::string> sensorNames =
319 variant_ns::get<std::vector<std::string>>(
320 base.at("Inputs"));
321 auto findOutputs =
322 base.find("Outputs"); // currently only fans have outputs
323 if (findOutputs != base.end())
324 {
325 std::vector<std::string> outputs =
326 variant_ns::get<std::vector<std::string>>(
327 findOutputs->second);
328 sensorNames.insert(sensorNames.end(), outputs.begin(),
329 outputs.end());
330 }
James Feist1738e2a2019-02-04 15:57:03 -0800331
James Feist50fdfe32018-09-24 15:51:09 -0700332 std::vector<std::string> inputs;
James Feist1738e2a2019-02-04 15:57:03 -0800333 std::vector<std::pair<std::string, std::string>>
334 sensorInterfaces;
James Feist50fdfe32018-09-24 15:51:09 -0700335 for (const std::string& sensorName : sensorNames)
336 {
337 std::string name = sensorName;
338 // replace spaces with underscores to be legal on dbus
339 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800340 findSensors(sensors, name, sensorInterfaces);
341 }
James Feist50fdfe32018-09-24 15:51:09 -0700342
James Feist1738e2a2019-02-04 15:57:03 -0800343 // if the sensors aren't available in the current state, don't
344 // add them to the configuration.
345 if (sensorInterfaces.empty())
346 {
347 continue;
348 }
349 for (const auto& sensorPathIfacePair : sensorInterfaces)
350 {
351
James Feist50fdfe32018-09-24 15:51:09 -0700352 if (sensorPathIfacePair.second == sensorInterface)
353 {
James Feist1738e2a2019-02-04 15:57:03 -0800354 size_t idx =
355 sensorPathIfacePair.first.find_last_of("/") + 1;
356 std::string shortName =
357 sensorPathIfacePair.first.substr(idx);
358
359 inputs.push_back(shortName);
360 auto& config = sensorConfig[shortName];
James Feist50fdfe32018-09-24 15:51:09 -0700361 config.type =
362 variant_ns::get<std::string>(base.at("Class"));
363 config.readpath = sensorPathIfacePair.first;
364 // todo: maybe un-hardcode this if we run into slower
365 // timeouts with sensors
366 if (config.type == "temp")
367 {
368 config.timeout = 500;
369 }
370 }
371 else if (sensorPathIfacePair.second == pwmInterface)
372 {
373 // copy so we can modify it
374 for (std::string otherSensor : sensorNames)
375 {
James Feist1738e2a2019-02-04 15:57:03 -0800376 std::replace(otherSensor.begin(), otherSensor.end(),
377 ' ', '_');
378 if (sensorPathIfacePair.first.find(otherSensor) !=
379 std::string::npos)
James Feist50fdfe32018-09-24 15:51:09 -0700380 {
381 continue;
382 }
James Feist1738e2a2019-02-04 15:57:03 -0800383
Patrick Venturec54fbd82018-10-30 19:40:05 -0700384 auto& config = sensorConfig[otherSensor];
James Feist50fdfe32018-09-24 15:51:09 -0700385 config.writepath = sensorPathIfacePair.first;
386 // todo: un-hardcode this if there are fans with
387 // different ranges
388 config.max = 255;
389 config.min = 0;
390 }
391 }
392 }
James Feist1738e2a2019-02-04 15:57:03 -0800393
Patrick Venturef3252312018-10-30 08:42:53 -0700394 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700395 conf[variant_ns::get<std::string>(base.at("Name"))];
396 info.inputs = std::move(inputs);
397
398 info.type = variant_ns::get<std::string>(base.at("Class"));
James Feist22c257a2018-08-31 14:07:12 -0700399 // todo: auto generation yaml -> c script seems to discard this
400 // value for fans, verify this is okay
401 if (info.type == "fan")
402 {
403 info.setpoint = 0;
404 }
405 else
406 {
James Feist208abce2018-12-06 09:59:10 -0800407 info.setpoint = variant_ns::visit(VariantToDoubleVisitor(),
408 base.at("SetPoint"));
James Feist22c257a2018-08-31 14:07:12 -0700409 }
410 info.pidInfo.ts = 1.0; // currently unused
James Feist208abce2018-12-06 09:59:10 -0800411 info.pidInfo.p_c = variant_ns::visit(VariantToDoubleVisitor(),
412 base.at("PCoefficient"));
413 info.pidInfo.i_c = variant_ns::visit(VariantToDoubleVisitor(),
414 base.at("ICoefficient"));
415 info.pidInfo.ff_off = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800416 VariantToDoubleVisitor(), base.at("FFOffCoefficient"));
James Feist208abce2018-12-06 09:59:10 -0800417 info.pidInfo.ff_gain = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800418 VariantToDoubleVisitor(), base.at("FFGainCoefficient"));
James Feist208abce2018-12-06 09:59:10 -0800419 info.pidInfo.i_lim.max = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800420 VariantToDoubleVisitor(), base.at("ILimitMax"));
James Feist208abce2018-12-06 09:59:10 -0800421 info.pidInfo.i_lim.min = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800422 VariantToDoubleVisitor(), base.at("ILimitMin"));
James Feist208abce2018-12-06 09:59:10 -0800423 info.pidInfo.out_lim.max = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800424 VariantToDoubleVisitor(), base.at("OutLimitMax"));
James Feist208abce2018-12-06 09:59:10 -0800425 info.pidInfo.out_lim.min = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800426 VariantToDoubleVisitor(), base.at("OutLimitMin"));
James Feist208abce2018-12-06 09:59:10 -0800427 info.pidInfo.slew_neg = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800428 VariantToDoubleVisitor(), base.at("SlewNeg"));
James Feist208abce2018-12-06 09:59:10 -0800429 info.pidInfo.slew_pos = variant_ns::visit(
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800430 VariantToDoubleVisitor(), base.at("SlewPos"));
James Feist22c257a2018-08-31 14:07:12 -0700431 }
432 }
433 auto findStepwise =
434 configuration.second.find(stepwiseConfigurationInterface);
435 if (findStepwise != configuration.second.end())
436 {
437 const auto& base = findStepwise->second;
438 const std::vector<std::string>& zones =
James Feist50fdfe32018-09-24 15:51:09 -0700439 variant_ns::get<std::vector<std::string>>(base.at("Zones"));
James Feist22c257a2018-08-31 14:07:12 -0700440 for (const std::string& zone : zones)
441 {
James Feistffd418b2018-11-15 14:46:36 -0800442 size_t index = getZoneIndex(zone, foundZones);
Patrick Venturec54fbd82018-10-30 19:40:05 -0700443 PIDConf& conf = zoneConfig[index];
James Feist50fdfe32018-09-24 15:51:09 -0700444
445 std::vector<std::string> inputs;
446 std::vector<std::string> sensorNames =
447 variant_ns::get<std::vector<std::string>>(
448 base.at("Inputs"));
449
James Feist1738e2a2019-02-04 15:57:03 -0800450 bool sensorFound = false;
James Feist50fdfe32018-09-24 15:51:09 -0700451 for (const std::string& sensorName : sensorNames)
452 {
453 std::string name = sensorName;
454 // replace spaces with underscores to be legal on dbus
455 std::replace(name.begin(), name.end(), ' ', '_');
James Feist1738e2a2019-02-04 15:57:03 -0800456 std::vector<std::pair<std::string, std::string>>
457 sensorPathIfacePairs;
James Feist50fdfe32018-09-24 15:51:09 -0700458
James Feist1738e2a2019-02-04 15:57:03 -0800459 if (!findSensors(sensors, name, sensorPathIfacePairs))
James Feist50fdfe32018-09-24 15:51:09 -0700460 {
James Feist50fdfe32018-09-24 15:51:09 -0700461 break;
462 }
463
James Feist1738e2a2019-02-04 15:57:03 -0800464 for (const auto& sensorPathIfacePair : sensorPathIfacePairs)
465 {
466 size_t idx =
467 sensorPathIfacePair.first.find_last_of("/") + 1;
468 std::string shortName =
469 sensorPathIfacePair.first.substr(idx);
James Feist50fdfe32018-09-24 15:51:09 -0700470
James Feist1738e2a2019-02-04 15:57:03 -0800471 inputs.push_back(shortName);
472 auto& config = sensorConfig[shortName];
473 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 sensorFound = true;
480 }
James Feist50fdfe32018-09-24 15:51:09 -0700481 }
482 if (!sensorFound)
483 {
484 continue;
485 }
Patrick Venturef3252312018-10-30 08:42:53 -0700486 struct ControllerInfo& info =
James Feist50fdfe32018-09-24 15:51:09 -0700487 conf[variant_ns::get<std::string>(base.at("Name"))];
488 info.inputs = std::move(inputs);
489
James Feist22c257a2018-08-31 14:07:12 -0700490 info.type = "stepwise";
491 info.stepwiseInfo.ts = 1.0; // currently unused
James Feist3dfaafd2018-09-20 15:46:58 -0700492 info.stepwiseInfo.positiveHysteresis = 0.0;
493 info.stepwiseInfo.negativeHysteresis = 0.0;
494 auto findPosHyst = base.find("PositiveHysteresis");
495 auto findNegHyst = base.find("NegativeHysteresis");
496 if (findPosHyst != base.end())
497 {
James Feist208abce2018-12-06 09:59:10 -0800498 info.stepwiseInfo.positiveHysteresis = variant_ns::visit(
499 VariantToDoubleVisitor(), findPosHyst->second);
James Feist3dfaafd2018-09-20 15:46:58 -0700500 }
501 if (findNegHyst != base.end())
502 {
James Feist208abce2018-12-06 09:59:10 -0800503 info.stepwiseInfo.positiveHysteresis = variant_ns::visit(
504 VariantToDoubleVisitor(), 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