mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-09-29 06:06:38 +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) {
|
if (len > MAX_FRAME_SIZE) {
|
||||||
BLE_DEBUG_PRINTLN("ERROR: onWrite(), frame too big, len=%d", len);
|
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 {
|
} else {
|
||||||
recv_queue[recv_queue_len].len = len;
|
Frame frame = {};
|
||||||
memcpy(recv_queue[recv_queue_len].buf, rxValue, len);
|
frame.len = len;
|
||||||
recv_queue_len++;
|
memcpy(frame.buf, rxValue, len);
|
||||||
|
|
||||||
|
if (xQueueSend(recv_queue, &frame, 0) != pdTRUE) {
|
||||||
|
BLE_DEBUG_PRINTLN("ERROR: onWrite(), recv_queue is full!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- public methods
|
// ---------- public methods
|
||||||
|
|
||||||
|
void SerialBLEInterface::clearBuffers() {
|
||||||
|
xQueueReset(recv_queue);
|
||||||
|
send_queue_len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void SerialBLEInterface::enable() {
|
void SerialBLEInterface::enable() {
|
||||||
if (_isEnabled) return;
|
if (_isEnabled) return;
|
||||||
|
|
||||||
@@ -202,17 +209,11 @@ size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recv_queue_len > 0) { // check recv queue
|
Frame frame = {};
|
||||||
size_t len = recv_queue[0].len; // take from top of queue
|
if (xQueueReceive(recv_queue, &frame, 0) == pdTRUE) {
|
||||||
memcpy(dest, recv_queue[0].buf, len);
|
memcpy(dest, frame.buf, frame.len);
|
||||||
|
BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", (uint32_t) frame.len, (uint32_t) dest[0]);
|
||||||
BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", len, (uint32_t) dest[0]);
|
return frame.len;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pServer->getConnectedCount() == 0) deviceConnected = false;
|
if (pServer->getConnectedCount() == 0) deviceConnected = false;
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <BLEServer.h>
|
#include <BLEServer.h>
|
||||||
#include <BLEUtils.h>
|
#include <BLEUtils.h>
|
||||||
#include <BLE2902.h>
|
#include <BLE2902.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
|
||||||
class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLEServerCallbacks, BLECharacteristicCallbacks {
|
class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLEServerCallbacks, BLECharacteristicCallbacks {
|
||||||
BLEServer *pServer;
|
BLEServer *pServer;
|
||||||
@@ -24,12 +26,13 @@ class SerialBLEInterface : public BaseSerialInterface, BLESecurityCallbacks, BLE
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define FRAME_QUEUE_SIZE 4
|
#define FRAME_QUEUE_SIZE 4
|
||||||
int recv_queue_len;
|
StaticQueue_t recv_queue_state;
|
||||||
Frame recv_queue[FRAME_QUEUE_SIZE];
|
uint8_t recv_queue_storage[FRAME_QUEUE_SIZE * sizeof(Frame)];
|
||||||
|
QueueHandle_t recv_queue;
|
||||||
int send_queue_len;
|
int send_queue_len;
|
||||||
Frame send_queue[FRAME_QUEUE_SIZE];
|
Frame send_queue[FRAME_QUEUE_SIZE];
|
||||||
|
|
||||||
void clearBuffers() { recv_queue_len = 0; send_queue_len = 0; }
|
void clearBuffers();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// BLESecurityCallbacks methods
|
// BLESecurityCallbacks methods
|
||||||
@@ -58,7 +61,10 @@ public:
|
|||||||
_isEnabled = false;
|
_isEnabled = false;
|
||||||
_last_write = 0;
|
_last_write = 0;
|
||||||
last_conn_id = 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