Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
LittleFsStorageManager.h
Go to the documentation of this file.
1
11#pragma once
12
13#include "StorageManager.h"
14#include "lfs.h"
15#include <vector>
16#include <string>
17#include <FreeRTOS.h> // for FreeRTOS types and functions
18#include <semphr.h> // for SemaphoreHandle_t, StaticSemaphore_t, xSemaphoreCreateMutexStatic, xSemaphoreTake, xSemaphoreGive
19
26{
27public:
32
34 std::unique_ptr<StorageFileReader> openReader(const std::string& path) override;
35
40 bool mount() override;
41
46 bool unmount() override;
47
52 bool isMounted() const override;
53
59 bool exists(const std::string &path) override;
60
66 bool remove(const std::string &path) override;
67
74 bool rename(const std::string &from, const std::string &to) override;
75
82 bool readFile(const std::string &path, std::vector<uint8_t> &out) override;
83
92 bool readFileString(const std::string &path, uint32_t startPosition, uint32_t length, std::string &buffer);
93
100 bool writeFile(const std::string &path, const std::vector<uint8_t> &data) override;
101 bool writeFile(const std::string& path, const unsigned char* data, size_t size) override;
102
110 bool appendToFile(const std::string &path, const uint8_t *data, size_t size) override;
111
118 bool streamFile(const std::string &path, std::function<void(const uint8_t *, size_t)> chunkCallback) override;
119
125 size_t getFileSize(const std::string &path) override;
126
133 bool listDirectory(const std::string &path, std::vector<FileInfo> &out) override;
134
140 bool createDirectory(const std::string &path) override;
141
147 bool removeDirectory(const std::string &path) override;
148
153 bool formatStorage() override;
154
159 uintptr_t getFlashBase() const
160 {
161 return flashBase;
162 }
163
164 void formatInner(bool *result);
165
166private:
167 uintptr_t flashBase = 0;
168 size_t flashSize = 0;
169
170 static constexpr uint32_t FLASH_BASE = 0x101E0000;
171 static constexpr size_t FLASH_SIZE = 128 * 1024;
172
173 static constexpr size_t READ_SIZE = 256;
174 static constexpr size_t PROG_SIZE = 256;
175 static constexpr size_t BLOCK_SIZE = 4096;
176 static constexpr size_t BLOCK_COUNT = FLASH_SIZE / BLOCK_SIZE;
177 static constexpr size_t CACHE_SIZE = 256;
178 static constexpr size_t LOOKAHEAD_SIZE = 256;
179
180 lfs_t lfs;
181 struct lfs_config config;
182
183 bool mounted = false;
184
185 // --- LittleFS Thread Safety Lock (FreeRTOS mutex, statically allocated) ---
186
187 static StaticSemaphore_t lfs_mutex_buf;
188 static SemaphoreHandle_t lfs_mutex;
189
190 void configure();
191 static int lfs_read_cb(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
192 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);
193 static int lfs_erase_cb(const struct lfs_config *c, lfs_block_t block);
194 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);
195 static int lfs_erase_cb_singlecore(const struct lfs_config *c, lfs_block_t block);
196 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);
197 static int lfs_erase_cb_multicore(const struct lfs_config *c, lfs_block_t block);
198 static int lfs_lock(const struct lfs_config *c);
199 static int lfs_unlock(const struct lfs_config *c);
200
206};
Abstract interface for file and directory storage backends.
A LittleFS-based implementation of StorageManager, storing files in flash memory.
static constexpr size_t PROG_SIZE
bool unmount() override
Unmount the LittleFS filesystem.
bool rename(const std::string &from, const std::string &to) override
Rename a file or directory.
static constexpr size_t BLOCK_COUNT
bool formatStorage() override
Format the filesystem.
bool appendToFile(const std::string &path, const uint8_t *data, size_t size) override
Append data to a file.
static int lfs_erase_cb_multicore(const struct lfs_config *c, lfs_block_t block)
static int lfs_lock(const struct lfs_config *c)
bool writeFile(const std::string &path, const std::vector< uint8_t > &data) override
Write a byte vector to a file (overwrite).
uintptr_t getFlashBase() const
get Flash base address
void formatInner(bool *result)
std::unique_ptr< StorageFileReader > openReader(const std::string &path) override
open a file for streaming read line access.
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 StaticSemaphore_t lfs_mutex_buf
static constexpr size_t LOOKAHEAD_SIZE
bool autoMountIfNeeded()
Mount automatically if not mounted yet.
static int lfs_unlock(const struct lfs_config *c)
bool streamFile(const std::string &path, std::function< void(const uint8_t *, size_t)> chunkCallback) override
Stream a file in chunks using a callback.
bool createDirectory(const std::string &path) override
Create a new directory.
bool removeDirectory(const std::string &path) override
Remove a directory.
bool remove(const std::string &path) override
Remove a file or directory.
static constexpr size_t FLASH_SIZE
128 KB
bool isMounted() const override
Check if the filesystem is mounted.
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)
LittleFsStorageManager()
Construct the manager and configure the filesystem.
bool readFileString(const std::string &path, uint32_t startPosition, uint32_t length, std::string &buffer)
Read a file string into a memory buffer.
static constexpr size_t CACHE_SIZE
bool mount() override
Mount the LittleFS filesystem.
static constexpr uint32_t FLASH_BASE
static int lfs_read_cb(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
bool exists(const std::string &path) override
Check if a file or directory exists.
static int lfs_erase_cb_singlecore(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)
bool listDirectory(const std::string &path, std::vector< FileInfo > &out) override
List files in a directory.
size_t getFileSize(const std::string &path) override
Get the size of a file.
bool readFile(const std::string &path, std::vector< uint8_t > &out) override
Read a file into a byte vector.
static SemaphoreHandle_t lfs_mutex
static int lfs_erase_cb(const struct lfs_config *c, lfs_block_t block)
static constexpr size_t READ_SIZE
static constexpr size_t BLOCK_SIZE
Abstract base class for storage access and file operations.