Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPIFFS.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_SPIFFS_SPIFFS_HXX_
35#define _FREERTOS_DRIVERS_SPIFFS_SPIFFS_HXX_
36
37#include <functional>
38
39#include "Devtab.hxx"
40#include "utils/Atomic.hxx"
41
42extern "C" {
43struct spiffs_t;
44typedef struct spiffs_t spiffs;
45};
46
48class SPIFFS : public FileSystem, private Atomic
49{
50public:
59 void mount(const char *mount_point) override
60 {
61 int result = do_mount();
62 if (result != 0)
63 {
64 // mounting failed, try reformatting
65 format();
66 result = do_mount();
67 }
68
69 if (result == 0)
70 {
71 name = mount_point;
73 {
75 }
76 formatted_ = false;
77 }
78 }
79
82 void format() override;
83
88 {
89 bool ret = false;
90 {
91 AtomicHolder h(this);
92 ret = anyDirty_;
93 anyDirty_ = false;
94 }
95 return ret;
96 }
97
102 {
103 mutex.lock();
104 for (unsigned int i = 0; i < numOpenFiles; i++)
105 {
106 if (files[i].inuse && files[i].dev == this && files[i].dirty)
107 {
108 this->fsync(&files[i]);
109 }
110 }
111 mutex.unlock();
112 }
113
116 inline static void extern_lock(struct spiffs_t *fs);
117
120 inline static void extern_unlock(struct spiffs_t *fs);
121
122protected:
128 SPIFFS(size_t physical_address, size_t size_on_disk,
129 size_t erase_block_size, size_t logical_block_size,
130 size_t logical_page_size, size_t max_num_open_descriptors = 16,
131 size_t cache_pages = 8,
132 std::function<void()> post_format_hook = nullptr);
133
135 ~SPIFFS();
136
139 void unmount();
140
146 static int flash_read(
147 struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *dst);
148
154 static int flash_write(
155 struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *src);
156
161 static int flash_erase(
162 struct spiffs_t *fs, unsigned addr, unsigned size);
163
168 virtual int32_t flash_read(uint32_t addr, uint32_t size, uint8_t *dst) = 0;
169
174 virtual int32_t flash_write(uint32_t addr, uint32_t size, uint8_t *src) = 0;
175
179 virtual int32_t flash_erase(uint32_t addr, uint32_t size) = 0;
180
182 spiffs *fs_;
183
184private:
186 struct OpenDir;
187
194 int open(File *file, const char *path, int flags, int mode) override;
195
200 int close(File *file) override;
201
205 int unlink(const char *path) override;
206
213 ssize_t read(File *file, void *buf, size_t count) override;
214
221 ssize_t write(File *file, const void *buf, size_t count) override;
222
230 off_t lseek(File* f, off_t offset, int whence) override;
231
236 int fstat(File* file, struct stat *stat) override;
237
242 int stat(const char *path, struct stat *stat) override;
243
248 int fsync(File *file) override;
249
253
254 int closedir(File *file) override;
255
260 File *opendir(File *file, const char *name) override;
261
266 struct dirent *readdir(File *file) override;
267
271 int errno_translate(int spiffs_error);
272
275 int do_mount();
276
278 std::function<void()> postFormatHook_;
279
282
284 uint8_t *workBuffer_;
285
287 uint32_t fdSpaceSize_;
288
290 uint8_t *fdSpace_;
291
293 uint32_t cacheSize_;
294
296 void *cache_;
297
299 bool formatted_ : 1;
300
302 bool anyDirty_ : 1;
303
305};
306
307#endif // _FREERTOS_DRIVERS_SPIFFS_SPIFFS_HXX_
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
static const unsigned int numOpenFiles
Definition Devtab.hxx:258
static File files[]
File descriptor pool.
Definition Devtab.hxx:261
const char * name
device name
Definition Devtab.hxx:266
static OSMutex mutex
mutual exclusion for fileio
Definition Devtab.hxx:264
Base class for all File systems.
Definition Devtab.hxx:280
This class provides a mutex API.
Definition OS.hxx:427
void lock()
Lock a mutex.
Definition OS.hxx:446
void unlock()
Unlock a mutex.
Definition OS.hxx:453
Generic SPIFFS base class.
Definition SPIFFS.hxx:49
OSMutex lock_
whole file system lock
Definition SPIFFS.hxx:281
static void extern_unlock(struct spiffs_t *fs)
Provide mutex unlock.
Definition SPIFFS.cxx:54
ssize_t read(File *file, void *buf, size_t count) override
Read from a file or device.
Definition SPIFFS.cxx:286
int errno_translate(int spiffs_error)
Translate a SPIFFS specific error number to a standard POSIX errno.
Definition SPIFFS.cxx:526
ssize_t write(File *file, const void *buf, size_t count) override
Write to a file or device.
Definition SPIFFS.cxx:303
File * opendir(File *file, const char *name) override
Open a directory.
Definition SPIFFS.cxx:474
int fsync(File *file) override
Synchronize (flush) a file to disk.
Definition SPIFFS.cxx:439
void mount(const char *mount_point) override
Mount the file system.
Definition SPIFFS.hxx:59
int open(File *file, const char *path, int flags, int mode) override
Open a file or device.
Definition SPIFFS.cxx:200
off_t lseek(File *f, off_t offset, int whence) override
Seek method.
Definition SPIFFS.cxx:326
int fstat(File *file, struct stat *stat) override
Get the status information of a file or device.
Definition SPIFFS.cxx:390
int close(File *file) override
Close a file or device.
Definition SPIFFS.cxx:253
static int flash_read(struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *dst)
SPIFFS callback to read flash.
Definition SPIFFS.cxx:80
int closedir(File *file) override
Close a directory.
Definition SPIFFS.cxx:455
void unmount()
Flushes caches and unmounts the filesystem.
Definition SPIFFS.cxx:151
spiffs * fs_
file system instance metadata
Definition SPIFFS.hxx:182
std::function< void()> postFormatHook_
callback to be called post a formating operation
Definition SPIFFS.hxx:278
void flush_cache()
Performs a sync on all files that have had a write but no fsync call since then.
Definition SPIFFS.hxx:101
int stat(const char *path, struct stat *stat) override
Get the status information of a file or device.
Definition SPIFFS.cxx:410
int unlink(const char *path) override
Remove a file.
Definition SPIFFS.cxx:271
virtual int32_t flash_erase(uint32_t addr, uint32_t size)=0
SPIFFS callback to erase flash, in context.
static int flash_write(struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *src)
SPIFFS callback to write flash.
Definition SPIFFS.cxx:87
uint32_t fdSpaceSize_
size in bytes of the fdSpace_
Definition SPIFFS.hxx:287
static void extern_lock(struct spiffs_t *fs)
Provide mutex lock.
Definition SPIFFS.cxx:49
uint8_t * fdSpace_
file descriptor metadata
Definition SPIFFS.hxx:290
bool is_any_dirty()
Definition SPIFFS.hxx:87
virtual int32_t flash_read(uint32_t addr, uint32_t size, uint8_t *dst)=0
SPIFFS callback to read flash, in context.
void * cache_
memory for cache
Definition SPIFFS.hxx:296
static int flash_erase(struct spiffs_t *fs, unsigned addr, unsigned size)
SPIFFS callback to erase flash.
Definition SPIFFS.cxx:94
struct dirent * readdir(File *file) override
Read the next entry in a directory.
Definition SPIFFS.cxx:502
~SPIFFS()
Destructor.
Definition SPIFFS.cxx:137
void format() override
Format the file system, all data will be lost.
Definition SPIFFS.cxx:172
virtual int32_t flash_write(uint32_t addr, uint32_t size, uint8_t *src)=0
SPIFFS callback to write flash, in context.
uint32_t cacheSize_
size in bytes of cache_
Definition SPIFFS.hxx:293
uint8_t * workBuffer_
work buffer for the file system
Definition SPIFFS.hxx:284
bool formatted_
has the file system been formatted since last reboot?
Definition SPIFFS.hxx:299
bool anyDirty_
Bit that is set to 1 when any write operation happens to this FS.
Definition SPIFFS.hxx:302
int do_mount()
Helper to mount the file system.
Definition SPIFFS.cxx:189
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
File information.
Definition Devtab.hxx:52
Open directory metadata structure.
Definition SPIFFS.cxx:164
Directory entry structure.
Definition dirent.h:49