Chromium Code Reviews| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "components/arc/arc_bridge_service.h" | 20 #include "components/arc/arc_bridge_service.h" |
| 21 #include "components/arc/bluetooth/bluetooth_type_converters.h" | 21 #include "components/arc/bluetooth/bluetooth_type_converters.h" |
| 22 #include "device/bluetooth/bluetooth_adapter_factory.h" | 22 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 23 #include "device/bluetooth/bluetooth_device.h" | 23 #include "device/bluetooth/bluetooth_device.h" |
| 24 #include "device/bluetooth/bluetooth_gatt_connection.h" | |
| 25 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | |
| 24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 25 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 27 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
| 26 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| 27 | 29 |
| 28 using device::BluetoothAdapter; | 30 using device::BluetoothAdapter; |
| 29 using device::BluetoothAdapterFactory; | 31 using device::BluetoothAdapterFactory; |
| 32 using device::BluetoothAdvertisement; | |
| 30 using device::BluetoothDevice; | 33 using device::BluetoothDevice; |
| 34 using device::BluetoothDiscoveryFilter; | |
| 31 using device::BluetoothDiscoverySession; | 35 using device::BluetoothDiscoverySession; |
| 36 using device::BluetoothGattConnection; | |
| 37 using device::BluetoothGattNotifySession; | |
| 38 using device::BluetoothGattCharacteristic; | |
| 39 using device::BluetoothGattDescriptor; | |
| 40 using device::BluetoothGattService; | |
| 32 using device::BluetoothRemoteGattCharacteristic; | 41 using device::BluetoothRemoteGattCharacteristic; |
| 33 using device::BluetoothRemoteGattDescriptor; | 42 using device::BluetoothRemoteGattDescriptor; |
| 34 using device::BluetoothRemoteGattService; | 43 using device::BluetoothRemoteGattService; |
| 44 using device::BluetoothUUID; | |
| 35 | 45 |
| 36 namespace arc { | 46 namespace arc { |
| 37 | 47 |
| 38 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 48 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
| 39 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 49 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
| 40 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 50 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 41 VLOG(1) << "registering bluetooth adapter"; | 51 VLOG(1) << "registering bluetooth adapter"; |
| 42 BluetoothAdapterFactory::GetAdapter(base::Bind( | 52 BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 43 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 53 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
| 44 } else { | 54 } else { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 return; | 108 return; |
| 99 | 109 |
| 100 if (device->IsConnected()) | 110 if (device->IsConnected()) |
| 101 return; | 111 return; |
| 102 | 112 |
| 103 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 113 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 104 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 114 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 105 | 115 |
| 106 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 116 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 107 std::move(properties)); | 117 std::move(properties)); |
| 118 | |
| 119 if (arc_bridge_service()->bluetooth_version() < 1) { | |
|
rkc
2016/06/07 02:34:31
s/1/kMinBtleVersion
Here and other places where yo
puthik_chromium
2016/06/08 23:59:49
Done.
| |
| 120 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 mojom::BluetoothAddressPtr addr = | |
| 125 mojom::BluetoothAddress::From(device->GetAddress()); | |
| 126 int rssi = device->GetInquiryRSSI(); | |
| 127 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | |
| 128 GetAdvertisingData(device); | |
| 129 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | |
| 130 std::move(addr), rssi, std::move(adv_data)); | |
| 108 } | 131 } |
| 109 | 132 |
| 110 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, | 133 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, |
| 111 BluetoothDevice* device) { | 134 BluetoothDevice* device) { |
| 112 // TODO(smbarber): device properties changed; inform the container. | 135 // TODO(smbarber): device properties changed; inform the container. |
| 113 } | 136 } |
| 114 | 137 |
| 115 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, | 138 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, |
| 116 BluetoothDevice* device, | 139 BluetoothDevice* device, |
| 117 const std::string& old_address) { | 140 const std::string& old_address) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 | 180 |
| 158 void ArcBluetoothBridge::GattServiceRemoved( | 181 void ArcBluetoothBridge::GattServiceRemoved( |
| 159 BluetoothAdapter* adapter, | 182 BluetoothAdapter* adapter, |
| 160 BluetoothDevice* device, | 183 BluetoothDevice* device, |
| 161 BluetoothRemoteGattService* service) { | 184 BluetoothRemoteGattService* service) { |
| 162 // Placeholder for GATT client functionality | 185 // Placeholder for GATT client functionality |
| 163 } | 186 } |
| 164 | 187 |
| 165 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, | 188 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, |
| 166 BluetoothDevice* device) { | 189 BluetoothDevice* device) { |
| 167 // Placeholder for GATT client functionality | 190 if (!HasBluetoothInstance()) |
| 191 return; | |
| 192 | |
| 193 if (arc_bridge_service()->bluetooth_version() < 1) { | |
| 194 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | |
| 195 return; | |
| 196 } | |
| 197 | |
| 198 mojom::BluetoothAddressPtr addr = | |
| 199 mojom::BluetoothAddress::From(device->GetAddress()); | |
| 200 | |
| 201 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | |
| 202 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 168 } | 203 } |
| 169 | 204 |
| 170 void ArcBluetoothBridge::GattDiscoveryCompleteForService( | 205 void ArcBluetoothBridge::GattDiscoveryCompleteForService( |
| 171 BluetoothAdapter* adapter, | 206 BluetoothAdapter* adapter, |
| 172 BluetoothRemoteGattService* service) { | 207 BluetoothRemoteGattService* service) { |
| 173 // Placeholder for GATT client functionality | 208 // Placeholder for GATT client functionality |
| 174 } | 209 } |
| 175 | 210 |
| 176 void ArcBluetoothBridge::GattServiceChanged( | 211 void ArcBluetoothBridge::GattServiceChanged( |
| 177 BluetoothAdapter* adapter, | 212 BluetoothAdapter* adapter, |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 BluetoothDevice* device = | 489 BluetoothDevice* device = |
| 455 bluetooth_adapter_->GetDevice(addr->To<std::string>()); | 490 bluetooth_adapter_->GetDevice(addr->To<std::string>()); |
| 456 if (!device) { | 491 if (!device) { |
| 457 callback.Run(false); | 492 callback.Run(false); |
| 458 return; | 493 return; |
| 459 } | 494 } |
| 460 | 495 |
| 461 callback.Run(device->IsConnected()); | 496 callback.Run(device->IsConnected()); |
| 462 } | 497 } |
| 463 | 498 |
| 499 void ArcBluetoothBridge::StartLEScan() { | |
| 500 DCHECK(bluetooth_adapter_); | |
| 501 if (discovery_session_) { | |
| 502 LOG(ERROR) << "Discovery session already running; leaving alone"; | |
|
rkc
2016/06/07 02:34:31
s/ERROR/WARNING
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 503 SendCachedDevicesFound(); | |
| 504 return; | |
| 505 } | |
| 506 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
| 507 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
| 508 std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
| 509 bluetooth_adapter_->StartDiscoverySessionWithFilter( | |
| 510 std::move(discovery_filter), | |
|
rkc
2016/06/07 02:34:30
s/discovery_filter/base::WrapUniqe(new BluetoothDi
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 511 base::Bind(&ArcBluetoothBridge::OnDiscoveryStarted, | |
| 512 weak_factory_.GetWeakPtr()), | |
| 513 base::Bind(&ArcBluetoothBridge::OnDiscoveryError, | |
| 514 weak_factory_.GetWeakPtr())); | |
| 515 } | |
| 516 | |
| 517 void ArcBluetoothBridge::StopLEScan() { | |
| 518 CancelDiscovery(); | |
| 519 } | |
| 520 | |
| 521 void ArcBluetoothBridge::OnGattConnected( | |
| 522 mojom::BluetoothAddressPtr addr, | |
| 523 std::unique_ptr<BluetoothGattConnection> connection) const { | |
| 524 if (!HasBluetoothInstance()) | |
| 525 return; | |
| 526 | |
| 527 if (arc_bridge_service()->bluetooth_version() < 1) { | |
| 528 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | |
| 529 return; | |
| 530 } | |
| 531 | |
| 532 DCHECK(addr); | |
| 533 | |
| 534 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | |
| 535 std::move(addr), true); | |
| 536 } | |
| 537 | |
| 538 void ArcBluetoothBridge::OnGattConnectError( | |
| 539 mojom::BluetoothAddressPtr addr, | |
| 540 BluetoothDevice::ConnectErrorCode error_code) const { | |
| 541 if (!HasBluetoothInstance()) | |
| 542 return; | |
| 543 | |
| 544 if (arc_bridge_service()->bluetooth_version() < 1) { | |
| 545 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | |
| 546 return; | |
| 547 } | |
| 548 | |
| 549 DCHECK(addr); | |
| 550 | |
| 551 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | |
| 552 std::move(addr), false); | |
| 553 } | |
| 554 | |
| 555 void ArcBluetoothBridge::OnGattDisconnected( | |
| 556 mojom::BluetoothAddressPtr addr) const { | |
| 557 if (!HasBluetoothInstance()) | |
| 558 return; | |
| 559 | |
| 560 if (arc_bridge_service()->bluetooth_version() < 1) { | |
| 561 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE"; | |
| 562 return; | |
| 563 } | |
| 564 | |
| 565 DCHECK(addr); | |
| 566 | |
| 567 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | |
| 568 std::move(addr), false); | |
| 569 } | |
| 570 | |
| 571 void ArcBluetoothBridge::ConnectLEDevice( | |
| 572 mojom::BluetoothAddressPtr remote_addr) { | |
| 573 if (!HasBluetoothInstance()) | |
| 574 return; | |
| 575 | |
| 576 BluetoothDevice* device = | |
|
rkc
2016/06/07 02:34:30
Check if device is nullptr? Here and a few places
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 577 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 578 | |
| 579 if (device->IsConnected()) { | |
| 580 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | |
| 581 std::move(remote_addr), true); | |
| 582 return; | |
| 583 } | |
| 584 | |
| 585 // Also pass disconnect callback in error case | |
| 586 // since it would be disconnected anyway. | |
| 587 mojom::BluetoothAddressPtr remote_addr1 = remote_addr.Clone(); | |
|
rkc
2016/06/07 02:34:29
remote_addr_clone would be cleaner than just addin
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 588 device->CreateGattConnection( | |
| 589 base::Bind(&ArcBluetoothBridge::OnGattConnected, | |
| 590 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | |
| 591 base::Bind(&ArcBluetoothBridge::OnGattConnectError, | |
| 592 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr1))); | |
| 593 } | |
| 594 | |
| 595 void ArcBluetoothBridge::DisconnectLEDevice( | |
| 596 mojom::BluetoothAddressPtr remote_addr) { | |
| 597 if (!HasBluetoothInstance()) | |
| 598 return; | |
| 599 | |
| 600 BluetoothDevice* device = | |
| 601 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 602 | |
| 603 if (!device->IsConnected()) { | |
| 604 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | |
| 605 std::move(remote_addr), false); | |
| 606 return; | |
| 607 } | |
| 608 | |
| 609 mojom::BluetoothAddressPtr remote_addr1 = remote_addr.Clone(); | |
| 610 device->Disconnect( | |
| 611 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | |
| 612 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | |
| 613 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | |
| 614 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr1))); | |
|
rkc
2016/06/07 02:34:29
as above.
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 615 } | |
| 616 | |
| 617 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { | |
| 618 if (!HasBluetoothInstance()) | |
| 619 return; | |
| 620 | |
| 621 BluetoothDevice* device = | |
| 622 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 623 | |
| 624 // Call the callback if discovery is completed | |
| 625 if (device->IsGattServicesDiscoveryComplete()) { | |
| 626 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | |
| 627 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 628 return; | |
| 629 } | |
| 630 | |
| 631 // Discard result. Will call the callback when discovery is completed. | |
| 632 device->GetGattServices(); | |
| 633 } | |
| 634 | |
| 635 void ArcBluetoothBridge::OnStartLEListenDone( | |
| 636 const StartLEListenCallback& callback, | |
| 637 scoped_refptr<BluetoothAdvertisement> adv) { | |
| 638 advertisment_ = adv; | |
| 639 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 640 } | |
| 641 | |
| 642 void ArcBluetoothBridge::OnStartLEListenError( | |
| 643 const StartLEListenCallback& callback, | |
| 644 BluetoothAdvertisement::ErrorCode error_code) { | |
| 645 advertisment_ = nullptr; | |
| 646 callback.Run(mojom::BluetoothGattStatus::GATT_FAILURE); | |
|
rkc
2016/06/07 02:34:31
Are there any better errors we can send up to Andr
puthik_chromium
2016/06/08 23:59:49
No
| |
| 647 } | |
| 648 | |
| 649 void ArcBluetoothBridge::StartLEListen(const StartLEListenCallback& callback) { | |
| 650 std::unique_ptr<BluetoothAdvertisement::Data> adv_data = | |
| 651 base::WrapUnique(new BluetoothAdvertisement::Data( | |
| 652 BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST)); | |
| 653 adv_data->set_service_uuids( | |
|
rkc
2016/06/07 02:34:31
You should be able to create an advertisement with
puthik_chromium
2016/06/08 23:59:49
Done.
| |
| 654 base::WrapUnique(new BluetoothAdvertisement::UUIDList())); | |
| 655 adv_data->set_manufacturer_data( | |
| 656 base::WrapUnique(new BluetoothAdvertisement::ManufacturerData())); | |
| 657 adv_data->set_solicit_uuids( | |
| 658 base::WrapUnique(new BluetoothAdvertisement::UUIDList())); | |
| 659 adv_data->set_service_data( | |
| 660 base::WrapUnique(new BluetoothAdvertisement::ServiceData())); | |
| 661 bluetooth_adapter_->RegisterAdvertisement( | |
| 662 std::move(adv_data), base::Bind(&ArcBluetoothBridge::OnStartLEListenDone, | |
| 663 weak_factory_.GetWeakPtr(), callback), | |
| 664 base::Bind(&ArcBluetoothBridge::OnStartLEListenError, | |
| 665 weak_factory_.GetWeakPtr(), callback)); | |
| 666 } | |
| 667 | |
| 668 void ArcBluetoothBridge::OnStopLEListenDone( | |
| 669 const StopLEListenCallback& callback) { | |
| 670 advertisment_ = nullptr; | |
| 671 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 672 } | |
| 673 | |
| 674 void ArcBluetoothBridge::OnStopLEListenError( | |
| 675 const StopLEListenCallback& callback, | |
| 676 BluetoothAdvertisement::ErrorCode error_code) { | |
| 677 advertisment_ = nullptr; | |
| 678 callback.Run(mojom::BluetoothGattStatus::GATT_FAILURE); | |
| 679 } | |
| 680 | |
| 681 void ArcBluetoothBridge::StopLEListen(const StopLEListenCallback& callback) { | |
| 682 if (!advertisment_) { | |
| 683 OnStopLEListenError( | |
| 684 callback, | |
| 685 BluetoothAdvertisement::ErrorCode::ERROR_ADVERTISEMENT_DOES_NOT_EXIST); | |
| 686 return; | |
| 687 } | |
| 688 advertisment_->Unregister(base::Bind(&ArcBluetoothBridge::OnStopLEListenDone, | |
| 689 weak_factory_.GetWeakPtr(), callback), | |
| 690 base::Bind(&ArcBluetoothBridge::OnStopLEListenError, | |
| 691 weak_factory_.GetWeakPtr(), callback)); | |
| 692 } | |
| 693 | |
| 694 void ArcBluetoothBridge::GetGattDB(mojom::BluetoothAddressPtr remote_addr) { | |
| 695 if (!HasBluetoothInstance()) | |
| 696 return; | |
| 697 | |
| 698 BluetoothDevice* device = | |
| 699 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 700 mojo::Array<mojom::BluetoothGattDBElementPtr> db; | |
| 701 for (auto& service : device->GetGattServices()) { | |
|
rkc
2016/06/07 02:34:30
auto service
puthik_chromium
2016/06/08 23:59:51
Done.
| |
| 702 mojom::BluetoothGattDBElementPtr element = | |
| 703 mojom::BluetoothGattDBElement::New(); | |
| 704 | |
| 705 // Example: /org/bluez/hci0/dev_E0_CF_65_8C_86_1A/service001a | |
| 706 std::string id_str = service->GetIdentifier(); | |
| 707 | |
| 708 // Convert last digit of service id to int in base 16 | |
| 709 int id = std::stoi(id_str.substr(id_str.size() - 4), nullptr, 16); | |
| 710 | |
| 711 element->id = id; | |
| 712 element->uuid = mojom::BluetoothUUID::From(service->GetUUID()); | |
| 713 if (service->IsPrimary()) { | |
| 714 element->type = | |
| 715 mojom::BluetoothGattDBAttributeType::BTGATT_DB_PRIMARY_SERVICE; | |
| 716 } else { | |
| 717 element->type = | |
| 718 mojom::BluetoothGattDBAttributeType::BTGATT_DB_SECONDARY_SERVICE; | |
| 719 } | |
| 720 element->attribute_handle = id; | |
| 721 std::vector<BluetoothRemoteGattCharacteristic*> characteristics = | |
|
rkc
2016/06/07 02:34:30
const auto& characteristics = service->GetCharacte
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 722 service->GetCharacteristics(); | |
| 723 if (characteristics.size() > 0) { | |
| 724 std::string first_id_str = characteristics.front()->GetIdentifier(); | |
| 725 int first_id = | |
| 726 std::stoi(first_id_str.substr(first_id_str.size() - 4), nullptr, 16); | |
| 727 std::string last_id_str = characteristics.back()->GetIdentifier(); | |
| 728 int last_id = | |
| 729 std::stoi(last_id_str.substr(last_id_str.size() - 4), nullptr, 16); | |
| 730 element->start_handle = first_id; | |
| 731 element->end_handle = last_id; | |
| 732 } | |
| 733 db.push_back(std::move(element)); | |
| 734 | |
| 735 for (auto& characteristic : characteristics) { | |
|
rkc
2016/06/07 02:34:31
auto characteristic
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 736 mojom::BluetoothGattDBElementPtr element = | |
| 737 mojom::BluetoothGattDBElement::New(); | |
| 738 std::string id_str = characteristic->GetIdentifier(); | |
| 739 int id = std::stoi(id_str.substr(id_str.size() - 4), nullptr, 16); | |
| 740 element->id = id; | |
| 741 element->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID()); | |
| 742 element->type = | |
| 743 mojom::BluetoothGattDBAttributeType::BTGATT_DB_CHARACTERISTIC; | |
| 744 element->attribute_handle = id; | |
| 745 element->properties = characteristic->GetProperties(); | |
| 746 db.push_back(std::move(element)); | |
| 747 | |
| 748 for (auto& descriptor : characteristic->GetDescriptors()) { | |
|
rkc
2016/06/07 02:34:30
auto descriptor
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 749 mojom::BluetoothGattDBElementPtr element = | |
|
rkc
2016/06/07 02:34:30
Naming all of these element is a scoping error wai
puthik_chromium
2016/06/08 23:59:49
Done.
| |
| 750 mojom::BluetoothGattDBElement::New(); | |
| 751 std::string id_str = descriptor->GetIdentifier(); | |
| 752 int id = std::stoi(id_str.substr(id_str.size() - 4), nullptr, 16); | |
| 753 element->id = id; | |
| 754 element->uuid = mojom::BluetoothUUID::From(descriptor->GetUUID()); | |
| 755 element->type = | |
| 756 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR; | |
| 757 element->attribute_handle = id; | |
| 758 db.push_back(std::move(element)); | |
| 759 } | |
| 760 } | |
| 761 } | |
| 762 | |
| 763 arc_bridge_service()->bluetooth_instance()->OnGetGattDB( | |
| 764 std::move(remote_addr), std::move(db)); | |
| 765 } | |
| 766 | |
| 767 BluetoothRemoteGattCharacteristic* ArcBluetoothBridge::FindGattCharacteristic( | |
| 768 mojom::BluetoothAddressPtr remote_addr, | |
| 769 mojom::BluetoothGattServiceIDPtr service_id, | |
| 770 mojom::BluetoothGattIDPtr char_id) const { | |
| 771 DCHECK(remote_addr); | |
| 772 DCHECK(service_id); | |
| 773 DCHECK(char_id); | |
| 774 | |
| 775 BluetoothDevice* device = | |
| 776 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 777 if (!device) | |
| 778 return nullptr; | |
| 779 | |
| 780 BluetoothUUID serv_uuid = service_id->id->uuid.To<BluetoothUUID>(); | |
| 781 | |
| 782 BluetoothRemoteGattService* service = nullptr; | |
| 783 for (auto& s : device->GetGattServices()) { | |
|
rkc
2016/06/07 02:34:30
use std::find_if to find the service?
See https://
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 784 if (s->GetUUID() == serv_uuid) { | |
| 785 service = s; | |
| 786 break; | |
| 787 } | |
| 788 } | |
| 789 if (!service) | |
| 790 return nullptr; | |
| 791 | |
| 792 BluetoothUUID char_uuid = char_id->uuid.To<BluetoothUUID>(); | |
| 793 | |
| 794 for (auto& c : service->GetCharacteristics()) { | |
| 795 if (c->GetUUID() == char_uuid) { | |
| 796 return c; | |
| 797 } | |
| 798 } | |
| 799 return nullptr; | |
| 800 } | |
| 801 | |
| 802 BluetoothRemoteGattDescriptor* ArcBluetoothBridge::FindGattDescriptor( | |
| 803 mojom::BluetoothAddressPtr remote_addr, | |
| 804 mojom::BluetoothGattServiceIDPtr service_id, | |
| 805 mojom::BluetoothGattIDPtr char_id, | |
| 806 mojom::BluetoothGattIDPtr desc_id) const { | |
| 807 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | |
| 808 std::move(remote_addr), std::move(service_id), std::move(char_id)); | |
| 809 | |
| 810 if (!characteristic) | |
| 811 return nullptr; | |
| 812 | |
| 813 BluetoothUUID desc_uuid = desc_id->uuid.To<BluetoothUUID>(); | |
| 814 for (auto& d : characteristic->GetDescriptors()) { | |
| 815 if (d->GetUUID() == desc_uuid) { | |
| 816 return d; | |
| 817 } | |
| 818 } | |
| 819 return nullptr; | |
| 820 } | |
| 821 | |
| 822 // same callback for both ReadGattCharacteristic and ReadGattDescriptor | |
|
rkc
2016/06/07 02:34:29
s/same/Same
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 823 void ArcBluetoothBridge::OnGattReadDone( | |
| 824 const GattReadCallback& callback, | |
| 825 const std::vector<uint8_t>& result) const { | |
| 826 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New(); | |
| 827 gattValue->status = mojom::BluetoothGattStatus::GATT_SUCCESS; | |
| 828 gattValue->len = result.size(); | |
| 829 gattValue->type = 0; | |
| 830 gattValue->value = mojo::Array<uint8_t>::From(result); | |
| 831 callback.Run(std::move(gattValue)); | |
| 832 } | |
| 833 | |
| 834 void ArcBluetoothBridge::OnGattReadError( | |
| 835 const GattReadCallback& callback, | |
| 836 BluetoothGattService::GattErrorCode error_code) const { | |
| 837 mojom::BluetoothGattValuePtr gattValue = mojom::BluetoothGattValue::New(); | |
| 838 gattValue->status = mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code); | |
| 839 gattValue->len = 0; | |
| 840 gattValue->type = 0; | |
| 841 gattValue->value = nullptr; | |
| 842 | |
| 843 callback.Run(std::move(gattValue)); | |
| 844 } | |
| 845 | |
| 846 // same callback for both WriteGattCharacteristic and WriteGattDescriptor | |
| 847 void ArcBluetoothBridge::OnGattWriteDone( | |
| 848 const GattWriteCallback& callback) const { | |
| 849 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 850 } | |
| 851 | |
| 852 void ArcBluetoothBridge::OnGattWriteError( | |
| 853 const GattWriteCallback& callback, | |
| 854 BluetoothGattService::GattErrorCode error_code) const { | |
| 855 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code)); | |
| 856 } | |
| 857 | |
| 858 void ArcBluetoothBridge::ReadGattCharacteristic( | |
| 859 mojom::BluetoothAddressPtr remote_addr, | |
| 860 mojom::BluetoothGattServiceIDPtr service_id, | |
| 861 mojom::BluetoothGattIDPtr char_id, | |
| 862 const ReadGattCharacteristicCallback& callback) { | |
| 863 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | |
| 864 std::move(remote_addr), std::move(service_id), std::move(char_id)); | |
| 865 | |
| 866 DCHECK(characteristic); | |
| 867 | |
| 868 DCHECK(characteristic->GetPermissions() && | |
| 869 BluetoothGattCharacteristic::Permission::PERMISSION_READ); | |
|
rkc
2016/06/07 02:34:30
There are several types of read permissions. Are w
puthik_chromium
2016/06/08 23:59:50
Reading BlueZ code, look like it is guaranteed. Bu
| |
| 870 | |
| 871 characteristic->ReadRemoteCharacteristic( | |
| 872 base::Bind(&ArcBluetoothBridge::OnGattReadDone, | |
| 873 weak_factory_.GetWeakPtr(), callback), | |
| 874 base::Bind(&ArcBluetoothBridge::OnGattReadError, | |
| 875 weak_factory_.GetWeakPtr(), callback)); | |
| 876 } | |
| 877 | |
| 878 void ArcBluetoothBridge::WriteGattCharacteristic( | |
| 879 mojom::BluetoothAddressPtr remote_addr, | |
| 880 mojom::BluetoothGattServiceIDPtr service_id, | |
| 881 mojom::BluetoothGattIDPtr char_id, | |
| 882 mojom::BluetoothGattValuePtr value, | |
| 883 const WriteGattCharacteristicCallback& callback) { | |
| 884 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | |
| 885 std::move(remote_addr), std::move(service_id), std::move(char_id)); | |
| 886 | |
| 887 DCHECK(characteristic); | |
| 888 | |
| 889 DCHECK(characteristic->GetPermissions() && | |
| 890 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE); | |
|
rkc
2016/06/07 02:34:29
Same with write permissions.
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 891 | |
| 892 std::vector<uint8_t> new_value = value->value.To<std::vector<uint8_t>>(); | |
|
rkc
2016/06/07 02:34:31
don't need |new_value|.
Here and below.
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 893 | |
| 894 characteristic->WriteRemoteCharacteristic( | |
| 895 new_value, base::Bind(&ArcBluetoothBridge::OnGattWriteDone, | |
| 896 weak_factory_.GetWeakPtr(), callback), | |
| 897 base::Bind(&ArcBluetoothBridge::OnGattWriteError, | |
| 898 weak_factory_.GetWeakPtr(), callback)); | |
| 899 } | |
| 900 | |
| 901 void ArcBluetoothBridge::ReadGattDescriptor( | |
| 902 mojom::BluetoothAddressPtr remote_addr, | |
| 903 mojom::BluetoothGattServiceIDPtr service_id, | |
| 904 mojom::BluetoothGattIDPtr char_id, | |
| 905 mojom::BluetoothGattIDPtr desc_id, | |
| 906 const ReadGattDescriptorCallback& callback) { | |
| 907 BluetoothRemoteGattDescriptor* descriptor = | |
| 908 FindGattDescriptor(std::move(remote_addr), std::move(service_id), | |
| 909 std::move(char_id), std::move(desc_id)); | |
| 910 DCHECK(descriptor); | |
| 911 | |
| 912 DCHECK(descriptor->GetPermissions() && | |
| 913 BluetoothGattCharacteristic::Permission::PERMISSION_READ); | |
| 914 | |
| 915 descriptor->ReadRemoteDescriptor( | |
| 916 base::Bind(&ArcBluetoothBridge::OnGattReadDone, | |
| 917 weak_factory_.GetWeakPtr(), callback), | |
| 918 base::Bind(&ArcBluetoothBridge::OnGattReadError, | |
| 919 weak_factory_.GetWeakPtr(), callback)); | |
| 920 } | |
| 921 | |
| 922 void ArcBluetoothBridge::WriteGattDescriptor( | |
| 923 mojom::BluetoothAddressPtr remote_addr, | |
| 924 mojom::BluetoothGattServiceIDPtr service_id, | |
| 925 mojom::BluetoothGattIDPtr char_id, | |
| 926 mojom::BluetoothGattIDPtr desc_id, | |
| 927 mojom::BluetoothGattValuePtr value, | |
| 928 const WriteGattDescriptorCallback& callback) { | |
| 929 BluetoothRemoteGattDescriptor* descriptor = | |
| 930 FindGattDescriptor(std::move(remote_addr), std::move(service_id), | |
| 931 std::move(char_id), std::move(desc_id)); | |
| 932 DCHECK(descriptor); | |
| 933 | |
| 934 DCHECK(descriptor->GetPermissions() && | |
| 935 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE); | |
| 936 | |
| 937 std::vector<uint8_t> new_value = value->value.To<std::vector<uint8_t>>(); | |
| 938 | |
| 939 descriptor->WriteRemoteDescriptor( | |
| 940 new_value, base::Bind(&ArcBluetoothBridge::OnGattWriteDone, | |
| 941 weak_factory_.GetWeakPtr(), callback), | |
| 942 base::Bind(&ArcBluetoothBridge::OnGattWriteError, | |
| 943 weak_factory_.GetWeakPtr(), callback)); | |
| 944 } | |
| 945 | |
| 946 void ArcBluetoothBridge::OnGattNotifyStartDone( | |
| 947 const RegisterForGattNotificationCallback& callback, | |
| 948 const std::string char_string_id, | |
| 949 std::unique_ptr<BluetoothGattNotifySession> notify_session) { | |
| 950 notification_session_[char_string_id] = std::move(notify_session); | |
| 951 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 952 } | |
| 953 | |
| 954 void ArcBluetoothBridge::OnGattNotifyStartError( | |
| 955 const RegisterForGattNotificationCallback& callback, | |
| 956 BluetoothGattService::GattErrorCode error_code) const { | |
| 957 callback.Run(mojo::ConvertTo<mojom::BluetoothGattStatus>(error_code)); | |
| 958 } | |
| 959 | |
| 960 void ArcBluetoothBridge::OnGattNotifyStopDone( | |
| 961 const DeregisterForGattNotificationCallback& callback) const { | |
| 962 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 963 } | |
| 964 | |
| 965 void ArcBluetoothBridge::RegisterForGattNotification( | |
| 966 mojom::BluetoothAddressPtr remote_addr, | |
| 967 mojom::BluetoothGattServiceIDPtr service_id, | |
| 968 mojom::BluetoothGattIDPtr char_id, | |
| 969 const RegisterForGattNotificationCallback& callback) { | |
| 970 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | |
| 971 std::move(remote_addr), std::move(service_id), std::move(char_id)); | |
| 972 | |
| 973 DCHECK(characteristic); | |
|
rkc
2016/06/07 02:34:30
Why not log an error here and just return? Same wi
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 974 | |
| 975 if (characteristic->IsNotifying()) { | |
| 976 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 977 return; | |
| 978 } | |
| 979 | |
| 980 characteristic->StartNotifySession( | |
| 981 base::Bind(&ArcBluetoothBridge::OnGattNotifyStartDone, | |
| 982 weak_factory_.GetWeakPtr(), callback, | |
| 983 characteristic->GetIdentifier()), | |
| 984 base::Bind(&ArcBluetoothBridge::OnGattNotifyStartError, | |
| 985 weak_factory_.GetWeakPtr(), callback)); | |
| 986 } | |
| 987 | |
| 988 void ArcBluetoothBridge::DeregisterForGattNotification( | |
| 989 mojom::BluetoothAddressPtr remote_addr, | |
| 990 mojom::BluetoothGattServiceIDPtr service_id, | |
| 991 mojom::BluetoothGattIDPtr char_id, | |
| 992 const DeregisterForGattNotificationCallback& callback) { | |
| 993 BluetoothRemoteGattCharacteristic* characteristic = FindGattCharacteristic( | |
| 994 std::move(remote_addr), std::move(service_id), std::move(char_id)); | |
| 995 | |
| 996 DCHECK(characteristic); | |
| 997 | |
| 998 if (!characteristic->IsNotifying()) { | |
| 999 callback.Run(mojom::BluetoothGattStatus::GATT_SUCCESS); | |
| 1000 return; | |
| 1001 } | |
| 1002 | |
| 1003 std::string char_id_str = characteristic->GetIdentifier(); | |
| 1004 std::unique_ptr<BluetoothGattNotifySession> notify = | |
| 1005 std::move(notification_session_[char_id_str]); | |
| 1006 notification_session_.erase(char_id_str); | |
| 1007 notify->Stop(base::Bind(&ArcBluetoothBridge::OnGattNotifyStopDone, | |
| 1008 weak_factory_.GetWeakPtr(), callback)); | |
| 1009 } | |
| 1010 | |
| 1011 void ArcBluetoothBridge::ReadRemoteRssi( | |
| 1012 mojom::BluetoothAddressPtr remote_addr, | |
| 1013 const ReadRemoteRssiCallback& callback) { | |
| 1014 BluetoothDevice* device = | |
| 1015 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | |
| 1016 int rssi = device->GetInquiryRSSI(); | |
| 1017 callback.Run(rssi); | |
| 1018 } | |
| 1019 | |
| 464 void ArcBluetoothBridge::OnDiscoveryError() { | 1020 void ArcBluetoothBridge::OnDiscoveryError() { |
| 465 LOG(WARNING) << "failed to change discovery state"; | 1021 LOG(WARNING) << "failed to change discovery state"; |
| 466 } | 1022 } |
| 467 | 1023 |
| 468 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 1024 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
| 469 if (!HasBluetoothInstance()) | 1025 if (!HasBluetoothInstance()) |
| 470 return; | 1026 return; |
| 471 | 1027 |
| 472 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1028 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( |
| 473 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1029 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 536 } | 1092 } |
| 537 if (type == mojom::BluetoothPropertyType::ALL || | 1093 if (type == mojom::BluetoothPropertyType::ALL || |
| 538 type == mojom::BluetoothPropertyType::BDADDR) { | 1094 type == mojom::BluetoothPropertyType::BDADDR) { |
| 539 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); | 1095 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); |
| 540 btp->set_bdaddr(mojom::BluetoothAddress::From(device->GetAddress())); | 1096 btp->set_bdaddr(mojom::BluetoothAddress::From(device->GetAddress())); |
| 541 properties.push_back(std::move(btp)); | 1097 properties.push_back(std::move(btp)); |
| 542 } | 1098 } |
| 543 if (type == mojom::BluetoothPropertyType::ALL || | 1099 if (type == mojom::BluetoothPropertyType::ALL || |
| 544 type == mojom::BluetoothPropertyType::UUIDS) { | 1100 type == mojom::BluetoothPropertyType::UUIDS) { |
| 545 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); | 1101 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); |
| 546 std::vector<device::BluetoothUUID> uuids = device->GetUUIDs(); | 1102 std::vector<BluetoothUUID> uuids = device->GetUUIDs(); |
| 547 mojo::Array<mojom::BluetoothUUIDPtr> uuid_results = | 1103 mojo::Array<mojom::BluetoothUUIDPtr> uuid_results = |
| 548 mojo::Array<mojom::BluetoothUUIDPtr>::New(0); | 1104 mojo::Array<mojom::BluetoothUUIDPtr>::New(0); |
| 549 | 1105 |
| 550 for (size_t i = 0; i < uuids.size(); i++) { | 1106 for (auto& uuid : uuids) { |
| 551 uuid_results.push_back(mojom::BluetoothUUID::From(uuids[i])); | 1107 uuid_results.push_back(mojom::BluetoothUUID::From(uuid)); |
| 552 } | 1108 } |
| 553 | 1109 |
| 554 btp->set_uuids(std::move(uuid_results)); | 1110 btp->set_uuids(std::move(uuid_results)); |
| 555 properties.push_back(std::move(btp)); | 1111 properties.push_back(std::move(btp)); |
| 556 } | 1112 } |
| 557 if (type == mojom::BluetoothPropertyType::ALL || | 1113 if (type == mojom::BluetoothPropertyType::ALL || |
| 558 type == mojom::BluetoothPropertyType::CLASS_OF_DEVICE) { | 1114 type == mojom::BluetoothPropertyType::CLASS_OF_DEVICE) { |
| 559 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); | 1115 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); |
| 560 btp->set_device_class(device->GetBluetoothClass()); | 1116 btp->set_device_class(device->GetBluetoothClass()); |
| 561 properties.push_back(std::move(btp)); | 1117 properties.push_back(std::move(btp)); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 properties.push_back(std::move(btp)); | 1191 properties.push_back(std::move(btp)); |
| 636 } | 1192 } |
| 637 if (type == mojom::BluetoothPropertyType::ALL || | 1193 if (type == mojom::BluetoothPropertyType::ALL || |
| 638 type == mojom::BluetoothPropertyType::ADAPTER_BONDED_DEVICES) { | 1194 type == mojom::BluetoothPropertyType::ADAPTER_BONDED_DEVICES) { |
| 639 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); | 1195 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); |
| 640 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1196 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
| 641 | 1197 |
| 642 mojo::Array<mojom::BluetoothAddressPtr> bonded_devices = | 1198 mojo::Array<mojom::BluetoothAddressPtr> bonded_devices = |
| 643 mojo::Array<mojom::BluetoothAddressPtr>::New(0); | 1199 mojo::Array<mojom::BluetoothAddressPtr>::New(0); |
| 644 | 1200 |
| 645 for (size_t i = 0; i < devices.size(); i++) { | 1201 for (auto& device : devices) { |
|
rkc
2016/06/07 02:34:30
auto device
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 646 if (!devices[i]->IsPaired()) | 1202 if (device->IsPaired()) |
| 647 continue; | 1203 continue; |
| 648 | 1204 |
| 649 mojom::BluetoothAddressPtr addr = | 1205 mojom::BluetoothAddressPtr addr = |
| 650 mojom::BluetoothAddress::From(devices[i]->GetAddress()); | 1206 mojom::BluetoothAddress::From(device->GetAddress()); |
| 651 bonded_devices.push_back(std::move(addr)); | 1207 bonded_devices.push_back(std::move(addr)); |
| 652 } | 1208 } |
| 653 | 1209 |
| 654 btp->set_bonded_devices(std::move(bonded_devices)); | 1210 btp->set_bonded_devices(std::move(bonded_devices)); |
| 655 properties.push_back(std::move(btp)); | 1211 properties.push_back(std::move(btp)); |
| 656 } | 1212 } |
| 657 if (type == mojom::BluetoothPropertyType::ALL || | 1213 if (type == mojom::BluetoothPropertyType::ALL || |
| 658 type == mojom::BluetoothPropertyType::ADAPTER_DISCOVERY_TIMEOUT) { | 1214 type == mojom::BluetoothPropertyType::ADAPTER_DISCOVERY_TIMEOUT) { |
| 659 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); | 1215 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); |
| 660 btp->set_discovery_timeout(120); | 1216 btp->set_discovery_timeout(120); |
| 661 properties.push_back(std::move(btp)); | 1217 properties.push_back(std::move(btp)); |
| 662 } | 1218 } |
| 663 | 1219 |
| 664 return properties; | 1220 return properties; |
| 665 } | 1221 } |
| 666 | 1222 |
| 1223 // Android support 5 types of Advertising Data. | |
| 1224 // However Chrome didn't expose AdvertiseFlag and ManufactureData. | |
|
rkc
2016/06/07 02:34:30
Please file a bug and add a TODO pointing to the b
puthik_chromium
2016/06/08 23:59:49
Done. http://crbug.com/618442
| |
| 1225 // So we will only expose local_name, service_uuids and service_data. | |
| 1226 mojo::Array<mojom::BluetoothAdvertisingDataPtr> | |
| 1227 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { | |
| 1228 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; | |
| 1229 | |
| 1230 // LocalName | |
| 1231 mojom::BluetoothAdvertisingDataPtr local_name = | |
| 1232 mojom::BluetoothAdvertisingData::New(); | |
| 1233 local_name->set_local_name(base::UTF16ToUTF8(device->GetNameForDisplay())); | |
| 1234 advertising_data.push_back(std::move(local_name)); | |
| 1235 | |
| 1236 // ServiceUuid | |
| 1237 BluetoothDevice::UUIDList uuid_list = device->GetServiceDataUUIDs(); | |
| 1238 if (uuid_list.size() > 0) { | |
| 1239 mojom::BluetoothAdvertisingDataPtr service_uuids = | |
| 1240 mojom::BluetoothAdvertisingData::New(); | |
| 1241 service_uuids->set_service_uuids( | |
| 1242 mojo::Array<mojom::BluetoothUUIDPtr>::From(uuid_list)); | |
| 1243 advertising_data.push_back(std::move(service_uuids)); | |
| 1244 } | |
| 1245 | |
| 1246 // Service data | |
| 1247 for (auto& uuid : uuid_list) { | |
| 1248 base::BinaryValue* data = device->GetServiceData(uuid); | |
| 1249 if (data->GetSize() == 0) | |
| 1250 continue; | |
| 1251 std::string data_str; | |
| 1252 if (!data->GetAsString(&data_str)) | |
| 1253 continue; | |
| 1254 | |
| 1255 mojom::BluetoothAdvertisingDataPtr service_data_element = | |
| 1256 mojom::BluetoothAdvertisingData::New(); | |
| 1257 mojom::BluetoothServiceDataPtr service_data = | |
| 1258 mojom::BluetoothServiceData::New(); | |
| 1259 | |
| 1260 std::string uuid_str = uuid.canonical_value(); | |
| 1261 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy | |
| 1262 service_data->uuid_16bit = std::stoi(uuid_str.substr(4, 4), nullptr, 16); | |
| 1263 for (auto& c : data_str) { | |
| 1264 service_data->data.push_back(c); | |
| 1265 } | |
| 1266 service_data_element->set_service_data(std::move(service_data)); | |
| 1267 advertising_data.push_back(std::move(service_data_element)); | |
| 1268 } | |
| 1269 | |
| 1270 return advertising_data; | |
| 1271 } | |
| 1272 | |
| 667 void ArcBluetoothBridge::SendCachedDevicesFound() const { | 1273 void ArcBluetoothBridge::SendCachedDevicesFound() const { |
| 668 // Send devices that have already been discovered, but aren't connected. | 1274 // Send devices that have already been discovered, but aren't connected. |
| 669 if (!HasBluetoothInstance()) | 1275 if (!HasBluetoothInstance()) |
| 670 return; | 1276 return; |
| 671 | 1277 |
| 672 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1278 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
| 673 for (size_t i = 0; i < devices.size(); i++) { | 1279 for (auto& device : devices) { |
|
rkc
2016/06/07 02:34:29
auto device
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 674 if (devices[i]->IsPaired()) | 1280 if (device->IsPaired()) |
| 675 continue; | 1281 continue; |
| 676 | 1282 |
| 677 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1283 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 678 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, devices[i]); | 1284 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 679 | 1285 |
| 680 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1286 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 681 std::move(properties)); | 1287 std::move(properties)); |
| 1288 | |
| 1289 if (arc_bridge_service()->bluetooth_version() >= 1) { | |
| 1290 mojom::BluetoothAddressPtr addr = | |
| 1291 mojom::BluetoothAddress::From(device->GetAddress()); | |
| 1292 int rssi = device->GetInquiryRSSI(); | |
| 1293 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | |
| 1294 GetAdvertisingData(device); | |
| 1295 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | |
| 1296 std::move(addr), rssi, std::move(adv_data)); | |
| 1297 } | |
| 682 } | 1298 } |
| 683 } | 1299 } |
| 684 | 1300 |
| 685 bool ArcBluetoothBridge::HasBluetoothInstance() const { | 1301 bool ArcBluetoothBridge::HasBluetoothInstance() const { |
| 686 if (!arc_bridge_service()->bluetooth_instance()) { | 1302 if (!arc_bridge_service()->bluetooth_instance()) { |
| 687 LOG(WARNING) << "no Bluetooth instance available"; | 1303 LOG(WARNING) << "no Bluetooth instance available"; |
| 688 return false; | 1304 return false; |
| 689 } | 1305 } |
| 690 | 1306 |
| 691 return true; | 1307 return true; |
| 692 } | 1308 } |
| 693 | 1309 |
| 694 void ArcBluetoothBridge::SendCachedPairedDevices() const { | 1310 void ArcBluetoothBridge::SendCachedPairedDevices() const { |
| 695 DCHECK(bluetooth_adapter_); | 1311 DCHECK(bluetooth_adapter_); |
| 1312 if (!HasBluetoothInstance()) | |
| 1313 return; | |
| 696 | 1314 |
| 697 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1315 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
| 698 for (BluetoothDevice* device : devices) { | 1316 for (auto& device : devices) { |
|
rkc
2016/06/07 02:34:31
auto device
puthik_chromium
2016/06/08 23:59:50
Done.
| |
| 699 if (!device->IsPaired()) | 1317 if (!device->IsPaired()) |
| 700 continue; | 1318 continue; |
| 701 | 1319 |
| 702 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1320 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
| 703 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1321 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
| 704 | 1322 |
| 705 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1323 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( |
| 706 std::move(properties)); | 1324 std::move(properties)); |
| 707 | 1325 |
| 708 mojom::BluetoothAddressPtr addr = | 1326 mojom::BluetoothAddressPtr addr = |
| 709 mojom::BluetoothAddress::From(device->GetAddress()); | 1327 mojom::BluetoothAddress::From(device->GetAddress()); |
| 710 | 1328 |
| 1329 if (arc_bridge_service()->bluetooth_version() >= 1) { | |
| 1330 int rssi = device->GetInquiryRSSI(); | |
| 1331 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | |
| 1332 GetAdvertisingData(device); | |
| 1333 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | |
| 1334 addr->Clone(), rssi, std::move(adv_data)); | |
| 1335 } | |
| 1336 | |
| 711 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING | 1337 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING |
| 712 // to | 1338 // to |
| 713 // make sure the bond state machine on Android is ready to take the | 1339 // make sure the bond state machine on Android is ready to take the |
| 714 // pair-done event. Otherwise the pair-done event will be dropped as an | 1340 // pair-done event. Otherwise the pair-done event will be dropped as an |
| 715 // invalid change of paired status. | 1341 // invalid change of paired status. |
| 716 OnPairing(addr->Clone()); | 1342 OnPairing(addr->Clone()); |
| 717 OnPairedDone(std::move(addr)); | 1343 OnPairedDone(std::move(addr)); |
| 718 } | 1344 } |
| 719 } | 1345 } |
| 720 | 1346 |
| 721 } // namespace arc | 1347 } // namespace arc |
| OLD | NEW |