File modes.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15144 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-11-28 07:06:07 +00:00
parent b7ed77e580
commit cff83afd75
7 changed files with 53 additions and 95 deletions

View File

@ -211,7 +211,9 @@ int main(void) {
} }
/* Opening a file for shell I/O.*/ /* Opening a file for shell I/O.*/
msg = vfsOpenFile("/dev/VSD1", &file); msg = vfsOpenFile("/dev/VSD1",
MODE_OPEN | MODE_RDWR,
&file);
if (msg != VFS_RET_SUCCESS) { if (msg != VFS_RET_SUCCESS) {
chSysHalt("VFS"); chSysHalt("VFS");
} }

View File

@ -109,6 +109,7 @@ include $(CHIBIOS)/os/common/ports/ARMv7-M/compilers/GCC/mk/port.mk
# VFS files (optional). # VFS files (optional).
include $(CHIBIOS)/os/vfs/vfs.mk include $(CHIBIOS)/os/vfs/vfs.mk
include $(CHIBIOS)/os/vfs/drivers/streams/drvstreams.mk include $(CHIBIOS)/os/vfs/drivers/streams/drvstreams.mk
include $(CHIBIOS)/os/vfs/drivers/fatfs/drvfatfs.mk
# Auto-build files in ./source recursively. # Auto-build files in ./source recursively.
include $(CHIBIOS)/tools/mk/autobuild.mk include $(CHIBIOS)/tools/mk/autobuild.mk
# Other files (optional). # Other files (optional).
@ -117,7 +118,7 @@ include $(CHIBIOS)/test/rt/rt_test.mk
include $(CHIBIOS)/test/oslib/oslib_test.mk include $(CHIBIOS)/test/oslib/oslib_test.mk
include $(CHIBIOS)/os/hal/lib/streams/streams.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk
include $(CHIBIOS)/os/various/shell/shell.mk include $(CHIBIOS)/os/various/shell/shell.mk
#include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk
# Define linker script file here. # Define linker script file here.
LDSCRIPT= $(STARTUPLD)/STM32L4R9xI.ld LDSCRIPT= $(STARTUPLD)/STM32L4R9xI.ld

View File

@ -71,13 +71,14 @@
*/ */
#define __vfs_fatfs_dir_node_data \ #define __vfs_fatfs_dir_node_data \
__vfs_directory_node_data \ __vfs_directory_node_data \
unsigned index; DIR dir;
/** /**
* @brief @p vfs_fatfs_file_node_t specific methods. * @brief @p vfs_fatfs_file_node_t specific methods.
*/ */
#define __vfs_fatfs_file_node_methods \ #define __vfs_fatfs_file_node_methods \
__vfs_file_node_methods __vfs_file_node_methods \
FIL file;
/** /**
* @brief @p vfs_fatfs_file_node_t specific data. * @brief @p vfs_fatfs_file_node_t specific data.
@ -157,6 +158,7 @@ static msg_t drv_open_dir(void *instance,
vfs_directory_node_t **vdnpp); vfs_directory_node_t **vdnpp);
static msg_t drv_open_file(void *instance, static msg_t drv_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp); vfs_file_node_t **vfnpp);
static const struct vfs_fatfs_driver_vmt driver_vmt = { static const struct vfs_fatfs_driver_vmt driver_vmt = {
@ -183,19 +185,19 @@ static vfs_offset_t node_file_getpos(void *instance);
static vfs_offset_t node_file_getsize(void *instance); static vfs_offset_t node_file_getsize(void *instance);
static const struct vfs_fatfs_file_node_vmt file_node_vmt = { static const struct vfs_fatfs_file_node_vmt file_node_vmt = {
.release = node_file_release, .release = node_file_release,
.get_stream = node_file_get_stream, .file_get_stream = node_file_get_stream,
.file_read = node_file_read, .file_read = node_file_read,
.file_write = node_file_write, .file_write = node_file_write,
.file_setpos = node_file_setpos, .file_setpos = node_file_setpos,
.file_getpos = node_file_getpos, .file_getpos = node_file_getpos,
.file_getsize = node_file_getsize .file_getsize = node_file_getsize
}; };
static vfs_fatfs_dir_node_t drv_dir_nodes[DRV_DIR_NODES_NUM]; static vfs_fatfs_dir_node_t drv_dir_nodes[DRV_DIR_NODES_NUM];
static vfs_fatfs_file_node_t drv_file_nodes[DRV_FILE_NODES_NUM]; static vfs_fatfs_file_node_t drv_file_nodes[DRV_FILE_NODES_NUM];
static vfs_fatfs_driver_t drv_streams; static vfs_fatfs_driver_t drv_fatfs;
/*===========================================================================*/ /*===========================================================================*/
/* Module local functions. */ /* Module local functions. */
@ -208,26 +210,6 @@ static msg_t drv_open_dir(void *instance,
msg_t err; msg_t err;
do { do {
vfs_fatfs_dir_node_t *ffdnp;
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
err = vfs_parse_match_end(&path);
VFS_BREAK_ON_ERROR(err);
ffdnp = chPoolAlloc(&drv_streams.dir_nodes_pool);
if (ffdnp != NULL) {
/* Node object initialization.*/
ffdnp->vmt = &dir_node_vmt;
ffdnp->refs = 1U;
ffdnp->driver = (vfs_driver_t *)drvp;
ffdnp->index = 0U;
*vdnpp = (vfs_directory_node_t *)ffdnp;
return VFS_RET_SUCCESS;
}
err = VFS_RET_NO_RESOURCE; err = VFS_RET_NO_RESOURCE;
} }
@ -238,46 +220,12 @@ static msg_t drv_open_dir(void *instance,
static msg_t drv_open_file(void *instance, static msg_t drv_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) { vfs_file_node_t **vfnpp) {
vfs_fatfs_driver_t *drvp = (vfs_fatfs_driver_t *)instance; vfs_fatfs_driver_t *drvp = (vfs_fatfs_driver_t *)instance;
const drv_stream_element_t *dsep;
msg_t err; msg_t err;
do { do {
char fname[VFS_CFG_MAX_NAMELEN + 1];
err = vfs_parse_match_separator(&path);
VFS_BREAK_ON_ERROR(err);
err = vfs_parse_filename(&path, fname);
VFS_BREAK_ON_ERROR(err);
err = vfs_parse_match_end(&path);
VFS_BREAK_ON_ERROR(err);
dsep = &drvp->streams[0];
while (dsep->name != NULL) {
if (strncmp(fname, dsep->name, VFS_CFG_MAX_NAMELEN) == 0) {
vfs_fatfs_file_node_t *fffnp;
fffnp = chPoolAlloc(&drv_streams.file_nodes_pool);
if (fffnp != NULL) {
/* Node object initialization.*/
fffnp->vmt = &file_node_vmt;
fffnp->refs = 1U;
fffnp->driver = (vfs_driver_t *)drvp;
fffnp->stream = dsep->stream;
*vfnpp = (vfs_file_node_t *)fffnp;
return VFS_RET_SUCCESS;
}
return VFS_RET_NO_RESOURCE;
}
dsep++;
}
err = VFS_RET_NOT_FOUND; err = VFS_RET_NOT_FOUND;
} }
@ -299,25 +247,12 @@ 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_first(void *instance, vfs_node_info_t *nip) {
vfs_fatfs_dir_node_t *ffdnp = (vfs_fatfs_dir_node_t *)instance; vfs_fatfs_dir_node_t *ffdnp = (vfs_fatfs_dir_node_t *)instance;
ffdnp->index = 0U; return VFS_RET_EOF;
return node_dir_next(instance, nip);
} }
static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) { static msg_t node_dir_next(void *instance, vfs_node_info_t *nip) {
vfs_fatfs_dir_node_t *ffdnp = (vfs_fatfs_dir_node_t *)instance; vfs_fatfs_dir_node_t *ffdnp = (vfs_fatfs_dir_node_t *)instance;
if (drv_streams.streams[ffdnp->index].name != NULL) {
nip->attr = VFS_NODE_ATTR_ISSTREAM;
nip->size = (vfs_offset_t)0;
strcpy(nip->name, drv_streams.streams[ffdnp->index].name);
ffdnp->index++;
return VFS_RET_SUCCESS;
}
return VFS_RET_EOF; return VFS_RET_EOF;
} }
@ -375,26 +310,24 @@ static vfs_offset_t node_file_getsize(void *instance) {
/* Module exported functions. */ /* Module exported functions. */
/*===========================================================================*/ /*===========================================================================*/
vfs_driver_t *drvStreamsInit(const char *rootname, vfs_driver_t *drvFatfsInit(const char *rootname) {
const drv_stream_element_t *streams) {
drv_streams.vmt = &driver_vmt; drv_fatfs.vmt = &driver_vmt;
drv_streams.rootname = rootname; drv_fatfs.rootname = rootname;
drv_streams.streams = streams; chPoolObjectInit(&drv_fatfs.dir_nodes_pool,
chPoolObjectInit(&drv_streams.dir_nodes_pool,
sizeof (vfs_fatfs_dir_node_t), sizeof (vfs_fatfs_dir_node_t),
chCoreAllocAligned); chCoreAllocAligned);
chPoolLoadArray(&drv_streams.dir_nodes_pool, chPoolLoadArray(&drv_fatfs.dir_nodes_pool,
drv_dir_nodes, drv_dir_nodes,
DRV_DIR_NODES_NUM); DRV_DIR_NODES_NUM);
chPoolObjectInit(&drv_streams.file_nodes_pool, chPoolObjectInit(&drv_fatfs.file_nodes_pool,
sizeof (vfs_fatfs_file_node_t), sizeof (vfs_fatfs_file_node_t),
chCoreAllocAligned); chCoreAllocAligned);
chPoolLoadArray(&drv_streams.file_nodes_pool, chPoolLoadArray(&drv_fatfs.file_nodes_pool,
drv_file_nodes, drv_file_nodes,
DRV_FILE_NODES_NUM); DRV_FILE_NODES_NUM);
return (vfs_driver_t *)&drv_streams; return (vfs_driver_t *)&drv_fatfs;
} }
/** @} */ /** @} */

View File

@ -156,6 +156,7 @@ static msg_t drv_open_dir(void *instance,
vfs_directory_node_t **vdnpp); vfs_directory_node_t **vdnpp);
static msg_t drv_open_file(void *instance, static msg_t drv_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp); vfs_file_node_t **vfnpp);
static const struct vfs_streams_driver_vmt driver_vmt = { static const struct vfs_streams_driver_vmt driver_vmt = {
@ -237,11 +238,14 @@ static msg_t drv_open_dir(void *instance,
static msg_t drv_open_file(void *instance, static msg_t drv_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) { vfs_file_node_t **vfnpp) {
vfs_streams_driver_t *drvp = (vfs_streams_driver_t *)instance; vfs_streams_driver_t *drvp = (vfs_streams_driver_t *)instance;
const drv_stream_element_t *dsep; const drv_stream_element_t *dsep;
msg_t err; msg_t err;
(void)mode;
do { do {
char fname[VFS_CFG_MAX_NAMELEN + 1]; char fname[VFS_CFG_MAX_NAMELEN + 1];

View File

@ -32,6 +32,21 @@
/* Module constants. */ /* Module constants. */
/*===========================================================================*/ /*===========================================================================*/
/**
* @name File open modes.
* @{
*/
#define MODE_READ 0x01U
#define MODE_WRITE 0x02U
#define MODE_RDWR (MODE_READ | MODE_WRITE)
#define MODE_OPEN 0x00U
#define MODE_CREATE_EXCL 0x04U
#define MODE_CREATE_TRUNCATE 0x08U
#define MODE_CREATE_OPEN 0x10U
#define MODE_APPEND 0x20U
#define MODE_OPEN_APPEND (MODE_CREATE_OPEN | MODE_APPEND)
/** @} */
/*===========================================================================*/ /*===========================================================================*/
/* Module pre-compile time settings. */ /* Module pre-compile time settings. */
/*===========================================================================*/ /*===========================================================================*/
@ -54,6 +69,7 @@
vfs_directory_node_t **vdnpp); \ vfs_directory_node_t **vdnpp); \
msg_t (*open_file)(void *instance, \ msg_t (*open_file)(void *instance, \
const char *path, \ const char *path, \
unsigned mode, \
vfs_file_node_t **vfnpp); vfs_file_node_t **vfnpp);
/** /**

View File

@ -103,7 +103,7 @@ extern "C" {
vfs_node_info_t *nip); vfs_node_info_t *nip);
msg_t vfsReadDirectoryNext(vfs_directory_node_t *vdnp, msg_t vfsReadDirectoryNext(vfs_directory_node_t *vdnp,
vfs_node_info_t *nip); vfs_node_info_t *nip);
msg_t vfsOpenFile(const char *name, vfs_file_node_t **vfnpp); msg_t vfsOpenFile(const char *name, unsigned mode, vfs_file_node_t **vfnpp);
void vfsCloseFile(vfs_file_node_t *vfnp); void vfsCloseFile(vfs_file_node_t *vfnp);
ssize_t vfsReadFile(vfs_file_node_t *vfnp, uint8_t *buf, size_t n); 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); ssize_t vfsWriteFile(vfs_file_node_t *vfnp, const uint8_t *buf, size_t n);

View File

@ -93,6 +93,7 @@ static msg_t root_open_dir(void *instance,
vfs_directory_node_t **vdnpp); vfs_directory_node_t **vdnpp);
static msg_t root_open_file(void *instance, static msg_t root_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp); vfs_file_node_t **vfnpp);
static const struct vfs_root_driver_vmt root_driver_vmt = { static const struct vfs_root_driver_vmt root_driver_vmt = {
@ -182,6 +183,7 @@ static msg_t root_open_dir(void *instance,
static msg_t root_open_file(void *instance, static msg_t root_open_file(void *instance,
const char *path, const char *path,
unsigned mode,
vfs_file_node_t **vfnpp) { vfs_file_node_t **vfnpp) {
msg_t err; msg_t err;
@ -202,7 +204,7 @@ static msg_t root_open_file(void *instance,
err = match_driver(&path, &dp); err = match_driver(&path, &dp);
VFS_BREAK_ON_ERROR(err); VFS_BREAK_ON_ERROR(err);
err = dp->vmt->open_file((void *)dp, path, vfnpp); err = dp->vmt->open_file((void *)dp, path, mode, vfnpp);
} }
} }
while (false); while (false);
@ -370,9 +372,9 @@ msg_t vfsReadDirectoryNext(vfs_directory_node_t *vdnp,
* *
* @api * @api
*/ */
msg_t vfsOpenFile(const char *path, vfs_file_node_t **vfnpp) { msg_t vfsOpenFile(const char *path, unsigned mode, vfs_file_node_t **vfnpp) {
return vfs.vmt->open_file((vfs_driver_t *)&vfs, path, vfnpp); return vfs.vmt->open_file((vfs_driver_t *)&vfs, path, mode, vfnpp);
} }
/** /**