More VFS code.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15121 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-11-21 13:01:29 +00:00
parent 47e16b7bd8
commit d1a4ba093d
6 changed files with 131 additions and 41 deletions

View File

@ -65,7 +65,10 @@
/** @} */
/* Dependencies.*/
#include "ch.h"
#include "osal.h"
#include "hal_objects.h"
#include "hal_streams.h"
#include "hal_channels.h"
/* License.*/
#include "chlicense.h"
@ -75,6 +78,7 @@
#include "vfschecks.h"
/* Base VFS headers.*/
#include "vfserrors.h"
#include "vfsnodes.h"
#include "vfsdrivers.h"
#include "vfssystem.h"

View File

@ -48,15 +48,17 @@
* @brief @p vfs_node_t specific methods.
*/
#define __vfs_driver_methods \
/* Instance offset, used for multiple inheritance, normally zero. It
represents the offset between the current object and the container
object*/ \
size_t instance_offset;
_base_object_methods \
/* Returns a pointer to the driver name constant.*/ \
const char *(*get_name)(void); \
vfserr_t (*open_dir)(const char *path, vfs_directory_node_t **vdnpp); \
vfserr_t (*open_file)(const char *path, vfs_file_node_t **vfnpp);
/**
* @brief @p vfs_node_t specific data.
*/
#define __vfs_driver_data
#define __vfs_driver_data \
_base_object_data
/**
* @brief @p vfs_node_t virtual methods table.

View File

@ -0,0 +1,76 @@
/*
ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
ChibiOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file vfs/include/vfserrors.h
* @brief VFS erors header file.
*
* @addtogroup VFS_ERRORS
* @{
*/
#ifndef VFSERRORS_H
#define VFSERRORS_H
/*===========================================================================*/
/* Module constants. */
/*===========================================================================*/
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief Type of a VFS error code.
*/
typedef enum {
VFS_RET_SUCCESS = 0,
VFS_RET_NO_RESOURCE = -1,
VFS_RET_NO_DRIVER = -2,
VFS_RET_INVALID_PATH = -3,
VFS_RET_NOT_FOUND = -4
} vfserr_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* VFSERRORS_H */
/** @} */

View File

@ -68,6 +68,15 @@ typedef uint32_t vfs_fileoffset_t;
*/
typedef uint32_t vfs_nodeattr_t;
/**
* @brief Type of a file node.
*/
typedef enum {
VFS_NODE_TYPE_FILE = 0,
VFS_NODE_TYPE_STREAM = 1,
VFS_NODE_TYPE_CHANNEL = 2,
} vfs_node_type_t;
/**
* @brief Type of a node information structure.
* @todo Add attributes, permissions etc.
@ -91,10 +100,7 @@ typedef struct vfs_node_info {
* @brief @p vfs_node_t specific methods.
*/
#define __vfs_node_methods \
/* Instance offset, used for multiple inheritance, normally zero. It
represents the offset between the current object and the container
object*/ \
size_t instance_offset; \
_base_object_methods \
/* Node release, the object is disposed when the counter reaches zero.*/ \
void (*release)(void *instance);
@ -102,6 +108,7 @@ typedef struct vfs_node_info {
* @brief @p vfs_node_t specific data.
*/
#define __vfs_node_data \
_base_object_data \
/* Number of references to this node.*/ \
unsigned references_counter; \
/* Driver handling this node.*/ \
@ -173,7 +180,15 @@ typedef struct vfs_directory_node {
* @brief @p vfs_file_node_t virtual methods table.
*/
struct vfs_file_node_vmt {
__vfs_file_node_methods
__vfs_file_node_methods \
/* Type of the interface of the attached object.*/ \
vfs_node_type_t type; \
/* Object instance pointers.*/ \
union { \
/*BaseFileStream *file;*/ \
BaseSequentialStream *stream; \
BaseAsynchronousChannel *channel; \
} instance;
};
/**

View File

@ -44,15 +44,6 @@
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief Type of a VFS error code.
*/
typedef enum {
VFS_RET_SUCCESS = 0,
VFS_RET_NO_RESOURCE = -1,
VFS_RET_INVALID_PATH = -2,
} vfserr_t;
/**
* @brief @p vfs_system_directory_node_t specific methods.
*/
@ -87,17 +78,10 @@ typedef struct vfs_system_directory_node {
* @brief Type of a structure representing the VFS system.
*/
typedef struct vfs_system {
#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
/**
* @brief VFS access mutex.
*/
mutex_t mtx;
#else
/**
* @brief VFS access fallback semaphore.
*/
semaphore_t sem;
#endif
/**
* @brief Absolute root node.
*/
@ -119,13 +103,8 @@ typedef struct vfs_system {
/*
* Defaults on the best synchronization mechanism available.
*/
#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
#define VFS_LOCK() chMtxLock(&vfs.mtx)
#define VFS_UNLOCK() chMtxUnlock(&vfs.mtx)
#else
#define VFS_LOCK() (void) chSemWait(&vfs.sem)
#define VFS_UNLOCK() chSemSignal(&vfs.sem)
#endif
#define VFS_LOCK() osalMutexLock(&vfs.mtx)
#define VFS_UNLOCK() osalMutexUnlock(&vfs.mtx)
/*===========================================================================*/
/* External declarations. */

View File

@ -25,6 +25,8 @@
* @{
*/
#include <string.h>
#include "vfs.h"
/*===========================================================================*/
@ -98,14 +100,28 @@ vfserr_t vfs_match_filename(const char **pathp, char *fname) {
return err;
}
vfserr_t vfs_match_driver(const char **pathp, vfs_driver_t **dpp) {
vfserr_t vfs_match_driver(const char **pathp, vfs_driver_t **vdpp) {
char fname[VFS_CFG_MAX_NAMELEN + 1];
vfserr_t err;
vfs_driver_t **pp;
do {
err = vfs_match_separator(pathp);
BREAK_ON_ERROR(err);
err = vfs_match_filename(pathp, fname);
BREAK_ON_ERROR(err);
/* Searching among registered drivers.*/
pp = &vfs.drivers[0];
while (pp < vfs.next_driver) {
if (strncmp(fname, (*pp)->vmt->get_name(), VFS_CFG_MAX_NAMELEN) == 0) {
*vdpp = *pp;
return VFS_RET_SUCCESS;
}
}
err = VFS_RET_NO_DRIVER;
}
while (false);
@ -170,11 +186,10 @@ vfserr_t vfsOpenDirectory(const char *path, vfs_directory_node_t **vdnpp) {
vfs_driver_t *dp;
do {
err = vfs_match_separator(&path);
BREAK_ON_ERROR(err);
err = vfs_match_driver(&path, &dp);
BREAK_ON_ERROR(err);
err = dp->vmt->open_dir(path, vdnpp);
}
while (false);
@ -194,11 +209,10 @@ vfserr_t vfsOpenFile(const char *path, vfs_file_node_t **vfnpp) {
vfs_driver_t *dp;
do {
err = vfs_match_separator(&path);
BREAK_ON_ERROR(err);
err = vfs_match_driver(&path, &dp);
BREAK_ON_ERROR(err);
err = dp->vmt->open_file(path, vfnpp);
}
while (false);