Added comments

This commit is contained in:
Clive Blackledge 2025-10-12 19:50:33 -07:00
parent b6118f27ad
commit fa494b07d5
3 changed files with 8 additions and 0 deletions

View File

@ -435,6 +435,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf)
case STATE_SEND_OTHER_NODEINFOS: { case STATE_SEND_OTHER_NODEINFOS: {
LOG_DEBUG("Send known nodes"); LOG_DEBUG("Send known nodes");
if (nodeInfoForPhone.num == 0 && !nodeInfoQueue.empty()) { if (nodeInfoForPhone.num == 0 && !nodeInfoQueue.empty()) {
// Serve the next cached node without re-reading from the DB iterator.
nodeInfoForPhone = nodeInfoQueue.front(); nodeInfoForPhone = nodeInfoQueue.front();
nodeInfoQueue.pop_front(); nodeInfoQueue.pop_front();
} }
@ -557,6 +558,7 @@ void PhoneAPI::releaseQueueStatusPhonePacket()
void PhoneAPI::prefetchNodeInfos() void PhoneAPI::prefetchNodeInfos()
{ {
bool added = false; bool added = false;
// Keep the queue topped up so BLE reads stay responsive even if DB fetches take a moment.
while (nodeInfoQueue.size() < kNodePrefetchDepth) { while (nodeInfoQueue.size() < kNodePrefetchDepth) {
auto nextNode = nodeDB->readNextMeshNode(readIndex); auto nextNode = nodeDB->readNextMeshNode(readIndex);
if (!nextNode) if (!nextNode)

View File

@ -80,7 +80,9 @@ class PhoneAPI
/// We temporarily keep the nodeInfo here between the call to available and getFromRadio /// We temporarily keep the nodeInfo here between the call to available and getFromRadio
meshtastic_NodeInfo nodeInfoForPhone = meshtastic_NodeInfo_init_default; meshtastic_NodeInfo nodeInfoForPhone = meshtastic_NodeInfo_init_default;
// Prefetched node info entries ready for immediate transmission to the phone.
std::deque<meshtastic_NodeInfo> nodeInfoQueue; std::deque<meshtastic_NodeInfo> nodeInfoQueue;
// Tunable size of the node info cache so we can keep BLE reads non-blocking.
static constexpr size_t kNodePrefetchDepth = 4; static constexpr size_t kNodePrefetchDepth = 4;
meshtastic_ToRadio toRadioScratch = { meshtastic_ToRadio toRadioScratch = {

View File

@ -63,6 +63,7 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
queue_size = 0; queue_size = 0;
} }
if (!hasChecked && phoneWants) { if (!hasChecked && phoneWants) {
// Pull fresh data while we're outside of the NimBLE callback context.
numBytes = getFromRadio(fromRadioBytes); numBytes = getFromRadio(fromRadioBytes);
hasChecked = true; hasChecked = true;
} }
@ -141,6 +142,7 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks
std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex); std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex);
if (!bluetoothPhoneAPI->hasChecked) { if (!bluetoothPhoneAPI->hasChecked) {
// Fetch payload on demand; prefetch keeps this fast for the first read.
bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes); bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes);
bluetoothPhoneAPI->hasChecked = true; bluetoothPhoneAPI->hasChecked = true;
} }
@ -149,6 +151,7 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks
if (bluetoothPhoneAPI->numBytes != 0) { if (bluetoothPhoneAPI->numBytes != 0) {
#ifdef NIMBLE_TWO #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); pCharacteristic->notify(bluetoothPhoneAPI->fromRadioBytes, bluetoothPhoneAPI->numBytes, BLE_HS_CONN_HANDLE_NONE);
#else #else
pCharacteristic->notify(); pCharacteristic->notify();
@ -432,6 +435,7 @@ void NimbleBluetooth::setupService()
// Define the characteristics that the app is looking for // Define the characteristics that the app is looking for
if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN) { if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_NO_PIN) {
ToRadioCharacteristic = bleService->createCharacteristic(TORADIO_UUID, NIMBLE_PROPERTY::WRITE); ToRadioCharacteristic = bleService->createCharacteristic(TORADIO_UUID, NIMBLE_PROPERTY::WRITE);
// Allow notifications so phones can stream FromRadio without polling.
FromRadioCharacteristic = FromRadioCharacteristic =
bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY); bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
fromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ); fromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ);