diff options
author | 2025-03-18 16:45:39 -0700 | |
---|---|---|
committer | 2025-03-18 16:45:39 -0700 | |
commit | 7161f3d277f10af218ee197ad09a8ccaf6b79536 (patch) | |
tree | a042cc54f950f2140ac1d27772dc27439507d662 | |
parent | 65ff66e98a4e06b6a4fa333ec707bae101371e3b (diff) | |
parent | e1ce1f2459f9afb2f7c1ec4f269b28f7f17e3976 (diff) |
Merge "system/btcore: Remove unused sources and headers" into main
-rw-r--r-- | TEST_MAPPING | 6 | ||||
-rwxr-xr-x | build.py | 1 | ||||
-rw-r--r-- | system/BUILD.gn | 1 | ||||
-rw-r--r-- | system/btcore/Android.bp | 40 | ||||
-rw-r--r-- | system/btcore/BUILD.gn | 34 | ||||
-rw-r--r-- | system/btcore/fuzzer/Android.bp | 97 | ||||
-rw-r--r-- | system/btcore/fuzzer/README.md | 56 | ||||
-rw-r--r-- | system/btcore/fuzzer/btcore_device_class_fuzzer.cpp | 70 | ||||
-rw-r--r-- | system/btcore/fuzzer/btcore_module_fuzzer.cpp | 48 | ||||
-rw-r--r-- | system/btcore/fuzzer/btcore_property_fuzzer.cpp | 96 | ||||
-rw-r--r-- | system/btcore/include/device_class.h | 95 | ||||
-rw-r--r-- | system/btcore/include/device_features.h | 27 | ||||
-rw-r--r-- | system/btcore/include/event_mask.h | 28 | ||||
-rw-r--r-- | system/btcore/include/iac.h | 28 | ||||
-rw-r--r-- | system/btcore/include/property.h | 76 | ||||
-rw-r--r-- | system/btcore/include/version.h | 29 | ||||
-rw-r--r-- | system/btcore/src/device_class.cc | 167 | ||||
-rw-r--r-- | system/btcore/src/property.cc | 217 | ||||
-rw-r--r-- | system/btcore/test/device_class_test.cc | 224 | ||||
-rw-r--r-- | system/btcore/test/property_test.cc | 269 | ||||
-rw-r--r-- | system/test/suite/adapter/adapter_unittest.cc | 51 | ||||
-rw-r--r-- | system/test/suite/adapter/bluetooth_test.cc | 28 |
22 files changed, 77 insertions, 1611 deletions
diff --git a/TEST_MAPPING b/TEST_MAPPING index b1cc9a7594..c807f7d8ec 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -133,9 +133,6 @@ "name": "net_test_bta_security" }, { - "name": "net_test_btcore" - }, - { "name": "net_test_btif" }, { @@ -365,9 +362,6 @@ "name": "net_test_bta_security" }, { - "name": "net_test_btcore" - }, - { "name": "net_test_btif" }, { @@ -86,7 +86,6 @@ HOST_TESTS = [ # 'bluetooth_test_common', # 'bluetoothtbd_test', # 'net_test_avrcp', - # 'net_test_btcore', # 'net_test_types', # 'net_test_btm_iso', # 'net_test_btpackets', diff --git a/system/BUILD.gn b/system/BUILD.gn index 48b6207a60..1d6325e6d9 100644 --- a/system/BUILD.gn +++ b/system/BUILD.gn @@ -41,7 +41,6 @@ group("bluetooth") { #if (use.test) { #group("bluetooth_tests") { #deps = [ - #"//bt/system/btcore:net_test_btcore", #"//bt/system/common:bluetooth_test_common", #"//bt/system/profile/avrcp:net_test_avrcp", #"//bt/system/service:bluetoothtbd_test", diff --git a/system/btcore/Android.bp b/system/btcore/Android.bp index 6a0350afcb..b02ead0f8d 100644 --- a/system/btcore/Android.bp +++ b/system/btcore/Android.bp @@ -20,10 +20,8 @@ cc_library_static { "packages/modules/Bluetooth/system/gd", ], srcs: [ - "src/device_class.cc", "src/module.cc", "src/osi_module.cc", - "src/property.cc", ], header_libs: ["libbluetooth_headers"], static_libs: [ @@ -50,41 +48,3 @@ cc_library_headers { ], min_sdk_version: "30", } - -// Note: It's good to get the tests compiled both for the host and the target so -// we get to test with both Bionic libc and glibc -// libbtcore unit tests for target and host -cc_test { - name: "net_test_btcore", - test_suites: ["general-tests"], - defaults: [ - "fluoride_defaults", - "mts_defaults", - ], - local_include_dirs: ["include"], - include_dirs: ["packages/modules/Bluetooth/system"], - srcs: [ - "test/device_class_test.cc", - "test/property_test.cc", - ], - shared_libs: [ - "libbase", - "liblog", - ], - static_libs: [ - "libbluetooth-types", - "libbluetooth_log", - "libbtcore", - "libchrome", - "libcom.android.sysprop.bluetooth.wrapped", - "libosi", - ], - host_supported: true, - test_options: { - unit_test: true, - }, - sanitize: { - address: true, - }, - header_libs: ["libbluetooth_headers"], -} diff --git a/system/btcore/BUILD.gn b/system/btcore/BUILD.gn index 76853554cf..abfbd328ee 100644 --- a/system/btcore/BUILD.gn +++ b/system/btcore/BUILD.gn @@ -16,11 +16,9 @@ static_library("btcore") { sources = [ - "src/device_class.cc", "src/hal_util.cc", "src/module.cc", "src/osi_module.cc", - "src/property.cc", ] include_dirs = [ @@ -33,35 +31,3 @@ static_library("btcore") { "//bt/system/log:log_defaults", ] } - -if (use.test) { - executable("net_test_btcore") { - sources = [ - "test/device_class_test.cc", - "test/property_test.cc", - ] - - include_dirs = [ - "include", - "//bt/system", - ] - - deps = [ - "//bt/system/btcore", - "//bt/system/osi", - "//bt/system/types", - ] - - configs += [ - "//bt/system:external_gtest_main", - "//bt/system:target_defaults", - "//bt/system/log:log_defaults", - ] - - libs = [ - "pthread", - "rt", - "dl", - ] - } -} diff --git a/system/btcore/fuzzer/Android.bp b/system/btcore/fuzzer/Android.bp deleted file mode 100644 index 07b8c28f89..0000000000 --- a/system/btcore/fuzzer/Android.bp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package { - // See: http://go/android-license-faq - // A large-scale-change added 'default_applicable_licenses' to import - // all of the 'license_kinds' from "system_bt_license" - // to get the below license kinds: - // SPDX-license-identifier-Apache-2.0 - default_applicable_licenses: ["system_bt_license"], -} - -cc_defaults { - name: "btcore_fuzz_defaults", - shared_libs: [ - "libcrypto", - "libstatslog_bt", - ], - static_libs: [ - "libbase", - "libbluetooth-types", - "libbluetooth_crypto_toolbox", - "libbluetooth_log", - "libbt-common", - "libbtcore", - "libchrome", - "libcom.android.sysprop.bluetooth.wrapped", - "libevent", - "liblog", - "libmodpb64", - "libosi", - "libprotobuf-cpp-lite", - ], - include_dirs: [ - "packages/modules/Bluetooth/system", - ], - header_libs: [ - "libbluetooth_headers", - ], - fuzz_config: { - cc: ["android-bluetooth-security@google.com"], - componentid: 27441, - hotlists: [ - "4593311", - ], - description: "The fuzzer targets the APIs of libbtcore", - vector: "local_no_privileges_required", - service_privilege: "privileged", - users: "multi_user", - fuzzed_code_usage: "shipped", - }, -} - -cc_fuzz { - name: "btcore_device_class_fuzzer", - defaults: [ - "btcore_fuzz_defaults", - ], - srcs: [ - "btcore_device_class_fuzzer.cpp", - ], -} - -cc_fuzz { - name: "btcore_property_fuzzer", - defaults: [ - "btcore_fuzz_defaults", - ], - srcs: [ - "btcore_property_fuzzer.cpp", - ], - -} - -cc_fuzz { - name: "btcore_module_fuzzer", - enabled: false, - defaults: [ - "btcore_fuzz_defaults", - ], - srcs: [ - "btcore_module_fuzzer.cpp", - ], -} diff --git a/system/btcore/fuzzer/README.md b/system/btcore/fuzzer/README.md deleted file mode 100644 index cf770e4385..0000000000 --- a/system/btcore/fuzzer/README.md +++ /dev/null @@ -1,56 +0,0 @@ -# Fuzzers for libbtcore - -## Plugin Design Considerations -The fuzzer plugins for `libbtcore` are designed based on the understanding of the -source code and tries to achieve the following: - -##### Maximize code coverage -The configuration parameters are not hard-coded, but instead selected based on -incoming data. This ensures more code paths are reached by the fuzzers. - -Fuzzers assigns values to the following parameters to pass on to libbtcore: -1. Bluetooth Device Type (parameter name: `deviceType`) -2. Bluetooth Adapter Visibility Mode (parameter name: `mode`) -3. Bluetooth Address (parameter name: `btAddress`) -4. Bluetooth Device Class parameter (parameter name: `deviceClassT`) - -| Parameter| Valid Values| Configured Value| -|------------- |-------------| ----- | -| `deviceType` | 0.`BT_DEVICE_DEVTYPE_BREDR` 1.`BT_DEVICE_DEVTYPE_BLE` 2.`BT_DEVICE_DEVTYPE_DUAL` | Value obtained from FuzzedDataProvider | -| `mode` | 0.`BT_SCAN_MODE_NONE` 1.`BT_SCAN_MODE_CONNECTABLE` 2.`BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE` | Value obtained from FuzzedDataProvider | -| `btAddress` | Values inside array ranges from `0x0` to `0xFF` | Value obtained from FuzzedDataProvider | -| `deviceClassT` | Values inside array ranges from `0x0` to `0xFF` | Value obtained from FuzzedDataProvider | -This also ensures that the plugins are always deterministic for any given input. - -##### Maximize utilization of input data -The plugins feed the entire input data to the module. -This ensures that the plugins tolerates any kind of input (empty, huge, -malformed, etc) and doesn't `exit()` on any input and thereby increasing the -chance of identifying vulnerabilities. - -## Build - -This describes steps to build btcore_device_class_fuzzer, btcore_property_fuzzer and btcore_module_fuzzer binaries. - -### Android - -#### Steps to build -Build the fuzzer -``` - $ mm -j$(nproc) btcore_device_class_fuzzer - $ mm -j$(nproc) btcore_property_fuzzer - $ mm -j$(nproc) btcore_module_fuzzer -``` -### Steps to run - -To run on device -``` - $ adb sync data - $ adb shell /data/fuzz/arm64/btcore_device_class_fuzzer/btcore_device_class_fuzzer - $ adb shell /data/fuzz/arm64/btcore_property_fuzzer/btcore_property_fuzzer - $ adb shell /data/fuzz/arm64/btcore_module_fuzzer/btcore_module_fuzzer -``` - -## References: - * http://llvm.org/docs/LibFuzzer.html - * https://github.com/google/oss-fuzz diff --git a/system/btcore/fuzzer/btcore_device_class_fuzzer.cpp b/system/btcore/fuzzer/btcore_device_class_fuzzer.cpp deleted file mode 100644 index fef1e33370..0000000000 --- a/system/btcore/fuzzer/btcore_device_class_fuzzer.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <fuzzer/FuzzedDataProvider.h> - -#include "btcore/include/device_class.h" - -class BTCoreDeviceClassFuzzer { -public: - void process(const uint8_t* data, size_t size); - -private: - std::unique_ptr<FuzzedDataProvider> mFdp = nullptr; -}; - -void BTCoreDeviceClassFuzzer::process(const uint8_t* data, size_t size) { - mFdp = std::make_unique<FuzzedDataProvider>(data, size); - size_t dcStreamSize = sizeof(bt_device_class_t) * sizeof(uint8_t); - - std::vector<uint8_t> dcStreamSrc(dcStreamSize, 0x0); - mFdp->ConsumeData(dcStreamSrc.data(), dcStreamSize); - - bt_device_class_t deviceClass; - device_class_from_stream(&deviceClass, dcStreamSrc.data()); - - std::vector<uint8_t> dcStreamDst(dcStreamSize, 0x0); - (void)device_class_to_stream(&deviceClass, dcStreamDst.data(), dcStreamSize); - - device_class_set_limited(&deviceClass, mFdp->ConsumeBool()); - (void)device_class_get_limited(&deviceClass); - - int val = mFdp->ConsumeIntegral<int>(); - device_class_set_major_device(&deviceClass, val); - (void)device_class_get_major_device(&deviceClass); - - val = mFdp->ConsumeIntegral<int>(); - device_class_set_minor_device(&deviceClass, val); - (void)device_class_get_minor_device(&deviceClass); - - device_class_set_information(&deviceClass, mFdp->ConsumeBool()); - (void)device_class_get_information(&deviceClass); - - bt_device_class_t deviceClassCopy; - (void)device_class_copy(&deviceClassCopy, &deviceClass); - (void)device_class_equals(&deviceClass, &deviceClassCopy); - - val = mFdp->ConsumeIntegralInRange(1, INT_MAX); - device_class_from_int(&deviceClass, val); - (void)device_class_to_int(&deviceClass); -} - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - BTCoreDeviceClassFuzzer btCoreDeviceClassFuzzer; - btCoreDeviceClassFuzzer.process(data, size); - return 0; -} diff --git a/system/btcore/fuzzer/btcore_module_fuzzer.cpp b/system/btcore/fuzzer/btcore_module_fuzzer.cpp deleted file mode 100644 index adb21b724d..0000000000 --- a/system/btcore/fuzzer/btcore_module_fuzzer.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "btcore/include/hal_util.h" -#include "btcore/include/module.h" - -extern const module_t osi_module; - -extern "C" { -struct android_namespace_t* android_get_exported_namespace(const char*) { return nullptr; } -} - -class BTCoreModuleFuzzer { -public: - void process(); -}; - -void BTCoreModuleFuzzer::process() { - const bt_interface_t* interface; - (void)hal_util_load_bt_library(&interface); - module_management_start(); - module_init(&osi_module); - (void)module_start_up(&osi_module); - (void)get_module(osi_module.name); - module_shut_down(&osi_module); - module_clean_up(&osi_module); - module_management_stop(); -} - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t*, size_t) { - BTCoreModuleFuzzer btCoreModuleFuzzer; - btCoreModuleFuzzer.process(); - return 0; -} diff --git a/system/btcore/fuzzer/btcore_property_fuzzer.cpp b/system/btcore/fuzzer/btcore_property_fuzzer.cpp deleted file mode 100644 index eabcf505c3..0000000000 --- a/system/btcore/fuzzer/btcore_property_fuzzer.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2021 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include <fuzzer/FuzzedDataProvider.h> - -#include "btcore/include/property.h" -#include "types/bluetooth/uuid.h" -#include "types/raw_address.h" - -using bluetooth::Uuid; - -constexpr int32_t kRandomStringLength = 256; - -class BTCorePropertyFuzzer { -public: - void process(const uint8_t* data, size_t size); - -private: - std::unique_ptr<FuzzedDataProvider> mFdp = nullptr; -}; - -void BTCorePropertyFuzzer::process(const uint8_t* data, size_t size) { - mFdp = std::make_unique<FuzzedDataProvider>(data, size); - uint8_t addr[RawAddress::kLength]; - mFdp->ConsumeData(addr, sizeof(uint8_t) * RawAddress::kLength); - RawAddress btAddress = {addr}; - bt_property_t* property = property_new_addr(&btAddress); - property_as_addr(property); - property_free(property); - - bt_device_class_t deviceClass = {{mFdp->ConsumeIntegral<uint8_t>(), - mFdp->ConsumeIntegral<uint8_t>(), - mFdp->ConsumeIntegral<uint8_t>()}}; - property = property_new_device_class(&deviceClass); - - const bt_device_class_t* pDeviceClass = property_as_device_class(property); - (void)device_class_to_int(pDeviceClass); - property_free(property); - - bt_device_type_t deviceType = (bt_device_type_t)(mFdp->ConsumeIntegral<uint32_t>()); - property = property_new_device_type(deviceType); - (void)property_as_device_type(property); - property_free(property); - - uint32_t timeout = mFdp->ConsumeIntegral<uint32_t>(); - property = property_new_discoverable_timeout(timeout); - (void)property_as_discoverable_timeout(property); - property_free(property); - - std::string name = mFdp->ConsumeRandomLengthString(kRandomStringLength); - property = property_new_name(name.c_str()); - (void)property_as_name(property); - property_free(property); - - int8_t rssi = mFdp->ConsumeIntegral<int8_t>(); - property = property_new_rssi(rssi); - (void)property_as_rssi(property); - property_free(property); - - size_t uuidSize = sizeof(uint8_t) * bluetooth::Uuid::kNumBytes128; - uint8_t uuid[bluetooth::Uuid::kNumBytes128]; - mFdp->ConsumeData(uuid, uuidSize); - Uuid uuidBE = Uuid::From128BitBE(uuid); - property = property_new_uuids(&uuidBE, 1); - size_t uuidCount; - (void)property_as_uuids(property, &uuidCount); - property_free(property); - - mFdp->ConsumeData(uuid, uuidSize); - Uuid uuidLE = Uuid::From128BitLE(uuid); - Uuid uuids[] = {uuidBE, uuidLE}; - bt_property_t* propertySrc = property_new_uuids(uuids, std::size(uuids)); - bt_property_t propertyDest; - (void)property_copy(&propertyDest, propertySrc); - property_free(propertySrc); -} - -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - BTCorePropertyFuzzer btCorePropertyFuzzer; - btCorePropertyFuzzer.process(data, size); - return 0; -} diff --git a/system/btcore/include/device_class.h b/system/btcore/include/device_class.h deleted file mode 100644 index 6f56b119b0..0000000000 --- a/system/btcore/include/device_class.h +++ /dev/null @@ -1,95 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> - -// Provides Class Of Device primitive as specified in the bluetooth spec. -// [Class Of Device] -// (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband) - -// Device class may be defined in other structures. -// Only use defined methods to manipulate internals. -typedef struct bt_device_class_t { - uint8_t _[3]; // Do not access directly; use methods below. -} bt_device_class_t; - -// Copies the |data| class of device stream into device class |dc|. |dc| -// and |data| must not be NULL. -void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data); - -// Serializes the device class |dc| to pointer argument |data| in big endian -// format. |len| must contain the buffer size of |data|. Returns the actual -// number of bytes copied into |data|. |dc| and |data| must not be NULL. -int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data, size_t len); - -// Copies the |data| class of device integer into device class |dc|. |dc| -// must not be NULL. -void device_class_from_int(bt_device_class_t* dc, int data); - -// Returns the device class |dc| in integer format. |dc| must not be NULL. -int device_class_to_int(const bt_device_class_t* dc); - -// Compares and returns |true| if two device classes |p1| and |p2| are equal. -// False otherwise. -bool device_class_equals(const bt_device_class_t* p1, const bt_device_class_t* p2); - -// Copies and returns |true| if the device class was successfully copied from -// |p2| into |p1|. False otherwise. -bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src); - -// Query, getters and setters for the major device class. |dc| must not be -// NULL. -int device_class_get_major_device(const bt_device_class_t* dc); -void device_class_set_major_device(bt_device_class_t* dc, int val); - -// Query, getters and setters for the minor device class. |dc| must not be NULL. -int device_class_get_minor_device(const bt_device_class_t* dc); -void device_class_set_minor_device(bt_device_class_t* dc, int val); - -// Query, getters and setters for the various major service class features. -// |dc| must not be NULL. -bool device_class_get_limited(const bt_device_class_t* dc); -void device_class_set_limited(bt_device_class_t* dc, bool set); - -bool device_class_get_positioning(const bt_device_class_t* dc); -void device_class_set_positioning(bt_device_class_t* dc, bool set); - -bool device_class_get_networking(const bt_device_class_t* dc); -void device_class_set_networking(bt_device_class_t* dc, bool set); - -bool device_class_get_rendering(const bt_device_class_t* dc); -void device_class_set_rendering(bt_device_class_t* dc, bool set); - -bool device_class_get_capturing(const bt_device_class_t* dc); -void device_class_set_capturing(bt_device_class_t* dc, bool set); - -bool device_class_get_object_transfer(const bt_device_class_t* dc); -void device_class_set_object_transfer(bt_device_class_t* dc, bool set); - -bool device_class_get_audio(const bt_device_class_t* dc); -void device_class_set_audio(bt_device_class_t* dc, bool set); - -bool device_class_get_telephony(const bt_device_class_t* dc); -void device_class_set_telephony(bt_device_class_t* dc, bool set); - -bool device_class_get_information(const bt_device_class_t* dc); -void device_class_set_information(bt_device_class_t* dc, bool set); diff --git a/system/btcore/include/device_features.h b/system/btcore/include/device_features.h deleted file mode 100644 index 13f8653635..0000000000 --- a/system/btcore/include/device_features.h +++ /dev/null @@ -1,27 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <stdint.h> - -// Represents a page of device feature enabled/disabled bits returned -// by the local controller. See the bluetooth spec for bit indexes. -typedef struct { - uint8_t as_array[8]; -} bt_device_features_t; diff --git a/system/btcore/include/event_mask.h b/system/btcore/include/event_mask.h deleted file mode 100644 index f811c78c8e..0000000000 --- a/system/btcore/include/event_mask.h +++ /dev/null @@ -1,28 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <stdint.h> - -// Represents a mask which can be used to tell the controller which -// HCI events the stack wishes to be informed about. See the bluetooth -// spec for more information on what each bit means. -typedef struct { - const uint8_t as_array[8]; -} bt_event_mask_t; diff --git a/system/btcore/include/iac.h b/system/btcore/include/iac.h deleted file mode 100644 index 39c830e54b..0000000000 --- a/system/btcore/include/iac.h +++ /dev/null @@ -1,28 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <stdint.h> - -// Inquiry Access Code -// [Bluetooth Baseband] -// (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband) -typedef struct { - uint8_t iac[3]; -} __attribute__((packed)) bt_inquiry_access_code_t; diff --git a/system/btcore/include/property.h b/system/btcore/include/property.h deleted file mode 100644 index f8e06150ad..0000000000 --- a/system/btcore/include/property.h +++ /dev/null @@ -1,76 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <hardware/bluetooth.h> -#include <stdint.h> -#include <stdlib.h> - -#include "btcore/include/device_class.h" -#include "types/bluetooth/uuid.h" -#include "types/raw_address.h" - -// Copies an array of consecutive properties of |count| to a newly -// allocated array. |properties| must not be NULL. -bt_property_t* property_copy_array(const bt_property_t* properties, size_t count); - -// Copies |src| to |dest|. Returns the value of |dest|. -// |src| and |dest| must not be NULL. -bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src); - -// Returns true if the value of the two properties |p1| and |p2| are equal. -// |p1| and |p2| must not be NULL. -bool property_equals(const bt_property_t* p1, const bt_property_t* p2); - -// Property resource allocations. Caller is expected to free |property| -// using |property_free| or |property_free_array|. -// Parameter must not be NULL. A copy of the parameter is made and -// stored in the property. -bt_property_t* property_new_addr(const RawAddress* addr); -bt_property_t* property_new_device_class(const bt_device_class_t* dc); -bt_property_t* property_new_device_type(bt_device_type_t device_type); -bt_property_t* property_new_discoverable_timeout(const uint32_t timeout); -bt_property_t* property_new_name(const char* name); -bt_property_t* property_new_rssi(const int8_t rssi); -bt_property_t* property_new_uuids(const bluetooth::Uuid* uuid, size_t count); - -// Property resource frees both property and value. -void property_free(bt_property_t* property); -void property_free_array(bt_property_t* properties, size_t count); - -// Value check convenience methods. The contents of the property are -// checked for the respective validity and returns true, false otherwise. -// |property| must not be NULL. -bool property_is_addr(const bt_property_t* property); -bool property_is_device_class(const bt_property_t* property); -bool property_is_device_type(const bt_property_t* property); -bool property_is_discoverable_timeout(const bt_property_t* property); -bool property_is_name(const bt_property_t* property); -bool property_is_rssi(const bt_property_t* property); -bool property_is_uuids(const bt_property_t* property); - -// Value conversion convenience methods. The contents of the property are -// properly typed and returned to the caller. |property| must not be NULL. -const RawAddress* property_as_addr(const bt_property_t* property); -const bt_device_class_t* property_as_device_class(const bt_property_t* property); -bt_device_type_t property_as_device_type(const bt_property_t* property); -uint32_t property_as_discoverable_timeout(const bt_property_t* property); -const bt_bdname_t* property_as_name(const bt_property_t* property); -int8_t property_as_rssi(const bt_property_t* property); -const bluetooth::Uuid* property_as_uuids(const bt_property_t* property, size_t* count); diff --git a/system/btcore/include/version.h b/system/btcore/include/version.h deleted file mode 100644 index d7787c7409..0000000000 --- a/system/btcore/include/version.h +++ /dev/null @@ -1,29 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#pragma once - -#include <stdint.h> - -typedef struct { - uint8_t hci_version; - uint16_t hci_revision; - uint8_t lmp_version; - uint16_t manufacturer; - uint16_t lmp_subversion; -} bt_version_t; diff --git a/system/btcore/src/device_class.cc b/system/btcore/src/device_class.cc deleted file mode 100644 index 08d2ae191a..0000000000 --- a/system/btcore/src/device_class.cc +++ /dev/null @@ -1,167 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#include "btcore/include/device_class.h" - -#include <arpa/inet.h> -#include <bluetooth/log.h> -#include <endian.h> -#include <string.h> - -using namespace bluetooth; - -typedef struct _bt_device_class_t { - uint32_t unused : 2; // LSBs - uint32_t minor_device : 6; - uint32_t major_device : 5; - uint32_t major_service : 11; // MSBs -} __attribute__((__packed__)) _bt_device_class_t; - -// Convenience to interpret raw device class bytes. -#define DC(x) ((_bt_device_class_t*)(x)) - -// Ensure the internal device class implementation and public one -// have equal size. -static_assert(sizeof(_bt_device_class_t) == sizeof(bt_device_class_t), - "Internal and external device class implementation should have the same size"); - -// [Major Service Classes] -// (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband) -enum { - DC_LIMITED_DISCOVERABLE_MODE = 0x0001, - DC_RESERVED14 = 0x0002, - DC_RESERVED15 = 0x0004, - DC_POSITIONING = 0x0008, - DC_NETWORKING = 0x0010, - DC_RENDERING = 0x0020, - DC_CAPTURING = 0x0040, - DC_OBJECT_TRANSFER = 0x0080, - DC_AUDIO = 0x0100, - DC_TELEPHONY = 0x0200, - DC_INFORMATION = 0x0400, -}; - -static bool device_class_get_major_service_(const bt_device_class_t* dc, int bitmask); -static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask); -static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask); - -void device_class_from_stream(bt_device_class_t* dc, const uint8_t* data) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - log::assert_that(data != NULL, "assert failed: data != NULL"); - *dc = *(bt_device_class_t*)data; -} - -int device_class_to_stream(const bt_device_class_t* dc, uint8_t* data, size_t len) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - log::assert_that(data != NULL, "assert failed: data != NULL"); - log::assert_that(len >= sizeof(bt_device_class_t), - "assert failed: len >= sizeof(bt_device_class_t)"); - for (size_t i = 0; i < sizeof(bt_device_class_t); ++i) { - data[i] = dc->_[i]; - } - return sizeof(bt_device_class_t); -} - -void device_class_from_int(bt_device_class_t* dc, int data) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - log::assert_that(data != 0, "assert failed: data != 0"); - // Careful with endianess. - dc->_[0] = data & 0xff; - dc->_[1] = (data >> 8) & 0xff; - dc->_[2] = (data >> 16) & 0xff; -} - -int device_class_to_int(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - // Careful with endianess. - int val = 0; - memcpy(&val, dc, sizeof(*dc)); - return static_cast<int>(le32toh(val) & 0xffffff); -} - -bool device_class_equals(const bt_device_class_t* p1, const bt_device_class_t* p2) { - log::assert_that(p1 != NULL, "assert failed: p1 != NULL"); - log::assert_that(p2 != NULL, "assert failed: p2 != NULL"); - return memcmp(p1, p2, sizeof(bt_device_class_t)) == 0; -} - -bool device_class_copy(bt_device_class_t* dest, const bt_device_class_t* src) { - log::assert_that(dest != NULL, "assert failed: dest != NULL"); - log::assert_that(src != NULL, "assert failed: src != NULL"); - return memcpy(dest, src, sizeof(bt_device_class_t)) == dest; -} - -int device_class_get_major_device(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - return DC(dc)->major_device; -} - -void device_class_set_major_device(bt_device_class_t* dc, int val) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - DC(dc)->major_device = val; -} - -int device_class_get_minor_device(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - return DC(dc)->minor_device; -} - -void device_class_set_minor_device(bt_device_class_t* dc, int val) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - DC(dc)->minor_device = val; -} - -bool device_class_get_information(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - return device_class_get_major_service_(dc, DC_INFORMATION); -} - -void device_class_set_information(bt_device_class_t* dc, bool set) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - if (set) { - device_class_set_major_service_(dc, DC_INFORMATION); - } else { - device_class_clr_major_service_(dc, DC_INFORMATION); - } -} - -bool device_class_get_limited(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - return device_class_get_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE); -} - -void device_class_set_limited(bt_device_class_t* dc, bool set) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - if (set) { - device_class_set_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE); - } else { - device_class_clr_major_service_(dc, DC_LIMITED_DISCOVERABLE_MODE); - } -} - -static bool device_class_get_major_service_(const bt_device_class_t* dc, int bitmask) { - return DC(dc)->major_service & bitmask; -} - -static void device_class_clr_major_service_(bt_device_class_t* dc, int bitmask) { - DC(dc)->major_service &= ~bitmask; -} - -static void device_class_set_major_service_(bt_device_class_t* dc, int bitmask) { - DC(dc)->major_service |= bitmask; -} diff --git a/system/btcore/src/property.cc b/system/btcore/src/property.cc deleted file mode 100644 index 0379229432..0000000000 --- a/system/btcore/src/property.cc +++ /dev/null @@ -1,217 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#include "btcore/include/property.h" - -#include <bluetooth/log.h> -#include <string.h> - -#include "btcore/include/device_class.h" -#include "osi/include/allocator.h" -#include "osi/include/compat.h" -#include "types/bluetooth/uuid.h" -#include "types/raw_address.h" - -using bluetooth::Uuid; -using namespace bluetooth; - -static bt_property_t* property_new_(void* val, size_t len, bt_property_type_t type); - -bt_property_t* property_copy_array(const bt_property_t* properties, size_t count) { - log::assert_that(properties != NULL, "assert failed: properties != NULL"); - bt_property_t* clone = static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count)); - - memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count); - for (size_t i = 0; i < count; ++i) { - clone[i].val = osi_calloc(clone[i].len); - memcpy(clone[i].val, properties[i].val, clone[i].len); - } - - return clone; -} - -bt_property_t* property_copy(bt_property_t* dest, const bt_property_t* src) { - log::assert_that(dest != NULL, "assert failed: dest != NULL"); - log::assert_that(src != NULL, "assert failed: src != NULL"); - return (bt_property_t*)memcpy(dest, src, sizeof(bt_property_t)); -} - -bool property_equals(const bt_property_t* p1, const bt_property_t* p2) { - // Two null properties are not the same. May need to revisit that - // decision when we have a test case that exercises that condition. - if (!p1 || !p2 || p1->type != p2->type) { - return false; - } - - // Although the Bluetooth name is a 249-byte array, the implementation - // treats it like a variable-length array with its size specified in the - // property's `len` field. We special-case the equivalence of BDNAME - // types here by truncating the larger, zero-padded name to its string - // length and comparing against the shorter name. - // - // Note: it may be the case that both strings are zero-padded but that - // hasn't come up yet so this implementation doesn't handle it. - if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) { - const bt_property_t *shorter = p1, *longer = p2; - if (p1->len > p2->len) { - shorter = p2; - longer = p1; - } - return strlen((const char*)longer->val) == (size_t)shorter->len && - !memcmp(longer->val, shorter->val, shorter->len); - } - - return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len); -} - -bt_property_t* property_new_addr(const RawAddress* addr) { - log::assert_that(addr != NULL, "assert failed: addr != NULL"); - return property_new_((void*)addr, sizeof(RawAddress), BT_PROPERTY_BDADDR); -} - -bt_property_t* property_new_device_class(const bt_device_class_t* dc) { - log::assert_that(dc != NULL, "assert failed: dc != NULL"); - return property_new_((void*)dc, sizeof(bt_device_class_t), BT_PROPERTY_CLASS_OF_DEVICE); -} - -bt_property_t* property_new_device_type(bt_device_type_t type) { - return property_new_((void*)&type, sizeof(bt_device_type_t), BT_PROPERTY_TYPE_OF_DEVICE); -} - -bt_property_t* property_new_discoverable_timeout(const uint32_t timeout) { - return property_new_((void*)&timeout, sizeof(uint32_t), BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT); -} - -bt_property_t* property_new_name(const char* name) { - log::assert_that(name != NULL, "assert failed: name != NULL"); - return property_new_((void*)name, sizeof(bt_bdname_t), BT_PROPERTY_BDNAME); -} - -bt_property_t* property_new_rssi(int8_t rssi) { - return property_new_((void*)&rssi, sizeof(int8_t), BT_PROPERTY_REMOTE_RSSI); -} - -bt_property_t* property_new_uuids(const Uuid* uuid, size_t count) { - log::assert_that(uuid != NULL, "assert failed: uuid != NULL"); - return property_new_((void*)uuid, sizeof(Uuid) * count, BT_PROPERTY_UUIDS); -} - -void property_free(bt_property_t* property) { property_free_array(property, 1); } - -void property_free_array(bt_property_t* properties, size_t count) { - if (properties == NULL) { - return; - } - - for (size_t i = 0; i < count; ++i) { - osi_free(properties[i].val); - } - - osi_free(properties); -} - -bool property_is_addr(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_BDADDR; -} - -bool property_is_device_class(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_CLASS_OF_DEVICE; -} - -bool property_is_device_type(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_TYPE_OF_DEVICE; -} - -bool property_is_discoverable_timeout(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT; -} - -bool property_is_name(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_BDNAME; -} - -bool property_is_rssi(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_REMOTE_RSSI; -} - -bool property_is_uuids(const bt_property_t* property) { - log::assert_that(property != NULL, "assert failed: property != NULL"); - return property->type == BT_PROPERTY_UUIDS; -} - -// Convenience conversion methods to property values -const RawAddress* property_as_addr(const bt_property_t* property) { - log::assert_that(property_is_addr(property), "assert failed: property_is_addr(property)"); - return (const RawAddress*)property->val; -} - -const bt_device_class_t* property_as_device_class(const bt_property_t* property) { - log::assert_that(property_is_device_class(property), - "assert failed: property_is_device_class(property)"); - return (const bt_device_class_t*)property->val; -} - -bt_device_type_t property_as_device_type(const bt_property_t* property) { - log::assert_that(property_is_device_type(property), - "assert failed: property_is_device_type(property)"); - return *(const bt_device_type_t*)property->val; -} - -uint32_t property_as_discoverable_timeout(const bt_property_t* property) { - log::assert_that(property_is_discoverable_timeout(property), - "assert failed: property_is_discoverable_timeout(property)"); - return *(const uint32_t*)property->val; -} - -const bt_bdname_t* property_as_name(const bt_property_t* property) { - log::assert_that(property_is_name(property), "assert failed: property_is_name(property)"); - return (const bt_bdname_t*)property->val; -} - -int8_t property_as_rssi(const bt_property_t* property) { - log::assert_that(property_is_rssi(property), "assert failed: property_is_rssi(property)"); - return *(const int8_t*)property->val; -} - -const Uuid* property_as_uuids(const bt_property_t* property, size_t* count) { - log::assert_that(property_is_uuids(property), "assert failed: property_is_uuids(property)"); - *count = sizeof(Uuid) / property->len; - return (const Uuid*)property->val; -} - -static bt_property_t* property_new_(void* val, size_t len, bt_property_type_t type) { - bt_property_t* property = static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t))); - - property->val = osi_calloc(len + 1); - if (type == BT_PROPERTY_BDNAME) { - osi_strlcpy((char*)property->val, (const char*)val, len); - } else { - memcpy(property->val, val, len); - } - - property->type = type; - property->len = len; - - return property; -} diff --git a/system/btcore/test/device_class_test.cc b/system/btcore/test/device_class_test.cc deleted file mode 100644 index ed5f9581f0..0000000000 --- a/system/btcore/test/device_class_test.cc +++ /dev/null @@ -1,224 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#include "btcore/include/device_class.h" - -#include <arpa/inet.h> -#include <gtest/gtest.h> - -static ::testing::AssertionResult check_bitfield(const char* m_expr, const char* n_expr, int m, - int n) { - if (m == n) { - return ::testing::AssertionSuccess(); - } - - std::stringstream ss; - - ss.str(""); - ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << m; - std::string expected_str = ss.str(); - - ss.str(""); - ss << std::showbase << std::hex << std::setw(8) << std::setfill('0') << n; - std::string actual_str = ss.str(); - - return ::testing::AssertionFailure() - << m_expr << " and " << n_expr << " ( " << expected_str << " vs " << actual_str << " )"; -} - -class DeviceClassTest : public ::testing::Test {}; - -TEST_F(DeviceClassTest, cod_sizeof) { - uint8_t dc_stream[] = {0x00, 0x00, 0x00, 0x00}; - bt_device_class_t dc0; - device_class_from_stream(&dc0, dc_stream); - EXPECT_EQ((size_t)3, sizeof(dc0)); -} - -TEST_F(DeviceClassTest, simple) { - uint8_t dc_stream[][sizeof(bt_device_class_t)] = { - {0x00, 0x00, 0x00}, {0xff, 0xff, 0xff}, {0xaa, 0x55, 0xaa}, - {0x01, 0x23, 0x45}, {0x20, 0x07, 0x14}, - }; - - for (size_t i = 0; i < sizeof(dc_stream) / sizeof(bt_device_class_t); i++) { - bt_device_class_t dc; - device_class_from_stream(&dc, (uint8_t*)&dc_stream[i]); - - uint8_t* to_stream = (uint8_t*)&dc; - EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][0], to_stream[0]); - EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][1], to_stream[1]); - EXPECT_PRED_FORMAT2(check_bitfield, (unsigned)dc_stream[i][2], to_stream[2]); - } -} - -TEST_F(DeviceClassTest, to_stream) { - { - bt_device_class_t dc; - - uint8_t dc_stream0[] = {0x00, 0x00, 0x00, 0xaa}; - device_class_from_stream(&dc, dc_stream0); - - uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00}; - int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); - EXPECT_EQ(3, rc); - - uint32_t val = 0; - memcpy(&val, &dc, sizeof(dc)); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, val); - - EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[0]); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[1]); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00, dc_stream1[2]); - } - - { - uint8_t dc_stream0[] = {0xaa, 0x55, 0xaa, 0x55}; - uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00}; - - bt_device_class_t dc; - device_class_from_stream(&dc, dc_stream0); - - int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); - EXPECT_EQ(3, rc); - uint32_t val = 0; - memcpy(&val, &dc, sizeof(dc)); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00aa55aa, val); - - EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[0]); - EXPECT_PRED_FORMAT2(check_bitfield, 0x55, dc_stream1[1]); - EXPECT_PRED_FORMAT2(check_bitfield, 0xaa, dc_stream1[2]); - } - - { - uint8_t dc_stream0[] = {0x01, 0x23, 0x45, 0x67}; - uint8_t dc_stream1[] = {0x00, 0x00, 0x00, 0x00}; - - bt_device_class_t dc; - device_class_from_stream(&dc, dc_stream0); - - int rc = device_class_to_stream(&dc, dc_stream1, sizeof(dc_stream1)); - EXPECT_EQ(3, rc); - uint32_t val = 0; - memcpy(&val, &dc, sizeof(dc)); - EXPECT_PRED_FORMAT2(check_bitfield, 0x452301, val); - - EXPECT_PRED_FORMAT2(check_bitfield, 0x01, dc_stream1[0]); - EXPECT_PRED_FORMAT2(check_bitfield, 0x23, dc_stream1[1]); - EXPECT_PRED_FORMAT2(check_bitfield, 0x45, dc_stream1[2]); - } -} - -TEST_F(DeviceClassTest, limited_discoverable_mode) { - uint8_t dc_stream[] = {0x00, 0x00, 0x00}; - bt_device_class_t dc; - device_class_from_stream(&dc, dc_stream); - uint32_t test = 0; - memcpy(&test, &dc, sizeof(dc)); - - EXPECT_FALSE(device_class_get_limited(&dc)); - EXPECT_EQ((unsigned)0x00000000, test); - - device_class_set_limited(&dc, true); - test = 0; - memcpy(&test, &dc, sizeof(dc)); - EXPECT_TRUE(device_class_get_limited(&dc)); - EXPECT_EQ((unsigned)0x00002000, test); - - device_class_set_limited(&dc, false); - test = 0; - memcpy(&test, &dc, sizeof(dc)); - EXPECT_FALSE(device_class_get_limited(&dc)); - EXPECT_EQ((unsigned)0x00000000, test); - - device_class_set_limited(&dc, true); - test = 0; - memcpy(&test, &dc, sizeof(dc)); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00002000, test); - - device_class_set_limited(&dc, false); - test = 0; - memcpy(&test, &dc, sizeof(dc)); - EXPECT_PRED_FORMAT2(check_bitfield, 0x00000000, test); -} - -TEST_F(DeviceClassTest, equals) { - uint8_t dc_stream0[] = {0x00, 0x01, 0x02}; - uint8_t dc_stream1[] = {0x00, 0x02, 0x03}; - - bt_device_class_t dc0; - device_class_from_stream(&dc0, dc_stream0); - bt_device_class_t dc1; - device_class_from_stream(&dc1, dc_stream1); - EXPECT_FALSE(device_class_equals(&dc0, &dc1)); -} - -TEST_F(DeviceClassTest, copy) { - uint8_t dc_stream0[] = {0xaa, 0x55, 0x33}; - bt_device_class_t dc0; - device_class_from_stream(&dc0, dc_stream0); - bt_device_class_t dc1; - EXPECT_TRUE(device_class_copy(&dc1, &dc0)); - EXPECT_TRUE(device_class_equals(&dc0, &dc1)); -} - -TEST_F(DeviceClassTest, from_int) { - bt_device_class_t dc1; - int cod1 = 0x5a020c; // 5898764 - device_class_from_int(&dc1, cod1); - - uint8_t dc_stream[] = {0x0c, 0x02, 0x5a}; - bt_device_class_t dc2; - device_class_from_stream(&dc2, dc_stream); - EXPECT_TRUE(device_class_equals(&dc1, &dc2)); -} - -TEST_F(DeviceClassTest, to_int) { - bt_device_class_t dc1 = {{0x0c, 0x02, 0x5a}}; - int cod1 = device_class_to_int(&dc1); - - EXPECT_EQ(dc1._[0], 0x0c); - EXPECT_EQ(dc1._[1], 0x02); - EXPECT_EQ(dc1._[2], 0x5a); - - bt_device_class_t dc2; - uint8_t dc_stream[] = {0x0c, 0x02, 0x5a}; - device_class_from_stream(&dc2, dc_stream); - - EXPECT_EQ(dc2._[0], 0x0c); - EXPECT_EQ(dc2._[1], 0x02); - EXPECT_EQ(dc2._[2], 0x5a); - - int cod2 = device_class_to_int(&dc2); - EXPECT_EQ(cod1, cod2); - EXPECT_EQ(cod1, 0x5a020c); // 5898764 -} - -TEST_F(DeviceClassTest, endian) { - bt_device_class_t dc; - int cod1 = 0x200714; // 2098964 - device_class_from_int(&dc, cod1); - - EXPECT_EQ(dc._[0], 0x14); - EXPECT_EQ(dc._[1], 0x07); - EXPECT_EQ(dc._[2], 0x20); - - int cod2 = device_class_to_int(&dc); - EXPECT_EQ(cod1, cod2); - EXPECT_EQ(cod2, 0x200714); // 2098964 -} diff --git a/system/btcore/test/property_test.cc b/system/btcore/test/property_test.cc deleted file mode 100644 index 7477bca1a9..0000000000 --- a/system/btcore/test/property_test.cc +++ /dev/null @@ -1,269 +0,0 @@ -/****************************************************************************** - * - * Copyright 2014 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************************/ - -#include "btcore/include/property.h" - -#include <arpa/inet.h> -#include <gtest/gtest.h> - -#include "types/bluetooth/uuid.h" -#include "types/raw_address.h" - -using bluetooth::Uuid; - -class PropertyTest : public ::testing::Test {}; - -TEST_F(PropertyTest, addr) { - RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}}; - bt_property_t* property = property_new_addr(&addr0); - - EXPECT_EQ(addr0.address[0], ((uint8_t*)property->val)[0]); - EXPECT_EQ(addr0.address[1], ((uint8_t*)property->val)[1]); - EXPECT_EQ(addr0.address[2], ((uint8_t*)property->val)[2]); - EXPECT_EQ(addr0.address[3], ((uint8_t*)property->val)[3]); - EXPECT_EQ(addr0.address[4], ((uint8_t*)property->val)[4]); - EXPECT_EQ(addr0.address[5], ((uint8_t*)property->val)[5]); - EXPECT_EQ(BT_PROPERTY_BDADDR, property->type); - EXPECT_EQ((int)sizeof(RawAddress), property->len); - - const RawAddress* addr1 = property_as_addr(property); - EXPECT_EQ(addr0.address[0], addr1->address[0]); - - property_free(property); -} - -TEST_F(PropertyTest, device_class) { - bt_device_class_t dc0 = {{0x01, 0x23, 0x45}}; - bt_property_t* property = property_new_device_class(&dc0); - - EXPECT_EQ(dc0._[0], ((uint8_t*)property->val)[0]); - EXPECT_EQ(dc0._[1], ((uint8_t*)property->val)[1]); - EXPECT_EQ(dc0._[2], ((uint8_t*)property->val)[2]); - EXPECT_EQ(BT_PROPERTY_CLASS_OF_DEVICE, property->type); - EXPECT_EQ((int)sizeof(bt_device_class_t), property->len); - - const bt_device_class_t* dc1 = property_as_device_class(property); - int dc_int = device_class_to_int(dc1); - EXPECT_EQ(0x452301, dc_int); - - property_free(property); -} - -TEST_F(PropertyTest, device_type) { - bt_device_type_t dt0 = (bt_device_type_t)1; - bt_property_t* property = property_new_device_type(dt0); - - EXPECT_EQ((int)dt0, *(int*)property->val); - EXPECT_EQ(BT_PROPERTY_TYPE_OF_DEVICE, property->type); - EXPECT_EQ((int)sizeof(bt_device_type_t), property->len); - - bt_device_type_t dt1 = property_as_device_type(property); - EXPECT_EQ(1, (int)dt1); - - property_free(property); -} - -TEST_F(PropertyTest, discovery_timeout) { - uint32_t timeout0 = 12345; - bt_property_t* property = property_new_discoverable_timeout(timeout0); - - EXPECT_EQ(timeout0, *(uint32_t*)property->val); - EXPECT_EQ(BT_PROPERTY_ADAPTER_DISCOVERABLE_TIMEOUT, property->type); - EXPECT_EQ((int)sizeof(uint32_t), property->len); - - uint32_t timeout1 = property_as_discoverable_timeout(property); - EXPECT_EQ(timeout0, timeout1); - - property_free(property); -} - -TEST_F(PropertyTest, name) { - const char* name0 = "My btcore name"; - bt_property_t* property = property_new_name(name0); - - EXPECT_EQ(0, strcmp((char*)name0, (char*)property->val)); - EXPECT_EQ(BT_PROPERTY_BDNAME, property->type); - EXPECT_EQ((int)sizeof(bt_bdname_t), property->len); - - const bt_bdname_t* name1 = property_as_name(property); - EXPECT_EQ(0, strcmp((char*)name0, (char*)name1->name)); - - property_free(property); -} - -TEST_F(PropertyTest, rssi) { - int8_t rssi0 = -56; - bt_property_t* property = property_new_rssi(rssi0); - - EXPECT_EQ(*(int8_t*)property->val, rssi0); - EXPECT_EQ(BT_PROPERTY_REMOTE_RSSI, property->type); - EXPECT_EQ((int)sizeof(int8_t), property->len); - - int8_t rss1 = property_as_rssi(property); - EXPECT_EQ(rssi0, rss1); - - property_free(property); -} - -TEST_F(PropertyTest, uuids) { - Uuid uuid0 = Uuid::From128BitBE({{ - 0x00, - 0x11, - 0x22, - 0x33, - 0x44, - 0x55, - 0x66, - 0x77, - 0x88, - 0x99, - 0xaa, - 0xbb, - 0xcc, - 0xdd, - 0xee, - 0xff, - }}); - bt_property_t* property = property_new_uuids(&uuid0, 1); - - EXPECT_EQ(0, memcmp(uuid0.To128BitBE().data(), property->val, sizeof(Uuid))); - EXPECT_EQ(BT_PROPERTY_UUIDS, property->type); - EXPECT_EQ((int)sizeof(Uuid), property->len); - - size_t uuid_cnt1; - const Uuid* uuid1 = property_as_uuids(property, &uuid_cnt1); - EXPECT_EQ(uuid0, *uuid1); - - property_free(property); -} - -TEST_F(PropertyTest, copy) { - { - Uuid uuids[] = { - Uuid::From128BitBE({{ - 0x00, - 0x11, - 0x22, - 0x33, - 0x44, - 0x55, - 0x66, - 0x77, - 0x88, - 0x99, - 0xaa, - 0xbb, - 0xcc, - 0xdd, - 0xee, - 0xff, - }}), - Uuid::From128BitBE({{ - 0xf0, - 0xe1, - 0xd2, - 0xc3, - 0xf4, - 0xe5, - 0xd6, - 0xc7, - 0xf8, - 0xe9, - 0xda, - 0xcb, - 0xfc, - 0xed, - 0xde, - 0xcf, - }}), - }; - - bt_property_t* property0 = property_new_uuids(uuids, sizeof(uuids) / sizeof(Uuid)); - - bt_property_t property1; - property_copy(&property1, property0); - EXPECT_TRUE(property_equals(property0, &property1)); - - property_free(property0); - } -} - -TEST_F(PropertyTest, equals) { - { - RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}}; - bt_property_t* property0 = property_new_addr(&addr0); - - bt_device_class_t dc0 = {{0x01, 0x23, 0x45}}; - bt_property_t* property1 = property_new_device_class(&dc0); - - EXPECT_FALSE(property_equals(property0, property1)); - - property_free(property0); - property_free(property1); - } - - { - RawAddress addr = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}}; - bt_property_t* property0 = property_new_addr(&addr); - bt_property_t* property1 = property_new_addr(&addr); - - EXPECT_TRUE(property_equals(property0, property1)); - - property_free(property0); - property_free(property1); - } - - { - RawAddress addr0 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0x6}}; - bt_property_t* property0 = property_new_addr(&addr0); - - RawAddress addr1 = {{0x1, 0x2, 0x3, 0x4, 0x5, 0xff}}; - bt_property_t* property1 = property_new_addr(&addr1); - - EXPECT_FALSE(property_equals(property0, property1)); - - property_free(property0); - property_free(property1); - } - - { - const char* name0 = "My btcore name"; - bt_property_t* property0 = property_new_name(name0); - - const char* name1 = "My btcore name"; - bt_property_t* property1 = property_new_name(name1); - - EXPECT_TRUE(property_equals(property0, property1)); - - property_free(property0); - property_free(property1); - } - - { - const char* name0 = "My btcore name"; - bt_property_t* property0 = property_new_name(name0); - - const char* name1 = "My btcore name "; - bt_property_t* property1 = property_new_name(name1); - - EXPECT_FALSE(property_equals(property0, property1)); - - property_free(property0); - property_free(property1); - } -} diff --git a/system/test/suite/adapter/adapter_unittest.cc b/system/test/suite/adapter/adapter_unittest.cc index 8e2769ea10..e295cb473d 100644 --- a/system/test/suite/adapter/adapter_unittest.cc +++ b/system/test/suite/adapter/adapter_unittest.cc @@ -16,8 +16,11 @@ * ******************************************************************************/ +#include <bluetooth/log.h> + #include "adapter/bluetooth_test.h" -#include "btcore/include/property.h" +#include "osi/include/allocator.h" +#include "osi/include/compat.h" #include "types/bt_transport.h" #include "types/raw_address.h" @@ -58,6 +61,52 @@ TEST_F(BluetoothTest, AdapterRepeatedEnableDisable) { } } +static bt_property_t* property_new_name(const char* name) { + bluetooth::log::assert_that(name != NULL, "assert failed: name != NULL"); + bt_property_t* property = static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t))); + + property->val = osi_calloc(sizeof(bt_bdname_t) + 1); + osi_strlcpy((char*)property->val, name, sizeof(bt_bdname_t)); + + property->type = BT_PROPERTY_BDNAME; + property->len = sizeof(bt_bdname_t); + + return property; +} + +static void property_free(bt_property_t* property) { + if (property == NULL) { + return; + } + + osi_free(property->val); + osi_free(property); +} + +static const bt_bdname_t* property_as_name(const bt_property_t* property) { + bluetooth::log::assert_that(property->type == BT_PROPERTY_BDNAME, + "assert failed: property_is_name(property)"); + return (const bt_bdname_t*)property->val; +} + +static bool property_equals(const bt_property_t* p1, const bt_property_t* p2) { + if (!p1 || !p2 || p1->type != p2->type) { + return false; + } + + if (p1->type == BT_PROPERTY_BDNAME && p1->len != p2->len) { + const bt_property_t *shorter = p1, *longer = p2; + if (p1->len > p2->len) { + shorter = p2; + longer = p1; + } + return strlen((const char*)longer->val) == (size_t)shorter->len && + !memcmp(longer->val, shorter->val, shorter->len); + } + + return p1->len == p2->len && !memcmp(p1->val, p2->val, p1->len); +} + TEST_F(BluetoothTest, AdapterSetGetName) { bt_property_t* new_name = property_new_name("BluetoothTestName1"); diff --git a/system/test/suite/adapter/bluetooth_test.cc b/system/test/suite/adapter/bluetooth_test.cc index 4855b7a72d..490675a9f6 100644 --- a/system/test/suite/adapter/bluetooth_test.cc +++ b/system/test/suite/adapter/bluetooth_test.cc @@ -19,10 +19,11 @@ #include "adapter/bluetooth_test.h" #include <binder/ProcessState.h> +#include <bluetooth/log.h> #include <mutex> -#include "btcore/include/property.h" +#include "osi/include/allocator.h" #include "types/raw_address.h" extern bt_interface_t bluetoothInterface; @@ -35,6 +36,31 @@ namespace bttest { static BluetoothTest* instance = nullptr; +static void property_free_array(bt_property_t* properties, size_t count) { + if (properties == NULL) { + return; + } + + for (size_t i = 0; i < count; ++i) { + osi_free(properties[i].val); + } + + osi_free(properties); +} + +static bt_property_t* property_copy_array(const bt_property_t* properties, size_t count) { + bluetooth::log::assert_that(properties != NULL, "assert failed: properties != NULL"); + bt_property_t* clone = static_cast<bt_property_t*>(osi_calloc(sizeof(bt_property_t) * count)); + + memcpy(&clone[0], &properties[0], sizeof(bt_property_t) * count); + for (size_t i = 0; i < count; ++i) { + clone[i].val = osi_calloc(clone[i].len); + memcpy(clone[i].val, properties[i].val, clone[i].len); + } + + return clone; +} + void AdapterStateChangedCallback(bt_state_t new_state) { instance->state_ = new_state; semaphore_post(instance->adapter_state_changed_callback_sem_); |