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

Side by Side Diff: device/bluetooth/bluetooth_device_bluez.cc

Issue 1367663002: Add Linux support for the Bluetooth API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor_dbus
Patch Set: Created 5 years, 2 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_chromeos.h" 5 #include "device/bluetooth/bluetooth_device_bluez.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "dbus/bus.h" 14 #include "dbus/bus.h"
15 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 15 #include "device/bluetooth/bluetooth_adapter_bluez.h"
16 #include "device/bluetooth/bluetooth_gatt_connection_chromeos.h" 16 #include "device/bluetooth/bluetooth_gatt_connection_bluez.h"
17 #include "device/bluetooth/bluetooth_pairing_chromeos.h" 17 #include "device/bluetooth/bluetooth_pairing_bluez.h"
18 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" 18 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h"
19 #include "device/bluetooth/bluetooth_socket.h" 19 #include "device/bluetooth/bluetooth_socket.h"
20 #include "device/bluetooth/bluetooth_socket_chromeos.h" 20 #include "device/bluetooth/bluetooth_socket_bluez.h"
21 #include "device/bluetooth/bluetooth_socket_thread.h" 21 #include "device/bluetooth/bluetooth_socket_thread.h"
22 #include "device/bluetooth/bluetooth_uuid.h" 22 #include "device/bluetooth/bluetooth_uuid.h"
23 #include "device/bluetooth/dbus/bluetooth_adapter_client.h" 23 #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
24 #include "device/bluetooth/dbus/bluetooth_device_client.h" 24 #include "device/bluetooth/dbus/bluetooth_device_client.h"
25 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h" 25 #include "device/bluetooth/dbus/bluetooth_gatt_service_client.h"
26 #include "device/bluetooth/dbus/bluetooth_input_client.h" 26 #include "device/bluetooth/dbus/bluetooth_input_client.h"
27 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 27 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
28 #include "third_party/cros_system_api/dbus/service_constants.h" 28 #include "third_party/cros_system_api/dbus/service_constants.h"
29 29
30 using device::BluetoothDevice; 30 using device::BluetoothDevice;
(...skipping 25 matching lines...) Expand all
56 uint16* device_id) { 56 uint16* device_id) {
57 bluez::BluetoothDeviceClient::Properties* properties = 57 bluez::BluetoothDeviceClient::Properties* properties =
58 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 58 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
59 object_path); 59 object_path);
60 DCHECK(properties); 60 DCHECK(properties);
61 61
62 std::string modalias = properties->modalias.value(); 62 std::string modalias = properties->modalias.value();
63 BluetoothDevice::VendorIDSource source_value; 63 BluetoothDevice::VendorIDSource source_value;
64 int vendor_value, product_value, device_value; 64 int vendor_value, product_value, device_value;
65 65
66 if (sscanf(modalias.c_str(), "bluetooth:v%04xp%04xd%04x", 66 if (sscanf(modalias.c_str(), "bluetooth:v%04xp%04xd%04x", &vendor_value,
67 &vendor_value, &product_value, &device_value) == 3) { 67 &product_value, &device_value) == 3) {
68 source_value = BluetoothDevice::VENDOR_ID_BLUETOOTH; 68 source_value = BluetoothDevice::VENDOR_ID_BLUETOOTH;
69 } else if (sscanf(modalias.c_str(), "usb:v%04xp%04xd%04x", 69 } else if (sscanf(modalias.c_str(), "usb:v%04xp%04xd%04x", &vendor_value,
70 &vendor_value, &product_value, &device_value) == 3) { 70 &product_value, &device_value) == 3) {
71 source_value = BluetoothDevice::VENDOR_ID_USB; 71 source_value = BluetoothDevice::VENDOR_ID_USB;
72 } else { 72 } else {
73 return; 73 return;
74 } 74 }
75 75
76 if (vendor_id_source != NULL) 76 if (vendor_id_source != NULL)
77 *vendor_id_source = source_value; 77 *vendor_id_source = source_value;
78 if (vendor_id != NULL) 78 if (vendor_id != NULL)
79 *vendor_id = vendor_value; 79 *vendor_id = vendor_value;
80 if (product_id != NULL) 80 if (product_id != NULL)
(...skipping 23 matching lines...) Expand all
104 case BluetoothDevice::ERROR_AUTH_TIMEOUT: 104 case BluetoothDevice::ERROR_AUTH_TIMEOUT:
105 pairing_result = UMA_PAIRING_RESULT_AUTH_TIMEOUT; 105 pairing_result = UMA_PAIRING_RESULT_AUTH_TIMEOUT;
106 break; 106 break;
107 case BluetoothDevice::ERROR_UNSUPPORTED_DEVICE: 107 case BluetoothDevice::ERROR_UNSUPPORTED_DEVICE:
108 pairing_result = UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE; 108 pairing_result = UMA_PAIRING_RESULT_UNSUPPORTED_DEVICE;
109 break; 109 break;
110 default: 110 default:
111 pairing_result = UMA_PAIRING_RESULT_UNKNOWN_ERROR; 111 pairing_result = UMA_PAIRING_RESULT_UNKNOWN_ERROR;
112 } 112 }
113 113
114 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult", 114 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult", pairing_result,
115 pairing_result,
116 UMA_PAIRING_RESULT_COUNT); 115 UMA_PAIRING_RESULT_COUNT);
117 } 116 }
118 117
119 } // namespace 118 } // namespace
120 119
121 namespace chromeos { 120 namespace bluez {
122 121
123 BluetoothDeviceChromeOS::BluetoothDeviceChromeOS( 122 BluetoothDeviceBlueZ::BluetoothDeviceBlueZ(
124 BluetoothAdapterChromeOS* adapter, 123 BluetoothAdapterBlueZ* adapter,
125 const dbus::ObjectPath& object_path, 124 const dbus::ObjectPath& object_path,
126 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 125 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
127 scoped_refptr<device::BluetoothSocketThread> socket_thread) 126 scoped_refptr<device::BluetoothSocketThread> socket_thread)
128 : BluetoothDevice(adapter), 127 : BluetoothDevice(adapter),
129 object_path_(object_path), 128 object_path_(object_path),
130 num_connecting_calls_(0), 129 num_connecting_calls_(0),
131 connection_monitor_started_(false), 130 connection_monitor_started_(false),
132 ui_task_runner_(ui_task_runner), 131 ui_task_runner_(ui_task_runner),
133 socket_thread_(socket_thread), 132 socket_thread_(socket_thread),
134 weak_ptr_factory_(this) { 133 weak_ptr_factory_(this) {
135 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver( 134 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()->AddObserver(
136 this); 135 this);
137 136
138 // Add all known GATT services. 137 // Add all known GATT services.
139 const std::vector<dbus::ObjectPath> gatt_services = 138 const std::vector<dbus::ObjectPath> gatt_services =
140 bluez::BluezDBusManager::Get() 139 bluez::BluezDBusManager::Get()
141 ->GetBluetoothGattServiceClient() 140 ->GetBluetoothGattServiceClient()
142 ->GetServices(); 141 ->GetServices();
143 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin(); 142 for (std::vector<dbus::ObjectPath>::const_iterator it = gatt_services.begin();
144 it != gatt_services.end(); ++it) { 143 it != gatt_services.end(); ++it) {
145 GattServiceAdded(*it); 144 GattServiceAdded(*it);
146 } 145 }
147 } 146 }
148 147
149 BluetoothDeviceChromeOS::~BluetoothDeviceChromeOS() { 148 BluetoothDeviceBlueZ::~BluetoothDeviceBlueZ() {
150 bluez::BluezDBusManager::Get() 149 bluez::BluezDBusManager::Get()
151 ->GetBluetoothGattServiceClient() 150 ->GetBluetoothGattServiceClient()
152 ->RemoveObserver(this); 151 ->RemoveObserver(this);
153 152
154 // Copy the GATT services list here and clear the original so that when we 153 // Copy the GATT services list here and clear the original so that when we
155 // send GattServiceRemoved(), GetGattServices() returns no services. 154 // send GattServiceRemoved(), GetGattServices() returns no services.
156 GattServiceMap gatt_services = gatt_services_; 155 GattServiceMap gatt_services = gatt_services_;
157 gatt_services_.clear(); 156 gatt_services_.clear();
158 for (GattServiceMap::iterator iter = gatt_services.begin(); 157 for (GattServiceMap::iterator iter = gatt_services.begin();
159 iter != gatt_services.end(); ++iter) { 158 iter != gatt_services.end(); ++iter) {
160 DCHECK(adapter_); 159 DCHECK(adapter_);
161 adapter()->NotifyGattServiceRemoved( 160 adapter()->NotifyGattServiceRemoved(
162 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second)); 161 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second));
163 delete iter->second; 162 delete iter->second;
164 } 163 }
165 } 164 }
166 165
167 uint32 BluetoothDeviceChromeOS::GetBluetoothClass() const { 166 uint32 BluetoothDeviceBlueZ::GetBluetoothClass() const {
168 bluez::BluetoothDeviceClient::Properties* properties = 167 bluez::BluetoothDeviceClient::Properties* properties =
169 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 168 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
170 object_path_); 169 object_path_);
171 DCHECK(properties); 170 DCHECK(properties);
172 171
173 return properties->bluetooth_class.value(); 172 return properties->bluetooth_class.value();
174 } 173 }
175 174
176 std::string BluetoothDeviceChromeOS::GetDeviceName() const { 175 std::string BluetoothDeviceBlueZ::GetDeviceName() const {
177 bluez::BluetoothDeviceClient::Properties* properties = 176 bluez::BluetoothDeviceClient::Properties* properties =
178 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 177 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
179 object_path_); 178 object_path_);
180 DCHECK(properties); 179 DCHECK(properties);
181 180
182 return properties->alias.value(); 181 return properties->alias.value();
183 } 182 }
184 183
185 void BluetoothDeviceChromeOS::CreateGattConnectionImpl() { 184 void BluetoothDeviceBlueZ::CreateGattConnectionImpl() {
186 // ChromeOS implementation does not use the default CreateGattConnection 185 // BlueZ implementation does not use the default CreateGattConnection
187 // implementation. 186 // implementation.
188 NOTIMPLEMENTED(); 187 NOTIMPLEMENTED();
189 } 188 }
190 189
191 void BluetoothDeviceChromeOS::DisconnectGatt() { 190 void BluetoothDeviceBlueZ::DisconnectGatt() {
192 // ChromeOS implementation does not use the default CreateGattConnection 191 // BlueZ implementation does not use the default CreateGattConnection
193 // implementation. 192 // implementation.
194 NOTIMPLEMENTED(); 193 NOTIMPLEMENTED();
195 } 194 }
196 195
197 std::string BluetoothDeviceChromeOS::GetAddress() const { 196 std::string BluetoothDeviceBlueZ::GetAddress() const {
198 bluez::BluetoothDeviceClient::Properties* properties = 197 bluez::BluetoothDeviceClient::Properties* properties =
199 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 198 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
200 object_path_); 199 object_path_);
201 DCHECK(properties); 200 DCHECK(properties);
202 201
203 return CanonicalizeAddress(properties->address.value()); 202 return CanonicalizeAddress(properties->address.value());
204 } 203 }
205 204
206 BluetoothDevice::VendorIDSource 205 BluetoothDevice::VendorIDSource BluetoothDeviceBlueZ::GetVendorIDSource()
207 BluetoothDeviceChromeOS::GetVendorIDSource() const { 206 const {
208 VendorIDSource vendor_id_source = VENDOR_ID_UNKNOWN; 207 VendorIDSource vendor_id_source = VENDOR_ID_UNKNOWN;
209 ParseModalias(object_path_, &vendor_id_source, NULL, NULL, NULL); 208 ParseModalias(object_path_, &vendor_id_source, NULL, NULL, NULL);
210 return vendor_id_source; 209 return vendor_id_source;
211 } 210 }
212 211
213 uint16 BluetoothDeviceChromeOS::GetVendorID() const { 212 uint16 BluetoothDeviceBlueZ::GetVendorID() const {
214 uint16 vendor_id = 0; 213 uint16 vendor_id = 0;
215 ParseModalias(object_path_, NULL, &vendor_id, NULL, NULL); 214 ParseModalias(object_path_, NULL, &vendor_id, NULL, NULL);
216 return vendor_id; 215 return vendor_id;
217 } 216 }
218 217
219 uint16 BluetoothDeviceChromeOS::GetProductID() const { 218 uint16 BluetoothDeviceBlueZ::GetProductID() const {
220 uint16 product_id = 0; 219 uint16 product_id = 0;
221 ParseModalias(object_path_, NULL, NULL, &product_id, NULL); 220 ParseModalias(object_path_, NULL, NULL, &product_id, NULL);
222 return product_id; 221 return product_id;
223 } 222 }
224 223
225 uint16 BluetoothDeviceChromeOS::GetDeviceID() const { 224 uint16 BluetoothDeviceBlueZ::GetDeviceID() const {
226 uint16 device_id = 0; 225 uint16 device_id = 0;
227 ParseModalias(object_path_, NULL, NULL, NULL, &device_id); 226 ParseModalias(object_path_, NULL, NULL, NULL, &device_id);
228 return device_id; 227 return device_id;
229 } 228 }
230 229
231 bool BluetoothDeviceChromeOS::IsPaired() const { 230 bool BluetoothDeviceBlueZ::IsPaired() const {
232 bluez::BluetoothDeviceClient::Properties* properties = 231 bluez::BluetoothDeviceClient::Properties* properties =
233 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 232 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
234 object_path_); 233 object_path_);
235 DCHECK(properties); 234 DCHECK(properties);
236 235
237 // Trusted devices are devices that don't support pairing but that the 236 // Trusted devices are devices that don't support pairing but that the
238 // user has explicitly connected; it makes no sense for UI purposes to 237 // user has explicitly connected; it makes no sense for UI purposes to
239 // treat them differently from each other. 238 // treat them differently from each other.
240 return properties->paired.value() || properties->trusted.value(); 239 return properties->paired.value() || properties->trusted.value();
241 } 240 }
242 241
243 bool BluetoothDeviceChromeOS::IsConnected() const { 242 bool BluetoothDeviceBlueZ::IsConnected() const {
244 bluez::BluetoothDeviceClient::Properties* properties = 243 bluez::BluetoothDeviceClient::Properties* properties =
245 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 244 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
246 object_path_); 245 object_path_);
247 DCHECK(properties); 246 DCHECK(properties);
248 247
249 return properties->connected.value(); 248 return properties->connected.value();
250 } 249 }
251 250
252 bool BluetoothDeviceChromeOS::IsGattConnected() const { 251 bool BluetoothDeviceBlueZ::IsGattConnected() const {
253 NOTIMPLEMENTED(); 252 NOTIMPLEMENTED();
254 return false; 253 return false;
255 } 254 }
256 255
257 bool BluetoothDeviceChromeOS::IsConnectable() const { 256 bool BluetoothDeviceBlueZ::IsConnectable() const {
258 bluez::BluetoothInputClient::Properties* input_properties = 257 bluez::BluetoothInputClient::Properties* input_properties =
259 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties( 258 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties(
260 object_path_); 259 object_path_);
261 // GetProperties returns NULL when the device does not implement the given 260 // GetProperties returns NULL when the device does not implement the given
262 // interface. Non HID devices are normally connectable. 261 // interface. Non HID devices are normally connectable.
263 if (!input_properties) 262 if (!input_properties)
264 return true; 263 return true;
265 264
266 return input_properties->reconnect_mode.value() != "device"; 265 return input_properties->reconnect_mode.value() != "device";
267 } 266 }
268 267
269 bool BluetoothDeviceChromeOS::IsConnecting() const { 268 bool BluetoothDeviceBlueZ::IsConnecting() const {
270 return num_connecting_calls_ > 0; 269 return num_connecting_calls_ > 0;
271 } 270 }
272 271
273 BluetoothDeviceChromeOS::UUIDList BluetoothDeviceChromeOS::GetUUIDs() const { 272 BluetoothDeviceBlueZ::UUIDList BluetoothDeviceBlueZ::GetUUIDs() const {
274 bluez::BluetoothDeviceClient::Properties* properties = 273 bluez::BluetoothDeviceClient::Properties* properties =
275 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 274 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
276 object_path_); 275 object_path_);
277 DCHECK(properties); 276 DCHECK(properties);
278 277
279 std::vector<device::BluetoothUUID> uuids; 278 std::vector<device::BluetoothUUID> uuids;
280 const std::vector<std::string> &dbus_uuids = properties->uuids.value(); 279 const std::vector<std::string>& dbus_uuids = properties->uuids.value();
281 for (std::vector<std::string>::const_iterator iter = dbus_uuids.begin(); 280 for (std::vector<std::string>::const_iterator iter = dbus_uuids.begin();
282 iter != dbus_uuids.end(); ++iter) { 281 iter != dbus_uuids.end(); ++iter) {
283 device::BluetoothUUID uuid(*iter); 282 device::BluetoothUUID uuid(*iter);
284 DCHECK(uuid.IsValid()); 283 DCHECK(uuid.IsValid());
285 uuids.push_back(uuid); 284 uuids.push_back(uuid);
286 } 285 }
287 return uuids; 286 return uuids;
288 } 287 }
289 288
290 int16 BluetoothDeviceChromeOS::GetInquiryRSSI() const { 289 int16 BluetoothDeviceBlueZ::GetInquiryRSSI() const {
291 bluez::BluetoothDeviceClient::Properties* properties = 290 bluez::BluetoothDeviceClient::Properties* properties =
292 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 291 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
293 object_path_); 292 object_path_);
294 DCHECK(properties); 293 DCHECK(properties);
295 294
296 if (!properties->rssi.is_valid()) 295 if (!properties->rssi.is_valid())
297 return kUnknownPower; 296 return kUnknownPower;
298 297
299 return properties->rssi.value(); 298 return properties->rssi.value();
300 } 299 }
301 300
302 int16 BluetoothDeviceChromeOS::GetInquiryTxPower() const { 301 int16 BluetoothDeviceBlueZ::GetInquiryTxPower() const {
303 bluez::BluetoothDeviceClient::Properties* properties = 302 bluez::BluetoothDeviceClient::Properties* properties =
304 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 303 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
305 object_path_); 304 object_path_);
306 DCHECK(properties); 305 DCHECK(properties);
307 306
308 if (!properties->tx_power.is_valid()) 307 if (!properties->tx_power.is_valid())
309 return kUnknownPower; 308 return kUnknownPower;
310 309
311 return properties->tx_power.value(); 310 return properties->tx_power.value();
312 } 311 }
313 312
314 bool BluetoothDeviceChromeOS::ExpectingPinCode() const { 313 bool BluetoothDeviceBlueZ::ExpectingPinCode() const {
315 return pairing_.get() && pairing_->ExpectingPinCode(); 314 return pairing_.get() && pairing_->ExpectingPinCode();
316 } 315 }
317 316
318 bool BluetoothDeviceChromeOS::ExpectingPasskey() const { 317 bool BluetoothDeviceBlueZ::ExpectingPasskey() const {
319 return pairing_.get() && pairing_->ExpectingPasskey(); 318 return pairing_.get() && pairing_->ExpectingPasskey();
320 } 319 }
321 320
322 bool BluetoothDeviceChromeOS::ExpectingConfirmation() const { 321 bool BluetoothDeviceBlueZ::ExpectingConfirmation() const {
323 return pairing_.get() && pairing_->ExpectingConfirmation(); 322 return pairing_.get() && pairing_->ExpectingConfirmation();
324 } 323 }
325 324
326 void BluetoothDeviceChromeOS::GetConnectionInfo( 325 void BluetoothDeviceBlueZ::GetConnectionInfo(
327 const ConnectionInfoCallback& callback) { 326 const ConnectionInfoCallback& callback) {
328 // DBus method call should gracefully return an error if the device is not 327 // DBus method call should gracefully return an error if the device is not
329 // currently connected. 328 // currently connected.
330 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetConnInfo( 329 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetConnInfo(
331 object_path_, base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfo, 330 object_path_, base::Bind(&BluetoothDeviceBlueZ::OnGetConnInfo,
332 weak_ptr_factory_.GetWeakPtr(), callback), 331 weak_ptr_factory_.GetWeakPtr(), callback),
333 base::Bind(&BluetoothDeviceChromeOS::OnGetConnInfoError, 332 base::Bind(&BluetoothDeviceBlueZ::OnGetConnInfoError,
334 weak_ptr_factory_.GetWeakPtr(), callback)); 333 weak_ptr_factory_.GetWeakPtr(), callback));
335 } 334 }
336 335
337 void BluetoothDeviceChromeOS::Connect( 336 void BluetoothDeviceBlueZ::Connect(
338 BluetoothDevice::PairingDelegate* pairing_delegate, 337 BluetoothDevice::PairingDelegate* pairing_delegate,
339 const base::Closure& callback, 338 const base::Closure& callback,
340 const ConnectErrorCallback& error_callback) { 339 const ConnectErrorCallback& error_callback) {
341 if (num_connecting_calls_++ == 0) 340 if (num_connecting_calls_++ == 0)
342 adapter()->NotifyDeviceChanged(this); 341 adapter()->NotifyDeviceChanged(this);
343 342
344 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_ 343 VLOG(1) << object_path_.value() << ": Connecting, " << num_connecting_calls_
345 << " in progress"; 344 << " in progress";
346 345
347 if (IsPaired() || !pairing_delegate || !IsPairable()) { 346 if (IsPaired() || !pairing_delegate || !IsPairable()) {
348 // No need to pair, or unable to, skip straight to connection. 347 // No need to pair, or unable to, skip straight to connection.
349 ConnectInternal(false, callback, error_callback); 348 ConnectInternal(false, callback, error_callback);
350 } else { 349 } else {
351 // Initiate high-security connection with pairing. 350 // Initiate high-security connection with pairing.
352 BeginPairing(pairing_delegate); 351 BeginPairing(pairing_delegate);
353 352
354 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Pair( 353 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Pair(
355 object_path_, 354 object_path_,
356 base::Bind(&BluetoothDeviceChromeOS::OnPair, 355 base::Bind(&BluetoothDeviceBlueZ::OnPair,
357 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 356 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
358 base::Bind(&BluetoothDeviceChromeOS::OnPairError, 357 base::Bind(&BluetoothDeviceBlueZ::OnPairError,
359 weak_ptr_factory_.GetWeakPtr(), error_callback)); 358 weak_ptr_factory_.GetWeakPtr(), error_callback));
360 } 359 }
361 } 360 }
362 361
363 void BluetoothDeviceChromeOS::SetPinCode(const std::string& pincode) { 362 void BluetoothDeviceBlueZ::SetPinCode(const std::string& pincode) {
364 if (!pairing_.get()) 363 if (!pairing_.get())
365 return; 364 return;
366 365
367 pairing_->SetPinCode(pincode); 366 pairing_->SetPinCode(pincode);
368 } 367 }
369 368
370 void BluetoothDeviceChromeOS::SetPasskey(uint32 passkey) { 369 void BluetoothDeviceBlueZ::SetPasskey(uint32 passkey) {
371 if (!pairing_.get()) 370 if (!pairing_.get())
372 return; 371 return;
373 372
374 pairing_->SetPasskey(passkey); 373 pairing_->SetPasskey(passkey);
375 } 374 }
376 375
377 void BluetoothDeviceChromeOS::ConfirmPairing() { 376 void BluetoothDeviceBlueZ::ConfirmPairing() {
378 if (!pairing_.get()) 377 if (!pairing_.get())
379 return; 378 return;
380 379
381 pairing_->ConfirmPairing(); 380 pairing_->ConfirmPairing();
382 } 381 }
383 382
384 void BluetoothDeviceChromeOS::RejectPairing() { 383 void BluetoothDeviceBlueZ::RejectPairing() {
385 if (!pairing_.get()) 384 if (!pairing_.get())
386 return; 385 return;
387 386
388 pairing_->RejectPairing(); 387 pairing_->RejectPairing();
389 } 388 }
390 389
391 void BluetoothDeviceChromeOS::CancelPairing() { 390 void BluetoothDeviceBlueZ::CancelPairing() {
392 bool canceled = false; 391 bool canceled = false;
393 392
394 // If there is a callback in progress that we can reply to then use that 393 // If there is a callback in progress that we can reply to then use that
395 // to cancel the current pairing request. 394 // to cancel the current pairing request.
396 if (pairing_.get() && pairing_->CancelPairing()) 395 if (pairing_.get() && pairing_->CancelPairing())
397 canceled = true; 396 canceled = true;
398 397
399 // If not we have to send an explicit CancelPairing() to the device instead. 398 // If not we have to send an explicit CancelPairing() to the device instead.
400 if (!canceled) { 399 if (!canceled) {
401 VLOG(1) << object_path_.value() << ": No pairing context or callback. " 400 VLOG(1) << object_path_.value() << ": No pairing context or callback. "
402 << "Sending explicit cancel"; 401 << "Sending explicit cancel";
403 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->CancelPairing( 402 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->CancelPairing(
404 object_path_, base::Bind(&base::DoNothing), 403 object_path_, base::Bind(&base::DoNothing),
405 base::Bind(&BluetoothDeviceChromeOS::OnCancelPairingError, 404 base::Bind(&BluetoothDeviceBlueZ::OnCancelPairingError,
406 weak_ptr_factory_.GetWeakPtr())); 405 weak_ptr_factory_.GetWeakPtr()));
407 } 406 }
408 407
409 // Since there is no callback to this method it's possible that the pairing 408 // Since there is no callback to this method it's possible that the pairing
410 // delegate is going to be freed before things complete (indeed it's 409 // delegate is going to be freed before things complete (indeed it's
411 // documented that this is the method you should call while freeing the 410 // documented that this is the method you should call while freeing the
412 // pairing delegate), so clear our the context holding on to it. 411 // pairing delegate), so clear our the context holding on to it.
413 EndPairing(); 412 EndPairing();
414 } 413 }
415 414
416 void BluetoothDeviceChromeOS::Disconnect(const base::Closure& callback, 415 void BluetoothDeviceBlueZ::Disconnect(const base::Closure& callback,
417 const ErrorCallback& error_callback) { 416 const ErrorCallback& error_callback) {
418 VLOG(1) << object_path_.value() << ": Disconnecting"; 417 VLOG(1) << object_path_.value() << ": Disconnecting";
419 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Disconnect( 418 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Disconnect(
420 object_path_, base::Bind(&BluetoothDeviceChromeOS::OnDisconnect, 419 object_path_, base::Bind(&BluetoothDeviceBlueZ::OnDisconnect,
421 weak_ptr_factory_.GetWeakPtr(), callback), 420 weak_ptr_factory_.GetWeakPtr(), callback),
422 base::Bind(&BluetoothDeviceChromeOS::OnDisconnectError, 421 base::Bind(&BluetoothDeviceBlueZ::OnDisconnectError,
423 weak_ptr_factory_.GetWeakPtr(), error_callback)); 422 weak_ptr_factory_.GetWeakPtr(), error_callback));
424 } 423 }
425 424
426 void BluetoothDeviceChromeOS::Forget(const ErrorCallback& error_callback) { 425 void BluetoothDeviceBlueZ::Forget(const ErrorCallback& error_callback) {
427 VLOG(1) << object_path_.value() << ": Removing device"; 426 VLOG(1) << object_path_.value() << ": Removing device";
428 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->RemoveDevice( 427 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->RemoveDevice(
429 adapter()->object_path(), object_path_, base::Bind(&base::DoNothing), 428 adapter()->object_path(), object_path_, base::Bind(&base::DoNothing),
430 base::Bind(&BluetoothDeviceChromeOS::OnForgetError, 429 base::Bind(&BluetoothDeviceBlueZ::OnForgetError,
431 weak_ptr_factory_.GetWeakPtr(), error_callback)); 430 weak_ptr_factory_.GetWeakPtr(), error_callback));
432 } 431 }
433 432
434 void BluetoothDeviceChromeOS::ConnectToService( 433 void BluetoothDeviceBlueZ::ConnectToService(
435 const BluetoothUUID& uuid, 434 const BluetoothUUID& uuid,
436 const ConnectToServiceCallback& callback, 435 const ConnectToServiceCallback& callback,
437 const ConnectToServiceErrorCallback& error_callback) { 436 const ConnectToServiceErrorCallback& error_callback) {
438 VLOG(1) << object_path_.value() << ": Connecting to service: " 437 VLOG(1) << object_path_.value()
439 << uuid.canonical_value(); 438 << ": Connecting to service: " << uuid.canonical_value();
440 scoped_refptr<BluetoothSocketChromeOS> socket = 439 scoped_refptr<BluetoothSocketBlueZ> socket =
441 BluetoothSocketChromeOS::CreateBluetoothSocket( 440 BluetoothSocketBlueZ::CreateBluetoothSocket(ui_task_runner_,
442 ui_task_runner_, socket_thread_); 441 socket_thread_);
443 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_MEDIUM, 442 socket->Connect(this, uuid, BluetoothSocketBlueZ::SECURITY_LEVEL_MEDIUM,
444 base::Bind(callback, socket), error_callback); 443 base::Bind(callback, socket), error_callback);
445 } 444 }
446 445
447 void BluetoothDeviceChromeOS::ConnectToServiceInsecurely( 446 void BluetoothDeviceBlueZ::ConnectToServiceInsecurely(
448 const BluetoothUUID& uuid, 447 const BluetoothUUID& uuid,
449 const ConnectToServiceCallback& callback, 448 const ConnectToServiceCallback& callback,
450 const ConnectToServiceErrorCallback& error_callback) { 449 const ConnectToServiceErrorCallback& error_callback) {
451 VLOG(1) << object_path_.value() << ": Connecting insecurely to service: " 450 VLOG(1) << object_path_.value()
452 << uuid.canonical_value(); 451 << ": Connecting insecurely to service: " << uuid.canonical_value();
453 scoped_refptr<BluetoothSocketChromeOS> socket = 452 scoped_refptr<BluetoothSocketBlueZ> socket =
454 BluetoothSocketChromeOS::CreateBluetoothSocket( 453 BluetoothSocketBlueZ::CreateBluetoothSocket(ui_task_runner_,
455 ui_task_runner_, socket_thread_); 454 socket_thread_);
456 socket->Connect(this, uuid, BluetoothSocketChromeOS::SECURITY_LEVEL_LOW, 455 socket->Connect(this, uuid, BluetoothSocketBlueZ::SECURITY_LEVEL_LOW,
457 base::Bind(callback, socket), error_callback); 456 base::Bind(callback, socket), error_callback);
458 } 457 }
459 458
460 void BluetoothDeviceChromeOS::CreateGattConnection( 459 void BluetoothDeviceBlueZ::CreateGattConnection(
461 const GattConnectionCallback& callback, 460 const GattConnectionCallback& callback,
462 const ConnectErrorCallback& error_callback) { 461 const ConnectErrorCallback& error_callback) {
463 // TODO(sacomoto): Workaround to retrieve the connection for already connected 462 // TODO(sacomoto): Workaround to retrieve the connection for already connected
464 // devices. Currently, BluetoothGattConnection::Disconnect doesn't do 463 // devices. Currently, BluetoothGattConnection::Disconnect doesn't do
465 // anything, the unique underlying physical GATT connection is kept. This 464 // anything, the unique underlying physical GATT connection is kept. This
466 // should be removed once the correct behavour is implemented and the GATT 465 // should be removed once the correct behavour is implemented and the GATT
467 // connections are reference counted (see todo below). 466 // connections are reference counted (see todo below).
468 if (IsConnected()) { 467 if (IsConnected()) {
469 OnCreateGattConnection(callback); 468 OnCreateGattConnection(callback);
470 return; 469 return;
471 } 470 }
472 471
473 // TODO(armansito): Until there is a way to create a reference counted GATT 472 // TODO(armansito): Until there is a way to create a reference counted GATT
474 // connection in bluetoothd, simply do a regular connect. 473 // connection in bluetoothd, simply do a regular connect.
475 Connect(NULL, 474 Connect(NULL, base::Bind(&BluetoothDeviceBlueZ::OnCreateGattConnection,
476 base::Bind(&BluetoothDeviceChromeOS::OnCreateGattConnection, 475 weak_ptr_factory_.GetWeakPtr(), callback),
477 weak_ptr_factory_.GetWeakPtr(),
478 callback),
479 error_callback); 476 error_callback);
480 } 477 }
481 478
482 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::BeginPairing( 479 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::BeginPairing(
483 BluetoothDevice::PairingDelegate* pairing_delegate) { 480 BluetoothDevice::PairingDelegate* pairing_delegate) {
484 pairing_.reset(new BluetoothPairingChromeOS(this, pairing_delegate)); 481 pairing_.reset(new BluetoothPairingBlueZ(this, pairing_delegate));
485 return pairing_.get(); 482 return pairing_.get();
486 } 483 }
487 484
488 void BluetoothDeviceChromeOS::EndPairing() { 485 void BluetoothDeviceBlueZ::EndPairing() {
489 pairing_.reset(); 486 pairing_.reset();
490 } 487 }
491 488
492 BluetoothPairingChromeOS* BluetoothDeviceChromeOS::GetPairing() const { 489 BluetoothPairingBlueZ* BluetoothDeviceBlueZ::GetPairing() const {
493 return pairing_.get(); 490 return pairing_.get();
494 } 491 }
495 492
496 BluetoothAdapterChromeOS* BluetoothDeviceChromeOS::adapter() const { 493 BluetoothAdapterBlueZ* BluetoothDeviceBlueZ::adapter() const {
497 return static_cast<BluetoothAdapterChromeOS*>(adapter_); 494 return static_cast<BluetoothAdapterBlueZ*>(adapter_);
498 } 495 }
499 496
500 void BluetoothDeviceChromeOS::GattServiceAdded( 497 void BluetoothDeviceBlueZ::GattServiceAdded(
501 const dbus::ObjectPath& object_path) { 498 const dbus::ObjectPath& object_path) {
502 if (GetGattService(object_path.value())) { 499 if (GetGattService(object_path.value())) {
503 VLOG(1) << "Remote GATT service already exists: " << object_path.value(); 500 VLOG(1) << "Remote GATT service already exists: " << object_path.value();
504 return; 501 return;
505 } 502 }
506 503
507 bluez::BluetoothGattServiceClient::Properties* properties = 504 bluez::BluetoothGattServiceClient::Properties* properties =
508 bluez::BluezDBusManager::Get() 505 bluez::BluezDBusManager::Get()
509 ->GetBluetoothGattServiceClient() 506 ->GetBluetoothGattServiceClient()
510 ->GetProperties(object_path); 507 ->GetProperties(object_path);
511 DCHECK(properties); 508 DCHECK(properties);
512 if (properties->device.value() != object_path_) { 509 if (properties->device.value() != object_path_) {
513 VLOG(2) << "Remote GATT service does not belong to this device."; 510 VLOG(2) << "Remote GATT service does not belong to this device.";
514 return; 511 return;
515 } 512 }
516 513
517 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress(); 514 VLOG(1) << "Adding new remote GATT service for device: " << GetAddress();
518 515
519 BluetoothRemoteGattServiceChromeOS* service = 516 BluetoothRemoteGattServiceBlueZ* service =
520 new BluetoothRemoteGattServiceChromeOS(adapter(), this, object_path); 517 new BluetoothRemoteGattServiceBlueZ(adapter(), this, object_path);
521 518
522 gatt_services_[service->GetIdentifier()] = service; 519 gatt_services_[service->GetIdentifier()] = service;
523 DCHECK(service->object_path() == object_path); 520 DCHECK(service->object_path() == object_path);
524 DCHECK(service->GetUUID().IsValid()); 521 DCHECK(service->GetUUID().IsValid());
525 522
526 DCHECK(adapter_); 523 DCHECK(adapter_);
527 adapter()->NotifyGattServiceAdded(service); 524 adapter()->NotifyGattServiceAdded(service);
528 } 525 }
529 526
530 void BluetoothDeviceChromeOS::GattServiceRemoved( 527 void BluetoothDeviceBlueZ::GattServiceRemoved(
531 const dbus::ObjectPath& object_path) { 528 const dbus::ObjectPath& object_path) {
532 GattServiceMap::iterator iter = gatt_services_.find(object_path.value()); 529 GattServiceMap::iterator iter = gatt_services_.find(object_path.value());
533 if (iter == gatt_services_.end()) { 530 if (iter == gatt_services_.end()) {
534 VLOG(3) << "Unknown GATT service removed: " << object_path.value(); 531 VLOG(3) << "Unknown GATT service removed: " << object_path.value();
535 return; 532 return;
536 } 533 }
537 534
538 VLOG(1) << "Removing remote GATT service from device: " << GetAddress(); 535 VLOG(1) << "Removing remote GATT service from device: " << GetAddress();
539 536
540 BluetoothRemoteGattServiceChromeOS* service = 537 BluetoothRemoteGattServiceBlueZ* service =
541 static_cast<BluetoothRemoteGattServiceChromeOS*>(iter->second); 538 static_cast<BluetoothRemoteGattServiceBlueZ*>(iter->second);
542 DCHECK(service->object_path() == object_path); 539 DCHECK(service->object_path() == object_path);
543 gatt_services_.erase(iter); 540 gatt_services_.erase(iter);
544 541
545 DCHECK(adapter_); 542 DCHECK(adapter_);
546 adapter()->NotifyGattServiceRemoved(service); 543 adapter()->NotifyGattServiceRemoved(service);
547 544
548 delete service; 545 delete service;
549 } 546 }
550 547
551 void BluetoothDeviceChromeOS::OnGetConnInfo( 548 void BluetoothDeviceBlueZ::OnGetConnInfo(const ConnectionInfoCallback& callback,
552 const ConnectionInfoCallback& callback, 549 int16 rssi,
553 int16 rssi, 550 int16 transmit_power,
554 int16 transmit_power, 551 int16 max_transmit_power) {
555 int16 max_transmit_power) {
556 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power)); 552 callback.Run(ConnectionInfo(rssi, transmit_power, max_transmit_power));
557 } 553 }
558 554
559 void BluetoothDeviceChromeOS::OnGetConnInfoError( 555 void BluetoothDeviceBlueZ::OnGetConnInfoError(
560 const ConnectionInfoCallback& callback, 556 const ConnectionInfoCallback& callback,
561 const std::string& error_name, 557 const std::string& error_name,
562 const std::string& error_message) { 558 const std::string& error_message) {
563 LOG(WARNING) << object_path_.value() 559 LOG(WARNING) << object_path_.value()
564 << ": Failed to get connection info: " << error_name << ": " 560 << ": Failed to get connection info: " << error_name << ": "
565 << error_message; 561 << error_message;
566 callback.Run(ConnectionInfo()); 562 callback.Run(ConnectionInfo());
567 } 563 }
568 564
569 void BluetoothDeviceChromeOS::ConnectInternal( 565 void BluetoothDeviceBlueZ::ConnectInternal(
570 bool after_pairing, 566 bool after_pairing,
571 const base::Closure& callback, 567 const base::Closure& callback,
572 const ConnectErrorCallback& error_callback) { 568 const ConnectErrorCallback& error_callback) {
573 VLOG(1) << object_path_.value() << ": Connecting"; 569 VLOG(1) << object_path_.value() << ": Connecting";
574 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Connect( 570 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->Connect(
575 object_path_, 571 object_path_,
576 base::Bind(&BluetoothDeviceChromeOS::OnConnect, 572 base::Bind(&BluetoothDeviceBlueZ::OnConnect,
577 weak_ptr_factory_.GetWeakPtr(), after_pairing, callback), 573 weak_ptr_factory_.GetWeakPtr(), after_pairing, callback),
578 base::Bind(&BluetoothDeviceChromeOS::OnConnectError, 574 base::Bind(&BluetoothDeviceBlueZ::OnConnectError,
579 weak_ptr_factory_.GetWeakPtr(), after_pairing, 575 weak_ptr_factory_.GetWeakPtr(), after_pairing,
580 error_callback)); 576 error_callback));
581 } 577 }
582 578
583 void BluetoothDeviceChromeOS::OnConnect(bool after_pairing, 579 void BluetoothDeviceBlueZ::OnConnect(bool after_pairing,
584 const base::Closure& callback) { 580 const base::Closure& callback) {
585 if (--num_connecting_calls_ == 0) 581 if (--num_connecting_calls_ == 0)
586 adapter()->NotifyDeviceChanged(this); 582 adapter()->NotifyDeviceChanged(this);
587 583
588 DCHECK(num_connecting_calls_ >= 0); 584 DCHECK(num_connecting_calls_ >= 0);
589 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_ 585 VLOG(1) << object_path_.value() << ": Connected, " << num_connecting_calls_
590 << " still in progress"; 586 << " still in progress";
591 587
592 SetTrusted(); 588 SetTrusted();
593 589
594 if (after_pairing) 590 if (after_pairing)
595 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult", 591 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingResult",
596 UMA_PAIRING_RESULT_SUCCESS, 592 UMA_PAIRING_RESULT_SUCCESS,
597 UMA_PAIRING_RESULT_COUNT); 593 UMA_PAIRING_RESULT_COUNT);
598 594
599 callback.Run(); 595 callback.Run();
600 } 596 }
601 597
602 void BluetoothDeviceChromeOS::OnCreateGattConnection( 598 void BluetoothDeviceBlueZ::OnCreateGattConnection(
603 const GattConnectionCallback& callback) { 599 const GattConnectionCallback& callback) {
604 scoped_ptr<device::BluetoothGattConnection> conn( 600 scoped_ptr<device::BluetoothGattConnection> conn(
605 new BluetoothGattConnectionChromeOS( 601 new BluetoothGattConnectionBlueZ(adapter_, GetAddress(), object_path_));
606 adapter_, GetAddress(), object_path_));
607 callback.Run(conn.Pass()); 602 callback.Run(conn.Pass());
608 } 603 }
609 604
610 void BluetoothDeviceChromeOS::OnConnectError( 605 void BluetoothDeviceBlueZ::OnConnectError(
611 bool after_pairing, 606 bool after_pairing,
612 const ConnectErrorCallback& error_callback, 607 const ConnectErrorCallback& error_callback,
613 const std::string& error_name, 608 const std::string& error_name,
614 const std::string& error_message) { 609 const std::string& error_message) {
615 if (--num_connecting_calls_ == 0) 610 if (--num_connecting_calls_ == 0)
616 adapter()->NotifyDeviceChanged(this); 611 adapter()->NotifyDeviceChanged(this);
617 612
618 DCHECK(num_connecting_calls_ >= 0); 613 DCHECK(num_connecting_calls_ >= 0);
619 LOG(WARNING) << object_path_.value() << ": Failed to connect device: " 614 LOG(WARNING) << object_path_.value()
620 << error_name << ": " << error_message; 615 << ": Failed to connect device: " << error_name << ": "
616 << error_message;
621 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ 617 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
622 << " still in progress"; 618 << " still in progress";
623 619
624 // Determine the error code from error_name. 620 // Determine the error code from error_name.
625 ConnectErrorCode error_code = ERROR_UNKNOWN; 621 ConnectErrorCode error_code = ERROR_UNKNOWN;
626 if (error_name == bluetooth_device::kErrorFailed) { 622 if (error_name == bluetooth_device::kErrorFailed) {
627 error_code = ERROR_FAILED; 623 error_code = ERROR_FAILED;
628 } else if (error_name == bluetooth_device::kErrorInProgress) { 624 } else if (error_name == bluetooth_device::kErrorInProgress) {
629 error_code = ERROR_INPROGRESS; 625 error_code = ERROR_INPROGRESS;
630 } else if (error_name == bluetooth_device::kErrorNotSupported) { 626 } else if (error_name == bluetooth_device::kErrorNotSupported) {
631 error_code = ERROR_UNSUPPORTED_DEVICE; 627 error_code = ERROR_UNSUPPORTED_DEVICE;
632 } 628 }
633 629
634 if (after_pairing) 630 if (after_pairing)
635 RecordPairingResult(error_code); 631 RecordPairingResult(error_code);
636 error_callback.Run(error_code); 632 error_callback.Run(error_code);
637 } 633 }
638 634
639 void BluetoothDeviceChromeOS::OnPair( 635 void BluetoothDeviceBlueZ::OnPair(const base::Closure& callback,
640 const base::Closure& callback, 636 const ConnectErrorCallback& error_callback) {
641 const ConnectErrorCallback& error_callback) {
642 VLOG(1) << object_path_.value() << ": Paired"; 637 VLOG(1) << object_path_.value() << ": Paired";
643 638
644 EndPairing(); 639 EndPairing();
645 640
646 ConnectInternal(true, callback, error_callback); 641 ConnectInternal(true, callback, error_callback);
647 } 642 }
648 643
649 void BluetoothDeviceChromeOS::OnPairError( 644 void BluetoothDeviceBlueZ::OnPairError(
650 const ConnectErrorCallback& error_callback, 645 const ConnectErrorCallback& error_callback,
651 const std::string& error_name, 646 const std::string& error_name,
652 const std::string& error_message) { 647 const std::string& error_message) {
653 if (--num_connecting_calls_ == 0) 648 if (--num_connecting_calls_ == 0)
654 adapter()->NotifyDeviceChanged(this); 649 adapter()->NotifyDeviceChanged(this);
655 650
656 DCHECK(num_connecting_calls_ >= 0); 651 DCHECK(num_connecting_calls_ >= 0);
657 LOG(WARNING) << object_path_.value() << ": Failed to pair device: " 652 LOG(WARNING) << object_path_.value()
658 << error_name << ": " << error_message; 653 << ": Failed to pair device: " << error_name << ": "
654 << error_message;
659 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_ 655 VLOG(1) << object_path_.value() << ": " << num_connecting_calls_
660 << " still in progress"; 656 << " still in progress";
661 657
662 EndPairing(); 658 EndPairing();
663 659
664 // Determine the error code from error_name. 660 // Determine the error code from error_name.
665 ConnectErrorCode error_code = ERROR_UNKNOWN; 661 ConnectErrorCode error_code = ERROR_UNKNOWN;
666 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) { 662 if (error_name == bluetooth_device::kErrorConnectionAttemptFailed) {
667 error_code = ERROR_FAILED; 663 error_code = ERROR_FAILED;
668 } else if (error_name == bluetooth_device::kErrorFailed) { 664 } else if (error_name == bluetooth_device::kErrorFailed) {
669 error_code = ERROR_FAILED; 665 error_code = ERROR_FAILED;
670 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) { 666 } else if (error_name == bluetooth_device::kErrorAuthenticationFailed) {
671 error_code = ERROR_AUTH_FAILED; 667 error_code = ERROR_AUTH_FAILED;
672 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) { 668 } else if (error_name == bluetooth_device::kErrorAuthenticationCanceled) {
673 error_code = ERROR_AUTH_CANCELED; 669 error_code = ERROR_AUTH_CANCELED;
674 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) { 670 } else if (error_name == bluetooth_device::kErrorAuthenticationRejected) {
675 error_code = ERROR_AUTH_REJECTED; 671 error_code = ERROR_AUTH_REJECTED;
676 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) { 672 } else if (error_name == bluetooth_device::kErrorAuthenticationTimeout) {
677 error_code = ERROR_AUTH_TIMEOUT; 673 error_code = ERROR_AUTH_TIMEOUT;
678 } 674 }
679 675
680 RecordPairingResult(error_code); 676 RecordPairingResult(error_code);
681 error_callback.Run(error_code); 677 error_callback.Run(error_code);
682 } 678 }
683 679
684 void BluetoothDeviceChromeOS::OnCancelPairingError( 680 void BluetoothDeviceBlueZ::OnCancelPairingError(
685 const std::string& error_name, 681 const std::string& error_name,
686 const std::string& error_message) { 682 const std::string& error_message) {
687 LOG(WARNING) << object_path_.value() << ": Failed to cancel pairing: " 683 LOG(WARNING) << object_path_.value()
688 << error_name << ": " << error_message; 684 << ": Failed to cancel pairing: " << error_name << ": "
685 << error_message;
689 } 686 }
690 687
691 void BluetoothDeviceChromeOS::SetTrusted() { 688 void BluetoothDeviceBlueZ::SetTrusted() {
692 // Unconditionally send the property change, rather than checking the value 689 // Unconditionally send the property change, rather than checking the value
693 // first; there's no harm in doing this and it solves any race conditions 690 // first; there's no harm in doing this and it solves any race conditions
694 // with the property becoming true or false and this call happening before 691 // with the property becoming true or false and this call happening before
695 // we get the D-Bus signal about the earlier change. 692 // we get the D-Bus signal about the earlier change.
696 bluez::BluezDBusManager::Get() 693 bluez::BluezDBusManager::Get()
697 ->GetBluetoothDeviceClient() 694 ->GetBluetoothDeviceClient()
698 ->GetProperties(object_path_) 695 ->GetProperties(object_path_)
699 ->trusted.Set(true, base::Bind(&BluetoothDeviceChromeOS::OnSetTrusted, 696 ->trusted.Set(true, base::Bind(&BluetoothDeviceBlueZ::OnSetTrusted,
700 weak_ptr_factory_.GetWeakPtr())); 697 weak_ptr_factory_.GetWeakPtr()));
701 } 698 }
702 699
703 void BluetoothDeviceChromeOS::OnSetTrusted(bool success) { 700 void BluetoothDeviceBlueZ::OnSetTrusted(bool success) {
704 LOG_IF(WARNING, !success) << object_path_.value() 701 LOG_IF(WARNING, !success) << object_path_.value()
705 << ": Failed to set device as trusted"; 702 << ": Failed to set device as trusted";
706 } 703 }
707 704
708 void BluetoothDeviceChromeOS::OnDisconnect(const base::Closure& callback) { 705 void BluetoothDeviceBlueZ::OnDisconnect(const base::Closure& callback) {
709 VLOG(1) << object_path_.value() << ": Disconnected"; 706 VLOG(1) << object_path_.value() << ": Disconnected";
710 callback.Run(); 707 callback.Run();
711 } 708 }
712 709
713 void BluetoothDeviceChromeOS::OnDisconnectError( 710 void BluetoothDeviceBlueZ::OnDisconnectError(
714 const ErrorCallback& error_callback, 711 const ErrorCallback& error_callback,
715 const std::string& error_name, 712 const std::string& error_name,
716 const std::string& error_message) { 713 const std::string& error_message) {
717 LOG(WARNING) << object_path_.value() << ": Failed to disconnect device: " 714 LOG(WARNING) << object_path_.value()
718 << error_name << ": " << error_message; 715 << ": Failed to disconnect device: " << error_name << ": "
716 << error_message;
719 error_callback.Run(); 717 error_callback.Run();
720 } 718 }
721 719
722 void BluetoothDeviceChromeOS::OnForgetError( 720 void BluetoothDeviceBlueZ::OnForgetError(const ErrorCallback& error_callback,
723 const ErrorCallback& error_callback, 721 const std::string& error_name,
724 const std::string& error_name, 722 const std::string& error_message) {
725 const std::string& error_message) { 723 LOG(WARNING) << object_path_.value()
726 LOG(WARNING) << object_path_.value() << ": Failed to remove device: " 724 << ": Failed to remove device: " << error_name << ": "
727 << error_name << ": " << error_message; 725 << error_message;
728 error_callback.Run(); 726 error_callback.Run();
729 } 727 }
730 728
731 } // namespace chromeos 729 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698