blob: ba36e4152e9b7bab17701ab1225f3feb878064bb [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IntelCPUSensor.hpp"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <fcntl.h>
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +010022#include <peci.h>
James Feist6714a252018-09-10 15:26:18 -070023
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29#include <sdbusplus/bus/match.hpp>
30
31#include <array>
Jakub Nowackia38e5612023-03-08 12:27:01 +010032#include <cerrno>
James Feist24f02f22019-04-15 11:05:39 -070033#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070034#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <functional>
36#include <memory>
James Feist6714a252018-09-10 15:26:18 -070037#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <sstream>
39#include <stdexcept>
40#include <string>
41#include <utility>
42#include <variant>
43#include <vector>
James Feist6714a252018-09-10 15:26:18 -070044
James Feistf87dc4c2018-12-05 14:39:51 -080045// clang-format off
46// this needs to be included last or we'll have build issues
47#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080048#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
49#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
50#endif
James Feistf87dc4c2018-12-05 14:39:51 -080051// clang-format on
52
Ed Tanous8a57ec02020-10-09 12:46:52 -070053static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070054
Thu Nguyen255da6b2022-07-29 10:05:52 +070055boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
56 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080057boost::container::flat_map<std::string,
58 std::shared_ptr<sdbusplus::asio::dbus_interface>>
59 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080060
James Feist6714a252018-09-10 15:26:18 -070061enum State
62{
63 OFF, // host powered down
64 ON, // host powered on
65 READY // host powered on and mem test passed - fully ready
66};
67
68struct CPUConfig
69{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070070 CPUConfig(const uint64_t& bus, const uint64_t& addr,
71 const std::string& name, const State& state) :
72 bus(bus),
73 addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070074 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070075 int bus;
James Feist6714a252018-09-10 15:26:18 -070076 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070077 std::string name;
James Feist6714a252018-09-10 15:26:18 -070078 State state;
79
80 bool operator<(const CPUConfig& rhs) const
81 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093082 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070083 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070084 }
85};
86
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070087static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -080088static constexpr const char* peciDevPath = "/sys/bus/peci/devices/";
Paul Fertser6e699732023-03-29 12:58:30 +000089static constexpr const char* rescanPath = "/sys/bus/peci/rescan";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -070091
James Feistcf3bce62019-01-08 10:07:19 -080092namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080093
Zev Weiss054aad82022-08-18 01:37:34 -070094static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -080095static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +070096 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -070097
Jae Hyun Yood64262b2018-11-01 13:31:16 -070098void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -070099 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800100 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700101 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800102 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800103 boost::container::flat_set<CPUConfig>& cpuConfigs,
104 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700105
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200106std::string createSensorName(const std::string& label, const std::string& item,
107 const int& cpuId)
108{
109 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700110 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200111 {
112 sensorName += " " + item;
113 }
114 sensorName += " CPU" + std::to_string(cpuId);
115 // converting to Upper Camel case whole name
116 bool isWordEnd = true;
117 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
118 [&isWordEnd](int c) {
Ed Tanous2049bd22022-07-09 07:20:26 -0700119 if (std::isspace(c) != 0)
Ed Tanousbb679322022-05-16 16:10:00 -0700120 {
121 isWordEnd = true;
122 }
123 else
124 {
125 if (isWordEnd)
126 {
127 isWordEnd = false;
128 return std::toupper(c);
129 }
130 }
131 return c;
132 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200133 return sensorName;
134}
135
Ed Tanous1f978632023-02-28 18:16:39 -0800136bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800137 sdbusplus::asio::object_server& objectServer,
138 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
139 boost::container::flat_set<CPUConfig>& cpuConfigs,
140 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700141{
142 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800143 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700144 {
145 if (cpu.state != State::OFF)
146 {
147 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500148 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800149 inventoryIfaces[cpu.name];
150 if (iface != nullptr)
151 {
152 continue;
153 }
154 iface = objectServer.add_interface(
155 cpuInventoryPath + std::string("/") + cpu.name,
156 "xyz.openbmc_project.Inventory.Item");
157 iface->register_property("PrettyName", cpu.name);
158 iface->register_property("Present", true);
159 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 }
162 if (!available)
163 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700164 return false;
James Feist6714a252018-09-10 15:26:18 -0700165 }
166
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800167 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700168 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800169 return false;
James Feist6714a252018-09-10 15:26:18 -0700170 }
171
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700172 std::vector<fs::path> hwmonNamePaths;
Paul Fertser6e699732023-03-29 12:58:30 +0000173 findFiles(fs::path(peciDevPath),
174 R"(peci-\d+/\d+-.+/peci[-_].+/hwmon/hwmon\d+/name$)",
175 hwmonNamePaths, 6);
176 if (hwmonNamePaths.empty())
James Feist6714a252018-09-10 15:26:18 -0700177 {
178 std::cerr << "No CPU sensors in system\n";
Paul Fertser75f92582023-03-29 12:48:07 +0000179 return false;
James Feist6714a252018-09-10 15:26:18 -0700180 }
181
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700182 boost::container::flat_set<std::string> scannedDirectories;
183 boost::container::flat_set<std::string> createdSensors;
184
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800185 for (const fs::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700186 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700187 auto hwmonDirectory = hwmonNamePath.parent_path();
188
189 auto ret = scannedDirectories.insert(hwmonDirectory.string());
190 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700191 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700192 continue; // already searched this path
193 }
194
195 fs::path::iterator it = hwmonNamePath.begin();
196 std::advance(it, 6); // pick the 6th part for a PECI client device name
197 std::string deviceName = *it;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700198 auto findHyphen = deviceName.find('-');
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700199 if (findHyphen == std::string::npos)
200 {
201 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700202 continue;
203 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700204 std::string busStr = deviceName.substr(0, findHyphen);
205 std::string addrStr = deviceName.substr(findHyphen + 1);
206
207 size_t bus = 0;
208 size_t addr = 0;
209 try
210 {
211 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700212 addr = std::stoi(addrStr, nullptr, 16);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 }
Patrick Williams26601e82021-10-06 12:43:25 -0500214 catch (const std::invalid_argument&)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700215 {
216 continue;
217 }
218
219 std::ifstream nameFile(hwmonNamePath);
220 if (!nameFile.good())
221 {
222 std::cerr << "Failure reading " << hwmonNamePath << "\n";
223 continue;
224 }
225 std::string hwmonName;
226 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700227 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800228 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700229 {
230 // shouldn't have an empty name file
231 continue;
232 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700233 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700234 {
235 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
236 << "\n";
237 }
James Feist6714a252018-09-10 15:26:18 -0700238
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700239 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700240 const SensorData* sensorData = nullptr;
241 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700242 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700243
Zev Weiss8908b3c2022-08-12 18:21:01 -0700244 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700245 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700246 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700247 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700248 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700249 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700250 auto sensorBase =
251 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700252 if (sensorBase != sensorData->end())
253 {
254 baseConfiguration = &(*sensorBase);
255 break;
256 }
257 }
258 if (baseConfiguration == nullptr)
259 {
260 std::cerr << "error finding base configuration for" << hwmonName
261 << "\n";
262 continue;
263 }
264 auto configurationBus = baseConfiguration->second.find("Bus");
265 auto configurationAddress =
266 baseConfiguration->second.find("Address");
267
268 if (configurationBus == baseConfiguration->second.end() ||
269 configurationAddress == baseConfiguration->second.end())
270 {
271 std::cerr << "error finding bus or address in configuration";
272 continue;
273 }
274
James Feist3eb82622019-02-08 13:10:22 -0800275 if (std::get<uint64_t>(configurationBus->second) != bus ||
276 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700277 {
278 continue;
279 }
280
Zev Weiss8908b3c2022-08-12 18:21:01 -0700281 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700282 break;
283 }
284 if (interfacePath == nullptr)
285 {
286 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700287 continue;
288 }
289
290 auto findCpuId = baseConfiguration->second.find("CpuID");
291 if (findCpuId == baseConfiguration->second.end())
292 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700293 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700294 continue;
295 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500296 int cpuId = std::visit(VariantToUnsignedIntVisitor(),
297 findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700298
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700299 auto directory = hwmonNamePath.parent_path();
James Feist6714a252018-09-10 15:26:18 -0700300 std::vector<fs::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200301 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200302 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700303 {
304 std::cerr << "No temperature sensors in system\n";
305 continue;
306 }
307
308 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800309 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700310 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200311 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200312 if (!fileParts)
313 {
314 continue;
315 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200316 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700317 auto inputPathStr = inputPath.string();
Patrick Williams779c96a2023-05-10 07:50:42 -0500318 auto labelPath = boost::replace_all_copy(inputPathStr, item,
319 "label");
James Feist6714a252018-09-10 15:26:18 -0700320 std::ifstream labelFile(labelPath);
321 if (!labelFile.good())
322 {
323 std::cerr << "Failure reading " << labelPath << "\n";
324 continue;
325 }
326 std::string label;
327 std::getline(labelFile, label);
328 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800329
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200330 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700331
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800332 auto findSensor = gCpuSensors.find(sensorName);
333 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700334 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700335 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700336 {
337 std::cout << "Skipped: " << inputPath << ": " << sensorName
338 << " is already created\n";
339 }
340 continue;
341 }
342
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800343 // check hidden properties
344 bool show = true;
345 for (const char* prop : hiddenProps)
346 {
347 if (label == prop)
348 {
349 show = false;
350 break;
351 }
352 }
353
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700354 /*
355 * Find if there is DtsCritOffset is configured in config file
356 * set it if configured or else set it to 0
357 */
358 double dtsOffset = 0;
359 if (label == "DTS")
360 {
361 auto findThrOffset =
362 baseConfiguration->second.find("DtsCritOffset");
363 if (findThrOffset != baseConfiguration->second.end())
364 {
365 dtsOffset = std::visit(VariantToDoubleVisitor(),
366 findThrOffset->second);
367 }
368 }
369
James Feist6714a252018-09-10 15:26:18 -0700370 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700371 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700372 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700373 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800374 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700375 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700376 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700377 IntelCPUSensor::sensorScaleFactor,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700378 dtsOffset))
James Feist6714a252018-09-10 15:26:18 -0700379 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700380 std::cerr << "error populating thresholds for "
381 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700382 }
383 }
James Feistc140e202019-11-14 15:23:51 -0800384 auto& sensorPtr = gCpuSensors[sensorName];
385 // make sure destructor fires before creating a new one
386 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700387 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700388 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800389 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700390 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530391 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700392 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700393 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700394 {
James Feist6714a252018-09-10 15:26:18 -0700395 std::cout << "Mapped: " << inputPath << " to " << sensorName
396 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700397 }
James Feist6714a252018-09-10 15:26:18 -0700398 }
399 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700400
Ed Tanous2049bd22022-07-09 07:20:26 -0700401 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700402 {
403 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
404 << " created\n";
405 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700406
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700407 return true;
James Feist6714a252018-09-10 15:26:18 -0700408}
409
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800410bool exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700411{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700412 std::ostringstream hex;
413 hex << std::hex << config.addr;
414 const std::string& addrHexStr = hex.str();
415 std::string busStr = std::to_string(config.bus);
416
417 std::string parameters = "peci-client 0x" + addrHexStr;
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800418 std::string devPath = peciDevPath;
419 std::string delDevice = devPath + "peci-" + busStr + "/delete_device";
420 std::string newDevice = devPath + "peci-" + busStr + "/new_device";
421 std::string newClient = devPath + busStr + "-" + addrHexStr + "/driver";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700422
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800423 std::filesystem::path devicePath(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700424 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800425 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700426 {
James Feistcf3bce62019-01-08 10:07:19 -0800427 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700428 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700429 continue;
James Feist6714a252018-09-10 15:26:18 -0700430 }
431
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700432 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700433 if (directoryName.starts_with(busStr) &&
434 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700435 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700436 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700437 {
438 std::cout << parameters << " on bus " << busStr
439 << " is already exported\n";
440 }
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800441
442 std::ofstream delDeviceFile(delDevice);
443 if (!delDeviceFile.good())
444 {
445 std::cerr << "Error opening " << delDevice << "\n";
446 return false;
447 }
448 delDeviceFile << parameters;
449 delDeviceFile.close();
450
451 break;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700452 }
James Feist6714a252018-09-10 15:26:18 -0700453 }
454
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800455 std::ofstream deviceFile(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700456 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700457 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800458 std::cerr << "Error opening " << newDevice << "\n";
459 return false;
James Feist6714a252018-09-10 15:26:18 -0700460 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700461 deviceFile << parameters;
462 deviceFile.close();
463
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800464 if (!std::filesystem::exists(newClient))
465 {
466 std::cerr << "Error creating " << newClient << "\n";
467 return false;
468 }
469
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700470 std::cout << parameters << " on bus " << busStr << " is exported\n";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800471
472 return true;
James Feist6714a252018-09-10 15:26:18 -0700473}
474
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700475void detectCpu(boost::asio::steady_timer& pingTimer,
476 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800477 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800478 sdbusplus::asio::object_server& objectServer,
479 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
480 boost::container::flat_set<CPUConfig>& cpuConfigs,
481 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700482{
James Feist6714a252018-09-10 15:26:18 -0700483 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700484 static bool keepPinging = false;
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200485 int peciFd = -1;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700486
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800487 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700488 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800489 if (config.state == State::READY)
490 {
491 continue;
492 }
493
Paul Fertser6e699732023-03-29 12:58:30 +0000494 std::fstream rescan{rescanPath, std::ios::out};
495 if (rescan.is_open())
496 {
497 std::vector<fs::path> peciPaths;
498 std::ostringstream searchPath;
499 searchPath << std::hex << "peci-" << config.bus << "/" << config.bus
500 << "-" << config.addr;
501 findFiles(fs::path(peciDevPath + searchPath.str()),
502 R"(peci_cpu.dimmtemp.+/hwmon/hwmon\d+/name$)", peciPaths,
503 3);
504 if (!peciPaths.empty())
505 {
506 config.state = State::READY;
507 rescanDelaySeconds = 1;
508 }
509 else
510 {
511 findFiles(fs::path(peciDevPath + searchPath.str()),
512 R"(peci_cpu.cputemp.+/hwmon/hwmon\d+/name$)",
513 peciPaths, 3);
514 if (!peciPaths.empty())
515 {
516 config.state = State::ON;
517 rescanDelaySeconds = 3;
518 }
519 else
520 {
521 // https://www.kernel.org/doc/html/latest/admin-guide/abi-testing.html#abi-sys-bus-peci-rescan
522 rescan << "1";
523 }
524 }
525 if (config.state != State::READY)
526 {
527 keepPinging = true;
528 }
529
530 continue;
531 }
532
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700533 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800534
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200535 peci_SetDevName(peciDevPath.data());
536
Ed Tanous99c44092022-01-14 09:59:09 -0800537 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200538 if ((peci_Lock(&peciFd, PECI_NO_WAIT) != PECI_CC_SUCCESS) ||
539 (peciFd < 0))
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700540 {
Jakub Nowackia38e5612023-03-08 12:27:01 +0100541 std::cerr << "unable to open " << peciDevPath << " "
542 << std::strerror(errno) << "\n";
543 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
544 dbusConnection, cpuConfigs, sensorConfigs);
545 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700546 }
547
Ed Tanousa771f6a2022-01-14 09:36:51 -0800548 State newState = State::OFF;
Ed Tanous99c44092022-01-14 09:59:09 -0800549
550 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100551 if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700552 {
553 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700554 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700555 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100556 std::array<uint8_t, 8> pkgConfig{};
557 uint8_t cc = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800558
559 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100560 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_DDR_DIMM_TEMP,
561 rank, 4, &pkgConfig[0],
562 &cc) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700563 {
Matt Simmering38857572023-08-10 14:41:46 -0700564 // Depending on CPU generation, both 0 and 0xFF can be used
565 // to indicate no DIMM presence
566 if (((pkgConfig[0] != 0xFF) && (pkgConfig[0] != 0U)) ||
567 ((pkgConfig[1] != 0xFF) && (pkgConfig[1] != 0U)))
James Feist6714a252018-09-10 15:26:18 -0700568 {
569 dimmReady = true;
570 break;
571 }
572 }
573 else
574 {
575 break;
576 }
577 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700578
James Feist6714a252018-09-10 15:26:18 -0700579 if (dimmReady)
580 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700581 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700582 }
583 else
584 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700585 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700586 }
587 }
James Feist6714a252018-09-10 15:26:18 -0700588
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700589 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700590 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700591 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700592 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700593 if (config.state == State::OFF)
594 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800595 std::array<uint8_t, 8> pkgConfig{};
596 uint8_t cc = 0;
597
598 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_CPU_ID, 0,
599 4, &pkgConfig[0],
600 &cc) == PECI_CC_SUCCESS)
601 {
602 std::cout << config.name << " is detected\n";
603 if (!exportDevice(config))
604 {
605 newState = State::OFF;
606 }
607 }
608 else
609 {
610 newState = State::OFF;
611 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700612 }
James Feist6714a252018-09-10 15:26:18 -0700613
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700614 if (newState == State::ON)
615 {
616 rescanDelaySeconds = 3;
617 }
618 else if (newState == State::READY)
619 {
620 rescanDelaySeconds = 5;
621 std::cout << "DIMM(s) on " << config.name
622 << " is/are detected\n";
623 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700624 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700625
626 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700627 }
628
629 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700630 {
631 keepPinging = true;
632 }
633
Ed Tanous8a57ec02020-10-09 12:46:52 -0700634 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700635 {
636 std::cout << config.name << ", state: " << config.state << "\n";
637 }
Wang Xiaohua4cd5a902023-07-04 16:28:43 +0800638 peci_Unlock(peciFd);
James Feist6714a252018-09-10 15:26:18 -0700639 }
640
Ed Tanous2049bd22022-07-09 07:20:26 -0700641 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700642 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800643 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700644 creationTimer.async_wait([&](const boost::system::error_code& ec) {
645 if (ec == boost::asio::error::operation_aborted)
646 {
647 return; // we're being canceled
648 }
649
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800650 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700651 sensorConfigs) ||
652 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700653 {
654 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800655 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700656 }
657 });
James Feist6714a252018-09-10 15:26:18 -0700658 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700659 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700660 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800661 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800662 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700663 }
664}
665
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700666void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700667 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800668 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700669 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800670 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800671 boost::container::flat_set<CPUConfig>& cpuConfigs,
672 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700673{
Ed Tanous83db50c2023-03-01 10:20:24 -0800674 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700675 pingTimer.async_wait([&](const boost::system::error_code& ec) {
676 if (ec == boost::asio::error::operation_aborted)
677 {
678 return; // we're being canceled
679 }
680
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800681 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800682 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700683 });
684}
685
James Feistc140e202019-11-14 15:23:51 -0800686bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
687 boost::container::flat_set<CPUConfig>& cpuConfigs,
688 ManagedObjectType& sensorConfigs,
689 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700690{
James Feist6714a252018-09-10 15:26:18 -0700691 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800692 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700693 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700694 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700695 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700696 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700697 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700698 return false;
James Feist6714a252018-09-10 15:26:18 -0700699 }
700 useCache = true;
701 }
702
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700703 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700704 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700705 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700706 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700707 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700708 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700709 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700710 {
Zev Weiss054aad82022-08-18 01:37:34 -0700711 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700712 {
713 continue;
714 }
715
Zev Weiss8908b3c2022-08-12 18:21:01 -0700716 auto findName = cfg.find("Name");
717 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700718 {
719 continue;
720 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500721 std::string nameRaw = std::visit(VariantToStringVisitor(),
722 findName->second);
723 std::string name = std::regex_replace(nameRaw, illegalDbusRegex,
724 "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700725
James Feistc140e202019-11-14 15:23:51 -0800726 auto present = std::optional<bool>();
727 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700728 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700729 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700730 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700731 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700732 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700733 break;
James Feist58295ad2019-05-30 15:01:41 -0700734 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700735 }
736
James Feistc140e202019-11-14 15:23:51 -0800737 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
738 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700739 {
740 auto iface = objectServer.add_interface(
741 cpuInventoryPath + std::string("/") + name,
742 "xyz.openbmc_project.Inventory.Item");
743 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800744 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700745 iface->initialize();
746 inventoryIfaces[name] = std::move(iface);
747 }
James Feist58295ad2019-05-30 15:01:41 -0700748
Zev Weiss8908b3c2022-08-12 18:21:01 -0700749 auto findBus = cfg.find("Bus");
750 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700751 {
752 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
753 continue;
754 }
Patrick Williams779c96a2023-05-10 07:50:42 -0500755 uint64_t bus = std::visit(VariantToUnsignedIntVisitor(),
756 findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700757
Zev Weiss8908b3c2022-08-12 18:21:01 -0700758 auto findAddress = cfg.find("Address");
759 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700760 {
761 std::cerr << "Can't find 'Address' setting in " << name
762 << "\n";
763 continue;
764 }
James Feist3eb82622019-02-08 13:10:22 -0800765 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
766 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700767
Ed Tanous8a57ec02020-10-09 12:46:52 -0700768 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700769 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700770 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700771 std::cout << "addr: " << addr << "\n";
772 std::cout << "name: " << name << "\n";
773 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700774 }
775
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800776 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700777 }
778 }
779 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700780
Ed Tanous2049bd22022-07-09 07:20:26 -0700781 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700782 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800783 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700784 << " parsed\n";
785 return true;
786 }
787
788 return false;
James Feist6714a252018-09-10 15:26:18 -0700789}
790
James Feistb6c0b912019-07-09 12:21:44 -0700791int main()
James Feist6714a252018-09-10 15:26:18 -0700792{
Ed Tanous1f978632023-02-28 18:16:39 -0800793 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700794 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800795 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700796
Ed Tanous14ed5e92022-07-12 15:50:23 -0700797 sdbusplus::asio::object_server objectServer(systemBus, true);
798 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700799 boost::asio::steady_timer pingTimer(io);
800 boost::asio::steady_timer creationTimer(io);
801 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800802 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700803
Ed Tanous83db50c2023-03-01 10:20:24 -0800804 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700805 filterTimer.async_wait([&](const boost::system::error_code& ec) {
806 if (ec == boost::asio::error::operation_aborted)
807 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700808 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700809 }
810
James Feistc140e202019-11-14 15:23:51 -0800811 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700812 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800813 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800814 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700815 }
816 });
817
Patrick Williams92f8f512022-07-22 19:26:55 -0500818 std::function<void(sdbusplus::message_t&)> eventHandler =
819 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700820 if (message.is_method_error())
821 {
822 std::cerr << "callback method error\n";
823 return;
824 }
825
826 if (debug)
827 {
828 std::cout << message.get_path() << " is changed\n";
829 }
830
831 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800832 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700833 filterTimer.async_wait([&](const boost::system::error_code& ec) {
834 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700835 {
Ed Tanousbb679322022-05-16 16:10:00 -0700836 return; // we're being canceled
James Feist6714a252018-09-10 15:26:18 -0700837 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700838
Ed Tanousbb679322022-05-16 16:10:00 -0700839 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
840 objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700841 {
Ed Tanousbb679322022-05-16 16:10:00 -0700842 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
843 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700844 }
Ed Tanousbb679322022-05-16 16:10:00 -0700845 });
846 };
James Feist6714a252018-09-10 15:26:18 -0700847
Zev Weiss214d9712022-08-12 12:54:31 -0700848 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
849 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700850
Thu Nguyen255da6b2022-07-29 10:05:52 +0700851 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800852
853 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700854 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700855 return 0;
James Feist6714a252018-09-10 15:26:18 -0700856}