Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
LittleFsStorageManager Class Reference

A LittleFS-based implementation of StorageManager, storing files in flash memory. More...

#include <LittleFsStorageManager.h>

+ Inheritance diagram for LittleFsStorageManager:
+ Collaboration diagram for LittleFsStorageManager:

Public Member Functions

 LittleFsStorageManager ()
 Construct the manager and configure the filesystem.
 
std::unique_ptr< StorageFileReaderopenReader (const std::string &path) override
 open a file for streaming read line access.
 
bool mount () override
 Mount the LittleFS filesystem.
 
bool unmount () override
 Unmount the LittleFS filesystem.
 
bool isMounted () const override
 Check if the filesystem is mounted.
 
bool exists (const std::string &path) override
 Check if a file or directory exists.
 
bool remove (const std::string &path) override
 Remove a file or directory.
 
bool rename (const std::string &from, const std::string &to) override
 Rename a file or directory.
 
bool readFile (const std::string &path, std::vector< uint8_t > &out) override
 Read a file into a byte vector.
 
bool readFileString (const std::string &path, uint32_t startPosition, uint32_t length, std::string &buffer)
 Read a file string into a memory buffer.
 
bool writeFile (const std::string &path, const std::vector< uint8_t > &data) override
 Write a byte vector to a file (overwrite).
 
bool writeFile (const std::string &path, const unsigned char *data, size_t size) override
 Write from a raw buffer.
 
bool appendToFile (const std::string &path, const uint8_t *data, size_t size) override
 Append data to a file.
 
bool streamFile (const std::string &path, std::function< void(const uint8_t *, size_t)> chunkCallback) override
 Stream a file in chunks using a callback.
 
size_t getFileSize (const std::string &path) override
 Get the size of a file.
 
bool listDirectory (const std::string &path, std::vector< FileInfo > &out) override
 List files in a directory.
 
bool createDirectory (const std::string &path) override
 Create a new directory.
 
bool removeDirectory (const std::string &path) override
 Remove a directory.
 
bool formatStorage () override
 Format the filesystem.
 
uintptr_t getFlashBase () const
 get Flash base address
 
void formatInner (bool *result)
 
- Public Member Functions inherited from StorageManager
virtual ~StorageManager ()=default
 

Private Member Functions

void configure ()
 
bool autoMountIfNeeded ()
 Mount automatically if not mounted yet.
 

Static Private Member Functions

static int lfs_read_cb (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
 
static int lfs_prog_cb (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
 
static int lfs_erase_cb (const struct lfs_config *c, lfs_block_t block)
 
static int lfs_prog_cb_singlecore (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
 
static int lfs_erase_cb_singlecore (const struct lfs_config *c, lfs_block_t block)
 
static int lfs_prog_cb_multicore (const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
 
static int lfs_erase_cb_multicore (const struct lfs_config *c, lfs_block_t block)
 
static int lfs_lock (const struct lfs_config *c)
 
static int lfs_unlock (const struct lfs_config *c)
 

Private Attributes

uintptr_t flashBase = 0
 
size_t flashSize = 0
 
lfs_t lfs
 
struct lfs_config config
 
bool mounted = false
 

Static Private Attributes

static constexpr uint32_t FLASH_BASE = 0x101E0000
 
static constexpr size_t FLASH_SIZE = 128 * 1024
 128 KB
 
static constexpr size_t READ_SIZE = 256
 
static constexpr size_t PROG_SIZE = 256
 
static constexpr size_t BLOCK_SIZE = 4096
 
static constexpr size_t BLOCK_COUNT = FLASH_SIZE / BLOCK_SIZE
 
static constexpr size_t CACHE_SIZE = 256
 
static constexpr size_t LOOKAHEAD_SIZE = 256
 
static StaticSemaphore_t lfs_mutex_buf
 
static SemaphoreHandle_t lfs_mutex = xSemaphoreCreateMutexStatic(&lfs_mutex_buf)
 

Detailed Description

Automatically mounts on first access if not already mounted.

Definition at line 25 of file LittleFsStorageManager.h.

Constructor & Destructor Documentation

◆ LittleFsStorageManager()

LittleFsStorageManager::LittleFsStorageManager ( )

Definition at line 27 of file LittleFsStorageManager.cpp.

References configure().

+ Here is the call graph for this function:

Member Function Documentation

◆ appendToFile()

bool LittleFsStorageManager::appendToFile ( const std::string &  path,
const uint8_t *  data,
size_t  size 
)
overridevirtual
Parameters
pathPath to the file.
dataPointer to data.
sizeSize of data in bytes.
Returns
true if appended.

Implements StorageManager.

Definition at line 315 of file LittleFsStorageManager.cpp.

316{
317 lfs_file_t file;
318 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND) < 0)
319 {
320 printf("[LittleFS] appendToFile: open failed for '%s'\n", path.c_str());
321 lfs_file_close(&lfs, &file);
322 return false;
323 }
324 int written = lfs_file_write(&lfs, &file, data, size);
325 if (written < 0)
326 {
327 printf("[LittleFS] appendToFile: write failed for '%s'\n", path.c_str());
328 lfs_file_close(&lfs, &file);
329 return false;
330 }
331 lfs_file_close(&lfs, &file);
332 return written == (int)size;
333}

References lfs.

◆ autoMountIfNeeded()

bool LittleFsStorageManager::autoMountIfNeeded ( )
private
Returns
true if mounted successfully or already mounted.

◆ configure()

void LittleFsStorageManager::configure ( )
private

Definition at line 169 of file LittleFsStorageManager.cpp.

170{
171 flashBase = reinterpret_cast<uintptr_t>(&__flash_lfs_start);
172 flashSize = reinterpret_cast<uintptr_t>(&__flash_lfs_end) - flashBase;
173
174 std::memset(&config, 0, sizeof(config));
175
176 config.context = this;
177 config.read = lfs_read_cb;
178 config.prog = lfs_prog_cb;
179 config.erase = lfs_erase_cb;
180 config.sync = [](const struct lfs_config *) -> int
181 { return 0; };
182
183 config.read_size = 256;
184 config.prog_size = 256;
185 config.block_size = 4096;
186 config.block_count = flashSize / config.block_size;
187 config.cache_size = 256;
188 config.lookahead_size = 256;
189 config.block_cycles = 500;
190 config.compact_thresh = (lfs_size_t)-1;
191#if defined(LFS_THREADSAFE)
192 config.lock = lfs_lock;
193 config.unlock = lfs_unlock;
194#endif
195 // printf("[LittleFS] Configured, flash base: 0x%08x, size: %zu bytes (%zu blocks)\n",
196 // static_cast<unsigned>(flashBase), flashSize, config.block_count);
197}
uint8_t __flash_lfs_start
uint8_t __flash_lfs_end
static int lfs_lock(const struct lfs_config *c)
static int lfs_unlock(const struct lfs_config *c)
static int lfs_prog_cb(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
static int lfs_read_cb(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
static int lfs_erase_cb(const struct lfs_config *c, lfs_block_t block)

References __flash_lfs_end, __flash_lfs_start, config, flashBase, flashSize, lfs_erase_cb(), lfs_lock(), lfs_prog_cb(), lfs_read_cb(), and lfs_unlock().

Referenced by LittleFsStorageManager().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createDirectory()

bool LittleFsStorageManager::createDirectory ( const std::string &  path)
overridevirtual
Parameters
pathPath of directory.
Returns
true if created.

Implements StorageManager.

Definition at line 403 of file LittleFsStorageManager.cpp.

404{
405 TRACE("[LittleFS] Creating directory '%s'\n", path.c_str());
406 if (lfs_mkdir(&lfs, path.c_str()) < 0)
407 {
408 printf("[LittleFS] Failed to create directory '%s'\n", path.c_str());
409 return false;
410 }
411 TRACE("[LittleFS] Directory '%s' created successfully\n", path.c_str());
412 return true;
413}
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187

References lfs, and TRACE.

◆ exists()

bool LittleFsStorageManager::exists ( const std::string &  path)
overridevirtual
Parameters
pathPath to the file or directory.
Returns
true if it exists.

Implements StorageManager.

Definition at line 232 of file LittleFsStorageManager.cpp.

233{
234 struct lfs_info info;
235 int err = lfs_stat(&lfs, path.c_str(), &info);
236 return (err == 0);
237}

References lfs.

◆ formatInner()

void LittleFsStorageManager::formatInner ( bool *  result)

◆ formatStorage()

bool LittleFsStorageManager::formatStorage ( )
overridevirtual
Returns
true if formatted successfully.

Implements StorageManager.

Definition at line 420 of file LittleFsStorageManager.cpp.

421{
422 // Note that this function will unmount the filesystem before formatting
423 // and remount it afterwards if successful.
424
425
426
427 bool result = false;
428
429 if(mounted)
430 {
431 TRACE("[LittleFs] Unmounting before format\n");
432 unmount();
433 }
434 int err = lfs_format(&lfs, &config);
435 result = (err == 0);
436
437 if (result)
438 {
439 printf("[LittleFs] Format successful\n");
440 mount();
441 }
442 else
443 {
444 printf("[LittleFs] Format failed\n");
445 }
446 return result;
447}
bool unmount() override
Unmount the LittleFS filesystem.
bool mount() override
Mount the LittleFS filesystem.

References config, lfs, mount(), mounted, TRACE, and unmount().

+ Here is the call graph for this function:

◆ getFileSize()

size_t LittleFsStorageManager::getFileSize ( const std::string &  path)
overridevirtual
Parameters
pathPath to the file.
Returns
Size in bytes, or 0 on error.

Implements StorageManager.

Definition at line 360 of file LittleFsStorageManager.cpp.

361{
362 lfs_file_t file;
363 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_RDONLY) < 0)
364 {
365 printf("[LittleFS] getFileSize: open failed for '%s'\n", path.c_str());
366 return 0;
367 }
368 lfs_soff_t size = lfs_file_size(&lfs, &file);
369 lfs_file_close(&lfs, &file);
370 return size < 0 ? 0 : size;
371}

References lfs.

◆ getFlashBase()

uintptr_t LittleFsStorageManager::getFlashBase ( ) const
inline
Returns
Flash base address

Definition at line 159 of file LittleFsStorageManager.h.

160 {
161 return flashBase;
162 }

References flashBase.

Referenced by lfs_erase_cb_flashsafe(), and lfs_prog_multicore().

+ Here is the caller graph for this function:

◆ isMounted()

bool LittleFsStorageManager::isMounted ( ) const
overridevirtual
Returns
true if mounted.

Implements StorageManager.

Definition at line 227 of file LittleFsStorageManager.cpp.

228{
229 return mounted;
230}

References mounted.

◆ lfs_erase_cb()

int LittleFsStorageManager::lfs_erase_cb ( const struct lfs_config *  c,
lfs_block_t  block 
)
staticprivate

Definition at line 110 of file LittleFsStorageManager.cpp.

111{
112#if (configNUM_CORES > 1)
113 return lfs_erase_cb_multicore(c, block);
114#else
115 return lfs_erase_cb_singlecore(c, block);
116#endif
117}
static int lfs_erase_cb_multicore(const struct lfs_config *c, lfs_block_t block)
static int lfs_erase_cb_singlecore(const struct lfs_config *c, lfs_block_t block)

References lfs_erase_cb_multicore(), and lfs_erase_cb_singlecore().

Referenced by configure().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lfs_erase_cb_multicore()

int LittleFsStorageManager::lfs_erase_cb_multicore ( const struct lfs_config *  c,
lfs_block_t  block 
)
staticprivate

Definition at line 155 of file LittleFsStorageManager.cpp.

156{
157 auto *self = static_cast<LittleFsStorageManager *>(c->context);
158 uintptr_t addr = static_cast<LittleFsStorageManager *>(c->context)->flashBase + block * c->block_size;
159 int err = lfs_erase_cb_flashsafe(c, block);
160 return err;
161}
static int lfs_erase_cb_flashsafe(const struct lfs_config *c, lfs_block_t block)
A LittleFS-based implementation of StorageManager, storing files in flash memory.

References flashBase, and lfs_erase_cb_flashsafe().

Referenced by lfs_erase_cb().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lfs_erase_cb_singlecore()

int LittleFsStorageManager::lfs_erase_cb_singlecore ( const struct lfs_config *  c,
lfs_block_t  block 
)
staticprivate

Definition at line 119 of file LittleFsStorageManager.cpp.

120{
121 auto *self = static_cast<LittleFsStorageManager *>(c->context);
122 uintptr_t addr = self->flashBase + block * c->block_size;
123 uint32_t ints = save_and_disable_interrupts();
124 flash_range_erase(addr - XIP_BASE, c->block_size);
125 restore_interrupts(ints);
126 return 0;
127}

References flashBase.

Referenced by lfs_erase_cb().

+ Here is the caller graph for this function:

◆ lfs_lock()

int LittleFsStorageManager::lfs_lock ( const struct lfs_config *  c)
staticprivate

Definition at line 32 of file LittleFsStorageManager.cpp.

32 {
33 assert(lfs_mutex != nullptr);
34 return (xSemaphoreTake(lfs_mutex, portMAX_DELAY) == pdTRUE) ? 0 : -1;
35}
static SemaphoreHandle_t lfs_mutex

References lfs_mutex.

Referenced by configure().

+ Here is the caller graph for this function:

◆ lfs_prog_cb()

int LittleFsStorageManager::lfs_prog_cb ( const struct lfs_config *  c,
lfs_block_t  block,
lfs_off_t  off,
const void *  buffer,
lfs_size_t  size 
)
staticprivate

Definition at line 51 of file LittleFsStorageManager.cpp.

53{
54
55#if (configNUM_CORES > 1)
56 return lfs_prog_cb_multicore(c, block, off, buffer, size);
57#else
58 return lfs_prog_cb_singlecore(c, block, off, buffer, size);
59#endif
60}
static int lfs_prog_cb_multicore(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
static int lfs_prog_cb_singlecore(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)

References lfs_prog_cb_multicore(), and lfs_prog_cb_singlecore().

Referenced by configure().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lfs_prog_cb_multicore()

int LittleFsStorageManager::lfs_prog_cb_multicore ( const struct lfs_config *  c,
lfs_block_t  block,
lfs_off_t  off,
const void *  buffer,
lfs_size_t  size 
)
staticprivate

Definition at line 103 of file LittleFsStorageManager.cpp.

104{
105 auto *self = static_cast<LittleFsStorageManager *>(c->context);
106 uintptr_t addr = self->flashBase + block * c->block_size + off;
107 return lfs_prog_multicore(c, block, off, buffer, size);
108}
static int __not_in_flash_func() lfs_prog_multicore(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)

References flashBase, and lfs_prog_multicore().

Referenced by lfs_prog_cb().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lfs_prog_cb_singlecore()

int LittleFsStorageManager::lfs_prog_cb_singlecore ( const struct lfs_config *  c,
lfs_block_t  block,
lfs_off_t  off,
const void *  buffer,
lfs_size_t  size 
)
staticprivate

Definition at line 62 of file LittleFsStorageManager.cpp.

64{
65 auto *self = static_cast<LittleFsStorageManager *>(c->context);
66 uintptr_t addr = self->flashBase + block * c->block_size + off;
67 uint32_t ints = save_and_disable_interrupts();
68 flash_range_program(addr - XIP_BASE, reinterpret_cast<const uint8_t *>(buffer), size);
69 restore_interrupts(ints);
70 return 0;
71}

References flashBase.

Referenced by lfs_prog_cb().

+ Here is the caller graph for this function:

◆ lfs_read_cb()

int LittleFsStorageManager::lfs_read_cb ( const struct lfs_config *  c,
lfs_block_t  block,
lfs_off_t  off,
void *  buffer,
lfs_size_t  size 
)
staticprivate

Definition at line 41 of file LittleFsStorageManager.cpp.

43{
44 auto *self = static_cast<LittleFsStorageManager *>(c->context);
45 uintptr_t addr = self->flashBase + block * c->block_size + off;
46 std::memcpy(buffer, reinterpret_cast<const void *>(addr), size);
47 return 0;
48}

References flashBase.

Referenced by configure().

+ Here is the caller graph for this function:

◆ lfs_unlock()

int LittleFsStorageManager::lfs_unlock ( const struct lfs_config *  c)
staticprivate

Definition at line 37 of file LittleFsStorageManager.cpp.

37 {
38 return (xSemaphoreGive(lfs_mutex) == pdTRUE) ? 0 : -1;
39}

References lfs_mutex.

Referenced by configure().

+ Here is the caller graph for this function:

◆ listDirectory()

bool LittleFsStorageManager::listDirectory ( const std::string &  path,
std::vector< FileInfo > &  out 
)
overridevirtual
Parameters
pathPath to directory.
outVector to receive file entries.
Returns
true if listed successfully.

Implements StorageManager.

Definition at line 373 of file LittleFsStorageManager.cpp.

374{
375 if(!mounted)
376 {
377 mount();
378 }
379 lfs_dir_t dir;
380 struct lfs_info info = {};
381
382 if (lfs_dir_open(&lfs, &dir, path.c_str()) < 0)
383 return false;
384
385 while (lfs_dir_read(&lfs, &dir, &info) > 0)
386 {
387 if (strcmp(info.name, ".") == 0 || strcmp(info.name, "..") == 0)
388 continue;
389
390 FileInfo entry;
391 entry.name = info.name;
392 entry.size = info.size;
393 entry.isDirectory = (info.type == LFS_TYPE_DIR);
394 entry.isReadOnly = false; // LittleFS doesn't expose this, so hardcoded
395 out.push_back(entry);
396 }
397
398 lfs_dir_close(&lfs, &dir);
399 return true;
400}
Structure representing metadata for a file or directory.
bool isDirectory
True if item is a directory.
size_t size
Size in bytes.
bool isReadOnly
True if item is read-only.
std::string name
File or directory name.

References FileInfo::isDirectory, FileInfo::isReadOnly, lfs, mount(), mounted, FileInfo::name, and FileInfo::size.

+ Here is the call graph for this function:

◆ mount()

bool LittleFsStorageManager::mount ( )
overridevirtual
Returns
true if mounted successfully.

Implements StorageManager.

Definition at line 199 of file LittleFsStorageManager.cpp.

200{
201 if( mounted)
202 {
203 TRACE("[LittleFs] Already mounted\n");
204 return true;
205 }
206 TRACE("[LittleFs] Mounting LittleFS...\n");
207 int err = lfs_mount(&lfs, &config);
208 if (err < 0)
209 {
210 printf("[LittleFs] Mount failed with error %d\n", err);
211 return false;
212 }
213 TRACE("[LittleFs] Mounted successfully\n");
214 return mounted = true;
215}

References config, lfs, mounted, and TRACE.

Referenced by formatStorage(), listDirectory(), and openReader().

+ Here is the caller graph for this function:

◆ openReader()

std::unique_ptr< StorageFileReader > LittleFsStorageManager::openReader ( const std::string &  path)
overridevirtual

Implements StorageManager.

Definition at line 449 of file LittleFsStorageManager.cpp.

449 {
450 if (!mount()) return nullptr;
451 auto reader = std::make_unique<LittleFsFileReader>(&lfs);
452 if (!reader) {
453 return nullptr;
454 }
455 if (!reader->open(path)) return nullptr;
456 return reader;
457}

References lfs, and mount().

+ Here is the call graph for this function:

◆ readFile()

bool LittleFsStorageManager::readFile ( const std::string &  path,
std::vector< uint8_t > &  out 
)
overridevirtual
Parameters
pathPath to the file.
outOutput vector with file contents.
Returns
true if successful.

Implements StorageManager.

Definition at line 249 of file LittleFsStorageManager.cpp.

250{
251 lfs_file_t file;
252 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_RDONLY) < 0)
253 return false;
254
255 lfs_soff_t size = lfs_file_size(&lfs, &file);
256 if (size < 0)
257 {
258 lfs_file_close(&lfs, &file);
259 return false;
260 }
261
262 out.resize(size);
263 int bytes = lfs_file_read(&lfs, &file, out.data(), size);
264 lfs_file_close(&lfs, &file);
265 return (bytes >= 0) && (bytes == size);
266}

References lfs.

◆ readFileString()

bool LittleFsStorageManager::readFileString ( const std::string &  path,
uint32_t  startPosition,
uint32_t  length,
std::string &  buffer 
)
virtual
Parameters
pathPath to the file.
startPositionStart position in the file.
lengthLength of data to read.
bufferOutput string to fill with data.
Returns
true if successful.

Implements StorageManager.

Definition at line 268 of file LittleFsStorageManager.cpp.

268 {
269 lfs_file_t file;
270 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_RDONLY) < 0)
271 return false;
272
273 lfs_soff_t size = lfs_file_size(&lfs, &file);
274 if (size < 0 || startPosition >= size)
275 {
276 lfs_file_close(&lfs, &file);
277 return false;
278 }
279
280 if (startPosition + length > size)
281 length = size - startPosition;
282
283 buffer.resize(length);
284 lfs_file_seek(&lfs, &file, startPosition, LFS_SEEK_SET);
285 int bytesRead = lfs_file_read(&lfs, &file, buffer.data(), length);
286 lfs_file_close(&lfs, &file);
287 return (bytesRead >= 0) && (bytesRead == static_cast<int>(length));
288}

References lfs.

◆ remove()

bool LittleFsStorageManager::remove ( const std::string &  path)
overridevirtual
Parameters
pathPath to the file or directory.
Returns
true if removed.

Implements StorageManager.

Definition at line 239 of file LittleFsStorageManager.cpp.

240{
241 return lfs_remove(&lfs, path.c_str()) == 0;
242}

References lfs.

◆ removeDirectory()

bool LittleFsStorageManager::removeDirectory ( const std::string &  path)
overridevirtual
Parameters
pathPath of directory.
Returns
true if removed.

Implements StorageManager.

Definition at line 415 of file LittleFsStorageManager.cpp.

416{
417 return lfs_remove(&lfs, path.c_str()) == 0;
418}

References lfs.

◆ rename()

bool LittleFsStorageManager::rename ( const std::string &  from,
const std::string &  to 
)
overridevirtual
Parameters
fromSource path.
toDestination path.
Returns
true if renamed.

Implements StorageManager.

Definition at line 244 of file LittleFsStorageManager.cpp.

245{
246 return lfs_rename(&lfs, from.c_str(), to.c_str()) == 0;
247}

References lfs.

◆ streamFile()

bool LittleFsStorageManager::streamFile ( const std::string &  path,
std::function< void(const uint8_t *, size_t)>  chunkCallback 
)
overridevirtual
Parameters
pathPath to the file.
chunkCallbackCallback to receive chunks.
Returns
true if streamed successfully.

Implements StorageManager.

Definition at line 335 of file LittleFsStorageManager.cpp.

336{
337 lfs_file_t file;
338 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_RDONLY) < 0)
339 return false;
340
341 uint8_t buf[HTTP_BUFFER_SIZE];
342 int readBytes;
343 bool success = true;
344
345 while ((readBytes = lfs_file_read(&lfs, &file, buf, sizeof(buf))) > 0)
346 {
347 chunkCallback(buf, readBytes);
348 }
349
350 if (readBytes < 0)
351 {
352 printf("[LittleFS] streamFile: read failed for '%s'\n", path.c_str());
353 success = false;
354 }
355
356 lfs_file_close(&lfs, &file);
357 return success;
358}
#define HTTP_BUFFER_SIZE
Size of the HTTP buffer for request/response data.

References HTTP_BUFFER_SIZE, and lfs.

◆ unmount()

bool LittleFsStorageManager::unmount ( )
overridevirtual
Returns
true if unmounted successfully.

Implements StorageManager.

Definition at line 217 of file LittleFsStorageManager.cpp.

218{
219 if (mounted)
220 {
221 lfs_unmount(&lfs);
222 mounted = false;
223 }
224 return true;
225}

References lfs, and mounted.

Referenced by formatStorage().

+ Here is the caller graph for this function:

◆ writeFile() [1/2]

bool LittleFsStorageManager::writeFile ( const std::string &  path,
const std::vector< uint8_t > &  data 
)
overridevirtual
Parameters
pathPath to the file.
dataData to write.
Returns
true if successful.

Implements StorageManager.

Definition at line 290 of file LittleFsStorageManager.cpp.

291{
292 lfs_file_t file;
293 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) < 0)
294 return false;
295 int written = lfs_file_write(&lfs, &file, data.data(), data.size());
296 int closed = lfs_file_close(&lfs, &file);
297 return (written == static_cast<int>(data.size())) && (closed == 0);
298}

References lfs.

◆ writeFile() [2/2]

bool LittleFsStorageManager::writeFile ( const std::string &  path,
const unsigned char *  data,
size_t  size 
)
overridevirtual

Implements StorageManager.

Definition at line 300 of file LittleFsStorageManager.cpp.

301{
302 if (!mounted)
303 return false;
304
305 lfs_file_t file;
306 if (lfs_file_open(&lfs, &file, path.c_str(), LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) < 0)
307 return false;
308
309 int written = lfs_file_write(&lfs, &file, data, size);
310 lfs_file_close(&lfs, &file);
311
312 return (written == static_cast<int>(size));
313}

References lfs, and mounted.

Member Data Documentation

◆ BLOCK_COUNT

constexpr size_t LittleFsStorageManager::BLOCK_COUNT = FLASH_SIZE / BLOCK_SIZE
staticconstexprprivate

Definition at line 176 of file LittleFsStorageManager.h.

◆ BLOCK_SIZE

constexpr size_t LittleFsStorageManager::BLOCK_SIZE = 4096
staticconstexprprivate

Definition at line 175 of file LittleFsStorageManager.h.

◆ CACHE_SIZE

constexpr size_t LittleFsStorageManager::CACHE_SIZE = 256
staticconstexprprivate

Definition at line 177 of file LittleFsStorageManager.h.

◆ config

struct lfs_config LittleFsStorageManager::config
private

Definition at line 181 of file LittleFsStorageManager.h.

Referenced by configure(), formatStorage(), and mount().

◆ FLASH_BASE

constexpr uint32_t LittleFsStorageManager::FLASH_BASE = 0x101E0000
staticconstexprprivate

Definition at line 170 of file LittleFsStorageManager.h.

◆ FLASH_SIZE

constexpr size_t LittleFsStorageManager::FLASH_SIZE = 128 * 1024
staticconstexprprivate

Definition at line 171 of file LittleFsStorageManager.h.

◆ flashBase

uintptr_t LittleFsStorageManager::flashBase = 0
private

◆ flashSize

size_t LittleFsStorageManager::flashSize = 0
private

Definition at line 168 of file LittleFsStorageManager.h.

Referenced by configure().

◆ lfs

◆ lfs_mutex

SemaphoreHandle_t LittleFsStorageManager::lfs_mutex = xSemaphoreCreateMutexStatic(&lfs_mutex_buf)
staticprivate

Definition at line 188 of file LittleFsStorageManager.h.

Referenced by lfs_lock(), and lfs_unlock().

◆ lfs_mutex_buf

StaticSemaphore_t LittleFsStorageManager::lfs_mutex_buf
staticprivate

Definition at line 187 of file LittleFsStorageManager.h.

◆ LOOKAHEAD_SIZE

constexpr size_t LittleFsStorageManager::LOOKAHEAD_SIZE = 256
staticconstexprprivate

Definition at line 178 of file LittleFsStorageManager.h.

◆ mounted

bool LittleFsStorageManager::mounted = false
private

◆ PROG_SIZE

constexpr size_t LittleFsStorageManager::PROG_SIZE = 256
staticconstexprprivate

Definition at line 174 of file LittleFsStorageManager.h.

◆ READ_SIZE

constexpr size_t LittleFsStorageManager::READ_SIZE = 256
staticconstexprprivate

Definition at line 173 of file LittleFsStorageManager.h.


The documentation for this class was generated from the following files: