mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
* DataStore, advert blob record format change
This commit is contained in:
parent
dd808ee6c7
commit
9c833486bf
5 changed files with 43 additions and 25 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "DataStore.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)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
identity_store(fs, "")
|
identity_store(fs, "")
|
||||||
#elif defined(RP2040_PLATFORM)
|
#elif defined(RP2040_PLATFORM)
|
||||||
|
|
@ -252,16 +252,23 @@ void DataStore::saveChannels(DataStoreHost* host) {
|
||||||
|
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#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() {
|
void DataStore::checkAdvBlobFile() {
|
||||||
if (!_fs->exists("/adv_blobs")) {
|
if (!_fs->exists("/adv_blobs")) {
|
||||||
File file = openWrite(_fs, "/adv_blobs");
|
File file = openWrite(_fs, "/adv_blobs");
|
||||||
if (file) {
|
if (file) {
|
||||||
uint8_t zeroes[1 + MAX_ADVERT_PKT_LEN];
|
BlobRec zeroes;
|
||||||
memset(zeroes, 0, sizeof(zeroes));
|
memset(&zeroes, 0, sizeof(zeroes));
|
||||||
for (int i = 0; i < 24; i++) { // pre-allocate to fixed size
|
for (int i = 0; i < 20; i++) { // pre-allocate to fixed size
|
||||||
file.write(zeroes, sizeof(zeroes));
|
file.write((uint8_t *) &zeroes, sizeof(zeroes));
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
@ -271,12 +278,13 @@ void DataStore::checkAdvBlobFile() {
|
||||||
uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
|
uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
|
||||||
File file = _fs->open("/adv_blobs");
|
File file = _fs->open("/adv_blobs");
|
||||||
uint8_t len = 0; // 0 = not found
|
uint8_t len = 0; // 0 = not found
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
uint8_t tmp[1 + MAX_ADVERT_PKT_LEN];
|
BlobRec tmp;
|
||||||
while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) {
|
while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
|
||||||
if (memcmp(key, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob
|
if (memcmp(key, tmp.key, sizeof(tmp.key)) == 0) { // only match by 7 byte prefix
|
||||||
len = tmp[0];
|
len = tmp.len;
|
||||||
memcpy(dest_buf, &tmp[1], len);
|
memcpy(dest_buf, tmp.data, len);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -296,25 +304,28 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src
|
||||||
uint32_t min_timestamp = 0xFFFFFFFF;
|
uint32_t min_timestamp = 0xFFFFFFFF;
|
||||||
|
|
||||||
// search for matching key OR evict by oldest timestmap
|
// search for matching key OR evict by oldest timestmap
|
||||||
uint8_t tmp[1 + MAX_ADVERT_PKT_LEN];
|
BlobRec tmp;
|
||||||
while (file.read(tmp, sizeof(tmp)) == sizeof(tmp)) {
|
file.seek(0);
|
||||||
if (memcmp(src_buf, &tmp[1], PUB_KEY_SIZE) == 0) { // public key is first 32 bytes of advert blob
|
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;
|
found_pos = pos;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
uint32_t timestamp;
|
if (tmp.timestamp < min_timestamp) {
|
||||||
memcpy(×tamp, &tmp[1 + PUB_KEY_SIZE], 4);
|
min_timestamp = tmp.timestamp;
|
||||||
if (timestamp < min_timestamp) {
|
|
||||||
min_timestamp = timestamp;
|
|
||||||
found_pos = pos;
|
found_pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
pos += sizeof(tmp);
|
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.seek(found_pos);
|
||||||
file.write(&len, 1);
|
file.write((uint8_t *) &tmp, sizeof(tmp));
|
||||||
file.write(src_buf, len);
|
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ public:
|
||||||
|
|
||||||
class DataStore {
|
class DataStore {
|
||||||
FILESYSTEM* _fs;
|
FILESYSTEM* _fs;
|
||||||
|
mesh::RTCClock* _clock;
|
||||||
IdentityStore identity_store;
|
IdentityStore identity_store;
|
||||||
|
|
||||||
void loadPrefsInt(const char *filename, NodePrefs& prefs, double& node_lat, double& node_lon);
|
void loadPrefsInt(const char *filename, NodePrefs& prefs, double& node_lat, double& node_lon);
|
||||||
|
|
@ -23,7 +24,7 @@ class DataStore {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DataStore(FILESYSTEM& fs);
|
DataStore(FILESYSTEM& fs, mesh::RTCClock& clock);
|
||||||
void begin();
|
void begin();
|
||||||
bool formatFileSystem();
|
bool formatFileSystem();
|
||||||
bool loadMainIdentity(mesh::LocalIdentity &identity);
|
bool loadMainIdentity(mesh::LocalIdentity &identity);
|
||||||
|
|
|
||||||
|
|
@ -140,6 +140,12 @@ private:
|
||||||
void updateContactFromFrame(ContactInfo &contact, const uint8_t *frame, int len);
|
void updateContactFromFrame(ContactInfo &contact, const uint8_t *frame, int len);
|
||||||
void addToOfflineQueue(const uint8_t frame[], int len);
|
void addToOfflineQueue(const uint8_t frame[], int len);
|
||||||
int getFromOfflineQueue(uint8_t frame[]);
|
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 checkCLIRescueCmd();
|
||||||
void checkSerialInterface();
|
void checkSerialInterface();
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,13 @@ static uint32_t _atoi(const char* sp) {
|
||||||
|
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#include <InternalFileSystem.h>
|
#include <InternalFileSystem.h>
|
||||||
DataStore store(InternalFS);
|
DataStore store(InternalFS, rtc_clock);
|
||||||
#elif defined(RP2040_PLATFORM)
|
#elif defined(RP2040_PLATFORM)
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
DataStore store(LittleFS);
|
DataStore store(LittleFS, rtc_clock);
|
||||||
#elif defined(ESP32)
|
#elif defined(ESP32)
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
DataStore store(SPIFFS);
|
DataStore store(SPIFFS, rtc_clock);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ build_flags =
|
||||||
-D MAX_CONTACTS=100
|
-D MAX_CONTACTS=100
|
||||||
-D MAX_GROUP_CHANNELS=8
|
-D MAX_GROUP_CHANNELS=8
|
||||||
-D BLE_PIN_CODE=123456
|
-D BLE_PIN_CODE=123456
|
||||||
-D BLE_DEBUG_LOGGING=1
|
; -D BLE_DEBUG_LOGGING=1
|
||||||
-D OFFLINE_QUEUE_SIZE=256
|
-D OFFLINE_QUEUE_SIZE=256
|
||||||
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
; -D ENABLE_PRIVATE_KEY_IMPORT=1
|
||||||
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
; -D ENABLE_PRIVATE_KEY_EXPORT=1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue