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_device_mac.h" | 5 #include "device/bluetooth/bluetooth_device_mac.h" |
6 | 6 |
7 #include <IOBluetooth/Bluetooth.h> | 7 #include <IOBluetooth/Bluetooth.h> |
8 #import <IOBluetooth/objc/IOBluetoothDevice.h> | 8 #import <IOBluetooth/objc/IOBluetoothDevice.h> |
9 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> | 9 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> |
10 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h> | 10 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/hash.h" | 15 #include "base/hash.h" |
| 16 #include "base/sequenced_task_runner.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
18 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
19 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 20 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
20 #include "device/bluetooth/bluetooth_profile_mac.h" | 21 #include "device/bluetooth/bluetooth_profile_mac.h" |
21 #include "device/bluetooth/bluetooth_service_record_mac.h" | 22 #include "device/bluetooth/bluetooth_service_record_mac.h" |
22 #include "device/bluetooth/bluetooth_socket_mac.h" | 23 #include "device/bluetooth/bluetooth_socket_mac.h" |
23 | 24 |
24 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | 25 // Replicate specific 10.7 SDK declarations for building with prior SDKs. |
25 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 26 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
26 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 27 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
27 | 28 |
28 @interface IOBluetoothDevice (LionSDKDeclarations) | 29 @interface IOBluetoothDevice (LionSDKDeclarations) |
29 - (NSString*)addressString; | 30 - (NSString*)addressString; |
30 - (NSString*)name; | 31 - (NSString*)name; |
31 - (unsigned int)classOfDevice; | 32 - (unsigned int)classOfDevice; |
32 - (NSArray*)services; | 33 - (NSArray*)services; |
33 @end | 34 @end |
34 | 35 |
35 #endif // MAC_OS_X_VERSION_10_7 | 36 #endif // MAC_OS_X_VERSION_10_7 |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 const char kFailedToConnect[] = "Connection failed"; | |
40 | |
41 // Converts |uuid| to a IOBluetoothSDPUUID instance. | 40 // Converts |uuid| to a IOBluetoothSDPUUID instance. |
42 // | 41 // |
43 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. | 42 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. |
44 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) { | 43 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) { |
45 DCHECK(uuid.size() == 36); | 44 DCHECK(uuid.size() == 36); |
46 DCHECK(uuid[8] == '-'); | 45 DCHECK(uuid[8] == '-'); |
47 DCHECK(uuid[13] == '-'); | 46 DCHECK(uuid[13] == '-'); |
48 DCHECK(uuid[18] == '-'); | 47 DCHECK(uuid[18] == '-'); |
49 DCHECK(uuid[23] == '-'); | 48 DCHECK(uuid[23] == '-'); |
50 std::string numbers_only = uuid; | 49 std::string numbers_only = uuid; |
51 numbers_only.erase(23, 1); | 50 numbers_only.erase(23, 1); |
52 numbers_only.erase(18, 1); | 51 numbers_only.erase(18, 1); |
53 numbers_only.erase(13, 1); | 52 numbers_only.erase(13, 1); |
54 numbers_only.erase(8, 1); | 53 numbers_only.erase(8, 1); |
55 std::vector<uint8> uuid_bytes_vector; | 54 std::vector<uint8> uuid_bytes_vector; |
56 base::HexStringToBytes(numbers_only, &uuid_bytes_vector); | 55 base::HexStringToBytes(numbers_only, &uuid_bytes_vector); |
57 DCHECK(uuid_bytes_vector.size() == 16); | 56 DCHECK(uuid_bytes_vector.size() == 16); |
58 | 57 |
59 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0] | 58 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0] |
60 length:uuid_bytes_vector.size()]; | 59 length:uuid_bytes_vector.size()]; |
61 } | 60 } |
62 | 61 |
63 } // namespace | 62 } // namespace |
64 | 63 |
65 namespace device { | 64 namespace device { |
66 | 65 |
67 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device) | 66 BluetoothDeviceMac::BluetoothDeviceMac( |
68 : BluetoothDevice(), device_([device retain]) { | 67 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner, |
69 } | 68 IOBluetoothDevice* device) |
| 69 : BluetoothDevice(), |
| 70 ui_task_runner_(ui_task_runner), |
| 71 device_([device retain]) {} |
70 | 72 |
71 BluetoothDeviceMac::~BluetoothDeviceMac() { | 73 BluetoothDeviceMac::~BluetoothDeviceMac() { |
72 [device_ release]; | 74 [device_ release]; |
73 } | 75 } |
74 | 76 |
75 void BluetoothDeviceMac::AddObserver( | 77 void BluetoothDeviceMac::AddObserver( |
76 device::BluetoothDevice::Observer* observer) { | 78 device::BluetoothDevice::Observer* observer) { |
77 DCHECK(observer); | 79 DCHECK(observer); |
78 observers_.AddObserver(observer); | 80 observers_.AddObserver(observer); |
79 } | 81 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 NOTIMPLEMENTED(); | 194 NOTIMPLEMENTED(); |
193 } | 195 } |
194 | 196 |
195 void BluetoothDeviceMac::ConnectToService( | 197 void BluetoothDeviceMac::ConnectToService( |
196 const device::BluetoothUUID& service_uuid, | 198 const device::BluetoothUUID& service_uuid, |
197 const SocketCallback& callback) { | 199 const SocketCallback& callback) { |
198 IOBluetoothSDPServiceRecord* record = | 200 IOBluetoothSDPServiceRecord* record = |
199 [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID( | 201 [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID( |
200 service_uuid.canonical_value())]; | 202 service_uuid.canonical_value())]; |
201 if (record != nil) { | 203 if (record != nil) { |
202 BluetoothServiceRecordMac service_record(record); | |
203 scoped_refptr<BluetoothSocket> socket( | 204 scoped_refptr<BluetoothSocket> socket( |
204 BluetoothSocketMac::CreateBluetoothSocket(service_record)); | 205 BluetoothSocketMac::CreateBluetoothSocket(ui_task_runner_, record)); |
205 if (socket.get() != NULL) | 206 if (socket.get() != NULL) |
206 callback.Run(socket); | 207 callback.Run(socket); |
207 } | 208 } |
208 } | 209 } |
209 | 210 |
210 void BluetoothDeviceMac::ConnectToProfile( | 211 void BluetoothDeviceMac::ConnectToProfile( |
211 BluetoothProfile* profile, | 212 BluetoothProfile* profile, |
212 const base::Closure& callback, | 213 const base::Closure& callback, |
213 const ConnectToProfileErrorCallback& error_callback) { | 214 const ConnectToProfileErrorCallback& error_callback) { |
214 if (static_cast<BluetoothProfileMac*>(profile)->Connect(device_)) | 215 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
215 callback.Run(); | 216 static_cast<BluetoothProfileMac*>(profile) |
216 else | 217 ->Connect(ui_task_runner_, device_, callback, error_callback); |
217 error_callback.Run(kFailedToConnect); | |
218 } | 218 } |
219 | 219 |
220 void BluetoothDeviceMac::SetOutOfBandPairingData( | 220 void BluetoothDeviceMac::SetOutOfBandPairingData( |
221 const BluetoothOutOfBandPairingData& data, | 221 const BluetoothOutOfBandPairingData& data, |
222 const base::Closure& callback, | 222 const base::Closure& callback, |
223 const ErrorCallback& error_callback) { | 223 const ErrorCallback& error_callback) { |
224 NOTIMPLEMENTED(); | 224 NOTIMPLEMENTED(); |
225 } | 225 } |
226 | 226 |
227 void BluetoothDeviceMac::ClearOutOfBandPairingData( | 227 void BluetoothDeviceMac::ClearOutOfBandPairingData( |
228 const base::Closure& callback, | 228 const base::Closure& callback, |
229 const ErrorCallback& error_callback) { | 229 const ErrorCallback& error_callback) { |
230 NOTIMPLEMENTED(); | 230 NOTIMPLEMENTED(); |
231 } | 231 } |
232 | 232 |
233 } // namespace device | 233 } // namespace device |
OLD | NEW |