Some rework, not yet complete.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15137 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-11-26 13:58:45 +00:00
parent 821b75bb53
commit e1289f82da
3 changed files with 140 additions and 40 deletions

View File

@ -163,17 +163,17 @@ static const struct vfs_streams_driver_vmt driver_vmt = {
.open_file = drv_open_file
};
static void node_release(void *instance);
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_stream_dir_node_vmt dir_node_vmt = {
.release = node_release,
.release = node_dir_release,
.dir_first = node_dir_first,
.dir_next = node_dir_next
};
static void node_file_release(void *instance);
static BaseSequentialStream *node_file_get_stream(void *instance);
static ssize_t node_file_read(void *instance, uint8_t *buf, size_t n);
static ssize_t node_file_write(void *instance, const uint8_t *buf, size_t n);
@ -182,7 +182,7 @@ static vfs_offset_t node_file_getpos(void *instance);
static vfs_offset_t node_file_getsize(void *instance);
static const struct vfs_stream_file_node_vmt file_node_vmt = {
.release = node_release,
.release = node_file_release,
.get_stream = node_file_get_stream,
.file_read = node_file_read,
.file_write = node_file_write,
@ -191,7 +191,7 @@ static const struct vfs_stream_file_node_vmt file_node_vmt = {
.file_getsize = node_file_getsize
};
static vfs_stream_file_node_t drv_dir_nodes[DRV_DIR_NODES_NUM];
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;
@ -285,13 +285,13 @@ static msg_t drv_open_file(void *instance,
return err;
}
static void node_release(void *instance) {
vfs_stream_file_node_t *sfnp = (vfs_stream_file_node_t *)instance;
static void node_dir_release(void *instance) {
vfs_stream_dir_node_t *sdnp = (vfs_stream_dir_node_t *)instance;
if (--sfnp->refs == 0U) {
if (--sdnp->refs == 0U) {
chPoolFree(&((vfs_streams_driver_t *)sfnp->driver)->file_nodes_pool,
(void *)sfnp);
chPoolFree(&((vfs_streams_driver_t *)sdnp->driver)->dir_nodes_pool,
(void *)sdnp);
}
}
@ -320,6 +320,16 @@ static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
return VFS_RET_EOF;
}
static void node_file_release(void *instance) {
vfs_stream_file_node_t *sfnp = (vfs_stream_file_node_t *)instance;
if (--sfnp->refs == 0U) {
chPoolFree(&((vfs_streams_driver_t *)sfnp->driver)->file_nodes_pool,
(void *)sfnp);
}
}
static BaseSequentialStream *node_file_get_stream(void *instance) {
vfs_stream_file_node_t *sfnp = (vfs_stream_file_node_t *)instance;

View File

@ -32,6 +32,26 @@
/* 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. */
/*===========================================================================*/
@ -44,39 +64,33 @@
/* 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_system {
typedef struct vfs_root_driver {
/**
* @brief VFS access mutex.
* @brief Virtual Methods Table.
*/
mutex_t mtx;
/**
* @brief Next registration slot.
*/
vfs_driver_t **next_driver;
/**
* @brief Registration slots.
*/
vfs_driver_t *drivers[VFS_CFG_MAX_DRIVERS];
} vfs_system_t;
const struct vfs_root_driver_vmt *vmt;
__vfs_root_driver_data
} vfs_root_driver_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
/*
* Defaults on the best synchronization mechanism available.
*/
#define VFS_LOCK() osalMutexLock(&vfs.mtx)
#define VFS_UNLOCK() osalMutexUnlock(&vfs.mtx)
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
extern vfs_system_t vfs;
extern vfs_root_driver_t vfs;
#ifdef __cplusplus
extern "C" {

View File

@ -33,6 +33,11 @@
/* 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.
*/
@ -51,9 +56,9 @@
/*===========================================================================*/
/**
* @brief Global VFS state.
* @brief Root VFS driver.
*/
vfs_system_t vfs;
vfs_root_driver_t vfs;
/*===========================================================================*/
/* Module local types. */
@ -81,6 +86,30 @@ typedef struct vfs_root_dir_node {
/* 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,
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. */
/*===========================================================================*/
@ -91,9 +120,6 @@ msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
vfs_driver_t **pp;
do {
err = vfs_parse_match_separator(pathp);
VFS_BREAK_ON_ERROR(err);
err = vfs_parse_filename(pathp, fname);
VFS_BREAK_ON_ERROR(err);
@ -113,6 +139,30 @@ msg_t match_driver(const char **pathp, vfs_driver_t **vdpp) {
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;
return VFS_RET_EOF;
}
/*===========================================================================*/
/* Module exported functions. */
/*===========================================================================*/
@ -127,6 +177,12 @@ void vfsInit(void) {
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
@ -145,8 +201,6 @@ void vfsInit(void) {
msg_t vfsRegisterDriver(vfs_driver_t *vdp) {
msg_t err;
VFS_LOCK();
if (vfs.next_driver >= &vfs.drivers[VFS_CFG_MAX_DRIVERS]) {
err = VFS_RET_NO_RESOURCE;
}
@ -155,8 +209,6 @@ msg_t vfsRegisterDriver(vfs_driver_t *vdp) {
err = VFS_RET_SUCCESS;
}
VFS_UNLOCK();
return err;
}
@ -175,10 +227,31 @@ msg_t vfsOpenDirectory(const char *path, vfs_directory_node_t **vdnpp) {
vfs_driver_t *dp;
do {
err = match_driver(&path, &dp);
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_dir((void *)dp, path, vdnpp);
if (*path == '\0') {
/* Creating a root directory node.*/
vfs_root_dir_node_t *rdnp = chPoolAlloc(&vfs.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 {
/* 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);
@ -249,6 +322,9 @@ msg_t vfsOpenFile(const char *path, vfs_file_node_t **vfnpp) {
vfs_driver_t *dp;
do {
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
err = match_driver(&path, &dp);
VFS_BREAK_ON_ERROR(err);