| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_adapter_mac.h" | 5 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 6 | 6 |
| 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 7 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
| 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> | 8 #import <IOBluetooth/objc/IOBluetoothHostController.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // hash the probability of this occuring with 10,000 devices | 507 // hash the probability of this occuring with 10,000 devices |
| 508 // simultaneously present is 1e-6 (see | 508 // simultaneously present is 1e-6 (see |
| 509 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We | 509 // https://en.wikipedia.org/wiki/Birthday_problem#Probability_table). We |
| 510 // ignore the second device by returning. | 510 // ignore the second device by returning. |
| 511 return; | 511 return; |
| 512 } | 512 } |
| 513 } | 513 } |
| 514 | 514 |
| 515 DCHECK(device_mac); | 515 DCHECK(device_mac); |
| 516 | 516 |
| 517 // Get Advertised UUIDs | 517 if (advertisement_data) { |
| 518 BluetoothDevice::UUIDList advertised_uuids; | 518 // Get Advertised UUIDs |
| 519 NSArray* service_uuids = | 519 BluetoothDevice::UUIDList advertised_uuids; |
| 520 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; | 520 NSArray* service_uuids = |
| 521 for (CBUUID* uuid in service_uuids) { | 521 [advertisement_data objectForKey:CBAdvertisementDataServiceUUIDsKey]; |
| 522 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); | 522 for (CBUUID* uuid in service_uuids) { |
| 523 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| 524 } |
| 525 NSArray* overflow_service_uuids = [advertisement_data |
| 526 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; |
| 527 for (CBUUID* uuid in overflow_service_uuids) { |
| 528 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); |
| 529 } |
| 530 |
| 531 // Get Service Data. |
| 532 BluetoothDevice::ServiceDataMap service_data_map; |
| 533 NSDictionary* service_data = |
| 534 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; |
| 535 for (CBUUID* uuid in service_data) { |
| 536 NSData* data = [service_data objectForKey:uuid]; |
| 537 const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]); |
| 538 size_t length = [data length]; |
| 539 service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]), |
| 540 std::vector<uint8_t>(bytes, bytes + length)); |
| 541 } |
| 542 |
| 543 // Get Tx Power. |
| 544 NSNumber* tx_power = |
| 545 [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey]; |
| 546 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); |
| 547 |
| 548 device_mac->UpdateAdvertisementData( |
| 549 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), |
| 550 std::move(service_data_map), |
| 551 tx_power == nil ? nullptr : &clamped_tx_power); |
| 523 } | 552 } |
| 524 NSArray* overflow_service_uuids = [advertisement_data | |
| 525 objectForKey:CBAdvertisementDataOverflowServiceUUIDsKey]; | |
| 526 for (CBUUID* uuid in overflow_service_uuids) { | |
| 527 advertised_uuids.push_back(BluetoothUUID([[uuid UUIDString] UTF8String])); | |
| 528 } | |
| 529 | |
| 530 // Get Service Data. | |
| 531 BluetoothDevice::ServiceDataMap service_data_map; | |
| 532 NSDictionary* service_data = | |
| 533 [advertisement_data objectForKey:CBAdvertisementDataServiceDataKey]; | |
| 534 for (CBUUID* uuid in service_data) { | |
| 535 NSData* data = [service_data objectForKey:uuid]; | |
| 536 const uint8_t* bytes = static_cast<const uint8_t*>([data bytes]); | |
| 537 size_t length = [data length]; | |
| 538 service_data_map.emplace(BluetoothUUID([[uuid UUIDString] UTF8String]), | |
| 539 std::vector<uint8_t>(bytes, bytes + length)); | |
| 540 } | |
| 541 | |
| 542 // Get Tx Power. | |
| 543 NSNumber* tx_power = | |
| 544 [advertisement_data objectForKey:CBAdvertisementDataTxPowerLevelKey]; | |
| 545 int8_t clamped_tx_power = BluetoothDevice::ClampPower([tx_power intValue]); | |
| 546 | |
| 547 device_mac->UpdateAdvertisementData( | |
| 548 BluetoothDevice::ClampPower(rssi), std::move(advertised_uuids), | |
| 549 std::move(service_data_map), | |
| 550 tx_power == nil ? nullptr : &clamped_tx_power); | |
| 551 | 553 |
| 552 if (is_new_device) { | 554 if (is_new_device) { |
| 553 std::string device_address = | 555 std::string device_address = |
| 554 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 556 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 555 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); | 557 devices_.add(device_address, std::unique_ptr<BluetoothDevice>(device_mac)); |
| 556 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 558 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 557 DeviceAdded(this, device_mac)); | 559 DeviceAdded(this, device_mac)); |
| 558 } else { | 560 } else { |
| 559 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 561 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
| 560 DeviceChanged(this, device_mac)); | 562 DeviceChanged(this, device_mac)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 std::string device_address = | 637 std::string device_address = |
| 636 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); | 638 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| 637 DevicesMap::const_iterator iter = devices_.find(device_address); | 639 DevicesMap::const_iterator iter = devices_.find(device_address); |
| 638 if (iter == devices_.end()) { | 640 if (iter == devices_.end()) { |
| 639 return nil; | 641 return nil; |
| 640 } | 642 } |
| 641 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); | 643 return static_cast<BluetoothLowEnergyDeviceMac*>(iter->second); |
| 642 } | 644 } |
| 643 | 645 |
| 644 } // namespace device | 646 } // namespace device |
| OLD | NEW |