blob: 7f721bfe455d44977b348018c3830beed83afc70 [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 Feist7136a5a2018-07-19 09:52:05 -070024#include <set>
James Feist64f072a2018-08-10 16:39:24 -070025#include <thread>
James Feist7136a5a2018-07-19 09:52:05 -070026#include <unordered_map>
27
28static constexpr bool DEBUG = false; // enable to print found configuration
29
30std::map<std::string, struct sensor> SensorConfig = {};
31std::map<int64_t, PIDConf> ZoneConfig = {};
32std::map<int64_t, struct zone> ZoneDetailsConfig = {};
33
Patrick Venturee2ec0f62018-09-04 12:30:27 -070034constexpr const char* pidConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070035 "xyz.openbmc_project.Configuration.Pid";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070036constexpr const char* objectManagerInterface =
James Feist7136a5a2018-07-19 09:52:05 -070037 "org.freedesktop.DBus.ObjectManager";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070038constexpr const char* pidZoneConfigurationInterface =
James Feist7136a5a2018-07-19 09:52:05 -070039 "xyz.openbmc_project.Configuration.Pid.Zone";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070040constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value";
41constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm";
James Feist7136a5a2018-07-19 09:52:05 -070042
43namespace dbus_configuration
44{
45
Patrick Venturee2ec0f62018-09-04 12:30:27 -070046bool findSensor(const std::unordered_map<std::string, std::string>& sensors,
47 const std::string& search,
48 std::pair<std::string, std::string>& sensor)
James Feist7136a5a2018-07-19 09:52:05 -070049{
Patrick Venturee2ec0f62018-09-04 12:30:27 -070050 for (const auto& s : sensors)
James Feist7136a5a2018-07-19 09:52:05 -070051 {
52 if (s.first.find(search) != std::string::npos)
53 {
54 sensor = s;
55 return true;
56 }
57 }
58 return false;
59}
60
61// this function prints the configuration into a form similar to the cpp
62// generated code to help in verification, should be turned off during normal
63// use
64void debugPrint(void)
65{
66 // print sensor config
67 std::cout << "sensor config:\n";
68 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070069 for (auto& pair : SensorConfig)
James Feist7136a5a2018-07-19 09:52:05 -070070 {
71
72 std::cout << "\t{" << pair.first << ",\n\t\t{";
73 std::cout << pair.second.type << ", ";
74 std::cout << pair.second.readpath << ", ";
75 std::cout << pair.second.writepath << ", ";
76 std::cout << pair.second.min << ", ";
77 std::cout << pair.second.max << ", ";
78 std::cout << pair.second.timeout << "},\n\t},\n";
79 }
80 std::cout << "}\n\n";
81 std::cout << "ZoneDetailsConfig\n";
82 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070083 for (auto& zone : ZoneDetailsConfig)
James Feist7136a5a2018-07-19 09:52:05 -070084 {
85 std::cout << "\t{" << zone.first << ",\n";
86 std::cout << "\t\t{" << zone.second.minthermalrpm << ", ";
87 std::cout << zone.second.failsafepercent << "}\n\t},\n";
88 }
89 std::cout << "}\n\n";
90 std::cout << "ZoneConfig\n";
91 std::cout << "{\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070092 for (auto& zone : ZoneConfig)
James Feist7136a5a2018-07-19 09:52:05 -070093 {
94 std::cout << "\t{" << zone.first << "\n";
Patrick Venturee2ec0f62018-09-04 12:30:27 -070095 for (auto& pidconf : zone.second)
James Feist7136a5a2018-07-19 09:52:05 -070096 {
97 std::cout << "\t\t{" << pidconf.first << ",\n";
98 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
99 std::cout << "\t\t\t{";
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700100 for (auto& input : pidconf.second.inputs)
James Feist7136a5a2018-07-19 09:52:05 -0700101 {
102 std::cout << "\n\t\t\t" << input << ",\n";
103 }
104 std::cout << "\t\t\t}\n";
105 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
106 std::cout << "\t\t\t{" << pidconf.second.info.ts << ",\n";
107 std::cout << "\t\t\t" << pidconf.second.info.p_c << ",\n";
108 std::cout << "\t\t\t" << pidconf.second.info.i_c << ",\n";
109 std::cout << "\t\t\t" << pidconf.second.info.ff_off << ",\n";
110 std::cout << "\t\t\t" << pidconf.second.info.ff_gain << ",\n";
111 std::cout << "\t\t\t{" << pidconf.second.info.i_lim.min << ","
112 << pidconf.second.info.i_lim.max << "},\n";
113 std::cout << "\t\t\t{" << pidconf.second.info.out_lim.min << ","
114 << pidconf.second.info.out_lim.max << "},\n";
115 std::cout << "\t\t\t" << pidconf.second.info.slew_neg << ",\n";
116 std::cout << "\t\t\t" << pidconf.second.info.slew_pos << ",\n";
117 std::cout << "\t\t\t}\n\t\t}\n";
118 }
119 std::cout << "\t},\n";
120 }
121 std::cout << "}\n\n";
122}
123
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700124void init(sdbusplus::bus::bus& bus)
James Feist7136a5a2018-07-19 09:52:05 -0700125{
126 using ManagedObjectType = std::unordered_map<
127 sdbusplus::message::object_path,
128 std::unordered_map<
129 std::string,
130 std::unordered_map<std::string,
131 sdbusplus::message::variant<
132 uint64_t, int64_t, double, std::string,
133 std::vector<std::string>>>>>;
James Feist64f072a2018-08-10 16:39:24 -0700134
135 // install watch for properties changed
136 std::function<void(sdbusplus::message::message & message)> eventHandler =
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700137 [](const sdbusplus::message::message&) {
James Feist64f072a2018-08-10 16:39:24 -0700138 // do a brief sleep as we tend to get a bunch of these events at
139 // once
140 std::this_thread::sleep_for(std::chrono::seconds(5));
141 std::cout << "New configuration detected, restarting\n.";
142 std::exit(EXIT_SUCCESS); // service file should make us restart
143 };
144
145 static sdbusplus::bus::match::match match(
146 bus,
147 "type='signal',member='PropertiesChanged',arg0namespace='" +
148 std::string(pidConfigurationInterface) + "'",
149 eventHandler);
150
James Feist7136a5a2018-07-19 09:52:05 -0700151 auto mapper =
152 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
153 "/xyz/openbmc_project/object_mapper",
154 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
155 mapper.append("", 0,
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700156 std::array<const char*, 5>{objectManagerInterface,
157 pidConfigurationInterface,
158 pidZoneConfigurationInterface,
159 sensorInterface, pwmInterface});
James Feist7136a5a2018-07-19 09:52:05 -0700160 auto resp = bus.call(mapper);
161 if (resp.is_method_error())
162 {
163 throw std::runtime_error("ObjectMapper Call Failure");
164 }
165 std::unordered_map<
166 std::string, std::unordered_map<std::string, std::vector<std::string>>>
167 respData;
168
169 resp.read(respData);
170 if (respData.empty())
171 {
172 throw std::runtime_error("No configuration data available from Mapper");
173 }
174 // create a map of pair of <has pid configuration, ObjectManager path>
175 std::unordered_map<std::string, std::pair<bool, std::string>> owners;
176 // and a map of <path, interface> for sensors
177 std::unordered_map<std::string, std::string> sensors;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700178 for (const auto& objectPair : respData)
James Feist7136a5a2018-07-19 09:52:05 -0700179 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700180 for (const auto& ownerPair : objectPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700181 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700182 auto& owner = owners[ownerPair.first];
183 for (const std::string& interface : ownerPair.second)
James Feist7136a5a2018-07-19 09:52:05 -0700184 {
185
186 if (interface == objectManagerInterface)
187 {
188 owner.second = objectPair.first;
189 }
190 if (interface == pidConfigurationInterface ||
191 interface == pidZoneConfigurationInterface)
192 {
193 owner.first = true;
194 }
195 if (interface == sensorInterface || interface == pwmInterface)
196 {
197 // we're not interested in pwm sensors, just pwm control
198 if (interface == sensorInterface &&
199 objectPair.first.find("pwm") != std::string::npos)
200 {
201 continue;
202 }
203 sensors[objectPair.first] = interface;
204 }
205 }
206 }
207 }
208 ManagedObjectType configurations;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700209 for (const auto& owner : owners)
James Feist7136a5a2018-07-19 09:52:05 -0700210 {
211 // skip if no pid configuration (means probably a sensor)
212 if (!owner.second.first)
213 {
214 continue;
215 }
216 auto endpoint = bus.new_method_call(
217 owner.first.c_str(), owner.second.second.c_str(),
218 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
219 auto responce = bus.call(endpoint);
220 if (responce.is_method_error())
221 {
222 throw std::runtime_error("Error getting managed objects from " +
223 owner.first);
224 }
225 ManagedObjectType configuration;
226 responce.read(configuration);
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700227 for (auto& pathPair : configuration)
James Feist7136a5a2018-07-19 09:52:05 -0700228 {
229 if (pathPair.second.find(pidConfigurationInterface) !=
230 pathPair.second.end() ||
231 pathPair.second.find(pidZoneConfigurationInterface) !=
232 pathPair.second.end())
233 {
234 configurations.emplace(pathPair);
235 }
236 }
237 }
James Feist8c3c51e2018-08-08 16:31:43 -0700238
239 // on dbus having an index field is a bit strange, so randomly
240 // assign index based on name property
241 std::vector<std::string> zoneIndex;
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700242 for (const auto& configuration : configurations)
James Feist7136a5a2018-07-19 09:52:05 -0700243 {
244 auto findZone =
245 configuration.second.find(pidZoneConfigurationInterface);
246 if (findZone != configuration.second.end())
247 {
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700248 const auto& zone = findZone->second;
James Feist8c3c51e2018-08-08 16:31:43 -0700249 size_t index = 1;
250 const std::string& name =
251 sdbusplus::message::variant_ns::get<std::string>(
252 zone.at("Name"));
253 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), name);
254 if (it == zoneIndex.end())
255 {
256 zoneIndex.emplace_back(name);
257 index = zoneIndex.size();
258 }
259 else
260 {
261 index = zoneIndex.end() - it;
262 }
263
264 auto& details = ZoneDetailsConfig[index];
James Feist7136a5a2018-07-19 09:52:05 -0700265 details.minthermalrpm = mapbox::util::apply_visitor(
266 VariantToFloatVisitor(), zone.at("MinThermalRpm"));
267 details.failsafepercent = mapbox::util::apply_visitor(
268 VariantToFloatVisitor(), zone.at("FailSafePercent"));
269 }
270 auto findBase = configuration.second.find(pidConfigurationInterface);
271 if (findBase == configuration.second.end())
272 {
273 continue;
274 }
James Feist8c3c51e2018-08-08 16:31:43 -0700275
Patrick Venturee2ec0f62018-09-04 12:30:27 -0700276 const auto& base = configuration.second.at(pidConfigurationInterface);
James Feist8c3c51e2018-08-08 16:31:43 -0700277 const std::vector<std::string>& zones =
James Feist7136a5a2018-07-19 09:52:05 -0700278 sdbusplus::message::variant_ns::get<std::vector<std::string>>(
James Feist8c3c51e2018-08-08 16:31:43 -0700279 base.at("Zones"));
280 for (const std::string& zone : zones)
James Feist7136a5a2018-07-19 09:52:05 -0700281 {
James Feist8c3c51e2018-08-08 16:31:43 -0700282 auto it = std::find(zoneIndex.begin(), zoneIndex.end(), zone);
283 size_t index = 1;
284 if (it == zoneIndex.end())
285 {
286 zoneIndex.emplace_back(zone);
287 index = zoneIndex.size();
288 }
289 else
290 {
291 index = zoneIndex.end() - it;
292 }
293 PIDConf& conf = ZoneConfig[index];
294 struct controller_info& info =
295 conf[sdbusplus::message::variant_ns::get<std::string>(
296 base.at("Name"))];
297 info.type = sdbusplus::message::variant_ns::get<std::string>(
298 base.at("Class"));
299 // todo: auto generation yaml -> c script seems to discard this
300 // value for fans, verify this is okay
301 if (info.type == "fan")
302 {
303 info.setpoint = 0;
304 }
305 else
306 {
307 info.setpoint = mapbox::util::apply_visitor(
308 VariantToFloatVisitor(), base.at("SetPoint"));
309 }
310 info.info.ts = 1.0; // currently unused
311 info.info.p_c = mapbox::util::apply_visitor(
312 VariantToFloatVisitor(), base.at("PCoefficient"));
313 info.info.i_c = mapbox::util::apply_visitor(
314 VariantToFloatVisitor(), base.at("ICoefficient"));
315 info.info.ff_off = mapbox::util::apply_visitor(
316 VariantToFloatVisitor(), base.at("FFOffCoefficient"));
317 info.info.ff_gain = mapbox::util::apply_visitor(
318 VariantToFloatVisitor(), base.at("FFGainCoefficient"));
319 info.info.i_lim.max = mapbox::util::apply_visitor(
320 VariantToFloatVisitor(), base.at("ILimitMax"));
321 info.info.i_lim.min = mapbox::util::apply_visitor(
322 VariantToFloatVisitor(), base.at("ILimitMin"));
323 info.info.out_lim.max = mapbox::util::apply_visitor(
324 VariantToFloatVisitor(), base.at("OutLimitMax"));
325 info.info.out_lim.min = mapbox::util::apply_visitor(
326 VariantToFloatVisitor(), base.at("OutLimitMin"));
327 info.info.slew_neg = mapbox::util::apply_visitor(
328 VariantToFloatVisitor(), base.at("SlewNeg"));
329 info.info.slew_pos = mapbox::util::apply_visitor(
330 VariantToFloatVisitor(), base.at("SlewPos"));
James Feist7136a5a2018-07-19 09:52:05 -0700331
James Feist8c3c51e2018-08-08 16:31:43 -0700332 std::vector<std::string> sensorNames =
333 sdbusplus::message::variant_ns::get<std::vector<std::string>>(
334 base.at("Inputs"));
335 auto findOutputs =
336 base.find("Outputs"); // currently only fans have outputs
337 if (findOutputs != base.end())
James Feist7136a5a2018-07-19 09:52:05 -0700338 {
James Feist8c3c51e2018-08-08 16:31:43 -0700339 std::vector<std::string> outputs =
340 sdbusplus::message::variant_ns::get<
341 std::vector<std::string>>(findOutputs->second);
342 sensorNames.insert(sensorNames.end(), outputs.begin(),
343 outputs.end());
James Feist7136a5a2018-07-19 09:52:05 -0700344 }
James Feist8c3c51e2018-08-08 16:31:43 -0700345 for (const std::string& sensorName : sensorNames)
James Feist7136a5a2018-07-19 09:52:05 -0700346 {
James Feist8c3c51e2018-08-08 16:31:43 -0700347 std::string name = sensorName;
348 // replace spaces with underscores to be legal on dbus
349 std::replace(name.begin(), name.end(), ' ', '_');
350 std::pair<std::string, std::string> sensorPathIfacePair;
351
352 if (!findSensor(sensors, name, sensorPathIfacePair))
James Feist7136a5a2018-07-19 09:52:05 -0700353 {
James Feist8c3c51e2018-08-08 16:31:43 -0700354 throw std::runtime_error(
355 "Could not map configuration to sensor " + name);
James Feist7136a5a2018-07-19 09:52:05 -0700356 }
James Feist8c3c51e2018-08-08 16:31:43 -0700357 if (sensorPathIfacePair.second == sensorInterface)
James Feist7136a5a2018-07-19 09:52:05 -0700358 {
James Feist8c3c51e2018-08-08 16:31:43 -0700359 info.inputs.push_back(name);
360 auto& config = SensorConfig[name];
361 config.type =
362 sdbusplus::message::variant_ns::get<std::string>(
363 base.at("Class"));
364 config.readpath = sensorPathIfacePair.first;
365 // todo: maybe un-hardcode this if we run into slower
366 // timeouts with sensors
367 if (config.type == "temp")
James Feist7136a5a2018-07-19 09:52:05 -0700368 {
James Feist8c3c51e2018-08-08 16:31:43 -0700369 config.timeout = 500;
James Feist7136a5a2018-07-19 09:52:05 -0700370 }
James Feist8c3c51e2018-08-08 16:31:43 -0700371 }
372 if (sensorPathIfacePair.second == pwmInterface)
373 {
374 // copy so we can modify it
375 for (std::string otherSensor : sensorNames)
376 {
377 if (otherSensor == sensorName)
378 {
379 continue;
380 }
381 std::replace(otherSensor.begin(), otherSensor.end(),
382 ' ', '_');
383 auto& config = SensorConfig[otherSensor];
384 config.writepath = sensorPathIfacePair.first;
385 // todo: un-hardcode this if there are fans with
386 // different ranges
387 config.max = 255;
388 config.min = 0;
389 }
James Feist7136a5a2018-07-19 09:52:05 -0700390 }
391 }
392 }
393 }
394 if (DEBUG)
395 {
396 debugPrint();
397 }
398}
399} // namespace dbus_configuration