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