Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 895f0b34 authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

legacy: Add tBLE_ADDR_TYPE test and functionality am: 38d6c984 am: 9fe49db0 am: 34b5af01

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2030645

Change-Id: I7301ee08a30990935784108b0596ac90d489bb68
parents 9fe73c99 34b5af01
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -50,11 +50,15 @@ 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",
    ],
    shared_libs: [
        "android.hardware.bluetooth@1.0",
+30 −0
Original line number Diff line number Diff line
@@ -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;
+113 −0
Original line number Diff line number Diff line
/*
 * 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);
}