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

Commit 812f8ad3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Bluetooth: Generate a random address"

parents 5d6712c7 11f10bae
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -83,6 +83,34 @@ bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
    valid_bda = true;
  }

  /* Generate new BDA if necessary */
  if (!valid_bda) {
    char bdstr[kStringLength + 1];

    /* No autogen BDA. Generate one now. */
    local_addr[0] = 0x22;
    local_addr[1] = 0x22;
    local_addr[2] = (uint8_t)rand();
    local_addr[3] = (uint8_t)rand();
    local_addr[4] = (uint8_t)rand();
    local_addr[5] = (uint8_t)rand();

    /* Convert to ascii, and store as a persistent property */
    bytes_to_string(local_addr, bdstr);

    ALOGE("%s: No preset BDA! Generating BDA: %s for prop %s", __func__,
          (char*)bdstr, PERSIST_BDADDR_PROPERTY);
    ALOGE("%s: This is a bug in the platform!  Please fix!", __func__);

    if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) {
      ALOGE("%s: Failed to set random BDA in prop %s", __func__,
            PERSIST_BDADDR_PROPERTY);
      valid_bda = false;
    } else {
      valid_bda = true;
    }
  }

  return valid_bda;
}

+10 −2
Original line number Diff line number Diff line
@@ -219,10 +219,18 @@ TEST_F(BluetoothAddressTest, get_local_address) {
  EXPECT_TRUE(BluetoothAddress::get_local_address(address));
  EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0);

  // File contains a zero address.
  // File contains a zero address.  A random address will be generated.
  FileWriteString(kAddrPath, kZeros);
  EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0);
  EXPECT_FALSE(BluetoothAddress::get_local_address(address));
  EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0);
  EXPECT_TRUE(BluetoothAddress::get_local_address(address));
  EXPECT_TRUE(memcmp(address, kZeros_bytes, BluetoothAddress::kBytes) != 0);
  char prop[PROP_VALUE_MAX] = "Before reading";
  EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) ==
              BluetoothAddress::kStringLength);
  char address_str[BluetoothAddress::kStringLength + 1];
  BluetoothAddress::bytes_to_string(address, address_str);
  EXPECT_TRUE(memcmp(address_str, prop, BluetoothAddress::kStringLength) == 0);

  // Factory property contains an address.
  EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0);