Skip to content

Commit 608ad33

Browse files
return error when not initialised
1 parent a8ac925 commit 608ad33

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ ble_error_t GenericSecurityManager::reset(void) {
123123
}
124124

125125
ble_error_t GenericSecurityManager::preserveBondingStateOnReset(bool enabled) {
126-
MBED_ASSERT(_db);
126+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
127127
_db->set_restore(enabled);
128128
return BLE_ERROR_NONE;
129129
}
@@ -133,13 +133,13 @@ ble_error_t GenericSecurityManager::preserveBondingStateOnReset(bool enabled) {
133133
//
134134

135135
ble_error_t GenericSecurityManager::purgeAllBondingState(void) {
136-
MBED_ASSERT(_db);
136+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
137137
_db->clear_entries();
138138
return BLE_ERROR_NONE;
139139
}
140140

141141
ble_error_t GenericSecurityManager::generateWhitelistFromBondTable(Gap::Whitelist_t *whitelist) const {
142-
MBED_ASSERT(_db);
142+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
143143
if (eventHandler) {
144144
_db->generate_whitelist_from_bond_table(
145145
mbed::callback(eventHandler, &::SecurityManager::EventHandler::whitelistFromBondTable),
@@ -154,6 +154,7 @@ ble_error_t GenericSecurityManager::generateWhitelistFromBondTable(Gap::Whitelis
154154
//
155155

156156
ble_error_t GenericSecurityManager::requestPairing(connection_handle_t connection) {
157+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
157158
ControlBlock_t *cb = get_control_block(connection);
158159
if (!cb) {
159160
return BLE_ERROR_INVALID_PARAM;
@@ -206,6 +207,7 @@ ble_error_t GenericSecurityManager::requestPairing(connection_handle_t connectio
206207
}
207208

208209
ble_error_t GenericSecurityManager::acceptPairingRequest(connection_handle_t connection) {
210+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
209211
ControlBlock_t *cb = get_control_block(connection);
210212
if (!cb) {
211213
return BLE_ERROR_INVALID_PARAM;
@@ -263,10 +265,12 @@ ble_error_t GenericSecurityManager::acceptPairingRequest(connection_handle_t con
263265
}
264266

265267
ble_error_t GenericSecurityManager::cancelPairingRequest(connection_handle_t connection) {
268+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
266269
return _pal.cancel_pairing(connection, pairing_failure_t::UNSPECIFIED_REASON);
267270
}
268271

269272
ble_error_t GenericSecurityManager::setPairingRequestAuthorisation(bool required) {
273+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
270274
_pairing_authorisation_required = required;
271275
return BLE_ERROR_NONE;
272276
}
@@ -289,24 +293,28 @@ ble_error_t GenericSecurityManager::getSecureConnectionsSupport(bool *enabled) {
289293
//
290294

291295
ble_error_t GenericSecurityManager::setIoCapability(SecurityIOCapabilities_t iocaps) {
296+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
292297
return _pal.set_io_capability((io_capability_t::type) iocaps);
293298
}
294299

295300
ble_error_t GenericSecurityManager::setDisplayPasskey(const Passkey_t passkey) {
301+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
296302
return _pal.set_display_passkey(PasskeyAscii::to_num(passkey));
297303
}
298304

299305
ble_error_t GenericSecurityManager::setAuthenticationTimeout(
300306
connection_handle_t connection,
301307
uint32_t timeout_in_ms
302308
) {
309+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
303310
return _pal.set_authentication_timeout(connection, timeout_in_ms / 10);
304311
}
305312

306313
ble_error_t GenericSecurityManager::getAuthenticationTimeout(
307314
connection_handle_t connection,
308315
uint32_t *timeout_in_ms
309316
) {
317+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
310318
uint16_t timeout_in_10ms;
311319
ble_error_t status = _pal.get_authentication_timeout(connection, timeout_in_10ms);
312320
*timeout_in_ms = 10 * timeout_in_10ms;
@@ -317,6 +325,7 @@ ble_error_t GenericSecurityManager::setLinkSecurity(
317325
connection_handle_t connection,
318326
SecurityMode_t securityMode
319327
) {
328+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
320329
ControlBlock_t *cb = get_control_block(connection);
321330
if (!cb) {
322331
return BLE_ERROR_INVALID_PARAM;
@@ -348,6 +357,7 @@ ble_error_t GenericSecurityManager::setLinkSecurity(
348357
}
349358

350359
ble_error_t GenericSecurityManager::setKeypressNotification(bool enabled) {
360+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
351361
_default_authentication.set_keypress_notification(enabled);
352362
return BLE_ERROR_NONE;
353363
}
@@ -356,7 +366,7 @@ ble_error_t GenericSecurityManager::enableSigning(
356366
connection_handle_t connection,
357367
bool enabled
358368
) {
359-
MBED_ASSERT(_db);
369+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
360370
ControlBlock_t *cb = get_control_block(connection);
361371
if (!cb) {
362372
return BLE_ERROR_INVALID_PARAM;
@@ -406,7 +416,7 @@ ble_error_t GenericSecurityManager::getLinkEncryption(
406416
connection_handle_t connection,
407417
link_encryption_t *encryption
408418
) {
409-
MBED_ASSERT(_db);
419+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
410420
ControlBlock_t *cb = get_control_block(connection);
411421
if (!cb) {
412422
return BLE_ERROR_INVALID_PARAM;
@@ -440,7 +450,7 @@ ble_error_t GenericSecurityManager::setLinkEncryption(
440450
connection_handle_t connection,
441451
link_encryption_t encryption
442452
) {
443-
MBED_ASSERT(_db);
453+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
444454
ControlBlock_t *cb = get_control_block(connection);
445455
if (!cb) {
446456
return BLE_ERROR_INVALID_PARAM;
@@ -511,7 +521,7 @@ ble_error_t GenericSecurityManager::getEncryptionKeySize(
511521
connection_handle_t connection,
512522
uint8_t *size
513523
) {
514-
MBED_ASSERT(_db);
524+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
515525
ControlBlock_t *cb = get_control_block(connection);
516526
if (!cb) {
517527
return BLE_ERROR_INVALID_PARAM;
@@ -530,6 +540,7 @@ ble_error_t GenericSecurityManager::setEncryptionKeyRequirements(
530540
uint8_t minimumByteSize,
531541
uint8_t maximumByteSize
532542
) {
543+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
533544
return _pal.set_encryption_key_requirements(minimumByteSize, maximumByteSize);
534545
}
535546

@@ -538,7 +549,7 @@ ble_error_t GenericSecurityManager::setEncryptionKeyRequirements(
538549
//
539550

540551
ble_error_t GenericSecurityManager::getSigningKey(connection_handle_t connection, bool authenticated) {
541-
MBED_ASSERT(_db);
552+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
542553
ControlBlock_t *cb = get_control_block(connection);
543554
if (!cb) {
544555
return BLE_ERROR_INVALID_PARAM;
@@ -576,6 +587,7 @@ ble_error_t GenericSecurityManager::getSigningKey(connection_handle_t connection
576587
//
577588

578589
ble_error_t GenericSecurityManager::setPrivateAddressTimeout(uint16_t timeout_in_seconds) {
590+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
579591
return _pal.set_private_address_timeout(timeout_in_seconds);
580592
}
581593

@@ -584,7 +596,7 @@ ble_error_t GenericSecurityManager::setPrivateAddressTimeout(uint16_t timeout_in
584596
//
585597

586598
ble_error_t GenericSecurityManager::requestAuthentication(connection_handle_t connection) {
587-
MBED_ASSERT(_db);
599+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
588600
ControlBlock_t *cb = get_control_block(connection);
589601
if (!cb) {
590602
return BLE_ERROR_INVALID_PARAM;
@@ -619,6 +631,7 @@ ble_error_t GenericSecurityManager::requestAuthentication(connection_handle_t co
619631
ble_error_t GenericSecurityManager::generateOOB(
620632
const address_t *address
621633
) {
634+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
622635
/* legacy pairing */
623636
ble_error_t status = get_random_data(_oob_temporary_key.data(), 16);
624637

@@ -658,6 +671,7 @@ ble_error_t GenericSecurityManager::setOOBDataUsage(
658671
bool useOOB,
659672
bool OOBProvidesMITM
660673
) {
674+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
661675
ControlBlock_t *cb = get_control_block(connection);
662676
if (!cb) {
663677
return BLE_ERROR_INVALID_PARAM;
@@ -677,13 +691,15 @@ ble_error_t GenericSecurityManager::confirmationEntered(
677691
connection_handle_t connection,
678692
bool confirmation
679693
) {
694+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
680695
return _pal.confirmation_entered(connection, confirmation);
681696
}
682697

683698
ble_error_t GenericSecurityManager::passkeyEntered(
684699
connection_handle_t connection,
685700
Passkey_t passkey
686701
) {
702+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
687703
return _pal.passkey_request_reply(
688704
connection,
689705
PasskeyAscii::to_num(passkey)
@@ -694,14 +710,15 @@ ble_error_t GenericSecurityManager::sendKeypressNotification(
694710
connection_handle_t connection,
695711
Keypress_t keypress
696712
) {
713+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
697714
return _pal.send_keypress_notification(connection, keypress);
698715
}
699716

700717
ble_error_t GenericSecurityManager::legacyPairingOobReceived(
701718
const address_t *address,
702719
const oob_tk_t *tk
703720
) {
704-
MBED_ASSERT(_db);
721+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
705722
if (address && tk) {
706723
ControlBlock_t *cb = get_control_block(*address);
707724
if (!cb) {
@@ -736,6 +753,7 @@ ble_error_t GenericSecurityManager::oobReceived(
736753
const oob_lesc_value_t *random,
737754
const oob_confirm_t *confirm
738755
) {
756+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
739757
if (address && random && confirm) {
740758
_oob_peer_address = *address;
741759
_oob_peer_random = *random;
@@ -751,7 +769,7 @@ ble_error_t GenericSecurityManager::oobReceived(
751769
//
752770

753771
ble_error_t GenericSecurityManager::init_signing() {
754-
MBED_ASSERT(_db);
772+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
755773
const csrk_t *pcsrk = _db->get_local_csrk();
756774
sign_count_t local_sign_counter = _db->get_local_sign_counter();
757775

@@ -791,6 +809,7 @@ ble_error_t GenericSecurityManager::get_random_data(uint8_t *buffer, size_t size
791809
}
792810

793811
ble_error_t GenericSecurityManager::slave_security_request(connection_handle_t connection) {
812+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
794813
ControlBlock_t *cb = get_control_block(connection);
795814
if (!cb) {
796815
return BLE_ERROR_INVALID_PARAM;
@@ -801,7 +820,7 @@ ble_error_t GenericSecurityManager::slave_security_request(connection_handle_t c
801820
}
802821

803822
ble_error_t GenericSecurityManager::enable_encryption(connection_handle_t connection) {
804-
MBED_ASSERT(_db);
823+
if (!_db) return BLE_ERROR_INITIALIZATION_INCOMPLETE;
805824
ControlBlock_t *cb = get_control_block(connection);
806825
if (!cb) {
807826
return BLE_ERROR_INVALID_PARAM;

0 commit comments

Comments
 (0)