Chromium Code Reviews| 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/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 10 #include "base/mac/sdk_forward_declarations.h" | 11 #include "base/mac/sdk_forward_declarations.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_mac.h" | |
| 12 | 14 |
| 13 using device::BluetoothDevice; | 15 using device::BluetoothDevice; |
| 14 using device::BluetoothLowEnergyDeviceMac; | 16 using device::BluetoothLowEnergyDeviceMac; |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // Converts a CBUUID to a Cocoa string. | |
| 19 // | |
| 20 // The string representation can have the following formats: | |
| 21 // - 16 bit: xxxx | |
| 22 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
| 23 // CBUUID supports only 16 bits and 128 bits formats. | |
| 24 // | |
| 25 // In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we | |
| 26 // need to provide this function. | |
| 27 NSString* stringWithCBUUID(CBUUID* uuid) { | |
| 28 NSData* data = [uuid data]; | |
| 29 | |
| 30 NSUInteger bytesToConvert = [data length]; | |
| 31 const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; | |
| 32 NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; | |
| 33 | |
| 34 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; | |
| 35 currentByteIndex++) { | |
| 36 switch (currentByteIndex) { | |
| 37 case 3: | |
| 38 case 5: | |
| 39 case 7: | |
| 40 case 9: | |
| 41 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; | |
| 42 break; | |
| 43 default: | |
| 44 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; | |
| 45 } | |
| 46 } | |
| 47 return outputString; | |
| 48 } | |
| 49 | |
| 50 // Converts a CBUUID to a BluetoothUUID. | 20 // Converts a CBUUID to a BluetoothUUID. |
| 51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | 21 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
| 52 NSString* uuidString = nil; | 22 // UUIDString only available OS X >= 10.8. |
| 53 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10. | 23 CHECK(base::mac::IsOSMountainLionOrLater()); |
| 54 if ([uuid respondsToSelector:@selector(UUIDString)]) { | 24 std::string uuid_c_string = base::SysNSStringToUTF8([uuid UUIDString]); |
| 55 uuidString = [uuid UUIDString]; | |
| 56 } else { | |
| 57 uuidString = stringWithCBUUID(uuid); | |
| 58 } | |
| 59 std::string uuid_c_string = base::SysNSStringToUTF8(uuidString); | |
| 60 return device::BluetoothUUID(uuid_c_string); | 25 return device::BluetoothUUID(uuid_c_string); |
| 61 } | 26 } |
| 62 | 27 |
| 63 } // namespace | 28 } // namespace |
| 64 | 29 |
| 65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | 30 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| 66 CBPeripheral* peripheral, | 31 CBPeripheral* peripheral, |
| 67 NSDictionary* advertisementData, | 32 NSDictionary* advertisementData, |
| 68 int rssi) { | 33 int rssi) { |
| 34 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus | |
|
scheib
2015/07/10 17:48:05
Can remove this comment (and other locations), as
krstnmnlsn
2015/07/10 18:08:33
Done.
| |
| 35 // BluetoothLowEnergyDeviceMac should never be instantiated on previous | |
| 36 // versions. | |
| 37 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); | |
| 69 Update(peripheral, advertisementData, rssi); | 38 Update(peripheral, advertisementData, rssi); |
| 70 } | 39 } |
| 71 | 40 |
| 72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { | 41 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() { |
| 73 } | 42 } |
| 74 | 43 |
| 75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | 44 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
| 76 NSDictionary* advertisementData, | 45 NSDictionary* advertisementData, |
| 77 int rssi) { | 46 int rssi) { |
| 78 last_update_time_.reset([[NSDate date] retain]); | 47 last_update_time_.reset([[NSDate date] retain]); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 } | 197 } |
| 229 | 198 |
| 230 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { | 199 NSDate* BluetoothLowEnergyDeviceMac::GetLastUpdateTime() const { |
| 231 return last_update_time_.get(); | 200 return last_update_time_.get(); |
| 232 } | 201 } |
| 233 | 202 |
| 234 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | 203 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
| 235 return base::SysNSStringToUTF8([peripheral_ name]); | 204 return base::SysNSStringToUTF8([peripheral_ name]); |
| 236 } | 205 } |
| 237 | 206 |
| 207 // static | |
| 238 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( | 208 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier( |
| 239 CBPeripheral* peripheral) { | 209 CBPeripheral* peripheral) { |
| 240 // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9. | 210 // For stability we only use CoreBluetooth on OS X >= 10.10. |
| 241 if ([peripheral respondsToSelector:@selector(identifier)]) { | 211 CHECK(BluetoothAdapterMac::IsLowEnergyAvailable()); |
| 242 // When -[CBPeripheral identifier] is available. | 212 NSUUID* uuid = [peripheral identifier]; |
| 243 NSUUID* uuid = [peripheral identifier]; | 213 NSString* uuidString = [uuid UUIDString]; |
| 244 NSString* uuidString = [uuid UUIDString]; | 214 return base::SysNSStringToUTF8(uuidString); |
| 245 return base::SysNSStringToUTF8(uuidString); | |
| 246 } | |
| 247 | |
| 248 base::ScopedCFTypeRef<CFStringRef> str( | |
| 249 CFUUIDCreateString(NULL, [peripheral UUID])); | |
| 250 return SysCFStringRefToUTF8(str); | |
| 251 } | 215 } |
| OLD | NEW |