From 7d76e688a7fc9f8dda83442c1676d0c5ce9c7820 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 5 Dec 2021 11:24:33 +0000 Subject: [PATCH] Added VFS-related commands to the shell, disabled by default. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15198 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- demos/STM32/RT-VFS-FATFS/main.c | 56 +------------- .../make/stm32g474re_nucleo64.make | 2 +- .../make/stm32l4r9ai_discovery.make | 2 +- os/various/shell/shell_cmd.c | 76 +++++++++++++++++++ os/various/shell/shell_cmd.h | 4 + readme.txt | 1 + 6 files changed, 84 insertions(+), 57 deletions(-) diff --git a/demos/STM32/RT-VFS-FATFS/main.c b/demos/STM32/RT-VFS-FATFS/main.c index d65d68157..0706b2b4e 100644 --- a/demos/STM32/RT-VFS-FATFS/main.c +++ b/demos/STM32/RT-VFS-FATFS/main.c @@ -129,69 +129,15 @@ static const drv_stream_element_t streams[] = { {NULL, NULL} }; -/* Generic large buffer.*/ -static char pathbuf[1024]; - -static void scan_nodes(BaseSequentialStream *chp, char *path) { - msg_t res; - vfs_directory_node_c *dirp; - static vfs_node_info_t ni; - - chprintf(chp, "%s\r\n", path); - res = vfsOpenDirectory(path, &dirp); - if (res == VFS_RET_SUCCESS) { - size_t i = strlen(path); - - while (true) { - char *fn = ni.name; - res = vfsReadDirectoryNext(dirp, &ni); - if (res != VFS_RET_SUCCESS) { - break; - } - - fn = ni.name; - if (ni.attr & VFS_NODE_ATTR_ISDIR) { - strcpy(path + i, fn); - strcat(path + i, "/"); - scan_nodes(chp, path); - path[i] = '\0'; - } - else { - chprintf(chp, "%s%s\r\n", path, fn); - } - } - - vfsCloseDirectory(dirp); - } -} - /*===========================================================================*/ /* Command line related. */ /*===========================================================================*/ #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) -static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) { - - (void)argv; - - if (argc > 0) { - chprintf(chp, "Usage: tree\r\n"); - return; - } - - strcpy(pathbuf, "/"); - scan_nodes(chp, (char *)pathbuf); -} - -static const ShellCommand commands[] = { - {"tree", cmd_tree}, - {NULL, NULL} -}; - static ShellConfig shell_cfg1 = { NULL, - commands + NULL }; /*===========================================================================*/ diff --git a/demos/STM32/RT-VFS-FATFS/make/stm32g474re_nucleo64.make b/demos/STM32/RT-VFS-FATFS/make/stm32g474re_nucleo64.make index 26d307df7..32dd795f4 100644 --- a/demos/STM32/RT-VFS-FATFS/make/stm32g474re_nucleo64.make +++ b/demos/STM32/RT-VFS-FATFS/make/stm32g474re_nucleo64.make @@ -158,7 +158,7 @@ CPPWARN = -Wall -Wextra -Wundef # # List all user C define here, like -D_DEBUG=1 -UDEFS = +UDEFS = -DSHELL_CMD_FILES_ENABLED=1 # Define ASM defines here UADEFS = diff --git a/demos/STM32/RT-VFS-FATFS/make/stm32l4r9ai_discovery.make b/demos/STM32/RT-VFS-FATFS/make/stm32l4r9ai_discovery.make index 6ed77ca41..176a07b39 100644 --- a/demos/STM32/RT-VFS-FATFS/make/stm32l4r9ai_discovery.make +++ b/demos/STM32/RT-VFS-FATFS/make/stm32l4r9ai_discovery.make @@ -158,7 +158,7 @@ CPPWARN = -Wall -Wextra -Wundef # # List all user C define here, like -D_DEBUG=1 -UDEFS = +UDEFS = -DSHELL_CMD_FILES_ENABLED=1 # Define ASM defines here UADEFS = diff --git a/os/various/shell/shell_cmd.c b/os/various/shell/shell_cmd.c index b8fc6dbb6..099aba6a2 100644 --- a/os/various/shell/shell_cmd.c +++ b/os/various/shell/shell_cmd.c @@ -30,6 +30,10 @@ #include "shell_cmd.h" #include "chprintf.h" +#if (SHELL_CMD_FILES_ENABLED == TRUE) || defined(__DOXYGEN__) +#include "vfs.h" +#endif + #if (SHELL_CMD_TEST_ENABLED == TRUE) || defined(__DOXYGEN__) #include "rt_test_root.h" #include "oslib_test_root.h" @@ -226,6 +230,75 @@ static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) { } #endif +#if (SHELL_CMD_FILES_ENABLED == TRUE) || defined(__DOXYGEN__) +static void scan_nodes(BaseSequentialStream *chp, + char *path, + vfs_node_info_t *nip) { + msg_t res; + vfs_directory_node_c *dirp; + + chprintf(chp, "%s\r\n", path); + res = vfsOpenDirectory(path, &dirp); + if (res == VFS_RET_SUCCESS) { + size_t i = strlen(path); + + while (true) { + char *fn = nip->name; + res = vfsReadDirectoryNext(dirp, nip); + if (res != VFS_RET_SUCCESS) { + break; + } + + fn = nip->name; + if (nip->attr & VFS_NODE_ATTR_ISDIR) { + strcpy(path + i, fn); + strcat(path + i, "/"); + scan_nodes(chp, path, nip); + path[i] = '\0'; + } + else { + chprintf(chp, "%s%s\r\n", path, fn); + } + } + + vfsCloseDirectory(dirp); + } +} + +static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) { + char *pathbuf = NULL; + vfs_node_info_t *nip = NULL; + + (void)argv; + + if (argc > 0) { + chprintf(chp, "Usage: tree\r\n"); + return; + } + + do { + pathbuf = (char *)chHeapAlloc(NULL, 1024); + nip = (vfs_node_info_t *)chHeapAlloc(NULL, 1024); + if ((pathbuf == NULL) || (nip == NULL)) { + chprintf(chp, "Out of memory\r\n"); + break; + } + + strcpy(pathbuf, "/"); + scan_nodes(chp, pathbuf, nip); + } + while (false); + + if (pathbuf != NULL) { + chHeapFree((void *)pathbuf); + } + + if (nip != NULL) { + chHeapFree((void *)nip); + } +} +#endif + /*===========================================================================*/ /* Module exported functions. */ /*===========================================================================*/ @@ -252,6 +325,9 @@ const ShellCommand shell_local_commands[] = { #if SHELL_CMD_THREADS_ENABLED == TRUE {"threads", cmd_threads}, #endif +#if SHELL_CMD_FILES_ENABLED == TRUE + {"tree", cmd_tree}, +#endif #if SHELL_CMD_TEST_ENABLED == TRUE {"test", cmd_test}, #endif diff --git a/os/various/shell/shell_cmd.h b/os/various/shell/shell_cmd.h index 2ccf6d976..72147a6f3 100644 --- a/os/various/shell/shell_cmd.h +++ b/os/various/shell/shell_cmd.h @@ -61,6 +61,10 @@ #define SHELL_CMD_TEST_ENABLED TRUE #endif +#if !defined(SHELL_CMD_FILES_ENABLED) || defined(__DOXYGEN__) +#define SHELL_CMD_FILES_ENABLED FALSE +#endif + #if !defined(SHELL_CMD_TEST_WA_SIZE) || defined(__DOXYGEN__) #define SHELL_CMD_TEST_WA_SIZE THD_WORKING_AREA_SIZE(512) #endif diff --git a/readme.txt b/readme.txt index 7b7f239cc..56e15b89c 100644 --- a/readme.txt +++ b/readme.txt @@ -78,6 +78,7 @@ in chsem.c. - TODO: Implement pointers check everywhere in RT replacing assertions for NULL pointers. +- NEW: Added VFS-related commands to the shell, disabled by default. - NEW: Added a new VFS subsystem (Virtual File System), it allows to assemble trees of files from multiple "File System Drivers" into a single tree and access it as a whole.