From 6cef0564881aae048b47c2c82d9493b7b43a4e1c Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Tue, 4 Feb 2025 19:40:30 +1100 Subject: [PATCH] * companion radio: offline messages queue --- examples/companion_radio/main.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 1ebec27e..d2ce72ff 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -42,6 +42,10 @@ #define MAX_CONTACTS 100 #endif +#ifndef OFFLINE_QUEUE_SIZE + #define OFFLINE_QUEUE_SIZE 16 +#endif + #include #define SEND_TIMEOUT_BASE_MILLIS 300 @@ -143,6 +147,13 @@ class MyMesh : public BaseChatMesh { uint8_t cmd_frame[MAX_FRAME_SIZE+1]; uint8_t out_frame[MAX_FRAME_SIZE+1]; + struct Frame { + uint8_t len; + uint8_t buf[MAX_FRAME_SIZE]; + }; + int offline_queue_len; + Frame offline_queue[OFFLINE_QUEUE_SIZE]; + void loadContacts() { if (_fs->exists("/contacts3")) { File file = _fs->open("/contacts3"); @@ -254,9 +265,25 @@ class MyMesh : public BaseChatMesh { } void addToOfflineQueue(const uint8_t frame[], int len) { - // TODO + if (offline_queue_len >= OFFLINE_QUEUE_SIZE) { + MESH_DEBUG_PRINTLN("ERROR: offline_queue is full!"); + } else { + offline_queue[offline_queue_len].len = len; + memcpy(offline_queue[offline_queue_len].buf, frame, len); + offline_queue_len++; + } } int getFromOfflineQueue(uint8_t frame[]) { + if (offline_queue_len > 0) { // check offline queue + size_t len = offline_queue[0].len; // take from top of queue + memcpy(frame, offline_queue[0].buf, len); + + offline_queue_len--; + for (int i = 0; i < offline_queue_len; i++) { // delete top item from queue + offline_queue[i] = offline_queue[i + 1]; + } + return len; + } return 0; // queue is empty } @@ -364,6 +391,7 @@ public: : BaseChatMesh(radio, *new ArduinoMillis(), rng, rtc, *new StaticPoolPacketManager(16), tables), _serial(NULL) { _iter_started = false; + offline_queue_len = 0; // defaults memset(&_prefs, 0, sizeof(_prefs));