Skip to content

Commit 9c83348

Browse files
author
Scott Powell
committed
* DataStore, advert blob record format change
1 parent dd808ee commit 9c83348

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <Arduino.h>
22
#include "DataStore.h"
33

4-
DataStore::DataStore(FILESYSTEM& fs) : _fs(&fs),
4+
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
55
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
66
identity_store(fs, "")
77
#elif defined(RP2040_PLATFORM)
@@ -252,16 +252,23 @@ void DataStore::saveChannels(DataStoreHost* host) {
252252

253253
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
254254

255-
#define MAX_ADVERT_PKT_LEN (PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE)
255+
#define MAX_ADVERT_PKT_LEN (2 + 32 + PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE)
256+
257+
struct BlobRec {
258+
uint32_t timestamp;
259+
uint8_t key[7];
260+
uint8_t len;
261+
uint8_t data[MAX_ADVERT_PKT_LEN];
262+
};
256263

257264
void DataStore::checkAdvBlobFile() {
258265
if (!_fs->exists("/adv_blobs")) {
259266
File file = openWrite(_fs, "/adv_blobs");
260267
if (file) {
261-
uint8_t zeroes[1 + MAX_ADVERT_PKT_LEN];
262-
memset(zeroes, 0, sizeof(zeroes));
263-
for (int i = 0; i < 24; i++) { // pre-allocate to fixed size
264-
file.write(zeroes, sizeof(zeroes));
268+
BlobRec zeroes;
269+
memset(&zeroes, 0, sizeof(zeroes));
270+
for (int i = 0; i < 20; i++) { // pre-allocate to fixed size
271+
file.write((uint8_t *) &zeroes, sizeof(zeroes));
265272
}
266273
file.close();
267274
}
@@ -271,12 +278,13 @@ void DataStore::checkAdvBlobFile() {
271278
uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
272279
File file = _fs->open("/adv_blobs");
273280
uint8_t len = 0; // 0 = not found
281+
274282
if (file) {
275-
uint8_t tmp[1 + MAX_ADVERT_PKT_LEN];
276-
while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) {
277-
if (memcmp(key, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob
278-
len = tmp[0];
279-
memcpy(dest_buf, &tmp[1], len);
283+
BlobRec tmp;
284+
while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
285+
if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
286+
len = tmp.len;
287+
memcpy(dest_buf, tmp.data, len);
280288
break;
281289
}
282290
}
@@ -296,25 +304,28 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src
296304
uint32_t min_timestamp = 0xFFFFFFFF;
297305

298306
// search for matching key OR evict by oldest timestmap
299-
uint8_t tmp[1 + MAX_ADVERT_PKT_LEN];
300-
while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) {
301-
if (memcmp(src_buf, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob
307+
BlobRec tmp;
308+
file.seek(0);
309+
while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
310+
if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
302311
found_pos = pos;
303312
break;
304313
}
305-
uint32_t timestamp;
306-
memcpy(&timestamp, &tmp[1 + PUB_KEY_SIZE], 4);
307-
if (timestamp < min_timestamp) {
308-
min_timestamp = timestamp;
314+
if (tmp.timestamp < min_timestamp) {
315+
min_timestamp = tmp.timestamp;
309316
found_pos = pos;
310317
}
311318

312319
pos += sizeof(tmp);
313320
}
314321

322+
memcpy(tmp.key, key, sizeof(tmp.key)); // just record 7 byte prefix of key
323+
memcpy(tmp.data, src_buf, len);
324+
tmp.len = len;
325+
tmp.timestamp = _clock->getCurrentTime();
326+
315327
file.seek(found_pos);
316-
file.write(&len, 1);
317-
file.write(src_buf, len);
328+
file.write((uint8_t *) &tmp, sizeof(tmp));
318329

319330
file.close();
320331
return true;

examples/companion_radio/DataStore.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class DataStoreHost {
1515

1616
class DataStore {
1717
FILESYSTEM* _fs;
18+
mesh::RTCClock* _clock;
1819
IdentityStore identity_store;
1920

2021
void loadPrefsInt(const char *filename, NodePrefs& prefs, double& node_lat, double& node_lon);
@@ -23,7 +24,7 @@ class DataStore {
2324
#endif
2425

2526
public:
26-
DataStore(FILESYSTEM& fs);
27+
DataStore(FILESYSTEM& fs, mesh::RTCClock& clock);
2728
void begin();
2829
bool formatFileSystem();
2930
bool loadMainIdentity(mesh::LocalIdentity &identity);

examples/companion_radio/MyMesh.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class MyMesh : public BaseChatMesh, public DataStoreHost {
140140
void updateContactFromFrame(ContactInfo &contact, const uint8_t *frame, int len);
141141
void addToOfflineQueue(const uint8_t frame[], int len);
142142
int getFromOfflineQueue(uint8_t frame[]);
143+
int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override {
144+
return _store->getBlobByKey(key, key_len, dest_buf);
145+
}
146+
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) override {
147+
return _store->putBlobByKey(key, key_len, src_buf, len);
148+
}
143149

144150
void checkCLIRescueCmd();
145151
void checkSerialInterface();

examples/companion_radio/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ static uint32_t _atoi(const char* sp) {
1414

1515
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
1616
#include <InternalFileSystem.h>
17-
DataStore store(InternalFS);
17+
DataStore store(InternalFS, rtc_clock);
1818
#elif defined(RP2040_PLATFORM)
1919
#include <LittleFS.h>
20-
DataStore store(LittleFS);
20+
DataStore store(LittleFS, rtc_clock);
2121
#elif defined(ESP32)
2222
#include <SPIFFS.h>
23-
DataStore store(SPIFFS);
23+
DataStore store(SPIFFS, rtc_clock);
2424
#endif
2525

2626
#ifdef ESP32

variants/t114/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ build_flags =
7070
-D MAX_CONTACTS=100
7171
-D MAX_GROUP_CHANNELS=8
7272
-D BLE_PIN_CODE=123456
73-
-D BLE_DEBUG_LOGGING=1
73+
; -D BLE_DEBUG_LOGGING=1
7474
-D OFFLINE_QUEUE_SIZE=256
7575
; -D ENABLE_PRIVATE_KEY_IMPORT=1
7676
; -D ENABLE_PRIVATE_KEY_EXPORT=1

0 commit comments

Comments
 (0)