2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
79 /* Standard includes. */
\r
85 /* FreeRTOS+CLI includes. */
\r
86 #include "FreeRTOS_CLI.h"
\r
88 /* File system includes. */
\r
90 #include "api_mdriver_ram.h"
\r
93 #define snprintf _snprintf
\r
96 #define cliNEW_LINE "\r\n"
\r
98 /*******************************************************************************
\r
99 * See the URL in the comments within main.c for the location of the online
\r
101 ******************************************************************************/
\r
104 * Print out information on a single file.
\r
106 static void prvCreateFileInfoString( int8_t *pcBuffer, F_FIND *pxFindStruct );
\r
109 * Copies an existing file into a newly created file.
\r
111 static portBASE_TYPE prvPerformCopy( int8_t *pcSourceFile,
\r
112 int32_t lSourceFileLength,
\r
113 int8_t *pcDestinationFile,
\r
114 int8_t *pxWriteBuffer,
\r
115 size_t xWriteBufferLen );
\r
118 * Implements the DIR command.
\r
120 static portBASE_TYPE prvDIRCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
123 * Implements the CD command.
\r
125 static portBASE_TYPE prvCDCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
128 * Implements the DEL command.
\r
130 static portBASE_TYPE prvDELCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
133 * Implements the TYPE command.
\r
135 static portBASE_TYPE prvTYPECommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
138 * Implements the COPY command.
\r
140 static portBASE_TYPE prvCOPYCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
142 /* Structure that defines the DIR command line command, which lists all the
\r
143 files in the current directory. */
\r
144 static const CLI_Command_Definition_t xDIR =
\r
146 ( const int8_t * const ) "dir", /* The command string to type. */
\r
147 ( const int8_t * const ) "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
148 prvDIRCommand, /* The function to run. */
\r
149 0 /* No parameters are expected. */
\r
152 /* Structure that defines the CD command line command, which changes the
\r
153 working directory. */
\r
154 static const CLI_Command_Definition_t xCD =
\r
156 ( const int8_t * const ) "cd", /* The command string to type. */
\r
157 ( const int8_t * const ) "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
158 prvCDCommand, /* The function to run. */
\r
159 1 /* One parameter is expected. */
\r
162 /* Structure that defines the TYPE command line command, which prints the
\r
163 contents of a file to the console. */
\r
164 static const CLI_Command_Definition_t xTYPE =
\r
166 ( const int8_t * const ) "type", /* The command string to type. */
\r
167 ( const int8_t * const ) "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
168 prvTYPECommand, /* The function to run. */
\r
169 1 /* One parameter is expected. */
\r
172 /* Structure that defines the DEL command line command, which deletes a file. */
\r
173 static const CLI_Command_Definition_t xDEL =
\r
175 ( const int8_t * const ) "del", /* The command string to type. */
\r
176 ( const int8_t * const ) "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
177 prvDELCommand, /* The function to run. */
\r
178 1 /* One parameter is expected. */
\r
181 /* Structure that defines the COPY command line command, which deletes a file. */
\r
182 static const CLI_Command_Definition_t xCOPY =
\r
184 ( const int8_t * const ) "copy", /* The command string to type. */
\r
185 ( const int8_t * const ) "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
186 prvCOPYCommand, /* The function to run. */
\r
187 2 /* Two parameters are expected. */
\r
190 /*-----------------------------------------------------------*/
\r
192 void vRegisterFileSystemCLICommands( void )
\r
194 /* Register all the command line commands defined immediately above. */
\r
195 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
196 FreeRTOS_CLIRegisterCommand( &xCD );
\r
197 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
198 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
199 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
201 /*-----------------------------------------------------------*/
\r
203 static portBASE_TYPE prvTYPECommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
205 int8_t *pcParameter;
\r
206 portBASE_TYPE xParameterStringLength, xReturn = pdTRUE;
\r
207 static F_FILE *pxFile = NULL;
\r
210 size_t xColumns = 50U;
\r
212 /* Ensure there is always a null terminator after each character written. */
\r
213 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
215 /* Ensure the buffer leaves space for the \r\n. */
\r
216 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
217 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
219 if( xWriteBufferLen < xColumns )
\r
221 /* Ensure the loop that uses xColumns as an end condition does not
\r
222 write off the end of the buffer. */
\r
223 xColumns = xWriteBufferLen;
\r
226 if( pxFile == NULL )
\r
228 /* The file has not been opened yet. Find the file name. */
\r
229 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
231 pcCommandString, /* The command string itself. */
\r
232 1, /* Return the first parameter. */
\r
233 &xParameterStringLength /* Store the parameter string length. */
\r
236 /* Sanity check something was returned. */
\r
237 configASSERT( pcParameter );
\r
239 /* Attempt to open the requested file. */
\r
240 pxFile = f_open( ( const char * ) pcParameter, "r" );
\r
243 if( pxFile != NULL )
\r
245 /* Read the next chunk of data from the file. */
\r
246 for( xByte = 0; xByte < xColumns; xByte++ )
\r
248 iChar = f_getc( pxFile );
\r
252 /* No more characters to return. */
\r
259 pcWriteBuffer[ xByte ] = ( int8_t ) iChar;
\r
264 if( pxFile == NULL )
\r
266 /* Either the file was not opened, or all the data from the file has
\r
267 been returned and the file is now closed. */
\r
271 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
275 /*-----------------------------------------------------------*/
\r
277 static portBASE_TYPE prvCDCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
279 int8_t *pcParameter;
\r
280 portBASE_TYPE xParameterStringLength;
\r
281 unsigned char ucReturned;
\r
282 size_t xStringLength;
\r
284 /* Obtain the parameter string. */
\r
285 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
287 pcCommandString, /* The command string itself. */
\r
288 1, /* Return the first parameter. */
\r
289 &xParameterStringLength /* Store the parameter string length. */
\r
292 /* Sanity check something was returned. */
\r
293 configASSERT( pcParameter );
\r
295 /* Attempt to move to the requested directory. */
\r
296 ucReturned = f_chdir( ( char * ) pcParameter );
\r
298 if( ucReturned == F_NO_ERROR )
\r
300 sprintf( ( char * ) pcWriteBuffer, "In: " );
\r
301 xStringLength = strlen( ( const char * ) pcWriteBuffer );
\r
302 f_getcwd( ( char * ) &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
306 sprintf( ( char * ) pcWriteBuffer, "Error" );
\r
309 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
313 /*-----------------------------------------------------------*/
\r
315 static portBASE_TYPE prvDIRCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
317 static F_FIND *pxFindStruct = NULL;
\r
318 unsigned char ucReturned;
\r
319 portBASE_TYPE xReturn = pdFALSE;
\r
321 /* This assumes pcWriteBuffer is long enough. */
\r
322 ( void ) pcCommandString;
\r
324 /* Ensure the buffer leaves space for the \r\n. */
\r
325 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
326 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
328 if( pxFindStruct == NULL )
\r
330 /* This is the first time this function has been executed since the Dir
\r
331 command was run. Create the find structure. */
\r
332 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
334 if( pxFindStruct != NULL )
\r
336 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
338 if( ucReturned == F_NO_ERROR )
\r
340 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
345 snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
350 snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
355 /* The find struct has already been created. Find the next file in
\r
357 ucReturned = f_findnext( pxFindStruct );
\r
359 if( ucReturned == F_NO_ERROR )
\r
361 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
366 /* There are no more files. Free the find structure. */
\r
367 vPortFree( pxFindStruct );
\r
368 pxFindStruct = NULL;
\r
370 /* No string to return. */
\r
371 pcWriteBuffer[ 0 ] = 0x00;
\r
375 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
379 /*-----------------------------------------------------------*/
\r
381 static portBASE_TYPE prvDELCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
383 int8_t *pcParameter;
\r
384 portBASE_TYPE xParameterStringLength;
\r
385 unsigned char ucReturned;
\r
387 /* This function assumes xWriteBufferLen is large enough! */
\r
388 ( void ) xWriteBufferLen;
\r
390 /* Obtain the parameter string. */
\r
391 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
393 pcCommandString, /* The command string itself. */
\r
394 1, /* Return the first parameter. */
\r
395 &xParameterStringLength /* Store the parameter string length. */
\r
398 /* Sanity check something was returned. */
\r
399 configASSERT( pcParameter );
\r
401 /* Attempt to delete the file. */
\r
402 ucReturned = f_delete( ( const char * ) pcParameter );
\r
404 if( ucReturned == F_NO_ERROR )
\r
406 sprintf( ( char * ) pcWriteBuffer, "%s was deleted", pcParameter );
\r
410 sprintf( ( char * ) pcWriteBuffer, "Error" );
\r
413 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
417 /*-----------------------------------------------------------*/
\r
419 static portBASE_TYPE prvCOPYCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
421 int8_t *pcSourceFile, *pcDestinationFile;
\r
422 portBASE_TYPE xParameterStringLength;
\r
423 long lSourceLength, lDestinationLength = 0;
\r
425 /* Obtain the name of the destination file. */
\r
426 pcDestinationFile = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
428 pcCommandString, /* The command string itself. */
\r
429 2, /* Return the second parameter. */
\r
430 &xParameterStringLength /* Store the parameter string length. */
\r
433 /* Sanity check something was returned. */
\r
434 configASSERT( pcDestinationFile );
\r
436 /* Obtain the name of the source file. */
\r
437 pcSourceFile = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
439 pcCommandString, /* The command string itself. */
\r
440 1, /* Return the first parameter. */
\r
441 &xParameterStringLength /* Store the parameter string length. */
\r
444 /* Sanity check something was returned. */
\r
445 configASSERT( pcSourceFile );
\r
447 /* Terminate the string. */
\r
448 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
450 /* See if the source file exists, obtain its length if it does. */
\r
451 lSourceLength = f_filelength( ( const char * ) pcSourceFile );
\r
453 if( lSourceLength == 0 )
\r
455 sprintf( ( char * ) pcWriteBuffer, "Source file does not exist" );
\r
459 /* See if the destination file exists. */
\r
460 lDestinationLength = f_filelength( ( const char * ) pcDestinationFile );
\r
462 if( lDestinationLength != 0 )
\r
464 sprintf( ( char * ) pcWriteBuffer, "Error: Destination file already exists" );
\r
468 /* Continue only if the source file exists and the destination file does
\r
470 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
472 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
474 sprintf( ( char * ) pcWriteBuffer, "Copy made" );
\r
478 sprintf( ( char * ) pcWriteBuffer, "Error during copy" );
\r
482 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
486 /*-----------------------------------------------------------*/
\r
488 static portBASE_TYPE prvPerformCopy( int8_t *pcSourceFile,
\r
489 int32_t lSourceFileLength,
\r
490 int8_t *pcDestinationFile,
\r
491 int8_t *pxWriteBuffer,
\r
492 size_t xWriteBufferLen )
\r
494 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
496 portBASE_TYPE xReturn = pdPASS;
\r
498 /* NOTE: Error handling has been omitted for clarity. */
\r
500 while( lBytesRead < lSourceFileLength )
\r
502 /* How many bytes are left? */
\r
503 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
505 /* How many bytes should be read this time around the loop. Can't
\r
506 read more bytes than will fit into the buffer. */
\r
507 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
509 lBytesToRead = ( long ) xWriteBufferLen;
\r
513 lBytesToRead = lBytesRemaining;
\r
516 /* Open the source file, seek past the data that has already been
\r
517 read from the file, read the next block of data, then close the
\r
518 file again so the destination file can be opened. */
\r
519 pxFile = f_open( ( const char * ) pcSourceFile, "r" );
\r
520 if( pxFile != NULL )
\r
522 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
523 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
532 /* Open the destination file and write the block of data to the end of
\r
534 pxFile = f_open( ( const char * ) pcDestinationFile, "a" );
\r
535 if( pxFile != NULL )
\r
537 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
546 lBytesRead += lBytesToRead;
\r
551 /*-----------------------------------------------------------*/
\r
553 static void prvCreateFileInfoString( int8_t *pcBuffer, F_FIND *pxFindStruct )
\r
555 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
556 const char * pcAttrib;
\r
558 /* Point pcAttrib to a string that describes the file. */
\r
559 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
561 pcAttrib = pcDirectory;
\r
563 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
565 pcAttrib = pcReadOnlyFile;
\r
569 pcAttrib = pcWritableFile;
\r
572 /* Create a string that includes the file name, the file size and the
\r
573 attributes string. */
\r
574 sprintf( ( char * ) pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, ( int ) pxFindStruct->filesize );
\r