blob: be88a0dfc0929e14823cdba8667536c99d5415b4 [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070018#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103019#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
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>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/error.hpp>
26#include <boost/asio/io_context.hpp>
27#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070029#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/message.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <algorithm>
James Feist38fb5982020-05-28 10:09:54 -070036#include <array>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070037#include <cctype>
Jakub Nowackia38e5612023-03-08 12:27:01 +010038#include <cerrno>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <chrono>
40#include <cstddef>
41#include <cstdint>
42#include <cstring>
James Feist24f02f22019-04-15 11:05:39 -070043#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070044#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070045#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <ios>
47#include <iostream>
48#include <iterator>
Patrick Venture96e97db2019-10-31 13:44:38 -070049#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070050#include <optional>
James Feist6714a252018-09-10 15:26:18 -070051#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070052#include <sstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070053#include <string>
54#include <utility>
55#include <variant>
56#include <vector>
James Feist6714a252018-09-10 15:26:18 -070057
James Feistf87dc4c2018-12-05 14:39:51 -080058// clang-format off
59// this needs to be included last or we'll have build issues
60#include <linux/peci-ioctl.h>
Jae Hyun Yoo201c8d92019-02-27 15:41:56 -080061#if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP)
62#define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP
63#endif
James Feistf87dc4c2018-12-05 14:39:51 -080064// clang-format on
65
Ed Tanous8a57ec02020-10-09 12:46:52 -070066static constexpr bool debug = false;
James Feist6714a252018-09-10 15:26:18 -070067
Thu Nguyen255da6b2022-07-29 10:05:52 +070068boost::container::flat_map<std::string, std::shared_ptr<IntelCPUSensor>>
69 gCpuSensors;
James Feistc140e202019-11-14 15:23:51 -080070boost::container::flat_map<std::string,
71 std::shared_ptr<sdbusplus::asio::dbus_interface>>
72 inventoryIfaces;
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -080073
James Feist6714a252018-09-10 15:26:18 -070074enum State
75{
76 OFF, // host powered down
77 ON, // host powered on
78 READY // host powered on and mem test passed - fully ready
79};
80
81struct CPUConfig
82{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070083 CPUConfig(const uint64_t& bus, const uint64_t& addr,
84 const std::string& name, const State& state) :
Patrick Williams2aaf7172024-08-16 15:20:40 -040085 bus(bus), addr(addr), name(name), state(state)
James Feist38fb5982020-05-28 10:09:54 -070086 {}
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070087 int bus;
James Feist6714a252018-09-10 15:26:18 -070088 int addr;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070089 std::string name;
James Feist6714a252018-09-10 15:26:18 -070090 State state;
91
92 bool operator<(const CPUConfig& rhs) const
93 {
Andrew Jeffery92b96292021-05-27 16:41:31 +093094 // NOLINTNEXTLINE
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -070095 return (name < rhs.name);
James Feist6714a252018-09-10 15:26:18 -070096 }
97};
98
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -070099static constexpr const char* peciDev = "/dev/peci-";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800100static constexpr const char* peciDevPath = "/sys/bus/peci/devices/";
Paul Fertser6e699732023-03-29 12:58:30 +0000101static constexpr const char* rescanPath = "/sys/bus/peci/rescan";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102static constexpr const unsigned int rankNumMax = 8;
James Feist6714a252018-09-10 15:26:18 -0700103
Zev Weiss054aad82022-08-18 01:37:34 -0700104static constexpr auto sensorTypes{std::to_array<const char*>({"XeonCPU"})};
Brandon Kim66558232021-11-09 16:53:08 -0800105static constexpr auto hiddenProps{std::to_array<const char*>(
Thu Nguyen255da6b2022-07-29 10:05:52 +0700106 {IntelCPUSensor::labelTcontrol, "Tthrottle", "Tjmax"})};
James Feist6714a252018-09-10 15:26:18 -0700107
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700108void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700109 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800110 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700111 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800112 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800113 boost::container::flat_set<CPUConfig>& cpuConfigs,
114 ManagedObjectType& sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700115
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200116std::string createSensorName(const std::string& label, const std::string& item,
117 const int& cpuId)
118{
119 std::string sensorName = label;
Ed Tanous2049bd22022-07-09 07:20:26 -0700120 if (item != "input")
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200121 {
122 sensorName += " " + item;
123 }
PavanKumarIntel40baf772024-07-03 14:32:57 +0000124
125 std::string cpuStr = "CPU" + std::to_string(cpuId);
126 constexpr const char* subLabel = "DIMM";
127 std::size_t found = label.find(subLabel);
128 if (found != std::string::npos)
129 {
130 sensorName = cpuStr + " " + sensorName;
131 }
132 else
133 {
134 sensorName += " " + cpuStr;
135 }
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200136 // converting to Upper Camel case whole name
137 bool isWordEnd = true;
138 std::transform(sensorName.begin(), sensorName.end(), sensorName.begin(),
139 [&isWordEnd](int c) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400140 if (std::isspace(c) != 0)
141 {
142 isWordEnd = true;
143 }
144 else
145 {
146 if (isWordEnd)
147 {
148 isWordEnd = false;
149 return std::toupper(c);
150 }
151 }
152 return c;
153 });
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200154 return sensorName;
155}
156
Ed Tanous1f978632023-02-28 18:16:39 -0800157bool createSensors(boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800158 sdbusplus::asio::object_server& objectServer,
159 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
160 boost::container::flat_set<CPUConfig>& cpuConfigs,
161 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700162{
163 bool available = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800164 for (const CPUConfig& cpu : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700165 {
166 if (cpu.state != State::OFF)
167 {
168 available = true;
Zev Weiss9702c9d2021-04-21 22:41:51 -0500169 std::shared_ptr<sdbusplus::asio::dbus_interface>& iface =
James Feistc140e202019-11-14 15:23:51 -0800170 inventoryIfaces[cpu.name];
171 if (iface != nullptr)
172 {
173 continue;
174 }
175 iface = objectServer.add_interface(
176 cpuInventoryPath + std::string("/") + cpu.name,
177 "xyz.openbmc_project.Inventory.Item");
178 iface->register_property("PrettyName", cpu.name);
179 iface->register_property("Present", true);
180 iface->initialize();
James Feist6714a252018-09-10 15:26:18 -0700181 }
182 }
183 if (!available)
184 {
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700185 return false;
James Feist6714a252018-09-10 15:26:18 -0700186 }
187
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800188 if (sensorConfigs.empty())
James Feist6714a252018-09-10 15:26:18 -0700189 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800190 return false;
James Feist6714a252018-09-10 15:26:18 -0700191 }
192
Ed Tanous2e466962025-01-30 10:59:49 -0800193 std::vector<std::filesystem::path> hwmonNamePaths;
194 findFiles(std::filesystem::path(peciDevPath),
Paul Fertser6e699732023-03-29 12:58:30 +0000195 R"(peci-\d+/\d+-.+/peci[-_].+/hwmon/hwmon\d+/name$)",
196 hwmonNamePaths, 6);
197 if (hwmonNamePaths.empty())
James Feist6714a252018-09-10 15:26:18 -0700198 {
199 std::cerr << "No CPU sensors in system\n";
Paul Fertser75f92582023-03-29 12:48:07 +0000200 return false;
James Feist6714a252018-09-10 15:26:18 -0700201 }
202
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700203 boost::container::flat_set<std::string> scannedDirectories;
204 boost::container::flat_set<std::string> createdSensors;
205
Ed Tanous2e466962025-01-30 10:59:49 -0800206 for (const std::filesystem::path& hwmonNamePath : hwmonNamePaths)
James Feist6714a252018-09-10 15:26:18 -0700207 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700208 auto hwmonDirectory = hwmonNamePath.parent_path();
209
210 auto ret = scannedDirectories.insert(hwmonDirectory.string());
211 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -0700212 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700213 continue; // already searched this path
214 }
215
Ed Tanous2e466962025-01-30 10:59:49 -0800216 std::filesystem::path::iterator it = hwmonNamePath.begin();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700217 std::advance(it, 6); // pick the 6th part for a PECI client device name
218 std::string deviceName = *it;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700219
220 size_t bus = 0;
221 size_t addr = 0;
Akshit Shah03d333e2023-08-23 22:14:28 +0000222 if (!getDeviceBusAddr(deviceName, bus, addr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700223 {
224 continue;
225 }
226
227 std::ifstream nameFile(hwmonNamePath);
228 if (!nameFile.good())
229 {
230 std::cerr << "Failure reading " << hwmonNamePath << "\n";
231 continue;
232 }
233 std::string hwmonName;
234 std::getline(nameFile, hwmonName);
James Feist6714a252018-09-10 15:26:18 -0700235 nameFile.close();
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800236 if (hwmonName.empty())
James Feist6714a252018-09-10 15:26:18 -0700237 {
238 // shouldn't have an empty name file
239 continue;
240 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700241 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700242 {
243 std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName
244 << "\n";
245 }
James Feist6714a252018-09-10 15:26:18 -0700246
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700247 std::string sensorType;
James Feist6714a252018-09-10 15:26:18 -0700248 const SensorData* sensorData = nullptr;
249 const std::string* interfacePath = nullptr;
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700250 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700251
Zev Weiss8908b3c2022-08-12 18:21:01 -0700252 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700253 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700254 sensorData = &cfgData;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700255 for (const char* type : sensorTypes)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700256 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700257 sensorType = type;
Zev Weiss054aad82022-08-18 01:37:34 -0700258 auto sensorBase =
259 sensorData->find(configInterfaceName(sensorType));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700260 if (sensorBase != sensorData->end())
261 {
262 baseConfiguration = &(*sensorBase);
263 break;
264 }
265 }
266 if (baseConfiguration == nullptr)
267 {
268 std::cerr << "error finding base configuration for" << hwmonName
269 << "\n";
270 continue;
271 }
272 auto configurationBus = baseConfiguration->second.find("Bus");
273 auto configurationAddress =
274 baseConfiguration->second.find("Address");
275
276 if (configurationBus == baseConfiguration->second.end() ||
277 configurationAddress == baseConfiguration->second.end())
278 {
279 std::cerr << "error finding bus or address in configuration";
280 continue;
281 }
282
James Feist3eb82622019-02-08 13:10:22 -0800283 if (std::get<uint64_t>(configurationBus->second) != bus ||
284 std::get<uint64_t>(configurationAddress->second) != addr)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700285 {
286 continue;
287 }
288
Zev Weiss8908b3c2022-08-12 18:21:01 -0700289 interfacePath = &path.str;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700290 break;
291 }
292 if (interfacePath == nullptr)
293 {
294 std::cerr << "failed to find match for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700295 continue;
296 }
297
298 auto findCpuId = baseConfiguration->second.find("CpuID");
299 if (findCpuId == baseConfiguration->second.end())
300 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700301 std::cerr << "could not determine CPU ID for " << hwmonName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700302 continue;
303 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400304 int cpuId =
305 std::visit(VariantToUnsignedIntVisitor(), findCpuId->second);
James Feist6714a252018-09-10 15:26:18 -0700306
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700307 auto directory = hwmonNamePath.parent_path();
Ed Tanous2e466962025-01-30 10:59:49 -0800308 std::vector<std::filesystem::path> inputPaths;
Zbigniew Kurzynski8d8d8d72020-05-29 19:21:24 +0200309 if (!findFiles(directory, R"((temp|power)\d+_(input|average|cap)$)",
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200310 inputPaths, 0))
James Feist6714a252018-09-10 15:26:18 -0700311 {
312 std::cerr << "No temperature sensors in system\n";
313 continue;
314 }
315
316 // iterate through all found temp sensors
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800317 for (const auto& inputPath : inputPaths)
James Feist6714a252018-09-10 15:26:18 -0700318 {
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200319 auto fileParts = splitFileName(inputPath);
Zbigniew Kurzynski0a4c4802020-04-01 11:22:27 +0200320 if (!fileParts)
321 {
322 continue;
323 }
Zbigniew Kurzynskidbfd4662020-09-28 18:06:00 +0200324 auto& [type, nr, item] = *fileParts;
James Feist6714a252018-09-10 15:26:18 -0700325 auto inputPathStr = inputPath.string();
Patrick Williams2aaf7172024-08-16 15:20:40 -0400326 auto labelPath =
327 boost::replace_all_copy(inputPathStr, item, "label");
James Feist6714a252018-09-10 15:26:18 -0700328 std::ifstream labelFile(labelPath);
329 if (!labelFile.good())
330 {
331 std::cerr << "Failure reading " << labelPath << "\n";
332 continue;
333 }
334 std::string label;
335 std::getline(labelFile, label);
336 labelFile.close();
Jae Hyun Yoo13f48882019-02-19 13:37:07 -0800337
Zbigniew Kurzynski0eee0c12020-06-18 14:20:08 +0200338 std::string sensorName = createSensorName(label, item, cpuId);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700339
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800340 auto findSensor = gCpuSensors.find(sensorName);
341 if (findSensor != gCpuSensors.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700342 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700343 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700344 {
345 std::cout << "Skipped: " << inputPath << ": " << sensorName
346 << " is already created\n";
347 }
348 continue;
349 }
350
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800351 // check hidden properties
352 bool show = true;
353 for (const char* prop : hiddenProps)
354 {
355 if (label == prop)
356 {
357 show = false;
358 break;
359 }
360 }
361
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700362 /*
363 * Find if there is DtsCritOffset is configured in config file
364 * set it if configured or else set it to 0
365 */
366 double dtsOffset = 0;
367 if (label == "DTS")
368 {
369 auto findThrOffset =
370 baseConfiguration->second.find("DtsCritOffset");
371 if (findThrOffset != baseConfiguration->second.end())
372 {
373 dtsOffset = std::visit(VariantToDoubleVisitor(),
374 findThrOffset->second);
375 }
376 }
377
James Feist6714a252018-09-10 15:26:18 -0700378 std::vector<thresholds::Threshold> sensorThresholds;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700379 std::string labelHead = label.substr(0, label.find(' '));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700380 parseThresholdsFromConfig(*sensorData, sensorThresholds,
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700381 &labelHead);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800382 if (sensorThresholds.empty())
James Feist6714a252018-09-10 15:26:18 -0700383 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700384 if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr,
Thu Nguyen255da6b2022-07-29 10:05:52 +0700385 IntelCPUSensor::sensorScaleFactor,
Chris Sidesa3279232023-04-24 16:08:13 -0500386 dtsOffset, 0))
James Feist6714a252018-09-10 15:26:18 -0700387 {
Yoo, Jae Hyun81a464c2018-10-09 16:38:58 -0700388 std::cerr << "error populating thresholds for "
389 << sensorName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700390 }
391 }
James Feistc140e202019-11-14 15:23:51 -0800392 auto& sensorPtr = gCpuSensors[sensorName];
393 // make sure destructor fires before creating a new one
394 sensorPtr = nullptr;
Thu Nguyen255da6b2022-07-29 10:05:52 +0700395 sensorPtr = std::make_shared<IntelCPUSensor>(
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700396 inputPathStr, sensorType, objectServer, dbusConnection, io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800397 sensorName, std::move(sensorThresholds), *interfacePath, cpuId,
Vijay Khemka86dea2b2019-06-06 11:14:37 -0700398 show, dtsOffset);
Arun P. Mohanan04d05062021-10-29 20:30:26 +0530399 sensorPtr->setupRead();
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700400 createdSensors.insert(sensorName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700401 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700402 {
James Feist6714a252018-09-10 15:26:18 -0700403 std::cout << "Mapped: " << inputPath << " to " << sensorName
404 << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700405 }
James Feist6714a252018-09-10 15:26:18 -0700406 }
407 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700408
Ed Tanous2049bd22022-07-09 07:20:26 -0700409 if (static_cast<unsigned int>(!createdSensors.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700410 {
411 std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are")
412 << " created\n";
413 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700414
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700415 return true;
James Feist6714a252018-09-10 15:26:18 -0700416}
417
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800418bool exportDevice(const CPUConfig& config)
James Feist6714a252018-09-10 15:26:18 -0700419{
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700420 std::ostringstream hex;
421 hex << std::hex << config.addr;
422 const std::string& addrHexStr = hex.str();
423 std::string busStr = std::to_string(config.bus);
424
425 std::string parameters = "peci-client 0x" + addrHexStr;
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800426 std::string devPath = peciDevPath;
427 std::string delDevice = devPath + "peci-" + busStr + "/delete_device";
428 std::string newDevice = devPath + "peci-" + busStr + "/new_device";
429 std::string newClient = devPath + busStr + "-" + addrHexStr + "/driver";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700430
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800431 std::filesystem::path devicePath(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700432 const std::string& dir = devicePath.parent_path().string();
James Feistcf3bce62019-01-08 10:07:19 -0800433 for (const auto& path : std::filesystem::directory_iterator(dir))
James Feist6714a252018-09-10 15:26:18 -0700434 {
James Feistcf3bce62019-01-08 10:07:19 -0800435 if (!std::filesystem::is_directory(path))
James Feist6714a252018-09-10 15:26:18 -0700436 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700437 continue;
James Feist6714a252018-09-10 15:26:18 -0700438 }
439
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700440 const std::string& directoryName = path.path().filename();
Zev Weiss6c106d62022-08-17 20:50:00 -0700441 if (directoryName.starts_with(busStr) &&
442 directoryName.ends_with(addrHexStr))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700443 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700444 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700445 {
446 std::cout << parameters << " on bus " << busStr
447 << " is already exported\n";
448 }
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800449
450 std::ofstream delDeviceFile(delDevice);
451 if (!delDeviceFile.good())
452 {
453 std::cerr << "Error opening " << delDevice << "\n";
454 return false;
455 }
456 delDeviceFile << parameters;
457 delDeviceFile.close();
458
459 break;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700460 }
James Feist6714a252018-09-10 15:26:18 -0700461 }
462
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800463 std::ofstream deviceFile(newDevice);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700464 if (!deviceFile.good())
James Feist6714a252018-09-10 15:26:18 -0700465 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800466 std::cerr << "Error opening " << newDevice << "\n";
467 return false;
James Feist6714a252018-09-10 15:26:18 -0700468 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700469 deviceFile << parameters;
470 deviceFile.close();
471
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800472 if (!std::filesystem::exists(newClient))
473 {
474 std::cerr << "Error creating " << newClient << "\n";
475 return false;
476 }
477
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700478 std::cout << parameters << " on bus " << busStr << " is exported\n";
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800479
480 return true;
James Feist6714a252018-09-10 15:26:18 -0700481}
482
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700483void detectCpu(boost::asio::steady_timer& pingTimer,
484 boost::asio::steady_timer& creationTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800485 boost::asio::io_context& io,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800486 sdbusplus::asio::object_server& objectServer,
487 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
488 boost::container::flat_set<CPUConfig>& cpuConfigs,
489 ManagedObjectType& sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700490{
James Feist6714a252018-09-10 15:26:18 -0700491 size_t rescanDelaySeconds = 0;
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700492 static bool keepPinging = false;
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200493 int peciFd = -1;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700494
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800495 for (CPUConfig& config : cpuConfigs)
James Feist6714a252018-09-10 15:26:18 -0700496 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800497 if (config.state == State::READY)
498 {
499 continue;
500 }
501
Paul Fertser6e699732023-03-29 12:58:30 +0000502 std::fstream rescan{rescanPath, std::ios::out};
503 if (rescan.is_open())
504 {
Ed Tanous2e466962025-01-30 10:59:49 -0800505 std::vector<std::filesystem::path> peciPaths;
Paul Fertser6e699732023-03-29 12:58:30 +0000506 std::ostringstream searchPath;
507 searchPath << std::hex << "peci-" << config.bus << "/" << config.bus
508 << "-" << config.addr;
Ed Tanous2e466962025-01-30 10:59:49 -0800509 findFiles(std::filesystem::path(peciDevPath + searchPath.str()),
Paul Fertser6e699732023-03-29 12:58:30 +0000510 R"(peci_cpu.dimmtemp.+/hwmon/hwmon\d+/name$)", peciPaths,
511 3);
512 if (!peciPaths.empty())
513 {
514 config.state = State::READY;
515 rescanDelaySeconds = 1;
516 }
517 else
518 {
Ed Tanous2e466962025-01-30 10:59:49 -0800519 findFiles(std::filesystem::path(peciDevPath + searchPath.str()),
Paul Fertser6e699732023-03-29 12:58:30 +0000520 R"(peci_cpu.cputemp.+/hwmon/hwmon\d+/name$)",
521 peciPaths, 3);
522 if (!peciPaths.empty())
523 {
524 config.state = State::ON;
525 rescanDelaySeconds = 3;
526 }
527 else
528 {
529 // https://www.kernel.org/doc/html/latest/admin-guide/abi-testing.html#abi-sys-bus-peci-rescan
530 rescan << "1";
531 }
532 }
533 if (config.state != State::READY)
534 {
535 keepPinging = true;
536 }
537
538 continue;
539 }
540
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700541 std::string peciDevPath = peciDev + std::to_string(config.bus);
Ed Tanous99c44092022-01-14 09:59:09 -0800542
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200543 peci_SetDevName(peciDevPath.data());
544
Ed Tanous99c44092022-01-14 09:59:09 -0800545 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko28c56bf2023-05-11 15:42:53 +0200546 if ((peci_Lock(&peciFd, PECI_NO_WAIT) != PECI_CC_SUCCESS) ||
547 (peciFd < 0))
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700548 {
Jakub Nowackia38e5612023-03-08 12:27:01 +0100549 std::cerr << "unable to open " << peciDevPath << " "
550 << std::strerror(errno) << "\n";
551 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
552 dbusConnection, cpuConfigs, sensorConfigs);
553 return;
Jae Hyun Yoo9c55e6a2018-10-26 10:09:01 -0700554 }
555
Ed Tanousa771f6a2022-01-14 09:36:51 -0800556 State newState = State::OFF;
Ed Tanous99c44092022-01-14 09:59:09 -0800557
558 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100559 if (peci_Ping(config.addr) == PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700560 {
561 bool dimmReady = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700562 for (unsigned int rank = 0; rank < rankNumMax; rank++)
James Feist6714a252018-09-10 15:26:18 -0700563 {
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100564 std::array<uint8_t, 8> pkgConfig{};
565 uint8_t cc = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800566
567 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Oleksandr Shulzhenko77141ac2023-03-14 14:52:36 +0100568 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_DDR_DIMM_TEMP,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400569 rank, 4, pkgConfig.data(), &cc) ==
570 PECI_CC_SUCCESS)
James Feist6714a252018-09-10 15:26:18 -0700571 {
Matt Simmering38857572023-08-10 14:41:46 -0700572 // Depending on CPU generation, both 0 and 0xFF can be used
573 // to indicate no DIMM presence
574 if (((pkgConfig[0] != 0xFF) && (pkgConfig[0] != 0U)) ||
575 ((pkgConfig[1] != 0xFF) && (pkgConfig[1] != 0U)))
James Feist6714a252018-09-10 15:26:18 -0700576 {
577 dimmReady = true;
578 break;
579 }
580 }
581 else
582 {
583 break;
584 }
585 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700586
James Feist6714a252018-09-10 15:26:18 -0700587 if (dimmReady)
588 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700589 newState = State::READY;
James Feist6714a252018-09-10 15:26:18 -0700590 }
591 else
592 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700593 newState = State::ON;
James Feist6714a252018-09-10 15:26:18 -0700594 }
595 }
James Feist6714a252018-09-10 15:26:18 -0700596
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700597 if (config.state != newState)
James Feist6714a252018-09-10 15:26:18 -0700598 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700599 if (newState != State::OFF)
James Feist6714a252018-09-10 15:26:18 -0700600 {
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700601 if (config.state == State::OFF)
602 {
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800603 std::array<uint8_t, 8> pkgConfig{};
604 uint8_t cc = 0;
605
606 if (peci_RdPkgConfig(config.addr, PECI_MBX_INDEX_CPU_ID, 0,
Patrick Williams2aaf7172024-08-16 15:20:40 -0400607 4, pkgConfig.data(), &cc) ==
608 PECI_CC_SUCCESS)
Jae Hyun Yoo6d0f27b2021-01-27 15:52:16 -0800609 {
610 std::cout << config.name << " is detected\n";
611 if (!exportDevice(config))
612 {
613 newState = State::OFF;
614 }
615 }
616 else
617 {
618 newState = State::OFF;
619 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700620 }
James Feist6714a252018-09-10 15:26:18 -0700621
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700622 if (newState == State::ON)
623 {
624 rescanDelaySeconds = 3;
625 }
626 else if (newState == State::READY)
627 {
628 rescanDelaySeconds = 5;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400629 std::cout
630 << "DIMM(s) on " << config.name << " is/are detected\n";
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700631 }
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700632 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700633
634 config.state = newState;
Yoo, Jae Hyunf78d0a42018-10-10 11:03:11 -0700635 }
636
637 if (config.state != State::READY)
James Feist6714a252018-09-10 15:26:18 -0700638 {
639 keepPinging = true;
640 }
641
Ed Tanous8a57ec02020-10-09 12:46:52 -0700642 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700643 {
644 std::cout << config.name << ", state: " << config.state << "\n";
645 }
Wang Xiaohua4cd5a902023-07-04 16:28:43 +0800646 peci_Unlock(peciFd);
James Feist6714a252018-09-10 15:26:18 -0700647 }
648
Ed Tanous2049bd22022-07-09 07:20:26 -0700649 if (rescanDelaySeconds != 0U)
James Feist6714a252018-09-10 15:26:18 -0700650 {
Ed Tanous83db50c2023-03-01 10:20:24 -0800651 creationTimer.expires_after(std::chrono::seconds(rescanDelaySeconds));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700652 creationTimer.async_wait([&](const boost::system::error_code& ec) {
653 if (ec == boost::asio::error::operation_aborted)
654 {
655 return; // we're being canceled
656 }
657
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800658 if (!createSensors(io, objectServer, dbusConnection, cpuConfigs,
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700659 sensorConfigs) ||
660 keepPinging)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700661 {
662 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800663 dbusConnection, cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700664 }
665 });
James Feist6714a252018-09-10 15:26:18 -0700666 }
Jae Hyun Yoo18ae22f2019-04-02 10:11:30 -0700667 else if (keepPinging)
James Feist6714a252018-09-10 15:26:18 -0700668 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800669 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800670 dbusConnection, cpuConfigs, sensorConfigs);
James Feist6714a252018-09-10 15:26:18 -0700671 }
672}
673
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700674void detectCpuAsync(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700675 boost::asio::steady_timer& pingTimer,
Ed Tanous1f978632023-02-28 18:16:39 -0800676 boost::asio::steady_timer& creationTimer, boost::asio::io_context& io,
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700677 sdbusplus::asio::object_server& objectServer,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800678 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800679 boost::container::flat_set<CPUConfig>& cpuConfigs,
680 ManagedObjectType& sensorConfigs)
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700681{
Ed Tanous83db50c2023-03-01 10:20:24 -0800682 pingTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700683 pingTimer.async_wait([&](const boost::system::error_code& ec) {
684 if (ec == boost::asio::error::operation_aborted)
685 {
686 return; // we're being canceled
687 }
688
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800689 detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800690 cpuConfigs, sensorConfigs);
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700691 });
692}
693
James Feistc140e202019-11-14 15:23:51 -0800694bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus,
695 boost::container::flat_set<CPUConfig>& cpuConfigs,
696 ManagedObjectType& sensorConfigs,
697 sdbusplus::asio::object_server& objectServer)
James Feist6714a252018-09-10 15:26:18 -0700698{
James Feist6714a252018-09-10 15:26:18 -0700699 bool useCache = false;
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800700 sensorConfigs.clear();
James Feist6714a252018-09-10 15:26:18 -0700701 // use new data the first time, then refresh
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700702 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700703 {
Zev Weiss26fb1492022-08-17 15:33:46 -0700704 if (!getSensorConfiguration(type, systemBus, sensorConfigs, useCache))
James Feist6714a252018-09-10 15:26:18 -0700705 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700706 return false;
James Feist6714a252018-09-10 15:26:18 -0700707 }
708 useCache = true;
709 }
710
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700711 // check PECI client addresses and names from CPU configuration
James Feist6714a252018-09-10 15:26:18 -0700712 // before starting ping operation
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700713 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700714 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700715 for (const auto& [path, cfgData] : sensorConfigs)
James Feist6714a252018-09-10 15:26:18 -0700716 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700717 for (const auto& [intf, cfg] : cfgData)
James Feist6714a252018-09-10 15:26:18 -0700718 {
Zev Weiss054aad82022-08-18 01:37:34 -0700719 if (intf != configInterfaceName(type))
James Feist6714a252018-09-10 15:26:18 -0700720 {
721 continue;
722 }
723
Zev Weiss8908b3c2022-08-12 18:21:01 -0700724 auto findName = cfg.find("Name");
725 if (findName == cfg.end())
James Feist6714a252018-09-10 15:26:18 -0700726 {
727 continue;
728 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400729 std::string nameRaw =
730 std::visit(VariantToStringVisitor(), findName->second);
731 std::string name =
732 std::regex_replace(nameRaw, illegalDbusRegex, "_");
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700733
James Feistc140e202019-11-14 15:23:51 -0800734 auto present = std::optional<bool>();
735 // if we can't detect it via gpio, we set presence later
Zev Weiss8908b3c2022-08-12 18:21:01 -0700736 for (const auto& [suppIntf, suppCfg] : cfgData)
James Feist58295ad2019-05-30 15:01:41 -0700737 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700738 if (suppIntf.find("PresenceGpio") != std::string::npos)
James Feist58295ad2019-05-30 15:01:41 -0700739 {
Zev Weiss8908b3c2022-08-12 18:21:01 -0700740 present = cpuIsPresent(suppCfg);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700741 break;
James Feist58295ad2019-05-30 15:01:41 -0700742 }
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700743 }
744
James Feistc140e202019-11-14 15:23:51 -0800745 if (inventoryIfaces.find(name) == inventoryIfaces.end() &&
746 present)
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700747 {
748 auto iface = objectServer.add_interface(
749 cpuInventoryPath + std::string("/") + name,
750 "xyz.openbmc_project.Inventory.Item");
751 iface->register_property("PrettyName", name);
James Feistc140e202019-11-14 15:23:51 -0800752 iface->register_property("Present", *present);
Jae Hyun Yooffa07e22019-07-17 10:47:18 -0700753 iface->initialize();
754 inventoryIfaces[name] = std::move(iface);
755 }
James Feist58295ad2019-05-30 15:01:41 -0700756
Zev Weiss8908b3c2022-08-12 18:21:01 -0700757 auto findBus = cfg.find("Bus");
758 if (findBus == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700759 {
760 std::cerr << "Can't find 'Bus' setting in " << name << "\n";
761 continue;
762 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400763 uint64_t bus =
764 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700765
Zev Weiss8908b3c2022-08-12 18:21:01 -0700766 auto findAddress = cfg.find("Address");
767 if (findAddress == cfg.end())
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700768 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400769 std::cerr
770 << "Can't find 'Address' setting in " << name << "\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700771 continue;
772 }
James Feist3eb82622019-02-08 13:10:22 -0800773 uint64_t addr = std::visit(VariantToUnsignedIntVisitor(),
774 findAddress->second);
James Feist6714a252018-09-10 15:26:18 -0700775
Ed Tanous8a57ec02020-10-09 12:46:52 -0700776 if (debug)
James Feist6714a252018-09-10 15:26:18 -0700777 {
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700778 std::cout << "bus: " << bus << "\n";
James Feist6714a252018-09-10 15:26:18 -0700779 std::cout << "addr: " << addr << "\n";
780 std::cout << "name: " << name << "\n";
781 std::cout << "type: " << type << "\n";
James Feist6714a252018-09-10 15:26:18 -0700782 }
783
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800784 cpuConfigs.emplace(bus, addr, name, State::OFF);
James Feist6714a252018-09-10 15:26:18 -0700785 }
786 }
787 }
Yoo, Jae Hyuna441f3c2018-10-09 16:43:03 -0700788
Ed Tanous2049bd22022-07-09 07:20:26 -0700789 if (static_cast<unsigned int>(!cpuConfigs.empty()) != 0U)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700790 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800791 std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are")
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700792 << " parsed\n";
793 return true;
794 }
795
796 return false;
James Feist6714a252018-09-10 15:26:18 -0700797}
798
James Feistb6c0b912019-07-09 12:21:44 -0700799int main()
James Feist6714a252018-09-10 15:26:18 -0700800{
Ed Tanous1f978632023-02-28 18:16:39 -0800801 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700802 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800803 boost::container::flat_set<CPUConfig> cpuConfigs;
James Feist6714a252018-09-10 15:26:18 -0700804
Ed Tanous14ed5e92022-07-12 15:50:23 -0700805 sdbusplus::asio::object_server objectServer(systemBus, true);
806 objectServer.add_manager("/xyz/openbmc_project/sensors");
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700807 boost::asio::steady_timer pingTimer(io);
808 boost::asio::steady_timer creationTimer(io);
809 boost::asio::steady_timer filterTimer(io);
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800810 ManagedObjectType sensorConfigs;
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700811
Ed Tanous83db50c2023-03-01 10:20:24 -0800812 filterTimer.expires_after(std::chrono::seconds(1));
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700813 filterTimer.async_wait([&](const boost::system::error_code& ec) {
814 if (ec == boost::asio::error::operation_aborted)
815 {
Jae Hyun Yood64262b2018-11-01 13:31:16 -0700816 return; // we're being canceled
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700817 }
818
James Feistc140e202019-11-14 15:23:51 -0800819 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs, objectServer))
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700820 {
Jae Hyun Yooe8b60d02019-01-14 15:27:13 -0800821 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
Jae Hyun Yoo73ca5512019-02-28 21:20:17 -0800822 systemBus, cpuConfigs, sensorConfigs);
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700823 }
824 });
825
Patrick Williams92f8f512022-07-22 19:26:55 -0500826 std::function<void(sdbusplus::message_t&)> eventHandler =
827 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400828 if (message.is_method_error())
James Feist6714a252018-09-10 15:26:18 -0700829 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400830 std::cerr << "callback method error\n";
831 return;
James Feist6714a252018-09-10 15:26:18 -0700832 }
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700833
Patrick Williams2aaf7172024-08-16 15:20:40 -0400834 if (debug)
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700835 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400836 std::cout << message.get_path() << " is changed\n";
Jae Hyun Yoo8d9886d2018-10-22 15:24:29 -0700837 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400838
839 // this implicitly cancels the timer
840 filterTimer.expires_after(std::chrono::seconds(1));
841 filterTimer.async_wait([&](const boost::system::error_code& ec) {
842 if (ec == boost::asio::error::operation_aborted)
843 {
844 return; // we're being canceled
845 }
846
847 if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs,
848 objectServer))
849 {
850 detectCpuAsync(pingTimer, creationTimer, io, objectServer,
851 systemBus, cpuConfigs, sensorConfigs);
852 }
853 });
854 };
James Feist6714a252018-09-10 15:26:18 -0700855
Zev Weiss214d9712022-08-12 12:54:31 -0700856 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
857 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
James Feist6714a252018-09-10 15:26:18 -0700858
Thu Nguyen255da6b2022-07-29 10:05:52 +0700859 systemBus->request_name("xyz.openbmc_project.IntelCPUSensor");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800860
861 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700862 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700863 return 0;
James Feist6714a252018-09-10 15:26:18 -0700864}