blob: 7231453ef27daa3ee972f9034a67e02bf61cd1a1 [file] [log] [blame]
James Feist139cb572018-09-10 15:26:18 -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
17#include <fcntl.h>
18#include <linux/peci-ioctl.h>
19
20#include <CPUSensor.hpp>
21#include <Utils.hpp>
22#include <VariantVisitors.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/process/child.hpp>
28#include <experimental/filesystem>
29#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
34static constexpr bool DEBUG = false;
35
36enum State
37{
38 OFF, // host powered down
39 ON, // host powered on
40 READY // host powered on and mem test passed - fully ready
41};
42
43struct CPUConfig
44{
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -070045 CPUConfig(const uint64_t& bus, const uint64_t& addr,
46 const std::string& name, const State& state) :
47 bus(bus),
48 addr(addr), name(name), state(state)
James Feist139cb572018-09-10 15:26:18 -070049 {
50 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -070051 int bus;
James Feist139cb572018-09-10 15:26:18 -070052 int addr;
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -070053 std::string name;
James Feist139cb572018-09-10 15:26:18 -070054 State state;
55
56 bool operator<(const CPUConfig& rhs) const
57 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -070058 return (name < rhs.name);
James Feist139cb572018-09-10 15:26:18 -070059 }
60};
61
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070062static constexpr const char* peciDev = "/dev/peci-0";
63static constexpr const unsigned int rankNumMax = 8;
James Feist139cb572018-09-10 15:26:18 -070064
65namespace fs = std::experimental::filesystem;
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -070066namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070067static constexpr const char* configPrefix =
James Feist139cb572018-09-10 15:26:18 -070068 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070069static constexpr std::array<const char*, 3> sensorTypes = {
James Feist139cb572018-09-10 15:26:18 -070070 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
71
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -070072bool createSensors(
James Feist139cb572018-09-10 15:26:18 -070073 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
74 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
75 sensors,
76 boost::container::flat_set<CPUConfig>& configs,
77 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
78{
79 bool available = false;
80 for (CPUConfig cpu : configs)
81 {
82 if (cpu.state != State::OFF)
83 {
84 available = true;
85 break;
86 }
87 }
88 if (!available)
89 {
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -070090 return false;
James Feist139cb572018-09-10 15:26:18 -070091 }
92
93 // use new data the first time, then refresh
94 ManagedObjectType sensorConfigurations;
95 bool useCache = false;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070096 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -070097 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070098 if (!getSensorConfiguration(configPrefix + std::string(type),
James Feist139cb572018-09-10 15:26:18 -070099 dbusConnection, sensorConfigurations,
100 useCache))
101 {
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700102 return false;
James Feist139cb572018-09-10 15:26:18 -0700103 }
104 useCache = true;
105 }
106
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700107 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700108 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
109 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
110 hwmonNamePaths, 1))
James Feist139cb572018-09-10 15:26:18 -0700111 {
112 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700113 return true;
James Feist139cb572018-09-10 15:26:18 -0700114 }
115
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700116 boost::container::flat_set<std::string> scannedDirectories;
117 boost::container::flat_set<std::string> createdSensors;
118
119 for (fs::path& hwmonNamePath : hwmonNamePaths)
James Feist139cb572018-09-10 15:26:18 -0700120 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700121 const std::string& pathStr = hwmonNamePath.string();
122 auto hwmonDirectory = hwmonNamePath.parent_path();
123
124 auto ret = scannedDirectories.insert(hwmonDirectory.string());
125 if (!ret.second)
James Feist139cb572018-09-10 15:26:18 -0700126 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700127 continue; // already searched this path
128 }
129
130 fs::path::iterator it = hwmonNamePath.begin();
131 std::advance(it, 6); // pick the 6th part for a PECI client device name
132 std::string deviceName = *it;
133 auto findHyphen = deviceName.find("-");
134 if (findHyphen == std::string::npos)
135 {
136 std::cerr << "found bad device " << deviceName << "\n";
James Feist139cb572018-09-10 15:26:18 -0700137 continue;
138 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700139 std::string busStr = deviceName.substr(0, findHyphen);
140 std::string addrStr = deviceName.substr(findHyphen + 1);
141
142 size_t bus = 0;
143 size_t addr = 0;
144 try
145 {
146 bus = std::stoi(busStr);
147 addr = std::stoi(addrStr, 0, 16);
148 }
149 catch (std::invalid_argument)
150 {
151 continue;
152 }
153
154 std::ifstream nameFile(hwmonNamePath);
155 if (!nameFile.good())
156 {
157 std::cerr << "Failure reading " << hwmonNamePath << "\n";
158 continue;
159 }
160 std::string hwmonName;
161 std::getline(nameFile, hwmonName);
James Feist139cb572018-09-10 15:26:18 -0700162 nameFile.close();
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700163 if (!hwmonName.size())
James Feist139cb572018-09-10 15:26:18 -0700164 {
165 // shouldn't have an empty name file
166 continue;
167 }
James Feist139cb572018-09-10 15:26:18 -0700168 if (DEBUG)
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700169 {
170 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
171 << "\n";
172 }
James Feist139cb572018-09-10 15:26:18 -0700173
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700174 std::string sensorType;
James Feist139cb572018-09-10 15:26:18 -0700175 const SensorData* sensorData = nullptr;
176 const std::string* interfacePath = nullptr;
James Feist139cb572018-09-10 15:26:18 -0700177 const std::pair<std::string, boost::container::flat_map<
178 std::string, BasicVariantType>>*
179 baseConfiguration = nullptr;
James Feist139cb572018-09-10 15:26:18 -0700180
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700181 for (const std::pair<sdbusplus::message::object_path, SensorData>&
182 sensor : sensorConfigurations)
James Feist139cb572018-09-10 15:26:18 -0700183 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700184 sensorData = &(sensor.second);
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700185 for (const char* type : sensorTypes)
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700186 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700187 sensorType = configPrefix + std::string(type);
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700188 auto sensorBase = sensorData->find(sensorType);
189 if (sensorBase != sensorData->end())
190 {
191 baseConfiguration = &(*sensorBase);
192 break;
193 }
194 }
195 if (baseConfiguration == nullptr)
196 {
197 std::cerr << "error finding base configuration for" << hwmonName
198 << "\n";
199 continue;
200 }
201 auto configurationBus = baseConfiguration->second.find("Bus");
202 auto configurationAddress =
203 baseConfiguration->second.find("Address");
204
205 if (configurationBus == baseConfiguration->second.end() ||
206 configurationAddress == baseConfiguration->second.end())
207 {
208 std::cerr << "error finding bus or address in configuration";
209 continue;
210 }
211
212 if (sdbusplus::message::variant_ns::get<uint64_t>(
213 configurationBus->second) != bus ||
214 sdbusplus::message::variant_ns::get<uint64_t>(
215 configurationAddress->second) != addr)
216 {
217 continue;
218 }
219
220 interfacePath = &(sensor.first.str);
221 break;
222 }
223 if (interfacePath == nullptr)
224 {
225 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist139cb572018-09-10 15:26:18 -0700226 continue;
227 }
228
229 auto findCpuId = baseConfiguration->second.find("CpuID");
230 if (findCpuId == baseConfiguration->second.end())
231 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700232 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist139cb572018-09-10 15:26:18 -0700233 continue;
234 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700235 int cpuId =
236 variant_ns::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist139cb572018-09-10 15:26:18 -0700237
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700238 auto directory = hwmonNamePath.parent_path();
James Feist139cb572018-09-10 15:26:18 -0700239 std::vector<fs::path> inputPaths;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700240 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist139cb572018-09-10 15:26:18 -0700241 {
242 std::cerr << "No temperature sensors in system\n";
243 continue;
244 }
245
246 // iterate through all found temp sensors
247 for (auto& inputPath : inputPaths)
248 {
249 auto inputPathStr = inputPath.string();
250 auto labelPath =
251 boost::replace_all_copy(inputPathStr, "input", "label");
252 std::ifstream labelFile(labelPath);
253 if (!labelFile.good())
254 {
255 std::cerr << "Failure reading " << labelPath << "\n";
256 continue;
257 }
258 std::string label;
259 std::getline(labelFile, label);
260 labelFile.close();
261 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700262
263 auto findSensor = sensors.find(sensorName);
264 if (findSensor != sensors.end())
265 {
266 if (DEBUG)
267 {
268 std::cout << "Skipped: " << inputPath << ": " << sensorName
269 << " is already created\n";
270 }
271 continue;
272 }
273
James Feist139cb572018-09-10 15:26:18 -0700274 std::vector<thresholds::Threshold> sensorThresholds;
275 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700276 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyunac18e142018-10-09 16:38:58 -0700277 &labelHead);
James Feist139cb572018-09-10 15:26:18 -0700278 if (!sensorThresholds.size())
279 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700280 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
281 CPUSensor::sensorScaleFactor))
James Feist139cb572018-09-10 15:26:18 -0700282 {
Yoo, Jae Hyunac18e142018-10-09 16:38:58 -0700283 std::cerr << "error populating thresholds for "
284 << sensorName << "\n";
James Feist139cb572018-09-10 15:26:18 -0700285 }
286 }
287 sensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700288 inputPathStr, sensorType, objectServer, dbusConnection, io,
289 sensorName, std::move(sensorThresholds), *interfacePath);
290 createdSensors.insert(sensorName);
James Feist139cb572018-09-10 15:26:18 -0700291 if (DEBUG)
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700292 {
James Feist139cb572018-09-10 15:26:18 -0700293 std::cout << "Mapped: " << inputPath << " to " << sensorName
294 << "\n";
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700295 }
James Feist139cb572018-09-10 15:26:18 -0700296 }
297 }
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700298
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700299 if (createdSensors.size())
300 {
301 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
302 << " created\n";
303 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700304
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700305 return true;
James Feist139cb572018-09-10 15:26:18 -0700306}
307
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700308void exportDevice(const CPUConfig& config)
James Feist139cb572018-09-10 15:26:18 -0700309{
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700310 std::ostringstream hex;
311 hex << std::hex << config.addr;
312 const std::string& addrHexStr = hex.str();
313 std::string busStr = std::to_string(config.bus);
314
315 std::string parameters = "peci-client 0x" + addrHexStr;
316 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
317
318 std::experimental::filesystem::path devicePath(device);
319 const std::string& dir = devicePath.parent_path().string();
320 for (const auto& path :
321 std::experimental::filesystem::directory_iterator(dir))
James Feist139cb572018-09-10 15:26:18 -0700322 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700323 if (!std::experimental::filesystem::is_directory(path))
James Feist139cb572018-09-10 15:26:18 -0700324 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700325 continue;
James Feist139cb572018-09-10 15:26:18 -0700326 }
327
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700328 const std::string& directoryName = path.path().filename();
329 if (boost::starts_with(directoryName, busStr) &&
330 boost::ends_with(directoryName, addrHexStr))
331 {
332 if (DEBUG)
333 {
334 std::cout << parameters << " on bus " << busStr
335 << " is already exported\n";
336 }
337 return;
338 }
James Feist139cb572018-09-10 15:26:18 -0700339 }
340
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700341 std::ofstream deviceFile(device);
342 if (!deviceFile.good())
James Feist139cb572018-09-10 15:26:18 -0700343 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700344 std::cerr << "Error writing " << device << "\n";
James Feist139cb572018-09-10 15:26:18 -0700345 return;
346 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700347 deviceFile << parameters;
348 deviceFile.close();
349
350 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist139cb572018-09-10 15:26:18 -0700351}
352
353void detectCpu(boost::asio::deadline_timer& timer, boost::asio::io_service& io,
354 sdbusplus::asio::object_server& objectServer,
355 boost::container::flat_map<std::string,
356 std::unique_ptr<CPUSensor>>& sensors,
357 boost::container::flat_set<CPUConfig>& configs,
358 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
359{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700360 auto file = open(peciDev, O_RDWR);
James Feist139cb572018-09-10 15:26:18 -0700361 if (file < 0)
362 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700363 std::cerr << "unable to open " << peciDev << "\n";
James Feist139cb572018-09-10 15:26:18 -0700364 std::exit(EXIT_FAILURE);
365 }
366
367 size_t rescanDelaySeconds = 0;
368 bool keepPinging = false;
369 for (CPUConfig& config : configs)
370 {
371 State state;
372 struct peci_ping_msg msg;
373 msg.addr = config.addr;
374 if (!ioctl(file, PECI_IOC_PING, &msg))
375 {
376 bool dimmReady = false;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700377 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist139cb572018-09-10 15:26:18 -0700378 {
379 struct peci_rd_pkg_cfg_msg msg;
380 msg.addr = config.addr;
381 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
382 msg.param = rank;
383 msg.rx_len = 4;
384 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
385 {
386 if (msg.pkg_config[0] || msg.pkg_config[1] ||
387 msg.pkg_config[2])
388 {
389 dimmReady = true;
390 break;
391 }
392 }
393 else
394 {
395 break;
396 }
397 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700398
James Feist139cb572018-09-10 15:26:18 -0700399 if (dimmReady)
400 {
401 state = State::READY;
402 }
403 else
404 {
405 state = State::ON;
406 }
407 }
408 else
409 {
410 state = State::OFF;
411 }
412
413 if (config.state != state)
414 {
415 if (config.state == State::OFF)
416 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700417 std::cout << config.name << " is detected\n";
418 exportDevice(config);
James Feist139cb572018-09-10 15:26:18 -0700419 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700420 if (state == State::READY)
421 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700422 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700423 << " is/are detected\n";
424 }
James Feist139cb572018-09-10 15:26:18 -0700425 config.state = state;
426 }
427
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700428 if (config.state != State::OFF)
429 {
430 if (config.state == State::ON)
431 {
432 rescanDelaySeconds = 1;
433 }
434 else
435 {
436 rescanDelaySeconds = 5;
437 }
438 }
439
440 if (config.state != State::READY)
James Feist139cb572018-09-10 15:26:18 -0700441 {
442 keepPinging = true;
443 }
444
445 if (DEBUG)
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700446 {
447 std::cout << config.name << ", state: " << config.state << "\n";
448 }
James Feist139cb572018-09-10 15:26:18 -0700449 }
450
451 close(file);
452
453 if (rescanDelaySeconds)
454 {
455 std::this_thread::sleep_for(std::chrono::seconds(rescanDelaySeconds));
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700456 if (!createSensors(io, objectServer, sensors, configs, dbusConnection))
457 {
458 keepPinging = true;
459 }
James Feist139cb572018-09-10 15:26:18 -0700460 }
461
462 if (keepPinging)
463 {
464 timer.expires_from_now(boost::posix_time::seconds(1));
465 timer.async_wait([&](const boost::system::error_code& ec) {
466 if (ec == boost::asio::error::operation_aborted)
467 {
468 /* we were canceled*/
469 return;
470 }
471 else if (ec)
472 {
473 std::cerr << "timer error\n";
474 return;
475 }
476 detectCpu(timer, io, objectServer, sensors, configs,
477 dbusConnection);
478 });
479 }
480}
481
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700482bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
James Feist139cb572018-09-10 15:26:18 -0700483 boost::container::flat_set<CPUConfig>& configs)
484{
485 ManagedObjectType sensorConfigurations;
486 bool useCache = false;
487 // use new data the first time, then refresh
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700488 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700489 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700490 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
491 sensorConfigurations, useCache))
James Feist139cb572018-09-10 15:26:18 -0700492 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700493 return false;
James Feist139cb572018-09-10 15:26:18 -0700494 }
495 useCache = true;
496 }
497
498 // check PECI client addresses and DT overlay names from CPU configuration
499 // before starting ping operation
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700500 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700501 {
502 for (const std::pair<sdbusplus::message::object_path, SensorData>&
503 sensor : sensorConfigurations)
504 {
505 for (const std::pair<
506 std::string,
507 boost::container::flat_map<std::string, BasicVariantType>>&
508 config : sensor.second)
509 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700510 if ((configPrefix + std::string(type)) != config.first)
James Feist139cb572018-09-10 15:26:18 -0700511 {
512 continue;
513 }
514
James Feist139cb572018-09-10 15:26:18 -0700515 auto findName = config.second.find("Name");
516 if (findName == config.second.end())
517 {
518 continue;
519 }
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700520 std::string nameRaw = variant_ns::visit(
James Feist139cb572018-09-10 15:26:18 -0700521 VariantToStringVisitor(), findName->second);
522 std::string name =
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700523 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700524
525 auto findBus = config.second.find("Bus");
526 if (findBus == config.second.end())
527 {
528 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
529 continue;
530 }
531 uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(),
532 findBus->second);
533
534 auto findAddress = config.second.find("Address");
535 if (findAddress == config.second.end())
536 {
537 std::cerr << "Can't find 'Address' setting in " << name
538 << "\n";
539 continue;
540 }
541 uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(),
542 findAddress->second);
James Feist139cb572018-09-10 15:26:18 -0700543
544 if (DEBUG)
545 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700546 std::cout << "bus: " << bus << "\n";
James Feist139cb572018-09-10 15:26:18 -0700547 std::cout << "addr: " << addr << "\n";
548 std::cout << "name: " << name << "\n";
549 std::cout << "type: " << type << "\n";
James Feist139cb572018-09-10 15:26:18 -0700550 }
551
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700552 configs.emplace(bus, addr, name, State::OFF);
James Feist139cb572018-09-10 15:26:18 -0700553 }
554 }
555 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700556
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700557 if (configs.size())
558 {
559 std::cout << "CPU config" << (configs.size() == 1 ? " is" : "s are")
560 << " parsed\n";
561 return true;
562 }
563
564 return false;
James Feist139cb572018-09-10 15:26:18 -0700565}
566
567int main(int argc, char** argv)
568{
569 boost::asio::io_service io;
570 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
571 boost::container::flat_set<CPUConfig> configs;
572
573 systemBus->request_name("xyz.openbmc_project.CPUSensor");
574 sdbusplus::asio::object_server objectServer(systemBus);
575 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors;
576 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
577 boost::asio::deadline_timer pingTimer(io);
James Feist139cb572018-09-10 15:26:18 -0700578 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700579
580 filterTimer.expires_from_now(boost::posix_time::seconds(1));
581 filterTimer.async_wait([&](const boost::system::error_code& ec) {
582 if (ec == boost::asio::error::operation_aborted)
583 {
584 /* we were canceled*/
585 return;
586 }
587 else if (ec)
588 {
589 std::cerr << "timer error\n";
590 return;
591 }
592
593 if (getCpuConfig(systemBus, configs))
594 {
595 detectCpu(pingTimer, io, objectServer, sensors, configs, systemBus);
596 }
597 });
598
James Feist139cb572018-09-10 15:26:18 -0700599 std::function<void(sdbusplus::message::message&)> eventHandler =
600 [&](sdbusplus::message::message& message) {
601 if (message.is_method_error())
602 {
603 std::cerr << "callback method error\n";
604 return;
605 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700606
607 if (DEBUG)
608 {
609 std::cout << message.get_path() << " is changed\n";
610 }
611
James Feist139cb572018-09-10 15:26:18 -0700612 // this implicitly cancels the timer
613 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist139cb572018-09-10 15:26:18 -0700614 filterTimer.async_wait([&](const boost::system::error_code& ec) {
615 if (ec == boost::asio::error::operation_aborted)
616 {
617 /* we were canceled*/
618 return;
619 }
620 else if (ec)
621 {
622 std::cerr << "timer error\n";
623 return;
624 }
625
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700626 if (getCpuConfig(systemBus, configs))
James Feist139cb572018-09-10 15:26:18 -0700627 {
628 detectCpu(pingTimer, io, objectServer, sensors, configs,
629 systemBus);
630 }
631 });
632 };
633
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700634 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700635 {
636 auto match = std::make_unique<sdbusplus::bus::match::match>(
637 static_cast<sdbusplus::bus::bus&>(*systemBus),
638 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700639 std::string(inventoryPath) + "',arg0namespace='" +
640 configPrefix + type + "'",
James Feist139cb572018-09-10 15:26:18 -0700641 eventHandler);
642 matches.emplace_back(std::move(match));
643 }
644
645 io.run();
646}