git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15148 27425a3e-05d8-49a3-a47f-9c15f0e5edd8

This commit is contained in:
Giovanni Di Sirio 2021-11-29 11:56:34 +00:00
parent 00abdb85fe
commit 5e3c8f9358
7 changed files with 236 additions and 443 deletions

View File

@ -20,6 +20,7 @@
#include "ch.h"
#include "hal.h"
#include "vfs.h"
#include "drvoverlay.h"
#include "drvstreams.h"
#include "rt_test_root.h"
@ -41,12 +42,18 @@ static event_source_t inserted_event, removed_event;
/* VFS related. */
/*===========================================================================*/
static NullStream null;
/* VFS overlay driver object representing the root directory.*/
static vfs_overlay_driver_t vfs_root;
/* VFS streams driver object representing the /dev directory.*/
static vfs_streams_driver_t vfs_dev;
static NullStream nullstream;
/* Stream to be exposed under /dev as files.*/
static const drv_stream_element_t streams[] = {
{"VSD1", (BaseSequentialStream *)&PORTAB_SD1},
{"null", (BaseSequentialStream *)&null},
{"null", (BaseSequentialStream *)&nullstream},
{NULL, NULL}
};
@ -59,7 +66,7 @@ static msg_t scan_nodes(BaseSequentialStream *chp, char *path) {
static vfs_node_info_t ni;
chprintf(chp, "%s\r\n", path);
res = vfsOpenDirectory(path, &dirp);
res = vfsOpenDirectory((vfs_driver_t *)&vfs_root, path, &dirp);
if (res == VFS_RET_SUCCESS) {
size_t i = strlen(path);
@ -209,29 +216,34 @@ int main(void) {
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
* - VFS initialization.
* - Shell manager initialization.
*/
halInit();
chSysInit();
vfsInit();
shellInit();
/* Board-dependent setup code.*/
portab_setup();
/* Starting a serial port for the shell.*/
/* Starting a serial port for the shell, initializing other streams too.*/
sdStart(&PORTAB_SD1, NULL);
nullObjectInit(&nullstream);
/* Registering various streams on VFS.*/
nullObjectInit(&null);
msg = vfsRegisterDriver(drvStreamsInit("dev", &streams[0]));
/* Initializing an overlay VFS object as a root, no need for a name.*/
drvOverlayObjectInit(&vfs_root, "");
/* Registering a streams VFS driver on the VFS overlay root as "/dev".*/
msg = drvOverlayRegisterDriver(&vfs_root,
drvStreamsObjectInit(&vfs_dev,
"dev",
&streams[0]));
if (msg != VFS_RET_SUCCESS) {
chSysHalt("VFS");
}
/* Opening a file for shell I/O.*/
msg = vfsOpenFile("/dev/VSD1",
msg = vfsOpenFile((vfs_driver_t *)&vfs_root,
"/dev/VSD1",
MODE_OPEN | MODE_RDWR,
&file);
if (msg != VFS_RET_SUCCESS) {

View File

@ -73,7 +73,9 @@ static const struct vfs_overlay_dir_node_vmt dir_node_vmt = {
/* Module local functions. */
/*===========================================================================*/
static msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
static msg_t match_driver(vfs_overlay_driver_t *odp,
const char **pathp,
vfs_driver_t **vdpp) {
char fname[VFS_CFG_MAX_NAMELEN + 1];
msg_t err;
vfs_driver_t **pp;
@ -83,8 +85,8 @@ static msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
VFS_BREAK_ON_ERROR(err);
/* Searching among registered drivers.*/
pp = &vfs.drivers[0];
while (pp < vfs.next_driver) {
pp = &odp->drivers[0];
while (pp < odp->next_driver) {
if (strncmp(fname, (*pp)->rootname, VFS_CFG_MAX_NAMELEN) == 0) {
*vdpp = *pp;
return VFS_RET_SUCCESS;
@ -101,6 +103,7 @@ static msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
static msg_t drv_open_dir(void *instance,
const char *path,
vfs_directory_node_t **vdnpp) {
vfs_overlay_driver_t *odp = (vfs_overlay_driver_t *)instance;
msg_t err;
do {
@ -108,10 +111,9 @@ static msg_t drv_open_dir(void *instance,
VFS_BREAK_ON_ERROR(err);
if (*path == '\0') {
vfs_overlay_driver_t *dop = (vfs_overlay_driver_t *)instance;
/* Creating a root directory node.*/
vfs_overlay_dir_node_t *onp = chPoolAlloc(&dop->dir_nodes_pool);
vfs_overlay_dir_node_t *onp = chPoolAlloc(&odp->dir_nodes_pool);
if (onp != NULL) {
/* Node object initialization.*/
@ -128,7 +130,7 @@ static msg_t drv_open_dir(void *instance,
vfs_driver_t *dp;
/* Delegating node creation to a registered driver.*/
err = match_driver(&path, &dp);
err = match_driver(odp, &path, &dp);
VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_dir((void *)dp, path, vdnpp);
@ -143,6 +145,7 @@ static msg_t drv_open_file(void *instance,
const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) {
vfs_overlay_driver_t *odp = (vfs_overlay_driver_t *)instance;
msg_t err;
do {
@ -159,7 +162,7 @@ static msg_t drv_open_file(void *instance,
vfs_driver_t *dp;
/* Delegating node creation to a registered driver.*/
err = match_driver(&path, &dp);
err = match_driver(odp, &path, &dp);
VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_file((void *)dp, path, mode, vfnpp);
@ -209,6 +212,15 @@ static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
/* Module exported functions. */
/*===========================================================================*/
/**
* @brief VFS overlay object initialization.
*
* @param[out] vodp pointer to a @p vfs_overlay_driver_t structure
* @param[in] rootname name to be attributed to this object
* @return A pointer to this initialized object.
*
* @api
*/
vfs_driver_t *drvOverlayObjectInit(vfs_overlay_driver_t *vodp,
const char *rootname) {
@ -228,4 +240,27 @@ vfs_driver_t *drvOverlayObjectInit(vfs_overlay_driver_t *vodp,
return (vfs_driver_t *)vodp;
}
/**
* @brief Registers a VFS driver as an overlay.
*
* @param[in] vodp pointer to a @p vfs_overlay_driver_t structure
* @return The operation result.
*
* @api
*/
msg_t drvOverlayRegisterDriver(vfs_overlay_driver_t *vodp,
vfs_driver_t *vdp) {
msg_t err;
if (vodp->next_driver >= &vodp->drivers[VFS_CFG_MAX_DRIVERS]) {
err = VFS_RET_NO_RESOURCE;
}
else {
*vodp->next_driver++ = vdp;
err = VFS_RET_SUCCESS;
}
return err;
}
/** @} */

View File

@ -121,7 +121,7 @@ struct vfs_overlay_driver_vmt {
/**
* @brief Type of a structure representing a VFS Overlay driver.
*/
typedef struct vfs_drv_streams {
typedef struct vfs_drv_overlay {
/**
* @brief Virtual Methods Table.
*/
@ -142,6 +142,8 @@ extern "C" {
#endif
vfs_driver_t *drvOverlayObjectInit(vfs_overlay_driver_t *vodp,
const char *rootname);
msg_t drvOverlayRegisterDriver(vfs_overlay_driver_t *vodp,
vfs_driver_t *vdp);
#ifdef __cplusplus
}
#endif

View File

@ -34,57 +34,6 @@
/* Module local definitions. */
/*===========================================================================*/
/**
* @brief Number of directory nodes pre-allocated in the pool.
*/
#define DRV_DIR_NODES_NUM 1
/**
* @brief Number of file nodes pre-allocated in the pool.
*/
#define DRV_FILE_NODES_NUM 2
/**
* @brief @p vfs_streams_driver_t specific methods.
*/
#define __vfs_streams_driver_methods \
__vfs_driver_methods
/**
* @brief @p vfs_streams_driver_t specific data.
*/
#define __vfs_streams_driver_data \
__vfs_driver_data \
const drv_stream_element_t *streams; \
memory_pool_t file_nodes_pool; \
memory_pool_t dir_nodes_pool;
/**
* @brief @p vfs_stream_dir_node_t specific methods.
*/
#define __vfs_stream_dir_node_methods \
__vfs_directory_node_methods
/**
* @brief @p vfs_stream_dir_node_t specific data.
*/
#define __vfs_stream_dir_node_data \
__vfs_directory_node_data \
unsigned index;
/**
* @brief @p vfs_stream_file_node_t specific methods.
*/
#define __vfs_stream_file_node_methods \
__vfs_file_node_methods
/**
* @brief @p vfs_stream_file_node_t specific data.
*/
#define __vfs_stream_file_node_data \
__vfs_file_node_data \
BaseSequentialStream *stream;
/*===========================================================================*/
/* Module exported variables. */
/*===========================================================================*/
@ -93,60 +42,6 @@
/* Module local types. */
/*===========================================================================*/
/**
* @brief @p vfs_streams_driver_t virtual methods table.
*/
struct vfs_streams_driver_vmt {
__vfs_streams_driver_methods
};
/**
* @brief Type of a structure representing a VFS streams driver.
*/
typedef struct vfs_drv_streams {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_streams_driver_vmt *vmt;
__vfs_streams_driver_data
} vfs_streams_driver_t;
/**
* @brief @p vfs_stream_dir_node_t virtual methods table.
*/
struct vfs_stream_dir_node_vmt {
__vfs_stream_dir_node_methods
};
/**
* @brief Type of a structure representing a stream dir VFS node.
*/
typedef struct vfs_stream_dir_node {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_stream_dir_node_vmt *vmt;
__vfs_stream_dir_node_data
} vfs_stream_dir_node_t;
/**
* @brief @p vfs_stream_file_node_t virtual methods table.
*/
struct vfs_stream_file_node_vmt {
__vfs_stream_file_node_methods
};
/**
* @brief Type of a structure representing a stream file VFS node.
*/
typedef struct vfs_stream_file_node {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_stream_file_node_vmt *vmt;
__vfs_stream_file_node_data
} vfs_stream_file_node_t;
/*===========================================================================*/
/* Module local variables. */
/*===========================================================================*/
@ -192,11 +87,6 @@ static const struct vfs_stream_file_node_vmt file_node_vmt = {
.file_getsize = node_file_getsize
};
static vfs_stream_dir_node_t drv_dir_nodes[DRV_DIR_NODES_NUM];
static vfs_stream_file_node_t drv_file_nodes[DRV_FILE_NODES_NUM];
static vfs_streams_driver_t drv_streams;
/*===========================================================================*/
/* Module local functions. */
/*===========================================================================*/
@ -216,7 +106,7 @@ static msg_t drv_open_dir(void *instance,
err = vfs_parse_match_end(&path);
VFS_BREAK_ON_ERROR(err);
sdnp = chPoolAlloc(&drv_streams.dir_nodes_pool);
sdnp = chPoolAlloc(&drvp->dir_nodes_pool);
if (sdnp != NULL) {
/* Node object initialization.*/
@ -263,7 +153,7 @@ static msg_t drv_open_file(void *instance,
if (strncmp(fname, dsep->name, VFS_CFG_MAX_NAMELEN) == 0) {
vfs_stream_file_node_t *sfnp;
sfnp = chPoolAlloc(&drv_streams.file_nodes_pool);
sfnp = chPoolAlloc(&drvp->file_nodes_pool);
if (sfnp != NULL) {
/* Node object initialization.*/
@ -309,12 +199,13 @@ static msg_t node_dir_first(void *instance, vfs_node_info_t *nip) {
static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
vfs_stream_dir_node_t *sdnp = (vfs_stream_dir_node_t *)instance;
vfs_streams_driver_t *vsdp = (vfs_streams_driver_t *)sdnp->driver;
if (drv_streams.streams[sdnp->index].name != NULL) {
if (vsdp->streams[sdnp->index].name != NULL) {
nip->attr = VFS_NODE_ATTR_ISSTREAM;
nip->size = (vfs_offset_t)0;
strcpy(nip->name, drv_streams.streams[sdnp->index].name);
strcpy(nip->name, vsdp->streams[sdnp->index].name);
sdnp->index++;
@ -378,26 +269,41 @@ static vfs_offset_t node_file_getsize(void *instance) {
/* Module exported functions. */
/*===========================================================================*/
vfs_driver_t *drvStreamsInit(const char *rootname,
/**
* @brief VFS streams object initialization.
*
* @param[out] vsdp pointer to a @p vfs_streams_driver_t structure
* @param[in] rootname name to be attributed to this object
* @param[in] streams pointer to an array of @p drv_stream_element_t objects
* @return A pointer to this initialized object.
*
* @api
*/
vfs_driver_t *drvStreamsObjectInit(vfs_streams_driver_t *vsdp,
const char *rootname,
const drv_stream_element_t *streams) {
drv_streams.vmt = &driver_vmt;
drv_streams.rootname = rootname;
drv_streams.streams = streams;
chPoolObjectInit(&drv_streams.dir_nodes_pool,
vsdp->vmt = &driver_vmt;
vsdp->rootname = rootname;
vsdp->streams = streams;
/* Initializing pools.*/
chPoolObjectInit(&vsdp->dir_nodes_pool,
sizeof (vfs_stream_dir_node_t),
chCoreAllocAligned);
chPoolLoadArray(&drv_streams.dir_nodes_pool,
drv_dir_nodes,
DRV_DIR_NODES_NUM);
chPoolObjectInit(&drv_streams.file_nodes_pool,
chPoolObjectInit(&vsdp->file_nodes_pool,
sizeof (vfs_stream_file_node_t),
chCoreAllocAligned);
chPoolLoadArray(&drv_streams.file_nodes_pool,
drv_file_nodes,
DRV_FILE_NODES_NUM);
return (vfs_driver_t *)&drv_streams;
/* Preloading pools.*/
chPoolLoadArray(&vsdp->dir_nodes_pool,
&vsdp->dir_nodes[0],
DRV_CFG_STREAMS_DIR_NODES_NUM);
chPoolLoadArray(&vsdp->file_nodes_pool,
&vsdp->file_nodes[0],
DRV_CFG_STREAMS_FILE_NODES_NUM);
return (vfs_driver_t *)vsdp;
}
/** @} */

View File

@ -38,6 +38,20 @@
/* Module pre-compile time settings. */
/*===========================================================================*/
/**
* @brief Number of directory nodes pre-allocated in the pool.
*/
#if !defined(DRV_CFG_STREAMS_DIR_NODES_NUM) || defined(__DOXYGEN__)
#define DRV_CFG_STREAMS_DIR_NODES_NUM 1
#endif
/**
* @brief Number of file nodes pre-allocated in the pool.
*/
#if !defined(DRV_CFG_STREAMS_FILE_NODES_NUM) || defined(__DOXYGEN__)
#define DRV_CFG_STREAMS_FILE_NODES_NUM 2
#endif
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
@ -60,6 +74,103 @@ typedef struct drv_stream_element {
BaseSequentialStream *stream;
} drv_stream_element_t;
/**
* @brief @p vfs_stream_dir_node_t specific methods.
*/
#define __vfs_stream_dir_node_methods \
__vfs_directory_node_methods
/**
* @brief @p vfs_stream_dir_node_t specific data.
*/
#define __vfs_stream_dir_node_data \
__vfs_directory_node_data \
unsigned index;
/**
* @brief @p vfs_stream_dir_node_t virtual methods table.
*/
struct vfs_stream_dir_node_vmt {
__vfs_stream_dir_node_methods
};
/**
* @brief Type of a structure representing a stream dir VFS node.
*/
typedef struct vfs_stream_dir_node {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_stream_dir_node_vmt *vmt;
__vfs_stream_dir_node_data
} vfs_stream_dir_node_t;
/**
* @brief @p vfs_stream_file_node_t specific methods.
*/
#define __vfs_stream_file_node_methods \
__vfs_file_node_methods
/**
* @brief @p vfs_stream_file_node_t specific data.
*/
#define __vfs_stream_file_node_data \
__vfs_file_node_data \
BaseSequentialStream *stream;
/**
* @brief @p vfs_stream_file_node_t virtual methods table.
*/
struct vfs_stream_file_node_vmt {
__vfs_stream_file_node_methods
};
/**
* @brief Type of a structure representing a stream file VFS node.
*/
typedef struct vfs_stream_file_node {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_stream_file_node_vmt *vmt;
__vfs_stream_file_node_data
} vfs_stream_file_node_t;
/**
* @brief @p vfs_streams_driver_t specific methods.
*/
#define __vfs_streams_driver_methods \
__vfs_driver_methods
/**
* @brief @p vfs_streams_driver_t specific data.
*/
#define __vfs_streams_driver_data \
__vfs_driver_data \
const drv_stream_element_t *streams; \
memory_pool_t file_nodes_pool; \
memory_pool_t dir_nodes_pool; \
vfs_stream_dir_node_t dir_nodes[DRV_CFG_STREAMS_DIR_NODES_NUM]; \
vfs_stream_file_node_t file_nodes[DRV_CFG_STREAMS_FILE_NODES_NUM];
/**
* @brief @p vfs_streams_driver_t virtual methods table.
*/
struct vfs_streams_driver_vmt {
__vfs_streams_driver_methods
};
/**
* @brief Type of a structure representing a VFS streams driver.
*/
typedef struct vfs_drv_streams {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_streams_driver_vmt *vmt;
__vfs_streams_driver_data
} vfs_streams_driver_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
@ -71,7 +182,8 @@ typedef struct drv_stream_element {
#ifdef __cplusplus
extern "C" {
#endif
vfs_driver_t *drvStreamsInit(const char *rootname,
vfs_driver_t *drvStreamsObjectInit(vfs_streams_driver_t *vsdp,
const char *rootname,
const drv_stream_element_t *streams);
#ifdef __cplusplus
}

View File

@ -32,26 +32,6 @@
/* Module constants. */
/*===========================================================================*/
/**
* @brief @p vfs_root_driver_t specific methods.
*/
#define __vfs_root_driver_methods \
__vfs_driver_methods
/**
* @brief @p vfs_root_driver_t specific data.
*/
#define __vfs_root_driver_data \
__vfs_driver_data \
/* VFS access mutex.*/ \
mutex_t mtx; \
/* Pool of the root directory nodes.*/ \
memory_pool_t dir_nodes_pool; \
/* Next registration slot.*/ \
vfs_driver_t **next_driver; \
/* Registration slots.*/ \
vfs_driver_t *drivers[VFS_CFG_MAX_DRIVERS];
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
@ -64,24 +44,6 @@
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief @p vfs_root_driver_t virtual methods table.
*/
struct vfs_root_driver_vmt {
__vfs_root_driver_methods
};
/**
* @brief Type of a structure representing the VFS system.
*/
typedef struct vfs_root_driver {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_root_driver_vmt *vmt;
__vfs_root_driver_data
} vfs_root_driver_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
@ -90,20 +52,21 @@ typedef struct vfs_root_driver {
/* External declarations. */
/*===========================================================================*/
extern vfs_root_driver_t vfs;
#ifdef __cplusplus
extern "C" {
#endif
void vfsInit(void);
msg_t vfsRegisterDriver(vfs_driver_t *vdp);
msg_t vfsOpenDirectory(const char *name, vfs_directory_node_t **vdnpp);
msg_t vfsOpenDirectory(vfs_driver_t *vdp,
const char *name,
vfs_directory_node_t **vdnpp);
void vfsCloseDirectory(vfs_directory_node_t *vdnp);
msg_t vfsReadDirectoryFirst(vfs_directory_node_t *vdnp,
vfs_node_info_t *nip);
msg_t vfsReadDirectoryNext(vfs_directory_node_t *vdnp,
vfs_node_info_t *nip);
msg_t vfsOpenFile(const char *name, unsigned mode, vfs_file_node_t **vfnpp);
msg_t vfsOpenFile(vfs_driver_t *vdp,
const char *name,
unsigned mode,
vfs_file_node_t **vfnpp);
void vfsCloseFile(vfs_file_node_t *vfnp);
ssize_t vfsReadFile(vfs_file_node_t *vfnp, uint8_t *buf, size_t n);
ssize_t vfsWriteFile(vfs_file_node_t *vfnp, const uint8_t *buf, size_t n);

View File

@ -33,274 +33,30 @@
/* Module local definitions. */
/*===========================================================================*/
/**
* @brief Number of directory nodes pre-allocated in the pool.
*/
#define ROOT_DIR_NODES_NUM 1
/**
* @brief @p vfs_root_dir_node_t specific methods.
*/
#define __vfs_root_dir_node_methods \
__vfs_directory_node_methods
/**
* @brief @p vfs_root_dir_node_t specific data.
*/
#define __vfs_root_dir_node_data \
__vfs_directory_node_data \
unsigned index;
/*===========================================================================*/
/* Module exported variables. */
/*===========================================================================*/
/**
* @brief Root VFS driver.
*/
vfs_root_driver_t vfs;
/*===========================================================================*/
/* Module local types. */
/*===========================================================================*/
/**
* @brief @p vfs_root_dir_node_t virtual methods table.
*/
struct vfs_root_dir_node_vmt {
__vfs_root_dir_node_methods
};
/**
* @brief Type of a structure representing a root directory VFS node.
*/
typedef struct vfs_root_dir_node {
/**
* @brief Virtual Methods Table.
*/
const struct vfs_root_dir_node_vmt *vmt;
__vfs_root_dir_node_data
} vfs_root_dir_node_t;
/*===========================================================================*/
/* Module local variables. */
/*===========================================================================*/
static vfs_root_dir_node_t root_dir_nodes[ROOT_DIR_NODES_NUM];
static msg_t root_open_dir(void *instance,
const char *path,
vfs_directory_node_t **vdnpp);
static msg_t root_open_file(void *instance,
const char *path,
unsigned mode,
vfs_file_node_t **vfnpp);
static const struct vfs_root_driver_vmt root_driver_vmt = {
.open_dir = root_open_dir,
.open_file = root_open_file
};
static void node_dir_release(void *instance);
static msg_t node_dir_first(void *instance, vfs_node_info_t *nip);
static msg_t node_dir_next(void *instance, vfs_node_info_t *nip);
static const struct vfs_root_dir_node_vmt root_dir_node_vmt = {
.release = node_dir_release,
.dir_first = node_dir_first,
.dir_next = node_dir_next
};
/*===========================================================================*/
/* Module local functions. */
/*===========================================================================*/
msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
char fname[VFS_CFG_MAX_NAMELEN + 1];
msg_t err;
vfs_driver_t **pp;
do {
err = vfs_parse_filename(pathp, fname);
VFS_BREAK_ON_ERROR(err);
/* Searching among registered drivers.*/
pp = &vfs.drivers[0];
while (pp < vfs.next_driver) {
if (strncmp(fname, (*pp)->rootname, VFS_CFG_MAX_NAMELEN) == 0) {
*vdpp = *pp;
return VFS_RET_SUCCESS;
}
}
err = VFS_RET_NO_DRIVER;
}
while (false);
return err;
}
static msg_t root_open_dir(void *instance,
const char *path,
vfs_directory_node_t **vdnpp) {
msg_t err;
do {
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
if (*path == '\0') {
vfs_root_driver_t *rootp = (vfs_root_driver_t *)instance;
/* Creating a root directory node.*/
vfs_root_dir_node_t *rdnp = chPoolAlloc(&rootp->dir_nodes_pool);
if (rdnp != NULL) {
/* Node object initialization.*/
rdnp->vmt = &root_dir_node_vmt;
rdnp->refs = 1U;
rdnp->driver = (vfs_driver_t *)&vfs;
rdnp->index = 0U;
*vdnpp = (vfs_directory_node_t *)rdnp;
return VFS_RET_SUCCESS;
}
}
else {
vfs_driver_t *dp;
/* Delegating node creation to a registered driver.*/
err = match_driver(&path, &dp);
VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_dir((void *)dp, path, vdnpp);
}
}
while (false);
return err;
}
static msg_t root_open_file(void *instance,
const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) {
msg_t err;
do {
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
if (*path == '\0') {
(void)instance;
/* Always not found, there are no files in the root.*/
err = VFS_RET_NOT_FOUND;
}
else {
vfs_driver_t *dp;
/* Delegating node creation to a registered driver.*/
err = match_driver(&path, &dp);
VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_file((void *)dp, path, mode, vfnpp);
}
}
while (false);
return err;
}
static void node_dir_release(void *instance) {
vfs_root_dir_node_t *rdnp = (vfs_root_dir_node_t *)instance;
if (--rdnp->refs == 0U) {
chPoolFree(&((vfs_root_driver_t *)rdnp->driver)->dir_nodes_pool,
(void *)rdnp);
}
}
static msg_t node_dir_first(void *instance, vfs_node_info_t *nip) {
vfs_root_dir_node_t *rdnp = (vfs_root_dir_node_t *)instance;
rdnp->index = 0U;
return node_dir_next(instance, nip);
}
static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
vfs_root_dir_node_t *rdnp = (vfs_root_dir_node_t *)instance;
if (rdnp->index < VFS_CFG_MAX_DRIVERS) {
nip->attr = VFS_NODE_ATTR_ISDIR | VFS_NODE_ATTR_READONLY;
nip->size = (vfs_offset_t)0;
strcpy(nip->name,
((vfs_root_driver_t *)rdnp->driver)->drivers[rdnp->index]->rootname);
rdnp->index++;
return VFS_RET_SUCCESS;
}
return VFS_RET_EOF;
}
/*===========================================================================*/
/* Module exported functions. */
/*===========================================================================*/
/**
* @brief VFS initialization.
*
* @special
*/
void vfsInit(void) {
vfs.vmt = &root_driver_vmt;
vfs.rootname = "";
vfs.next_driver = &vfs.drivers[0];
chPoolObjectInit(&vfs.dir_nodes_pool,
sizeof (vfs_root_dir_node_t),
chCoreAllocAligned);
chPoolLoadArray(&vfs.dir_nodes_pool,
root_dir_nodes,
ROOT_DIR_NODES_NUM);
#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
chMtxObjectInit(&vfs.mtx);
#else
chSemObjectInit(&vfs.sem, (cnt_t)1);
#endif
}
/**
* @brief Registers a file system driver on VFS.
*
* @param[in] vdp pointer to a @p vfs_driver_t structure
* @return The operation result.
*
* @api
*/
msg_t vfsRegisterDriver(vfs_driver_t *vdp) {
msg_t err;
if (vfs.next_driver >= &vfs.drivers[VFS_CFG_MAX_DRIVERS]) {
err = VFS_RET_NO_RESOURCE;
}
else {
*vfs.next_driver++ = vdp;
err = VFS_RET_SUCCESS;
}
return err;
}
/**
* @brief Opens a VFS directory.
*
* @param[in] vdp VFS instance where to open the directory
* @param[in] path absolute path of the directory to be opened
* @param[out] vdnpp pointer to the pointer to the instantiated
* @p vfs_directory_node_t object
@ -308,9 +64,11 @@ msg_t vfsRegisterDriver(vfs_driver_t *vdp) {
*
* @api
*/
msg_t vfsOpenDirectory(const char *path, vfs_directory_node_t **vdnpp) {
msg_t vfsOpenDirectory(vfs_driver_t *vdp,
const char *path,
vfs_directory_node_t **vdnpp) {
return vfs.vmt->open_dir((vfs_driver_t *)&vfs, path, vdnpp);
return vdp->vmt->open_dir(vdp, path, vdnpp);
}
/**
@ -365,16 +123,21 @@ msg_t vfsReadDirectoryNext(vfs_directory_node_t *vdnp,
/**
* @brief Opens a VFS file.
*
* @param[in] vdp VFS instance where to open the file
* @param[in] path absolute path of the file to be opened
* @param[in] mode file open mode
* @param[out] vdnpp pointer to the pointer to the instantiated
* @p vfs_file_node_t object
* @return The operation result.
*
* @api
*/
msg_t vfsOpenFile(const char *path, unsigned mode, vfs_file_node_t **vfnpp) {
msg_t vfsOpenFile(vfs_driver_t *vdp,
const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) {
return vfs.vmt->open_file((vfs_driver_t *)&vfs, path, mode, vfnpp);
return vdp->vmt->open_file(vdp, path, mode, vfnpp);
}
/**