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
257264void 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() {
271278uint8_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 (×tamp, &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 ;
0 commit comments