From 52b551c3a3f7cfab580467730225e271be5a8528 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 8 Nov 2016 15:13:36 +0100 Subject: [PATCH] sm: fix Random Address generation when enabled before HCI is working --- src/ble/sm.c | 6 +++++- src/ble/sm.h | 1 - src/hci.c | 6 +++++- src/hci.h | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ble/sm.c b/src/ble/sm.c index 42b86e85f..d1c6f9d0d 100644 --- a/src/ble/sm.c +++ b/src/ble/sm.c @@ -2890,13 +2890,17 @@ static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint le_device_db_set_local_bd_addr(local_bd_addr); dkg_state = sm_persistent_irk_ready ? DKG_CALC_DHK : DKG_CALC_IRK; - rau_state = RAU_IDLE; #ifdef ENABLE_LE_SECURE_CONNECTIONS if (!sm_have_ec_keypair){ setup->sm_passkey_bit = 0; ec_key_generation_state = EC_KEY_GENERATION_ACTIVE; } #endif + // trigger Random Address generation if requested before + rau_state = RAU_IDLE; + if (gap_random_adress_type != GAP_RANDOM_ADDRESS_TYPE_OFF) { + gap_random_address_trigger(); + } sm_run(); } break; diff --git a/src/ble/sm.h b/src/ble/sm.h index bd9cd36e9..11ebd2f3c 100644 --- a/src/ble/sm.h +++ b/src/ble/sm.h @@ -283,7 +283,6 @@ int sm_le_device_index(hci_con_handle_t con_handle ); /** * @brief Set Elliptic Key Public/Private Keypair - * @note Creating a new key pair requires about 4600 bytes additional when used with MBEDTLS EC * @note Using the same key for more than one device is not recommended. * @param qx 32 bytes * @param qy 32 bytes diff --git a/src/hci.c b/src/hci.c index a5aff9c64..7f7fd9f0f 100644 --- a/src/hci.c +++ b/src/hci.c @@ -2026,6 +2026,7 @@ static void hci_state_reset(void){ // LE hci_stack->adv_addr_type = 0; + hci_stack->le_advertisements_random_address_set = 0; memset(hci_stack->adv_address, 0, 6); hci_stack->le_scanning_state = LE_SCAN_IDLE; hci_stack->le_scan_type = 0xff; @@ -2546,7 +2547,9 @@ static void hci_run(void){ hci_stack->le_scan_response_data); return; } - if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ + // Random address needs to be set before enabling advertisements + if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE) + && (hci_stack->le_advertisements_own_address_type == 0 || hci_stack->le_advertisements_random_address_set)){ hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; hci_send_cmd(&hci_le_set_advertise_enable, 1); return; @@ -2979,6 +2982,7 @@ int hci_send_cmd_packet(uint8_t *packet, int size){ hci_stack->adv_addr_type = packet[8]; } if (IS_COMMAND(packet, hci_le_set_random_address)){ + hci_stack->le_advertisements_random_address_set = 1; reverse_bd_addr(&packet[3], hci_stack->adv_address); } if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ diff --git a/src/hci.h b/src/hci.h index 5de837c2b..ef76e7755 100644 --- a/src/hci.h +++ b/src/hci.h @@ -642,7 +642,7 @@ typedef struct { /* buffer for scan enable cmd - 0xff no change */ uint8_t new_scan_enable_value; - uint16_t sco_voice_setting; + uint16_t sco_voice_setting; uint8_t loopback_mode; @@ -672,6 +672,7 @@ typedef struct { uint8_t le_advertisements_active; uint8_t le_advertisements_enabled; uint8_t le_advertisements_todo; + uint8_t le_advertisements_random_address_set; uint16_t le_advertisements_interval_min; uint16_t le_advertisements_interval_max;