blob: 0ea131b87dc72e94b994358cf666a33569f9255e [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
17#include <fcntl.h>
James Feist6714a252018-09-10 15:26:18 -070018
19#include <CPUSensor.hpp>
20#include <Utils.hpp>
21#include <VariantVisitors.hpp>
22#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
26#include <boost/process/child.hpp>
27#include <experimental/filesystem>
28#include <fstream>
29#include <regex>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
James Feistf87dc4c2018-12-05 14:39:51 -080033// clang-format off
34// this needs to be included last or we'll have build issues
35#include <linux/peci-ioctl.h>
36// clang-format on
37
James Feist6714a252018-09-10 15:26:18 -070038static constexpr bool DEBUG = false;
39
40enum State
41{
42 OFF, // host powered down
43 ON, // host powered on
44 READY // host powered on and mem test passed - fully ready
45};
46
47struct CPUConfig
48{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070049 CPUConfig(const uint64_t& bus, const uint64_t& addr,
50 const std::string& name, const State& state) :
51 bus(bus),
52 addr(addr), name(name), state(state)
James Feist6714a252018-09-10 15:26:18 -070053 {
54 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070055 int bus;
James Feist6714a252018-09-10 15:26:18 -070056 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070057 std::string name;
James Feist6714a252018-09-10 15:26:18 -070058 State state;
59
60 bool operator<(const CPUConfig& rhs) const
61 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070062 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070063 }
64};
65
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070066static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070067static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070068
69namespace fs = std::experimental::filesystem;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070070namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071static constexpr const char* configPrefix =
James Feist6714a252018-09-10 15:26:18 -070072 "xyz.openbmc_project.Configuration.";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070073static constexpr std::array<const char*, 3> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070074 "SkylakeCPU", "BroadwellCPU", "HaswellCPU"};
75
Jae Hyun Yood64262b2018-11-01 13:31:16 -070076void detectCpuAsync(
77 boost::asio::deadline_timer& pingTimer,
78 boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io,
79 sdbusplus::asio::object_server& objectServer,
80 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
81 sensors,
82 boost::container::flat_set<CPUConfig>& configs,
83 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection);
84
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -070085bool createSensors(
James Feist6714a252018-09-10 15:26:18 -070086 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
87 boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>&
88 sensors,
89 boost::container::flat_set<CPUConfig>& configs,
90 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
91{
92 bool available = false;
93 for (CPUConfig cpu : configs)
94 {
95 if (cpu.state != State::OFF)
96 {
97 available = true;
98 break;
99 }
100 }
101 if (!available)
102 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700103 return false;
James Feist6714a252018-09-10 15:26:18 -0700104 }
105
106 // use new data the first time, then refresh
107 ManagedObjectType sensorConfigurations;
108 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700110 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 if (!getSensorConfiguration(configPrefix + std::string(type),
James Feist6714a252018-09-10 15:26:18 -0700112 dbusConnection, sensorConfigurations,
113 useCache))
114 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700115 return false;
James Feist6714a252018-09-10 15:26:18 -0700116 }
117 useCache = true;
118 }
119
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700120 std::vector<fs::path> hwmonNamePaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700121 if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"),
122 R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)",
123 hwmonNamePaths, 1))
James Feist6714a252018-09-10 15:26:18 -0700124 {
125 std::cerr << "No CPU sensors in system\n";
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700126 return true;
James Feist6714a252018-09-10 15:26:18 -0700127 }
128
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700129 boost::container::flat_set<std::string> scannedDirectories;
130 boost::container::flat_set<std::string> createdSensors;
131
132 for (fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700133 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700134 const std::string& pathStr = hwmonNamePath.string();
135 auto hwmonDirectory = hwmonNamePath.parent_path();
136
137 auto ret = scannedDirectories.insert(hwmonDirectory.string());
138 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700139 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700140 continue; // already searched this path
141 }
142
143 fs::path::iterator it = hwmonNamePath.begin();
144 std::advance(it, 6); // pick the 6th part for a PECI client device name
145 std::string deviceName = *it;
146 auto findHyphen = deviceName.find("-");
147 if (findHyphen == std::string::npos)
148 {
149 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700150 continue;
151 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700152 std::string busStr = deviceName.substr(0, findHyphen);
153 std::string addrStr = deviceName.substr(findHyphen + 1);
154
155 size_t bus = 0;
156 size_t addr = 0;
157 try
158 {
159 bus = std::stoi(busStr);
160 addr = std::stoi(addrStr, 0, 16);
161 }
162 catch (std::invalid_argument)
163 {
164 continue;
165 }
166
167 std::ifstream nameFile(hwmonNamePath);
168 if (!nameFile.good())
169 {
170 std::cerr << "Failure reading " << hwmonNamePath << "\n";
171 continue;
172 }
173 std::string hwmonName;
174 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700175 nameFile.close();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700176 if (!hwmonName.size())
James Feist6714a252018-09-10 15:26:18 -0700177 {
178 // shouldn't have an empty name file
179 continue;
180 }
James Feist6714a252018-09-10 15:26:18 -0700181 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700182 {
183 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
184 << "\n";
185 }
James Feist6714a252018-09-10 15:26:18 -0700186
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700187 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700188 const SensorData* sensorData = nullptr;
189 const std::string* interfacePath = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700190 const std::pair<std::string, boost::container::flat_map<
191 std::string, BasicVariantType>>*
192 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700193
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700194 for (const std::pair<sdbusplus::message::object_path, SensorData>&
195 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700196 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700197 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700198 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700199 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700200 sensorType = configPrefix + std::string(type);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700201 auto sensorBase = sensorData->find(sensorType);
202 if (sensorBase != sensorData->end())
203 {
204 baseConfiguration = &(*sensorBase);
205 break;
206 }
207 }
208 if (baseConfiguration == nullptr)
209 {
210 std::cerr << "error finding base configuration for" << hwmonName
211 << "\n";
212 continue;
213 }
214 auto configurationBus = baseConfiguration->second.find("Bus");
215 auto configurationAddress =
216 baseConfiguration->second.find("Address");
217
218 if (configurationBus == baseConfiguration->second.end() ||
219 configurationAddress == baseConfiguration->second.end())
220 {
221 std::cerr << "error finding bus or address in configuration";
222 continue;
223 }
224
225 if (sdbusplus::message::variant_ns::get<uint64_t>(
226 configurationBus->second) != bus ||
227 sdbusplus::message::variant_ns::get<uint64_t>(
228 configurationAddress->second) != addr)
229 {
230 continue;
231 }
232
233 interfacePath = &(sensor.first.str);
234 break;
235 }
236 if (interfacePath == nullptr)
237 {
238 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700239 continue;
240 }
241
242 auto findCpuId = baseConfiguration->second.find("CpuID");
243 if (findCpuId == baseConfiguration->second.end())
244 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700245 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700246 continue;
247 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 int cpuId =
249 variant_ns::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700250
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700251 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700252 std::vector<fs::path> inputPaths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700253 if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700254 {
255 std::cerr << "No temperature sensors in system\n";
256 continue;
257 }
258
259 // iterate through all found temp sensors
260 for (auto& inputPath : inputPaths)
261 {
262 auto inputPathStr = inputPath.string();
263 auto labelPath =
264 boost::replace_all_copy(inputPathStr, "input", "label");
265 std::ifstream labelFile(labelPath);
266 if (!labelFile.good())
267 {
268 std::cerr << "Failure reading " << labelPath << "\n";
269 continue;
270 }
271 std::string label;
272 std::getline(labelFile, label);
273 labelFile.close();
274 std::string sensorName = label + " CPU" + std::to_string(cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700275
276 auto findSensor = sensors.find(sensorName);
277 if (findSensor != sensors.end())
278 {
279 if (DEBUG)
280 {
281 std::cout << "Skipped: " << inputPath << ": " << sensorName
282 << " is already created\n";
283 }
284 continue;
285 }
286
James Feist6714a252018-09-10 15:26:18 -0700287 std::vector<thresholds::Threshold> sensorThresholds;
288 std::string labelHead = label.substr(0, label.find(" "));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700289 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700290 &labelHead);
James Feist6714a252018-09-10 15:26:18 -0700291 if (!sensorThresholds.size())
292 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700293 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
294 CPUSensor::sensorScaleFactor))
James Feist6714a252018-09-10 15:26:18 -0700295 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700296 std::cerr << "error populating thresholds for "
297 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700298 }
299 }
300 sensors[sensorName] = std::make_unique<CPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700301 inputPathStr, sensorType, objectServer, dbusConnection, io,
302 sensorName, std::move(sensorThresholds), *interfacePath);
303 createdSensors.insert(sensorName);
James Feist6714a252018-09-10 15:26:18 -0700304 if (DEBUG)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700305 {
James Feist6714a252018-09-10 15:26:18 -0700306 std::cout << "Mapped: " << inputPath << " to " << sensorName
307 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700308 }
James Feist6714a252018-09-10 15:26:18 -0700309 }
310 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700311
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700312 if (createdSensors.size())
313 {
314 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
315 << " created\n";
316 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700317
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700318 return true;
James Feist6714a252018-09-10 15:26:18 -0700319}
320
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700321void exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700322{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700323 std::ostringstream hex;
324 hex << std::hex << config.addr;
325 const std::string& addrHexStr = hex.str();
326 std::string busStr = std::to_string(config.bus);
327
328 std::string parameters = "peci-client 0x" + addrHexStr;
329 std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device";
330
331 std::experimental::filesystem::path devicePath(device);
332 const std::string& dir = devicePath.parent_path().string();
333 for (const auto& path :
334 std::experimental::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700335 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700336 if (!std::experimental::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}