41
41
BLEServer* BLEDevice::m_pServer = nullptr ;
42
42
BLEScan* BLEDevice::m_pScan = nullptr ;
43
43
BLEClient* BLEDevice::m_pClient = nullptr ;
44
- bool initialized = false ;
44
+ bool initialized = false ;
45
45
esp_ble_sec_act_t BLEDevice::m_securityLevel = (esp_ble_sec_act_t )0 ;
46
46
BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr ;
47
47
uint16_t BLEDevice::m_localMTU = 23 ; // not sure if this variable is useful
@@ -348,7 +348,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
348
348
}
349
349
350
350
#ifndef CONFIG_BT_CLASSIC_ENABLED
351
- esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT);
351
+ esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT);
352
352
#endif
353
353
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
354
354
errRc = esp_bt_controller_init (&bt_cfg);
@@ -577,7 +577,7 @@ BLEAdvertising* BLEDevice::getAdvertising() {
577
577
log_i (" create advertising" );
578
578
}
579
579
log_d (" get advertising" );
580
- return m_bleAdvertising;
580
+ return m_bleAdvertising;
581
581
}
582
582
583
583
void BLEDevice::startAdvertising () {
@@ -629,10 +629,15 @@ void BLEDevice::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
629
629
m_connectedClientsMap.insert (std::pair<uint16_t , conn_status_t >(conn_id, status));
630
630
}
631
631
632
+ // there may have some situation that invoking this function simultaneously, that will cause CORRUPT HEAP
633
+ // let this function serializable
634
+ portMUX_TYPE BLEDevice::mux = portMUX_INITIALIZER_UNLOCKED;
632
635
void BLEDevice::removePeerDevice (uint16_t conn_id, bool _client) {
636
+ portENTER_CRITICAL (&mux);
633
637
log_i (" remove: %d, GATT role %s" , conn_id, _client?" client" :" server" );
634
638
if (m_connectedClientsMap.find (conn_id) != m_connectedClientsMap.end ())
635
639
m_connectedClientsMap.erase (conn_id);
640
+ portEXIT_CRITICAL (&mux);
636
641
}
637
642
638
643
/* multi connect support */
@@ -652,8 +657,8 @@ void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
652
657
if (release_memory) {
653
658
esp_bt_controller_mem_release (ESP_BT_MODE_BTDM); // <-- require tests because we released classic BT memory and this can cause crash (most likely not, esp-idf takes care of it)
654
659
} else {
655
- initialized = false ;
656
- }
660
+ initialized = false ;
661
+ }
657
662
#endif
658
663
}
659
664
0 commit comments