Delete peer keys on key missing and retry.

On BLE_ERR_PINKEY_MISSING, delete the peer keys from the store and
retry the connection.
This commit is contained in:
Guo-Rong 2020-08-20 09:57:20 -06:00 committed by h2zero
parent 4c751657ab
commit 88562bb310

View file

@ -238,6 +238,10 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttibutes) {
*/
bool NimBLEClient::secureConnection() {
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
int retryCount = 1;
do {
m_pTaskData = &taskData;
int rc = NimBLEDevice::startSecurity(m_conn_id);
@ -247,6 +251,7 @@ bool NimBLEClient::secureConnection() {
}
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
} while (taskData.rc == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING) && retryCount--);
if(taskData.rc != 0){
return false;
@ -822,12 +827,15 @@ uint16_t NimBLEClient::getMTU() {
return 0;
}
if(event->enc_change.status == 0) {
if(event->enc_change.status == 0 || event->enc_change.status == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING)) {
struct ble_gap_conn_desc desc;
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
assert(rc == 0);
if(NimBLEDevice::m_securityCallbacks != nullptr) {
if (event->enc_change.status == BLE_HS_HCI_ERR(BLE_ERR_PINKEY_MISSING)) {
// Key is missing, try deleting.
ble_store_util_delete_peer(&desc.peer_id_addr);
} else if(NimBLEDevice::m_securityCallbacks != nullptr) {
NimBLEDevice::m_securityCallbacks->onAuthenticationComplete(&desc);
} else {
client->m_pClientCallbacks->onAuthenticationComplete(&desc);