From 9c833486bf3c25b730fd81fc2d7f190f58d1f74c Mon Sep 17 00:00:00 2001 From: Scott Powell Date: Fri, 6 Jun 2025 21:35:54 +1000 Subject: [PATCH] * DataStore, advert blob record format change --- examples/companion_radio/DataStore.cpp | 51 ++++++++++++++++---------- examples/companion_radio/DataStore.h | 3 +- examples/companion_radio/MyMesh.h | 6 +++ examples/companion_radio/main.cpp | 6 +-- variants/t114/platformio.ini | 2 +- 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/examples/companion_radio/DataStore.cpp b/examples/companion_radio/DataStore.cpp index 8e0d119a..a37cfa49 100644 --- a/examples/companion_radio/DataStore.cpp +++ b/examples/companion_radio/DataStore.cpp @@ -1,7 +1,7 @@ #include #include "DataStore.h" -DataStore::DataStore(FILESYSTEM& fs) : _fs(&fs), +DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock), #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) identity_store(fs, "") #elif defined(RP2040_PLATFORM) @@ -252,16 +252,23 @@ void DataStore::saveChannels(DataStoreHost* host) { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) -#define MAX_ADVERT_PKT_LEN (PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE) +#define MAX_ADVERT_PKT_LEN (2 + 32 + PUB_KEY_SIZE + 4 + SIGNATURE_SIZE + MAX_ADVERT_DATA_SIZE) + +struct BlobRec { + uint32_t timestamp; + uint8_t key[7]; + uint8_t len; + uint8_t data[MAX_ADVERT_PKT_LEN]; +}; void DataStore::checkAdvBlobFile() { if (!_fs->exists("/adv_blobs")) { File file = openWrite(_fs, "/adv_blobs"); if (file) { - uint8_t zeroes[1 + MAX_ADVERT_PKT_LEN]; - memset(zeroes, 0, sizeof(zeroes)); - for (int i = 0; i < 24; i++) { // pre-allocate to fixed size - file.write(zeroes, sizeof(zeroes)); + BlobRec zeroes; + memset(&zeroes, 0, sizeof(zeroes)); + for (int i = 0; i < 20; i++) { // pre-allocate to fixed size + file.write((uint8_t *) &zeroes, sizeof(zeroes)); } file.close(); } @@ -271,12 +278,13 @@ void DataStore::checkAdvBlobFile() { uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) { File file = _fs->open("/adv_blobs"); uint8_t len = 0; // 0 = not found + if (file) { - uint8_t tmp[1 + MAX_ADVERT_PKT_LEN]; - while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) { - if (memcmp(key, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob - len = tmp[0]; - memcpy(dest_buf, &tmp[1], len); + BlobRec tmp; + while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) { + if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix + len = tmp.len; + memcpy(dest_buf, tmp.data, len); break; } } @@ -296,25 +304,28 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src uint32_t min_timestamp = 0xFFFFFFFF; // search for matching key OR evict by oldest timestmap - uint8_t tmp[1 + MAX_ADVERT_PKT_LEN]; - while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) { - if (memcmp(src_buf, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob + BlobRec tmp; + file.seek(0); + while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) { + if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix found_pos = pos; break; } - uint32_t timestamp; - memcpy(×tamp, &tmp[1 + PUB_KEY_SIZE], 4); - if (timestamp < min_timestamp) { - min_timestamp = timestamp; + if (tmp.timestamp < min_timestamp) { + min_timestamp = tmp.timestamp; found_pos = pos; } pos += sizeof(tmp); } + memcpy(tmp.key, key, sizeof(tmp.key)); // just record 7 byte prefix of key + memcpy(tmp.data, src_buf, len); + tmp.len = len; + tmp.timestamp = _clock->getCurrentTime(); + file.seek(found_pos); - file.write(&len, 1); - file.write(src_buf, len); + file.write((uint8_t *) &tmp, sizeof(tmp)); file.close(); return true; diff --git a/examples/companion_radio/DataStore.h b/examples/companion_radio/DataStore.h index 540bc9cd..139131e1 100644 --- a/examples/companion_radio/DataStore.h +++ b/examples/companion_radio/DataStore.h @@ -15,6 +15,7 @@ public: class DataStore { FILESYSTEM* _fs; + mesh::RTCClock* _clock; IdentityStore identity_store; void loadPrefsInt(const char *filename, NodePrefs& prefs, double& node_lat, double& node_lon); @@ -23,7 +24,7 @@ class DataStore { #endif public: - DataStore(FILESYSTEM& fs); + DataStore(FILESYSTEM& fs, mesh::RTCClock& clock); void begin(); bool formatFileSystem(); bool loadMainIdentity(mesh::LocalIdentity &identity); diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index 6ac030d9..43194f09 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -140,6 +140,12 @@ private: void updateContactFromFrame(ContactInfo &contact, const uint8_t *frame, int len); void addToOfflineQueue(const uint8_t frame[], int len); int getFromOfflineQueue(uint8_t frame[]); + int getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) override { + return _store->getBlobByKey(key, key_len, dest_buf); + } + bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], int len) override { + return _store->putBlobByKey(key, key_len, src_buf, len); + } void checkCLIRescueCmd(); void checkSerialInterface(); diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index b463dcbe..b46cdaab 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -14,13 +14,13 @@ static uint32_t _atoi(const char* sp) { #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) #include - DataStore store(InternalFS); + DataStore store(InternalFS, rtc_clock); #elif defined(RP2040_PLATFORM) #include - DataStore store(LittleFS); + DataStore store(LittleFS, rtc_clock); #elif defined(ESP32) #include - DataStore store(SPIFFS); + DataStore store(SPIFFS, rtc_clock); #endif #ifdef ESP32 diff --git a/variants/t114/platformio.ini b/variants/t114/platformio.ini index fd9d6f34..5343e5ad 100644 --- a/variants/t114/platformio.ini +++ b/variants/t114/platformio.ini @@ -70,7 +70,7 @@ build_flags = -D MAX_CONTACTS=100 -D MAX_GROUP_CHANNELS=8 -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 +; -D BLE_DEBUG_LOGGING=1 -D OFFLINE_QUEUE_SIZE=256 ; -D ENABLE_PRIVATE_KEY_IMPORT=1 ; -D ENABLE_PRIVATE_KEY_EXPORT=1