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

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2391973003: arc: bluetooth: Send update to Android when got DeviceChanged event (Closed)
Patch Set: Rebase / SendDeviceData -> SendDeviceInformation Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 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 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 // ready, we should register it now. 304 // ready, we should register it now.
305 if (bluetooth_adapter_ && !bluetooth_adapter_->HasObserver(this)) 305 if (bluetooth_adapter_ && !bluetooth_adapter_->HasObserver(this))
306 bluetooth_adapter_->AddObserver(this); 306 bluetooth_adapter_->AddObserver(this);
307 } 307 }
308 308
309 void ArcBluetoothBridge::OnInstanceClosed() { 309 void ArcBluetoothBridge::OnInstanceClosed() {
310 if (bluetooth_adapter_) 310 if (bluetooth_adapter_)
311 bluetooth_adapter_->RemoveObserver(this); 311 bluetooth_adapter_->RemoveObserver(this);
312 } 312 }
313 313
314 void ArcBluetoothBridge::DeviceAdded(BluetoothAdapter* adapter, 314 void ArcBluetoothBridge::SendDeviceInformation(
315 BluetoothDevice* device) { 315 const BluetoothDevice* device) const {
316 auto* bluetooth_instance = 316 auto* bluetooth_instance =
317 arc_bridge_service()->bluetooth()->GetInstanceForMethod("OnDeviceFound"); 317 arc_bridge_service()->bluetooth()->GetInstanceForMethod("OnDeviceFound");
318 if (!bluetooth_instance) 318 if (!bluetooth_instance)
319 return; 319 return;
320 320
321 mojo::Array<mojom::BluetoothPropertyPtr> properties = 321 mojo::Array<mojom::BluetoothPropertyPtr> properties =
322 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); 322 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device);
323 323
324 bluetooth_instance->OnDeviceFound(std::move(properties)); 324 bluetooth_instance->OnDeviceFound(std::move(properties));
325 325
326 auto* btle_instance = arc_bridge_service()->bluetooth()->GetInstanceForMethod(
327 "OnLEDeviceFound", kMinBtleVersion);
328 if (!btle_instance)
329 return;
330 326
331 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) 327 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE))
332 return; 328 return;
333 329
334 mojom::BluetoothAddressPtr addr =
335 mojom::BluetoothAddress::From(device->GetAddress());
336 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); 330 base::Optional<int8_t> rssi = device->GetInquiryRSSI();
Rahul Chaturvedi 2016/10/06 19:18:43 Optional nit: SendDevice seems more accurate, sinc
puthik_chromium 2016/10/06 19:36:51 Done.
337 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = 331 mojom::BluetoothAddressPtr addr;
338 GetAdvertisingData(device); 332
339 btle_instance->OnLEDeviceFound(std::move(addr), 333 // We only want to send updated advertise data to Android only when we are
340 rssi.value_or(mojom::kUnknownPower), 334 // scanning which is checked by the validity of rssi. Here are the 2 cases
341 std::move(adv_data)); 335 // that we don't want to send updated advertise data to Android.
336 // 1) Cached found device and 2) rssi became invalid when we stop scanning.
337 if (rssi.has_value()) {
338 auto* btle_instance =
339 arc_bridge_service()->bluetooth()->GetInstanceForMethod(
340 "OnLEDeviceFound", kMinBtleVersion);
341 if (!btle_instance)
342 return;
343 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data =
344 GetAdvertisingData(device);
345 addr = mojom::BluetoothAddress::From(device->GetAddress());
346 btle_instance->OnLEDeviceFound(std::move(addr), rssi.value(),
347 std::move(adv_data));
348 }
342 349
343 if (!device->IsConnected()) 350 if (!device->IsConnected())
344 return; 351 return;
345 352
346 addr = mojom::BluetoothAddress::From(device->GetAddress()); 353 addr = mojom::BluetoothAddress::From(device->GetAddress());
347 OnGattConnectStateChanged(std::move(addr), true); 354 OnGattConnectStateChanged(std::move(addr), true);
348 } 355 }
349 356
357 void ArcBluetoothBridge::DeviceAdded(BluetoothAdapter* adapter,
358 BluetoothDevice* device) {
359 SendDeviceInformation(device);
360 }
361
350 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, 362 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter,
351 BluetoothDevice* device) { 363 BluetoothDevice* device) {
364 SendDeviceInformation(device);
365
352 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE)) 366 if (!(device->GetType() & device::BLUETOOTH_TRANSPORT_LE))
353 return; 367 return;
354 368
355 auto it = gatt_connection_cache_.find(device->GetAddress()); 369 auto it = gatt_connection_cache_.find(device->GetAddress());
356 bool was_connected = it != gatt_connection_cache_.end(); 370 bool was_connected = it != gatt_connection_cache_.end();
357 bool is_connected = device->IsConnected(); 371 bool is_connected = device->IsConnected();
358 372
359 if (is_connected == was_connected) 373 if (is_connected == was_connected)
360 return; 374 return;
361 375
(...skipping 1335 matching lines...) Expand 10 before | Expand all | Expand 10 after
1697 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; 1711 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE;
1698 if (device && device->IsPaired()) { 1712 if (device && device->IsPaired()) {
1699 bond_state = mojom::BluetoothBondState::BONDED; 1713 bond_state = mojom::BluetoothBondState::BONDED;
1700 } 1714 }
1701 bluetooth_instance->OnBondStateChanged(mojom::BluetoothStatus::FAIL, 1715 bluetooth_instance->OnBondStateChanged(mojom::BluetoothStatus::FAIL,
1702 std::move(addr), bond_state); 1716 std::move(addr), bond_state);
1703 } 1717 }
1704 1718
1705 mojo::Array<mojom::BluetoothPropertyPtr> 1719 mojo::Array<mojom::BluetoothPropertyPtr>
1706 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, 1720 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type,
1707 BluetoothDevice* device) const { 1721 const BluetoothDevice* device) const {
1708 mojo::Array<mojom::BluetoothPropertyPtr> properties; 1722 mojo::Array<mojom::BluetoothPropertyPtr> properties;
1709 1723
1710 if (!device) { 1724 if (!device) {
1711 return properties; 1725 return properties;
1712 } 1726 }
1713 1727
1714 if (type == mojom::BluetoothPropertyType::ALL || 1728 if (type == mojom::BluetoothPropertyType::ALL ||
1715 type == mojom::BluetoothPropertyType::BDNAME) { 1729 type == mojom::BluetoothPropertyType::BDNAME) {
1716 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); 1730 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1717 btp->set_bdname(device->GetName() ? device->GetName().value() : nullptr); 1731 btp->set_bdname(device->GetName() ? device->GetName().value() : nullptr);
1718 properties.push_back(std::move(btp)); 1732 properties.push_back(std::move(btp));
1719 } 1733 }
1720 if (type == mojom::BluetoothPropertyType::ALL || 1734 if (type == mojom::BluetoothPropertyType::ALL ||
1721 type == mojom::BluetoothPropertyType::BDADDR) { 1735 type == mojom::BluetoothPropertyType::BDADDR) {
1722 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); 1736 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1723 btp->set_bdaddr(mojom::BluetoothAddress::From(device->GetAddress())); 1737 btp->set_bdaddr(mojom::BluetoothAddress::From(device->GetAddress()));
1724 properties.push_back(std::move(btp)); 1738 properties.push_back(std::move(btp));
1725 } 1739 }
1726 if (type == mojom::BluetoothPropertyType::ALL || 1740 if (type == mojom::BluetoothPropertyType::ALL ||
1727 type == mojom::BluetoothPropertyType::UUIDS) { 1741 type == mojom::BluetoothPropertyType::UUIDS) {
1728 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1729 BluetoothDevice::UUIDSet uuids = device->GetUUIDs(); 1742 BluetoothDevice::UUIDSet uuids = device->GetUUIDs();
1730 btp->set_uuids(std::vector<BluetoothUUID>(uuids.begin(), uuids.end())); 1743 if (uuids.size() > 0) {
1731 properties.push_back(std::move(btp)); 1744 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1745 btp->set_uuids(std::vector<BluetoothUUID>(uuids.begin(), uuids.end()));
1746 properties.push_back(std::move(btp));
1747 }
1732 } 1748 }
1733 if (type == mojom::BluetoothPropertyType::ALL || 1749 if (type == mojom::BluetoothPropertyType::ALL ||
1734 type == mojom::BluetoothPropertyType::CLASS_OF_DEVICE) { 1750 type == mojom::BluetoothPropertyType::CLASS_OF_DEVICE) {
1735 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); 1751 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1736 btp->set_device_class(device->GetBluetoothClass()); 1752 btp->set_device_class(device->GetBluetoothClass());
1737 properties.push_back(std::move(btp)); 1753 properties.push_back(std::move(btp));
1738 } 1754 }
1739 if (type == mojom::BluetoothPropertyType::ALL || 1755 if (type == mojom::BluetoothPropertyType::ALL ||
1740 type == mojom::BluetoothPropertyType::TYPE_OF_DEVICE) { 1756 type == mojom::BluetoothPropertyType::TYPE_OF_DEVICE) {
1741 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); 1757 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1742 btp->set_device_type(device->GetType()); 1758 btp->set_device_type(device->GetType());
1743 properties.push_back(std::move(btp)); 1759 properties.push_back(std::move(btp));
1744 } 1760 }
1745 if (type == mojom::BluetoothPropertyType::ALL || 1761 if (type == mojom::BluetoothPropertyType::ALL ||
1746 type == mojom::BluetoothPropertyType::REMOTE_FRIENDLY_NAME) { 1762 type == mojom::BluetoothPropertyType::REMOTE_FRIENDLY_NAME) {
1747 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New(); 1763 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1748 btp->set_remote_friendly_name( 1764 btp->set_remote_friendly_name(
1749 mojo::String::From(base::UTF16ToUTF8(device->GetNameForDisplay()))); 1765 mojo::String::From(base::UTF16ToUTF8(device->GetNameForDisplay())));
1750 properties.push_back(std::move(btp)); 1766 properties.push_back(std::move(btp));
1751 } 1767 }
1752 if (type == mojom::BluetoothPropertyType::ALL || 1768 if (type == mojom::BluetoothPropertyType::ALL ||
1753 type == mojom::BluetoothPropertyType::REMOTE_RSSI) { 1769 type == mojom::BluetoothPropertyType::REMOTE_RSSI) {
1754 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1755 base::Optional<int8_t> rssi = device->GetInquiryRSSI(); 1770 base::Optional<int8_t> rssi = device->GetInquiryRSSI();
1756 btp->set_remote_rssi(rssi.value_or(mojom::kUnknownPower)); 1771 if (rssi.has_value()) {
1757 properties.push_back(std::move(btp)); 1772 mojom::BluetoothPropertyPtr btp = mojom::BluetoothProperty::New();
1773 btp->set_remote_rssi(rssi.value());
1774 properties.push_back(std::move(btp));
1775 }
1758 } 1776 }
1759 // TODO(smbarber): Add remote version info 1777 // TODO(smbarber): Add remote version info
1760 1778
1761 return properties; 1779 return properties;
1762 } 1780 }
1763 1781
1764 mojo::Array<mojom::BluetoothPropertyPtr> 1782 mojo::Array<mojom::BluetoothPropertyPtr>
1765 ArcBluetoothBridge::GetAdapterProperties( 1783 ArcBluetoothBridge::GetAdapterProperties(
1766 mojom::BluetoothPropertyType type) const { 1784 mojom::BluetoothPropertyType type) const {
1767 mojo::Array<mojom::BluetoothPropertyPtr> properties; 1785 mojo::Array<mojom::BluetoothPropertyPtr> properties;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1861 return properties; 1879 return properties;
1862 } 1880 }
1863 1881
1864 // Android support 5 types of Advertising Data. 1882 // Android support 5 types of Advertising Data.
1865 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. 1883 // However Chrome didn't expose AdvertiseFlag and ManufacturerData.
1866 // So we will only expose local_name, service_uuids and service_data. 1884 // So we will only expose local_name, service_uuids and service_data.
1867 // Note that we need to use UUID 16 bits because Android does not support 1885 // Note that we need to use UUID 16 bits because Android does not support
1868 // UUID 128 bits. 1886 // UUID 128 bits.
1869 // TODO(crbug.com/618442) Make Chrome expose missing data. 1887 // TODO(crbug.com/618442) Make Chrome expose missing data.
1870 mojo::Array<mojom::BluetoothAdvertisingDataPtr> 1888 mojo::Array<mojom::BluetoothAdvertisingDataPtr>
1871 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { 1889 ArcBluetoothBridge::GetAdvertisingData(const BluetoothDevice* device) const {
1872 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; 1890 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data;
1873 1891
1874 // LocalName 1892 // LocalName
1875 mojom::BluetoothAdvertisingDataPtr local_name = 1893 mojom::BluetoothAdvertisingDataPtr local_name =
1876 mojom::BluetoothAdvertisingData::New(); 1894 mojom::BluetoothAdvertisingData::New();
1877 local_name->set_local_name(device->GetName() ? device->GetName().value() 1895 local_name->set_local_name(device->GetName() ? device->GetName().value()
1878 : nullptr); 1896 : nullptr);
1879 advertising_data.push_back(std::move(local_name)); 1897 advertising_data.push_back(std::move(local_name));
1880 1898
1881 // ServiceUuid 1899 // ServiceUuid
(...skipping 27 matching lines...) Expand all
1909 service_data->data.Swap(&data_copy); 1927 service_data->data.Swap(&data_copy);
1910 1928
1911 service_data_element->set_service_data(std::move(service_data)); 1929 service_data_element->set_service_data(std::move(service_data));
1912 advertising_data.push_back(std::move(service_data_element)); 1930 advertising_data.push_back(std::move(service_data_element));
1913 } 1931 }
1914 1932
1915 return advertising_data; 1933 return advertising_data;
1916 } 1934 }
1917 1935
1918 void ArcBluetoothBridge::SendCachedDevicesFound() const { 1936 void ArcBluetoothBridge::SendCachedDevicesFound() const {
1937 DCHECK(bluetooth_adapter_);
1938
1919 // Send devices that have already been discovered, but aren't connected. 1939 // Send devices that have already been discovered, but aren't connected.
1920 auto* bluetooth_instance =
1921 arc_bridge_service()->bluetooth()->GetInstanceForMethod("OnDeviceFound");
1922 if (!bluetooth_instance)
1923 return;
1924 auto* btle_instance = arc_bridge_service()->bluetooth()->GetInstanceForMethod(
1925 "OnLEDeviceFound", kMinBtleVersion);
1926
1927 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); 1940 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices();
1928 for (auto* device : devices) { 1941 for (auto* device : devices) {
1929 if (device->IsPaired()) 1942 if (device->IsPaired())
1930 continue; 1943 continue;
1931 1944
1932 mojo::Array<mojom::BluetoothPropertyPtr> properties = 1945 SendDeviceInformation(device);
1933 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device);
1934
1935 bluetooth_instance->OnDeviceFound(std::move(properties));
1936
1937 if (btle_instance) {
1938 mojom::BluetoothAddressPtr addr =
1939 mojom::BluetoothAddress::From(device->GetAddress());
1940 base::Optional<int8_t> rssi = device->GetInquiryRSSI();
1941 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data =
1942 GetAdvertisingData(device);
1943 btle_instance->OnLEDeviceFound(std::move(addr),
1944 rssi.value_or(mojom::kUnknownPower),
1945 std::move(adv_data));
1946 }
1947 } 1946 }
1948 } 1947 }
1949 1948
1950 void ArcBluetoothBridge::SendCachedPairedDevices() const { 1949 void ArcBluetoothBridge::SendCachedPairedDevices() const {
1951 DCHECK(bluetooth_adapter_); 1950 DCHECK(bluetooth_adapter_);
1952 auto* bluetooth_instance =
1953 arc_bridge_service()->bluetooth()->GetInstanceForMethod("OnDeviceFound");
1954 if (!bluetooth_instance)
1955 return;
1956 auto* btle_instance = arc_bridge_service()->bluetooth()->GetInstanceForMethod(
1957 "OnLEDeviceFound", kMinBtleVersion);
1958 1951
1959 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); 1952 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices();
1960 for (auto* device : devices) { 1953 for (auto* device : devices) {
1961 if (!device->IsPaired()) 1954 if (!device->IsPaired())
1962 continue; 1955 continue;
1963 1956
1964 mojo::Array<mojom::BluetoothPropertyPtr> properties = 1957 SendDeviceInformation(device);
1965 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device);
1966
1967 bluetooth_instance->OnDeviceFound(std::move(properties));
1968
1969 mojom::BluetoothAddressPtr addr =
1970 mojom::BluetoothAddress::From(device->GetAddress());
1971
1972 if (btle_instance) {
1973 base::Optional<int8_t> rssi = device->GetInquiryRSSI();
1974 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data =
1975 GetAdvertisingData(device);
1976 btle_instance->OnLEDeviceFound(addr->Clone(),
1977 rssi.value_or(mojom::kUnknownPower),
1978 std::move(adv_data));
1979 }
1980 1958
1981 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING 1959 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING
1982 // to make sure the bond state machine on Android is ready to take the 1960 // to make sure the bond state machine on Android is ready to take the
1983 // pair-done event. Otherwise the pair-done event will be dropped as an 1961 // pair-done event. Otherwise the pair-done event will be dropped as an
1984 // invalid change of paired status. 1962 // invalid change of paired status.
1963 mojom::BluetoothAddressPtr addr =
1964 mojom::BluetoothAddress::From(device->GetAddress());
1985 OnPairing(addr->Clone()); 1965 OnPairing(addr->Clone());
1986 OnPairedDone(std::move(addr)); 1966 OnPairedDone(std::move(addr));
1987 } 1967 }
1988 } 1968 }
1989 1969
1990 void ArcBluetoothBridge::OnGetServiceRecordsDone( 1970 void ArcBluetoothBridge::OnGetServiceRecordsDone(
1991 mojom::BluetoothAddressPtr remote_addr, 1971 mojom::BluetoothAddressPtr remote_addr,
1992 const BluetoothUUID& target_uuid, 1972 const BluetoothUUID& target_uuid,
1993 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez) { 1973 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez) {
1994 auto* sdp_bluetooth_instance = 1974 auto* sdp_bluetooth_instance =
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2030 sdp_bluetooth_instance->OnGetSdpRecords( 2010 sdp_bluetooth_instance->OnGetSdpRecords(
2031 status, std::move(remote_addr), target_uuid, 2011 status, std::move(remote_addr), target_uuid,
2032 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); 2012 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0));
2033 } 2013 }
2034 2014
2035 bool ArcBluetoothBridge::CalledOnValidThread() { 2015 bool ArcBluetoothBridge::CalledOnValidThread() {
2036 return thread_checker_.CalledOnValidThread(); 2016 return thread_checker_.CalledOnValidThread();
2037 } 2017 }
2038 2018
2039 } // namespace arc 2019 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | components/arc/bluetooth/arc_bluetooth_bridge_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698