Skip to content

Commit 782118d

Browse files
committed
Peek & pop separately when pushing messages to BLE client
I've noticed very often that the first few messages can get lost after reconnecting with BLE. This should fix that.
1 parent cdd3d5f commit 782118d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

examples/companion_radio/MyMesh.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ int MyMesh::getFromOfflineQueue(uint8_t frame[]) {
245245
return 0; // queue is empty
246246
}
247247

248+
int MyMesh::peekOfflineQueue(uint8_t frame[]) {
249+
if (offline_queue_len > 0) {
250+
size_t len = offline_queue[0].len;
251+
memcpy(frame, offline_queue[0].buf, len);
252+
return len;
253+
}
254+
return 0;
255+
}
256+
257+
void MyMesh::popOfflineQueue() {
258+
if (offline_queue_len > 0) {
259+
offline_queue_len--;
260+
for (int i = 0; i < offline_queue_len; i++) {
261+
offline_queue[i] = offline_queue[i + 1];
262+
}
263+
}
264+
}
265+
248266
float MyMesh::getAirtimeBudgetFactor() const {
249267
return _prefs.airtime_factor;
250268
}
@@ -1228,11 +1246,13 @@ void MyMesh::handleCmdFrame(size_t len) {
12281246
}
12291247
} else if (cmd_frame[0] == CMD_SYNC_NEXT_MESSAGE) {
12301248
int out_len;
1231-
if ((out_len = getFromOfflineQueue(out_frame)) > 0) {
1232-
_serial->writeFrame(out_frame, out_len);
1249+
if ((out_len = peekOfflineQueue(out_frame)) > 0) {
1250+
if (_serial->writeFrame(out_frame, out_len) > 0) {
1251+
popOfflineQueue();
12331252
#ifdef DISPLAY_CLASS
1234-
if (_ui) _ui->msgRead(offline_queue_len);
1253+
if (_ui) _ui->msgRead(offline_queue_len);
12351254
#endif
1255+
}
12361256
} else {
12371257
out_frame[0] = RESP_CODE_NO_MORE_MESSAGES;
12381258
_serial->writeFrame(out_frame, 1);

examples/companion_radio/MyMesh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
171171
void updateContactFromFrame(ContactInfo &contact, uint32_t& last_mod, const uint8_t *frame, int len);
172172
void addToOfflineQueue(const uint8_t frame[], int len);
173173
int getFromOfflineQueue(uint8_t frame[]);
174+
int peekOfflineQueue(uint8_t frame[]);
175+
void popOfflineQueue();
174176
int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
175177
return _store->getBlobByKey(key, key_len, dest_buf);
176178
}

0 commit comments

Comments
 (0)