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