From fa494b07d5abd0a1468db66b931a6baa0ad596af Mon Sep 17 00:00:00 2001 From: Clive Blackledge Date: Sun, 12 Oct 2025 19:50:33 -0700 Subject: [PATCH] Added comments --- src/mesh/PhoneAPI.cpp | 2 ++ src/mesh/PhoneAPI.h | 2 ++ src/nimble/NimbleBluetooth.cpp | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index dfc57b4e5..9eeadf5a2 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -435,6 +435,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_OTHER_NODEINFOS: { LOG_DEBUG("Send known nodes"); if (nodeInfoForPhone.num == 0 && !nodeInfoQueue.empty()) { + // Serve the next cached node without re-reading from the DB iterator. nodeInfoForPhone = nodeInfoQueue.front(); nodeInfoQueue.pop_front(); } @@ -557,6 +558,7 @@ void PhoneAPI::releaseQueueStatusPhonePacket() void PhoneAPI::prefetchNodeInfos() { bool added = false; + // Keep the queue topped up so BLE reads stay responsive even if DB fetches take a moment. while (nodeInfoQueue.size() < kNodePrefetchDepth) { auto nextNode = nodeDB->readNextMeshNode(readIndex); if (!nextNode) diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 24359e9a4..692fdd0b9 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -80,7 +80,9 @@ class PhoneAPI /// We temporarily keep the nodeInfo here between the call to available and getFromRadio meshtastic_NodeInfo nodeInfoForPhone = meshtastic_NodeInfo_init_default; + // Prefetched node info entries ready for immediate transmission to the phone. std::deque nodeInfoQueue; + // Tunable size of the node info cache so we can keep BLE reads non-blocking. static constexpr size_t kNodePrefetchDepth = 4; meshtastic_ToRadio toRadioScratch = { diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 113696bcd..4b0c33609 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -63,6 +63,7 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread queue_size = 0; } if (!hasChecked && phoneWants) { + // Pull fresh data while we're outside of the NimBLE callback context. numBytes = getFromRadio(fromRadioBytes); hasChecked = true; } @@ -141,6 +142,7 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks std::lock_guard guard(bluetoothPhoneAPI->nimble_mutex); if (!bluetoothPhoneAPI->hasChecked) { + // Fetch payload on demand; prefetch keeps this fast for the first read. bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes); bluetoothPhoneAPI->hasChecked = true; } @@ -149,6 +151,7 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks if (bluetoothPhoneAPI->numBytes != 0) { #ifdef NIMBLE_TWO + // Notify immediately so subscribed clients see the packet without an extra read. pCharacteristic->notify(bluetoothPhoneAPI->fromRadioBytes, bluetoothPhoneAPI->numBytes, BLE_HS_CONN_HANDLE_NONE); #else pCharacteristic->notify(); @@ -432,6 +435,7 @@ void NimbleBluetooth::setupService() // Define the characteristics that the app is looking for if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN) { ToRadioCharacteristic = bleService->createCharacteristic(TORADIO_UUID, NIMBLE_PROPERTY::WRITE); + // Allow notifications so phones can stream FromRadio without polling. FromRadioCharacteristic = bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY); fromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ);