blob: dcfb27ab9b02fbe53be90ebf4f841b6b0af44b90 [file] [log] [blame]
James Feist6714a252018-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
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070020
21#include <CPUSensor.hpp>
22#include <Utils.hpp>
23#include <VariantVisitors.hpp>
24#include <boost/algorithm/string/predicate.hpp>
25#include <boost/algorithm/string/replace.hpp>
26#include <boost/container/flat_set.hpp>
27#include <boost/date_time/posix_time/posix_time.hpp>
28#include <boost/process/child.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
30#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
James Feistf87dc4c2018-12-05 14:39:51 -080034// clang-format off
35// this needs to be included last or we'll have build issues
36#include <linux/peci-ioctl.h>
37// clang-format on
38
James Feist6714a252018-09-10 15:26:18 -070039static constexpr bool DEBUG = false;
40
41enum State
42{
43 OFF, // host powered down
44 ON, // host powered on
45 READY // host powered on and mem test passed - fully ready
46};
47
48struct CPUConfig
49{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070050 CPUConfig(const uint64_t& bus, const uint64_t& addr,
51 const std::string& name, const State& state) :
52 bus(bus),
53 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070054 {
55 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070056 int bus;
James Feist6714a252018-09-10 15:26:18 -070057 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070058 std::string name;
James Feist6714a252018-09-10 15:26:18 -070059 State state;
60
61 bool operator<(const CPUConfig& rhs) const
62 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070063 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070064 }
65};
66
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070067static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070069
James Feistcf3bce62019-01-08 10:07:19 -080070namespace fs = std::filesystem;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070071namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070072static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070073 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070075 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
76
Jae Hyun Yood64262b2018-11-01 13:31:16 -070077void detectCpuAsync(
78 boost::asio::deadline_timer& pingTimer,
79 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
80 sdbusplus::asio::object_server& objectServer,
81 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
82 sensors,
83 boost::container::flat_set<CPUConfig>& configs,
84 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection);
85
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -070086bool createSensors(
James Feist6714a252018-09-10 15:26:18 -070087 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
88 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
89 sensors,
90 boost::container::flat_set<CPUConfig>& configs,
91 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
92{
93 bool available = false;
94 for (CPUConfig cpu : configs)
95 {
96 if (cpu.state != State::OFF)
97 {
98 available = true;
99 break;
100 }
101 }
102 if (!available)
103 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700104 return false;
James Feist6714a252018-09-10 15:26:18 -0700105 }
106
107 // use new data the first time, then refresh
108 ManagedObjectType sensorConfigurations;
109 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700111 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112 if (!getSensorConfiguration(configPrefix + std::string(type),
James Feist6714a252018-09-10 15:26:18 -0700113 dbusConnection, sensorConfigurations,
114 useCache))
115 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700116 return false;
James Feist6714a252018-09-10 15:26:18 -0700117 }
118 useCache = true;
119 }
120
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700121 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700122 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
123 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
124 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700125 {
126 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700127 return true;
James Feist6714a252018-09-10 15:26:18 -0700128 }
129
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700130 boost::container::flat_set<std::string> scannedDirectories;
131 boost::container::flat_set<std::string> createdSensors;
132
133 for (fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700134 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700135 const std::string& pathStr = hwmonNamePath.string();
136 auto hwmonDirectory = hwmonNamePath.parent_path();
137
138 auto ret = scannedDirectories.insert(hwmonDirectory.string());
139 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700140 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700141 continue; // already searched this path
142 }
143
144 fs::path::iterator it = hwmonNamePath.begin();
145 std::advance(it, 6); // pick the 6th part for a PECI client device name
146 std::string deviceName = *it;
147 auto findHyphen = deviceName.find("-");
148 if (findHyphen == std::string::npos)
149 {
150 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700151 continue;
152 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700153 std::string busStr = deviceName.substr(0, findHyphen);
154 std::string addrStr = deviceName.substr(findHyphen + 1);
155
156 size_t bus = 0;
157 size_t addr = 0;
158 try
159 {
160 bus = std::stoi(busStr);
161 addr = std::stoi(addrStr, 0, 16);
162 }
163 catch (std::invalid_argument)
164 {
165 continue;
166 }
167
168 std::ifstream nameFile(hwmonNamePath);
169 if (!nameFile.good())
170 {
171 std::cerr << "Failure reading " << hwmonNamePath << "\n";
172 continue;
173 }
174 std::string hwmonName;
175 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700176 nameFile.close();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700177 if (!hwmonName.size())
James Feist6714a252018-09-10 15:26:18 -0700178 {
179 // shouldn't have an empty name file
180 continue;
181 }
James Feist6714a252018-09-10 15:26:18 -0700182 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700183 {
184 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
185 << "\n";
186 }
James Feist6714a252018-09-10 15:26:18 -0700187
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700188 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700189 const SensorData* sensorData = nullptr;
190 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700191 const std::pair<std::string, boost::container::flat_map<
192 std::string, BasicVariantType>>*
193 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700194
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700195 for (const std::pair<sdbusplus::message::object_path, SensorData>&
196 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700197 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700198 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700199 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700200 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700201 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700202 auto sensorBase = sensorData->find(sensorType);
203 if (sensorBase != sensorData->end())
204 {
205 baseConfiguration = &(*sensorBase);
206 break;
207 }
208 }
209 if (baseConfiguration == nullptr)
210 {
211 std::cerr << "error finding base configuration for" << hwmonName
212 << "\n";
213 continue;
214 }
215 auto configurationBus = baseConfiguration->second.find("Bus");
216 auto configurationAddress =
217 baseConfiguration->second.find("Address");
218
219 if (configurationBus == baseConfiguration->second.end() ||
220 configurationAddress == baseConfiguration->second.end())
221 {
222 std::cerr << "error finding bus or address in configuration";
223 continue;
224 }
225
226 if (sdbusplus::message::variant_ns::get<uint64_t>(
227 configurationBus->second) != bus ||
228 sdbusplus::message::variant_ns::get<uint64_t>(
229 configurationAddress->second) != addr)
230 {
231 continue;
232 }
233
234 interfacePath = &(sensor.first.str);
235 break;
236 }
237 if (interfacePath == nullptr)
238 {
239 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700240 continue;
241 }
242
243 auto findCpuId = baseConfiguration->second.find("CpuID");
244 if (findCpuId == baseConfiguration->second.end())
245 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700246 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700247 continue;
248 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700249 int cpuId =
250 variant_ns::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700251
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700252 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700253 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700254 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700255 {
256 std::cerr << "No temperature sensors in system\n";
257 continue;
258 }
259
260 // iterate through all found temp sensors
261 for (auto& inputPath : inputPaths)
262 {
263 auto inputPathStr = inputPath.string();
264 auto labelPath =
265 boost::replace_all_copy(inputPathStr, "input", "label");
266 std::ifstream labelFile(labelPath);
267 if (!labelFile.good())
268 {
269 std::cerr << "Failure reading " << labelPath << "\n";
270 continue;
271 }
272 std::string label;
273 std::getline(labelFile, label);
274 labelFile.close();
275 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700276
277 auto findSensor = sensors.find(sensorName);
278 if (findSensor != sensors.end())
279 {
280 if (DEBUG)
281 {
282 std::cout << "Skipped: " << inputPath << ": " << sensorName
283 << " is already created\n";
284 }
285 continue;
286 }
287
James Feist6714a252018-09-10 15:26:18 -0700288 std::vector<thresholds::Threshold> sensorThresholds;
289 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700290 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700291 &labelHead);
James Feist6714a252018-09-10 15:26:18 -0700292 if (!sensorThresholds.size())
293 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700294 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
295 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700296 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700297 std::cerr << "error populating thresholds for "
298 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700299 }
300 }
301 sensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700302 inputPathStr, sensorType, objectServer, dbusConnection, io,
303 sensorName, std::move(sensorThresholds), *interfacePath);
304 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700305 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700306 {
James Feist6714a252018-09-10 15:26:18 -0700307 std::cout << "Mapped: " << inputPath << " to " << sensorName
308 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700309 }
James Feist6714a252018-09-10 15:26:18 -0700310 }
311 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700312
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700313 if (createdSensors.size())
314 {
315 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
316 << " created\n";
317 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700318
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700319 return true;
James Feist6714a252018-09-10 15:26:18 -0700320}
321
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700322void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700323{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700324 std::ostringstream hex;
325 hex << std::hex << config.addr;
326 const std::string& addrHexStr = hex.str();
327 std::string busStr = std::to_string(config.bus);
328
329 std::string parameters = "peci-client 0x" + addrHexStr;
330 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
331
James Feistcf3bce62019-01-08 10:07:19 -0800332 std::filesystem::path devicePath(device);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700333 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800334 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700335 {
James Feistcf3bce62019-01-08 10:07:19 -0800336 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700337 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700338 continue;
James Feist6714a252018-09-10 15:26:18 -0700339 }
340
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700341 const std::string& directoryName = path.path().filename();
342 if (boost::starts_with(directoryName, busStr) &&
343 boost::ends_with(directoryName, addrHexStr))
344 {
345 if (DEBUG)
346 {
347 std::cout << parameters << " on bus " << busStr
348 << " is already exported\n";
349 }
350 return;
351 }
James Feist6714a252018-09-10 15:26:18 -0700352 }
353
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700354 std::ofstream deviceFile(device);
355 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700356 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700357 std::cerr << "Error writing " << device << "\n";
James Feist6714a252018-09-10 15:26:18 -0700358 return;
359 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700360 deviceFile << parameters;
361 deviceFile.close();
362
363 std::cout << parameters << " on bus " << busStr << " is exported\n";
James Feist6714a252018-09-10 15:26:18 -0700364}
365
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700366void detectCpu(boost::asio::deadline_timer& pingTimer,
367 boost::asio::deadline_timer& creationTimer,
368 boost::asio::io_service& io,
James Feist6714a252018-09-10 15:26:18 -0700369 sdbusplus::asio::object_server& objectServer,
370 boost::container::flat_map<std::string,
371 std::unique_ptr<CPUSensor>>& sensors,
372 boost::container::flat_set<CPUConfig>& configs,
373 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
374{
James Feist6714a252018-09-10 15:26:18 -0700375 size_t rescanDelaySeconds = 0;
376 bool keepPinging = false;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700377
James Feist6714a252018-09-10 15:26:18 -0700378 for (CPUConfig& config : configs)
379 {
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700380 std::string peciDevPath = peciDev + std::to_string(config.bus);
381 auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC);
382 if (file < 0)
383 {
384 std::cerr << "unable to open " << peciDevPath << "\n";
385 std::exit(EXIT_FAILURE);
386 }
387
James Feist6714a252018-09-10 15:26:18 -0700388 State state;
389 struct peci_ping_msg msg;
390 msg.addr = config.addr;
391 if (!ioctl(file, PECI_IOC_PING, &msg))
392 {
393 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700394 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700395 {
396 struct peci_rd_pkg_cfg_msg msg;
397 msg.addr = config.addr;
398 msg.index = MBX_INDEX_DDR_DIMM_TEMP;
399 msg.param = rank;
400 msg.rx_len = 4;
401 if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg))
402 {
403 if (msg.pkg_config[0] || msg.pkg_config[1] ||
404 msg.pkg_config[2])
405 {
406 dimmReady = true;
407 break;
408 }
409 }
410 else
411 {
412 break;
413 }
414 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700415
James Feist6714a252018-09-10 15:26:18 -0700416 if (dimmReady)
417 {
418 state = State::READY;
419 }
420 else
421 {
422 state = State::ON;
423 }
424 }
425 else
426 {
427 state = State::OFF;
428 }
429
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700430 close(file);
431
James Feist6714a252018-09-10 15:26:18 -0700432 if (config.state != state)
433 {
434 if (config.state == State::OFF)
435 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700436 std::cout << config.name << " is detected\n";
437 exportDevice(config);
James Feist6714a252018-09-10 15:26:18 -0700438 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700439 if (state == State::READY)
440 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700441 std::cout << "DIMM(s) on " << config.name
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700442 << " is/are detected\n";
443 }
James Feist6714a252018-09-10 15:26:18 -0700444 config.state = state;
445 }
446
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700447 if (config.state != State::OFF)
448 {
449 if (config.state == State::ON)
450 {
451 rescanDelaySeconds = 1;
452 }
453 else
454 {
455 rescanDelaySeconds = 5;
456 }
457 }
458
459 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700460 {
461 keepPinging = true;
462 }
463
464 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700465 {
466 std::cout << config.name << ", state: " << config.state << "\n";
467 }
James Feist6714a252018-09-10 15:26:18 -0700468 }
469
James Feist6714a252018-09-10 15:26:18 -0700470 if (rescanDelaySeconds)
471 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700472 creationTimer.expires_from_now(
473 boost::posix_time::seconds(rescanDelaySeconds));
474 creationTimer.async_wait([&](const boost::system::error_code& ec) {
475 if (ec == boost::asio::error::operation_aborted)
476 {
477 return; // we're being canceled
478 }
479
480 if (!createSensors(io, objectServer, sensors, configs,
481 dbusConnection))
482 {
483 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
484 sensors, configs, dbusConnection);
485 }
486 });
James Feist6714a252018-09-10 15:26:18 -0700487 }
488
489 if (keepPinging)
490 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700491 detectCpuAsync(pingTimer, creationTimer, io, objectServer, sensors,
492 configs, dbusConnection);
James Feist6714a252018-09-10 15:26:18 -0700493 }
494}
495
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700496void detectCpuAsync(
497 boost::asio::deadline_timer& pingTimer,
498 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
499 sdbusplus::asio::object_server& objectServer,
500 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
501 sensors,
502 boost::container::flat_set<CPUConfig>& configs,
503 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
504{
505 pingTimer.expires_from_now(boost::posix_time::seconds(1));
506 pingTimer.async_wait([&](const boost::system::error_code& ec) {
507 if (ec == boost::asio::error::operation_aborted)
508 {
509 return; // we're being canceled
510 }
511
512 detectCpu(pingTimer, creationTimer, io, objectServer, sensors, configs,
513 dbusConnection);
514 });
515}
516
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700517bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
James Feist6714a252018-09-10 15:26:18 -0700518 boost::container::flat_set<CPUConfig>& configs)
519{
520 ManagedObjectType sensorConfigurations;
521 bool useCache = false;
522 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700523 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700524 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700525 if (!getSensorConfiguration(configPrefix + std::string(type), systemBus,
526 sensorConfigurations, useCache))
James Feist6714a252018-09-10 15:26:18 -0700527 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700528 return false;
James Feist6714a252018-09-10 15:26:18 -0700529 }
530 useCache = true;
531 }
532
533 // check PECI client addresses and DT overlay names from CPU configuration
534 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700535 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700536 {
537 for (const std::pair<sdbusplus::message::object_path, SensorData>&
538 sensor : sensorConfigurations)
539 {
540 for (const std::pair<
541 std::string,
542 boost::container::flat_map<std::string, BasicVariantType>>&
543 config : sensor.second)
544 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700545 if ((configPrefix + std::string(type)) != config.first)
James Feist6714a252018-09-10 15:26:18 -0700546 {
547 continue;
548 }
549
James Feist6714a252018-09-10 15:26:18 -0700550 auto findName = config.second.find("Name");
551 if (findName == config.second.end())
552 {
553 continue;
554 }
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700555 std::string nameRaw = variant_ns::visit(
James Feist6714a252018-09-10 15:26:18 -0700556 VariantToStringVisitor(), findName->second);
557 std::string name =
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700558 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700559
560 auto findBus = config.second.find("Bus");
561 if (findBus == config.second.end())
562 {
563 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
564 continue;
565 }
566 uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(),
567 findBus->second);
568
569 auto findAddress = config.second.find("Address");
570 if (findAddress == config.second.end())
571 {
572 std::cerr << "Can't find 'Address' setting in " << name
573 << "\n";
574 continue;
575 }
576 uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(),
577 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700578
579 if (DEBUG)
580 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700581 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700582 std::cout << "addr: " << addr << "\n";
583 std::cout << "name: " << name << "\n";
584 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700585 }
586
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700587 configs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700588 }
589 }
590 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700591
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700592 if (configs.size())
593 {
594 std::cout << "CPU config" << (configs.size() == 1 ? " is" : "s are")
595 << " parsed\n";
596 return true;
597 }
598
599 return false;
James Feist6714a252018-09-10 15:26:18 -0700600}
601
602int main(int argc, char** argv)
603{
604 boost::asio::io_service io;
605 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
606 boost::container::flat_set<CPUConfig> configs;
607
608 systemBus->request_name("xyz.openbmc_project.CPUSensor");
609 sdbusplus::asio::object_server objectServer(systemBus);
610 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> sensors;
611 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
612 boost::asio::deadline_timer pingTimer(io);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700613 boost::asio::deadline_timer creationTimer(io);
James Feist6714a252018-09-10 15:26:18 -0700614 boost::asio::deadline_timer filterTimer(io);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700615
616 filterTimer.expires_from_now(boost::posix_time::seconds(1));
617 filterTimer.async_wait([&](const boost::system::error_code& ec) {
618 if (ec == boost::asio::error::operation_aborted)
619 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700620 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700621 }
622
623 if (getCpuConfig(systemBus, configs))
624 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700625 detectCpuAsync(pingTimer, creationTimer, io, objectServer, sensors,
626 configs, systemBus);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700627 }
628 });
629
James Feist6714a252018-09-10 15:26:18 -0700630 std::function<void(sdbusplus::message::message&)> eventHandler =
631 [&](sdbusplus::message::message& message) {
632 if (message.is_method_error())
633 {
634 std::cerr << "callback method error\n";
635 return;
636 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700637
638 if (DEBUG)
639 {
640 std::cout << message.get_path() << " is changed\n";
641 }
642
James Feist6714a252018-09-10 15:26:18 -0700643 // this implicitly cancels the timer
644 filterTimer.expires_from_now(boost::posix_time::seconds(1));
James Feist6714a252018-09-10 15:26:18 -0700645 filterTimer.async_wait([&](const boost::system::error_code& ec) {
646 if (ec == boost::asio::error::operation_aborted)
647 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700648 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700649 }
650
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700651 if (getCpuConfig(systemBus, configs))
James Feist6714a252018-09-10 15:26:18 -0700652 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700653 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
654 sensors, configs, systemBus);
James Feist6714a252018-09-10 15:26:18 -0700655 }
656 });
657 };
658
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700659 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700660 {
661 auto match = std::make_unique<sdbusplus::bus::match::match>(
662 static_cast<sdbusplus::bus::bus&>(*systemBus),
663 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700664 std::string(inventoryPath) + "',arg0namespace='" +
665 configPrefix + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700666 eventHandler);
667 matches.emplace_back(std::move(match));
668 }
669
670 io.run();
671}