Fix BLE stateful issues (#8287)

This commit is contained in:
Ben Meadors 2025-10-09 12:44:00 -05:00 committed by Jonathan Bennett
parent eee80ce636
commit 73cadce581
2 changed files with 4 additions and 21 deletions

View File

@ -734,7 +734,7 @@ int PhoneAPI::onNotify(uint32_t newValue)
LOG_INFO("Tell client we have new packets %u", newValue); LOG_INFO("Tell client we have new packets %u", newValue);
onNowHasData(newValue); onNowHasData(newValue);
} else { } else {
LOG_DEBUG("(Client not yet interested in packets)"); LOG_DEBUG("Client not yet interested in packets (state=%d)", state);
} }
return timeout ? -1 : 0; // If we timed out, MeshService should stop iterating through observers as we just removed one return timeout ? -1 : 0; // If we timed out, MeshService should stop iterating through observers as we just removed one

View File

@ -31,11 +31,8 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
std::vector<NimBLEAttValue> nimble_queue; std::vector<NimBLEAttValue> nimble_queue;
std::mutex nimble_mutex; std::mutex nimble_mutex;
uint8_t queue_size = 0; uint8_t queue_size = 0;
bool has_fromRadio = false;
uint8_t fromRadioBytes[meshtastic_FromRadio_size] = {0}; uint8_t fromRadioBytes[meshtastic_FromRadio_size] = {0};
size_t numBytes = 0; size_t numBytes = 0;
bool hasChecked = false;
bool phoneWants = false;
protected: protected:
virtual int32_t runOnce() override virtual int32_t runOnce() override
@ -48,10 +45,7 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
LOG_DEBUG("Queue_size %u", queue_size); LOG_DEBUG("Queue_size %u", queue_size);
queue_size = 0; queue_size = 0;
} }
if (hasChecked == false && phoneWants == true) { // Note: phoneWants/hasChecked logic removed since onRead() handles getFromRadio() directly
numBytes = getFromRadio(fromRadioBytes);
hasChecked = true;
}
// the run is triggered via NimbleBluetoothToRadioCallback and NimbleBluetoothFromRadioCallback // the run is triggered via NimbleBluetoothToRadioCallback and NimbleBluetoothFromRadioCallback
return INT32_MAX; return INT32_MAX;
@ -122,15 +116,8 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks
{ {
std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex); std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex);
// If we don't have fresh data, trigger a refresh // Get fresh data immediately when client reads
if (!bluetoothPhoneAPI->hasChecked || bluetoothPhoneAPI->numBytes == 0) { bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes);
bluetoothPhoneAPI->phoneWants = true;
bluetoothPhoneAPI->setIntervalFromNow(0);
// Get fresh data immediately
bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes);
bluetoothPhoneAPI->hasChecked = true;
}
// Set the characteristic value with whatever data we have // Set the characteristic value with whatever data we have
pCharacteristic->setValue(bluetoothPhoneAPI->fromRadioBytes, bluetoothPhoneAPI->numBytes); pCharacteristic->setValue(bluetoothPhoneAPI->fromRadioBytes, bluetoothPhoneAPI->numBytes);
@ -138,8 +125,6 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks
if (bluetoothPhoneAPI->numBytes != 0) // if we did send something, queue it up right away to reload if (bluetoothPhoneAPI->numBytes != 0) // if we did send something, queue it up right away to reload
bluetoothPhoneAPI->setIntervalFromNow(0); bluetoothPhoneAPI->setIntervalFromNow(0);
bluetoothPhoneAPI->numBytes = 0; bluetoothPhoneAPI->numBytes = 0;
bluetoothPhoneAPI->hasChecked = false;
bluetoothPhoneAPI->phoneWants = false;
} }
}; };
@ -248,8 +233,6 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
if (bluetoothPhoneAPI) { if (bluetoothPhoneAPI) {
std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex); std::lock_guard<std::mutex> guard(bluetoothPhoneAPI->nimble_mutex);
bluetoothPhoneAPI->close(); bluetoothPhoneAPI->close();
bluetoothPhoneAPI->hasChecked = false;
bluetoothPhoneAPI->phoneWants = false;
bluetoothPhoneAPI->numBytes = 0; bluetoothPhoneAPI->numBytes = 0;
bluetoothPhoneAPI->queue_size = 0; bluetoothPhoneAPI->queue_size = 0;
} }