Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: device/bluetooth/bluetooth_device_mac.mm

Issue 13983004: Simplified BluetoothDeviceMac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 std::string numbers_only = uuid;
Mark Mentovai 2013/04/11 21:48:58 DCHECK that the string has the right length, and t
youngki 2013/04/11 22:14:29 Done.
43 numbers_only.erase(8, 1);
Mark Mentovai 2013/04/11 21:48:58 It’s a tiny and pointless bit less memmoving if yo
youngki 2013/04/11 22:14:29 Done.
44 numbers_only.erase(12, 1);
45 numbers_only.erase(16, 1);
46 numbers_only.erase(20, 1);
47 std::vector<uint8> uuid_bytes_vector;
48 base::HexStringToBytes(numbers_only, &uuid_bytes_vector);
Mark Mentovai 2013/04/11 21:48:58 …DCHECK that uuid_bytes_vector is the expected siz
youngki 2013/04/11 22:14:29 Done.
49 uint8 uuid_bytes[16];
Mark Mentovai 2013/04/11 21:48:58 Why the separate array and the copy? Why not uuidW
youngki 2013/04/11 22:14:29 Done.
50 std::copy(uuid_bytes_vector.begin(),
51 uuid_bytes_vector.end(),
52 uuid_bytes);
53
54 return [IOBluetoothSDPUUID uuidWithBytes:uuid_bytes length:16];
55 }
56
57 } // namespace
58
34 namespace device { 59 namespace device {
35 60
36 BluetoothDeviceMac::BluetoothDeviceMac(const IOBluetoothDevice* device) 61 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device)
37 : BluetoothDevice(), 62 : 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 } 63 }
52 64
53 BluetoothDeviceMac::~BluetoothDeviceMac() { 65 BluetoothDeviceMac::~BluetoothDeviceMac() {
66 [device_ release];
54 } 67 }
55 68
56 uint32 BluetoothDeviceMac::GetBluetoothClass() const { 69 uint32 BluetoothDeviceMac::GetBluetoothClass() const {
57 return bluetooth_class_; 70 return [device_ classOfDevice];
58 } 71 }
59 72
60 std::string BluetoothDeviceMac::GetDeviceName() const { 73 std::string BluetoothDeviceMac::GetDeviceName() const {
61 return name_; 74 return base::SysNSStringToUTF8([device_ name]);
62 } 75 }
63 76
64 std::string BluetoothDeviceMac::GetAddress() const { 77 std::string BluetoothDeviceMac::GetAddress() const {
65 return address_; 78 return base::SysNSStringToUTF8([device_ addressString]);
66 } 79 }
67 80
68 bool BluetoothDeviceMac::IsPaired() const { 81 bool BluetoothDeviceMac::IsPaired() const {
69 return paired_; 82 return [device_ isPaired];
70 } 83 }
71 84
72 bool BluetoothDeviceMac::IsConnected() const { 85 bool BluetoothDeviceMac::IsConnected() const {
73 return connected_; 86 return [device_ isConnected];
74 } 87 }
75 88
76 bool BluetoothDeviceMac::IsConnectable() const { 89 bool BluetoothDeviceMac::IsConnectable() const {
77 return false; 90 return false;
78 } 91 }
79 92
80 bool BluetoothDeviceMac::IsConnecting() const { 93 bool BluetoothDeviceMac::IsConnecting() const {
81 return false; 94 return false;
82 } 95 }
83 96
97 // TODO(youngki): BluetoothServiceRecord is deprecated; implement it without
Mark Mentovai 2013/04/11 21:48:58 “it” seems to refer to BluetoothServiceRecord here
youngki 2013/04/11 22:14:29 Changed 'it' to 'this method'.
98 // using BluetoothServiceRecord.
84 BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const { 99 BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const {
85 return service_uuids_; 100 ServiceList service_uuids;
101 for (IOBluetoothSDPServiceRecord* service in [device_ services]) {
102 BluetoothServiceRecordMac service_record(service);
103 service_uuids.push_back(service_record.uuid());
104 }
105 return service_uuids;
86 } 106 }
87 107
108 // NOTE(youngki): This method is deprecated; it will be removed soon.
88 void BluetoothDeviceMac::GetServiceRecords( 109 void BluetoothDeviceMac::GetServiceRecords(
89 const ServiceRecordsCallback& callback, 110 const ServiceRecordsCallback& callback,
90 const ErrorCallback& error_callback) { 111 const ErrorCallback& error_callback) {
91 callback.Run(service_record_list_); 112 ServiceRecordList service_record_list;
113 for (IOBluetoothSDPServiceRecord* service in [device_ services]) {
114 BluetoothServiceRecord* service_record =
115 new BluetoothServiceRecordMac(service);
116 service_record_list.push_back(service_record);
117 }
118
119 callback.Run(service_record_list);
92 } 120 }
93 121
122 // NOTE(youngki): This method is deprecated; it will be removed soon.
94 void BluetoothDeviceMac::ProvidesServiceWithName( 123 void BluetoothDeviceMac::ProvidesServiceWithName(
95 const std::string& name, 124 const std::string& name,
96 const ProvidesServiceCallback& callback) { 125 const ProvidesServiceCallback& callback) {
97 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); 126 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); 127 callback.Run(false);
106 } 128 }
107 129
108 bool BluetoothDeviceMac::ExpectingPinCode() const { 130 bool BluetoothDeviceMac::ExpectingPinCode() const {
109 NOTIMPLEMENTED(); 131 NOTIMPLEMENTED();
110 return false; 132 return false;
111 } 133 }
112 134
113 bool BluetoothDeviceMac::ExpectingPasskey() const { 135 bool BluetoothDeviceMac::ExpectingPasskey() const {
114 NOTIMPLEMENTED(); 136 NOTIMPLEMENTED();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 NOTIMPLEMENTED(); 175 NOTIMPLEMENTED();
154 } 176 }
155 177
156 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { 178 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) {
157 NOTIMPLEMENTED(); 179 NOTIMPLEMENTED();
158 } 180 }
159 181
160 void BluetoothDeviceMac::ConnectToService( 182 void BluetoothDeviceMac::ConnectToService(
161 const std::string& service_uuid, 183 const std::string& service_uuid,
162 const SocketCallback& callback) { 184 const SocketCallback& callback) {
163 for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); 185 IOBluetoothSDPServiceRecord* record =
164 iter != service_record_list_.end(); 186 [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)];
165 ++iter) { 187 if (record != nil) {
166 if ((*iter)->uuid() == service_uuid) { 188 BluetoothServiceRecordMac service_record(record);
167 // If multiple service records are found, use the first one that works. 189 scoped_refptr<BluetoothSocket> socket(
168 scoped_refptr<BluetoothSocket> socket( 190 BluetoothSocketMac::CreateBluetoothSocket(service_record));
169 BluetoothSocketMac::CreateBluetoothSocket(**iter)); 191 if (socket.get() != NULL)
170 if (socket.get() != NULL) { 192 callback.Run(socket);
171 callback.Run(socket);
172 return;
173 }
174 }
175 } 193 }
176 } 194 }
177 195
178 void BluetoothDeviceMac::SetOutOfBandPairingData( 196 void BluetoothDeviceMac::SetOutOfBandPairingData(
179 const BluetoothOutOfBandPairingData& data, 197 const BluetoothOutOfBandPairingData& data,
180 const base::Closure& callback, 198 const base::Closure& callback,
181 const ErrorCallback& error_callback) { 199 const ErrorCallback& error_callback) {
182 NOTIMPLEMENTED(); 200 NOTIMPLEMENTED();
183 } 201 }
184 202
185 void BluetoothDeviceMac::ClearOutOfBandPairingData( 203 void BluetoothDeviceMac::ClearOutOfBandPairingData(
186 const base::Closure& callback, 204 const base::Closure& callback,
187 const ErrorCallback& error_callback) { 205 const ErrorCallback& error_callback) {
188 NOTIMPLEMENTED(); 206 NOTIMPLEMENTED();
189 } 207 }
190 208
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 209 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698