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) { |
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 |
206 if (!isUuid16(uuid)) | |
207 LOG(WARNING) << "Illegal conversion to UUID16 " << uuid.canonical_value(); | |
200 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); | 208 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); |
201 } | 209 } |
202 | 210 |
203 mojo::Array<arc::mojom::BluetoothPropertyPtr> GetDiscoveryTimeoutProperty( | 211 mojo::Array<arc::mojom::BluetoothPropertyPtr> GetDiscoveryTimeoutProperty( |
204 uint32_t timeout) { | 212 uint32_t timeout) { |
205 arc::mojom::BluetoothPropertyPtr property = | 213 arc::mojom::BluetoothPropertyPtr property = |
206 arc::mojom::BluetoothProperty::New(); | 214 arc::mojom::BluetoothProperty::New(); |
207 property->set_discovery_timeout(timeout); | 215 property->set_discovery_timeout(timeout); |
208 mojo::Array<arc::mojom::BluetoothPropertyPtr> properties; | 216 mojo::Array<arc::mojom::BluetoothPropertyPtr> properties; |
209 properties.push_back(std::move(property)); | 217 properties.push_back(std::move(property)); |
(...skipping 1639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1849 btp->set_local_le_features(std::move(le_features)); | 1857 btp->set_local_le_features(std::move(le_features)); |
1850 properties.push_back(std::move(btp)); | 1858 properties.push_back(std::move(btp)); |
1851 } | 1859 } |
1852 | 1860 |
1853 return properties; | 1861 return properties; |
1854 } | 1862 } |
1855 | 1863 |
1856 // Android support 5 types of Advertising Data. | 1864 // Android support 5 types of Advertising Data. |
1857 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. | 1865 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. |
1858 // So we will only expose local_name, service_uuids and service_data. | 1866 // So we will only expose local_name, service_uuids and service_data. |
1859 // Note that we need to use UUID 16 bits because Android does not support | 1867 // Note that we need to use UUID 16 bits because Android does not support |
Luis Héctor Chávez
2016/09/28 16:11:48
This does not seem to be true anymore? Or at least
puthik_chromium
2016/09/29 21:06:50
Done.
| |
1860 // UUID 128 bits. | 1868 // UUID 128 bits. |
1861 // TODO(crbug.com/618442) Make Chrome expose missing data. | 1869 // TODO(crbug.com/618442) Make Chrome expose missing data. |
1862 mojo::Array<mojom::BluetoothAdvertisingDataPtr> | 1870 mojo::Array<mojom::BluetoothAdvertisingDataPtr> |
1863 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { | 1871 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { |
1864 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; | 1872 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; |
1865 | 1873 |
1866 // LocalName | 1874 // LocalName |
1867 mojom::BluetoothAdvertisingDataPtr local_name = | 1875 mojom::BluetoothAdvertisingDataPtr local_name = |
1868 mojom::BluetoothAdvertisingData::New(); | 1876 mojom::BluetoothAdvertisingData::New(); |
1869 local_name->set_local_name(device->GetName() ? device->GetName().value() | 1877 local_name->set_local_name(device->GetName() ? device->GetName().value() |
1870 : nullptr); | 1878 : nullptr); |
1871 advertising_data.push_back(std::move(local_name)); | 1879 advertising_data.push_back(std::move(local_name)); |
1872 | 1880 |
1873 // ServiceUuid | 1881 // ServiceUuid |
1874 BluetoothDevice::UUIDSet uuid_set = device->GetUUIDs(); | 1882 BluetoothDevice::UUIDSet uuid_set = device->GetUUIDs(); |
1875 if (uuid_set.size() > 0) { | 1883 if (uuid_set.size() > 0) { |
1876 mojom::BluetoothAdvertisingDataPtr service_uuids_16 = | 1884 mojom::BluetoothAdvertisingDataPtr service_uuids = |
1877 mojom::BluetoothAdvertisingData::New(); | 1885 mojom::BluetoothAdvertisingData::New(); |
1878 mojo::Array<uint16_t> uuid16s(uuid_set.size()); | 1886 if (std::all_of(uuid_set.begin(), uuid_set.end(), isUuid16)) { |
1879 size_t i = 0; | 1887 mojo::Array<uint16_t> uuid16s(uuid_set.size()); |
1880 for (const auto& uuid : uuid_set) { | 1888 size_t i = 0; |
1881 uuid16s[i] = GetUUID16(uuid); | 1889 for (const auto& uuid : uuid_set) { |
1882 i++; | 1890 uuid16s[i] = GetUUID16(uuid); |
1891 i++; | |
Luis Héctor Chávez
2016/09/28 16:11:48
Ugh, more of this pattern :(
Can you try using th
puthik_chromium
2016/09/29 02:06:08
It works.
Thanks for doing this.
| |
1892 } | |
1893 service_uuids->set_service_uuids_16(std::move(uuid16s)); | |
1894 } else { | |
1895 mojo::Array<BluetoothUUID> uuids(uuid_set.size()); | |
1896 size_t i = 0; | |
1897 for (const auto& uuid : uuid_set) { | |
1898 uuids[i] = uuid; | |
1899 i++; | |
1900 } | |
1901 service_uuids->set_service_uuids(std::move(uuids)); | |
1883 } | 1902 } |
1884 service_uuids_16->set_service_uuids_16(std::move(uuid16s)); | 1903 advertising_data.push_back(std::move(service_uuids)); |
1885 advertising_data.push_back(std::move(service_uuids_16)); | |
1886 } | 1904 } |
1887 | 1905 |
1888 // Service data | 1906 // Service data |
1889 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { | 1907 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { |
1890 mojom::BluetoothAdvertisingDataPtr service_data_element = | 1908 mojom::BluetoothAdvertisingDataPtr service_data_element = |
1891 mojom::BluetoothAdvertisingData::New(); | 1909 mojom::BluetoothAdvertisingData::New(); |
1892 mojom::BluetoothServiceDataPtr service_data = | 1910 mojom::BluetoothServiceDataPtr service_data = |
1893 mojom::BluetoothServiceData::New(); | 1911 mojom::BluetoothServiceData::New(); |
1894 | 1912 |
1895 service_data->uuid_16bit = GetUUID16(uuid); | 1913 service_data->uuid_16bit = GetUUID16(uuid); |
Luis Héctor Chávez
2016/09/28 16:11:48
In which cases |uuid| won't be an uuid16? Will it
puthik_chromium
2016/09/29 21:06:50
I think it is rare not to have uuid 16 bit for ser
| |
1896 | 1914 |
1897 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); | 1915 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); |
1898 DCHECK(data != nullptr); | 1916 DCHECK(data != nullptr); |
1899 | 1917 |
1900 std::vector<uint8_t> data_copy = *data; | 1918 std::vector<uint8_t> data_copy = *data; |
1901 service_data->data.Swap(&data_copy); | 1919 service_data->data.Swap(&data_copy); |
1902 | 1920 |
1903 service_data_element->set_service_data(std::move(service_data)); | 1921 service_data_element->set_service_data(std::move(service_data)); |
1904 advertising_data.push_back(std::move(service_data_element)); | 1922 advertising_data.push_back(std::move(service_data_element)); |
1905 } | 1923 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2022 sdp_bluetooth_instance->OnGetSdpRecords( | 2040 sdp_bluetooth_instance->OnGetSdpRecords( |
2023 status, std::move(remote_addr), target_uuid, | 2041 status, std::move(remote_addr), target_uuid, |
2024 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); | 2042 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); |
2025 } | 2043 } |
2026 | 2044 |
2027 bool ArcBluetoothBridge::CalledOnValidThread() { | 2045 bool ArcBluetoothBridge::CalledOnValidThread() { |
2028 return thread_checker_.CalledOnValidThread(); | 2046 return thread_checker_.CalledOnValidThread(); |
2029 } | 2047 } |
2030 | 2048 |
2031 } // namespace arc | 2049 } // namespace arc |
OLD | NEW |