2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
6 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
8 ***************************************************************************
\r
10 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
11 * Complete, revised, and edited pdf reference manuals are also *
\r
14 * Purchasing FreeRTOS documentation will not only help you, by *
\r
15 * ensuring you get running as quickly as possible and with an *
\r
16 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
17 * the FreeRTOS project to continue with its mission of providing *
\r
18 * professional grade, cross platform, de facto standard solutions *
\r
19 * for microcontrollers - completely free of charge! *
\r
21 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
23 * Thank you for using FreeRTOS, and thank you for your support! *
\r
25 ***************************************************************************
\r
28 This file is part of the FreeRTOS distribution.
\r
30 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
31 the terms of the GNU General Public License (version 2) as published by the
\r
32 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
34 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
35 distribute a combined work that includes FreeRTOS without being obliged to
\r
36 provide the source code for proprietary components outside of the FreeRTOS
\r
39 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
40 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
41 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
42 details. You should have received a copy of the GNU General Public License
\r
43 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
44 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
45 writing to Real Time Engineers Ltd., contact details for whom are available
\r
46 on the FreeRTOS WEB site.
\r
50 ***************************************************************************
\r
52 * Having a problem? Start by reading the FAQ "My application does *
\r
53 * not run, what could be wrong?" *
\r
55 * http://www.FreeRTOS.org/FAQHelp.html *
\r
57 ***************************************************************************
\r
60 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
61 license and Real Time Engineers Ltd. contact details.
\r
63 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
64 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
65 fully thread aware and reentrant UDP/IP stack.
\r
67 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
68 Integrity Systems, who sell the code with commercial support,
\r
69 indemnification and middleware, under the OpenRTOS brand.
\r
71 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
72 engineered and independently SIL3 certified version for use in safety and
\r
73 mission critical applications that require provable dependability.
\r
76 /* FreeRTOS includes. */
\r
77 #include "FreeRTOS.h"
\r
80 /* Standard includes. */
\r
86 /* FreeRTOS+CLI includes. */
\r
87 #include "FreeRTOS_CLI.h"
\r
89 /* File system includes. */
\r
91 #include "api_mdriver_ram.h"
\r
94 #define snprintf _snprintf
\r
97 #define cliNEW_LINE "\r\n"
\r
99 /*******************************************************************************
\r
100 * See the URL in the comments within main.c for the location of the online
\r
102 ******************************************************************************/
\r
105 * Print out information on a single file.
\r
107 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );
\r
110 * Copies an existing file into a newly created file.
\r
112 static portBASE_TYPE prvPerformCopy( char *pcSourceFile,
\r
113 int32_t lSourceFileLength,
\r
114 char *pcDestinationFile,
\r
115 char *pxWriteBuffer,
\r
116 size_t xWriteBufferLen );
\r
119 * Implements the DIR command.
\r
121 static portBASE_TYPE prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
124 * Implements the CD command.
\r
126 static portBASE_TYPE prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
129 * Implements the DEL command.
\r
131 static portBASE_TYPE prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
134 * Implements the TYPE command.
\r
136 static portBASE_TYPE prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
139 * Implements the COPY command.
\r
141 static portBASE_TYPE prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
144 * Registers the CLI commands that are specific to the files system with the
\r
145 * FreeRTOS+CLI command interpreter.
\r
147 void vRegisterFileSystemCLICommands( void );
\r
149 /* Structure that defines the DIR command line command, which lists all the
\r
150 files in the current directory. */
\r
151 static const CLI_Command_Definition_t xDIR =
\r
153 "dir", /* The command string to type. */
\r
154 "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
155 prvDIRCommand, /* The function to run. */
\r
156 0 /* No parameters are expected. */
\r
159 /* Structure that defines the CD command line command, which changes the
\r
160 working directory. */
\r
161 static const CLI_Command_Definition_t xCD =
\r
163 "cd", /* The command string to type. */
\r
164 "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
165 prvCDCommand, /* The function to run. */
\r
166 1 /* One parameter is expected. */
\r
169 /* Structure that defines the TYPE command line command, which prints the
\r
170 contents of a file to the console. */
\r
171 static const CLI_Command_Definition_t xTYPE =
\r
173 "type", /* The command string to type. */
\r
174 "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
175 prvTYPECommand, /* The function to run. */
\r
176 1 /* One parameter is expected. */
\r
179 /* Structure that defines the DEL command line command, which deletes a file. */
\r
180 static const CLI_Command_Definition_t xDEL =
\r
182 "del", /* The command string to type. */
\r
183 "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
184 prvDELCommand, /* The function to run. */
\r
185 1 /* One parameter is expected. */
\r
188 /* Structure that defines the COPY command line command, which deletes a file. */
\r
189 static const CLI_Command_Definition_t xCOPY =
\r
191 "copy", /* The command string to type. */
\r
192 "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
193 prvCOPYCommand, /* The function to run. */
\r
194 2 /* Two parameters are expected. */
\r
197 /*-----------------------------------------------------------*/
\r
199 void vRegisterFileSystemCLICommands( void )
\r
201 /* Register all the command line commands defined immediately above. */
\r
202 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
203 FreeRTOS_CLIRegisterCommand( &xCD );
\r
204 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
205 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
206 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
208 /*-----------------------------------------------------------*/
\r
210 static portBASE_TYPE prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
212 const char *pcParameter;
\r
213 portBASE_TYPE xParameterStringLength, xReturn = pdTRUE;
\r
214 static F_FILE *pxFile = NULL;
\r
217 size_t xColumns = 50U;
\r
219 /* Ensure there is always a null terminator after each character written. */
\r
220 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
222 /* Ensure the buffer leaves space for the \r\n. */
\r
223 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
224 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
226 if( xWriteBufferLen < xColumns )
\r
228 /* Ensure the loop that uses xColumns as an end condition does not
\r
229 write off the end of the buffer. */
\r
230 xColumns = xWriteBufferLen;
\r
233 if( pxFile == NULL )
\r
235 /* The file has not been opened yet. Find the file name. */
\r
236 pcParameter = FreeRTOS_CLIGetParameter
\r
238 pcCommandString, /* The command string itself. */
\r
239 1, /* Return the first parameter. */
\r
240 &xParameterStringLength /* Store the parameter string length. */
\r
243 /* Sanity check something was returned. */
\r
244 configASSERT( pcParameter );
\r
246 /* Attempt to open the requested file. */
\r
247 pxFile = f_open( pcParameter, "r" );
\r
250 if( pxFile != NULL )
\r
252 /* Read the next chunk of data from the file. */
\r
253 for( xByte = 0; xByte < xColumns; xByte++ )
\r
255 iChar = f_getc( pxFile );
\r
259 /* No more characters to return. */
\r
266 pcWriteBuffer[ xByte ] = ( char ) iChar;
\r
271 if( pxFile == NULL )
\r
273 /* Either the file was not opened, or all the data from the file has
\r
274 been returned and the file is now closed. */
\r
278 strcat( pcWriteBuffer, cliNEW_LINE );
\r
282 /*-----------------------------------------------------------*/
\r
284 static portBASE_TYPE prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
286 const char *pcParameter;
\r
287 portBASE_TYPE xParameterStringLength;
\r
288 unsigned char ucReturned;
\r
289 size_t xStringLength;
\r
291 /* Obtain the parameter string. */
\r
292 pcParameter = FreeRTOS_CLIGetParameter
\r
294 pcCommandString, /* The command string itself. */
\r
295 1, /* Return the first parameter. */
\r
296 &xParameterStringLength /* Store the parameter string length. */
\r
299 /* Sanity check something was returned. */
\r
300 configASSERT( pcParameter );
\r
302 /* Attempt to move to the requested directory. */
\r
303 ucReturned = f_chdir( pcParameter );
\r
305 if( ucReturned == F_NO_ERROR )
\r
307 sprintf( pcWriteBuffer, "In: " );
\r
308 xStringLength = strlen( pcWriteBuffer );
\r
309 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
313 sprintf( pcWriteBuffer, "Error" );
\r
316 strcat( pcWriteBuffer, cliNEW_LINE );
\r
320 /*-----------------------------------------------------------*/
\r
322 static portBASE_TYPE prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
324 static F_FIND *pxFindStruct = NULL;
\r
325 unsigned char ucReturned;
\r
326 portBASE_TYPE xReturn = pdFALSE;
\r
328 /* This assumes pcWriteBuffer is long enough. */
\r
329 ( void ) pcCommandString;
\r
331 /* Ensure the buffer leaves space for the \r\n. */
\r
332 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
333 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
335 if( pxFindStruct == NULL )
\r
337 /* This is the first time this function has been executed since the Dir
\r
338 command was run. Create the find structure. */
\r
339 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
341 if( pxFindStruct != NULL )
\r
343 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
345 if( ucReturned == F_NO_ERROR )
\r
347 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
352 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
357 snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
362 /* The find struct has already been created. Find the next file in
\r
364 ucReturned = f_findnext( pxFindStruct );
\r
366 if( ucReturned == F_NO_ERROR )
\r
368 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
373 /* There are no more files. Free the find structure. */
\r
374 vPortFree( pxFindStruct );
\r
375 pxFindStruct = NULL;
\r
377 /* No string to return. */
\r
378 pcWriteBuffer[ 0 ] = 0x00;
\r
382 strcat( pcWriteBuffer, cliNEW_LINE );
\r
386 /*-----------------------------------------------------------*/
\r
388 static portBASE_TYPE prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
390 const char *pcParameter;
\r
391 portBASE_TYPE xParameterStringLength;
\r
392 unsigned char ucReturned;
\r
394 /* This function assumes xWriteBufferLen is large enough! */
\r
395 ( void ) xWriteBufferLen;
\r
397 /* Obtain the parameter string. */
\r
398 pcParameter = FreeRTOS_CLIGetParameter
\r
400 pcCommandString, /* The command string itself. */
\r
401 1, /* Return the first parameter. */
\r
402 &xParameterStringLength /* Store the parameter string length. */
\r
405 /* Sanity check something was returned. */
\r
406 configASSERT( pcParameter );
\r
408 /* Attempt to delete the file. */
\r
409 ucReturned = f_delete( pcParameter );
\r
411 if( ucReturned == F_NO_ERROR )
\r
413 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );
\r
417 sprintf( pcWriteBuffer, "Error" );
\r
420 strcat( pcWriteBuffer, cliNEW_LINE );
\r
424 /*-----------------------------------------------------------*/
\r
426 static portBASE_TYPE prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
428 char *pcSourceFile, *pcDestinationFile;
\r
429 portBASE_TYPE xParameterStringLength;
\r
430 long lSourceLength, lDestinationLength = 0;
\r
432 /* Obtain the name of the destination file. */
\r
433 pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter
\r
435 pcCommandString, /* The command string itself. */
\r
436 2, /* Return the second parameter. */
\r
437 &xParameterStringLength /* Store the parameter string length. */
\r
440 /* Sanity check something was returned. */
\r
441 configASSERT( pcDestinationFile );
\r
443 /* Obtain the name of the source file. */
\r
444 pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
\r
446 pcCommandString, /* The command string itself. */
\r
447 1, /* Return the first parameter. */
\r
448 &xParameterStringLength /* Store the parameter string length. */
\r
451 /* Sanity check something was returned. */
\r
452 configASSERT( pcSourceFile );
\r
454 /* Terminate the string. */
\r
455 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
457 /* See if the source file exists, obtain its length if it does. */
\r
458 lSourceLength = f_filelength( pcSourceFile );
\r
460 if( lSourceLength == 0 )
\r
462 sprintf( pcWriteBuffer, "Source file does not exist" );
\r
466 /* See if the destination file exists. */
\r
467 lDestinationLength = f_filelength( pcDestinationFile );
\r
469 if( lDestinationLength != 0 )
\r
471 sprintf( pcWriteBuffer, "Error: Destination file already exists" );
\r
475 /* Continue only if the source file exists and the destination file does
\r
477 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
479 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
481 sprintf( pcWriteBuffer, "Copy made" );
\r
485 sprintf( pcWriteBuffer, "Error during copy" );
\r
489 strcat( pcWriteBuffer, cliNEW_LINE );
\r
493 /*-----------------------------------------------------------*/
\r
495 static portBASE_TYPE prvPerformCopy( char *pcSourceFile,
\r
496 int32_t lSourceFileLength,
\r
497 char *pcDestinationFile,
\r
498 char *pxWriteBuffer,
\r
499 size_t xWriteBufferLen )
\r
501 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
503 portBASE_TYPE xReturn = pdPASS;
\r
505 /* NOTE: Error handling has been omitted for clarity. */
\r
507 while( lBytesRead < lSourceFileLength )
\r
509 /* How many bytes are left? */
\r
510 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
512 /* How many bytes should be read this time around the loop. Can't
\r
513 read more bytes than will fit into the buffer. */
\r
514 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
516 lBytesToRead = ( long ) xWriteBufferLen;
\r
520 lBytesToRead = lBytesRemaining;
\r
523 /* Open the source file, seek past the data that has already been
\r
524 read from the file, read the next block of data, then close the
\r
525 file again so the destination file can be opened. */
\r
526 pxFile = f_open( pcSourceFile, "r" );
\r
527 if( pxFile != NULL )
\r
529 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
530 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
539 /* Open the destination file and write the block of data to the end of
\r
541 pxFile = f_open( pcDestinationFile, "a" );
\r
542 if( pxFile != NULL )
\r
544 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
553 lBytesRead += lBytesToRead;
\r
558 /*-----------------------------------------------------------*/
\r
560 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )
\r
562 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
563 const char * pcAttrib;
\r
565 /* Point pcAttrib to a string that describes the file. */
\r
566 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
568 pcAttrib = pcDirectory;
\r
570 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
572 pcAttrib = pcReadOnlyFile;
\r
576 pcAttrib = pcWritableFile;
\r
579 /* Create a string that includes the file name, the file size and the
\r
580 attributes string. */
\r
581 sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, pxFindStruct->filesize );
\r