Skip to content

Commit a147147

Browse files
committed
fix(nimble): Fix crashes in server
1 parent 4931b99 commit a147147

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

libraries/BLE/src/BLEServer.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,10 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
610610
return 0;
611611
}
612612

613-
server->m_pServerCallbacks->onConnect(server);
614-
server->m_pServerCallbacks->onConnect(server, &desc);
613+
if (server->m_pServerCallbacks != nullptr) {
614+
server->m_pServerCallbacks->onConnect(server);
615+
server->m_pServerCallbacks->onConnect(server, &desc);
616+
}
615617
}
616618

617619
return 0;
@@ -638,8 +640,10 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
638640
server->resetGATT();
639641
}
640642

641-
server->m_pServerCallbacks->onDisconnect(server);
642-
server->m_pServerCallbacks->onDisconnect(server, &event->disconnect.conn);
643+
if (server->m_pServerCallbacks != nullptr) {
644+
server->m_pServerCallbacks->onDisconnect(server);
645+
server->m_pServerCallbacks->onDisconnect(server, &event->disconnect.conn);
646+
}
643647

644648
return 0;
645649
} // BLE_GAP_EVENT_DISCONNECT
@@ -678,7 +682,9 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
678682
return 0;
679683
}
680684

681-
server->m_pServerCallbacks->onMtuChanged(server, &desc, event->mtu.value);
685+
if (server->m_pServerCallbacks != nullptr) {
686+
server->m_pServerCallbacks->onMtuChanged(server, &desc, event->mtu.value);
687+
}
682688
return 0;
683689
} // BLE_GAP_EVENT_MTU
684690

@@ -768,7 +774,7 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
768774

769775
if (BLEDevice::m_securityCallbacks != nullptr) {
770776
BLEDevice::m_securityCallbacks->onAuthenticationComplete(&desc);
771-
} else {
777+
} else if (server->m_pServerCallbacks != nullptr) {
772778
server->m_pServerCallbacks->onAuthenticationComplete(&desc);
773779
}
774780

@@ -786,7 +792,9 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
786792
// if the (static)passkey is the default, check the callback for custom value
787793
// both values default to the same.
788794
if (pkey.passkey == BLE_SM_DEFAULT_PASSKEY) {
789-
pkey.passkey = server->m_pServerCallbacks->onPassKeyRequest();
795+
if (server->m_pServerCallbacks != nullptr) {
796+
pkey.passkey = server->m_pServerCallbacks->onPassKeyRequest();
797+
}
790798
}
791799
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
792800
log_d("BLE_SM_IOACT_DISP; ble_sm_inject_io result: %d", rc);
@@ -797,7 +805,7 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
797805
// Compatibility only - Do not use, should be removed the in future
798806
if (BLEDevice::m_securityCallbacks != nullptr) {
799807
pkey.numcmp_accept = BLEDevice::m_securityCallbacks->onConfirmPIN(event->passkey.params.numcmp);
800-
} else {
808+
} else if (server->m_pServerCallbacks != nullptr) {
801809
pkey.numcmp_accept = server->m_pServerCallbacks->onConfirmPIN(event->passkey.params.numcmp);
802810
}
803811

@@ -820,7 +828,7 @@ int BLEServer::handleGATTServerEvent(struct ble_gap_event *event, void *arg) {
820828
// Compatibility only - Do not use, should be removed the in future
821829
if (BLEDevice::m_securityCallbacks != nullptr) {
822830
pkey.passkey = BLEDevice::m_securityCallbacks->onPassKeyRequest();
823-
} else {
831+
} else if (server->m_pServerCallbacks != nullptr) {
824832
pkey.passkey = server->m_pServerCallbacks->onPassKeyRequest();
825833
}
826834

libraries/BLE/src/BLEService.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ bool BLEService::start() {
527527
// Nimble requires an array of services to be sent to the api
528528
// Since we are adding 1 at a time we create an array of 2 and set the type
529529
// of the second service to 0 to indicate the end of the array.
530-
ble_gatt_svc_def *svc = new ble_gatt_svc_def[2];
530+
ble_gatt_svc_def *svc = new ble_gatt_svc_def[2]{};
531531
ble_gatt_chr_def *pChr_a = nullptr;
532532
ble_gatt_dsc_def *pDsc_a = nullptr;
533533

534534
svc[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
535-
svc[0].uuid = &m_uuid.getNative()->u;
536-
svc[0].includes = NULL;
535+
svc[0].uuid = (const ble_uuid_t *) &(m_uuid.getNative()->u);
536+
svc[0].includes = nullptr;
537537

538538
int removedCount = 0;
539539
BLECharacteristic *pCharacteristic;
@@ -558,12 +558,12 @@ bool BLEService::start() {
558558
log_d("Adding %d characteristics for service %s", numChrs, toString().c_str());
559559

560560
if (!numChrs) {
561-
svc[0].characteristics = NULL;
561+
svc[0].characteristics = nullptr;
562562
} else {
563563
// Nimble requires the last characteristic to have it's uuid = 0 to indicate the end
564564
// of the characteristics for the service. We create 1 extra and set it to null
565565
// for this purpose.
566-
pChr_a = new ble_gatt_chr_def[numChrs + 1];
566+
pChr_a = new ble_gatt_chr_def[numChrs + 1]{};
567567
uint8_t i = 0;
568568
pCharacteristic = m_characteristicMap.getFirst();
569569
while (pCharacteristic != nullptr) {
@@ -585,17 +585,18 @@ bool BLEService::start() {
585585
}
586586

587587
size_t numDscs = pCharacteristic->m_descriptorMap.getRegisteredDescriptorCount() - removedCount;
588+
log_d("Adding %d descriptors for characteristic %s", numDscs, pCharacteristic->getUUID().toString().c_str());
588589

589590
if (!numDscs) {
590-
pChr_a[i].descriptors = NULL;
591+
pChr_a[i].descriptors = nullptr;
591592
} else {
592593
// Must have last descriptor uuid = 0 so we have to create 1 extra
593-
pDsc_a = new ble_gatt_dsc_def[numDscs + 1];
594+
pDsc_a = new ble_gatt_dsc_def[numDscs + 1]{};
594595
uint8_t d = 0;
595596
pDescriptor = pCharacteristic->m_descriptorMap.getFirst();
596597
while (pDescriptor != nullptr) {
597598
if (pDescriptor->m_removed <= 0) {
598-
pDsc_a[d].uuid = &pDescriptor->m_bleUUID.getNative()->u;
599+
pDsc_a[d].uuid = (const ble_uuid_t *) &(pDescriptor->m_bleUUID.getNative()->u);
599600
pDsc_a[d].att_flags = pDescriptor->m_permissions;
600601
pDsc_a[d].min_key_size = 0;
601602
pDsc_a[d].access_cb = BLEDescriptor::handleGATTServerEvent;
@@ -605,11 +606,11 @@ bool BLEService::start() {
605606
pDescriptor = pCharacteristic->m_descriptorMap.getNext();
606607
}
607608

608-
pDsc_a[numDscs].uuid = NULL;
609+
pDsc_a[numDscs].uuid = nullptr;
609610
pChr_a[i].descriptors = pDsc_a;
610611
}
611612

612-
pChr_a[i].uuid = &pCharacteristic->m_bleUUID.getNative()->u;
613+
pChr_a[i].uuid = (const ble_uuid_t *) &(pCharacteristic->m_bleUUID.getNative()->u);
613614
pChr_a[i].access_cb = BLECharacteristic::handleGATTServerEvent;
614615
pChr_a[i].arg = pCharacteristic;
615616
pChr_a[i].flags = pCharacteristic->m_properties;
@@ -621,7 +622,7 @@ bool BLEService::start() {
621622
pCharacteristic = m_characteristicMap.getNext();
622623
}
623624

624-
pChr_a[numChrs].uuid = NULL;
625+
pChr_a[numChrs].uuid = nullptr;
625626
svc[0].characteristics = pChr_a;
626627
}
627628

0 commit comments

Comments
 (0)