James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [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 <Utils.hpp> |
| 18 | #include <boost/container/flat_map.hpp> |
| 19 | #include <ctime> |
| 20 | #include <dbus/connection.hpp> |
| 21 | #include <dbus/endpoint.hpp> |
| 22 | #include <dbus/message.hpp> |
| 23 | #include <dbus/properties.hpp> |
| 24 | #include <fcntl.h> |
| 25 | #include <fstream> |
| 26 | #include <future> |
James Feist | b5320a7 | 2018-01-24 12:28:12 -0800 | [diff] [blame] | 27 | #include <linux/i2c-dev-user.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 28 | #include <iostream> |
| 29 | #include <sys/ioctl.h> |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 30 | #include <regex> |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 31 | #include <sys/inotify.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 32 | |
| 33 | namespace fs = std::experimental::filesystem; |
| 34 | static constexpr bool DEBUG = false; |
| 35 | static size_t UNKNOWN_BUS_OBJECT_COUNT = 0; |
| 36 | |
| 37 | const static constexpr char *BASEBOARD_FRU_LOCATION = |
| 38 | "/etc/fru/baseboard.fru.bin"; |
| 39 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 40 | const static constexpr char *I2C_DEV_LOCATION = "/dev"; |
| 41 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 42 | static constexpr std::array<const char *, 5> FRU_AREAS = { |
| 43 | "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"}; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 44 | const static constexpr char *POWER_OBJECT_NAME = "/org/openbmc/control/power0"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 45 | using DeviceMap = boost::container::flat_map<int, std::vector<char>>; |
| 46 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 47 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 48 | static bool isMuxBus(size_t bus) |
| 49 | { |
| 50 | return is_symlink(std::experimental::filesystem::path( |
| 51 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device")); |
| 52 | } |
| 53 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 54 | int get_bus_frus(int file, int first, int last, int bus, |
| 55 | std::shared_ptr<DeviceMap> devices) |
| 56 | { |
| 57 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data; |
| 58 | |
| 59 | for (int ii = first; ii <= last; ii++) |
| 60 | { |
| 61 | // Set slave address |
| 62 | if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0) |
| 63 | { |
| 64 | std::cerr << "device at bus " << bus << "register " << ii |
| 65 | << "busy\n"; |
| 66 | continue; |
| 67 | } |
| 68 | // probe |
| 69 | else if (i2c_smbus_read_byte(file) < 0) |
| 70 | { |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | if (DEBUG) |
| 75 | { |
| 76 | std::cout << "something at bus " << bus << "addr " << ii << "\n"; |
| 77 | } |
| 78 | if (i2c_smbus_read_i2c_block_data(file, 0x0, 0x8, block_data.data()) < |
| 79 | 0) |
| 80 | { |
| 81 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 82 | << "\n"; |
| 83 | continue; |
| 84 | } |
| 85 | size_t sum = 0; |
| 86 | for (int jj = 0; jj < 7; jj++) |
| 87 | { |
| 88 | sum += block_data[jj]; |
| 89 | } |
| 90 | sum = (256 - sum) & 0xFF; |
| 91 | |
| 92 | // check the header checksum |
| 93 | if (sum == block_data[7]) |
| 94 | { |
| 95 | std::vector<char> device; |
| 96 | device.insert(device.end(), block_data.begin(), |
| 97 | block_data.begin() + 8); |
| 98 | |
| 99 | for (int jj = 1; jj <= FRU_AREAS.size(); jj++) |
| 100 | { |
| 101 | auto area_offset = device[jj]; |
| 102 | if (area_offset != 0) |
| 103 | { |
| 104 | area_offset *= 8; |
| 105 | if (i2c_smbus_read_i2c_block_data(file, area_offset, 0x8, |
| 106 | block_data.data()) < 0) |
| 107 | { |
| 108 | std::cerr << "failed to read bus " << bus << " address " |
| 109 | << ii << "\n"; |
| 110 | return -1; |
| 111 | } |
| 112 | int length = block_data[1] * 8; |
| 113 | device.insert(device.end(), block_data.begin(), |
| 114 | block_data.begin() + 8); |
| 115 | length -= 8; |
| 116 | area_offset += 8; |
| 117 | |
| 118 | while (length > 0) |
| 119 | { |
| 120 | auto to_get = std::min(0x20, length); |
| 121 | if (i2c_smbus_read_i2c_block_data( |
| 122 | file, area_offset, to_get, block_data.data()) < |
| 123 | 0) |
| 124 | { |
| 125 | std::cerr << "failed to read bus " << bus |
| 126 | << " address " << ii << "\n"; |
| 127 | return -1; |
| 128 | } |
| 129 | device.insert(device.end(), block_data.begin(), |
| 130 | block_data.begin() + to_get); |
| 131 | area_offset += to_get; |
| 132 | length -= to_get; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | (*devices).emplace(ii, device); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static BusMap FindI2CDevices(const std::vector<fs::path> &i2cBuses) |
| 144 | { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 145 | std::vector<std::future<void>> futures; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 146 | BusMap busMap; |
| 147 | for (auto &i2cBus : i2cBuses) |
| 148 | { |
| 149 | auto busnum = i2cBus.string(); |
| 150 | auto lastDash = busnum.rfind(std::string("-")); |
| 151 | // delete everything before dash inclusive |
| 152 | if (lastDash != std::string::npos) |
| 153 | { |
| 154 | busnum.erase(0, lastDash + 1); |
| 155 | } |
| 156 | auto bus = std::stoi(busnum); |
| 157 | |
| 158 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 159 | if (file < 0) |
| 160 | { |
| 161 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 162 | << "\n"; |
| 163 | continue; |
| 164 | } |
| 165 | unsigned long funcs = 0; |
| 166 | |
| 167 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 168 | { |
| 169 | std::cerr |
| 170 | << "Error: Could not get the adapter functionality matrix bus" |
| 171 | << bus << "\n"; |
| 172 | continue; |
| 173 | } |
| 174 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 175 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 176 | { |
| 177 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 178 | << bus << "\n"; |
| 179 | continue; |
| 180 | } |
| 181 | auto &device = busMap[bus]; |
| 182 | device = std::make_shared<DeviceMap>(); |
| 183 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 184 | // don't scan muxed buses async as don't want to confuse the mux |
| 185 | if (isMuxBus(bus)) |
| 186 | { |
| 187 | get_bus_frus(file, 0x03, 0x77, bus, device); |
| 188 | close(file); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | // todo: call with boost asio? |
| 193 | futures.emplace_back( |
| 194 | std::async(std::launch::async, [file, device, bus] { |
| 195 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 196 | // this is |
| 197 | // what we |
| 198 | // have tested with, use this range. Could be changed in |
| 199 | // future. |
| 200 | get_bus_frus(file, 0x03, 0x77, bus, device); |
| 201 | close(file); |
| 202 | })); |
| 203 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 204 | } |
| 205 | for (auto &fut : futures) |
| 206 | { |
| 207 | fut.get(); // wait for all scans |
| 208 | } |
| 209 | return busMap; |
| 210 | } |
| 211 | |
| 212 | static const std::tm intelEpoch(void) |
| 213 | { |
| 214 | std::tm val = {0}; |
| 215 | val.tm_year = 1996 - 1900; |
| 216 | return val; |
| 217 | } |
| 218 | |
| 219 | bool formatFru(const std::vector<char> &fruBytes, |
| 220 | boost::container::flat_map<std::string, std::string> &result) |
| 221 | { |
| 222 | static const std::vector<const char *> CHASSIS_FRU_AREAS = { |
| 223 | "PART_NUMBER", "SERIAL_NUMBER", "CHASSIS_INFO_AM1", "CHASSIS_INFO_AM2"}; |
| 224 | |
| 225 | static const std::vector<const char *> BOARD_FRU_AREAS = { |
| 226 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 227 | "VERSION_ID"}; |
| 228 | |
| 229 | static const std::vector<const char *> PRODUCT_FRU_AREAS = { |
| 230 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", |
| 231 | "PRODUCT_VERSION", "PRODUCT_SERIAL_NUMBER", "ASSET_TAG"}; |
| 232 | |
| 233 | size_t sum = 0; |
| 234 | |
| 235 | if (fruBytes.size() < 8) |
| 236 | { |
| 237 | return false; |
| 238 | } |
| 239 | std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin(); |
| 240 | result["Common Format Version"] = |
| 241 | std::to_string(static_cast<int>(*fruAreaOffsetField)); |
| 242 | |
| 243 | const std::vector<const char *> *fieldData; |
| 244 | |
| 245 | for (auto &area : FRU_AREAS) |
| 246 | { |
| 247 | fruAreaOffsetField++; |
| 248 | if (fruAreaOffsetField >= fruBytes.end()) |
| 249 | { |
| 250 | return false; |
| 251 | } |
| 252 | size_t offset = (*fruAreaOffsetField) * 8; |
| 253 | |
| 254 | if (offset > 1) |
| 255 | { |
| 256 | // +2 to skip format and length |
| 257 | std::vector<char>::const_iterator fruBytesIter = |
| 258 | fruBytes.begin() + offset + 2; |
| 259 | |
| 260 | if (fruBytesIter >= fruBytes.end()) |
| 261 | { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | if (area == "CHASSIS") |
| 266 | { |
| 267 | result["CHASSIS_TYPE"] = |
| 268 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 269 | fruBytesIter += 1; |
| 270 | fieldData = &CHASSIS_FRU_AREAS; |
| 271 | } |
| 272 | else if (area == "BOARD") |
| 273 | { |
| 274 | result["BOARD_LANGUAGE_CODE"] = |
| 275 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 276 | fruBytesIter += 1; |
| 277 | if (fruBytesIter >= fruBytes.end()) |
| 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | unsigned int minutes = *fruBytesIter | |
| 283 | *(fruBytesIter + 1) << 8 | |
| 284 | *(fruBytesIter + 2) << 16; |
| 285 | std::tm fruTime = intelEpoch(); |
| 286 | time_t timeValue = mktime(&fruTime); |
| 287 | timeValue += minutes * 60; |
| 288 | fruTime = *gmtime(&timeValue); |
| 289 | |
| 290 | result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime); |
| 291 | result["BOARD_MANUFACTURE_DATE"] |
| 292 | .pop_back(); // remove trailing null |
| 293 | fruBytesIter += 3; |
| 294 | fieldData = &BOARD_FRU_AREAS; |
| 295 | } |
| 296 | else if (area == "PRODUCT") |
| 297 | { |
| 298 | result["PRODUCT_LANGUAGE_CODE"] = |
| 299 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 300 | fruBytesIter += 1; |
| 301 | fieldData = &PRODUCT_FRU_AREAS; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | continue; |
| 306 | } |
| 307 | for (auto &field : *fieldData) |
| 308 | { |
| 309 | if (fruBytesIter >= fruBytes.end()) |
| 310 | { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | size_t length = *fruBytesIter & 0x3f; |
| 315 | fruBytesIter += 1; |
| 316 | |
| 317 | if (fruBytesIter >= fruBytes.end()) |
| 318 | { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | result[std::string(area) + "_" + field] = |
| 323 | std::string(fruBytesIter, fruBytesIter + length); |
| 324 | fruBytesIter += length; |
| 325 | if (fruBytesIter >= fruBytes.end()) |
| 326 | { |
| 327 | std::cerr << "Warning Fru Length Mismatch:\n "; |
| 328 | for (auto &c : fruBytes) |
| 329 | { |
| 330 | std::cerr << c; |
| 331 | } |
| 332 | std::cerr << "\n"; |
| 333 | if (DEBUG) |
| 334 | { |
| 335 | for (auto &keyPair : result) |
| 336 | { |
| 337 | std::cerr << keyPair.first << " : " |
| 338 | << keyPair.second << "\n"; |
| 339 | } |
| 340 | } |
| 341 | return false; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | void AddFruObjectToDbus( |
| 351 | std::shared_ptr<dbus::connection> dbusConn, std::vector<char> &device, |
| 352 | dbus::DbusObjectServer &objServer, |
| 353 | boost::container::flat_map<std::pair<size_t, size_t>, |
| 354 | std::shared_ptr<dbus::DbusObject>> |
| 355 | &dbusObjectMap, |
| 356 | int bus, size_t address) |
| 357 | { |
| 358 | boost::container::flat_map<std::string, std::string> formattedFru; |
| 359 | if (!formatFru(device, formattedFru)) |
| 360 | { |
| 361 | std::cerr << "failed to format fru for device at bus " << std::hex |
| 362 | << bus << "address " << address << "\n"; |
| 363 | return; |
| 364 | } |
| 365 | auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME"); |
| 366 | std::string productName; |
| 367 | if (productNameFind == formattedFru.end()) |
| 368 | { |
| 369 | productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME"); |
| 370 | } |
| 371 | if (productNameFind != formattedFru.end()) |
| 372 | { |
| 373 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 374 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 375 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 376 | } |
| 377 | else |
| 378 | { |
| 379 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 380 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 381 | } |
| 382 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 383 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
| 384 | |
| 385 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 386 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 387 | { |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 388 | size_t index = 0; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 389 | for (auto const &busObj : dbusObjectMap) |
| 390 | { |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 391 | if ((busObj.second->object_name == productName)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 392 | { |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 393 | if (isMuxBus(bus) && address == busObj.first.second) |
| 394 | { |
| 395 | continue; |
| 396 | } |
| 397 | // add underscore _index for the same object path on dbus |
| 398 | std::string strIndex = std::to_string(index); |
| 399 | if (index > 0) |
| 400 | { |
| 401 | productName.substr(0, productName.size() - strIndex.size()); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | productName += "_"; |
| 406 | } |
| 407 | productName += std::to_string(index++); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | auto object = objServer.add_object(productName); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 412 | dbusObjectMap[std::pair<size_t, size_t>(bus, address)] = object; |
| 413 | |
| 414 | auto iface = std::make_shared<dbus::DbusInterface>( |
| 415 | "xyz.openbmc_project.FruDevice", dbusConn); |
| 416 | object->register_interface(iface); |
| 417 | for (auto &property : formattedFru) |
| 418 | { |
| 419 | iface->set_property(property.first, property.second); |
| 420 | } |
| 421 | // baseboard can set this to -1 to not set a bus / address |
| 422 | if (bus > 0) |
| 423 | { |
| 424 | std::stringstream data; |
| 425 | data << "0x" << std::hex << bus; |
| 426 | iface->set_property("BUS", data.str()); |
| 427 | data.str(""); |
| 428 | data << "0x" << std::hex << address; |
| 429 | iface->set_property("ADDRESS", data.str()); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static bool readBaseboardFru(std::vector<char> &baseboardFru) |
| 434 | { |
| 435 | // try to read baseboard fru from file |
| 436 | std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 437 | if (baseboardFruFile.good()) |
| 438 | { |
| 439 | baseboardFruFile.seekg(0, std::ios_base::end); |
| 440 | std::streampos fileSize = baseboardFruFile.tellg(); |
| 441 | baseboardFru.resize(fileSize); |
| 442 | baseboardFruFile.seekg(0, std::ios_base::beg); |
| 443 | baseboardFruFile.read(baseboardFru.data(), fileSize); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | return false; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 452 | void rescanBusses(boost::container::flat_map<std::pair<size_t, size_t>, |
| 453 | std::shared_ptr<dbus::DbusObject>> |
| 454 | &dbusObjectMap, |
| 455 | std::shared_ptr<dbus::connection> systemBus, |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 456 | dbus::DbusObjectServer &objServer, |
| 457 | std::atomic_bool &pendingCallback) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 458 | { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 459 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 460 | do |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 461 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 462 | auto devDir = fs::path("/dev/"); |
| 463 | auto matchString = std::string("i2c*"); |
| 464 | std::vector<fs::path> i2cBuses; |
| 465 | pendingCallback = false; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 466 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 467 | if (!find_files(devDir, matchString, i2cBuses, 0)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 468 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 469 | std::cerr << "unable to find i2c devices\n"; |
| 470 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 471 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 472 | // scanning muxes in reverse order seems to have adverse effects |
| 473 | // sorting this list seems to be a fix for that |
| 474 | std::sort(i2cBuses.begin(), i2cBuses.end()); |
| 475 | BusMap busMap = FindI2CDevices(i2cBuses); |
| 476 | |
| 477 | for (auto &busObj : dbusObjectMap) |
| 478 | { |
| 479 | objServer.remove_object(busObj.second); |
| 480 | } |
| 481 | |
| 482 | dbusObjectMap.clear(); |
| 483 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
| 484 | |
| 485 | for (auto &devicemap : busMap) |
| 486 | { |
| 487 | for (auto &device : *devicemap.second) |
| 488 | { |
| 489 | AddFruObjectToDbus(systemBus, device.second, objServer, |
| 490 | dbusObjectMap, devicemap.first, |
| 491 | device.first); |
| 492 | } |
| 493 | } |
| 494 | // todo, get this from a more sensable place |
| 495 | std::vector<char> baseboardFru; |
| 496 | if (readBaseboardFru(baseboardFru)) |
| 497 | { |
| 498 | AddFruObjectToDbus(systemBus, baseboardFru, objServer, |
| 499 | dbusObjectMap, -1, -1); |
| 500 | } |
| 501 | } while (pendingCallback); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 502 | } |
| 503 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 504 | int main(int argc, char **argv) |
| 505 | { |
| 506 | auto devDir = fs::path("/dev/"); |
| 507 | auto matchString = std::string("i2c*"); |
| 508 | std::vector<fs::path> i2cBuses; |
| 509 | |
| 510 | if (!find_files(devDir, matchString, i2cBuses, 0)) |
| 511 | { |
| 512 | std::cerr << "unable to find i2c devices\n"; |
| 513 | return 1; |
| 514 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 515 | |
| 516 | boost::asio::io_service io; |
| 517 | auto systemBus = std::make_shared<dbus::connection>(io, dbus::bus::system); |
| 518 | dbus::DbusObjectServer objServer(systemBus); |
| 519 | systemBus->request_name("com.intel.FruDevice"); |
| 520 | |
| 521 | // this is a map with keys of pair(bus number, adddress) and values of the |
| 522 | // object on dbus |
| 523 | boost::container::flat_map<std::pair<size_t, size_t>, |
| 524 | std::shared_ptr<dbus::DbusObject>> |
| 525 | dbusObjectMap; |
| 526 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 527 | auto iface = std::make_shared<dbus::DbusInterface>( |
| 528 | "xyz.openbmc_project.FruDeviceManager", systemBus); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 529 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 530 | std::atomic_bool threadRunning(false); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 531 | std::atomic_bool pendingCallback(false); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 532 | std::future<void> future; |
| 533 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 534 | iface->register_method("ReScan", [&]() { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 535 | bool notRunning = false; |
| 536 | if (threadRunning.compare_exchange_strong(notRunning, true)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 537 | { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 538 | future = std::async(std::launch::async, [&] { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 539 | rescanBusses(dbusObjectMap, systemBus, objServer, |
| 540 | pendingCallback); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 541 | threadRunning = false; |
| 542 | }); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 543 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 544 | else |
| 545 | { |
| 546 | pendingCallback = true; |
| 547 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 548 | return std::tuple<>(); // this is a bug in boost-dbus, needs some sort |
| 549 | // of return |
| 550 | }); |
| 551 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 552 | dbus::match powerChange(systemBus, |
| 553 | "type='signal',path_namespace='" + |
| 554 | std::string(POWER_OBJECT_NAME) + "'"); |
| 555 | |
| 556 | dbus::filter filter(systemBus, [](dbus::message &m) { |
| 557 | auto member = m.get_member(); |
| 558 | return member == "PropertiesChanged"; |
| 559 | }); |
| 560 | std::function<void(boost::system::error_code, dbus::message)> eventHandler = |
| 561 | [&](boost::system::error_code ec, dbus::message s) { |
| 562 | boost::container::flat_map<std::string, dbus::dbus_variant> values; |
| 563 | std::string objectName; |
| 564 | s.unpack(objectName, values); |
| 565 | auto findPgood = values.find("pgood"); |
| 566 | if (findPgood != values.end()) |
| 567 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 568 | bool notRunning = false; |
| 569 | if (threadRunning.compare_exchange_strong(notRunning, true)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 570 | { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 571 | future = std::async(std::launch::async, [&] { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 572 | rescanBusses(dbusObjectMap, systemBus, objServer, |
| 573 | pendingCallback); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 574 | threadRunning = false; |
| 575 | }); |
| 576 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 577 | else |
| 578 | { |
| 579 | pendingCallback = true; |
| 580 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 581 | } |
| 582 | filter.async_dispatch(eventHandler); |
| 583 | |
| 584 | }; |
| 585 | filter.async_dispatch(eventHandler); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 586 | int fd = inotify_init(); |
| 587 | int wd = inotify_add_watch(fd, I2C_DEV_LOCATION, |
| 588 | IN_CREATE | IN_MOVED_TO | IN_DELETE); |
| 589 | std::array<char, 4096> readBuffer; |
| 590 | std::string pendingBuffer; |
| 591 | // monitor for new i2c devices |
| 592 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 593 | std::function<void(const boost::system::error_code, std::size_t)> |
| 594 | watchI2cBusses = [&](const boost::system::error_code &ec, |
| 595 | std::size_t bytes_transferred) { |
| 596 | if (ec) |
| 597 | { |
| 598 | std::cout << "Callback Error " << ec << "\n"; |
| 599 | return; |
| 600 | } |
| 601 | pendingBuffer += std::string(readBuffer.data(), bytes_transferred); |
| 602 | bool devChange = false; |
| 603 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 604 | { |
| 605 | const inotify_event *iEvent = |
| 606 | reinterpret_cast<const inotify_event *>( |
| 607 | pendingBuffer.data()); |
| 608 | switch (iEvent->mask) |
| 609 | { |
| 610 | case IN_CREATE: |
| 611 | case IN_MOVED_TO: |
| 612 | case IN_DELETE: |
| 613 | if (boost::starts_with(std::string(iEvent->name), "i2c")) |
| 614 | { |
| 615 | devChange = true; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 620 | } |
| 621 | bool notRunning = false; |
| 622 | if (devChange && |
| 623 | threadRunning.compare_exchange_strong(notRunning, true)) |
| 624 | { |
| 625 | future = std::async(std::launch::async, [&] { |
| 626 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 627 | rescanBusses(dbusObjectMap, systemBus, objServer, |
| 628 | pendingCallback); |
| 629 | threadRunning = false; |
| 630 | }); |
| 631 | } |
| 632 | else if (devChange) |
| 633 | { |
| 634 | pendingCallback = true; |
| 635 | } |
| 636 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 637 | watchI2cBusses); |
| 638 | }; |
| 639 | |
| 640 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
| 641 | |
| 642 | // run the intial scan |
| 643 | rescanBusses(dbusObjectMap, systemBus, objServer, pendingCallback); |
| 644 | |
| 645 | auto object = std::make_shared<dbus::DbusObject>( |
| 646 | systemBus, "/xyz/openbmc_project/FruDevice"); |
| 647 | objServer.register_object(object); |
| 648 | object->register_interface(iface); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 649 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 650 | io.run(); |
| 651 | return 0; |
| 652 | } |