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 | 11 |
11 #include <string> | 12 #include <string> |
12 | 13 |
13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
14 #include "base/hash.h" | 15 #include "base/hash.h" |
| 16 #include "base/string_number_conversions.h" |
15 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
16 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
17 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 19 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
18 #include "device/bluetooth/bluetooth_service_record_mac.h" | 20 #include "device/bluetooth/bluetooth_service_record_mac.h" |
19 #include "device/bluetooth/bluetooth_socket_mac.h" | 21 #include "device/bluetooth/bluetooth_socket_mac.h" |
20 | 22 |
21 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | 23 // Replicate specific 10.7 SDK declarations for building with prior SDKs. |
22 #if !defined(MAC_OS_X_VERSION_10_7) || \ | 24 #if !defined(MAC_OS_X_VERSION_10_7) || \ |
23 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | 25 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 |
24 | 26 |
25 @interface IOBluetoothDevice (LionSDKDeclarations) | 27 @interface IOBluetoothDevice (LionSDKDeclarations) |
26 - (NSString*)addressString; | 28 - (NSString*)addressString; |
27 - (NSString*)name; | 29 - (NSString*)name; |
28 - (unsigned int)classOfDevice; | 30 - (unsigned int)classOfDevice; |
29 - (NSArray*)services; | 31 - (NSArray*)services; |
30 @end | 32 @end |
31 | 33 |
32 #endif // MAC_OS_X_VERSION_10_7 | 34 #endif // MAC_OS_X_VERSION_10_7 |
33 | 35 |
| 36 namespace { |
| 37 |
| 38 // Converts |uuid| to a IOBluetoothSDPUUID instance. |
| 39 // |
| 40 // |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. |
| 41 IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) { |
| 42 DCHECK(uuid.size() == 36); |
| 43 DCHECK(uuid[8] == '-'); |
| 44 DCHECK(uuid[13] == '-'); |
| 45 DCHECK(uuid[18] == '-'); |
| 46 DCHECK(uuid[23] == '-'); |
| 47 std::string numbers_only = uuid; |
| 48 numbers_only.erase(23, 1); |
| 49 numbers_only.erase(18, 1); |
| 50 numbers_only.erase(13, 1); |
| 51 numbers_only.erase(8, 1); |
| 52 std::vector<uint8> uuid_bytes_vector; |
| 53 base::HexStringToBytes(numbers_only, &uuid_bytes_vector); |
| 54 DCHECK(uuid_bytes_vector.size() == 16); |
| 55 |
| 56 return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0] |
| 57 length:uuid_bytes_vector.size()]; |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
34 namespace device { | 62 namespace device { |
35 | 63 |
36 BluetoothDeviceMac::BluetoothDeviceMac(const IOBluetoothDevice* device) | 64 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device) |
37 : BluetoothDevice(), | 65 : BluetoothDevice(), device_([device retain]) { |
38 device_fingerprint_(ComputeDeviceFingerprint(device)) { | |
39 name_ = base::SysNSStringToUTF8([device name]); | |
40 address_ = base::SysNSStringToUTF8([device addressString]); | |
41 bluetooth_class_ = [device classOfDevice]; | |
42 connected_ = [device isConnected]; | |
43 paired_ = [device isPaired]; | |
44 | |
45 for (IOBluetoothSDPServiceRecord* service in [device services]) { | |
46 BluetoothServiceRecord* service_record = | |
47 new BluetoothServiceRecordMac(service); | |
48 service_record_list_.push_back(service_record); | |
49 service_uuids_.push_back(service_record->uuid()); | |
50 } | |
51 } | 66 } |
52 | 67 |
53 BluetoothDeviceMac::~BluetoothDeviceMac() { | 68 BluetoothDeviceMac::~BluetoothDeviceMac() { |
| 69 [device_ release]; |
54 } | 70 } |
55 | 71 |
56 uint32 BluetoothDeviceMac::GetBluetoothClass() const { | 72 uint32 BluetoothDeviceMac::GetBluetoothClass() const { |
57 return bluetooth_class_; | 73 return [device_ classOfDevice]; |
58 } | 74 } |
59 | 75 |
60 std::string BluetoothDeviceMac::GetDeviceName() const { | 76 std::string BluetoothDeviceMac::GetDeviceName() const { |
61 return name_; | 77 return base::SysNSStringToUTF8([device_ name]); |
62 } | 78 } |
63 | 79 |
64 std::string BluetoothDeviceMac::GetAddress() const { | 80 std::string BluetoothDeviceMac::GetAddress() const { |
65 return address_; | 81 return base::SysNSStringToUTF8([device_ addressString]); |
66 } | 82 } |
67 | 83 |
68 bool BluetoothDeviceMac::IsPaired() const { | 84 bool BluetoothDeviceMac::IsPaired() const { |
69 return paired_; | 85 return [device_ isPaired]; |
70 } | 86 } |
71 | 87 |
72 bool BluetoothDeviceMac::IsConnected() const { | 88 bool BluetoothDeviceMac::IsConnected() const { |
73 return connected_; | 89 return [device_ isConnected]; |
74 } | 90 } |
75 | 91 |
76 bool BluetoothDeviceMac::IsConnectable() const { | 92 bool BluetoothDeviceMac::IsConnectable() const { |
77 return false; | 93 return false; |
78 } | 94 } |
79 | 95 |
80 bool BluetoothDeviceMac::IsConnecting() const { | 96 bool BluetoothDeviceMac::IsConnecting() const { |
81 return false; | 97 return false; |
82 } | 98 } |
83 | 99 |
| 100 // TODO(youngki): BluetoothServiceRecord is deprecated; implement this method |
| 101 // without using BluetoothServiceRecord. |
84 BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const { | 102 BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const { |
85 return service_uuids_; | 103 ServiceList service_uuids; |
| 104 for (IOBluetoothSDPServiceRecord* service in [device_ services]) { |
| 105 BluetoothServiceRecordMac service_record(service); |
| 106 service_uuids.push_back(service_record.uuid()); |
| 107 } |
| 108 return service_uuids; |
86 } | 109 } |
87 | 110 |
| 111 // NOTE(youngki): This method is deprecated; it will be removed soon. |
88 void BluetoothDeviceMac::GetServiceRecords( | 112 void BluetoothDeviceMac::GetServiceRecords( |
89 const ServiceRecordsCallback& callback, | 113 const ServiceRecordsCallback& callback, |
90 const ErrorCallback& error_callback) { | 114 const ErrorCallback& error_callback) { |
91 callback.Run(service_record_list_); | 115 ServiceRecordList service_record_list; |
| 116 for (IOBluetoothSDPServiceRecord* service in [device_ services]) { |
| 117 BluetoothServiceRecord* service_record = |
| 118 new BluetoothServiceRecordMac(service); |
| 119 service_record_list.push_back(service_record); |
| 120 } |
| 121 |
| 122 callback.Run(service_record_list); |
92 } | 123 } |
93 | 124 |
| 125 // NOTE(youngki): This method is deprecated; it will be removed soon. |
94 void BluetoothDeviceMac::ProvidesServiceWithName( | 126 void BluetoothDeviceMac::ProvidesServiceWithName( |
95 const std::string& name, | 127 const std::string& name, |
96 const ProvidesServiceCallback& callback) { | 128 const ProvidesServiceCallback& callback) { |
97 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); | 129 NOTIMPLEMENTED(); |
98 iter != service_record_list_.end(); | |
99 ++iter) { | |
100 if ((*iter)->name() == name) { | |
101 callback.Run(true); | |
102 return; | |
103 } | |
104 } | |
105 callback.Run(false); | 130 callback.Run(false); |
106 } | 131 } |
107 | 132 |
108 bool BluetoothDeviceMac::ExpectingPinCode() const { | 133 bool BluetoothDeviceMac::ExpectingPinCode() const { |
109 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
110 return false; | 135 return false; |
111 } | 136 } |
112 | 137 |
113 bool BluetoothDeviceMac::ExpectingPasskey() const { | 138 bool BluetoothDeviceMac::ExpectingPasskey() const { |
114 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 NOTIMPLEMENTED(); | 178 NOTIMPLEMENTED(); |
154 } | 179 } |
155 | 180 |
156 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { | 181 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { |
157 NOTIMPLEMENTED(); | 182 NOTIMPLEMENTED(); |
158 } | 183 } |
159 | 184 |
160 void BluetoothDeviceMac::ConnectToService( | 185 void BluetoothDeviceMac::ConnectToService( |
161 const std::string& service_uuid, | 186 const std::string& service_uuid, |
162 const SocketCallback& callback) { | 187 const SocketCallback& callback) { |
163 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); | 188 IOBluetoothSDPServiceRecord* record = |
164 iter != service_record_list_.end(); | 189 [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)]; |
165 ++iter) { | 190 if (record != nil) { |
166 if ((*iter)->uuid() == service_uuid) { | 191 BluetoothServiceRecordMac service_record(record); |
167 // If multiple service records are found, use the first one that works. | 192 scoped_refptr<BluetoothSocket> socket( |
168 scoped_refptr<BluetoothSocket> socket( | 193 BluetoothSocketMac::CreateBluetoothSocket(service_record)); |
169 BluetoothSocketMac::CreateBluetoothSocket(**iter)); | 194 if (socket.get() != NULL) |
170 if (socket.get() != NULL) { | 195 callback.Run(socket); |
171 callback.Run(socket); | |
172 return; | |
173 } | |
174 } | |
175 } | 196 } |
176 } | 197 } |
177 | 198 |
178 void BluetoothDeviceMac::SetOutOfBandPairingData( | 199 void BluetoothDeviceMac::SetOutOfBandPairingData( |
179 const BluetoothOutOfBandPairingData& data, | 200 const BluetoothOutOfBandPairingData& data, |
180 const base::Closure& callback, | 201 const base::Closure& callback, |
181 const ErrorCallback& error_callback) { | 202 const ErrorCallback& error_callback) { |
182 NOTIMPLEMENTED(); | 203 NOTIMPLEMENTED(); |
183 } | 204 } |
184 | 205 |
185 void BluetoothDeviceMac::ClearOutOfBandPairingData( | 206 void BluetoothDeviceMac::ClearOutOfBandPairingData( |
186 const base::Closure& callback, | 207 const base::Closure& callback, |
187 const ErrorCallback& error_callback) { | 208 const ErrorCallback& error_callback) { |
188 NOTIMPLEMENTED(); | 209 NOTIMPLEMENTED(); |
189 } | 210 } |
190 | 211 |
191 // static | |
192 uint32 BluetoothDeviceMac::ComputeDeviceFingerprint( | |
193 const IOBluetoothDevice* device) { | |
194 std::string device_string = base::StringPrintf("%s|%s|%u|%d|%d", | |
195 base::SysNSStringToUTF8([device name]).c_str(), | |
196 base::SysNSStringToUTF8([device addressString]).c_str(), | |
197 [device classOfDevice], | |
198 [device isConnected], | |
199 [device isPaired]); | |
200 | |
201 for (IOBluetoothSDPServiceRecord* record in [device services]) { | |
202 base::StringAppendF( | |
203 &device_string, | |
204 "|%s|%lu", | |
205 base::SysNSStringToUTF8([record getServiceName]).c_str(), | |
206 static_cast<unsigned long>([[record attributes] count])); | |
207 } | |
208 | |
209 return base::Hash(device_string); | |
210 } | |
211 | |
212 } // namespace device | 212 } // namespace device |
OLD | NEW |