Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/companion_radio/DataStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void DataStore::begin() {

#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
int _countLfsBlock(void *p, lfs_block_t block){
if (block > _ContactsChannelsTotalBlocks) {
if (block >= _ContactsChannelsTotalBlocks) {
MESH_DEBUG_PRINTLN("ERROR: Block %d exceeds filesystem bounds - CORRUPTION DETECTED!", block);
return LFS_ERR_CORRUPT; // return error to abort lfs_traverse() gracefully
}
Expand All @@ -91,7 +91,9 @@ int _countLfsBlock(void *p, lfs_block_t block){

lfs_ssize_t _getLfsUsedBlockCount(FILESYSTEM* fs) {
lfs_size_t size = 0;
fs->_lockFS();
int err = lfs_traverse(fs->_getFS(), _countLfsBlock, &size);
fs->_unlockFS();
if (err) {
MESH_DEBUG_PRINTLN("ERROR: lfs_traverse() error: %d", err);
return 0;
Expand Down
52 changes: 52 additions & 0 deletions examples/companion_radio/LockedLFS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <CustomLFS.h>
#include <InternalFileSystem.h>

// InternalFS and a CustomLFS on internal flash use the same flash cache, which
// has no lock of its own. InternalFS uses it under its own lock, so take that
// lock around all operations here too.
class LockedLFS : public CustomLFS {
lfs_config orig;

static const lfs_config& base(const lfs_config* c) {
return static_cast<LockedLFS*>(static_cast<CustomLFS*>(c->context))->orig;
}

static int read(const lfs_config* c, lfs_block_t b, lfs_off_t o, void* buf, lfs_size_t n) {
InternalFS._lockFS();
int r = base(c).read(c, b, o, buf, n);
InternalFS._unlockFS();
return r;
}

static int prog(const lfs_config* c, lfs_block_t b, lfs_off_t o, const void* buf, lfs_size_t n) {
InternalFS._lockFS();
int r = base(c).prog(c, b, o, buf, n);
InternalFS._unlockFS();
return r;
}

static int erase(const lfs_config* c, lfs_block_t b) {
InternalFS._lockFS();
int r = base(c).erase(c, b);
InternalFS._unlockFS();
return r;
}

static int sync(const lfs_config* c) {
InternalFS._lockFS();
int r = base(c).sync(c);
InternalFS._unlockFS();
return r;
}

public:
LockedLFS(uint32_t addr, uint32_t size, uint32_t block)
: CustomLFS(addr, size, block), orig(_lfs_config) {
_lfs_config.read = read;
_lfs_config.prog = prog;
_lfs_config.erase = erase;
_lfs_config.sync = sync;
}
};
4 changes: 2 additions & 2 deletions examples/companion_radio/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ MultiSerialInterface interface_manager;
DataStore store(InternalFS, QSPIFlash, rtc_clock);
#else
#if defined(EXTRAFS)
#include <CustomLFS.h>
CustomLFS ExtraFS(0xD4000, 0x19000, 128);
#include "LockedLFS.h"
LockedLFS ExtraFS(0xD4000, 0x19000, 128);
DataStore store(InternalFS, ExtraFS, rtc_clock);
#else
DataStore store(InternalFS, rtc_clock);
Expand Down