From 6b930d6449b55d50c2dd371af9279668c667a40b Mon Sep 17 00:00:00 2001 From: Paul Szczepanek Date: Wed, 9 Jun 2021 14:02:38 +0100 Subject: [PATCH] translate adv set error codes and only act on successful stop --- .../FEATURE_BLE/source/generic/GapImpl.cpp | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp index c6acf530cf..fe02f45410 100644 --- a/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp +++ b/connectivity/FEATURE_BLE/source/generic/GapImpl.cpp @@ -3481,16 +3481,34 @@ void Gap::on_advertising_set_terminated( to_string(status), number_of_completed_extended_advertising_events); - _active_sets.clear(advertising_handle); - _pending_sets.clear(advertising_handle); + ble_error_t error_code = BLE_ERROR_UNSPECIFIED; + bool connected = false; - // If this is part of the address refresh start advertising again. - if (_address_refresh_sets.get(advertising_handle) && !connection_handle) { - _address_refresh_sets.clear(advertising_handle); - tr_info("Part of the address refresh, restarting advertising"); - startAdvertising(advertising_handle); - _adv_started_from_refresh.set(advertising_handle); - return; + /* translate HCI error into BLE API error code */ + if (status == hci_error_code_t::SUCCESS) { + error_code = BLE_ERROR_NONE; + /* self cancelled set will have the handle set to invalid value */ + if (connection_handle != DM_CONN_ID_NONE) { + connected = true; + } + } else if (status == hci_error_code_t::ADVERTISING_TIMEOUT) { + error_code = BLE_ERROR_TIMEOUT; + } else if (status == hci_error_code_t::LIMIT_REACHED) { + error_code = BLE_ERROR_LIMIT_REACHED; + } + + if (error_code != BLE_ERROR_UNSPECIFIED) { + _active_sets.clear(advertising_handle); + _pending_sets.clear(advertising_handle); + + // If this is part of the address refresh start advertising again. + if (_address_refresh_sets.get(advertising_handle) && !connection_handle) { + _address_refresh_sets.clear(advertising_handle); + tr_info("Part of the address refresh, restarting advertising"); + startAdvertising(advertising_handle); + _adv_started_from_refresh.set(advertising_handle); + return; + } } /* postpone as other events may still be pending */ @@ -3509,7 +3527,8 @@ void Gap::on_advertising_set_terminated( advertising_handle, connection_handle, number_of_completed_extended_advertising_events, - status == hci_error_code_t::SUCCESS + connected, + error_code ) ); }