* DataStore, advert blob record format change

This commit is contained in:
Scott Powell
2025-06-06 21:35:54 +10:00
parent dd808ee6c7
commit 9c833486bf
5 changed files with 43 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
#include <Arduino.h>
#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(&timestamp, &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;

View File

@@ -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);

View File

@@ -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();

View File

@@ -14,13 +14,13 @@ static uint32_t _atoi(const char* sp) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#include <InternalFileSystem.h>
DataStore store(InternalFS);
DataStore store(InternalFS, rtc_clock);
#elif defined(RP2040_PLATFORM)
#include <LittleFS.h>
DataStore store(LittleFS);
DataStore store(LittleFS, rtc_clock);
#elif defined(ESP32)
#include <SPIFFS.h>
DataStore store(SPIFFS);
DataStore store(SPIFFS, rtc_clock);
#endif
#ifdef ESP32

View File

@@ -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