From 02ba2848a8b0b3f8bbdebd01a8f6e6a6ed530f3a Mon Sep 17 00:00:00 2001 From: paul-szczepanek-arm <33840200+paul-szczepanek-arm@users.noreply.github.com> Date: Mon, 26 Mar 2018 17:48:32 +0100 Subject: [PATCH] avoid recalculating oob fi already calculating --- .../FEATURE_BLE/ble/pal/PalSecurityManager.h | 3 +- .../source/generic/GenericSecurityManager.cpp | 36 ++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/features/FEATURE_BLE/ble/pal/PalSecurityManager.h b/features/FEATURE_BLE/ble/pal/PalSecurityManager.h index f5ac38497e..a8bccc8142 100644 --- a/features/FEATURE_BLE/ble/pal/PalSecurityManager.h +++ b/features/FEATURE_BLE/ble/pal/PalSecurityManager.h @@ -974,7 +974,8 @@ public: ) = 0; /** - * Generate local OOB data to be sent to the application which sends it to the peer.p + * Generate local OOB data to be sent to the application which sends it to the peer. + * @retval BLE_ERROR_NONE On success, else an error code indicating reason for failure */ virtual ble_error_t generate_secure_connections_oob() = 0; diff --git a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp index a599868770..a6606e9d64 100644 --- a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp +++ b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp @@ -510,17 +510,35 @@ ble_error_t GenericSecurityManager::generateOOB( const address_t *address ) { /* legacy pairing */ - _oob_temporary_key_creator_address = *address; - get_random_data(_oob_temporary_key.buffer(), 16); + ble_error_t status = get_random_data(_oob_temporary_key.buffer(), 16); - eventHandler->legacyPairingOobGenerated( - &_oob_temporary_key_creator_address, - &_oob_temporary_key - ); + if (status == BLE_ERROR_NONE) { + _oob_temporary_key_creator_address = *address; - /* secure connections */ - _oob_local_address = *address; - _pal.generate_secure_connections_oob(); + eventHandler->legacyPairingOobGenerated( + &_oob_temporary_key_creator_address, + &_oob_temporary_key + ); + } else { + return status; + } + + /* Secure connections. Avoid generating if we're already waiting for it. + * If a local random is set to 0 it means we're already calculating + * unless the address is invalid which means we've never done so yet */ + if (_oob_local_random != 0 || _oob_local_address == address_t()) { + status = _pal.generate_secure_connections_oob(); + + if (status == BLE_ERROR_NONE) { + _oob_local_address = *address; + /* this will be updated when calculation completes */ + _oob_local_random = 0; + } else if (status != BLE_ERROR_NOT_IMPLEMENTED) { + return status; + } + } else { + return BLE_STACK_BUSY; + } return BLE_ERROR_NONE; }