James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 18 | |
| 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> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 27 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | #include <fstream> |
| 29 | #include <regex> |
| 30 | #include <sdbusplus/asio/connection.hpp> |
| 31 | #include <sdbusplus/asio/object_server.hpp> |
| 32 | |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 33 | // clang-format off |
| 34 | // this needs to be included last or we'll have build issues |
| 35 | #include <linux/peci-ioctl.h> |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 36 | #if !defined(PECI_MBX_INDEX_DDR_DIMM_TEMP) |
| 37 | #define PECI_MBX_INDEX_DDR_DIMM_TEMP MBX_INDEX_DDR_DIMM_TEMP |
| 38 | #endif |
James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 39 | // clang-format on |
| 40 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 41 | static constexpr bool DEBUG = false; |
| 42 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 43 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> gCpuSensors; |
| 44 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 45 | enum State |
| 46 | { |
| 47 | OFF, // host powered down |
| 48 | ON, // host powered on |
| 49 | READY // host powered on and mem test passed - fully ready |
| 50 | }; |
| 51 | |
| 52 | struct CPUConfig |
| 53 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 54 | CPUConfig(const uint64_t& bus, const uint64_t& addr, |
| 55 | const std::string& name, const State& state) : |
| 56 | bus(bus), |
| 57 | addr(addr), name(name), state(state) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 60 | int bus; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | int addr; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 62 | std::string name; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | State state; |
| 64 | |
| 65 | bool operator<(const CPUConfig& rhs) const |
| 66 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 67 | return (name < rhs.name); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 71 | static constexpr const char* peciDev = "/dev/peci-"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | static constexpr const unsigned int rankNumMax = 8; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 74 | namespace fs = std::filesystem; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 75 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 76 | static constexpr const char* configPrefix = |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | "xyz.openbmc_project.Configuration."; |
Jae Hyun Yoo | 7d47bf5 | 2019-04-23 16:43:50 -0700 | [diff] [blame^] | 78 | static constexpr std::array<const char*, 1> sensorTypes = {"XeonCPU"}; |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 79 | static constexpr std::array<const char*, 3> hiddenProps = { |
| 80 | CPUSensor::labelTcontrol, "Tthrottle", "Tjmax"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 82 | void detectCpuAsync( |
| 83 | boost::asio::deadline_timer& pingTimer, |
| 84 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, |
| 85 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 86 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 87 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 88 | ManagedObjectType& sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 89 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 90 | bool createSensors(boost::asio::io_service& io, |
| 91 | sdbusplus::asio::object_server& objectServer, |
| 92 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 93 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 94 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 95 | { |
| 96 | bool available = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 97 | for (const CPUConfig& cpu : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 98 | { |
| 99 | if (cpu.state != State::OFF) |
| 100 | { |
| 101 | available = true; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | if (!available) |
| 106 | { |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 107 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 110 | if (sensorConfigs.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 112 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 115 | std::vector<fs::path> hwmonNamePaths; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 116 | if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"), |
| 117 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", |
| 118 | hwmonNamePaths, 1)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 119 | { |
| 120 | std::cerr << "No CPU sensors in system\n"; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 121 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 124 | boost::container::flat_set<std::string> scannedDirectories; |
| 125 | boost::container::flat_set<std::string> createdSensors; |
| 126 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 127 | for (const fs::path& hwmonNamePath : hwmonNamePaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 129 | const std::string& pathStr = hwmonNamePath.string(); |
| 130 | auto hwmonDirectory = hwmonNamePath.parent_path(); |
| 131 | |
| 132 | auto ret = scannedDirectories.insert(hwmonDirectory.string()); |
| 133 | if (!ret.second) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 135 | continue; // already searched this path |
| 136 | } |
| 137 | |
| 138 | fs::path::iterator it = hwmonNamePath.begin(); |
| 139 | std::advance(it, 6); // pick the 6th part for a PECI client device name |
| 140 | std::string deviceName = *it; |
| 141 | auto findHyphen = deviceName.find("-"); |
| 142 | if (findHyphen == std::string::npos) |
| 143 | { |
| 144 | std::cerr << "found bad device " << deviceName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | continue; |
| 146 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 147 | std::string busStr = deviceName.substr(0, findHyphen); |
| 148 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 149 | |
| 150 | size_t bus = 0; |
| 151 | size_t addr = 0; |
| 152 | try |
| 153 | { |
| 154 | bus = std::stoi(busStr); |
| 155 | addr = std::stoi(addrStr, 0, 16); |
| 156 | } |
| 157 | catch (std::invalid_argument) |
| 158 | { |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | std::ifstream nameFile(hwmonNamePath); |
| 163 | if (!nameFile.good()) |
| 164 | { |
| 165 | std::cerr << "Failure reading " << hwmonNamePath << "\n"; |
| 166 | continue; |
| 167 | } |
| 168 | std::string hwmonName; |
| 169 | std::getline(nameFile, hwmonName); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 170 | nameFile.close(); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 171 | if (hwmonName.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 172 | { |
| 173 | // shouldn't have an empty name file |
| 174 | continue; |
| 175 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 176 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 177 | { |
| 178 | std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName |
| 179 | << "\n"; |
| 180 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 181 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 182 | std::string sensorType; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 183 | const SensorData* sensorData = nullptr; |
| 184 | const std::string* interfacePath = nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 185 | const std::pair<std::string, boost::container::flat_map< |
| 186 | std::string, BasicVariantType>>* |
| 187 | baseConfiguration = nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 188 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 189 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 190 | sensor : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 191 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 192 | sensorData = &(sensor.second); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 193 | for (const char* type : sensorTypes) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 194 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 195 | sensorType = configPrefix + std::string(type); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 196 | auto sensorBase = sensorData->find(sensorType); |
| 197 | if (sensorBase != sensorData->end()) |
| 198 | { |
| 199 | baseConfiguration = &(*sensorBase); |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | if (baseConfiguration == nullptr) |
| 204 | { |
| 205 | std::cerr << "error finding base configuration for" << hwmonName |
| 206 | << "\n"; |
| 207 | continue; |
| 208 | } |
| 209 | auto configurationBus = baseConfiguration->second.find("Bus"); |
| 210 | auto configurationAddress = |
| 211 | baseConfiguration->second.find("Address"); |
| 212 | |
| 213 | if (configurationBus == baseConfiguration->second.end() || |
| 214 | configurationAddress == baseConfiguration->second.end()) |
| 215 | { |
| 216 | std::cerr << "error finding bus or address in configuration"; |
| 217 | continue; |
| 218 | } |
| 219 | |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 220 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 221 | std::get<uint64_t>(configurationAddress->second) != addr) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 222 | { |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | interfacePath = &(sensor.first.str); |
| 227 | break; |
| 228 | } |
| 229 | if (interfacePath == nullptr) |
| 230 | { |
| 231 | std::cerr << "failed to find match for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 232 | continue; |
| 233 | } |
| 234 | |
| 235 | auto findCpuId = baseConfiguration->second.find("CpuID"); |
| 236 | if (findCpuId == baseConfiguration->second.end()) |
| 237 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 238 | std::cerr << "could not determine CPU ID for " << hwmonName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 239 | continue; |
| 240 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 241 | int cpuId = |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 242 | std::visit(VariantToUnsignedIntVisitor(), findCpuId->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 243 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 244 | auto directory = hwmonNamePath.parent_path(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 245 | std::vector<fs::path> inputPaths; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 246 | if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 247 | { |
| 248 | std::cerr << "No temperature sensors in system\n"; |
| 249 | continue; |
| 250 | } |
| 251 | |
| 252 | // iterate through all found temp sensors |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 253 | for (const auto& inputPath : inputPaths) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 254 | { |
| 255 | auto inputPathStr = inputPath.string(); |
| 256 | auto labelPath = |
| 257 | boost::replace_all_copy(inputPathStr, "input", "label"); |
| 258 | std::ifstream labelFile(labelPath); |
| 259 | if (!labelFile.good()) |
| 260 | { |
| 261 | std::cerr << "Failure reading " << labelPath << "\n"; |
| 262 | continue; |
| 263 | } |
| 264 | std::string label; |
| 265 | std::getline(labelFile, label); |
| 266 | labelFile.close(); |
Jae Hyun Yoo | 13f4888 | 2019-02-19 13:37:07 -0800 | [diff] [blame] | 267 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 268 | std::string sensorName = label + " CPU" + std::to_string(cpuId); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 269 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 270 | auto findSensor = gCpuSensors.find(sensorName); |
| 271 | if (findSensor != gCpuSensors.end()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 272 | { |
| 273 | if (DEBUG) |
| 274 | { |
| 275 | std::cout << "Skipped: " << inputPath << ": " << sensorName |
| 276 | << " is already created\n"; |
| 277 | } |
| 278 | continue; |
| 279 | } |
| 280 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 281 | // check hidden properties |
| 282 | bool show = true; |
| 283 | for (const char* prop : hiddenProps) |
| 284 | { |
| 285 | if (label == prop) |
| 286 | { |
| 287 | show = false; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 292 | std::vector<thresholds::Threshold> sensorThresholds; |
| 293 | std::string labelHead = label.substr(0, label.find(" ")); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 294 | parseThresholdsFromConfig(*sensorData, sensorThresholds, |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 295 | &labelHead); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 296 | if (sensorThresholds.empty()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 297 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 298 | if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr, |
| 299 | CPUSensor::sensorScaleFactor)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 300 | { |
Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 301 | std::cerr << "error populating thresholds for " |
| 302 | << sensorName << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 305 | gCpuSensors[sensorName] = std::make_unique<CPUSensor>( |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 306 | inputPathStr, sensorType, objectServer, dbusConnection, io, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 307 | sensorName, std::move(sensorThresholds), *interfacePath, cpuId, |
| 308 | show); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 309 | createdSensors.insert(sensorName); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 310 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 311 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 312 | std::cout << "Mapped: " << inputPath << " to " << sensorName |
| 313 | << "\n"; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 314 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 317 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 318 | if (createdSensors.size()) |
| 319 | { |
| 320 | std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are") |
| 321 | << " created\n"; |
| 322 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 323 | |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 324 | return true; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 327 | void exportDevice(const CPUConfig& config) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 328 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 329 | std::ostringstream hex; |
| 330 | hex << std::hex << config.addr; |
| 331 | const std::string& addrHexStr = hex.str(); |
| 332 | std::string busStr = std::to_string(config.bus); |
| 333 | |
| 334 | std::string parameters = "peci-client 0x" + addrHexStr; |
| 335 | std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device"; |
| 336 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 337 | std::filesystem::path devicePath(device); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 338 | const std::string& dir = devicePath.parent_path().string(); |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 339 | for (const auto& path : std::filesystem::directory_iterator(dir)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 340 | { |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 341 | if (!std::filesystem::is_directory(path)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 342 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 343 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 346 | const std::string& directoryName = path.path().filename(); |
| 347 | if (boost::starts_with(directoryName, busStr) && |
| 348 | boost::ends_with(directoryName, addrHexStr)) |
| 349 | { |
| 350 | if (DEBUG) |
| 351 | { |
| 352 | std::cout << parameters << " on bus " << busStr |
| 353 | << " is already exported\n"; |
| 354 | } |
| 355 | return; |
| 356 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 359 | std::ofstream deviceFile(device); |
| 360 | if (!deviceFile.good()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 361 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 362 | std::cerr << "Error writing " << device << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 363 | return; |
| 364 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 365 | deviceFile << parameters; |
| 366 | deviceFile.close(); |
| 367 | |
| 368 | std::cout << parameters << " on bus " << busStr << " is exported\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 371 | void detectCpu(boost::asio::deadline_timer& pingTimer, |
| 372 | boost::asio::deadline_timer& creationTimer, |
| 373 | boost::asio::io_service& io, |
| 374 | sdbusplus::asio::object_server& objectServer, |
| 375 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 376 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 377 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 378 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 379 | size_t rescanDelaySeconds = 0; |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 380 | static bool keepPinging = false; |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 381 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 382 | for (CPUConfig& config : cpuConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 383 | { |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 384 | std::string peciDevPath = peciDev + std::to_string(config.bus); |
| 385 | auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC); |
| 386 | if (file < 0) |
| 387 | { |
| 388 | std::cerr << "unable to open " << peciDevPath << "\n"; |
| 389 | std::exit(EXIT_FAILURE); |
| 390 | } |
| 391 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 392 | State newState; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 393 | struct peci_ping_msg msg; |
| 394 | msg.addr = config.addr; |
| 395 | if (!ioctl(file, PECI_IOC_PING, &msg)) |
| 396 | { |
| 397 | bool dimmReady = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 398 | for (unsigned int rank = 0; rank < rankNumMax; rank++) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 399 | { |
| 400 | struct peci_rd_pkg_cfg_msg msg; |
| 401 | msg.addr = config.addr; |
Jae Hyun Yoo | 201c8d9 | 2019-02-27 15:41:56 -0800 | [diff] [blame] | 402 | msg.index = PECI_MBX_INDEX_DDR_DIMM_TEMP; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 403 | msg.param = rank; |
| 404 | msg.rx_len = 4; |
| 405 | if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg)) |
| 406 | { |
| 407 | if (msg.pkg_config[0] || msg.pkg_config[1] || |
| 408 | msg.pkg_config[2]) |
| 409 | { |
| 410 | dimmReady = true; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | break; |
| 417 | } |
| 418 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 419 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 420 | if (dimmReady) |
| 421 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 422 | newState = State::READY; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 423 | } |
| 424 | else |
| 425 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 426 | newState = State::ON; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | else |
| 430 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 431 | newState = State::OFF; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 434 | close(file); |
| 435 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 436 | if (config.state != newState) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 437 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 438 | if (newState != State::OFF) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 439 | { |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 440 | if (config.state == State::OFF) |
| 441 | { |
| 442 | std::cout << config.name << " is detected\n"; |
| 443 | exportDevice(config); |
| 444 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 445 | |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 446 | if (newState == State::ON) |
| 447 | { |
| 448 | rescanDelaySeconds = 3; |
| 449 | } |
| 450 | else if (newState == State::READY) |
| 451 | { |
| 452 | rescanDelaySeconds = 5; |
| 453 | std::cout << "DIMM(s) on " << config.name |
| 454 | << " is/are detected\n"; |
| 455 | } |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 456 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 457 | |
| 458 | config.state = newState; |
Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (config.state != State::READY) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 462 | { |
| 463 | keepPinging = true; |
| 464 | } |
| 465 | |
| 466 | if (DEBUG) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 467 | { |
| 468 | std::cout << config.name << ", state: " << config.state << "\n"; |
| 469 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 470 | } |
| 471 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 472 | if (rescanDelaySeconds) |
| 473 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 474 | creationTimer.expires_from_now( |
| 475 | boost::posix_time::seconds(rescanDelaySeconds)); |
| 476 | creationTimer.async_wait([&](const boost::system::error_code& ec) { |
| 477 | if (ec == boost::asio::error::operation_aborted) |
| 478 | { |
| 479 | return; // we're being canceled |
| 480 | } |
| 481 | |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 482 | if (!createSensors(io, objectServer, dbusConnection, cpuConfigs, |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 483 | sensorConfigs) || |
| 484 | keepPinging) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 485 | { |
| 486 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 487 | dbusConnection, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 488 | } |
| 489 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 490 | } |
Jae Hyun Yoo | 18ae22f | 2019-04-02 10:11:30 -0700 | [diff] [blame] | 491 | else if (keepPinging) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 492 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 493 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 494 | dbusConnection, cpuConfigs, sensorConfigs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 498 | void detectCpuAsync( |
| 499 | boost::asio::deadline_timer& pingTimer, |
| 500 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, |
| 501 | sdbusplus::asio::object_server& objectServer, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 502 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 503 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 504 | ManagedObjectType& sensorConfigs) |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 505 | { |
| 506 | pingTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 507 | pingTimer.async_wait([&](const boost::system::error_code& ec) { |
| 508 | if (ec == boost::asio::error::operation_aborted) |
| 509 | { |
| 510 | return; // we're being canceled |
| 511 | } |
| 512 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 513 | detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 514 | cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 515 | }); |
| 516 | } |
| 517 | |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 518 | bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 519 | boost::container::flat_set<CPUConfig>& cpuConfigs, |
| 520 | ManagedObjectType& sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 521 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 522 | bool useCache = false; |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 523 | sensorConfigs.clear(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 524 | // use new data the first time, then refresh |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 525 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 526 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 527 | if (!getSensorConfiguration(configPrefix + std::string(type), systemBus, |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 528 | sensorConfigs, useCache)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 529 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 530 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 531 | } |
| 532 | useCache = true; |
| 533 | } |
| 534 | |
| 535 | // check PECI client addresses and DT overlay names from CPU configuration |
| 536 | // before starting ping operation |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 537 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 538 | { |
| 539 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 540 | sensor : sensorConfigs) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 541 | { |
| 542 | for (const std::pair< |
| 543 | std::string, |
| 544 | boost::container::flat_map<std::string, BasicVariantType>>& |
| 545 | config : sensor.second) |
| 546 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 547 | if ((configPrefix + std::string(type)) != config.first) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 548 | { |
| 549 | continue; |
| 550 | } |
| 551 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 552 | auto findName = config.second.find("Name"); |
| 553 | if (findName == config.second.end()) |
| 554 | { |
| 555 | continue; |
| 556 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 557 | std::string nameRaw = |
| 558 | std::visit(VariantToStringVisitor(), findName->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 559 | std::string name = |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 560 | std::regex_replace(nameRaw, illegalDbusRegex, "_"); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 561 | |
| 562 | auto findBus = config.second.find("Bus"); |
| 563 | if (findBus == config.second.end()) |
| 564 | { |
| 565 | std::cerr << "Can't find 'Bus' setting in " << name << "\n"; |
| 566 | continue; |
| 567 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 568 | uint64_t bus = |
| 569 | std::visit(VariantToUnsignedIntVisitor(), findBus->second); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 570 | |
| 571 | auto findAddress = config.second.find("Address"); |
| 572 | if (findAddress == config.second.end()) |
| 573 | { |
| 574 | std::cerr << "Can't find 'Address' setting in " << name |
| 575 | << "\n"; |
| 576 | continue; |
| 577 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 578 | uint64_t addr = std::visit(VariantToUnsignedIntVisitor(), |
| 579 | findAddress->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 580 | |
| 581 | if (DEBUG) |
| 582 | { |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 583 | std::cout << "bus: " << bus << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 584 | std::cout << "addr: " << addr << "\n"; |
| 585 | std::cout << "name: " << name << "\n"; |
| 586 | std::cout << "type: " << type << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 589 | cpuConfigs.emplace(bus, addr, name, State::OFF); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 593 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 594 | if (cpuConfigs.size()) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 595 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 596 | std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are") |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 597 | << " parsed\n"; |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | return false; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | int main(int argc, char** argv) |
| 605 | { |
| 606 | boost::asio::io_service io; |
| 607 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 608 | boost::container::flat_set<CPUConfig> cpuConfigs; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 609 | |
| 610 | systemBus->request_name("xyz.openbmc_project.CPUSensor"); |
| 611 | sdbusplus::asio::object_server objectServer(systemBus); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 612 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 613 | boost::asio::deadline_timer pingTimer(io); |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 614 | boost::asio::deadline_timer creationTimer(io); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 615 | boost::asio::deadline_timer filterTimer(io); |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 616 | ManagedObjectType sensorConfigs; |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 617 | |
| 618 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 619 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 620 | if (ec == boost::asio::error::operation_aborted) |
| 621 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 622 | return; // we're being canceled |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 625 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs)) |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 626 | { |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 627 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 628 | systemBus, cpuConfigs, sensorConfigs); |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 629 | } |
| 630 | }); |
| 631 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 632 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 633 | [&](sdbusplus::message::message& message) { |
| 634 | if (message.is_method_error()) |
| 635 | { |
| 636 | std::cerr << "callback method error\n"; |
| 637 | return; |
| 638 | } |
Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 639 | |
| 640 | if (DEBUG) |
| 641 | { |
| 642 | std::cout << message.get_path() << " is changed\n"; |
| 643 | } |
| 644 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 645 | // this implicitly cancels the timer |
| 646 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 647 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 648 | if (ec == boost::asio::error::operation_aborted) |
| 649 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 650 | return; // we're being canceled |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 653 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 654 | { |
Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 655 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, |
Jae Hyun Yoo | 73ca551 | 2019-02-28 21:20:17 -0800 | [diff] [blame] | 656 | systemBus, cpuConfigs, sensorConfigs); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 657 | } |
| 658 | }); |
| 659 | }; |
| 660 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 661 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 662 | { |
| 663 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 664 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 665 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 666 | std::string(inventoryPath) + "',arg0namespace='" + |
| 667 | configPrefix + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 668 | eventHandler); |
| 669 | matches.emplace_back(std::move(match)); |
| 670 | } |
| 671 | |
| 672 | io.run(); |
| 673 | } |