BLE P2P stability: PSRAM zero-init, pool sizing, stuck-state recovery

Root cause: Bytes objects stored in PSRAM-allocated BLEInterface had
corrupted shared_ptr members from uninitialized memory, causing crashes
in processDiscoveredPeers(). Fixed by using heap_caps_calloc instead of
heap_caps_malloc for PSRAM placement-new allocation.

Additional fixes:
- Reduce pool sizes to fit memory budget (reassembler 134KB→17KB,
  fragmenters 8→4, handshakes 32→4, pending data 64→8)
- Store local MAC as BLEAddress struct instead of Bytes to avoid
  heap allocation in PSRAM-resident object
- Move setLocalMac after platform start (NimBLE needs to be running
  for valid random address), add lazy MAC init fallback in loop()
- Add stuck-state detector: resets GAP state machine if hardware
  is idle but state machine thinks it's busy
- Enhance getLocalAddress with 3 fallback methods (NimBLE API,
  ble_hs_id_copy_addr RANDOM, esp_read_mac efuse)
- Fix C++17 structured binding to C++11 compatibility
- Increase BLE task stack 8KB→12KB for string ops in debug logs

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
torlando-techandClaude Opus 4.6 committed 2026-02-22 20:57:05 -05:00
1 parent d58ac9573f
commit 769c9952bd
7 files changed
+114 -22

No files matched your search

+5 -3
View File
@@ -14,7 +14,7 @@
namespace RNS { namespace BLE {
BLEPeerManager::BLEPeerManager() {
_local_mac = Bytes(6); // Initialize to zeros
memset(_local_mac_addr.addr, 0, 6); // Initialize to zeros
// Initialize all pools to empty state
for (size_t i = 0; i < PEERS_POOL_SIZE; i++) {
@@ -31,7 +31,8 @@ BLEPeerManager::BLEPeerManager() {
void BLEPeerManager::setLocalMac(const Bytes& mac) {
if (mac.size() >= Limits::MAC_SIZE) {
_local_mac = Bytes(mac.data(), Limits::MAC_SIZE);
memcpy(_local_mac_addr.addr, mac.data(), Limits::MAC_SIZE);
DEBUG("BLEPeerManager: Local MAC set to " + _local_mac_addr.toString());
}
}
@@ -321,7 +322,8 @@ PeerInfo* BLEPeerManager::getBestConnectionCandidate() {
}
bool BLEPeerManager::shouldInitiateConnection(const Bytes& peer_mac) const {
return shouldInitiateConnection(_local_mac, peer_mac);
Bytes our_mac(_local_mac_addr.addr, Limits::MAC_SIZE);
return shouldInitiateConnection(our_mac, peer_mac);
}
bool BLEPeerManager::shouldInitiateConnection(const Bytes& our_mac, const Bytes& peer_mac) {