OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "base/sequenced_task_runner.h" | |
14 #include "base/single_thread_task_runner.h" | |
15 #include "base/thread_task_runner_handle.h" | |
16 #include "chromeos/system/devicetype.h" | |
17 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
18 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" | |
19 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h" | |
20 #include "device/bluetooth/bluetooth_device.h" | |
21 #include "device/bluetooth/bluetooth_device_chromeos.h" | |
22 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | |
23 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | |
24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" | |
25 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" | |
26 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" | |
27 #include "device/bluetooth/bluetooth_socket_chromeos.h" | |
28 #include "device/bluetooth/bluetooth_socket_thread.h" | |
29 #include "device/bluetooth/bluetooth_uuid.h" | |
30 #include "device/bluetooth/dbus/bluetooth_adapter_client.h" | |
31 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" | |
32 #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h" | |
33 #include "device/bluetooth/dbus/bluetooth_device_client.h" | |
34 #include "device/bluetooth/dbus/bluetooth_input_client.h" | |
35 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
36 #include "third_party/cros_system_api/dbus/service_constants.h" | |
37 | |
38 using device::BluetoothAdapter; | |
39 using device::BluetoothAudioSink; | |
40 using device::BluetoothDevice; | |
41 using device::BluetoothDiscoveryFilter; | |
42 using device::BluetoothSocket; | |
43 using device::BluetoothUUID; | |
44 using device::UMABluetoothDiscoverySessionOutcome; | |
45 | |
46 namespace { | |
47 | |
48 // The agent path is relatively meaningless since BlueZ only permits one to | |
49 // exist per D-Bus connection, it just has to be unique within Chromium. | |
50 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; | |
51 | |
52 void OnUnregisterAgentError(const std::string& error_name, | |
53 const std::string& error_message) { | |
54 // It's okay if the agent didn't exist, it means we never saw an adapter. | |
55 if (error_name == bluetooth_agent_manager::kErrorDoesNotExist) | |
56 return; | |
57 | |
58 LOG(WARNING) << "Failed to unregister pairing agent: " | |
59 << error_name << ": " << error_message; | |
60 } | |
61 | |
62 UMABluetoothDiscoverySessionOutcome TranslateDiscoveryErrorToUMA( | |
63 const std::string& error_name) { | |
64 if (error_name == bluez::BluetoothAdapterClient::kUnknownAdapterError) { | |
65 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_UNKNOWN_ADAPTER; | |
66 } else if (error_name == bluez::BluetoothAdapterClient::kNoResponseError) { | |
67 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NO_RESPONSE; | |
68 } else if (error_name == bluetooth_device::kErrorInProgress) { | |
69 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_IN_PROGRESS; | |
70 } else if (error_name == bluetooth_device::kErrorNotReady) { | |
71 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NOT_READY; | |
72 } else if (error_name == bluetooth_device::kErrorFailed) { | |
73 return UMABluetoothDiscoverySessionOutcome::FAILED; | |
74 } else { | |
75 LOG(WARNING) << "Can't histogram DBus error " << error_name; | |
76 return UMABluetoothDiscoverySessionOutcome::UNKNOWN; | |
77 } | |
78 } | |
79 | |
80 } // namespace | |
81 | |
82 namespace device { | |
83 | |
84 // static | |
85 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( | |
86 const InitCallback& init_callback) { | |
87 return chromeos::BluetoothAdapterChromeOS::CreateAdapter(); | |
88 } | |
89 | |
90 } // namespace device | |
91 | |
92 namespace chromeos { | |
93 | |
94 // static | |
95 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { | |
96 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); | |
97 return adapter->weak_ptr_factory_.GetWeakPtr(); | |
98 } | |
99 | |
100 void BluetoothAdapterChromeOS::Shutdown() { | |
101 if (dbus_is_shutdown_) | |
102 return; | |
103 DCHECK(bluez::BluezDBusManager::IsInitialized()) | |
104 << "Call BluetoothAdapterFactory::Shutdown() before " | |
105 "BluezDBusManager::Shutdown()."; | |
106 | |
107 if (IsPresent()) | |
108 RemoveAdapter(); // Also deletes devices_. | |
109 DCHECK(devices_.empty()); | |
110 // profiles_ is empty because all BluetoothSockets have been notified | |
111 // that this adapter is disappearing. | |
112 DCHECK(profiles_.empty()); | |
113 | |
114 for (auto& it : profile_queues_) | |
115 delete it.second; | |
116 profile_queues_.clear(); | |
117 | |
118 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->RemoveObserver( | |
119 this); | |
120 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver( | |
121 this); | |
122 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->RemoveObserver( | |
123 this); | |
124 | |
125 VLOG(1) << "Unregistering pairing agent"; | |
126 bluez::BluezDBusManager::Get() | |
127 ->GetBluetoothAgentManagerClient() | |
128 ->UnregisterAgent(dbus::ObjectPath(kAgentPath), | |
129 base::Bind(&base::DoNothing), | |
130 base::Bind(&OnUnregisterAgentError)); | |
131 | |
132 agent_.reset(); | |
133 dbus_is_shutdown_ = true; | |
134 } | |
135 | |
136 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() | |
137 : dbus_is_shutdown_(false), | |
138 num_discovery_sessions_(0), | |
139 discovery_request_pending_(false), | |
140 weak_ptr_factory_(this) { | |
141 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
142 socket_thread_ = device::BluetoothSocketThread::Get(); | |
143 | |
144 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->AddObserver( | |
145 this); | |
146 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); | |
147 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->AddObserver(this); | |
148 | |
149 // Register the pairing agent. | |
150 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); | |
151 agent_.reset(bluez::BluetoothAgentServiceProvider::Create( | |
152 system_bus, dbus::ObjectPath(kAgentPath), this)); | |
153 DCHECK(agent_.get()); | |
154 | |
155 std::vector<dbus::ObjectPath> object_paths = bluez::BluezDBusManager::Get() | |
156 ->GetBluetoothAdapterClient() | |
157 ->GetAdapters(); | |
158 | |
159 if (!object_paths.empty()) { | |
160 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; | |
161 SetAdapter(object_paths[0]); | |
162 } | |
163 } | |
164 | |
165 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { | |
166 Shutdown(); | |
167 } | |
168 | |
169 std::string BluetoothAdapterChromeOS::GetAddress() const { | |
170 if (!IsPresent()) | |
171 return std::string(); | |
172 | |
173 bluez::BluetoothAdapterClient::Properties* properties = | |
174 bluez::BluezDBusManager::Get() | |
175 ->GetBluetoothAdapterClient() | |
176 ->GetProperties(object_path_); | |
177 DCHECK(properties); | |
178 | |
179 return BluetoothDevice::CanonicalizeAddress(properties->address.value()); | |
180 } | |
181 | |
182 std::string BluetoothAdapterChromeOS::GetName() const { | |
183 if (!IsPresent()) | |
184 return std::string(); | |
185 | |
186 bluez::BluetoothAdapterClient::Properties* properties = | |
187 bluez::BluezDBusManager::Get() | |
188 ->GetBluetoothAdapterClient() | |
189 ->GetProperties(object_path_); | |
190 DCHECK(properties); | |
191 | |
192 return properties->alias.value(); | |
193 } | |
194 | |
195 void BluetoothAdapterChromeOS::SetName(const std::string& name, | |
196 const base::Closure& callback, | |
197 const ErrorCallback& error_callback) { | |
198 if (!IsPresent()) { | |
199 error_callback.Run(); | |
200 return; | |
201 } | |
202 | |
203 bluez::BluezDBusManager::Get() | |
204 ->GetBluetoothAdapterClient() | |
205 ->GetProperties(object_path_) | |
206 ->alias.Set( | |
207 name, | |
208 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
209 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
210 } | |
211 | |
212 bool BluetoothAdapterChromeOS::IsInitialized() const { | |
213 return true; | |
214 } | |
215 | |
216 bool BluetoothAdapterChromeOS::IsPresent() const { | |
217 return !dbus_is_shutdown_ && !object_path_.value().empty(); | |
218 } | |
219 | |
220 bool BluetoothAdapterChromeOS::IsPowered() const { | |
221 if (!IsPresent()) | |
222 return false; | |
223 | |
224 bluez::BluetoothAdapterClient::Properties* properties = | |
225 bluez::BluezDBusManager::Get() | |
226 ->GetBluetoothAdapterClient() | |
227 ->GetProperties(object_path_); | |
228 | |
229 return properties->powered.value(); | |
230 } | |
231 | |
232 void BluetoothAdapterChromeOS::SetPowered( | |
233 bool powered, | |
234 const base::Closure& callback, | |
235 const ErrorCallback& error_callback) { | |
236 if (!IsPresent()) { | |
237 error_callback.Run(); | |
238 return; | |
239 } | |
240 | |
241 bluez::BluezDBusManager::Get() | |
242 ->GetBluetoothAdapterClient() | |
243 ->GetProperties(object_path_) | |
244 ->powered.Set( | |
245 powered, | |
246 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
247 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
248 } | |
249 | |
250 bool BluetoothAdapterChromeOS::IsDiscoverable() const { | |
251 if (!IsPresent()) | |
252 return false; | |
253 | |
254 bluez::BluetoothAdapterClient::Properties* properties = | |
255 bluez::BluezDBusManager::Get() | |
256 ->GetBluetoothAdapterClient() | |
257 ->GetProperties(object_path_); | |
258 | |
259 return properties->discoverable.value(); | |
260 } | |
261 | |
262 void BluetoothAdapterChromeOS::SetDiscoverable( | |
263 bool discoverable, | |
264 const base::Closure& callback, | |
265 const ErrorCallback& error_callback) { | |
266 if (!IsPresent()) { | |
267 error_callback.Run(); | |
268 return; | |
269 } | |
270 | |
271 bluez::BluezDBusManager::Get() | |
272 ->GetBluetoothAdapterClient() | |
273 ->GetProperties(object_path_) | |
274 ->discoverable.Set( | |
275 discoverable, | |
276 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoverable, | |
277 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
278 } | |
279 | |
280 bool BluetoothAdapterChromeOS::IsDiscovering() const { | |
281 if (!IsPresent()) | |
282 return false; | |
283 | |
284 bluez::BluetoothAdapterClient::Properties* properties = | |
285 bluez::BluezDBusManager::Get() | |
286 ->GetBluetoothAdapterClient() | |
287 ->GetProperties(object_path_); | |
288 | |
289 return properties->discovering.value(); | |
290 } | |
291 | |
292 void BluetoothAdapterChromeOS::CreateRfcommService( | |
293 const BluetoothUUID& uuid, | |
294 const ServiceOptions& options, | |
295 const CreateServiceCallback& callback, | |
296 const CreateServiceErrorCallback& error_callback) { | |
297 DCHECK(!dbus_is_shutdown_); | |
298 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " | |
299 << uuid.canonical_value(); | |
300 scoped_refptr<BluetoothSocketChromeOS> socket = | |
301 BluetoothSocketChromeOS::CreateBluetoothSocket( | |
302 ui_task_runner_, socket_thread_); | |
303 socket->Listen(this, | |
304 BluetoothSocketChromeOS::kRfcomm, | |
305 uuid, | |
306 options, | |
307 base::Bind(callback, socket), | |
308 error_callback); | |
309 } | |
310 | |
311 void BluetoothAdapterChromeOS::CreateL2capService( | |
312 const BluetoothUUID& uuid, | |
313 const ServiceOptions& options, | |
314 const CreateServiceCallback& callback, | |
315 const CreateServiceErrorCallback& error_callback) { | |
316 DCHECK(!dbus_is_shutdown_); | |
317 VLOG(1) << object_path_.value() << ": Creating L2CAP service: " | |
318 << uuid.canonical_value(); | |
319 scoped_refptr<BluetoothSocketChromeOS> socket = | |
320 BluetoothSocketChromeOS::CreateBluetoothSocket( | |
321 ui_task_runner_, socket_thread_); | |
322 socket->Listen(this, | |
323 BluetoothSocketChromeOS::kL2cap, | |
324 uuid, | |
325 options, | |
326 base::Bind(callback, socket), | |
327 error_callback); | |
328 } | |
329 | |
330 void BluetoothAdapterChromeOS::RegisterAudioSink( | |
331 const BluetoothAudioSink::Options& options, | |
332 const device::BluetoothAdapter::AcquiredCallback& callback, | |
333 const BluetoothAudioSink::ErrorCallback& error_callback) { | |
334 VLOG(1) << "Registering audio sink"; | |
335 if (!this->IsPresent()) { | |
336 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); | |
337 return; | |
338 } | |
339 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink( | |
340 new BluetoothAudioSinkChromeOS(this)); | |
341 audio_sink->Register( | |
342 options, base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, | |
343 weak_ptr_factory_.GetWeakPtr(), callback, | |
344 error_callback, audio_sink), | |
345 error_callback); | |
346 } | |
347 | |
348 void BluetoothAdapterChromeOS::RegisterAdvertisement( | |
349 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data, | |
350 const CreateAdvertisementCallback& callback, | |
351 const CreateAdvertisementErrorCallback& error_callback) { | |
352 scoped_refptr<BluetoothAdvertisementChromeOS> advertisement( | |
353 new BluetoothAdvertisementChromeOS(advertisement_data.Pass(), this)); | |
354 advertisement->Register(base::Bind(callback, advertisement), error_callback); | |
355 } | |
356 | |
357 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( | |
358 BluetoothDevice::PairingDelegate* pairing_delegate) { | |
359 // Check if any device is using the pairing delegate. | |
360 // If so, clear the pairing context which will make any responses no-ops. | |
361 for (DevicesMap::iterator iter = devices_.begin(); | |
362 iter != devices_.end(); ++iter) { | |
363 BluetoothDeviceChromeOS* device_chromeos = | |
364 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
365 | |
366 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | |
367 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) | |
368 device_chromeos->EndPairing(); | |
369 } | |
370 } | |
371 | |
372 void BluetoothAdapterChromeOS::AdapterAdded( | |
373 const dbus::ObjectPath& object_path) { | |
374 // Set the adapter to the newly added adapter only if no adapter is present. | |
375 if (!IsPresent()) | |
376 SetAdapter(object_path); | |
377 } | |
378 | |
379 void BluetoothAdapterChromeOS::AdapterRemoved( | |
380 const dbus::ObjectPath& object_path) { | |
381 if (object_path == object_path_) | |
382 RemoveAdapter(); | |
383 } | |
384 | |
385 void BluetoothAdapterChromeOS::AdapterPropertyChanged( | |
386 const dbus::ObjectPath& object_path, | |
387 const std::string& property_name) { | |
388 if (object_path != object_path_) | |
389 return; | |
390 DCHECK(IsPresent()); | |
391 | |
392 bluez::BluetoothAdapterClient::Properties* properties = | |
393 bluez::BluezDBusManager::Get() | |
394 ->GetBluetoothAdapterClient() | |
395 ->GetProperties(object_path_); | |
396 | |
397 if (property_name == properties->powered.name()) { | |
398 PoweredChanged(properties->powered.value()); | |
399 } else if (property_name == properties->discoverable.name()) { | |
400 DiscoverableChanged(properties->discoverable.value()); | |
401 } else if (property_name == properties->discovering.name()) { | |
402 DiscoveringChanged(properties->discovering.value()); | |
403 } | |
404 } | |
405 | |
406 void BluetoothAdapterChromeOS::DeviceAdded( | |
407 const dbus::ObjectPath& object_path) { | |
408 DCHECK(bluez::BluezDBusManager::Get()); | |
409 bluez::BluetoothDeviceClient::Properties* properties = | |
410 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
411 object_path); | |
412 if (!properties || properties->adapter.value() != object_path_) | |
413 return; | |
414 DCHECK(IsPresent()); | |
415 | |
416 BluetoothDeviceChromeOS* device_chromeos = | |
417 new BluetoothDeviceChromeOS(this, | |
418 object_path, | |
419 ui_task_runner_, | |
420 socket_thread_); | |
421 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | |
422 | |
423 devices_[device_chromeos->GetAddress()] = device_chromeos; | |
424 | |
425 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
426 DeviceAdded(this, device_chromeos)); | |
427 } | |
428 | |
429 void BluetoothAdapterChromeOS::DeviceRemoved( | |
430 const dbus::ObjectPath& object_path) { | |
431 for (DevicesMap::iterator iter = devices_.begin(); | |
432 iter != devices_.end(); ++iter) { | |
433 BluetoothDeviceChromeOS* device_chromeos = | |
434 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
435 if (device_chromeos->object_path() == object_path) { | |
436 devices_.erase(iter); | |
437 | |
438 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
439 DeviceRemoved(this, device_chromeos)); | |
440 delete device_chromeos; | |
441 return; | |
442 } | |
443 } | |
444 } | |
445 | |
446 void BluetoothAdapterChromeOS::DevicePropertyChanged( | |
447 const dbus::ObjectPath& object_path, | |
448 const std::string& property_name) { | |
449 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
450 if (!device_chromeos) | |
451 return; | |
452 | |
453 bluez::BluetoothDeviceClient::Properties* properties = | |
454 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( | |
455 object_path); | |
456 | |
457 if (property_name == properties->address.name()) { | |
458 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { | |
459 if (iter->second->GetAddress() == device_chromeos->GetAddress()) { | |
460 std::string old_address = iter->first; | |
461 VLOG(1) << "Device changed address, old: " << old_address | |
462 << " new: " << device_chromeos->GetAddress(); | |
463 devices_.erase(iter); | |
464 | |
465 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); | |
466 devices_[device_chromeos->GetAddress()] = device_chromeos; | |
467 NotifyDeviceAddressChanged(device_chromeos, old_address); | |
468 break; | |
469 } | |
470 } | |
471 } | |
472 | |
473 if (property_name == properties->bluetooth_class.name() || | |
474 property_name == properties->address.name() || | |
475 property_name == properties->alias.name() || | |
476 property_name == properties->paired.name() || | |
477 property_name == properties->trusted.name() || | |
478 property_name == properties->connected.name() || | |
479 property_name == properties->uuids.name() || | |
480 property_name == properties->rssi.name() || | |
481 property_name == properties->tx_power.name()) { | |
482 NotifyDeviceChanged(device_chromeos); | |
483 } | |
484 | |
485 // When a device becomes paired, mark it as trusted so that the user does | |
486 // not need to approve every incoming connection | |
487 if (property_name == properties->paired.name() && | |
488 properties->paired.value() && !properties->trusted.value()) { | |
489 device_chromeos->SetTrusted(); | |
490 } | |
491 | |
492 // UMA connection counting | |
493 if (property_name == properties->connected.name()) { | |
494 // PlayStation joystick tries to reconnect after disconnection from USB. | |
495 // If it is still not trusted, set it, so it becomes available on the | |
496 // list of known devices. | |
497 if (properties->connected.value() && device_chromeos->IsTrustable() && | |
498 !properties->trusted.value()) | |
499 device_chromeos->SetTrusted(); | |
500 | |
501 int count = 0; | |
502 | |
503 for (DevicesMap::iterator iter = devices_.begin(); | |
504 iter != devices_.end(); ++iter) { | |
505 if (iter->second->IsPaired() && iter->second->IsConnected()) | |
506 ++count; | |
507 } | |
508 | |
509 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); | |
510 } | |
511 } | |
512 | |
513 void BluetoothAdapterChromeOS::InputPropertyChanged( | |
514 const dbus::ObjectPath& object_path, | |
515 const std::string& property_name) { | |
516 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
517 if (!device_chromeos) | |
518 return; | |
519 | |
520 bluez::BluetoothInputClient::Properties* properties = | |
521 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties( | |
522 object_path); | |
523 | |
524 // Properties structure can be removed, which triggers a change in the | |
525 // BluetoothDevice::IsConnectable() property, as does a change in the | |
526 // actual reconnect_mode property. | |
527 if (!properties || property_name == properties->reconnect_mode.name()) { | |
528 NotifyDeviceChanged(device_chromeos); | |
529 } | |
530 } | |
531 | |
532 void BluetoothAdapterChromeOS::Released() { | |
533 VLOG(1) << "Release"; | |
534 if (!IsPresent()) | |
535 return; | |
536 DCHECK(agent_.get()); | |
537 | |
538 // Called after we unregister the pairing agent, e.g. when changing I/O | |
539 // capabilities. Nothing much to be done right now. | |
540 } | |
541 | |
542 void BluetoothAdapterChromeOS::RequestPinCode( | |
543 const dbus::ObjectPath& device_path, | |
544 const PinCodeCallback& callback) { | |
545 DCHECK(IsPresent()); | |
546 DCHECK(agent_.get()); | |
547 VLOG(1) << device_path.value() << ": RequestPinCode"; | |
548 | |
549 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
550 if (!pairing) { | |
551 callback.Run(REJECTED, ""); | |
552 return; | |
553 } | |
554 | |
555 pairing->RequestPinCode(callback); | |
556 } | |
557 | |
558 void BluetoothAdapterChromeOS::DisplayPinCode( | |
559 const dbus::ObjectPath& device_path, | |
560 const std::string& pincode) { | |
561 DCHECK(IsPresent()); | |
562 DCHECK(agent_.get()); | |
563 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; | |
564 | |
565 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
566 if (!pairing) | |
567 return; | |
568 | |
569 pairing->DisplayPinCode(pincode); | |
570 } | |
571 | |
572 void BluetoothAdapterChromeOS::RequestPasskey( | |
573 const dbus::ObjectPath& device_path, | |
574 const PasskeyCallback& callback) { | |
575 DCHECK(IsPresent()); | |
576 DCHECK(agent_.get()); | |
577 VLOG(1) << device_path.value() << ": RequestPasskey"; | |
578 | |
579 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
580 if (!pairing) { | |
581 callback.Run(REJECTED, 0); | |
582 return; | |
583 } | |
584 | |
585 pairing->RequestPasskey(callback); | |
586 } | |
587 | |
588 void BluetoothAdapterChromeOS::DisplayPasskey( | |
589 const dbus::ObjectPath& device_path, | |
590 uint32 passkey, | |
591 uint16 entered) { | |
592 DCHECK(IsPresent()); | |
593 DCHECK(agent_.get()); | |
594 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey | |
595 << " (" << entered << " entered)"; | |
596 | |
597 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
598 if (!pairing) | |
599 return; | |
600 | |
601 if (entered == 0) | |
602 pairing->DisplayPasskey(passkey); | |
603 | |
604 pairing->KeysEntered(entered); | |
605 } | |
606 | |
607 void BluetoothAdapterChromeOS::RequestConfirmation( | |
608 const dbus::ObjectPath& device_path, | |
609 uint32 passkey, | |
610 const ConfirmationCallback& callback) { | |
611 DCHECK(IsPresent()); | |
612 DCHECK(agent_.get()); | |
613 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; | |
614 | |
615 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
616 if (!pairing) { | |
617 callback.Run(REJECTED); | |
618 return; | |
619 } | |
620 | |
621 pairing->RequestConfirmation(passkey, callback); | |
622 } | |
623 | |
624 void BluetoothAdapterChromeOS::RequestAuthorization( | |
625 const dbus::ObjectPath& device_path, | |
626 const ConfirmationCallback& callback) { | |
627 DCHECK(IsPresent()); | |
628 DCHECK(agent_.get()); | |
629 VLOG(1) << device_path.value() << ": RequestAuthorization"; | |
630 | |
631 BluetoothPairingChromeOS* pairing = GetPairing(device_path); | |
632 if (!pairing) { | |
633 callback.Run(REJECTED); | |
634 return; | |
635 } | |
636 | |
637 pairing->RequestAuthorization(callback); | |
638 } | |
639 | |
640 void BluetoothAdapterChromeOS::AuthorizeService( | |
641 const dbus::ObjectPath& device_path, | |
642 const std::string& uuid, | |
643 const ConfirmationCallback& callback) { | |
644 DCHECK(IsPresent()); | |
645 DCHECK(agent_.get()); | |
646 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; | |
647 | |
648 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); | |
649 if (!device_chromeos) { | |
650 callback.Run(CANCELLED); | |
651 return; | |
652 } | |
653 | |
654 // We always set paired devices to Trusted, so the only reason that this | |
655 // method call would ever be called is in the case of a race condition where | |
656 // our "Set('Trusted', true)" method call is still pending in the Bluetooth | |
657 // daemon because it's busy handling the incoming connection. | |
658 if (device_chromeos->IsPaired()) { | |
659 callback.Run(SUCCESS); | |
660 return; | |
661 } | |
662 | |
663 // TODO(keybuk): reject service authorizations when not paired, determine | |
664 // whether this is acceptable long-term. | |
665 LOG(WARNING) << "Rejecting service connection from unpaired device " | |
666 << device_chromeos->GetAddress() << " for UUID " << uuid; | |
667 callback.Run(REJECTED); | |
668 } | |
669 | |
670 void BluetoothAdapterChromeOS::Cancel() { | |
671 DCHECK(IsPresent()); | |
672 DCHECK(agent_.get()); | |
673 VLOG(1) << "Cancel"; | |
674 } | |
675 | |
676 void BluetoothAdapterChromeOS::OnRegisterAgent() { | |
677 VLOG(1) << "Pairing agent registered, requesting to be made default"; | |
678 | |
679 bluez::BluezDBusManager::Get() | |
680 ->GetBluetoothAgentManagerClient() | |
681 ->RequestDefaultAgent( | |
682 dbus::ObjectPath(kAgentPath), | |
683 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, | |
684 weak_ptr_factory_.GetWeakPtr()), | |
685 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, | |
686 weak_ptr_factory_.GetWeakPtr())); | |
687 } | |
688 | |
689 void BluetoothAdapterChromeOS::OnRegisterAgentError( | |
690 const std::string& error_name, | |
691 const std::string& error_message) { | |
692 // Our agent being already registered isn't an error. | |
693 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) | |
694 return; | |
695 | |
696 LOG(WARNING) << ": Failed to register pairing agent: " | |
697 << error_name << ": " << error_message; | |
698 } | |
699 | |
700 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { | |
701 VLOG(1) << "Pairing agent now default"; | |
702 } | |
703 | |
704 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( | |
705 const std::string& error_name, | |
706 const std::string& error_message) { | |
707 LOG(WARNING) << ": Failed to make pairing agent default: " | |
708 << error_name << ": " << error_message; | |
709 } | |
710 | |
711 void BluetoothAdapterChromeOS::OnRegisterAudioSink( | |
712 const device::BluetoothAdapter::AcquiredCallback& callback, | |
713 const device::BluetoothAudioSink::ErrorCallback& error_callback, | |
714 scoped_refptr<BluetoothAudioSink> audio_sink) { | |
715 if (!IsPresent()) { | |
716 VLOG(1) << "Failed to register audio sink, adapter not present"; | |
717 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); | |
718 return; | |
719 } | |
720 DCHECK(audio_sink.get()); | |
721 callback.Run(audio_sink); | |
722 } | |
723 | |
724 BluetoothDeviceChromeOS* | |
725 BluetoothAdapterChromeOS::GetDeviceWithPath( | |
726 const dbus::ObjectPath& object_path) { | |
727 if (!IsPresent()) | |
728 return nullptr; | |
729 | |
730 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); | |
731 ++iter) { | |
732 BluetoothDeviceChromeOS* device_chromeos = | |
733 static_cast<BluetoothDeviceChromeOS*>(iter->second); | |
734 if (device_chromeos->object_path() == object_path) | |
735 return device_chromeos; | |
736 } | |
737 | |
738 return nullptr; | |
739 } | |
740 | |
741 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( | |
742 const dbus::ObjectPath& object_path) { | |
743 DCHECK(IsPresent()); | |
744 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); | |
745 if (!device_chromeos) { | |
746 LOG(WARNING) << "Pairing Agent request for unknown device: " | |
747 << object_path.value(); | |
748 return nullptr; | |
749 } | |
750 | |
751 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); | |
752 if (pairing) | |
753 return pairing; | |
754 | |
755 // The device doesn't have its own pairing context, so this is an incoming | |
756 // pairing request that should use our best default delegate (if we have one). | |
757 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); | |
758 if (!pairing_delegate) | |
759 return nullptr; | |
760 | |
761 return device_chromeos->BeginPairing(pairing_delegate); | |
762 } | |
763 | |
764 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { | |
765 DCHECK(!IsPresent()); | |
766 DCHECK(!dbus_is_shutdown_); | |
767 object_path_ = object_path; | |
768 | |
769 VLOG(1) << object_path_.value() << ": using adapter."; | |
770 | |
771 VLOG(1) << "Registering pairing agent"; | |
772 bluez::BluezDBusManager::Get() | |
773 ->GetBluetoothAgentManagerClient() | |
774 ->RegisterAgent( | |
775 dbus::ObjectPath(kAgentPath), | |
776 bluetooth_agent_manager::kKeyboardDisplayCapability, | |
777 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, | |
778 weak_ptr_factory_.GetWeakPtr()), | |
779 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgentError, | |
780 weak_ptr_factory_.GetWeakPtr())); | |
781 | |
782 SetDefaultAdapterName(); | |
783 | |
784 bluez::BluetoothAdapterClient::Properties* properties = | |
785 bluez::BluezDBusManager::Get() | |
786 ->GetBluetoothAdapterClient() | |
787 ->GetProperties(object_path_); | |
788 | |
789 PresentChanged(true); | |
790 | |
791 if (properties->powered.value()) | |
792 PoweredChanged(true); | |
793 if (properties->discoverable.value()) | |
794 DiscoverableChanged(true); | |
795 if (properties->discovering.value()) | |
796 DiscoveringChanged(true); | |
797 | |
798 std::vector<dbus::ObjectPath> device_paths = | |
799 bluez::BluezDBusManager::Get() | |
800 ->GetBluetoothDeviceClient() | |
801 ->GetDevicesForAdapter(object_path_); | |
802 | |
803 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); | |
804 iter != device_paths.end(); ++iter) { | |
805 DeviceAdded(*iter); | |
806 } | |
807 } | |
808 | |
809 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { | |
810 DCHECK(IsPresent()); | |
811 | |
812 std::string alias; | |
813 switch (chromeos::GetDeviceType()) { | |
814 case DeviceType::kChromebase: | |
815 alias = "Chromebase"; | |
816 break; | |
817 case DeviceType::kChromebit: | |
818 alias = "Chromebit"; | |
819 break; | |
820 case DeviceType::kChromebook: | |
821 alias = "Chromebook"; | |
822 break; | |
823 case DeviceType::kChromebox: | |
824 alias = "Chromebox"; | |
825 break; | |
826 case DeviceType::kUnknown: | |
827 alias = "Chromebook"; | |
828 break; | |
829 } | |
830 | |
831 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); | |
832 } | |
833 | |
834 void BluetoothAdapterChromeOS::RemoveAdapter() { | |
835 DCHECK(IsPresent()); | |
836 VLOG(1) << object_path_.value() << ": adapter removed."; | |
837 | |
838 bluez::BluetoothAdapterClient::Properties* properties = | |
839 bluez::BluezDBusManager::Get() | |
840 ->GetBluetoothAdapterClient() | |
841 ->GetProperties(object_path_); | |
842 | |
843 object_path_ = dbus::ObjectPath(""); | |
844 | |
845 if (properties->powered.value()) | |
846 PoweredChanged(false); | |
847 if (properties->discoverable.value()) | |
848 DiscoverableChanged(false); | |
849 if (properties->discovering.value()) | |
850 DiscoveringChanged(false); | |
851 | |
852 // Copy the devices list here and clear the original so that when we | |
853 // send DeviceRemoved(), GetDevices() returns no devices. | |
854 DevicesMap devices = devices_; | |
855 devices_.clear(); | |
856 | |
857 for (DevicesMap::iterator iter = devices.begin(); | |
858 iter != devices.end(); ++iter) { | |
859 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
860 DeviceRemoved(this, iter->second)); | |
861 delete iter->second; | |
862 } | |
863 | |
864 PresentChanged(false); | |
865 } | |
866 | |
867 void BluetoothAdapterChromeOS::PoweredChanged(bool powered) { | |
868 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
869 AdapterPoweredChanged(this, powered)); | |
870 } | |
871 | |
872 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { | |
873 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
874 AdapterDiscoverableChanged(this, discoverable)); | |
875 } | |
876 | |
877 void BluetoothAdapterChromeOS::DiscoveringChanged( | |
878 bool discovering) { | |
879 // If the adapter stopped discovery due to a reason other than a request by | |
880 // us, reset the count to 0. | |
881 VLOG(1) << "Discovering changed: " << discovering; | |
882 if (!discovering && !discovery_request_pending_ && | |
883 num_discovery_sessions_ > 0) { | |
884 VLOG(1) << "Marking sessions as inactive."; | |
885 num_discovery_sessions_ = 0; | |
886 MarkDiscoverySessionsAsInactive(); | |
887 } | |
888 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
889 AdapterDiscoveringChanged(this, discovering)); | |
890 } | |
891 | |
892 void BluetoothAdapterChromeOS::PresentChanged(bool present) { | |
893 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
894 AdapterPresentChanged(this, present)); | |
895 } | |
896 | |
897 void BluetoothAdapterChromeOS::NotifyDeviceChanged( | |
898 BluetoothDeviceChromeOS* device) { | |
899 DCHECK(device); | |
900 DCHECK(device->adapter_ == this); | |
901 | |
902 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
903 DeviceChanged(this, device)); | |
904 } | |
905 | |
906 void BluetoothAdapterChromeOS::NotifyDeviceAddressChanged( | |
907 BluetoothDeviceChromeOS* device, | |
908 const std::string& old_address) { | |
909 DCHECK(device->adapter_ == this); | |
910 | |
911 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | |
912 DeviceAddressChanged(this, device, old_address)); | |
913 } | |
914 | |
915 void BluetoothAdapterChromeOS::NotifyGattServiceAdded( | |
916 BluetoothRemoteGattServiceChromeOS* service) { | |
917 DCHECK_EQ(service->GetAdapter(), this); | |
918 DCHECK_EQ( | |
919 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, | |
920 this); | |
921 | |
922 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
923 observers_, | |
924 GattServiceAdded(this, service->GetDevice(), service)); | |
925 } | |
926 | |
927 void BluetoothAdapterChromeOS::NotifyGattServiceRemoved( | |
928 BluetoothRemoteGattServiceChromeOS* service) { | |
929 DCHECK_EQ(service->GetAdapter(), this); | |
930 DCHECK_EQ( | |
931 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, | |
932 this); | |
933 | |
934 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
935 observers_, | |
936 GattServiceRemoved(this, service->GetDevice(), service)); | |
937 } | |
938 | |
939 void BluetoothAdapterChromeOS::NotifyGattServiceChanged( | |
940 BluetoothRemoteGattServiceChromeOS* service) { | |
941 DCHECK_EQ(service->GetAdapter(), this); | |
942 | |
943 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
944 observers_, | |
945 GattServiceChanged(this, service)); | |
946 } | |
947 | |
948 void BluetoothAdapterChromeOS::NotifyGattDiscoveryComplete( | |
949 BluetoothRemoteGattServiceChromeOS* service) { | |
950 DCHECK_EQ(service->GetAdapter(), this); | |
951 | |
952 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
953 observers_, | |
954 GattDiscoveryCompleteForService(this, service)); | |
955 } | |
956 | |
957 void BluetoothAdapterChromeOS::NotifyGattCharacteristicAdded( | |
958 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { | |
959 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
960 characteristic->GetService())->GetAdapter(), | |
961 this); | |
962 | |
963 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
964 observers_, | |
965 GattCharacteristicAdded(this, characteristic)); | |
966 } | |
967 | |
968 void BluetoothAdapterChromeOS::NotifyGattCharacteristicRemoved( | |
969 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { | |
970 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
971 characteristic->GetService())->GetAdapter(), | |
972 this); | |
973 | |
974 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
975 observers_, | |
976 GattCharacteristicRemoved(this, characteristic)); | |
977 } | |
978 | |
979 void BluetoothAdapterChromeOS::NotifyGattDescriptorAdded( | |
980 BluetoothRemoteGattDescriptorChromeOS* descriptor) { | |
981 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
982 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
983 this); | |
984 | |
985 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
986 observers_, | |
987 GattDescriptorAdded(this, descriptor)); | |
988 } | |
989 | |
990 void BluetoothAdapterChromeOS::NotifyGattDescriptorRemoved( | |
991 BluetoothRemoteGattDescriptorChromeOS* descriptor) { | |
992 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
993 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
994 this); | |
995 | |
996 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
997 observers_, | |
998 GattDescriptorRemoved(this, descriptor)); | |
999 } | |
1000 | |
1001 void BluetoothAdapterChromeOS::NotifyGattCharacteristicValueChanged( | |
1002 BluetoothRemoteGattCharacteristicChromeOS* characteristic, | |
1003 const std::vector<uint8>& value) { | |
1004 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
1005 characteristic->GetService())->GetAdapter(), | |
1006 this); | |
1007 | |
1008 FOR_EACH_OBSERVER( | |
1009 BluetoothAdapter::Observer, | |
1010 observers_, | |
1011 GattCharacteristicValueChanged(this, characteristic, value)); | |
1012 } | |
1013 | |
1014 void BluetoothAdapterChromeOS::NotifyGattDescriptorValueChanged( | |
1015 BluetoothRemoteGattDescriptorChromeOS* descriptor, | |
1016 const std::vector<uint8>& value) { | |
1017 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( | |
1018 descriptor->GetCharacteristic()->GetService())->GetAdapter(), | |
1019 this); | |
1020 | |
1021 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, | |
1022 observers_, | |
1023 GattDescriptorValueChanged(this, descriptor, value)); | |
1024 } | |
1025 | |
1026 void BluetoothAdapterChromeOS::UseProfile( | |
1027 const BluetoothUUID& uuid, | |
1028 const dbus::ObjectPath& device_path, | |
1029 const bluez::BluetoothProfileManagerClient::Options& options, | |
1030 bluez::BluetoothProfileServiceProvider::Delegate* delegate, | |
1031 const ProfileRegisteredCallback& success_callback, | |
1032 const ErrorCompletionCallback& error_callback) { | |
1033 DCHECK(delegate); | |
1034 | |
1035 if (!IsPresent()) { | |
1036 VLOG(2) << "Adapter not present, erroring out"; | |
1037 error_callback.Run("Adapter not present"); | |
1038 return; | |
1039 } | |
1040 | |
1041 if (profiles_.find(uuid) != profiles_.end()) { | |
1042 // TODO(jamuraa) check that the options are the same and error when they are | |
1043 // not. | |
1044 SetProfileDelegate(uuid, device_path, delegate, success_callback, | |
1045 error_callback); | |
1046 return; | |
1047 } | |
1048 | |
1049 if (profile_queues_.find(uuid) == profile_queues_.end()) { | |
1050 BluetoothAdapterProfileChromeOS::Register( | |
1051 uuid, options, | |
1052 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid), | |
1053 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, | |
1054 uuid)); | |
1055 | |
1056 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>(); | |
1057 } | |
1058 | |
1059 profile_queues_[uuid]->push_back(std::make_pair( | |
1060 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate, this, uuid, | |
1061 device_path, delegate, success_callback, error_callback), | |
1062 error_callback)); | |
1063 } | |
1064 | |
1065 void BluetoothAdapterChromeOS::ReleaseProfile( | |
1066 const dbus::ObjectPath& device_path, | |
1067 BluetoothAdapterProfileChromeOS* profile) { | |
1068 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value() | |
1069 << " from " << device_path.value(); | |
1070 profile->RemoveDelegate( | |
1071 device_path, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile, | |
1072 weak_ptr_factory_.GetWeakPtr(), profile->uuid())); | |
1073 } | |
1074 | |
1075 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID& uuid) { | |
1076 VLOG(2) << "Remove Profile: " << uuid.canonical_value(); | |
1077 | |
1078 if (profiles_.find(uuid) != profiles_.end()) { | |
1079 delete profiles_[uuid]; | |
1080 profiles_.erase(uuid); | |
1081 } | |
1082 } | |
1083 | |
1084 void BluetoothAdapterChromeOS::OnRegisterProfile( | |
1085 const BluetoothUUID& uuid, | |
1086 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) { | |
1087 profiles_[uuid] = profile.release(); | |
1088 | |
1089 if (profile_queues_.find(uuid) == profile_queues_.end()) | |
1090 return; | |
1091 | |
1092 for (auto& it : *profile_queues_[uuid]) | |
1093 it.first.Run(); | |
1094 delete profile_queues_[uuid]; | |
1095 profile_queues_.erase(uuid); | |
1096 } | |
1097 | |
1098 void BluetoothAdapterChromeOS::SetProfileDelegate( | |
1099 const BluetoothUUID& uuid, | |
1100 const dbus::ObjectPath& device_path, | |
1101 bluez::BluetoothProfileServiceProvider::Delegate* delegate, | |
1102 const ProfileRegisteredCallback& success_callback, | |
1103 const ErrorCompletionCallback& error_callback) { | |
1104 if (profiles_.find(uuid) == profiles_.end()) { | |
1105 error_callback.Run("Cannot find profile!"); | |
1106 return; | |
1107 } | |
1108 | |
1109 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { | |
1110 success_callback.Run(profiles_[uuid]); | |
1111 return; | |
1112 } | |
1113 // Already set | |
1114 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); | |
1115 } | |
1116 | |
1117 void BluetoothAdapterChromeOS::OnRegisterProfileError( | |
1118 const BluetoothUUID& uuid, | |
1119 const std::string& error_name, | |
1120 const std::string& error_message) { | |
1121 VLOG(2) << object_path_.value() << ": Failed to register profile: " | |
1122 << error_name << ": " << error_message; | |
1123 if (profile_queues_.find(uuid) == profile_queues_.end()) | |
1124 return; | |
1125 | |
1126 for (auto& it : *profile_queues_[uuid]) | |
1127 it.second.Run(error_message); | |
1128 | |
1129 delete profile_queues_[uuid]; | |
1130 profile_queues_.erase(uuid); | |
1131 } | |
1132 | |
1133 void BluetoothAdapterChromeOS::OnSetDiscoverable( | |
1134 const base::Closure& callback, | |
1135 const ErrorCallback& error_callback, | |
1136 bool success) { | |
1137 if (!IsPresent()) { | |
1138 error_callback.Run(); | |
1139 return; | |
1140 } | |
1141 | |
1142 // Set the discoverable_timeout property to zero so the adapter remains | |
1143 // discoverable forever. | |
1144 bluez::BluezDBusManager::Get() | |
1145 ->GetBluetoothAdapterClient() | |
1146 ->GetProperties(object_path_) | |
1147 ->discoverable_timeout.Set( | |
1148 0, | |
1149 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, | |
1150 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1151 } | |
1152 | |
1153 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( | |
1154 const base::Closure& callback, | |
1155 const ErrorCallback& error_callback, | |
1156 bool success) { | |
1157 if (IsPresent() && success) { | |
1158 callback.Run(); | |
1159 } else { | |
1160 error_callback.Run(); | |
1161 } | |
1162 } | |
1163 | |
1164 void BluetoothAdapterChromeOS::AddDiscoverySession( | |
1165 BluetoothDiscoveryFilter* discovery_filter, | |
1166 const base::Closure& callback, | |
1167 const DiscoverySessionErrorCallback& error_callback) { | |
1168 if (!IsPresent()) { | |
1169 error_callback.Run( | |
1170 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); | |
1171 return; | |
1172 } | |
1173 VLOG(1) << __func__; | |
1174 if (discovery_request_pending_) { | |
1175 // The pending request is either to stop a previous session or to start a | |
1176 // new one. Either way, queue this one. | |
1177 DCHECK(num_discovery_sessions_ == 1 || num_discovery_sessions_ == 0); | |
1178 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | |
1179 << "request to start a new discovery session."; | |
1180 discovery_request_queue_.push( | |
1181 std::make_tuple(discovery_filter, callback, error_callback)); | |
1182 return; | |
1183 } | |
1184 | |
1185 // The adapter is already discovering. | |
1186 if (num_discovery_sessions_ > 0) { | |
1187 DCHECK(IsDiscovering()); | |
1188 DCHECK(!discovery_request_pending_); | |
1189 num_discovery_sessions_++; | |
1190 SetDiscoveryFilter(BluetoothDiscoveryFilter::Merge( | |
1191 GetMergedDiscoveryFilter().get(), discovery_filter), | |
1192 callback, error_callback); | |
1193 return; | |
1194 } | |
1195 | |
1196 // There are no active discovery sessions. | |
1197 DCHECK_EQ(num_discovery_sessions_, 0); | |
1198 | |
1199 if (discovery_filter) { | |
1200 discovery_request_pending_ = true; | |
1201 | |
1202 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter( | |
1203 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); | |
1204 df->CopyFrom(*discovery_filter); | |
1205 SetDiscoveryFilter( | |
1206 df.Pass(), | |
1207 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter, | |
1208 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1209 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError, | |
1210 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1211 return; | |
1212 } else { | |
1213 current_filter_.reset(); | |
1214 } | |
1215 | |
1216 // This is the first request to start device discovery. | |
1217 discovery_request_pending_ = true; | |
1218 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( | |
1219 object_path_, | |
1220 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | |
1221 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1222 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | |
1223 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1224 } | |
1225 | |
1226 void BluetoothAdapterChromeOS::RemoveDiscoverySession( | |
1227 BluetoothDiscoveryFilter* discovery_filter, | |
1228 const base::Closure& callback, | |
1229 const DiscoverySessionErrorCallback& error_callback) { | |
1230 if (!IsPresent()) { | |
1231 error_callback.Run( | |
1232 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); | |
1233 return; | |
1234 } | |
1235 | |
1236 VLOG(1) << __func__; | |
1237 // There are active sessions other than the one currently being removed. | |
1238 if (num_discovery_sessions_ > 1) { | |
1239 DCHECK(IsDiscovering()); | |
1240 DCHECK(!discovery_request_pending_); | |
1241 num_discovery_sessions_--; | |
1242 | |
1243 SetDiscoveryFilter(GetMergedDiscoveryFilterMasked(discovery_filter), | |
1244 callback, error_callback); | |
1245 return; | |
1246 } | |
1247 | |
1248 // If there is a pending request to BlueZ, then queue this request. | |
1249 if (discovery_request_pending_) { | |
1250 VLOG(1) << "Pending request to start/stop device discovery. Queueing " | |
1251 << "request to stop discovery session."; | |
1252 error_callback.Run( | |
1253 UMABluetoothDiscoverySessionOutcome::REMOVE_WITH_PENDING_REQUEST); | |
1254 return; | |
1255 } | |
1256 | |
1257 // There are no active sessions. Return error. | |
1258 if (num_discovery_sessions_ == 0) { | |
1259 // TODO(armansito): This should never happen once we have the | |
1260 // DiscoverySession API. Replace this case with an assert once it's | |
1261 // the deprecated methods have been removed. (See crbug.com/3445008). | |
1262 VLOG(1) << "No active discovery sessions. Returning error."; | |
1263 error_callback.Run( | |
1264 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER); | |
1265 return; | |
1266 } | |
1267 | |
1268 // There is exactly one active discovery session. Request BlueZ to stop | |
1269 // discovery. | |
1270 DCHECK_EQ(num_discovery_sessions_, 1); | |
1271 discovery_request_pending_ = true; | |
1272 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery( | |
1273 object_path_, base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, | |
1274 weak_ptr_factory_.GetWeakPtr(), callback), | |
1275 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, | |
1276 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
1277 } | |
1278 | |
1279 void BluetoothAdapterChromeOS::SetDiscoveryFilter( | |
1280 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, | |
1281 const base::Closure& callback, | |
1282 const DiscoverySessionErrorCallback& error_callback) { | |
1283 if (!IsPresent()) { | |
1284 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1285 return; | |
1286 } | |
1287 | |
1288 // If old and new filter are equal (null) then don't make request, just call | |
1289 // succes callback | |
1290 if (!current_filter_ && !discovery_filter.get()) { | |
1291 callback.Run(); | |
1292 return; | |
1293 } | |
1294 | |
1295 // If old and new filter are not null and equal then don't make request, just | |
1296 // call succes callback | |
1297 if (current_filter_ && discovery_filter && | |
1298 current_filter_->Equals(*discovery_filter)) { | |
1299 callback.Run(); | |
1300 return; | |
1301 } | |
1302 | |
1303 current_filter_.reset(discovery_filter.release()); | |
1304 | |
1305 bluez::BluetoothAdapterClient::DiscoveryFilter dbus_discovery_filter; | |
1306 | |
1307 if (current_filter_.get()) { | |
1308 uint16_t pathloss; | |
1309 int16_t rssi; | |
1310 uint8_t transport; | |
1311 std::set<device::BluetoothUUID> uuids; | |
1312 | |
1313 if (current_filter_->GetPathloss(&pathloss)) | |
1314 dbus_discovery_filter.pathloss.reset(new uint16_t(pathloss)); | |
1315 | |
1316 if (current_filter_->GetRSSI(&rssi)) | |
1317 dbus_discovery_filter.rssi.reset(new int16_t(rssi)); | |
1318 | |
1319 transport = current_filter_->GetTransport(); | |
1320 if (transport == BluetoothDiscoveryFilter::Transport::TRANSPORT_LE) { | |
1321 dbus_discovery_filter.transport.reset(new std::string("le")); | |
1322 } else if (transport == | |
1323 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC) { | |
1324 dbus_discovery_filter.transport.reset(new std::string("bredr")); | |
1325 } else if (transport == | |
1326 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL) { | |
1327 dbus_discovery_filter.transport.reset(new std::string("auto")); | |
1328 } | |
1329 | |
1330 current_filter_->GetUUIDs(uuids); | |
1331 if (uuids.size()) { | |
1332 dbus_discovery_filter.uuids = | |
1333 scoped_ptr<std::vector<std::string>>(new std::vector<std::string>); | |
1334 | |
1335 for (const auto& it : uuids) | |
1336 dbus_discovery_filter.uuids.get()->push_back(it.value()); | |
1337 } | |
1338 } | |
1339 | |
1340 bluez::BluezDBusManager::Get() | |
1341 ->GetBluetoothAdapterClient() | |
1342 ->SetDiscoveryFilter( | |
1343 object_path_, dbus_discovery_filter, | |
1344 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilter, | |
1345 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1346 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilterError, | |
1347 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1348 } | |
1349 | |
1350 void BluetoothAdapterChromeOS::OnStartDiscovery( | |
1351 const base::Closure& callback, | |
1352 const DiscoverySessionErrorCallback& error_callback) { | |
1353 // Report success on the original request and increment the count. | |
1354 VLOG(1) << __func__; | |
1355 DCHECK(discovery_request_pending_); | |
1356 DCHECK_EQ(num_discovery_sessions_, 0); | |
1357 discovery_request_pending_ = false; | |
1358 num_discovery_sessions_++; | |
1359 if (IsPresent()) { | |
1360 callback.Run(); | |
1361 } else { | |
1362 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1363 } | |
1364 | |
1365 // Try to add a new discovery session for each queued request. | |
1366 ProcessQueuedDiscoveryRequests(); | |
1367 } | |
1368 | |
1369 void BluetoothAdapterChromeOS::OnStartDiscoveryError( | |
1370 const base::Closure& callback, | |
1371 const DiscoverySessionErrorCallback& error_callback, | |
1372 const std::string& error_name, | |
1373 const std::string& error_message) { | |
1374 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " | |
1375 << error_name << ": " << error_message; | |
1376 | |
1377 // Failed to start discovery. This can only happen if the count is at 0. | |
1378 DCHECK_EQ(num_discovery_sessions_, 0); | |
1379 DCHECK(discovery_request_pending_); | |
1380 discovery_request_pending_ = false; | |
1381 | |
1382 // Discovery request may fail if discovery was previously initiated by Chrome, | |
1383 // but the session were invalidated due to the discovery state unexpectedly | |
1384 // changing to false and then back to true. In this case, report success. | |
1385 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress && | |
1386 IsDiscovering()) { | |
1387 VLOG(1) << "Discovery previously initiated. Reporting success."; | |
1388 num_discovery_sessions_++; | |
1389 callback.Run(); | |
1390 } else { | |
1391 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); | |
1392 } | |
1393 | |
1394 // Try to add a new discovery session for each queued request. | |
1395 ProcessQueuedDiscoveryRequests(); | |
1396 } | |
1397 | |
1398 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { | |
1399 // Report success on the original request and decrement the count. | |
1400 VLOG(1) << __func__; | |
1401 DCHECK(discovery_request_pending_); | |
1402 DCHECK_EQ(num_discovery_sessions_, 1); | |
1403 discovery_request_pending_ = false; | |
1404 num_discovery_sessions_--; | |
1405 callback.Run(); | |
1406 | |
1407 current_filter_.reset(); | |
1408 | |
1409 // Try to add a new discovery session for each queued request. | |
1410 ProcessQueuedDiscoveryRequests(); | |
1411 } | |
1412 | |
1413 void BluetoothAdapterChromeOS::OnStopDiscoveryError( | |
1414 const DiscoverySessionErrorCallback& error_callback, | |
1415 const std::string& error_name, | |
1416 const std::string& error_message) { | |
1417 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " | |
1418 << error_name << ": " << error_message; | |
1419 | |
1420 // Failed to stop discovery. This can only happen if the count is at 1. | |
1421 DCHECK(discovery_request_pending_); | |
1422 DCHECK_EQ(num_discovery_sessions_, 1); | |
1423 discovery_request_pending_ = false; | |
1424 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); | |
1425 | |
1426 // Try to add a new discovery session for each queued request. | |
1427 ProcessQueuedDiscoveryRequests(); | |
1428 } | |
1429 | |
1430 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter( | |
1431 const base::Closure& callback, | |
1432 const DiscoverySessionErrorCallback& error_callback) { | |
1433 // This is the first request to start device discovery. | |
1434 DCHECK(discovery_request_pending_); | |
1435 DCHECK_EQ(num_discovery_sessions_, 0); | |
1436 | |
1437 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( | |
1438 object_path_, | |
1439 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, | |
1440 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), | |
1441 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, | |
1442 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | |
1443 } | |
1444 | |
1445 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError( | |
1446 const base::Closure& callback, | |
1447 const DiscoverySessionErrorCallback& error_callback, | |
1448 UMABluetoothDiscoverySessionOutcome outcome) { | |
1449 LOG(WARNING) << object_path_.value() | |
1450 << ": Failed to pre set discovery filter."; | |
1451 | |
1452 // Failed to start discovery. This can only happen if the count is at 0. | |
1453 DCHECK_EQ(num_discovery_sessions_, 0); | |
1454 DCHECK(discovery_request_pending_); | |
1455 discovery_request_pending_ = false; | |
1456 | |
1457 error_callback.Run(outcome); | |
1458 | |
1459 // Try to add a new discovery session for each queued request. | |
1460 ProcessQueuedDiscoveryRequests(); | |
1461 } | |
1462 | |
1463 void BluetoothAdapterChromeOS::OnSetDiscoveryFilter( | |
1464 const base::Closure& callback, | |
1465 const DiscoverySessionErrorCallback& error_callback) { | |
1466 // Report success on the original request and increment the count. | |
1467 VLOG(1) << __func__; | |
1468 if (IsPresent()) { | |
1469 callback.Run(); | |
1470 } else { | |
1471 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); | |
1472 } | |
1473 } | |
1474 | |
1475 void BluetoothAdapterChromeOS::OnSetDiscoveryFilterError( | |
1476 const base::Closure& callback, | |
1477 const DiscoverySessionErrorCallback& error_callback, | |
1478 const std::string& error_name, | |
1479 const std::string& error_message) { | |
1480 LOG(WARNING) << object_path_.value() | |
1481 << ": Failed to set discovery filter: " << error_name << ": " | |
1482 << error_message; | |
1483 | |
1484 UMABluetoothDiscoverySessionOutcome outcome = | |
1485 TranslateDiscoveryErrorToUMA(error_name); | |
1486 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) { | |
1487 // bluez/doc/adapter-api.txt says "Failed" is returned from | |
1488 // SetDiscoveryFilter when the controller doesn't support the requested | |
1489 // transport. | |
1490 outcome = UMABluetoothDiscoverySessionOutcome:: | |
1491 CHROMEOS_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT; | |
1492 } | |
1493 error_callback.Run(outcome); | |
1494 | |
1495 // Try to add a new discovery session for each queued request. | |
1496 ProcessQueuedDiscoveryRequests(); | |
1497 } | |
1498 | |
1499 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { | |
1500 while (!discovery_request_queue_.empty()) { | |
1501 VLOG(1) << "Process queued discovery request."; | |
1502 DiscoveryParamTuple params = discovery_request_queue_.front(); | |
1503 discovery_request_queue_.pop(); | |
1504 AddDiscoverySession(std::get<0>(params), std::get<1>(params), | |
1505 std::get<2>(params)); | |
1506 | |
1507 // If the queued request resulted in a pending call, then let it | |
1508 // asynchonously process the remaining queued requests once the pending | |
1509 // call returns. | |
1510 if (discovery_request_pending_) | |
1511 return; | |
1512 } | |
1513 } | |
1514 | |
1515 } // namespace chromeos | |
OLD | NEW |