Skip to content

Commit

Permalink
component/bt: refactor ble random address setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewenxiang committed Dec 3, 2020
1 parent ea678c3 commit dfefe7b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/bt/bluedroid/api/include/api/esp_gap_ble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params);
esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_data_length);

/**
* @brief This function sets the random address for the application
* @brief This function sets the static Random Address and Non-Resolvable Private Address for the application
*
* @param[in] rand_addr: the random address which should be setting
*
Expand Down
32 changes: 24 additions & 8 deletions components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,22 +856,38 @@ static void btc_ble_set_rand_addr (BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *
• The two most significant bits of the address shall be equal to 1
• All bits of the random part of the address shall not be equal to 1
• All bits of the random part of the address shall not be equal to 0
A non-resolvable private address is a 48-bit randomly generated address and shall meet the following requirements:
• The two most significant bits of the address shall be equal to 0
• All bits of the random part of the address shall not be equal to 1
• All bits of the random part of the address shall not be equal to 0
*/
BD_ADDR invalid_rand_addr_a, invalid_rand_addr_b;
memset(invalid_rand_addr_a, 0xff, sizeof(BD_ADDR));
memset(invalid_rand_addr_b, 0x00, sizeof(BD_ADDR));
invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK
&& memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0
&& memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0){
BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback);
} else {

if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK) {
invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0 && memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0) {
BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback);
} else {
btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
BTC_TRACE_ERROR("Invalid static random address, the high bit should be 0b11, bits of the random part shall not be all 1 or 0");
}
} else if ((rand_addr[0] | BT_NON_RPA_MASK) == BT_NON_RPA_MASK) {
invalid_rand_addr_a[0] = invalid_rand_addr_a[0] & BT_NON_RPA_MASK;
if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0 && memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0) {
BTA_DmSetRandAddress(rand_addr, btc_set_rand_addr_callback);
} else {
btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
BTC_TRACE_ERROR("Invalid non-resolvable private address, the high bit should be 0b00, bits of the random part shall not be all 1 or 0");
}
}else {
btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
BTC_TRACE_ERROR("Invalid random address, the high bit should be 0b11, bits of the random part shall not be all 1 or 0");
BTC_TRACE_ERROR("Invalid random address type");
}
} else {
btc_set_rand_addr_callback(BTM_INVALID_STATIC_RAND_ADDR);
BTC_TRACE_ERROR("Invalid random addressm, the address value is NULL");
BTC_TRACE_ERROR("Invalid address, the address value is NULL");
}
}

Expand Down
1 change: 1 addition & 0 deletions components/bt/bluedroid/stack/include/stack/bt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef bool BOOLEAN;
#define BT_EVT_MASK 0xFF00
#define BT_SUB_EVT_MASK 0x00FF
#define BT_STATIC_RAND_ADDR_MASK 0xC0
#define BT_NON_RPA_MASK 0x3F
/* To Bluetooth Upper Layers */
/************************************/
#define BT_EVT_TO_BTU_L2C_EVT 0x0900 /* L2CAP event */
Expand Down

0 comments on commit dfefe7b

Please sign in to comment.