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 // This is needed because Android only support UUID 16 bits in service data |
| 198 // section in advertising data |
198 uint16_t GetUUID16(const BluetoothUUID& uuid) { | 199 uint16_t GetUUID16(const BluetoothUUID& uuid) { |
199 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy | 200 // Convert xxxxyyyy-xxxx-xxxx-xxxx-xxxxxxxxxxxx to int16 yyyy |
200 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); | 201 return std::stoi(uuid.canonical_value().substr(4, 4), nullptr, 16); |
201 } | 202 } |
202 | 203 |
203 arc::mojom::BluetoothPropertyPtr GetDiscoveryTimeoutProperty(uint32_t timeout) { | 204 arc::mojom::BluetoothPropertyPtr GetDiscoveryTimeoutProperty(uint32_t timeout) { |
204 arc::mojom::BluetoothPropertyPtr property = | 205 arc::mojom::BluetoothPropertyPtr property = |
205 arc::mojom::BluetoothProperty::New(); | 206 arc::mojom::BluetoothProperty::New(); |
206 property->set_discovery_timeout(timeout); | 207 property->set_discovery_timeout(timeout); |
207 return property; | 208 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)); | 1858 btp->set_local_le_features(std::move(le_features)); |
1858 properties.push_back(std::move(btp)); | 1859 properties.push_back(std::move(btp)); |
1859 } | 1860 } |
1860 | 1861 |
1861 return properties; | 1862 return properties; |
1862 } | 1863 } |
1863 | 1864 |
1864 // Android support 5 types of Advertising Data. | 1865 // Android support 5 types of Advertising Data. |
1865 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. | 1866 // However Chrome didn't expose AdvertiseFlag and ManufacturerData. |
1866 // So we will only expose local_name, service_uuids and service_data. | 1867 // 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 | 1868 // Note that we need to use UUID 16 bits in service_data section |
1868 // UUID 128 bits. | 1869 // because Android does not support UUID 128 bits there. |
1869 // TODO(crbug.com/618442) Make Chrome expose missing data. | 1870 // TODO(crbug.com/618442) Make Chrome expose missing data. |
1870 mojo::Array<mojom::BluetoothAdvertisingDataPtr> | 1871 mojo::Array<mojom::BluetoothAdvertisingDataPtr> |
1871 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { | 1872 ArcBluetoothBridge::GetAdvertisingData(BluetoothDevice* device) const { |
1872 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; | 1873 mojo::Array<mojom::BluetoothAdvertisingDataPtr> advertising_data; |
1873 | 1874 |
1874 // LocalName | 1875 // LocalName |
1875 mojom::BluetoothAdvertisingDataPtr local_name = | 1876 mojom::BluetoothAdvertisingDataPtr local_name = |
1876 mojom::BluetoothAdvertisingData::New(); | 1877 mojom::BluetoothAdvertisingData::New(); |
1877 local_name->set_local_name(device->GetName() ? device->GetName().value() | 1878 local_name->set_local_name(device->GetName() ? device->GetName().value() |
1878 : nullptr); | 1879 : nullptr); |
1879 advertising_data.push_back(std::move(local_name)); | 1880 advertising_data.push_back(std::move(local_name)); |
1880 | 1881 |
1881 // ServiceUuid | 1882 // ServiceUuid |
1882 BluetoothDevice::UUIDSet uuid_set = device->GetUUIDs(); | 1883 const BluetoothDevice::UUIDSet& uuid_set = device->GetUUIDs(); |
1883 if (uuid_set.size() > 0) { | 1884 if (uuid_set.size() > 0) { |
1884 mojom::BluetoothAdvertisingDataPtr service_uuids_16 = | 1885 mojom::BluetoothAdvertisingDataPtr service_uuids = |
1885 mojom::BluetoothAdvertisingData::New(); | 1886 mojom::BluetoothAdvertisingData::New(); |
1886 mojo::Array<uint16_t> uuid16s(uuid_set.size()); | 1887 service_uuids->set_service_uuids(mojo::Array<BluetoothUUID>::From( |
1887 size_t i = 0; | 1888 std::vector<BluetoothUUID>(uuid_set.begin(), uuid_set.end()))); |
1888 for (const auto& uuid : uuid_set) { | 1889 advertising_data.push_back(std::move(service_uuids)); |
1889 uuid16s[i] = GetUUID16(uuid); | |
1890 i++; | |
1891 } | |
1892 service_uuids_16->set_service_uuids_16(std::move(uuid16s)); | |
1893 advertising_data.push_back(std::move(service_uuids_16)); | |
1894 } | 1890 } |
1895 | 1891 |
1896 // Service data | 1892 // Service data |
1897 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { | 1893 for (const BluetoothUUID& uuid : device->GetServiceDataUUIDs()) { |
1898 mojom::BluetoothAdvertisingDataPtr service_data_element = | 1894 mojom::BluetoothAdvertisingDataPtr service_data_element = |
1899 mojom::BluetoothAdvertisingData::New(); | 1895 mojom::BluetoothAdvertisingData::New(); |
1900 mojom::BluetoothServiceDataPtr service_data = | 1896 mojom::BluetoothServiceDataPtr service_data = |
1901 mojom::BluetoothServiceData::New(); | 1897 mojom::BluetoothServiceData::New(); |
1902 | 1898 |
| 1899 // Android only supports UUID 16 bit here. |
1903 service_data->uuid_16bit = GetUUID16(uuid); | 1900 service_data->uuid_16bit = GetUUID16(uuid); |
1904 | 1901 |
1905 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); | 1902 const std::vector<uint8_t>* data = device->GetServiceDataForUUID(uuid); |
1906 DCHECK(data != nullptr); | 1903 DCHECK(data != nullptr); |
1907 | 1904 |
1908 std::vector<uint8_t> data_copy = *data; | 1905 std::vector<uint8_t> data_copy = *data; |
1909 service_data->data.Swap(&data_copy); | 1906 service_data->data.Swap(&data_copy); |
1910 | 1907 |
1911 service_data_element->set_service_data(std::move(service_data)); | 1908 service_data_element->set_service_data(std::move(service_data)); |
1912 advertising_data.push_back(std::move(service_data_element)); | 1909 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( | 2027 sdp_bluetooth_instance->OnGetSdpRecords( |
2031 status, std::move(remote_addr), target_uuid, | 2028 status, std::move(remote_addr), target_uuid, |
2032 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); | 2029 mojo::Array<mojom::BluetoothSdpRecordPtr>::New(0)); |
2033 } | 2030 } |
2034 | 2031 |
2035 bool ArcBluetoothBridge::CalledOnValidThread() { | 2032 bool ArcBluetoothBridge::CalledOnValidThread() { |
2036 return thread_checker_.CalledOnValidThread(); | 2033 return thread_checker_.CalledOnValidThread(); |
2037 } | 2034 } |
2038 | 2035 |
2039 } // namespace arc | 2036 } // namespace arc |
OLD | NEW |