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 <bluetooth/bluetooth.h> | 7 #include <bluetooth/bluetooth.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 BluetoothDevice* device) { | 323 BluetoothDevice* device) { |
324 if (!HasBluetoothInstance()) | 324 if (!HasBluetoothInstance()) |
325 return; | 325 return; |
326 | 326 |
327 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 327 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
328 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 328 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
329 | 329 |
330 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( | 330 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
331 std::move(properties)); | 331 std::move(properties)); |
332 | 332 |
333 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 333 auto* btle_instance = |
334 arc_bridge_service()->bluetooth()->GetInstanceForVersion( | |
335 kMinBtleVersion, "OnLEDeviceFound"); | |
336 if (!btle_instance) | |
334 return; | 337 return; |
335 | 338 |
336 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) | 339 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) |
337 return; | 340 return; |
338 | 341 |
339 mojom::BluetoothAddressPtr addr = | 342 mojom::BluetoothAddressPtr addr = |
340 mojom::BluetoothAddress::From(device->GetAddress()); | 343 mojom::BluetoothAddress::From(device->GetAddress()); |
341 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); | 344 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); |
342 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 345 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
343 GetAdvertisingData(device); | 346 GetAdvertisingData(device); |
344 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( | 347 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
Yusuke Sato
2016/09/16 23:58:48
btle_instance->OnLEDevice...(
Luis Héctor Chávez
2016/09/17 00:30:53
Done.
| |
345 std::move(addr), rssi.value_or(mojom::kUnknownPower), | 348 std::move(addr), rssi.value_or(mojom::kUnknownPower), |
346 std::move(adv_data)); | 349 std::move(adv_data)); |
347 | 350 |
348 if (!device->IsConnected()) | 351 if (!device->IsConnected()) |
349 return; | 352 return; |
350 | 353 |
351 addr = mojom::BluetoothAddress::From(device->GetAddress()); | 354 addr = mojom::BluetoothAddress::From(device->GetAddress()); |
352 OnGattConnectStateChanged(std::move(addr), true); | 355 OnGattConnectStateChanged(std::move(addr), true); |
353 } | 356 } |
354 | 357 |
(...skipping 28 matching lines...) Expand all Loading... | |
383 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) | 386 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) |
384 return; | 387 return; |
385 | 388 |
386 auto it = gatt_connection_cache_.find(old_address); | 389 auto it = gatt_connection_cache_.find(old_address); |
387 if (it == gatt_connection_cache_.end()) | 390 if (it == gatt_connection_cache_.end()) |
388 return; | 391 return; |
389 | 392 |
390 gatt_connection_cache_.erase(it); | 393 gatt_connection_cache_.erase(it); |
391 gatt_connection_cache_.insert(device->GetAddress()); | 394 gatt_connection_cache_.insert(device->GetAddress()); |
392 | 395 |
393 if (!HasBluetoothInstance()) | 396 auto* btle_instance = |
394 return; | 397 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
395 | 398 kMinAddrChangeVersion, "OnLEDeviceAddressChange"); |
396 if (!CheckBluetoothInstanceVersion(kMinAddrChangeVersion)) | 399 if (!btle_instance) |
397 return; | 400 return; |
398 | 401 |
399 mojom::BluetoothAddressPtr old_addr = | 402 mojom::BluetoothAddressPtr old_addr = |
400 mojom::BluetoothAddress::From(old_address); | 403 mojom::BluetoothAddress::From(old_address); |
401 mojom::BluetoothAddressPtr new_addr = | 404 mojom::BluetoothAddressPtr new_addr = |
402 mojom::BluetoothAddress::From(device->GetAddress()); | 405 mojom::BluetoothAddress::From(device->GetAddress()); |
403 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceAddressChange( | 406 btle_instance->OnLEDeviceAddressChange(std::move(old_addr), |
404 std::move(old_addr), std::move(new_addr)); | 407 std::move(new_addr)); |
405 } | 408 } |
406 | 409 |
407 void ArcBluetoothBridge::DevicePairedChanged(BluetoothAdapter* adapter, | 410 void ArcBluetoothBridge::DevicePairedChanged(BluetoothAdapter* adapter, |
408 BluetoothDevice* device, | 411 BluetoothDevice* device, |
409 bool new_paired_status) { | 412 bool new_paired_status) { |
410 DCHECK(adapter); | 413 DCHECK(adapter); |
411 DCHECK(device); | 414 DCHECK(device); |
412 | 415 |
413 mojom::BluetoothAddressPtr addr = | 416 mojom::BluetoothAddressPtr addr = |
414 mojom::BluetoothAddress::From(device->GetAddress()); | 417 mojom::BluetoothAddress::From(device->GetAddress()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 | 454 |
452 void ArcBluetoothBridge::GattServiceRemoved( | 455 void ArcBluetoothBridge::GattServiceRemoved( |
453 BluetoothAdapter* adapter, | 456 BluetoothAdapter* adapter, |
454 BluetoothDevice* device, | 457 BluetoothDevice* device, |
455 BluetoothRemoteGattService* service) { | 458 BluetoothRemoteGattService* service) { |
456 // Placeholder for GATT client functionality | 459 // Placeholder for GATT client functionality |
457 } | 460 } |
458 | 461 |
459 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, | 462 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, |
460 BluetoothDevice* device) { | 463 BluetoothDevice* device) { |
461 if (!HasBluetoothInstance()) | 464 auto* btle_instance = |
462 return; | 465 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
463 | 466 kMinBtleVersion, "OnSearchComplete"); |
464 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 467 if (!btle_instance) |
465 return; | 468 return; |
466 | 469 |
467 mojom::BluetoothAddressPtr addr = | 470 mojom::BluetoothAddressPtr addr = |
468 mojom::BluetoothAddress::From(device->GetAddress()); | 471 mojom::BluetoothAddress::From(device->GetAddress()); |
469 | 472 |
470 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( | 473 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( |
Yusuke Sato
2016/09/16 23:58:48
same
Luis Héctor Chávez
2016/09/17 00:30:53
Done.
| |
471 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 474 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
472 } | 475 } |
473 | 476 |
474 void ArcBluetoothBridge::GattDiscoveryCompleteForService( | 477 void ArcBluetoothBridge::GattDiscoveryCompleteForService( |
475 BluetoothAdapter* adapter, | 478 BluetoothAdapter* adapter, |
476 BluetoothRemoteGattService* service) { | 479 BluetoothRemoteGattService* service) { |
477 // Placeholder for GATT client functionality | 480 // Placeholder for GATT client functionality |
478 } | 481 } |
479 | 482 |
480 void ArcBluetoothBridge::GattServiceChanged( | 483 void ArcBluetoothBridge::GattServiceChanged( |
(...skipping 23 matching lines...) Expand all Loading... | |
504 void ArcBluetoothBridge::GattDescriptorRemoved( | 507 void ArcBluetoothBridge::GattDescriptorRemoved( |
505 BluetoothAdapter* adapter, | 508 BluetoothAdapter* adapter, |
506 BluetoothRemoteGattDescriptor* descriptor) { | 509 BluetoothRemoteGattDescriptor* descriptor) { |
507 // Placeholder for GATT client functionality | 510 // Placeholder for GATT client functionality |
508 } | 511 } |
509 | 512 |
510 void ArcBluetoothBridge::GattCharacteristicValueChanged( | 513 void ArcBluetoothBridge::GattCharacteristicValueChanged( |
511 BluetoothAdapter* adapter, | 514 BluetoothAdapter* adapter, |
512 BluetoothRemoteGattCharacteristic* characteristic, | 515 BluetoothRemoteGattCharacteristic* characteristic, |
513 const std::vector<uint8_t>& value) { | 516 const std::vector<uint8_t>& value) { |
514 if (!HasBluetoothInstance()) | 517 auto* btle_instance = |
515 return; | 518 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
516 | 519 kMinBtleNotifyVersion, "OnGattNotify"); |
517 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion)) | 520 if (!btle_instance) |
518 return; | 521 return; |
519 | 522 |
520 BluetoothRemoteGattService* service = characteristic->GetService(); | 523 BluetoothRemoteGattService* service = characteristic->GetService(); |
521 BluetoothDevice* device = service->GetDevice(); | 524 BluetoothDevice* device = service->GetDevice(); |
522 mojom::BluetoothAddressPtr address = | 525 mojom::BluetoothAddressPtr address = |
523 mojom::BluetoothAddress::From(device->GetAddress()); | 526 mojom::BluetoothAddress::From(device->GetAddress()); |
524 mojom::BluetoothGattServiceIDPtr service_id = | 527 mojom::BluetoothGattServiceIDPtr service_id = |
525 mojom::BluetoothGattServiceID::New(); | 528 mojom::BluetoothGattServiceID::New(); |
526 service_id->is_primary = service->IsPrimary(); | 529 service_id->is_primary = service->IsPrimary(); |
527 service_id->id = mojom::BluetoothGattID::New(); | 530 service_id->id = mojom::BluetoothGattID::New(); |
528 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); | 531 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); |
529 service_id->id->uuid = service->GetUUID(); | 532 service_id->id->uuid = service->GetUUID(); |
530 | 533 |
531 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); | 534 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); |
532 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); | 535 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); |
533 char_id->uuid = characteristic->GetUUID(); | 536 char_id->uuid = characteristic->GetUUID(); |
534 | 537 |
535 arc_bridge_service()->bluetooth()->instance()->OnGattNotify( | 538 arc_bridge_service()->bluetooth()->instance()->OnGattNotify( |
Yusuke Sato
2016/09/16 23:58:48
same
Luis Héctor Chávez
2016/09/17 00:30:53
Done.
| |
536 std::move(address), std::move(service_id), std::move(char_id), | 539 std::move(address), std::move(service_id), std::move(char_id), |
537 true /* is_notify */, mojo::Array<uint8_t>::From(value)); | 540 true /* is_notify */, mojo::Array<uint8_t>::From(value)); |
538 } | 541 } |
539 | 542 |
540 void ArcBluetoothBridge::GattDescriptorValueChanged( | 543 void ArcBluetoothBridge::GattDescriptorValueChanged( |
541 BluetoothAdapter* adapter, | 544 BluetoothAdapter* adapter, |
542 BluetoothRemoteGattDescriptor* descriptor, | 545 BluetoothRemoteGattDescriptor* descriptor, |
543 const std::vector<uint8_t>& value) { | 546 const std::vector<uint8_t>& value) { |
544 // Placeholder for GATT client functionality | 547 // Placeholder for GATT client functionality |
545 } | 548 } |
546 | 549 |
547 template <class LocalGattAttribute> | 550 template <class LocalGattAttribute> |
548 void ArcBluetoothBridge::OnGattAttributeReadRequest( | 551 void ArcBluetoothBridge::OnGattAttributeReadRequest( |
549 const BluetoothDevice* device, | 552 const BluetoothDevice* device, |
550 const LocalGattAttribute* attribute, | 553 const LocalGattAttribute* attribute, |
551 int offset, | 554 int offset, |
552 const ValueCallback& success_callback, | 555 const ValueCallback& success_callback, |
553 const ErrorCallback& error_callback) { | 556 const ErrorCallback& error_callback) { |
554 DCHECK(CalledOnValidThread()); | 557 DCHECK(CalledOnValidThread()); |
555 if (!HasBluetoothInstance() || | 558 auto* bluetooth_instance = |
556 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || | 559 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
557 !IsGattOffsetValid(offset)) { | 560 kMinGattServerVersion, "RequestGattRead"); |
561 if (!bluetooth_instance || !IsGattOffsetValid(offset)) { | |
558 error_callback.Run(); | 562 error_callback.Run(); |
559 return; | 563 return; |
560 } | 564 } |
561 | 565 |
562 DCHECK(gatt_handle_.find(attribute->GetIdentifier()) != gatt_handle_.end()); | 566 DCHECK(gatt_handle_.find(attribute->GetIdentifier()) != gatt_handle_.end()); |
563 | 567 |
564 arc_bridge_service()->bluetooth()->instance()->RequestGattRead( | 568 bluetooth_instance->RequestGattRead( |
565 mojom::BluetoothAddress::From(device->GetAddress()), | 569 mojom::BluetoothAddress::From(device->GetAddress()), |
566 gatt_handle_[attribute->GetIdentifier()], offset, false /* is_long */, | 570 gatt_handle_[attribute->GetIdentifier()], offset, false /* is_long */, |
567 base::Bind(&OnGattServerRead, success_callback, error_callback)); | 571 base::Bind(&OnGattServerRead, success_callback, error_callback)); |
568 } | 572 } |
569 | 573 |
570 template <class LocalGattAttribute> | 574 template <class LocalGattAttribute> |
571 void ArcBluetoothBridge::OnGattAttributeWriteRequest( | 575 void ArcBluetoothBridge::OnGattAttributeWriteRequest( |
572 const BluetoothDevice* device, | 576 const BluetoothDevice* device, |
573 const LocalGattAttribute* attribute, | 577 const LocalGattAttribute* attribute, |
574 const std::vector<uint8_t>& value, | 578 const std::vector<uint8_t>& value, |
575 int offset, | 579 int offset, |
576 const base::Closure& success_callback, | 580 const base::Closure& success_callback, |
577 const ErrorCallback& error_callback) { | 581 const ErrorCallback& error_callback) { |
578 DCHECK(CalledOnValidThread()); | 582 DCHECK(CalledOnValidThread()); |
579 if (!HasBluetoothInstance() || | 583 auto* bluetooth_instance = |
580 !CheckBluetoothInstanceVersion(kMinGattServerVersion) || | 584 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
581 !IsGattOffsetValid(offset)) { | 585 kMinGattServerVersion, "RequestGattWrite"); |
586 if (!bluetooth_instance || !IsGattOffsetValid(offset)) { | |
582 error_callback.Run(); | 587 error_callback.Run(); |
583 return; | 588 return; |
584 } | 589 } |
585 | 590 |
586 DCHECK(gatt_handle_.find(attribute->GetIdentifier()) != gatt_handle_.end()); | 591 DCHECK(gatt_handle_.find(attribute->GetIdentifier()) != gatt_handle_.end()); |
587 | 592 |
588 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite( | 593 bluetooth_instance->RequestGattWrite( |
589 mojom::BluetoothAddress::From(device->GetAddress()), | 594 mojom::BluetoothAddress::From(device->GetAddress()), |
590 gatt_handle_[attribute->GetIdentifier()], offset, | 595 gatt_handle_[attribute->GetIdentifier()], offset, |
591 mojo::Array<uint8_t>::From(value), | 596 mojo::Array<uint8_t>::From(value), |
592 base::Bind(&OnGattServerWrite, success_callback, error_callback)); | 597 base::Bind(&OnGattServerWrite, success_callback, error_callback)); |
593 } | 598 } |
594 | 599 |
595 void ArcBluetoothBridge::OnCharacteristicReadRequest( | 600 void ArcBluetoothBridge::OnCharacteristicReadRequest( |
596 const BluetoothDevice* device, | 601 const BluetoothDevice* device, |
597 const BluetoothLocalGattCharacteristic* characteristic, | 602 const BluetoothLocalGattCharacteristic* characteristic, |
598 int offset, | 603 int offset, |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
972 weak_factory_.GetWeakPtr())); | 977 weak_factory_.GetWeakPtr())); |
973 } | 978 } |
974 | 979 |
975 void ArcBluetoothBridge::StopLEScan() { | 980 void ArcBluetoothBridge::StopLEScan() { |
976 CancelDiscovery(); | 981 CancelDiscovery(); |
977 } | 982 } |
978 | 983 |
979 void ArcBluetoothBridge::OnGattConnectStateChanged( | 984 void ArcBluetoothBridge::OnGattConnectStateChanged( |
980 mojom::BluetoothAddressPtr addr, | 985 mojom::BluetoothAddressPtr addr, |
981 bool connected) const { | 986 bool connected) const { |
982 if (!HasBluetoothInstance()) | 987 auto* btle_instance = |
983 return; | 988 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
984 | 989 kMinBtleVersion, "OnLEConnectionStateChange"); |
985 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 990 if (!btle_instance) |
986 return; | 991 return; |
987 | 992 |
988 DCHECK(addr); | 993 DCHECK(addr); |
989 | 994 |
990 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( | 995 btle_instance->OnLEConnectionStateChange(std::move(addr), connected); |
991 std::move(addr), connected); | |
992 } | 996 } |
993 | 997 |
994 void ArcBluetoothBridge::OnGattConnected( | 998 void ArcBluetoothBridge::OnGattConnected( |
995 mojom::BluetoothAddressPtr addr, | 999 mojom::BluetoothAddressPtr addr, |
996 std::unique_ptr<BluetoothGattConnection> connection) { | 1000 std::unique_ptr<BluetoothGattConnection> connection) { |
997 DCHECK(CalledOnValidThread()); | 1001 DCHECK(CalledOnValidThread()); |
998 gatt_connections_[addr->To<std::string>()] = std::move(connection); | 1002 gatt_connections_[addr->To<std::string>()] = std::move(connection); |
999 OnGattConnectStateChanged(std::move(addr), true); | 1003 OnGattConnectStateChanged(std::move(addr), true); |
1000 } | 1004 } |
1001 | 1005 |
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1842 advertising_data.push_back(std::move(service_data_element)); | 1846 advertising_data.push_back(std::move(service_data_element)); |
1843 } | 1847 } |
1844 | 1848 |
1845 return advertising_data; | 1849 return advertising_data; |
1846 } | 1850 } |
1847 | 1851 |
1848 void ArcBluetoothBridge::SendCachedDevicesFound() const { | 1852 void ArcBluetoothBridge::SendCachedDevicesFound() const { |
1849 // Send devices that have already been discovered, but aren't connected. | 1853 // Send devices that have already been discovered, but aren't connected. |
1850 if (!HasBluetoothInstance()) | 1854 if (!HasBluetoothInstance()) |
1851 return; | 1855 return; |
1856 auto* btle_instance = | |
1857 arc_bridge_service()->bluetooth()->GetInstanceForVersion( | |
1858 kMinBtleVersion, "OnLEDeviceFound"); | |
1852 | 1859 |
1853 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1860 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1854 for (auto* device : devices) { | 1861 for (auto* device : devices) { |
1855 if (device->IsPaired()) | 1862 if (device->IsPaired()) |
1856 continue; | 1863 continue; |
1857 | 1864 |
1858 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1865 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1859 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1866 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1860 | 1867 |
1861 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( | 1868 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1862 std::move(properties)); | 1869 std::move(properties)); |
1863 | 1870 |
1864 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { | 1871 if (btle_instance) { |
1865 mojom::BluetoothAddressPtr addr = | 1872 mojom::BluetoothAddressPtr addr = |
1866 mojom::BluetoothAddress::From(device->GetAddress()); | 1873 mojom::BluetoothAddress::From(device->GetAddress()); |
1867 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); | 1874 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); |
1868 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1875 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1869 GetAdvertisingData(device); | 1876 GetAdvertisingData(device); |
1870 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( | 1877 btle_instance->OnLEDeviceFound(std::move(addr), |
1871 std::move(addr), rssi.value_or(mojom::kUnknownPower), | 1878 rssi.value_or(mojom::kUnknownPower), |
1872 std::move(adv_data)); | 1879 std::move(adv_data)); |
1873 } | 1880 } |
1874 } | 1881 } |
1875 } | 1882 } |
1876 | 1883 |
1877 bool ArcBluetoothBridge::HasBluetoothInstance() const { | 1884 bool ArcBluetoothBridge::HasBluetoothInstance() const { |
Yusuke Sato
2016/09/17 01:38:56
nice!
| |
1878 if (!arc_bridge_service()->bluetooth()->instance()) { | 1885 if (!arc_bridge_service()->bluetooth()->instance()) { |
1879 LOG(WARNING) << "no Bluetooth instance available"; | 1886 LOG(WARNING) << "no Bluetooth instance available"; |
1880 return false; | 1887 return false; |
1881 } | 1888 } |
1882 | 1889 |
1883 return true; | 1890 return true; |
1884 } | 1891 } |
1885 | 1892 |
1886 void ArcBluetoothBridge::SendCachedPairedDevices() const { | 1893 void ArcBluetoothBridge::SendCachedPairedDevices() const { |
1887 DCHECK(bluetooth_adapter_); | 1894 DCHECK(bluetooth_adapter_); |
1888 if (!HasBluetoothInstance()) | 1895 if (!HasBluetoothInstance()) |
1889 return; | 1896 return; |
1897 auto* btle_instance = | |
1898 arc_bridge_service()->bluetooth()->GetInstanceForVersion( | |
1899 kMinBtleVersion, "OnLEDeviceFound"); | |
1890 | 1900 |
1891 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1901 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1892 for (auto* device : devices) { | 1902 for (auto* device : devices) { |
1893 if (!device->IsPaired()) | 1903 if (!device->IsPaired()) |
1894 continue; | 1904 continue; |
1895 | 1905 |
1896 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1906 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1897 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1907 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1898 | 1908 |
1899 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( | 1909 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1900 std::move(properties)); | 1910 std::move(properties)); |
1901 | 1911 |
1902 mojom::BluetoothAddressPtr addr = | 1912 mojom::BluetoothAddressPtr addr = |
1903 mojom::BluetoothAddress::From(device->GetAddress()); | 1913 mojom::BluetoothAddress::From(device->GetAddress()); |
1904 | 1914 |
1905 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { | 1915 if (btle_instance) { |
1906 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); | 1916 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); |
1907 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1917 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1908 GetAdvertisingData(device); | 1918 GetAdvertisingData(device); |
1909 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( | 1919 btle_instance->OnLEDeviceFound(addr->Clone(), |
1910 addr->Clone(), rssi.value_or(mojom::kUnknownPower), | 1920 rssi.value_or(mojom::kUnknownPower), |
1911 std::move(adv_data)); | 1921 std::move(adv_data)); |
1912 } | 1922 } |
1913 | 1923 |
1914 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING | 1924 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING |
1915 // to make sure the bond state machine on Android is ready to take the | 1925 // to make sure the bond state machine on Android is ready to take the |
1916 // pair-done event. Otherwise the pair-done event will be dropped as an | 1926 // pair-done event. Otherwise the pair-done event will be dropped as an |
1917 // invalid change of paired status. | 1927 // invalid change of paired status. |
1918 OnPairing(addr->Clone()); | 1928 OnPairing(addr->Clone()); |
1919 OnPairedDone(std::move(addr)); | 1929 OnPairedDone(std::move(addr)); |
1920 } | 1930 } |
1921 } | 1931 } |
1922 | 1932 |
1923 void ArcBluetoothBridge::OnGetServiceRecordsDone( | 1933 void ArcBluetoothBridge::OnGetServiceRecordsDone( |
1924 mojom::BluetoothAddressPtr remote_addr, | 1934 mojom::BluetoothAddressPtr remote_addr, |
1925 const BluetoothUUID& target_uuid, | 1935 const BluetoothUUID& target_uuid, |
1926 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez) { | 1936 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez) { |
1927 if (!CheckBluetoothInstanceVersion(kMinSdpSupportVersion)) | 1937 auto* sdp_bluetooth_instance = |
1938 arc_bridge_service()->bluetooth()->GetInstanceForVersion( | |
1939 kMinSdpSupportVersion, "OnGetSdpRecords"); | |
1940 if (!sdp_bluetooth_instance) | |
1928 return; | 1941 return; |
1929 | 1942 |
1930 if (!HasBluetoothInstance()) | 1943 sdp_bluetooth_instance->OnGetSdpRecords( |
1931 return; | |
1932 | |
1933 arc_bridge_service()->bluetooth()->instance()->OnGetSdpRecords( | |
1934 mojom::BluetoothStatus::SUCCESS, std::move(remote_addr), target_uuid, | 1944 mojom::BluetoothStatus::SUCCESS, std::move(remote_addr), target_uuid, |
1935 mojo::Array<mojom::BluetoothSdpRecordPtr>::From(records_bluez)); | 1945 mojo::Array<mojom::BluetoothSdpRecordPtr>::From(records_bluez)); |
1936 } | 1946 } |
1937 | 1947 |
1938 void ArcBluetoothBridge::OnGetServiceRecordsError( | 1948 void ArcBluetoothBridge::OnGetServiceRecordsError( |
1939 mojom::BluetoothAddressPtr remote_addr, | 1949 mojom::BluetoothAddressPtr remote_addr, |
1940 const BluetoothUUID& target_uuid, | 1950 const BluetoothUUID& target_uuid, |
1941 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { | 1951 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code) { |
1942 if (!HasBluetoothInstance()) | 1952 auto* sdp_bluetooth_instance = |
1943 return; | 1953 arc_bridge_service()->bluetooth()->GetInstanceForVersion( |
1944 | 1954 kMinSdpSupportVersion, "OnGetSdpRecords"); |
1945 if (!CheckBluetoothInstanceVersion(kMinSdpSupportVersion)) | 1955 if (!sdp_bluetooth_instance) |
1946 return; | 1956 return; |
1947 | 1957 |
1948 mojom::BluetoothStatus status; | 1958 mojom::BluetoothStatus status; |
1949 | 1959 |
1950 switch (error_code) { | 1960 switch (error_code) { |
1951 case bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY: | 1961 case bluez::BluetoothServiceRecordBlueZ::ErrorCode::ERROR_ADAPTER_NOT_READY: |
1952 status = mojom::BluetoothStatus::NOT_READY; | 1962 status = mojom::BluetoothStatus::NOT_READY; |
1953 break; | 1963 break; |
1954 case bluez::BluetoothServiceRecordBlueZ::ErrorCode:: | 1964 case bluez::BluetoothServiceRecordBlueZ::ErrorCode:: |
1955 ERROR_DEVICE_DISCONNECTED: | 1965 ERROR_DEVICE_DISCONNECTED: |
1956 status = mojom::BluetoothStatus::RMT_DEV_DOWN; | 1966 status = mojom::BluetoothStatus::RMT_DEV_DOWN; |
1957 break; | 1967 break; |
1958 default: | 1968 default: |
1959 status = mojom::BluetoothStatus::FAIL; | 1969 status = mojom::BluetoothStatus::FAIL; |
1960 break; | 1970 break; |
1961 } | 1971 } |
1962 | 1972 |
1963 arc_bridge_service()->bluetooth()->instance()->OnGetSdpRecords( | 1973 sdp_bluetooth_instance->OnGetSdpRecords( |
1964 status, std::move(remote_addr), target_uuid, | 1974 status, std::move(remote_addr), target_uuid, |
1965 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); | 1975 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); |
1966 } | 1976 } |
1967 | 1977 |
1968 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion( | |
1969 uint32_t version_need) const { | |
1970 uint32_t version = arc_bridge_service()->bluetooth()->version(); | |
1971 if (version >= version_need) | |
1972 return true; | |
1973 LOG(WARNING) << "Bluetooth instance is too old (version " << version | |
1974 << ") need version " << version_need; | |
1975 return false; | |
1976 } | |
1977 | |
1978 bool ArcBluetoothBridge::CalledOnValidThread() { | 1978 bool ArcBluetoothBridge::CalledOnValidThread() { |
1979 return thread_checker_.CalledOnValidThread(); | 1979 return thread_checker_.CalledOnValidThread(); |
1980 } | 1980 } |
1981 | 1981 |
1982 } // namespace arc | 1982 } // namespace arc |
OLD | NEW |