Mark //testing/perf target testonly.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_low_energy_device_mac.mm
blobae56d5388dab9202ca79f840a0c4787d1a4f8163
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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_low_energy_device_mac.h"
7 #import <CoreFoundation/CoreFoundation.h>
9 #include "base/mac/scoped_cftyperef.h"
10 #include "base/mac/sdk_forward_declarations.h"
11 #include "base/strings/sys_string_conversions.h"
13 using device::BluetoothDevice;
14 using device::BluetoothLowEnergyDeviceMac;
16 namespace {
18 // Converts a CBUUID to a Cocoa string.
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.
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];
30   NSUInteger bytesToConvert = [data length];
31   const unsigned char* uuidBytes = (const unsigned char*)[data bytes];
32   NSMutableString* outputString = [NSMutableString stringWithCapacity:16];
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;
50 // Converts a CBUUID to a BluetoothUUID.
51 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) {
52   NSString* uuidString = nil;
53   // TODO(dvh): Remove this once we moved to OSX SDK >= 10.10.
54   if ([uuid respondsToSelector:@selector(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);
63 }  // namespace
65 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac(
66     CBPeripheral* peripheral,
67     NSDictionary* advertisementData,
68     int rssi) {
69   Update(peripheral, advertisementData, rssi);
72 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {
75 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral,
76                                          NSDictionary* advertisementData,
77                                          int rssi) {
78   peripheral_.reset([peripheral retain]);
79   rssi_ = rssi;
80   ClearServiceData();
81   NSNumber* nbConnectable =
82       [advertisementData objectForKey:CBAdvertisementDataIsConnectable];
83   connectable_ = [nbConnectable boolValue];
84   NSDictionary* serviceData =
85       [advertisementData objectForKey:CBAdvertisementDataServiceDataKey];
86   for (CBUUID* uuid in serviceData) {
87     NSData* data = [serviceData objectForKey:uuid];
88     BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid);
89     SetServiceData(serviceUUID, (const char*)[data bytes], [data length]);
90   }
93 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const {
94   return GetPeripheralIdentifier(peripheral_);
97 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const {
98   return 0;
101 std::string BluetoothLowEnergyDeviceMac::GetAddress() const {
102   return std::string();
105 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource()
106     const {
107   return VENDOR_ID_UNKNOWN;
110 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const {
111   return 0;
114 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const {
115   return 0;
118 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const {
119   return 0;
122 int BluetoothLowEnergyDeviceMac::GetRSSI() const {
123   return rssi_;
126 bool BluetoothLowEnergyDeviceMac::IsPaired() const {
127   return false;
130 bool BluetoothLowEnergyDeviceMac::IsConnected() const {
131   return [peripheral_ isConnected];
134 bool BluetoothLowEnergyDeviceMac::IsConnectable() const {
135   return connectable_;
138 bool BluetoothLowEnergyDeviceMac::IsConnecting() const {
139   return false;
142 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const {
143   return std::vector<device::BluetoothUUID>();
146 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
147   return false;
150 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
151   return false;
154 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
155   return false;
158 void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
159     const ConnectionInfoCallback& callback) {
160   NOTIMPLEMENTED();
163 void BluetoothLowEnergyDeviceMac::Connect(
164     PairingDelegate* pairing_delegate,
165     const base::Closure& callback,
166     const ConnectErrorCallback& error_callback) {
167   NOTIMPLEMENTED();
170 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
171   NOTIMPLEMENTED();
174 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) {
175   NOTIMPLEMENTED();
178 void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
179   NOTIMPLEMENTED();
182 void BluetoothLowEnergyDeviceMac::RejectPairing() {
183   NOTIMPLEMENTED();
186 void BluetoothLowEnergyDeviceMac::CancelPairing() {
187   NOTIMPLEMENTED();
190 void BluetoothLowEnergyDeviceMac::Disconnect(
191     const base::Closure& callback,
192     const ErrorCallback& error_callback) {
193   NOTIMPLEMENTED();
196 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) {
197   NOTIMPLEMENTED();
200 void BluetoothLowEnergyDeviceMac::ConnectToService(
201     const BluetoothUUID& uuid,
202     const ConnectToServiceCallback& callback,
203     const ConnectToServiceErrorCallback& error_callback) {
204   NOTIMPLEMENTED();
207 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
208     const device::BluetoothUUID& uuid,
209     const ConnectToServiceCallback& callback,
210     const ConnectToServiceErrorCallback& error_callback) {
211   NOTIMPLEMENTED();
214 void BluetoothLowEnergyDeviceMac::CreateGattConnection(
215     const GattConnectionCallback& callback,
216     const ConnectErrorCallback& error_callback) {
217   NOTIMPLEMENTED();
220 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const {
221   return base::SysNSStringToUTF8([peripheral_ name]);
224 std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(
225     CBPeripheral* peripheral) {
226   // TODO(dvh): Remove this once we moved to OSX SDK >= 10.9.
227   if ([peripheral respondsToSelector:@selector(identifier)]) {
228     // When -[CBPeripheral identifier] is available.
229     NSUUID* uuid = [peripheral identifier];
230     NSString* uuidString = [uuid UUIDString];
231     return base::SysNSStringToUTF8(uuidString);
232   }
234   base::ScopedCFTypeRef<CFStringRef> str(
235       CFUUIDCreateString(NULL, [peripheral UUID]));
236   return SysCFStringRefToUTF8(str);