| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_low_energy_device_mac.h" | 5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| 6 | 6 |
| 7 #import <CoreFoundation/CoreFoundation.h> | 7 #import <CoreFoundation/CoreFoundation.h> |
| 8 | 8 |
| 9 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
| 10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 11 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/sdk_forward_declarations.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_mac.h" | 14 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 15 #include "device/bluetooth/bluetooth_device.h" |
| 14 | 16 |
| 15 using device::BluetoothDevice; | 17 using device::BluetoothDevice; |
| 16 using device::BluetoothLowEnergyDeviceMac; | 18 using device::BluetoothLowEnergyDeviceMac; |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // Converts a CBUUID to a BluetoothUUID. | 22 // Converts a CBUUID to a BluetoothUUID. |
| 21 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | 23 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
| 22 // UUIDString only available OS X >= 10.8. | 24 // UUIDString only available OS X >= 10.8. |
| 23 DCHECK(base::mac::IsOSMountainLionOrLater()); | 25 DCHECK(base::mac::IsOSMountainLionOrLater()); |
| 24 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]); | 26 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]); |
| 25 return device::BluetoothUUID(uuid_c_string); | 27 return device::BluetoothUUID(uuid_c_string); |
| 26 } | 28 } |
| 27 | 29 |
| 28 } // namespace | 30 } // namespace |
| 29 | 31 |
| 30 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 32 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| 31 CBPeripheral* peripheral, | 33 CBPeripheral* peripheral, |
| 32 NSDictionary* advertisementData, | 34 NSDictionary* advertisementData, |
| 33 int rssi) { | 35 int rssi) { |
| 34 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 36 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 37 identifier_ = GetPeripheralIdentifier(peripheral); |
| 38 hash_address_ = GetPeripheralHashAddress(peripheral); |
| 35 Update(peripheral, advertisementData, rssi); | 39 Update(peripheral, advertisementData, rssi); |
| 36 } | 40 } |
| 37 | 41 |
| 38 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 42 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
| 39 } | 43 } |
| 40 | 44 |
| 41 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | 45 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
| 42 NSDictionary* advertisementData, | 46 NSDictionary* advertisementData, |
| 43 int rssi) { | 47 int rssi) { |
| 44 last_update_time_.reset([[NSDate date] retain]); | 48 last_update_time_.reset([[NSDate date] retain]); |
| 45 peripheral_.reset([peripheral retain]); | 49 peripheral_.reset([peripheral retain]); |
| 46 rssi_ = rssi; | 50 rssi_ = rssi; |
| 47 ClearServiceData(); | 51 ClearServiceData(); |
| 48 NSNumber* nbConnectable = | 52 NSNumber* nbConnectable = |
| 49 [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; | 53 [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; |
| 50 connectable_ = [nbConnectable boolValue]; | 54 connectable_ = [nbConnectable boolValue]; |
| 51 NSDictionary* serviceData = | 55 NSDictionary* serviceData = |
| 52 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; | 56 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; |
| 53 for (CBUUID* uuid in serviceData) { | 57 for (CBUUID* uuid in serviceData) { |
| 54 NSData* data = [serviceData objectForKey:uuid]; | 58 NSData* data = [serviceData objectForKey:uuid]; |
| 55 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); | 59 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); |
| 56 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); | 60 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); |
| 57 } | 61 } |
| 58 } | 62 } |
| 59 | 63 |
| 60 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { | 64 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { |
| 61 return GetPeripheralIdentifier(peripheral_); | 65 return identifier_; |
| 62 } | 66 } |
| 63 | 67 |
| 64 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { | 68 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { |
| 65 return 0; | 69 return 0; |
| 66 } | 70 } |
| 67 | 71 |
| 68 std::string BluetoothLowEnergyDeviceMac::GetAddress() const { | 72 std::string BluetoothLowEnergyDeviceMac::GetAddress() const { |
| 69 return std::string(); | 73 return hash_address_; |
| 70 } | 74 } |
| 71 | 75 |
| 72 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() | 76 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() |
| 73 const { | 77 const { |
| 74 return VENDOR_ID_UNKNOWN; | 78 return VENDOR_ID_UNKNOWN; |
| 75 } | 79 } |
| 76 | 80 |
| 77 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { | 81 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { |
| 78 return 0; | 82 return 0; |
| 79 } | 83 } |
| 80 | 84 |
| 81 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { | 85 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { |
| 82 return 0; | 86 return 0; |
| 83 } | 87 } |
| 84 | 88 |
| 85 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { | 89 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { |
| 86 return 0; | 90 return 0; |
| 87 } | 91 } |
| 88 | 92 |
| 89 int BluetoothLowEnergyDeviceMac::GetRSSI() const { | 93 int BluetoothLowEnergyDeviceMac::GetRSSI() const { |
| 90 return rssi_; | 94 return rssi_; |
| 91 } | 95 } |
| 92 | 96 |
| 93 bool BluetoothLowEnergyDeviceMac::IsPaired() const { | 97 bool BluetoothLowEnergyDeviceMac::IsPaired() const { |
| 94 return false; | 98 return false; |
| 95 } | 99 } |
| 96 | 100 |
| 97 bool BluetoothLowEnergyDeviceMac::IsConnected() const { | 101 bool BluetoothLowEnergyDeviceMac::IsConnected() const { |
| 98 return [peripheral_ isConnected]; | 102 return (GetPeripheralState() == CBPeripheralStateConnected); |
| 99 } | 103 } |
| 100 | 104 |
| 101 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { | 105 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { |
| 102 return connectable_; | 106 return connectable_; |
| 103 } | 107 } |
| 104 | 108 |
| 105 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { | 109 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { |
| 106 return false; | 110 return false; |
| 107 } | 111 } |
| 108 | 112 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 206 } |
| 203 | 207 |
| 204 // static | 208 // static |
| 205 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 209 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 206 CBPeripheral* peripheral) { | 210 CBPeripheral* peripheral) { |
| 207 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | 211 DCHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 208 NSUUID* uuid = [peripheral identifier]; | 212 NSUUID* uuid = [peripheral identifier]; |
| 209 NSString* uuidString = [uuid UUIDString]; | 213 NSString* uuidString = [uuid UUIDString]; |
| 210 return base::SysNSStringToUTF8(uuidString); | 214 return base::SysNSStringToUTF8(uuidString); |
| 211 } | 215 } |
| 216 |
| 217 // static |
| 218 std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress( |
| 219 CBPeripheral* peripheral) { |
| 220 const size_t kCanonicalAddressNumberOfBytes = 6; |
| 221 char raw[kCanonicalAddressNumberOfBytes]; |
| 222 crypto::SHA256HashString(GetPeripheralIdentifier(peripheral), raw, |
| 223 sizeof(raw)); |
| 224 std::string hash = base::HexEncode(raw, sizeof(raw)); |
| 225 return BluetoothDevice::CanonicalizeAddress(hash); |
| 226 } |
| 227 |
| 228 CBPeripheralState BluetoothLowEnergyDeviceMac::GetPeripheralState() const { |
| 229 Class peripheral_class = NSClassFromString(@"CBPeripheral"); |
| 230 base::scoped_nsobject<NSMethodSignature> signature([[peripheral_class |
| 231 instanceMethodSignatureForSelector:@selector(state)] retain]); |
| 232 base::scoped_nsobject<NSInvocation> invocation( |
| 233 [[NSInvocation invocationWithMethodSignature:signature] retain]); |
| 234 [invocation setTarget:peripheral_]; |
| 235 [invocation setSelector:@selector(state)]; |
| 236 [invocation invoke]; |
| 237 CBPeripheralState state = CBPeripheralStateDisconnected; |
| 238 [invocation getReturnValue:&state]; |
| 239 return state; |
| 240 } |
| OLD | NEW |