2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 /* FreeRTOS includes. */
\r
31 #include "FreeRTOS.h"
\r
34 /* Standard includes. */
\r
40 /* FreeRTOS+CLI includes. */
\r
41 #include "FreeRTOS_CLI.h"
\r
43 /* File system includes. */
\r
45 #include "api_mdriver_ram.h"
\r
48 #define snprintf _snprintf
\r
51 #define cliNEW_LINE "\r\n"
\r
53 /*******************************************************************************
\r
54 * See the URL in the comments within main.c for the location of the online
\r
56 ******************************************************************************/
\r
59 * Print out information on a single file.
\r
61 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );
\r
64 * Copies an existing file into a newly created file.
\r
66 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
67 int32_t lSourceFileLength,
\r
68 const char *pcDestinationFile,
\r
69 char *pxWriteBuffer,
\r
70 size_t xWriteBufferLen );
\r
73 * Implements the DIR command.
\r
75 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
78 * Implements the CD command.
\r
80 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
83 * Implements the DEL command.
\r
85 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
88 * Implements the TYPE command.
\r
90 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
93 * Implements the COPY command.
\r
95 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
97 /* Structure that defines the DIR command line command, which lists all the
\r
98 files in the current directory. */
\r
99 static const CLI_Command_Definition_t xDIR =
\r
101 "dir", /* The command string to type. */
\r
102 "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
103 prvDIRCommand, /* The function to run. */
\r
104 0 /* No parameters are expected. */
\r
107 /* Structure that defines the CD command line command, which changes the
\r
108 working directory. */
\r
109 static const CLI_Command_Definition_t xCD =
\r
111 "cd", /* The command string to type. */
\r
112 "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
113 prvCDCommand, /* The function to run. */
\r
114 1 /* One parameter is expected. */
\r
117 /* Structure that defines the TYPE command line command, which prints the
\r
118 contents of a file to the console. */
\r
119 static const CLI_Command_Definition_t xTYPE =
\r
121 "type", /* The command string to type. */
\r
122 "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
123 prvTYPECommand, /* The function to run. */
\r
124 1 /* One parameter is expected. */
\r
127 /* Structure that defines the DEL command line command, which deletes a file. */
\r
128 static const CLI_Command_Definition_t xDEL =
\r
130 "del", /* The command string to type. */
\r
131 "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
132 prvDELCommand, /* The function to run. */
\r
133 1 /* One parameter is expected. */
\r
136 /* Structure that defines the COPY command line command, which deletes a file. */
\r
137 static const CLI_Command_Definition_t xCOPY =
\r
139 "copy", /* The command string to type. */
\r
140 "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
141 prvCOPYCommand, /* The function to run. */
\r
142 2 /* Two parameters are expected. */
\r
146 /*-----------------------------------------------------------*/
\r
148 void vRegisterFileSystemCLICommands( void )
\r
150 /* Register all the command line commands defined immediately above. */
\r
151 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
152 FreeRTOS_CLIRegisterCommand( &xCD );
\r
153 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
154 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
155 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
157 /*-----------------------------------------------------------*/
\r
159 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
161 const char *pcParameter;
\r
162 BaseType_t xParameterStringLength, xReturn = pdTRUE;
\r
163 static F_FILE *pxFile = NULL;
\r
166 size_t xColumns = 50U;
\r
168 /* Ensure there is always a null terminator after each character written. */
\r
169 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
171 /* Ensure the buffer leaves space for the \r\n. */
\r
172 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
173 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
175 if( xWriteBufferLen < xColumns )
\r
177 /* Ensure the loop that uses xColumns as an end condition does not
\r
178 write off the end of the buffer. */
\r
179 xColumns = xWriteBufferLen;
\r
182 if( pxFile == NULL )
\r
184 /* The file has not been opened yet. Find the file name. */
\r
185 pcParameter = FreeRTOS_CLIGetParameter
\r
187 pcCommandString, /* The command string itself. */
\r
188 1, /* Return the first parameter. */
\r
189 &xParameterStringLength /* Store the parameter string length. */
\r
192 /* Sanity check something was returned. */
\r
193 configASSERT( pcParameter );
\r
195 /* Attempt to open the requested file. */
\r
196 pxFile = f_open( pcParameter, "r" );
\r
199 if( pxFile != NULL )
\r
201 /* Read the next chunk of data from the file. */
\r
202 for( xByte = 0; xByte < xColumns; xByte++ )
\r
204 iChar = f_getc( pxFile );
\r
208 /* No more characters to return. */
\r
215 pcWriteBuffer[ xByte ] = ( char ) iChar;
\r
220 if( pxFile == NULL )
\r
222 /* Either the file was not opened, or all the data from the file has
\r
223 been returned and the file is now closed. */
\r
227 strcat( pcWriteBuffer, cliNEW_LINE );
\r
231 /*-----------------------------------------------------------*/
\r
233 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
235 const char *pcParameter;
\r
236 BaseType_t xParameterStringLength;
\r
237 unsigned char ucReturned;
\r
238 size_t xStringLength;
\r
240 /* Obtain the parameter string. */
\r
241 pcParameter = FreeRTOS_CLIGetParameter
\r
243 pcCommandString, /* The command string itself. */
\r
244 1, /* Return the first parameter. */
\r
245 &xParameterStringLength /* Store the parameter string length. */
\r
248 /* Sanity check something was returned. */
\r
249 configASSERT( pcParameter );
\r
251 /* Attempt to move to the requested directory. */
\r
252 ucReturned = f_chdir( pcParameter );
\r
254 if( ucReturned == F_NO_ERROR )
\r
256 sprintf( pcWriteBuffer, "In: " );
\r
257 xStringLength = strlen( pcWriteBuffer );
\r
258 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
262 sprintf( pcWriteBuffer, "Error" );
\r
265 strcat( pcWriteBuffer, cliNEW_LINE );
\r
269 /*-----------------------------------------------------------*/
\r
271 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
273 static F_FIND *pxFindStruct = NULL;
\r
274 unsigned char ucReturned;
\r
275 BaseType_t xReturn = pdFALSE;
\r
277 /* This assumes pcWriteBuffer is long enough. */
\r
278 ( void ) pcCommandString;
\r
280 /* Ensure the buffer leaves space for the \r\n. */
\r
281 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
282 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
284 if( pxFindStruct == NULL )
\r
286 /* This is the first time this function has been executed since the Dir
\r
287 command was run. Create the find structure. */
\r
288 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
290 if( pxFindStruct != NULL )
\r
292 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
294 if( ucReturned == F_NO_ERROR )
\r
296 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
301 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
306 snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
311 /* The find struct has already been created. Find the next file in
\r
313 ucReturned = f_findnext( pxFindStruct );
\r
315 if( ucReturned == F_NO_ERROR )
\r
317 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
322 /* There are no more files. Free the find structure. */
\r
323 vPortFree( pxFindStruct );
\r
324 pxFindStruct = NULL;
\r
326 /* No string to return. */
\r
327 pcWriteBuffer[ 0 ] = 0x00;
\r
331 strcat( pcWriteBuffer, cliNEW_LINE );
\r
335 /*-----------------------------------------------------------*/
\r
337 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
339 const char *pcParameter;
\r
340 BaseType_t xParameterStringLength;
\r
341 unsigned char ucReturned;
\r
343 /* This function assumes xWriteBufferLen is large enough! */
\r
344 ( void ) xWriteBufferLen;
\r
346 /* Obtain the parameter string. */
\r
347 pcParameter = FreeRTOS_CLIGetParameter
\r
349 pcCommandString, /* The command string itself. */
\r
350 1, /* Return the first parameter. */
\r
351 &xParameterStringLength /* Store the parameter string length. */
\r
354 /* Sanity check something was returned. */
\r
355 configASSERT( pcParameter );
\r
357 /* Attempt to delete the file. */
\r
358 ucReturned = f_delete( pcParameter );
\r
360 if( ucReturned == F_NO_ERROR )
\r
362 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );
\r
366 sprintf( pcWriteBuffer, "Error" );
\r
369 strcat( pcWriteBuffer, cliNEW_LINE );
\r
373 /*-----------------------------------------------------------*/
\r
375 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
377 char *pcSourceFile, *pcDestinationFile;
\r
378 BaseType_t xParameterStringLength;
\r
379 long lSourceLength, lDestinationLength = 0;
\r
381 /* Obtain the name of the destination file. */
\r
382 pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter
\r
384 pcCommandString, /* The command string itself. */
\r
385 2, /* Return the second parameter. */
\r
386 &xParameterStringLength /* Store the parameter string length. */
\r
389 /* Sanity check something was returned. */
\r
390 configASSERT( pcDestinationFile );
\r
392 /* Obtain the name of the source file. */
\r
393 pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
\r
395 pcCommandString, /* The command string itself. */
\r
396 1, /* Return the first parameter. */
\r
397 &xParameterStringLength /* Store the parameter string length. */
\r
400 /* Sanity check something was returned. */
\r
401 configASSERT( pcSourceFile );
\r
403 /* Terminate the string. */
\r
404 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
406 /* See if the source file exists, obtain its length if it does. */
\r
407 lSourceLength = f_filelength( pcSourceFile );
\r
409 if( lSourceLength == 0 )
\r
411 sprintf( pcWriteBuffer, "Source file does not exist" );
\r
415 /* See if the destination file exists. */
\r
416 lDestinationLength = f_filelength( pcDestinationFile );
\r
418 if( lDestinationLength != 0 )
\r
420 sprintf( pcWriteBuffer, "Error: Destination file already exists" );
\r
424 /* Continue only if the source file exists and the destination file does
\r
426 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
428 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
430 sprintf( pcWriteBuffer, "Copy made" );
\r
434 sprintf( pcWriteBuffer, "Error during copy" );
\r
438 strcat( pcWriteBuffer, cliNEW_LINE );
\r
442 /*-----------------------------------------------------------*/
\r
444 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
445 int32_t lSourceFileLength,
\r
446 const char *pcDestinationFile,
\r
447 char *pxWriteBuffer,
\r
448 size_t xWriteBufferLen )
\r
450 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
452 BaseType_t xReturn = pdPASS;
\r
454 /* NOTE: Error handling has been omitted for clarity. */
\r
456 while( lBytesRead < lSourceFileLength )
\r
458 /* How many bytes are left? */
\r
459 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
461 /* How many bytes should be read this time around the loop. Can't
\r
462 read more bytes than will fit into the buffer. */
\r
463 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
465 lBytesToRead = ( long ) xWriteBufferLen;
\r
469 lBytesToRead = lBytesRemaining;
\r
472 /* Open the source file, seek past the data that has already been
\r
473 read from the file, read the next block of data, then close the
\r
474 file again so the destination file can be opened. */
\r
475 pxFile = f_open( pcSourceFile, "r" );
\r
476 if( pxFile != NULL )
\r
478 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
479 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
488 /* Open the destination file and write the block of data to the end of
\r
490 pxFile = f_open( pcDestinationFile, "a" );
\r
491 if( pxFile != NULL )
\r
493 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
502 lBytesRead += lBytesToRead;
\r
507 /*-----------------------------------------------------------*/
\r
509 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )
\r
511 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
512 const char * pcAttrib;
\r
514 /* Point pcAttrib to a string that describes the file. */
\r
515 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
517 pcAttrib = pcDirectory;
\r
519 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
521 pcAttrib = pcReadOnlyFile;
\r
525 pcAttrib = pcWritableFile;
\r
528 /* Create a string that includes the file name, the file size and the
\r
529 attributes string. */
\r
530 sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, ( int ) pxFindStruct->filesize );
\r