Chromium Code Reviews| Index: device/bluetooth/bluetooth_low_energy_device_mac.mm |
| diff --git a/device/bluetooth/bluetooth_low_energy_device_mac.mm b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3c59c29fb428e73be751036d377dba4d5dc79e19 |
| --- /dev/null |
| +++ b/device/bluetooth/bluetooth_low_energy_device_mac.mm |
| @@ -0,0 +1,191 @@ |
| +#include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
|
armansito
2015/01/24 18:07:09
Copyright header?
dvh
2015/01/27 00:14:55
Done.
|
| + |
| +#import <CoreFoundation/CoreFoundation.h> |
| + |
| +#include "base/mac/scoped_cftyperef.h" |
| + |
| +using namespace device; |
| + |
| +namespace { |
|
armansito
2015/01/24 18:07:09
nit: new line here.
dvh
2015/01/27 00:14:55
Done.
|
| +// Converts a CBUUID to a Cocoa string. |
| +// |
| +// The string representation can have the following formats: |
| +// - 16 bit: xxxx |
| +// - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| +// CBUUID supports only 16 bits and 128 bits formats. |
| +// |
| +// In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we |
| +// need to provide this function. |
| +NSString* stringWithCBUUID(CBUUID* uuid) { |
| + NSData* data = [uuid data]; |
| + |
| + NSUInteger bytesToConvert = [data length]; |
| + const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; |
| + NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; |
| + |
| + for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; |
| + currentByteIndex++) { |
| + switch (currentByteIndex) { |
| + case 3: |
| + case 5: |
| + case 7: |
| + case 9: |
| + [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; |
| + break; |
| + default: |
| + [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; |
| + } |
| + } |
| + return outputString; |
| +} |
| + |
| +// Converts a CBUUID to a BluetoothUUID. |
| +device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { |
| + NSString* uuidString = nil; |
| + if ([uuid respondsToSelector:@selector(UUIDString)]) { |
| + uuidString = [uuid performSelector:@selector(UUIDString)]; |
| + } else { |
| + uuidString = stringWithCBUUID(uuid); |
| + } |
| + std::string uuid_c_string([uuidString UTF8String]); |
| + return BluetoothUUID(uuid_c_string); |
| +} |
|
armansito
2015/01/24 18:07:10
nit: new line.
|
| +} |
|
armansito
2015/01/24 18:07:10
nit: Closing brace for namespace should read "} /
dvh
2015/01/27 00:14:55
Done.
|
| + |
| +BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( |
| + CBPeripheral* peripheral, NSDictionary* advertisementData, int rssi) { |
| + Update(peripheral, advertisementData, rssi); |
| +} |
| + |
| +BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {} |
| + |
| +void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, |
| + NSDictionary* advertisementData, |
| + int rssi) { |
| + peripheral_.reset([peripheral retain]); |
| + rssi_ = rssi; |
| + ClearServiceData(); |
| + NSNumber* nbConnectable = |
| + [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; |
| + connectable_ = [nbConnectable boolValue]; |
| + NSDictionary* serviceData = |
| + [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; |
| + for (CBUUID* uuid in serviceData) { |
| + NSData* data = [serviceData objectForKey:uuid]; |
| + BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); |
| + SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); |
| + } |
| +} |
| + |
| +std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { |
| + if ([peripheral_ respondsToSelector:@selector(identifier)]) { |
| + // When -[CBPeripheral identifier] is available. |
| + NSUUID* uuid = [peripheral_ performSelector:@selector(identifier)]; |
| + NSString* uuidString = [uuid UUIDString]; |
| + return std::string([uuidString UTF8String]); |
|
armansito
2015/01/24 18:07:09
nit: Remove else since you already have an early r
dvh
2015/01/27 00:14:55
Done.
|
| + } else { |
| + base::ScopedCFTypeRef<CFStringRef> str( |
| + CFUUIDCreateString(NULL, [peripheral_ UUID])); |
| + base::ScopedCFTypeRef<CFDataRef> data(CFStringCreateExternalRepresentation( |
| + NULL, str, kCFStringEncodingASCII, 0)); |
| + return std::string((const char*)CFDataGetBytePtr(data), |
| + CFDataGetLength(data)); |
| + } |
| +} |
| + |
| +uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { return 0; } |
| + |
| +std::string BluetoothLowEnergyDeviceMac::GetAddress() const { |
| + return std::string(); |
| +} |
| + |
| +BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() |
| + const { |
| + return VENDOR_ID_UNKNOWN; |
| +} |
| + |
| +uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { return 0; } |
| + |
| +uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { return 0; } |
| + |
| +uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { return 0; } |
| + |
| +int BluetoothLowEnergyDeviceMac::GetRSSI() const { return rssi_; } |
| + |
| +bool BluetoothLowEnergyDeviceMac::IsPaired() const { return false; } |
| + |
| +bool BluetoothLowEnergyDeviceMac::IsConnected() const { |
| + return [peripheral_ isConnected]; |
| +} |
| + |
| +bool BluetoothLowEnergyDeviceMac::IsConnectable() const { return connectable_; } |
| + |
| +bool BluetoothLowEnergyDeviceMac::IsConnecting() const { return false; } |
| + |
| +BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { |
| + return std::vector<device::BluetoothUUID>(); |
| +} |
| + |
| +bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { return false; } |
| + |
| +bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const { return false; } |
| + |
| +bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const { |
| + return false; |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::GetConnectionInfo( |
| + const ConnectionInfoCallback& callback) {} |
|
armansito
2015/01/24 18:07:10
Add NOTIMPLEMENTED() ?
dvh
2015/01/27 00:14:55
Done.
|
| + |
| +void BluetoothLowEnergyDeviceMac::Connect( |
| + PairingDelegate* pairing_delegate, const base::Closure& callback, |
| + const ConnectErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::ConfirmPairing() { NOTIMPLEMENTED(); } |
| + |
| +void BluetoothLowEnergyDeviceMac::RejectPairing() { NOTIMPLEMENTED(); } |
| + |
| +void BluetoothLowEnergyDeviceMac::CancelPairing() { NOTIMPLEMENTED(); } |
| + |
| +void BluetoothLowEnergyDeviceMac::Disconnect( |
| + const base::Closure& callback, const ErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::ConnectToService( |
| + const BluetoothUUID& uuid, const ConnectToServiceCallback& callback, |
| + const ConnectToServiceErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( |
| + const device::BluetoothUUID& uuid, const ConnectToServiceCallback& callback, |
| + const ConnectToServiceErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void BluetoothLowEnergyDeviceMac::CreateGattConnection( |
| + const GattConnectionCallback& callback, |
| + const ConnectErrorCallback& error_callback) { |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { |
| + NSData* data = [[peripheral_ name] dataUsingEncoding:NSUTF8StringEncoding |
| + allowLossyConversion:YES]; |
| + return std::string((const char*)[data bytes], [data length]); |
| +} |