Merge "Bluetooth: Generate a random address"

This commit is contained in:
Treehugger Robot 2017-02-06 18:07:12 +00:00 committed by Gerrit Code Review
commit 812f8ad3ab
2 changed files with 38 additions and 2 deletions

View file

@ -83,6 +83,34 @@ bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
valid_bda = true; 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; return valid_bda;
} }

View file

@ -219,10 +219,18 @@ TEST_F(BluetoothAddressTest, get_local_address) {
EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(BluetoothAddress::get_local_address(address));
EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); 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); FileWriteString(kAddrPath, kZeros);
EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); 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. // Factory property contains an address.
EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0);