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

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

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

Powered by Google App Engine
This is Rietveld 408576698