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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 if (status == arc::mojom::BluetoothGattStatus::GATT_SUCCESS) | 187 if (status == arc::mojom::BluetoothGattStatus::GATT_SUCCESS) |
188 success_callback.Run(); | 188 success_callback.Run(); |
189 else | 189 else |
190 error_callback.Run(); | 190 error_callback.Run(); |
191 } | 191 } |
192 | 192 |
193 bool IsGattOffsetValid(int offset) { | 193 bool IsGattOffsetValid(int offset) { |
194 return 0 <= offset && offset < kMaxGattAttributeLength; | 194 return 0 <= offset && offset < kMaxGattAttributeLength; |
195 } | 195 } |
196 | 196 |
197 // This is needed because Android only support UUID 16 bits in advertising data. | 197 bool isUuid16(const BluetoothUUID& uuid) { |
Rahul Chaturvedi
2016/09/29 22:10:47
Can't we get this from uuid.Format()?
puthik_chromium
2016/09/29 22:39:25
uuid.Format() return the size of uuid string given
| |
198 // Convert to uuid16 and compare with original uuid. | |
199 return uuid == BluetoothUUID(uuid.canonical_value().substr(4, 4)); | |
200 } | |
201 | |
202 // This is needed because Android only support UUID 16 bits in service data | |
203 // section in advertising data | |
198 uint16_t GetUUID16(const BluetoothUUID& uuid) { | 204 uint16_t GetUUID16(const BluetoothUUID& uuid) { |
199 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy | 205 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy |
200 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); | 206 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); |
201 } | 207 } |
202 | 208 |
203 arc::mojom::BluetoothPropertyPtr GetDiscoveryTimeoutProperty(uint32_t timeout) { | 209 arc::mojom::BluetoothPropertyPtr GetDiscoveryTimeoutProperty(uint32_t timeout) { |
204 arc::mojom::BluetoothPropertyPtr property = | 210 arc::mojom::BluetoothPropertyPtr property = |
205 arc::mojom::BluetoothProperty::New(); | 211 arc::mojom::BluetoothProperty::New(); |
206 property->set_discovery_timeout(timeout); | 212 property->set_discovery_timeout(timeout); |
207 return property; | 213 return property; |
(...skipping 1649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1857 btp->set_local_le_features(std::move(le_features)); | 1863 btp->set_local_le_features(std::move(le_features)); |
1858 properties.push_back(std::move(btp)); | 1864 properties.push_back(std::move(btp)); |
1859 } | 1865 } |
1860 | 1866 |
1861 return properties; | 1867 return properties; |
1862 } | 1868 } |
1863 | 1869 |
1864 // Android support 5 types of Advertising Data. | 1870 // Android support 5 types of Advertising Data. |
1865 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. | 1871 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. |
1866 // So we will only expose local_name, service_uuids and service_data. | 1872 // 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 | 1873 // Note that we need to use UUID 16 bits in service_data section |
1868 // UUID 128 bits. | 1874 // because Android does not support UUID 128 bits there. |
1869 // TODO(crbug.com/618442) Make Chrome expose missing data. | 1875 // TODO(crbug.com/618442) Make Chrome expose missing data. |
1870 mojo::Array<mojom::BluetoothAdvertisingDataPtr> | 1876 mojo::Array<mojom::BluetoothAdvertisingDataPtr> |
1871 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { | 1877 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { |
1872 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; | 1878 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; |
1873 | 1879 |
1874 // LocalName | 1880 // LocalName |
1875 mojom::BluetoothAdvertisingDataPtr local_name = | 1881 mojom::BluetoothAdvertisingDataPtr local_name = |
1876 mojom::BluetoothAdvertisingData::New(); | 1882 mojom::BluetoothAdvertisingData::New(); |
1877 local_name->set_local_name(device->GetName() ? device->GetName().value() | 1883 local_name->set_local_name(device->GetName() ? device->GetName().value() |
1878 : nullptr); | 1884 : nullptr); |
1879 advertising_data.push_back(std::move(local_name)); | 1885 advertising_data.push_back(std::move(local_name)); |
1880 | 1886 |
1881 // ServiceUuid | 1887 // ServiceUuid |
1882 BluetoothDevice::UUIDSet uuid_set = device->GetUUIDs(); | 1888 const BluetoothDevice::UUIDSet& uuid_set = device->GetUUIDs(); |
1883 if (uuid_set.size() > 0) { | 1889 if (uuid_set.size() > 0) { |
1884 mojom::BluetoothAdvertisingDataPtr service_uuids_16 = | 1890 mojom::BluetoothAdvertisingDataPtr service_uuids = |
1885 mojom::BluetoothAdvertisingData::New(); | 1891 mojom::BluetoothAdvertisingData::New(); |
1886 mojo::Array<uint16_t> uuid16s(uuid_set.size()); | 1892 if (std::all_of(uuid_set.begin(), uuid_set.end(), isUuid16)) { |
1887 size_t i = 0; | 1893 mojo::Array<uint16_t> uuid16s; |
1888 for (const auto& uuid : uuid_set) { | 1894 uuid16s.reserve(uuid_set.size()); |
1889 uuid16s[i] = GetUUID16(uuid); | 1895 for (const auto& uuid : uuid_set) |
1890 i++; | 1896 uuid16s.push_back(GetUUID16(uuid)); |
1897 service_uuids->set_service_uuids_16(std::move(uuid16s)); | |
Rahul Chaturvedi
2016/09/29 22:10:47
Why do 16 bit UUIDs need to be handled differently
puthik_chromium
2016/09/29 22:39:25
Why do 16 bit UUIDs need to be handled differently
Rahul Chaturvedi
2016/09/29 22:53:39
Android's implementation and APIs can change (in f
| |
1898 } else { | |
1899 mojo::Array<BluetoothUUID> uuids; | |
1900 uuids.reserve(uuid_set.size()); | |
1901 for (const auto& uuid : uuid_set) | |
1902 uuids.push_back(uuid); | |
1903 service_uuids->set_service_uuids(std::move(uuids)); | |
1891 } | 1904 } |
1892 service_uuids_16->set_service_uuids_16(std::move(uuid16s)); | 1905 advertising_data.push_back(std::move(service_uuids)); |
1893 advertising_data.push_back(std::move(service_uuids_16)); | |
1894 } | 1906 } |
1895 | 1907 |
1896 // Service data | 1908 // Service data |
1897 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { | 1909 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { |
1898 mojom::BluetoothAdvertisingDataPtr service_data_element = | 1910 mojom::BluetoothAdvertisingDataPtr service_data_element = |
1899 mojom::BluetoothAdvertisingData::New(); | 1911 mojom::BluetoothAdvertisingData::New(); |
1900 mojom::BluetoothServiceDataPtr service_data = | 1912 mojom::BluetoothServiceDataPtr service_data = |
1901 mojom::BluetoothServiceData::New(); | 1913 mojom::BluetoothServiceData::New(); |
1902 | 1914 |
1915 // Android only support UUID 16 bit here. | |
Luis Héctor Chávez
2016/09/29 21:10:15
nit: s/support/supports/
puthik_chromium
2016/10/05 18:18:57
Done.
| |
1903 service_data->uuid_16bit = GetUUID16(uuid); | 1916 service_data->uuid_16bit = GetUUID16(uuid); |
1904 | 1917 |
1905 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); | 1918 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); |
1906 DCHECK(data != nullptr); | 1919 DCHECK(data != nullptr); |
1907 | 1920 |
1908 std::vector<uint8_t> data_copy = *data; | 1921 std::vector<uint8_t> data_copy = *data; |
1909 service_data->data.Swap(&data_copy); | 1922 service_data->data.Swap(&data_copy); |
1910 | 1923 |
1911 service_data_element->set_service_data(std::move(service_data)); | 1924 service_data_element->set_service_data(std::move(service_data)); |
1912 advertising_data.push_back(std::move(service_data_element)); | 1925 advertising_data.push_back(std::move(service_data_element)); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2030 sdp_bluetooth_instance->OnGetSdpRecords( | 2043 sdp_bluetooth_instance->OnGetSdpRecords( |
2031 status, std::move(remote_addr), target_uuid, | 2044 status, std::move(remote_addr), target_uuid, |
2032 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); | 2045 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); |
2033 } | 2046 } |
2034 | 2047 |
2035 bool ArcBluetoothBridge::CalledOnValidThread() { | 2048 bool ArcBluetoothBridge::CalledOnValidThread() { |
2036 return thread_checker_.CalledOnValidThread(); | 2049 return thread_checker_.CalledOnValidThread(); |
2037 } | 2050 } |
2038 | 2051 |
2039 } // namespace arc | 2052 } // namespace arc |
OLD | NEW |