blob: 5ad7b9baf822280afe4f5b79de5f01c53a0d6df5 [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 Yoo0003f462018-10-26 10:09:01 -070062static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070063static 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{
James Feist139cb572018-09-10 15:26:18 -0700360 size_t rescanDelaySeconds = 0;
361 bool keepPinging = false;
Jae Hyun Yoo0003f462018-10-26 10:09:01 -0700362
James Feist139cb572018-09-10 15:26:18 -0700363 for (CPUConfig& config : configs)
364 {
Jae Hyun Yoo0003f462018-10-26 10:09:01 -0700365 std::string peciDevPath = peciDev + std::to_string(config.bus);
366 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
367 if (file < 0)
368 {
369 std::cerr << "unable to open " << peciDevPath << "\n";
370 std::exit(EXIT_FAILURE);
371 }
372
James Feist139cb572018-09-10 15:26:18 -0700373 State state;
374 struct peci_ping_msg msg;
375 msg.addr = config.addr;
376 if (!ioctl(file, PECI_IOC_PING, &msg))
377 {
378 bool dimmReady = false;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700379 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist139cb572018-09-10 15:26:18 -0700380 {
381 struct peci_rd_pkg_cfg_msg msg;
382 msg.addr = config.addr;
383 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
384 msg.param = rank;
385 msg.rx_len = 4;
386 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
387 {
388 if (msg.pkg_config[0] || msg.pkg_config[1] ||
389 msg.pkg_config[2])
390 {
391 dimmReady = true;
392 break;
393 }
394 }
395 else
396 {
397 break;
398 }
399 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700400
James Feist139cb572018-09-10 15:26:18 -0700401 if (dimmReady)
402 {
403 state = State::READY;
404 }
405 else
406 {
407 state = State::ON;
408 }
409 }
410 else
411 {
412 state = State::OFF;
413 }
414
Jae Hyun Yoo0003f462018-10-26 10:09:01 -0700415 close(file);
416
James Feist139cb572018-09-10 15:26:18 -0700417 if (config.state != state)
418 {
419 if (config.state == State::OFF)
420 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700421 std::cout << config.name << " is detected\n";
422 exportDevice(config);
James Feist139cb572018-09-10 15:26:18 -0700423 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700424 if (state == State::READY)
425 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700426 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700427 << " is/are detected\n";
428 }
James Feist139cb572018-09-10 15:26:18 -0700429 config.state = state;
430 }
431
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700432 if (config.state != State::OFF)
433 {
434 if (config.state == State::ON)
435 {
436 rescanDelaySeconds = 1;
437 }
438 else
439 {
440 rescanDelaySeconds = 5;
441 }
442 }
443
444 if (config.state != State::READY)
James Feist139cb572018-09-10 15:26:18 -0700445 {
446 keepPinging = true;
447 }
448
449 if (DEBUG)
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700450 {
451 std::cout << config.name << ", state: " << config.state << "\n";
452 }
James Feist139cb572018-09-10 15:26:18 -0700453 }
454
James Feist139cb572018-09-10 15:26:18 -0700455 if (rescanDelaySeconds)
456 {
457 std::this_thread::sleep_for(std::chrono::seconds(rescanDelaySeconds));
Yoo, Jae Hyun60e14d32018-10-10 11:03:11 -0700458 if (!createSensors(io, objectServer, sensors, configs, dbusConnection))
459 {
460 keepPinging = true;
461 }
James Feist139cb572018-09-10 15:26:18 -0700462 }
463
464 if (keepPinging)
465 {
466 timer.expires_from_now(boost::posix_time::seconds(1));
467 timer.async_wait([&](const boost::system::error_code& ec) {
468 if (ec == boost::asio::error::operation_aborted)
469 {
470 /* we were canceled*/
471 return;
472 }
473 else if (ec)
474 {
475 std::cerr << "timer error\n";
476 return;
477 }
478 detectCpu(timer, io, objectServer, sensors, configs,
479 dbusConnection);
480 });
481 }
482}
483
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700484bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
James Feist139cb572018-09-10 15:26:18 -0700485 boost::container::flat_set<CPUConfig>& configs)
486{
487 ManagedObjectType sensorConfigurations;
488 bool useCache = false;
489 // use new data the first time, then refresh
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700490 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700491 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700492 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
493 sensorConfigurations, useCache))
James Feist139cb572018-09-10 15:26:18 -0700494 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700495 return false;
James Feist139cb572018-09-10 15:26:18 -0700496 }
497 useCache = true;
498 }
499
500 // check PECI client addresses and DT overlay names from CPU configuration
501 // before starting ping operation
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700502 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700503 {
504 for (const std::pair<sdbusplus::message::object_path, SensorData>&
505 sensor : sensorConfigurations)
506 {
507 for (const std::pair<
508 std::string,
509 boost::container::flat_map<std::string, BasicVariantType>>&
510 config : sensor.second)
511 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700512 if ((configPrefix + std::string(type)) != config.first)
James Feist139cb572018-09-10 15:26:18 -0700513 {
514 continue;
515 }
516
James Feist139cb572018-09-10 15:26:18 -0700517 auto findName = config.second.find("Name");
518 if (findName == config.second.end())
519 {
520 continue;
521 }
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700522 std::string nameRaw = variant_ns::visit(
James Feist139cb572018-09-10 15:26:18 -0700523 VariantToStringVisitor(), findName->second);
524 std::string name =
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700525 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700526
527 auto findBus = config.second.find("Bus");
528 if (findBus == config.second.end())
529 {
530 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
531 continue;
532 }
533 uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(),
534 findBus->second);
535
536 auto findAddress = config.second.find("Address");
537 if (findAddress == config.second.end())
538 {
539 std::cerr << "Can't find 'Address' setting in " << name
540 << "\n";
541 continue;
542 }
543 uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(),
544 findAddress->second);
James Feist139cb572018-09-10 15:26:18 -0700545
546 if (DEBUG)
547 {
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700548 std::cout << "bus: " << bus << "\n";
James Feist139cb572018-09-10 15:26:18 -0700549 std::cout << "addr: " << addr << "\n";
550 std::cout << "name: " << name << "\n";
551 std::cout << "type: " << type << "\n";
James Feist139cb572018-09-10 15:26:18 -0700552 }
553
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700554 configs.emplace(bus, addr, name, State::OFF);
James Feist139cb572018-09-10 15:26:18 -0700555 }
556 }
557 }
Yoo, Jae Hyun5481fac2018-10-09 16:43:03 -0700558
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700559 if (configs.size())
560 {
561 std::cout << "CPU config" << (configs.size() == 1 ? " is" : "s are")
562 << " parsed\n";
563 return true;
564 }
565
566 return false;
James Feist139cb572018-09-10 15:26:18 -0700567}
568
569int main(int argc, char** argv)
570{
571 boost::asio::io_service io;
572 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
573 boost::container::flat_set<CPUConfig> configs;
574
575 systemBus->request_name("xyz.openbmc_project.CPUSensor");
576 sdbusplus::asio::object_server objectServer(systemBus);
577 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors;
578 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
579 boost::asio::deadline_timer pingTimer(io);
James Feist139cb572018-09-10 15:26:18 -0700580 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700581
582 filterTimer.expires_from_now(boost::posix_time::seconds(1));
583 filterTimer.async_wait([&](const boost::system::error_code& ec) {
584 if (ec == boost::asio::error::operation_aborted)
585 {
586 /* we were canceled*/
587 return;
588 }
589 else if (ec)
590 {
591 std::cerr << "timer error\n";
592 return;
593 }
594
595 if (getCpuConfig(systemBus, configs))
596 {
597 detectCpu(pingTimer, io, objectServer, sensors, configs, systemBus);
598 }
599 });
600
James Feist139cb572018-09-10 15:26:18 -0700601 std::function<void(sdbusplus::message::message&)> eventHandler =
602 [&](sdbusplus::message::message& message) {
603 if (message.is_method_error())
604 {
605 std::cerr << "callback method error\n";
606 return;
607 }
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700608
609 if (DEBUG)
610 {
611 std::cout << message.get_path() << " is changed\n";
612 }
613
James Feist139cb572018-09-10 15:26:18 -0700614 // this implicitly cancels the timer
615 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist139cb572018-09-10 15:26:18 -0700616 filterTimer.async_wait([&](const boost::system::error_code& ec) {
617 if (ec == boost::asio::error::operation_aborted)
618 {
619 /* we were canceled*/
620 return;
621 }
622 else if (ec)
623 {
624 std::cerr << "timer error\n";
625 return;
626 }
627
Jae Hyun Yooa8fa4d22018-10-22 15:24:29 -0700628 if (getCpuConfig(systemBus, configs))
James Feist139cb572018-09-10 15:26:18 -0700629 {
630 detectCpu(pingTimer, io, objectServer, sensors, configs,
631 systemBus);
632 }
633 });
634 };
635
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700636 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700637 {
638 auto match = std::make_unique<sdbusplus::bus::match::match>(
639 static_cast<sdbusplus::bus::bus&>(*systemBus),
640 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700641 std::string(inventoryPath) + "',arg0namespace='" +
642 configPrefix + type + "'",
James Feist139cb572018-09-10 15:26:18 -0700643 eventHandler);
644 matches.emplace_back(std::move(match));
645 }
646
647 io.run();
648}