Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPIFFS Class Referenceabstract

Generic SPIFFS base class. More...

#include <SPIFFS.hxx>

Inheritance diagram for SPIFFS:
FileSystem Atomic FileIO SpiSPIFFS Stm32SPIFFS TiSPIFFS< ERASE_PAGE_SIZE >

Classes

struct  OpenDir
 Open directory metadata structure. More...
 

Public Member Functions

void mount (const char *mount_point) override
 Mount the file system.
 
void format () override
 Format the file system, all data will be lost.
 
bool is_any_dirty ()
 
void flush_cache ()
 Performs a sync on all files that have had a write but no fsync call since then.
 
- Public Member Functions inherited from FileSystem
 FileSystem ()
 Constructor.
 
virtual ~FileSystem ()
 Destructor.
 

Static Public Member Functions

static void extern_lock (struct spiffs_t *fs)
 Provide mutex lock.
 
static void extern_unlock (struct spiffs_t *fs)
 Provide mutex unlock.
 
- Static Public Member Functions inherited from FileSystem
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int unlink (struct _reent *reent, const char *path)
 Remove a file.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int fsync (int fd)
 Synchronize (flush) a file to disk.
 
static int closedir (DIR *dirp)
 Close a directory.
 
static DIRopendir (const char *name)
 Open a directory.
 
static struct direntreaddir (DIR *dirp)
 Read the next entry in a directory.
 
- Static Public Member Functions inherited from FileIO
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 

Protected Member Functions

 SPIFFS (size_t physical_address, size_t size_on_disk, size_t erase_block_size, size_t logical_block_size, size_t logical_page_size, size_t max_num_open_descriptors=16, size_t cache_pages=8, std::function< void()> post_format_hook=nullptr)
 Constructor.
 
 ~SPIFFS ()
 Destructor.
 
void unmount ()
 Flushes caches and unmounts the filesystem.
 
virtual int32_t flash_read (uint32_t addr, uint32_t size, uint8_t *dst)=0
 SPIFFS callback to read flash, in context.
 
virtual int32_t flash_write (uint32_t addr, uint32_t size, uint8_t *src)=0
 SPIFFS callback to write flash, in context.
 
virtual int32_t flash_erase (uint32_t addr, uint32_t size)=0
 SPIFFS callback to erase flash, in context.
 
- Protected Member Functions inherited from FileIO
 FileIO (const char *name)
 Constructor.
 
virtual ~FileIO ()
 Destructor.
 
virtual int ioctl (File *file, unsigned long int key, unsigned long data)
 Request an ioctl transaction.
 
virtual int fcntl (File *file, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
virtual bool select (File *file, int mode)
 Device select method.
 

Static Protected Member Functions

static int flash_read (struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *dst)
 SPIFFS callback to read flash.
 
static int flash_write (struct spiffs_t *fs, unsigned addr, unsigned size, uint8_t *src)
 SPIFFS callback to write flash.
 
static int flash_erase (struct spiffs_t *fs, unsigned addr, unsigned size)
 SPIFFS callback to erase flash.
 
- Static Protected Member Functions inherited from FileIO
static int fd_alloc (void)
 Allocate a free file descriptor.
 
static void fd_free (int fd)
 Free up a file descriptor.
 
static Filefile_lookup (int fd)
 Looks up a reference to a File corresponding to a given file descriptor.
 
static int fd_lookup (File *file)
 Looks up a file descriptor corresponding to a given File reference.
 

Protected Attributes

spiffs * fs_
 file system instance metadata
 
- Protected Attributes inherited from FileIO
const char * name
 device name
 

Private Member Functions

int open (File *file, const char *path, int flags, int mode) override
 Open a file or device.
 
int close (File *file) override
 Close a file or device.
 
int unlink (const char *path) override
 Remove a file.
 
ssize_t read (File *file, void *buf, size_t count) override
 Read from a file or device.
 
ssize_t write (File *file, const void *buf, size_t count) override
 Write to a file or device.
 
off_t lseek (File *f, off_t offset, int whence) override
 Seek method.
 
int fstat (File *file, struct stat *stat) override
 Get the status information of a file or device.
 
int stat (const char *path, struct stat *stat) override
 Get the status information of a file or device.
 
int fsync (File *file) override
 Synchronize (flush) a file to disk.
 
int closedir (File *file) override
 Close a directory.
 
Fileopendir (File *file, const char *name) override
 Open a directory.
 
struct direntreaddir (File *file) override
 Read the next entry in a directory.
 
int errno_translate (int spiffs_error)
 Translate a SPIFFS specific error number to a standard POSIX errno.
 
int do_mount ()
 Helper to mount the file system.
 
 DISALLOW_COPY_AND_ASSIGN (SPIFFS)
 
- Private Member Functions inherited from Atomic
void lock ()
 
void unlock ()
 

Private Attributes

std::function< void()> postFormatHook_
 callback to be called post a formating operation
 
OSMutex lock_
 whole file system lock
 
uint8_t * workBuffer_
 work buffer for the file system
 
uint32_t fdSpaceSize_
 size in bytes of the fdSpace_
 
uint8_t * fdSpace_
 file descriptor metadata
 
uint32_t cacheSize_
 size in bytes of cache_
 
void * cache_
 memory for cache
 
bool formatted_: 1
 has the file system been formatted since last reboot?
 
bool anyDirty_: 1
 Bit that is set to 1 when any write operation happens to this FS.
 

Additional Inherited Members

- Static Protected Attributes inherited from FileIO
static const unsigned int numOpenFiles = 20
 
static File files []
 File descriptor pool.
 
static OSMutex mutex
 mutual exclusion for fileio
 

Detailed Description

Generic SPIFFS base class.

Definition at line 48 of file SPIFFS.hxx.

Constructor & Destructor Documentation

◆ SPIFFS()

SPIFFS::SPIFFS ( size_t  physical_address,
size_t  size_on_disk,
size_t  erase_block_size,
size_t  logical_block_size,
size_t  logical_page_size,
size_t  max_num_open_descriptors = 16,
size_t  cache_pages = 8,
std::function< void()>  post_format_hook = nullptr 
)
protected

Constructor.

Parameters
post_format_hookmethod to be called after a clean format of the file system. This allows the user to prime a clean or factory reset file system with an initial set files.

Definition at line 102 of file SPIFFS.cxx.

◆ ~SPIFFS()

SPIFFS::~SPIFFS ( )
protected

Destructor.

Definition at line 137 of file SPIFFS.cxx.

Member Function Documentation

◆ close()

int SPIFFS::close ( File file)
overrideprivatevirtual

Close a file or device.

Parameters
filefile reference for this device
fdfile descriptor to close
Returns
0 upon success, -1 upon failure with errno containing the cause

Implements FileIO.

Definition at line 253 of file SPIFFS.cxx.

◆ closedir()

int SPIFFS::closedir ( File file)
overrideprivatevirtual

Close a directory.

Parameters
filefile reference for this device
Returns
0 upon success, -1 upon failure with errno containing the cause

Implements FileSystem.

Definition at line 455 of file SPIFFS.cxx.

◆ do_mount()

int SPIFFS::do_mount ( )
private

Helper to mount the file system.

Returns
0 if successful, else some error code

Definition at line 189 of file SPIFFS.cxx.

◆ errno_translate()

int SPIFFS::errno_translate ( int  spiffs_error)
private

Translate a SPIFFS specific error number to a standard POSIX errno.

Parameters
spiffs_errorSPIFFS specific error number
Returns
standard POSX errno

Definition at line 526 of file SPIFFS.cxx.

◆ extern_lock()

void SPIFFS::extern_lock ( struct spiffs_t *  fs)
inlinestatic

Provide mutex lock.

Parameters
fsreference to the file system instance

Definition at line 49 of file SPIFFS.cxx.

◆ extern_unlock()

void SPIFFS::extern_unlock ( struct spiffs_t *  fs)
inlinestatic

Provide mutex unlock.

Parameters
fsreference to the file system instance

Definition at line 54 of file SPIFFS.cxx.

◆ flash_erase() [1/2]

int SPIFFS::flash_erase ( struct spiffs_t *  fs,
unsigned  addr,
unsigned  size 
)
staticprotected

SPIFFS callback to erase flash.

Parameters
fsreference to SPIFFS instance
addradddress location to erase
sizesize of erase region in bytes

Definition at line 94 of file SPIFFS.cxx.

◆ flash_erase() [2/2]

virtual int32_t SPIFFS::flash_erase ( uint32_t  addr,
uint32_t  size 
)
protectedpure virtual

SPIFFS callback to erase flash, in context.

Parameters
addradddress location to erase
sizesize of erase region in bytes

Implemented in TiSPIFFS< ERASE_PAGE_SIZE >, SpiSPIFFS, and Stm32SPIFFS.

◆ flash_read() [1/2]

int SPIFFS::flash_read ( struct spiffs_t *  fs,
unsigned  addr,
unsigned  size,
uint8_t *  dst 
)
staticprotected

SPIFFS callback to read flash.

Parameters
fsreference to SPIFFS instance
addradddress location to read
sizesize of read in bytes
dstdestination buffer for read

Definition at line 80 of file SPIFFS.cxx.

◆ flash_read() [2/2]

virtual int32_t SPIFFS::flash_read ( uint32_t  addr,
uint32_t  size,
uint8_t *  dst 
)
protectedpure virtual

SPIFFS callback to read flash, in context.

Parameters
addradddress location to read
sizesize of read in bytes
dstdestination buffer for read

Implemented in TiSPIFFS< ERASE_PAGE_SIZE >, SpiSPIFFS, and Stm32SPIFFS.

◆ flash_write() [1/2]

int SPIFFS::flash_write ( struct spiffs_t *  fs,
unsigned  addr,
unsigned  size,
uint8_t *  src 
)
staticprotected

SPIFFS callback to write flash.

Parameters
fsreference to SPIFFS instance
addradddress location to write
sizesize of write in bytes
srcsource buffer for write

Definition at line 87 of file SPIFFS.cxx.

◆ flash_write() [2/2]

virtual int32_t SPIFFS::flash_write ( uint32_t  addr,
uint32_t  size,
uint8_t *  src 
)
protectedpure virtual

SPIFFS callback to write flash, in context.

Parameters
addradddress location to write
sizesize of write in bytes
srcsource buffer for write

Implemented in TiSPIFFS< ERASE_PAGE_SIZE >, SpiSPIFFS, and Stm32SPIFFS.

◆ flush_cache()

void SPIFFS::flush_cache ( )
inline

Performs a sync on all files that have had a write but no fsync call since then.

This can be used by the caller to implement a background flush thread.

Definition at line 101 of file SPIFFS.hxx.

◆ format()

void SPIFFS::format ( )
overridevirtual

Format the file system, all data will be lost.

The file system must not be mounted at the time of calling this.

Implements FileSystem.

Definition at line 172 of file SPIFFS.cxx.

◆ fstat()

int SPIFFS::fstat ( File file,
struct stat stat 
)
overrideprivatevirtual

Get the status information of a file or device.

Parameters
filefile reference for this device
statstructure to fill status info into
Returns
0 upon successor or negative error number upon error.

Reimplemented from FileSystem.

Definition at line 390 of file SPIFFS.cxx.

◆ fsync()

int SPIFFS::fsync ( File file)
overrideprivatevirtual

Synchronize (flush) a file to disk.

Parameters
filefile reference for this device
Returns
0 upon success, -1 upon failure with errno containing the cause

Implements FileSystem.

Definition at line 439 of file SPIFFS.cxx.

◆ is_any_dirty()

bool SPIFFS::is_any_dirty ( )
inline
Returns
true if there was any file written on this filesystem since the last call to is_any_dirty. (Transactionality guaranteed.) The caller can create a background flush thread using this information.

Definition at line 87 of file SPIFFS.hxx.

◆ lseek()

off_t SPIFFS::lseek ( File f,
off_t  offset,
int  whence 
)
overrideprivatevirtual

Seek method.

Parameters
ffile reference for this device
offsetoffset in bytes from whence directive
whenceSEEK_SET if to set the file offset to an abosolute position, SEEK_CUR if to set the file offset from current position SEEK_END if to set the file offset to the end of the file
Returns
current offset or negative error number upon error.

Reimplemented from FileIO.

Definition at line 326 of file SPIFFS.cxx.

◆ mount()

void SPIFFS::mount ( const char *  mount_point)
inlineoverridevirtual

Mount the file system.

Parameters
mount_pointpath in the root file system for the mount point. Must not contain any trailing '/' characters, e.g. "/usr", not "/usr/". Unlike in linux, each mount must be entirely unique point in the system. For example, mounting of both "/usr" and "/usr/bin" is is not supported. However, "/usr" and "/usr1" is supported. This saves on file open logic.

Implements FileSystem.

Definition at line 59 of file SPIFFS.hxx.

◆ open()

int SPIFFS::open ( File file,
const char *  path,
int  flags,
int  mode 
)
overrideprivatevirtual

Open a file or device.

Parameters
filefile reference for this device
pathfile or device name
flagsopen flags
modeopen mode, ignored in this implementation
Returns
0 upon success, -1 upon failure with errno containing the cause

Implements FileIO.

Definition at line 200 of file SPIFFS.cxx.

◆ opendir()

File * SPIFFS::opendir ( File file,
const char *  name 
)
overrideprivatevirtual

Open a directory.

Parameters
filefile reference for this device
namedirectory path
Returns
pointer to the open directory on success, NULL on error

Implements FileSystem.

Definition at line 474 of file SPIFFS.cxx.

◆ read()

ssize_t SPIFFS::read ( File file,
void *  buf,
size_t  count 
)
overrideprivatevirtual

Read from a file or device.

Parameters
filefile reference for this device
buflocation to place read data
countnumber of bytes to read
Returns
number of bytes read upon success, -1 upon failure with errno containing the cause

Implements FileIO.

Definition at line 286 of file SPIFFS.cxx.

◆ readdir()

struct dirent * SPIFFS::readdir ( File file)
overrideprivatevirtual

Read the next entry in a directory.

Parameters
filefile reference for this device
Returns
pointer to a struct dirent representing the next directectory entry

Implements FileSystem.

Definition at line 502 of file SPIFFS.cxx.

◆ stat()

int SPIFFS::stat ( const char *  path,
struct stat *  stat 
)
overrideprivatevirtual

Get the status information of a file or device.

Parameters
pathfile or device name
statstructure to fill status info into
Returns
0 upon success, -1 upon failure with errno containing the cause

Implements FileSystem.

Definition at line 410 of file SPIFFS.cxx.

◆ unlink()

int SPIFFS::unlink ( const char *  path)
overrideprivatevirtual

Remove a file.

Parameters
pathfile name
Returns
0 upon successor or negative error number upon error.

Implements FileSystem.

Definition at line 271 of file SPIFFS.cxx.

◆ unmount()

void SPIFFS::unmount ( )
protected

Flushes caches and unmounts the filesystem.

The destructor of the derived class MUST call this function.

Definition at line 151 of file SPIFFS.cxx.

◆ write()

ssize_t SPIFFS::write ( File file,
const void *  buf,
size_t  count 
)
overrideprivatevirtual

Write to a file or device.

Parameters
filefile reference for this device
buflocation to find write data
countnumber of bytes to write
Returns
number of bytes written upon success, -1 upon failure with errno containing the cause

Implements FileIO.

Definition at line 303 of file SPIFFS.cxx.

Member Data Documentation

◆ anyDirty_

bool SPIFFS::anyDirty_
private

Bit that is set to 1 when any write operation happens to this FS.

Definition at line 302 of file SPIFFS.hxx.

◆ cache_

void* SPIFFS::cache_
private

memory for cache

Definition at line 296 of file SPIFFS.hxx.

◆ cacheSize_

uint32_t SPIFFS::cacheSize_
private

size in bytes of cache_

Definition at line 293 of file SPIFFS.hxx.

◆ fdSpace_

uint8_t* SPIFFS::fdSpace_
private

file descriptor metadata

Definition at line 290 of file SPIFFS.hxx.

◆ fdSpaceSize_

uint32_t SPIFFS::fdSpaceSize_
private

size in bytes of the fdSpace_

Definition at line 287 of file SPIFFS.hxx.

◆ formatted_

bool SPIFFS::formatted_
private

has the file system been formatted since last reboot?

Definition at line 299 of file SPIFFS.hxx.

◆ fs_

spiffs* SPIFFS::fs_
protected

file system instance metadata

Definition at line 182 of file SPIFFS.hxx.

◆ lock_

OSMutex SPIFFS::lock_
private

whole file system lock

Definition at line 281 of file SPIFFS.hxx.

◆ postFormatHook_

std::function<void()> SPIFFS::postFormatHook_
private

callback to be called post a formating operation

Definition at line 278 of file SPIFFS.hxx.

◆ workBuffer_

uint8_t* SPIFFS::workBuffer_
private

work buffer for the file system

Definition at line 284 of file SPIFFS.hxx.


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