diff options
-rw-r--r-- | system/types/Android.bp | 6 | ||||
-rw-r--r-- | system/types/ble_address_with_type.h | 30 | ||||
-rw-r--r-- | system/types/test/ble_address_with_type_unittest.cc | 113 |
3 files changed, 148 insertions, 1 deletions
diff --git a/system/types/Android.bp b/system/types/Android.bp index 8b9c4efed1..745c61f6d2 100644 --- a/system/types/Android.bp +++ b/system/types/Android.bp @@ -48,10 +48,14 @@ cc_test { name: "net_test_types", test_suites: ["device-tests"], defaults: ["fluoride_defaults"], + include_dirs: [ + "packages/modules/Bluetooth/system", + ], host_supported: true, srcs: [ + "test/ble_address_with_type_unittest.cc", + "test/bluetooth/uuid_unittest.cc", "test/class_of_device_unittest.cc", "test/raw_address_unittest.cc", - "test/bluetooth/uuid_unittest.cc", ], } diff --git a/system/types/ble_address_with_type.h b/system/types/ble_address_with_type.h index 850abb900c..ecd62a85fb 100644 --- a/system/types/ble_address_with_type.h +++ b/system/types/ble_address_with_type.h @@ -45,8 +45,38 @@ inline std::string AddressTypeText(tBLE_ADDR_TYPE type) { } #endif // __cplusplus +inline bool is_ble_addr_type_valid(uint8_t raw_type) { return raw_type < 4; } + +inline bool is_ble_addr_type_known(tBLE_ADDR_TYPE type) { + switch (type) { + case BLE_ADDR_PUBLIC: + case BLE_ADDR_PUBLIC_ID: + case BLE_ADDR_RANDOM: + case BLE_ADDR_RANDOM_ID: + return true; + default: + return false; + } +} + +inline tBLE_ADDR_TYPE to_ble_addr_type(uint8_t raw_type) { + return (tBLE_ADDR_TYPE)raw_type; +} +inline uint8_t from_ble_addr_type(tBLE_ADDR_TYPE type) { return (uint8_t)type; } + /* BLE ADDR type ID bit */ #define BLE_ADDR_TYPE_ID_BIT 0x02 +inline bool is_identity_type(const tBLE_ADDR_TYPE& type) { + return type & BLE_ADDR_TYPE_ID_BIT; +} + +#define STREAM_TO_BLE_ADDR_TYPE(type, p) \ + { \ + (type) = (tBLE_ADDR_TYPE)(*(p)); \ + (p) += sizeof(tBLE_ADDR_TYPE); \ + } +#define BLE_ADDR_TYPE_TO_STREAM(p, type) \ + { *(p)++ = (tBLE_ADDR_TYPE)(type); } #ifdef __cplusplus constexpr uint8_t kBleAddressPublicDevice = BLE_ADDR_PUBLIC; diff --git a/system/types/test/ble_address_with_type_unittest.cc b/system/types/test/ble_address_with_type_unittest.cc new file mode 100644 index 0000000000..5332d99e28 --- /dev/null +++ b/system/types/test/ble_address_with_type_unittest.cc @@ -0,0 +1,113 @@ +/* + * Copyright 2022 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 "types/ble_address_with_type.h" + +#include <gtest/gtest.h> + +TEST(BleAddressWithTypeTest, to_ble_addr_type) { + for (unsigned i = 0; i < 0xff + 1; i++) { + switch (to_ble_addr_type((uint8_t)i)) { + case BLE_ADDR_PUBLIC: + ASSERT_TRUE(i == 0); + break; + case BLE_ADDR_RANDOM: + ASSERT_TRUE(i == 1); + break; + case BLE_ADDR_PUBLIC_ID: + ASSERT_TRUE(i == 2); + break; + case BLE_ADDR_RANDOM_ID: + ASSERT_TRUE(i == 3); + break; + case BLE_ADDR_ANONYMOUS: + ASSERT_TRUE(i == 0xff); + break; + default: + ASSERT_TRUE(i > 3 && i != 0xff); + break; + } + } +} + +TEST(BleAddressWithTypeTest, from_ble_addr_type) { + struct type_table_t { + tBLE_ADDR_TYPE type; + uint8_t value; + } type_table[] = { + {BLE_ADDR_PUBLIC, 0}, {BLE_ADDR_RANDOM, 1}, + {BLE_ADDR_PUBLIC_ID, 2}, {BLE_ADDR_RANDOM_ID, 3}, + {BLE_ADDR_ANONYMOUS, 0xff}, + }; + + for (unsigned i = 0; i < sizeof(type_table) / sizeof(type_table[0]); i++) { + ASSERT_TRUE(from_ble_addr_type(type_table[i].type) == type_table[i].value); + } +} + +TEST(BleAddressWithTypeTest, STREAM_TO_BLE_ADDR_TYPE) { + uint8_t buf[256] = {0x00, 0x01, 0x02, 0x03}; + buf[10] = 0x01; + buf[20] = 0x02; + buf[30] = 0x03; + buf[127] = 0xff; + buf[255] = 0xff; + + uint8_t* p = buf; + for (unsigned i = 0; i < sizeof(buf); i++) { + tBLE_ADDR_TYPE type; + STREAM_TO_BLE_ADDR_TYPE(type, p); + switch (i) { + case 0: + ASSERT_TRUE(type == BLE_ADDR_PUBLIC); + break; + case 1: + case 10: + ASSERT_TRUE(type == BLE_ADDR_RANDOM); + break; + case 2: + case 20: + ASSERT_TRUE(type == BLE_ADDR_PUBLIC_ID); + break; + case 3: + case 30: + ASSERT_TRUE(type == BLE_ADDR_RANDOM_ID); + break; + case 127: + case 255: + ASSERT_TRUE(type == BLE_ADDR_ANONYMOUS); + break; + default: + ASSERT_TRUE(type == BLE_ADDR_PUBLIC); + break; + } + } +} + +TEST(BleAddressWithTypeTest, BLE_ADDR_TYPE_TO_STREAM) { + uint8_t buf[256] = {0}; + uint8_t* p = buf; + + BLE_ADDR_TYPE_TO_STREAM(p, BLE_ADDR_PUBLIC); + BLE_ADDR_TYPE_TO_STREAM(p, BLE_ADDR_RANDOM); + BLE_ADDR_TYPE_TO_STREAM(p, BLE_ADDR_PUBLIC_ID); + BLE_ADDR_TYPE_TO_STREAM(p, BLE_ADDR_RANDOM_ID); + BLE_ADDR_TYPE_TO_STREAM(p, BLE_ADDR_ANONYMOUS); + + const uint8_t exp[] = {0x0, 0x1, 0x2, 0x3, 0xff}; + ASSERT_EQ(*exp, *buf); + ASSERT_EQ(5, p - buf); +} |