OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <iomanip> | 10 #include <iomanip> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
65 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 65 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
66 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 66 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
67 VLOG(1) << "registering bluetooth adapter"; | 67 VLOG(1) << "registering bluetooth adapter"; |
68 BluetoothAdapterFactory::GetAdapter(base::Bind( | 68 BluetoothAdapterFactory::GetAdapter(base::Bind( |
69 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 69 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
70 } else { | 70 } else { |
71 VLOG(1) << "no bluetooth adapter available"; | 71 VLOG(1) << "no bluetooth adapter available"; |
72 } | 72 } |
73 | 73 |
74 arc_bridge_service()->AddObserver(this); | 74 arc_bridge_service()->bluetooth()->AddObserver(this); |
75 } | 75 } |
76 | 76 |
77 ArcBluetoothBridge::~ArcBluetoothBridge() { | 77 ArcBluetoothBridge::~ArcBluetoothBridge() { |
78 arc_bridge_service()->RemoveObserver(this); | 78 arc_bridge_service()->bluetooth()->RemoveObserver(this); |
79 | 79 |
80 if (bluetooth_adapter_) | 80 if (bluetooth_adapter_) |
81 bluetooth_adapter_->RemoveObserver(this); | 81 bluetooth_adapter_->RemoveObserver(this); |
82 } | 82 } |
83 | 83 |
84 void ArcBluetoothBridge::OnAdapterInitialized( | 84 void ArcBluetoothBridge::OnAdapterInitialized( |
85 scoped_refptr<BluetoothAdapter> adapter) { | 85 scoped_refptr<BluetoothAdapter> adapter) { |
86 // We can downcast here because we are always running on Chrome OS, and | 86 // We can downcast here because we are always running on Chrome OS, and |
87 // so our adapter uses BlueZ. | 87 // so our adapter uses BlueZ. |
88 bluetooth_adapter_ = | 88 bluetooth_adapter_ = |
89 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); | 89 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); |
90 bluetooth_adapter_->AddObserver(this); | 90 bluetooth_adapter_->AddObserver(this); |
91 } | 91 } |
92 | 92 |
93 void ArcBluetoothBridge::OnBluetoothInstanceReady() { | 93 void ArcBluetoothBridge::OnInstanceReady() { |
94 mojom::BluetoothInstance* bluetooth_instance = | 94 mojom::BluetoothInstance* bluetooth_instance = |
95 arc_bridge_service()->bluetooth_instance(); | 95 arc_bridge_service()->bluetooth()->instance(); |
96 if (!bluetooth_instance) { | 96 if (!bluetooth_instance) { |
97 LOG(ERROR) << "OnBluetoothInstanceReady called, " | 97 LOG(ERROR) << "OnBluetoothInstanceReady called, " |
98 << "but no bluetooth instance found"; | 98 << "but no bluetooth instance found"; |
99 return; | 99 return; |
100 } | 100 } |
101 | 101 bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind()); |
102 arc_bridge_service()->bluetooth_instance()->Init( | |
103 binding_.CreateInterfacePtrAndBind()); | |
104 } | 102 } |
105 | 103 |
106 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, | 104 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, |
107 bool present) { | 105 bool present) { |
108 // If the adapter goes away, remove ourselves as an observer. | 106 // If the adapter goes away, remove ourselves as an observer. |
109 if (!present && adapter == bluetooth_adapter_) { | 107 if (!present && adapter == bluetooth_adapter_) { |
110 adapter->RemoveObserver(this); | 108 adapter->RemoveObserver(this); |
111 bluetooth_adapter_ = nullptr; | 109 bluetooth_adapter_ = nullptr; |
112 } | 110 } |
113 } | 111 } |
(...skipping 11 matching lines...) Expand all Loading... |
125 BluetoothDevice* device) { | 123 BluetoothDevice* device) { |
126 if (!HasBluetoothInstance()) | 124 if (!HasBluetoothInstance()) |
127 return; | 125 return; |
128 | 126 |
129 if (device->IsConnected()) | 127 if (device->IsConnected()) |
130 return; | 128 return; |
131 | 129 |
132 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 130 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
133 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 131 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
134 | 132 |
135 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 133 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
136 std::move(properties)); | 134 std::move(properties)); |
137 | 135 |
138 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 136 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
139 return; | 137 return; |
140 | 138 |
141 mojom::BluetoothAddressPtr addr = | 139 mojom::BluetoothAddressPtr addr = |
142 mojom::BluetoothAddress::From(device->GetAddress()); | 140 mojom::BluetoothAddress::From(device->GetAddress()); |
143 int rssi = device->GetInquiryRSSI(); | 141 int rssi = device->GetInquiryRSSI(); |
144 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 142 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
145 GetAdvertisingData(device); | 143 GetAdvertisingData(device); |
146 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 144 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
147 std::move(addr), rssi, std::move(adv_data)); | 145 std::move(addr), rssi, std::move(adv_data)); |
148 } | 146 } |
149 | 147 |
150 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, | 148 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, |
151 BluetoothDevice* device) { | 149 BluetoothDevice* device) { |
152 // TODO(smbarber): device properties changed; inform the container. | 150 // TODO(smbarber): device properties changed; inform the container. |
153 } | 151 } |
154 | 152 |
155 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, | 153 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, |
156 BluetoothDevice* device, | 154 BluetoothDevice* device, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 BluetoothDevice* device) { | 204 BluetoothDevice* device) { |
207 if (!HasBluetoothInstance()) | 205 if (!HasBluetoothInstance()) |
208 return; | 206 return; |
209 | 207 |
210 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 208 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
211 return; | 209 return; |
212 | 210 |
213 mojom::BluetoothAddressPtr addr = | 211 mojom::BluetoothAddressPtr addr = |
214 mojom::BluetoothAddress::From(device->GetAddress()); | 212 mojom::BluetoothAddress::From(device->GetAddress()); |
215 | 213 |
216 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | 214 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( |
217 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 215 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
218 } | 216 } |
219 | 217 |
220 void ArcBluetoothBridge::GattDiscoveryCompleteForService( | 218 void ArcBluetoothBridge::GattDiscoveryCompleteForService( |
221 BluetoothAdapter* adapter, | 219 BluetoothAdapter* adapter, |
222 BluetoothRemoteGattService* service) { | 220 BluetoothRemoteGattService* service) { |
223 // Placeholder for GATT client functionality | 221 // Placeholder for GATT client functionality |
224 } | 222 } |
225 | 223 |
226 void ArcBluetoothBridge::GattServiceChanged( | 224 void ArcBluetoothBridge::GattServiceChanged( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 mojom::BluetoothGattServiceID::New(); | 269 mojom::BluetoothGattServiceID::New(); |
272 service_id->is_primary = service->IsPrimary(); | 270 service_id->is_primary = service->IsPrimary(); |
273 service_id->id = mojom::BluetoothGattID::New(); | 271 service_id->id = mojom::BluetoothGattID::New(); |
274 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); | 272 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); |
275 service_id->id->uuid = mojom::BluetoothUUID::From(service->GetUUID()); | 273 service_id->id->uuid = mojom::BluetoothUUID::From(service->GetUUID()); |
276 | 274 |
277 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); | 275 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); |
278 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); | 276 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); |
279 char_id->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID()); | 277 char_id->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID()); |
280 | 278 |
281 arc_bridge_service()->bluetooth_instance()->OnGattNotify( | 279 arc_bridge_service()->bluetooth()->instance()->OnGattNotify( |
282 std::move(address), std::move(service_id), std::move(char_id), | 280 std::move(address), std::move(service_id), std::move(char_id), |
283 true /* is_notify */, mojo::Array<uint8_t>::From(value)); | 281 true /* is_notify */, mojo::Array<uint8_t>::From(value)); |
284 } | 282 } |
285 | 283 |
286 void ArcBluetoothBridge::GattDescriptorValueChanged( | 284 void ArcBluetoothBridge::GattDescriptorValueChanged( |
287 BluetoothAdapter* adapter, | 285 BluetoothAdapter* adapter, |
288 BluetoothRemoteGattDescriptor* descriptor, | 286 BluetoothRemoteGattDescriptor* descriptor, |
289 const std::vector<uint8_t>& value) { | 287 const std::vector<uint8_t>& value) { |
290 // Placeholder for GATT client functionality | 288 // Placeholder for GATT client functionality |
291 } | 289 } |
(...skipping 23 matching lines...) Expand all Loading... |
315 } | 313 } |
316 | 314 |
317 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { | 315 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { |
318 DCHECK(bluetooth_adapter_); | 316 DCHECK(bluetooth_adapter_); |
319 if (!HasBluetoothInstance()) | 317 if (!HasBluetoothInstance()) |
320 return; | 318 return; |
321 | 319 |
322 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 320 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
323 GetAdapterProperties(type); | 321 GetAdapterProperties(type); |
324 | 322 |
325 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( | 323 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
326 mojom::BluetoothStatus::SUCCESS, std::move(properties)); | 324 mojom::BluetoothStatus::SUCCESS, std::move(properties)); |
327 } | 325 } |
328 | 326 |
329 void ArcBluetoothBridge::SetAdapterProperty( | 327 void ArcBluetoothBridge::SetAdapterProperty( |
330 mojom::BluetoothPropertyPtr property) { | 328 mojom::BluetoothPropertyPtr property) { |
331 DCHECK(bluetooth_adapter_); | 329 DCHECK(bluetooth_adapter_); |
332 if (!HasBluetoothInstance()) | 330 if (!HasBluetoothInstance()) |
333 return; | 331 return; |
334 | 332 |
335 // TODO(smbarber): Implement SetAdapterProperty | 333 // TODO(smbarber): Implement SetAdapterProperty |
336 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( | 334 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
337 mojom::BluetoothStatus::FAIL, | 335 mojom::BluetoothStatus::FAIL, |
338 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 336 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
339 } | 337 } |
340 | 338 |
341 void ArcBluetoothBridge::GetRemoteDeviceProperty( | 339 void ArcBluetoothBridge::GetRemoteDeviceProperty( |
342 mojom::BluetoothAddressPtr remote_addr, | 340 mojom::BluetoothAddressPtr remote_addr, |
343 mojom::BluetoothPropertyType type) { | 341 mojom::BluetoothPropertyType type) { |
344 DCHECK(bluetooth_adapter_); | 342 DCHECK(bluetooth_adapter_); |
345 if (!HasBluetoothInstance()) | 343 if (!HasBluetoothInstance()) |
346 return; | 344 return; |
347 | 345 |
348 std::string addr_str = remote_addr->To<std::string>(); | 346 std::string addr_str = remote_addr->To<std::string>(); |
349 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 347 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
350 | 348 |
351 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 349 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
352 GetDeviceProperties(type, device); | 350 GetDeviceProperties(type, device); |
353 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; | 351 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; |
354 | 352 |
355 if (!device) { | 353 if (!device) { |
356 VLOG(1) << __func__ << ": device " << addr_str << " not available"; | 354 VLOG(1) << __func__ << ": device " << addr_str << " not available"; |
357 status = mojom::BluetoothStatus::FAIL; | 355 status = mojom::BluetoothStatus::FAIL; |
358 } | 356 } |
359 | 357 |
360 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( | 358 arc_bridge_service()->bluetooth()->instance()->OnRemoteDeviceProperties( |
361 status, std::move(remote_addr), std::move(properties)); | 359 status, std::move(remote_addr), std::move(properties)); |
362 } | 360 } |
363 | 361 |
364 void ArcBluetoothBridge::SetRemoteDeviceProperty( | 362 void ArcBluetoothBridge::SetRemoteDeviceProperty( |
365 mojom::BluetoothAddressPtr remote_addr, | 363 mojom::BluetoothAddressPtr remote_addr, |
366 mojom::BluetoothPropertyPtr property) { | 364 mojom::BluetoothPropertyPtr property) { |
367 DCHECK(bluetooth_adapter_); | 365 DCHECK(bluetooth_adapter_); |
368 if (!HasBluetoothInstance()) | 366 if (!HasBluetoothInstance()) |
369 return; | 367 return; |
370 | 368 |
371 // TODO(smbarber): Implement SetRemoteDeviceProperty | 369 // TODO(smbarber): Implement SetRemoteDeviceProperty |
372 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( | 370 arc_bridge_service()->bluetooth()->instance()->OnRemoteDeviceProperties( |
373 mojom::BluetoothStatus::FAIL, std::move(remote_addr), | 371 mojom::BluetoothStatus::FAIL, std::move(remote_addr), |
374 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 372 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
375 } | 373 } |
376 | 374 |
377 void ArcBluetoothBridge::GetRemoteServiceRecord( | 375 void ArcBluetoothBridge::GetRemoteServiceRecord( |
378 mojom::BluetoothAddressPtr remote_addr, | 376 mojom::BluetoothAddressPtr remote_addr, |
379 mojom::BluetoothUUIDPtr uuid) { | 377 mojom::BluetoothUUIDPtr uuid) { |
380 // TODO(smbarber): Implement GetRemoteServiceRecord | 378 // TODO(smbarber): Implement GetRemoteServiceRecord |
381 } | 379 } |
382 | 380 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 : mojom::BluetoothAdapterState::OFF); | 430 : mojom::BluetoothAdapterState::OFF); |
433 } | 431 } |
434 | 432 |
435 void ArcBluetoothBridge::OnDiscoveryStarted( | 433 void ArcBluetoothBridge::OnDiscoveryStarted( |
436 std::unique_ptr<BluetoothDiscoverySession> session) { | 434 std::unique_ptr<BluetoothDiscoverySession> session) { |
437 if (!HasBluetoothInstance()) | 435 if (!HasBluetoothInstance()) |
438 return; | 436 return; |
439 | 437 |
440 discovery_session_ = std::move(session); | 438 discovery_session_ = std::move(session); |
441 | 439 |
442 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( | 440 arc_bridge_service()->bluetooth()->instance()->OnDiscoveryStateChanged( |
443 mojom::BluetoothDiscoveryState::STARTED); | 441 mojom::BluetoothDiscoveryState::STARTED); |
444 | 442 |
445 SendCachedDevicesFound(); | 443 SendCachedDevicesFound(); |
446 } | 444 } |
447 | 445 |
448 void ArcBluetoothBridge::OnDiscoveryStopped() { | 446 void ArcBluetoothBridge::OnDiscoveryStopped() { |
449 if (!HasBluetoothInstance()) | 447 if (!HasBluetoothInstance()) |
450 return; | 448 return; |
451 | 449 |
452 discovery_session_.reset(); | 450 discovery_session_.reset(); |
453 | 451 |
454 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( | 452 arc_bridge_service()->bluetooth()->instance()->OnDiscoveryStateChanged( |
455 mojom::BluetoothDiscoveryState::STOPPED); | 453 mojom::BluetoothDiscoveryState::STOPPED); |
456 } | 454 } |
457 | 455 |
458 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, | 456 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, |
459 int32_t transport) { | 457 int32_t transport) { |
460 std::string addr_str = addr->To<std::string>(); | 458 std::string addr_str = addr->To<std::string>(); |
461 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 459 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
462 if (!device || !device->IsPairable()) { | 460 if (!device || !device->IsPairable()) { |
463 VLOG(1) << __func__ << ": device " << addr_str | 461 VLOG(1) << __func__ << ": device " << addr_str |
464 << " is no longer valid or pairable"; | 462 << " is no longer valid or pairable"; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 mojom::BluetoothAddressPtr addr, | 556 mojom::BluetoothAddressPtr addr, |
559 bool connected) const { | 557 bool connected) const { |
560 if (!HasBluetoothInstance()) | 558 if (!HasBluetoothInstance()) |
561 return; | 559 return; |
562 | 560 |
563 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 561 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
564 return; | 562 return; |
565 | 563 |
566 DCHECK(addr); | 564 DCHECK(addr); |
567 | 565 |
568 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 566 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
569 std::move(addr), connected); | 567 std::move(addr), connected); |
570 } | 568 } |
571 | 569 |
572 void ArcBluetoothBridge::OnGattConnected( | 570 void ArcBluetoothBridge::OnGattConnected( |
573 mojom::BluetoothAddressPtr addr, | 571 mojom::BluetoothAddressPtr addr, |
574 std::unique_ptr<BluetoothGattConnection> connection) const { | 572 std::unique_ptr<BluetoothGattConnection> connection) const { |
575 OnGattConnectStateChanged(std::move(addr), true); | 573 OnGattConnectStateChanged(std::move(addr), true); |
576 } | 574 } |
577 | 575 |
578 void ArcBluetoothBridge::OnGattConnectError( | 576 void ArcBluetoothBridge::OnGattConnectError( |
(...skipping 10 matching lines...) Expand all Loading... |
589 void ArcBluetoothBridge::ConnectLEDevice( | 587 void ArcBluetoothBridge::ConnectLEDevice( |
590 mojom::BluetoothAddressPtr remote_addr) { | 588 mojom::BluetoothAddressPtr remote_addr) { |
591 if (!HasBluetoothInstance()) | 589 if (!HasBluetoothInstance()) |
592 return; | 590 return; |
593 | 591 |
594 BluetoothDevice* device = | 592 BluetoothDevice* device = |
595 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 593 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
596 DCHECK(device); | 594 DCHECK(device); |
597 | 595 |
598 if (device->IsConnected()) { | 596 if (device->IsConnected()) { |
599 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 597 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
600 std::move(remote_addr), true); | 598 std::move(remote_addr), true); |
601 return; | 599 return; |
602 } | 600 } |
603 | 601 |
604 // Also pass disconnect callback in error case | 602 // Also pass disconnect callback in error case |
605 // since it would be disconnected anyway. | 603 // since it would be disconnected anyway. |
606 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 604 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
607 device->CreateGattConnection( | 605 device->CreateGattConnection( |
608 base::Bind(&ArcBluetoothBridge::OnGattConnected, | 606 base::Bind(&ArcBluetoothBridge::OnGattConnected, |
609 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 607 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
610 base::Bind(&ArcBluetoothBridge::OnGattConnectError, | 608 base::Bind(&ArcBluetoothBridge::OnGattConnectError, |
611 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 609 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
612 } | 610 } |
613 | 611 |
614 void ArcBluetoothBridge::DisconnectLEDevice( | 612 void ArcBluetoothBridge::DisconnectLEDevice( |
615 mojom::BluetoothAddressPtr remote_addr) { | 613 mojom::BluetoothAddressPtr remote_addr) { |
616 if (!HasBluetoothInstance()) | 614 if (!HasBluetoothInstance()) |
617 return; | 615 return; |
618 | 616 |
619 BluetoothDevice* device = | 617 BluetoothDevice* device = |
620 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 618 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
621 DCHECK(device); | 619 DCHECK(device); |
622 | 620 |
623 if (!device->IsConnected()) { | 621 if (!device->IsConnected()) { |
624 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 622 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
625 std::move(remote_addr), false); | 623 std::move(remote_addr), false); |
626 return; | 624 return; |
627 } | 625 } |
628 | 626 |
629 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 627 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
630 device->Disconnect( | 628 device->Disconnect( |
631 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 629 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
632 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 630 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
633 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 631 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
634 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 632 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
635 } | 633 } |
636 | 634 |
637 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { | 635 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { |
638 if (!HasBluetoothInstance()) | 636 if (!HasBluetoothInstance()) |
639 return; | 637 return; |
640 | 638 |
641 BluetoothDevice* device = | 639 BluetoothDevice* device = |
642 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 640 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
643 DCHECK(device); | 641 DCHECK(device); |
644 | 642 |
645 // Call the callback if discovery is completed | 643 // Call the callback if discovery is completed |
646 if (device->IsGattServicesDiscoveryComplete()) { | 644 if (device->IsGattServicesDiscoveryComplete()) { |
647 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | 645 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( |
648 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 646 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
649 return; | 647 return; |
650 } | 648 } |
651 | 649 |
652 // Discard result. Will call the callback when discovery is completed. | 650 // Discard result. Will call the callback when discovery is completed. |
653 device->GetGattServices(); | 651 device->GetGattServices(); |
654 } | 652 } |
655 | 653 |
656 void ArcBluetoothBridge::OnStartLEListenDone( | 654 void ArcBluetoothBridge::OnStartLEListenDone( |
657 const StartLEListenCallback& callback, | 655 const StartLEListenCallback& callback, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 db.push_back(std::move(characteristic_element)); | 761 db.push_back(std::move(characteristic_element)); |
764 | 762 |
765 for (auto descriptor : characteristic->GetDescriptors()) { | 763 for (auto descriptor : characteristic->GetDescriptors()) { |
766 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( | 764 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( |
767 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, | 765 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, |
768 descriptor)); | 766 descriptor)); |
769 } | 767 } |
770 } | 768 } |
771 } | 769 } |
772 | 770 |
773 arc_bridge_service()->bluetooth_instance()->OnGetGattDB( | 771 arc_bridge_service()->bluetooth()->instance()->OnGetGattDB( |
774 std::move(remote_addr), std::move(db)); | 772 std::move(remote_addr), std::move(db)); |
775 } | 773 } |
776 | 774 |
777 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. | 775 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. |
778 template <class T> | 776 template <class T> |
779 T* ArcBluetoothBridge::FindGattObjectFromUuid( | 777 T* ArcBluetoothBridge::FindGattObjectFromUuid( |
780 const std::vector<T*> gatt_objs, | 778 const std::vector<T*> gatt_objs, |
781 const device::BluetoothUUID uuid) const { | 779 const device::BluetoothUUID uuid) const { |
782 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), | 780 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), |
783 [&](T* obj) { return obj->GetUUID() == uuid; }); | 781 [&](T* obj) { return obj->GetUUID() == uuid; }); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 } | 1023 } |
1026 | 1024 |
1027 void ArcBluetoothBridge::OnDiscoveryError() { | 1025 void ArcBluetoothBridge::OnDiscoveryError() { |
1028 LOG(WARNING) << "failed to change discovery state"; | 1026 LOG(WARNING) << "failed to change discovery state"; |
1029 } | 1027 } |
1030 | 1028 |
1031 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 1029 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
1032 if (!HasBluetoothInstance()) | 1030 if (!HasBluetoothInstance()) |
1033 return; | 1031 return; |
1034 | 1032 |
1035 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1033 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1036 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1034 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1037 mojom::BluetoothBondState::BONDING); | 1035 mojom::BluetoothBondState::BONDING); |
1038 } | 1036 } |
1039 | 1037 |
1040 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { | 1038 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { |
1041 if (!HasBluetoothInstance()) | 1039 if (!HasBluetoothInstance()) |
1042 return; | 1040 return; |
1043 | 1041 |
1044 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1042 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1045 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1043 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1046 mojom::BluetoothBondState::BONDED); | 1044 mojom::BluetoothBondState::BONDED); |
1047 } | 1045 } |
1048 | 1046 |
1049 void ArcBluetoothBridge::OnPairedError( | 1047 void ArcBluetoothBridge::OnPairedError( |
1050 mojom::BluetoothAddressPtr addr, | 1048 mojom::BluetoothAddressPtr addr, |
1051 BluetoothDevice::ConnectErrorCode error_code) const { | 1049 BluetoothDevice::ConnectErrorCode error_code) const { |
1052 if (!HasBluetoothInstance()) | 1050 if (!HasBluetoothInstance()) |
1053 return; | 1051 return; |
1054 | 1052 |
1055 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1053 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1056 mojom::BluetoothStatus::FAIL, std::move(addr), | 1054 mojom::BluetoothStatus::FAIL, std::move(addr), |
1057 mojom::BluetoothBondState::NONE); | 1055 mojom::BluetoothBondState::NONE); |
1058 } | 1056 } |
1059 | 1057 |
1060 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { | 1058 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { |
1061 if (!HasBluetoothInstance()) | 1059 if (!HasBluetoothInstance()) |
1062 return; | 1060 return; |
1063 | 1061 |
1064 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1062 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1065 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1063 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1066 mojom::BluetoothBondState::NONE); | 1064 mojom::BluetoothBondState::NONE); |
1067 } | 1065 } |
1068 | 1066 |
1069 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { | 1067 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { |
1070 if (!HasBluetoothInstance()) | 1068 if (!HasBluetoothInstance()) |
1071 return; | 1069 return; |
1072 | 1070 |
1073 BluetoothDevice* device = | 1071 BluetoothDevice* device = |
1074 bluetooth_adapter_->GetDevice(addr->To<std::string>()); | 1072 bluetooth_adapter_->GetDevice(addr->To<std::string>()); |
1075 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; | 1073 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; |
1076 if (device && device->IsPaired()) { | 1074 if (device && device->IsPaired()) { |
1077 bond_state = mojom::BluetoothBondState::BONDED; | 1075 bond_state = mojom::BluetoothBondState::BONDED; |
1078 } | 1076 } |
1079 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1077 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1080 mojom::BluetoothStatus::FAIL, std::move(addr), bond_state); | 1078 mojom::BluetoothStatus::FAIL, std::move(addr), bond_state); |
1081 } | 1079 } |
1082 | 1080 |
1083 mojo::Array<mojom::BluetoothPropertyPtr> | 1081 mojo::Array<mojom::BluetoothPropertyPtr> |
1084 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, | 1082 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, |
1085 BluetoothDevice* device) const { | 1083 BluetoothDevice* device) const { |
1086 mojo::Array<mojom::BluetoothPropertyPtr> properties; | 1084 mojo::Array<mojom::BluetoothPropertyPtr> properties; |
1087 | 1085 |
1088 if (!device) { | 1086 if (!device) { |
1089 return properties; | 1087 return properties; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 return; | 1281 return; |
1284 | 1282 |
1285 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1283 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1286 for (auto device : devices) { | 1284 for (auto device : devices) { |
1287 if (device->IsPaired()) | 1285 if (device->IsPaired()) |
1288 continue; | 1286 continue; |
1289 | 1287 |
1290 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1288 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1291 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1289 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1292 | 1290 |
1293 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1291 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1294 std::move(properties)); | 1292 std::move(properties)); |
1295 | 1293 |
1296 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { | 1294 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { |
1297 mojom::BluetoothAddressPtr addr = | 1295 mojom::BluetoothAddressPtr addr = |
1298 mojom::BluetoothAddress::From(device->GetAddress()); | 1296 mojom::BluetoothAddress::From(device->GetAddress()); |
1299 int rssi = device->GetInquiryRSSI(); | 1297 int rssi = device->GetInquiryRSSI(); |
1300 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1298 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1301 GetAdvertisingData(device); | 1299 GetAdvertisingData(device); |
1302 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 1300 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
1303 std::move(addr), rssi, std::move(adv_data)); | 1301 std::move(addr), rssi, std::move(adv_data)); |
1304 } | 1302 } |
1305 } | 1303 } |
1306 } | 1304 } |
1307 | 1305 |
1308 bool ArcBluetoothBridge::HasBluetoothInstance() const { | 1306 bool ArcBluetoothBridge::HasBluetoothInstance() const { |
1309 if (!arc_bridge_service()->bluetooth_instance()) { | 1307 if (!arc_bridge_service()->bluetooth()->instance()) { |
1310 LOG(WARNING) << "no Bluetooth instance available"; | 1308 LOG(WARNING) << "no Bluetooth instance available"; |
1311 return false; | 1309 return false; |
1312 } | 1310 } |
1313 | 1311 |
1314 return true; | 1312 return true; |
1315 } | 1313 } |
1316 | 1314 |
1317 void ArcBluetoothBridge::SendCachedPairedDevices() const { | 1315 void ArcBluetoothBridge::SendCachedPairedDevices() const { |
1318 DCHECK(bluetooth_adapter_); | 1316 DCHECK(bluetooth_adapter_); |
1319 if (!HasBluetoothInstance()) | 1317 if (!HasBluetoothInstance()) |
1320 return; | 1318 return; |
1321 | 1319 |
1322 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1320 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1323 for (auto device : devices) { | 1321 for (auto device : devices) { |
1324 if (!device->IsPaired()) | 1322 if (!device->IsPaired()) |
1325 continue; | 1323 continue; |
1326 | 1324 |
1327 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1325 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1328 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1326 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1329 | 1327 |
1330 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1328 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1331 std::move(properties)); | 1329 std::move(properties)); |
1332 | 1330 |
1333 mojom::BluetoothAddressPtr addr = | 1331 mojom::BluetoothAddressPtr addr = |
1334 mojom::BluetoothAddress::From(device->GetAddress()); | 1332 mojom::BluetoothAddress::From(device->GetAddress()); |
1335 | 1333 |
1336 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { | 1334 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { |
1337 int rssi = device->GetInquiryRSSI(); | 1335 int rssi = device->GetInquiryRSSI(); |
1338 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1336 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1339 GetAdvertisingData(device); | 1337 GetAdvertisingData(device); |
1340 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 1338 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
1341 addr->Clone(), rssi, std::move(adv_data)); | 1339 addr->Clone(), rssi, std::move(adv_data)); |
1342 } | 1340 } |
1343 | 1341 |
1344 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING | 1342 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING |
1345 // to | 1343 // to |
1346 // make sure the bond state machine on Android is ready to take the | 1344 // make sure the bond state machine on Android is ready to take the |
1347 // pair-done event. Otherwise the pair-done event will be dropped as an | 1345 // pair-done event. Otherwise the pair-done event will be dropped as an |
1348 // invalid change of paired status. | 1346 // invalid change of paired status. |
1349 OnPairing(addr->Clone()); | 1347 OnPairing(addr->Clone()); |
1350 OnPairedDone(std::move(addr)); | 1348 OnPairedDone(std::move(addr)); |
1351 } | 1349 } |
1352 } | 1350 } |
1353 | 1351 |
1354 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion( | 1352 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion( |
1355 int32_t version_need) const { | 1353 uint32_t version_need) const { |
1356 int32_t version = arc_bridge_service()->bluetooth_version(); | 1354 uint32_t version = arc_bridge_service()->bluetooth()->version(); |
1357 if (version >= version_need) | 1355 if (version >= version_need) |
1358 return true; | 1356 return true; |
1359 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1357 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
1360 << ") need version " << version_need; | 1358 << ") need version " << version_need; |
1361 return false; | 1359 return false; |
1362 } | 1360 } |
1363 | 1361 |
1364 } // namespace arc | 1362 } // namespace arc |
OLD | NEW |