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