Skip to content

Commit 3d83556

Browse files
committed
refactor: use _getContactsChannelsFS() instead of ifdefs
1 parent accd1e0 commit 3d83556

File tree

2 files changed

+23
-66
lines changed

2 files changed

+23
-66
lines changed

examples/companion_radio/DataStore.cpp

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define MAX_BLOBRECS 20
88
#endif
99

10-
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
10+
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(nullptr), _clock(&clock),
1111
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
1212
identity_store(fs, "")
1313
#elif defined(RP2040_PLATFORM)
@@ -19,7 +19,6 @@ DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&
1919
}
2020

2121
#if defined(EXTRAFS) || defined(QSPIFLASH)
22-
2322
DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(&fsExtra), _clock(&clock),
2423
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
2524
identity_store(fs, "")
@@ -66,14 +65,12 @@ void DataStore::begin() {
6665
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
6766
#if defined(QSPIFLASH)
6867
#include <CustomLFS_QSPIFlash.h>
69-
#else
70-
#if defined(EXTRAFS)
68+
#elif defined(EXTRAFS)
7169
#include <CustomLFS.h>
7270
#else
7371
#include <InternalFileSystem.h>
7472
#endif
7573
#endif
76-
#endif
7774

7875
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
7976
int _countLfsBlock(void *p, lfs_block_t block){
@@ -98,13 +95,8 @@ uint32_t DataStore::getStorageUsedKb() const {
9895
_fs->info(info);
9996
return info.usedBytes / 1024;
10097
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
101-
#if defined(EXTRAFS) || defined(QSPIFLASH)
102-
const lfs_config* config = _fsExtra->_getFS()->cfg;
103-
int usedBlockCount = _getLfsUsedBlockCount(_fsExtra);
104-
#else
105-
const lfs_config* config = _fs->_getFS()->cfg;
106-
int usedBlockCount = _getLfsUsedBlockCount(_fs);
107-
#endif
98+
const lfs_config* config = _getContactsChannelsFS()->_getFS()->cfg;
99+
int usedBlockCount = _getLfsUsedBlockCount(_getContactsChannelsFS());
108100
int usedBytes = config->block_size * usedBlockCount;
109101
return usedBytes / 1024;
110102
#else
@@ -121,11 +113,7 @@ uint32_t DataStore::getStorageTotalKb() const {
121113
_fs->info(info);
122114
return info.totalBytes / 1024;
123115
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
124-
#if defined(EXTRAFS) || defined(QSPIFLASH)
125-
const lfs_config* config = _fsExtra->_getFS()->cfg;
126-
#else
127-
const lfs_config* config = _fs->_getFS()->cfg;
128-
#endif
116+
const lfs_config* config = _getContactsChannelsFS()->_getFS()->cfg;
129117
int totalBytes = config->block_size * config->block_count;
130118
return totalBytes / 1024;
131119
#else
@@ -163,11 +151,11 @@ bool DataStore::removeFile(FILESYSTEM* fs, const char* filename) {
163151

164152
bool DataStore::formatFileSystem() {
165153
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
166-
#if defined(EXTRAFS) || defined(QSPIFLASH)
167-
return _fs->format() && _fsExtra->format(); // in future maybe return an error code based on which format failed?
168-
#else
169-
return _fs->format();
170-
#endif
154+
if (_fsExtra == nullptr) {
155+
return _fs->format();
156+
} else {
157+
return _fs->format() && _fsExtra->format();
158+
}
171159
#elif defined(RP2040_PLATFORM)
172160
return LittleFS.format();
173161
#elif defined(ESP32)
@@ -262,13 +250,8 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
262250

263251
void DataStore::loadContacts(DataStoreHost* host) {
264252
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
265-
#if defined(EXTRAFS) || defined(QSPIFLASH)
266-
if (_fsExtra->exists("/contacts3")) {
267-
File file = _fsExtra->open("/contacts3");
268-
#else
269-
if (_fs->exists("/contacts3")) {
270-
File file = _fs->open("/contacts3");
271-
#endif
253+
if (_getContactsChannelsFS()->exists("/contacts3")) {
254+
File file = _getContactsChannelsFS()->open("/contacts3");
272255
#elif defined(RP2040_PLATFORM)
273256
if (_fs->exists("/contacts3")) {
274257
File file = _fs->open("/contacts3", "r");
@@ -307,11 +290,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
307290
}
308291

309292
void DataStore::saveContacts(DataStoreHost* host) {
310-
#if defined(EXTRAFS) || defined(QSPIFLASH)
311-
File file = openWrite(_fsExtra, "/contacts3");
312-
#else
313-
File file = openWrite(_fs, "/contacts3");
314-
#endif
293+
File file = openWrite(_getContactsChannelsFS(), "/contacts3");
315294
if (file) {
316295
uint32_t idx = 0;
317296
ContactInfo c;
@@ -341,13 +320,8 @@ void DataStore::saveContacts(DataStoreHost* host) {
341320

342321
void DataStore::loadChannels(DataStoreHost* host) {
343322
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
344-
#if defined(EXTRAFS) || defined(QSPIFLASH)
345-
if (_fsExtra->exists("/channels2")) {
346-
File file = _fsExtra->open("/channels2");
347-
#else
348-
if (_fs->exists("/channels2")) {
349-
File file = _fs->open("/channels2");
350-
#endif
323+
if (_getContactsChannelsFS()->exists("/channels2")) {
324+
File file = _getContactsChannelsFS()->open("/channels2");
351325
#elif defined(RP2040_PLATFORM)
352326
if (_fs->exists("/channels2")) {
353327
File file = _fs->open("/channels2", "r");
@@ -380,11 +354,7 @@ void DataStore::loadChannels(DataStoreHost* host) {
380354
}
381355

382356
void DataStore::saveChannels(DataStoreHost* host) {
383-
#if defined(EXTRAFS) || defined(QSPIFLASH)
384-
File file = openWrite(_fsExtra, "/channels2");
385-
#else
386-
File file = openWrite(_fs, "/channels2");
387-
#endif
357+
File file = openWrite(_getContactsChannelsFS(), "/channels2");
388358
if (file) {
389359
uint8_t channel_idx = 0;
390360
ChannelDetails ch;
@@ -415,13 +385,8 @@ struct BlobRec {
415385
};
416386

417387
void DataStore::checkAdvBlobFile() {
418-
#if defined(EXTRAFS) || defined(QSPIFLASH)
419-
if (!_fsExtra->exists("/adv_blobs")) {
420-
File file = openWrite(_fsExtra, "/adv_blobs");
421-
#else
422-
if (!_fs->exists("/adv_blobs")) {
423-
File file = openWrite(_fs, "/adv_blobs");
424-
#endif
388+
if (!_getContactsChannelsFS()->exists("/adv_blobs")) {
389+
File file = openWrite(_getContactsChannelsFS(), "/adv_blobs");
425390
if (file) {
426391
BlobRec zeroes;
427392
memset(&zeroes, 0, sizeof(zeroes));
@@ -542,13 +507,8 @@ void DataStore::migrateToSecondaryFS() {
542507
}
543508

544509
uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]) {
545-
#if defined(EXTRAFS) || defined(QSPIFLASH)
546-
File file = _fsExtra->open("/adv_blobs");
547-
#else
548-
File file = _fs->open("/adv_blobs");
549-
#endif
510+
File file = _getContactsChannelsFS()->open("/adv_blobs");
550511
uint8_t len = 0; // 0 = not found
551-
552512
if (file) {
553513
BlobRec tmp;
554514
while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
@@ -565,14 +525,8 @@ uint8_t DataStore::getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_b
565525

566526
bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
567527
if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
568-
569528
checkAdvBlobFile();
570-
571-
#if defined(EXTRAFS) || defined(QSPIFLASH)
572-
File file = _fsExtra->open("/adv_blobs", FILE_O_WRITE);
573-
#else
574-
File file = _fs->open("/adv_blobs", FILE_O_WRITE);
575-
#endif
529+
File file = _getContactsChannelsFS()->open("/adv_blobs", FILE_O_WRITE);
576530
if (file) {
577531
uint32_t pos = 0, found_pos = 0;
578532
uint32_t min_timestamp = 0xFFFFFFFF;

examples/companion_radio/DataStore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ class DataStore {
4848
bool removeFile(FILESYSTEM* fs, const char* filename);
4949
uint32_t getStorageUsedKb() const;
5050
uint32_t getStorageTotalKb() const;
51+
52+
private:
53+
FILESYSTEM* _getContactsChannelsFS() const { if (_fsExtra) return _fsExtra; return _fs;};
5154
};

0 commit comments

Comments
 (0)