mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
refactor: use _getContactsChannelsFS() instead of ifdefs
This commit is contained in:
parent
accd1e0a97
commit
3d83556829
2 changed files with 23 additions and 66 deletions
|
|
@ -7,7 +7,7 @@
|
||||||
#define MAX_BLOBRECS 20
|
#define MAX_BLOBRECS 20
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&clock),
|
DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(nullptr), _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)
|
||||||
|
|
@ -19,7 +19,6 @@ DataStore::DataStore(FILESYSTEM& fs, mesh::RTCClock& clock) : _fs(&fs), _clock(&
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
||||||
|
|
||||||
DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(&fsExtra), _clock(&clock),
|
DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock) : _fs(&fs), _fsExtra(&fsExtra), _clock(&clock),
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
identity_store(fs, "")
|
identity_store(fs, "")
|
||||||
|
|
@ -66,14 +65,12 @@ void DataStore::begin() {
|
||||||
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(QSPIFLASH)
|
#if defined(QSPIFLASH)
|
||||||
#include <CustomLFS_QSPIFlash.h>
|
#include <CustomLFS_QSPIFlash.h>
|
||||||
#else
|
#elif defined(EXTRAFS)
|
||||||
#if defined(EXTRAFS)
|
|
||||||
#include <CustomLFS.h>
|
#include <CustomLFS.h>
|
||||||
#else
|
#else
|
||||||
#include <InternalFileSystem.h>
|
#include <InternalFileSystem.h>
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
int _countLfsBlock(void *p, lfs_block_t block){
|
int _countLfsBlock(void *p, lfs_block_t block){
|
||||||
|
|
@ -98,13 +95,8 @@ uint32_t DataStore::getStorageUsedKb() const {
|
||||||
_fs->info(info);
|
_fs->info(info);
|
||||||
return info.usedBytes / 1024;
|
return info.usedBytes / 1024;
|
||||||
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
const lfs_config* config = _getContactsChannelsFS()->_getFS()->cfg;
|
||||||
const lfs_config* config = _fsExtra->_getFS()->cfg;
|
int usedBlockCount = _getLfsUsedBlockCount(_getContactsChannelsFS());
|
||||||
int usedBlockCount = _getLfsUsedBlockCount(_fsExtra);
|
|
||||||
#else
|
|
||||||
const lfs_config* config = _fs->_getFS()->cfg;
|
|
||||||
int usedBlockCount = _getLfsUsedBlockCount(_fs);
|
|
||||||
#endif
|
|
||||||
int usedBytes = config->block_size * usedBlockCount;
|
int usedBytes = config->block_size * usedBlockCount;
|
||||||
return usedBytes / 1024;
|
return usedBytes / 1024;
|
||||||
#else
|
#else
|
||||||
|
|
@ -121,11 +113,7 @@ uint32_t DataStore::getStorageTotalKb() const {
|
||||||
_fs->info(info);
|
_fs->info(info);
|
||||||
return info.totalBytes / 1024;
|
return info.totalBytes / 1024;
|
||||||
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#elif defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
const lfs_config* config = _getContactsChannelsFS()->_getFS()->cfg;
|
||||||
const lfs_config* config = _fsExtra->_getFS()->cfg;
|
|
||||||
#else
|
|
||||||
const lfs_config* config = _fs->_getFS()->cfg;
|
|
||||||
#endif
|
|
||||||
int totalBytes = config->block_size * config->block_count;
|
int totalBytes = config->block_size * config->block_count;
|
||||||
return totalBytes / 1024;
|
return totalBytes / 1024;
|
||||||
#else
|
#else
|
||||||
|
|
@ -163,11 +151,11 @@ bool DataStore::removeFile(FILESYSTEM* fs, const char* filename) {
|
||||||
|
|
||||||
bool DataStore::formatFileSystem() {
|
bool DataStore::formatFileSystem() {
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
if (_fsExtra == nullptr) {
|
||||||
return _fs->format() && _fsExtra->format(); // in future maybe return an error code based on which format failed?
|
return _fs->format();
|
||||||
#else
|
} else {
|
||||||
return _fs->format();
|
return _fs->format() && _fsExtra->format();
|
||||||
#endif
|
}
|
||||||
#elif defined(RP2040_PLATFORM)
|
#elif defined(RP2040_PLATFORM)
|
||||||
return LittleFS.format();
|
return LittleFS.format();
|
||||||
#elif defined(ESP32)
|
#elif defined(ESP32)
|
||||||
|
|
@ -262,13 +250,8 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
|
||||||
|
|
||||||
void DataStore::loadContacts(DataStoreHost* host) {
|
void DataStore::loadContacts(DataStoreHost* host) {
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
if (_getContactsChannelsFS()->exists("/contacts3")) {
|
||||||
if (_fsExtra->exists("/contacts3")) {
|
File file = _getContactsChannelsFS()->open("/contacts3");
|
||||||
File file = _fsExtra->open("/contacts3");
|
|
||||||
#else
|
|
||||||
if (_fs->exists("/contacts3")) {
|
|
||||||
File file = _fs->open("/contacts3");
|
|
||||||
#endif
|
|
||||||
#elif defined(RP2040_PLATFORM)
|
#elif defined(RP2040_PLATFORM)
|
||||||
if (_fs->exists("/contacts3")) {
|
if (_fs->exists("/contacts3")) {
|
||||||
File file = _fs->open("/contacts3", "r");
|
File file = _fs->open("/contacts3", "r");
|
||||||
|
|
@ -307,11 +290,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataStore::saveContacts(DataStoreHost* host) {
|
void DataStore::saveContacts(DataStoreHost* host) {
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
File file = openWrite(_getContactsChannelsFS(), "/contacts3");
|
||||||
File file = openWrite(_fsExtra, "/contacts3");
|
|
||||||
#else
|
|
||||||
File file = openWrite(_fs, "/contacts3");
|
|
||||||
#endif
|
|
||||||
if (file) {
|
if (file) {
|
||||||
uint32_t idx = 0;
|
uint32_t idx = 0;
|
||||||
ContactInfo c;
|
ContactInfo c;
|
||||||
|
|
@ -341,13 +320,8 @@ void DataStore::saveContacts(DataStoreHost* host) {
|
||||||
|
|
||||||
void DataStore::loadChannels(DataStoreHost* host) {
|
void DataStore::loadChannels(DataStoreHost* host) {
|
||||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
if (_getContactsChannelsFS()->exists("/channels2")) {
|
||||||
if (_fsExtra->exists("/channels2")) {
|
File file = _getContactsChannelsFS()->open("/channels2");
|
||||||
File file = _fsExtra->open("/channels2");
|
|
||||||
#else
|
|
||||||
if (_fs->exists("/channels2")) {
|
|
||||||
File file = _fs->open("/channels2");
|
|
||||||
#endif
|
|
||||||
#elif defined(RP2040_PLATFORM)
|
#elif defined(RP2040_PLATFORM)
|
||||||
if (_fs->exists("/channels2")) {
|
if (_fs->exists("/channels2")) {
|
||||||
File file = _fs->open("/channels2", "r");
|
File file = _fs->open("/channels2", "r");
|
||||||
|
|
@ -380,11 +354,7 @@ void DataStore::loadChannels(DataStoreHost* host) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataStore::saveChannels(DataStoreHost* host) {
|
void DataStore::saveChannels(DataStoreHost* host) {
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
File file = openWrite(_getContactsChannelsFS(), "/channels2");
|
||||||
File file = openWrite(_fsExtra, "/channels2");
|
|
||||||
#else
|
|
||||||
File file = openWrite(_fs, "/channels2");
|
|
||||||
#endif
|
|
||||||
if (file) {
|
if (file) {
|
||||||
uint8_t channel_idx = 0;
|
uint8_t channel_idx = 0;
|
||||||
ChannelDetails ch;
|
ChannelDetails ch;
|
||||||
|
|
@ -415,13 +385,8 @@ struct BlobRec {
|
||||||
};
|
};
|
||||||
|
|
||||||
void DataStore::checkAdvBlobFile() {
|
void DataStore::checkAdvBlobFile() {
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
if (!_getContactsChannelsFS()->exists("/adv_blobs")) {
|
||||||
if (!_fsExtra->exists("/adv_blobs")) {
|
File file = openWrite(_getContactsChannelsFS(), "/adv_blobs");
|
||||||
File file = openWrite(_fsExtra, "/adv_blobs");
|
|
||||||
#else
|
|
||||||
if (!_fs->exists("/adv_blobs")) {
|
|
||||||
File file = openWrite(_fs, "/adv_blobs");
|
|
||||||
#endif
|
|
||||||
if (file) {
|
if (file) {
|
||||||
BlobRec zeroes;
|
BlobRec zeroes;
|
||||||
memset(&zeroes, 0, sizeof(zeroes));
|
memset(&zeroes, 0, sizeof(zeroes));
|
||||||
|
|
@ -542,13 +507,8 @@ void DataStore::migrateToSecondaryFS() {
|
||||||
}
|
}
|
||||||
|
|
||||||
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[]) {
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
File file = _getContactsChannelsFS()->open("/adv_blobs");
|
||||||
File file = _fsExtra->open("/adv_blobs");
|
|
||||||
#else
|
|
||||||
File file = _fs->open("/adv_blobs");
|
|
||||||
#endif
|
|
||||||
uint8_t len = 0; // 0 = not found
|
uint8_t len = 0; // 0 = not found
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
BlobRec tmp;
|
BlobRec tmp;
|
||||||
while (file.read((uint8_t *) &tmp, sizeof(tmp)) == sizeof(tmp)) {
|
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
|
||||||
|
|
||||||
bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
|
bool DataStore::putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len) {
|
||||||
if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
|
if (len < PUB_KEY_SIZE+4+SIGNATURE_SIZE || len > MAX_ADVERT_PKT_LEN) return false;
|
||||||
|
|
||||||
checkAdvBlobFile();
|
checkAdvBlobFile();
|
||||||
|
File file = _getContactsChannelsFS()->open("/adv_blobs", FILE_O_WRITE);
|
||||||
#if defined(EXTRAFS) || defined(QSPIFLASH)
|
|
||||||
File file = _fsExtra->open("/adv_blobs", FILE_O_WRITE);
|
|
||||||
#else
|
|
||||||
File file = _fs->open("/adv_blobs", FILE_O_WRITE);
|
|
||||||
#endif
|
|
||||||
if (file) {
|
if (file) {
|
||||||
uint32_t pos = 0, found_pos = 0;
|
uint32_t pos = 0, found_pos = 0;
|
||||||
uint32_t min_timestamp = 0xFFFFFFFF;
|
uint32_t min_timestamp = 0xFFFFFFFF;
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,7 @@ public:
|
||||||
bool removeFile(FILESYSTEM* fs, const char* filename);
|
bool removeFile(FILESYSTEM* fs, const char* filename);
|
||||||
uint32_t getStorageUsedKb() const;
|
uint32_t getStorageUsedKb() const;
|
||||||
uint32_t getStorageTotalKb() const;
|
uint32_t getStorageTotalKb() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
FILESYSTEM* _getContactsChannelsFS() const { if (_fsExtra) return _fsExtra; return _fs;};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue