Skip to content

Commit 2b24c57

Browse files
committed
support dual filsystems on nrf52
store identity and prefs in UserData and contacts, channels and adv_blobs in ExtraData
1 parent bdfe9ad commit 2b24c57

File tree

5 files changed

+268
-29
lines changed

5 files changed

+268
-29
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 200 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@ DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&
1818
{
1919
}
2020

21-
static File openWrite(FILESYSTEM* _fs, const char* filename) {
21+
#if defined(EXTRAFS) || defined(QSPIFLASH)
22+
23+
DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(&fsExtra), _clock(&clock),
2224
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
23-
_fs->remove(filename);
24-
return _fs->open(filename, FILE_O_WRITE);
25+
identity_store(fs, "")
2526
#elif defined(RP2040_PLATFORM)
26-
return _fs->open(filename, "w");
27+
identity_store(fs, "/identity")
2728
#else
28-
return _fs->open(filename, "w", true);
29+
identity_store(fs, "/identity")
30+
#endif
31+
{
32+
}
33+
#endif
34+
35+
static File openWrite(FILESYSTEM* fs, const char* filename) {
36+
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
37+
fs->remove(filename);
38+
return fs->open(filename, FILE_O_WRITE);
39+
#elif defined(RP2040_PLATFORM)
40+
return fs->open(filename, "w");
41+
#else
42+
return fs->open(filename, "w", true);
2943
#endif
3044
}
3145

@@ -36,6 +50,9 @@ void DataStore::begin() {
3650

3751
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
3852
checkAdvBlobFile();
53+
#if defined(EXTRAFS) || defined(QSPIFLASH)
54+
migrateToSecondaryFS();
55+
#endif
3956
#else
4057
// init 'blob store' support
4158
_fs->mkdir("/bl");
@@ -46,12 +63,13 @@ void DataStore::begin() {
4663
#include <SPIFFS.h>
4764
#elif defined(RP2040_PLATFORM)
4865
#include <LittleFS.h>
49-
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
50-
// #include <InternalFileSystem.h> // disabled for now, leaving here for dual fs branch
66+
#elif defined(NRF52_PLATFORM)
5167
#include <CustomLFS.h>
5268
#if defined(QSPIFLASH)
5369
#include <CustomLFS_QSPIFlash.h>
5470
#endif
71+
#elif defined(STM32_PLATFORM)
72+
#include <InternalFileSystem.h>
5573
#endif
5674

5775
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
@@ -77,8 +95,13 @@ uint32_t DataStore::getStorageUsedKb() const {
7795
_fs->info(info);
7896
return info.usedBytes / 1024;
7997
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
98+
#if defined(EXTRAFS) || defined(QSPIFLASH)
99+
const lfs_config* config = _fsExtra->_getFS()->cfg;
100+
int usedBlockCount = _getLfsUsedBlockCount(_fsExtra);
101+
#else
80102
const lfs_config* config = _fs->_getFS()->cfg;
81103
int usedBlockCount = _getLfsUsedBlockCount(_fs);
104+
#endif
82105
int usedBytes = config->block_size * usedBlockCount;
83106
return usedBytes / 1024;
84107
#else
@@ -95,7 +118,11 @@ uint32_t DataStore::getStorageTotalKb() const {
95118
_fs->info(info);
96119
return info.totalBytes / 1024;
97120
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
121+
#if defined(EXTRAFS) || defined(QSPIFLASH)
122+
const lfs_config* config = _fsExtra->_getFS()->cfg;
123+
#else
98124
const lfs_config* config = _fs->_getFS()->cfg;
125+
#endif
99126
int totalBytes = config->block_size * config->block_count;
100127
return totalBytes / 1024;
101128
#else
@@ -113,14 +140,31 @@ File DataStore::openRead(const char* filename) {
113140
#endif
114141
}
115142

143+
File DataStore::openRead(FILESYSTEM* fs, const char* filename) {
144+
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
145+
return fs->open(filename, FILE_O_READ);
146+
#elif defined(RP2040_PLATFORM)
147+
return fs->open(filename, "r");
148+
#else
149+
return fs->open(filename, "r", false);
150+
#endif
151+
}
152+
116153
bool DataStore::removeFile(const char* filename) {
117154
return _fs->remove(filename);
118155
}
119156

157+
bool DataStore::removeFile(FILESYSTEM* fs, const char* filename) {
158+
return fs->remove(filename);
159+
}
160+
120161
bool DataStore::formatFileSystem() {
121162
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
122-
// InternalFS.format(); // leaving as placeholder to remind for dual fs branch
163+
#if defined(EXTRAFS) || defined(QSPIFLASH)
164+
return _fs->format() && _fsExtra->format(); // in future maybe return an error code based on which format failed?
165+
#else
123166
return _fs->format();
167+
#endif
124168
#elif defined(RP2040_PLATFORM)
125169
return LittleFS.format();
126170
#elif defined(ESP32)
@@ -214,11 +258,20 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
214258
}
215259

216260
void DataStore::loadContacts(DataStoreHost* host) {
261+
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
262+
#if defined(EXTRAFS) || defined(QSPIFLASH)
263+
if (_fsExtra->exists("/contacts3")) {
264+
File file = _fsExtra->open("/contacts3");
265+
#else
217266
if (_fs->exists("/contacts3")) {
218-
#if defined(RP2040_PLATFORM)
267+
File file = _fs->open("/contacts3");
268+
#endif
269+
#elif defined(RP2040_PLATFORM)
270+
if (_fs->exists("/contacts3")) {
219271
File file = _fs->open("/contacts3", "r");
220272
#else
221-
File file = _fs->open("/contacts3");
273+
if (_fs->exists("/contacts3")) {
274+
File file = _fs->open("/contacts3", "r", false);
222275
#endif
223276
if (file) {
224277
bool full = false;
@@ -251,7 +304,11 @@ void DataStore::loadContacts(DataStoreHost* host) {
251304
}
252305

253306
void DataStore::saveContacts(DataStoreHost* host) {
307+
#if defined(EXTRAFS) || defined(QSPIFLASH)
308+
File file = openWrite(_fsExtra, "/contacts3");
309+
#else
254310
File file = openWrite(_fs, "/contacts3");
311+
#endif
255312
if (file) {
256313
uint32_t idx = 0;
257314
ContactInfo c;
@@ -280,11 +337,20 @@ void DataStore::saveContacts(DataStoreHost* host) {
280337
}
281338

282339
void DataStore::loadChannels(DataStoreHost* host) {
340+
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
341+
#if defined(EXTRAFS) || defined(QSPIFLASH)
342+
if (_fsExtra->exists("/channels2")) {
343+
File file = _fsExtra->open("/channels2");
344+
#else
345+
if (_fs->exists("/channels2")) {
346+
File file = _fs->open("/channels2");
347+
#endif
348+
#elif defined(RP2040_PLATFORM)
283349
if (_fs->exists("/channels2")) {
284-
#if defined(RP2040_PLATFORM)
285350
File file = _fs->open("/channels2", "r");
286351
#else
287-
File file = _fs->open("/channels2");
352+
if (_fs->exists("/channels2")) {
353+
File file = _fs->open("/channels2", "r", false);
288354
#endif
289355
if (file) {
290356
bool full = false;
@@ -311,7 +377,11 @@ void DataStore::loadChannels(DataStoreHost* host) {
311377
}
312378

313379
void DataStore::saveChannels(DataStoreHost* host) {
380+
#if defined(EXTRAFS) || defined(QSPIFLASH)
381+
File file = openWrite(_fsExtra, "/channels2");
382+
#else
314383
File file = openWrite(_fs, "/channels2");
384+
#endif
315385
if (file) {
316386
uint8_t channel_idx = 0;
317387
ChannelDetails ch;
@@ -342,8 +412,13 @@ struct BlobRec {
342412
};
343413

344414
void DataStore::checkAdvBlobFile() {
415+
#if defined(EXTRAFS) || defined(QSPIFLASH)
416+
if (!_fsExtra->exists("/adv_blobs")) {
417+
File file = openWrite(_fsExtra, "/adv_blobs");
418+
#else
345419
if (!_fs->exists("/adv_blobs")) {
346420
File file = openWrite(_fs, "/adv_blobs");
421+
#endif
347422
if (file) {
348423
BlobRec zeroes;
349424
memset(&zeroes, 0, sizeof(zeroes));
@@ -355,8 +430,117 @@ void DataStore::checkAdvBlobFile() {
355430
}
356431
}
357432

433+
void DataStore::migrateToSecondaryFS() {
434+
// migrate old adv_blobs, contacts3 and channels2 files to secondary FS if they don't already exist
435+
if (!_fsExtra->exists("/adv_blobs")) {
436+
if (_fs->exists("/adv_blobs")) {
437+
File oldAdvBlobs = openRead(_fs, "/adv_blobs");
438+
File newAdvBlobs = openWrite(_fsExtra, "/adv_blobs");
439+
440+
if (oldAdvBlobs && newAdvBlobs) {
441+
BlobRec rec;
442+
size_t count = 0;
443+
444+
// Copy 20 BlobRecs from old to new
445+
while (count < 20 && oldAdvBlobs.read((uint8_t *)&rec, sizeof(rec)) == sizeof(rec)) {
446+
newAdvBlobs.seek(count * sizeof(BlobRec));
447+
newAdvBlobs.write((uint8_t *)&rec, sizeof(rec));
448+
count++;
449+
}
450+
}
451+
if (oldAdvBlobs) oldAdvBlobs.close();
452+
if (newAdvBlobs) newAdvBlobs.close();
453+
_fs->remove("/adv_blobs");
454+
}
455+
}
456+
if (!_fsExtra->exists("/contacts3")) {
457+
if (_fs->exists("/contacts3")) {
458+
File oldFile = openRead(_fs, "/contacts3");
459+
File newFile = openWrite(_fsExtra, "/contacts3");
460+
461+
if (oldFile && newFile) {
462+
uint8_t buf[64];
463+
int n;
464+
while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
465+
newFile.write(buf, n);
466+
}
467+
}
468+
if (oldFile) oldFile.close();
469+
if (newFile) newFile.close();
470+
_fs->remove("/contacts3");
471+
}
472+
}
473+
if (!_fsExtra->exists("/channels2")) {
474+
if (_fs->exists("/contacts2")) {
475+
File oldFile = openRead(_fs, "/contacts2");
476+
File newFile = openWrite(_fsExtra, "/contacts2");
477+
478+
if (oldFile && newFile) {
479+
uint8_t buf[64];
480+
int n;
481+
while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
482+
newFile.write(buf, n);
483+
}
484+
}
485+
if (oldFile) oldFile.close();
486+
if (newFile) newFile.close();
487+
_fs->remove("/contacts2");
488+
}
489+
}
490+
// cleanup nodes which have been testing the extra fs, copy _main.id and new_prefs back to primary
491+
if (_fsExtra->exists("/_main.id")) {
492+
if (_fs->exists("/_main.id")) {_fs->remove("/_main.id");}
493+
File oldFile = openRead(_fsExtra, "/_main.id");
494+
File newFile = openWrite(_fs, "/_main.id");
495+
496+
if (oldFile && newFile) {
497+
uint8_t buf[64];
498+
int n;
499+
while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
500+
newFile.write(buf, n);
501+
}
502+
}
503+
if (oldFile) oldFile.close();
504+
if (newFile) newFile.close();
505+
_fsExtra->remove("/_main.id");
506+
}
507+
if (_fsExtra->exists("/new_prefs")) {
508+
if (_fs->exists("/new_prefs")) {_fs->remove("/new_prefs");}
509+
File oldFile = openRead(_fsExtra, "/new_prefs");
510+
File newFile = openWrite(_fs, "/new_prefs");
511+
512+
if (oldFile && newFile) {
513+
uint8_t buf[64];
514+
int n;
515+
while ((n = oldFile.read(buf, sizeof(buf))) > 0) {
516+
newFile.write(buf, n);
517+
}
518+
}
519+
if (oldFile) oldFile.close();
520+
if (newFile) newFile.close();
521+
_fsExtra->remove("/new_prefs");
522+
}
523+
// remove files from where they should not be anymore
524+
if (_fs->exists("/adv_blobs")) {
525+
_fs->remove("/adv_blobs");
526+
}
527+
if (_fs->exists("/contacts3")) {
528+
_fs->remove("/contacts3");
529+
}
530+
if (_fsExtra->exists("/_main.id")) {
531+
_fsExtra->remove("/_main.id");
532+
}
533+
if (_fsExtra->exists("/new_prefs")) {
534+
_fsExtra->remove("/new_prefs");
535+
}
536+
}
537+
358538
uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
539+
#if defined(EXTRAFS) || defined(QSPIFLASH)
540+
File file = _fsExtra->open("/adv_blobs");
541+
#else
359542
File file = _fs->open("/adv_blobs");
543+
#endif
360544
uint8_t len = 0; // 0 = not found
361545

362546
if (file) {
@@ -378,7 +562,11 @@ bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src
378562

379563
checkAdvBlobFile();
380564

565+
#if defined(EXTRAFS) || defined(QSPIFLASH)
566+
File file = _fsExtra->open("/adv_blobs", FILE_O_WRITE);
567+
#else
381568
File file = _fs->open("/adv_blobs", FILE_O_WRITE);
569+
#endif
382570
if (file) {
383571
uint32_t pos = 0, found_pos = 0;
384572
uint32_t min_timestamp = 0xFFFFFFFF;

examples/companion_radio/DataStore.h

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

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

@@ -25,8 +26,11 @@ class DataStore {
2526

2627
public:
2728
DataStore(FILESYSTEM& fs, mesh::RTCClock& clock);
29+
DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock);
2830
void begin();
2931
bool formatFileSystem();
32+
FILESYSTEM* getPrimaryFS() const { return _fs; }
33+
FILESYSTEM* getSecondaryFS() const { return _fsExtra; }
3034
bool loadMainIdentity(mesh::LocalIdentity &identity);
3135
bool saveMainIdentity(const mesh::LocalIdentity &identity);
3236
void loadPrefs(NodePrefs& prefs, double& node_lat, double& node_lon);
@@ -35,10 +39,13 @@ class DataStore {
3539
void saveContacts(DataStoreHost* host);
3640
void loadChannels(DataStoreHost* host);
3741
void saveChannels(DataStoreHost* host);
42+
void migrateToSecondaryFS();
3843
uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]);
3944
bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
4045
File openRead(const char* filename);
46+
File openRead(FILESYSTEM* fs, const char* filename);
4147
bool removeFile(const char* filename);
48+
bool removeFile(FILESYSTEM* fs, const char* filename);
4249
uint32_t getStorageUsedKb() const;
4350
uint32_t getStorageTotalKb() const;
4451
};

0 commit comments

Comments
 (0)