mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-07-30 11:59:36 +00:00
Merge pull request #3007 from axhoff/agent/synchronize-ble-receive-queue
Synchronize the ESP32 BLE receive queue
This commit is contained in:
@@ -118,17 +118,24 @@ void SerialBLEInterface::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gat
|
||||
|
||||
if (len > MAX_FRAME_SIZE) {
|
||||
BLE_DEBUG_PRINTLN("ERROR: onWrite(), frame too big, len=%d", len);
|
||||
} else if (recv_queue_len >= FRAME_QUEUE_SIZE) {
|
||||
BLE_DEBUG_PRINTLN("ERROR: onWrite(), recv_queue is full!");
|
||||
} else {
|
||||
recv_queue[recv_queue_len].len = len;
|
||||
memcpy(recv_queue[recv_queue_len].buf, rxValue, len);
|
||||
recv_queue_len++;
|
||||
Frame frame = {};
|
||||
frame.len = len;
|
||||
memcpy(frame.buf, rxValue, len);
|
||||
|
||||
if (xQueueSend(recv_queue, &frame, 0) != pdTRUE) {
|
||||
BLE_DEBUG_PRINTLN("ERROR: onWrite(), recv_queue is full!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- public methods
|
||||
|
||||
void SerialBLEInterface::clearBuffers() {
|
||||
xQueueReset(recv_queue);
|
||||
send_queue_len = 0;
|
||||
}
|
||||
|
||||
void SerialBLEInterface::enable() {
|
||||
if (_isEnabled) return;
|
||||
|
||||
@@ -202,17 +209,11 @@ size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
|
||||
}
|
||||
}
|
||||
|
||||
if (recv_queue_len > 0) { // check recv queue
|
||||
size_t len = recv_queue[0].len; // take from top of queue
|
||||
memcpy(dest, recv_queue[0].buf, len);
|
||||
|
||||
BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", len, (uint32_t) dest[0]);
|
||||
|
||||
recv_queue_len--;
|
||||
for (int i = 0; i < recv_queue_len; i++) { // delete top item from queue
|
||||
recv_queue[i] = recv_queue[i + 1];
|
||||
}
|
||||
return len;
|
||||
Frame frame = {};
|
||||
if (xQueueReceive(recv_queue, &frame, 0) == pdTRUE) {
|
||||
memcpy(dest, frame.buf, frame.len);
|
||||
BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", (uint32_t) frame.len, (uint32_t) dest[0]);
|
||||
return frame.len;
|
||||
}
|
||||
|
||||
if (pServer->getConnectedCount() == 0) deviceConnected = false;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <BLEServer.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLE2902.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLEServerCallbacks, BLECharacteristicCallbacks {
|
||||
BLEServer *pServer;
|
||||
@@ -24,12 +26,13 @@ class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLE
|
||||
};
|
||||
|
||||
#define FRAME_QUEUE_SIZE 4
|
||||
int recv_queue_len;
|
||||
Frame recv_queue[FRAME_QUEUE_SIZE];
|
||||
StaticQueue_t recv_queue_state;
|
||||
uint8_t recv_queue_storage[FRAME_QUEUE_SIZE * sizeof(Frame)];
|
||||
QueueHandle_t recv_queue;
|
||||
int send_queue_len;
|
||||
Frame send_queue[FRAME_QUEUE_SIZE];
|
||||
|
||||
void clearBuffers() { recv_queue_len = 0; send_queue_len = 0; }
|
||||
void clearBuffers();
|
||||
|
||||
protected:
|
||||
// BLESecurityCallbacks methods
|
||||
@@ -58,7 +61,10 @@ public:
|
||||
_isEnabled = false;
|
||||
_last_write = 0;
|
||||
last_conn_id = 0;
|
||||
send_queue_len = recv_queue_len = 0;
|
||||
recv_queue = xQueueCreateStatic(
|
||||
FRAME_QUEUE_SIZE, sizeof(Frame), recv_queue_storage, &recv_queue_state
|
||||
);
|
||||
send_queue_len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user