2 FreeRTOS V7.4.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
84 /* FreeRTOS+CLI includes. */
\r
85 #include "FreeRTOS_CLI.h"
\r
87 /* File system includes. */
\r
89 #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
143 * Implements the TEST command.
\r
145 static portBASE_TYPE prvTESTFSCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
\r
147 /* Structure that defines the DIR command line command, which lists all the
\r
148 files in the current directory. */
\r
149 static const CLI_Command_Definition_t xDIR =
\r
151 ( const int8_t * const ) "dir", /* The command string to type. */
\r
152 ( const int8_t * const ) "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
153 prvDIRCommand, /* The function to run. */
\r
154 0 /* No parameters are expected. */
\r
157 /* Structure that defines the CD command line command, which changes the
\r
158 working directory. */
\r
159 static const CLI_Command_Definition_t xCD =
\r
161 ( const int8_t * const ) "cd", /* The command string to type. */
\r
162 ( const int8_t * const ) "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
163 prvCDCommand, /* The function to run. */
\r
164 1 /* One parameter is expected. */
\r
167 /* Structure that defines the TYPE command line command, which prints the
\r
168 contents of a file to the console. */
\r
169 static const CLI_Command_Definition_t xTYPE =
\r
171 ( const int8_t * const ) "type", /* The command string to type. */
\r
172 ( const int8_t * const ) "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
173 prvTYPECommand, /* The function to run. */
\r
174 1 /* One parameter is expected. */
\r
177 /* Structure that defines the DEL command line command, which deletes a file. */
\r
178 static const CLI_Command_Definition_t xDEL =
\r
180 ( const int8_t * const ) "del", /* The command string to type. */
\r
181 ( const int8_t * const ) "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
182 prvDELCommand, /* The function to run. */
\r
183 1 /* One parameter is expected. */
\r
186 /* Structure that defines the COPY command line command, which deletes a file. */
\r
187 static const CLI_Command_Definition_t xCOPY =
\r
189 ( const int8_t * const ) "copy", /* The command string to type. */
\r
190 ( const int8_t * const ) "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
191 prvCOPYCommand, /* The function to run. */
\r
192 2 /* Two parameters are expected. */
\r
195 /* Structure that defines the TEST command line command, which executes some
\r
196 file system driver tests. */
\r
197 static const CLI_Command_Definition_t xTEST_FS =
\r
199 ( const int8_t * const ) "test-fs", /* The command string to type. */
\r
200 ( const int8_t * const ) "\r\ntest-fs:\r\n Executes file system tests. ALL FILES WILL BE DELETED!!!\r\n",
\r
201 prvTESTFSCommand, /* The function to run. */
\r
202 0 /* No parameters are expected. */
\r
205 /*-----------------------------------------------------------*/
\r
207 void vRegisterFileSystemCLICommands( void )
\r
209 /* Register all the command line commands defined immediately above. */
\r
210 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
211 FreeRTOS_CLIRegisterCommand( &xCD );
\r
212 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
213 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
214 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
215 FreeRTOS_CLIRegisterCommand( &xTEST_FS );
\r
217 /*-----------------------------------------------------------*/
\r
219 static portBASE_TYPE prvTYPECommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
221 int8_t *pcParameter;
\r
222 portBASE_TYPE xParameterStringLength, xReturn = pdTRUE;
\r
223 static F_FILE *pxFile = NULL;
\r
226 size_t xColumns = 50U;
\r
228 /* Ensure there is always a null terminator after each character written. */
\r
229 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
231 /* Ensure the buffer leaves space for the \r\n. */
\r
232 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
233 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
235 if( xWriteBufferLen < xColumns )
\r
237 /* Ensure the loop that uses xColumns as an end condition does not
\r
238 write off the end of the buffer. */
\r
239 xColumns = xWriteBufferLen;
\r
242 if( pxFile == NULL )
\r
244 /* The file has not been opened yet. Find the file name. */
\r
245 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
247 pcCommandString, /* The command string itself. */
\r
248 1, /* Return the first parameter. */
\r
249 &xParameterStringLength /* Store the parameter string length. */
\r
252 /* Sanity check something was returned. */
\r
253 configASSERT( pcParameter );
\r
255 /* Attempt to open the requested file. */
\r
256 pxFile = f_open( ( const char * ) pcParameter, "r" );
\r
259 if( pxFile != NULL )
\r
261 /* Read the next chunk of data from the file. */
\r
262 for( xByte = 0; xByte < xColumns; xByte++ )
\r
264 iChar = f_getc( pxFile );
\r
268 /* No more characters to return. */
\r
275 pcWriteBuffer[ xByte ] = ( int8_t ) iChar;
\r
280 if( pxFile == NULL )
\r
282 /* Either the file was not opened, or all the data from the file has
\r
283 been returned and the file is now closed. */
\r
287 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
291 /*-----------------------------------------------------------*/
\r
293 static portBASE_TYPE prvCDCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
295 int8_t *pcParameter;
\r
296 portBASE_TYPE xParameterStringLength;
\r
297 unsigned char ucReturned;
\r
298 size_t xStringLength;
\r
300 /* Obtain the parameter string. */
\r
301 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
303 pcCommandString, /* The command string itself. */
\r
304 1, /* Return the first parameter. */
\r
305 &xParameterStringLength /* Store the parameter string length. */
\r
308 /* Sanity check something was returned. */
\r
309 configASSERT( pcParameter );
\r
311 /* Attempt to move to the requested directory. */
\r
312 ucReturned = f_chdir( ( char * ) pcParameter );
\r
314 if( ucReturned == F_NO_ERROR )
\r
316 sprintf( ( char * ) pcWriteBuffer, "In: " );
\r
317 xStringLength = strlen( ( const char * ) pcWriteBuffer );
\r
318 f_getcwd( ( char * ) &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
322 sprintf( ( char * ) pcWriteBuffer, "Error" );
\r
325 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
329 /*-----------------------------------------------------------*/
\r
331 static portBASE_TYPE prvDIRCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
333 static F_FIND *pxFindStruct = NULL;
\r
334 unsigned char ucReturned;
\r
335 portBASE_TYPE xReturn = pdFALSE;
\r
337 /* This assumes pcWriteBuffer is long enough. */
\r
338 ( void ) pcCommandString;
\r
340 /* Ensure the buffer leaves space for the \r\n. */
\r
341 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
342 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
344 if( pxFindStruct == NULL )
\r
346 /* This is the first time this function has been executed since the Dir
\r
347 command was run. Create the find structure. */
\r
348 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
350 if( pxFindStruct != NULL )
\r
352 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
354 if( ucReturned == F_NO_ERROR )
\r
356 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
361 snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
366 snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
371 /* The find struct has already been created. Find the next file in
\r
373 ucReturned = f_findnext( pxFindStruct );
\r
375 if( ucReturned == F_NO_ERROR )
\r
377 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
382 /* There are no more files. Free the find structure. */
\r
383 vPortFree( pxFindStruct );
\r
384 pxFindStruct = NULL;
\r
386 /* No string to return. */
\r
387 pcWriteBuffer[ 0 ] = 0x00;
\r
391 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
395 /*-----------------------------------------------------------*/
\r
397 static portBASE_TYPE prvDELCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
399 int8_t *pcParameter;
\r
400 portBASE_TYPE xParameterStringLength;
\r
401 unsigned char ucReturned;
\r
403 /* This function assumes xWriteBufferLen is large enough! */
\r
404 ( void ) xWriteBufferLen;
\r
406 /* Obtain the parameter string. */
\r
407 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
409 pcCommandString, /* The command string itself. */
\r
410 1, /* Return the first parameter. */
\r
411 &xParameterStringLength /* Store the parameter string length. */
\r
414 /* Sanity check something was returned. */
\r
415 configASSERT( pcParameter );
\r
417 /* Attempt to delete the file. */
\r
418 ucReturned = f_delete( ( const char * ) pcParameter );
\r
420 if( ucReturned == F_NO_ERROR )
\r
422 sprintf( ( char * ) pcWriteBuffer, "%s was deleted", pcParameter );
\r
426 sprintf( ( char * ) pcWriteBuffer, "Error" );
\r
429 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
433 /*-----------------------------------------------------------*/
\r
435 static portBASE_TYPE prvTESTFSCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
437 unsigned portBASE_TYPE uxOriginalPriority;
\r
439 /* Avoid compiler warnings. */
\r
440 ( void ) xWriteBufferLen;
\r
441 ( void ) pcCommandString;
\r
443 /* Limitations in the interaction with the Windows TCP/IP stack require
\r
444 the command console to run at the idle priority. Raise the priority for
\r
445 the duration of the tests to ensure there are not multiple switches to the
\r
446 idle task as in the simulated environment the idle task hook function may
\r
447 include a (relatively) long delay. */
\r
448 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
449 vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
\r
453 /* Reset back to the original priority. */
\r
454 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
456 sprintf( ( char * ) pcWriteBuffer, "%s", "Test results were sent to Windows console" );
\r
460 /*-----------------------------------------------------------*/
\r
462 static portBASE_TYPE prvCOPYCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
\r
464 int8_t *pcSourceFile, *pcDestinationFile;
\r
465 portBASE_TYPE xParameterStringLength;
\r
466 long lSourceLength, lDestinationLength = 0;
\r
468 /* Obtain the name of the destination file. */
\r
469 pcDestinationFile = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
471 pcCommandString, /* The command string itself. */
\r
472 2, /* Return the second parameter. */
\r
473 &xParameterStringLength /* Store the parameter string length. */
\r
476 /* Sanity check something was returned. */
\r
477 configASSERT( pcDestinationFile );
\r
479 /* Obtain the name of the source file. */
\r
480 pcSourceFile = ( int8_t * ) FreeRTOS_CLIGetParameter
\r
482 pcCommandString, /* The command string itself. */
\r
483 1, /* Return the first parameter. */
\r
484 &xParameterStringLength /* Store the parameter string length. */
\r
487 /* Sanity check something was returned. */
\r
488 configASSERT( pcSourceFile );
\r
490 /* Terminate the string. */
\r
491 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
493 /* See if the source file exists, obtain its length if it does. */
\r
494 lSourceLength = f_filelength( ( const char * ) pcSourceFile );
\r
496 if( lSourceLength == 0 )
\r
498 sprintf( ( char * ) pcWriteBuffer, "Source file does not exist" );
\r
502 /* See if the destination file exists. */
\r
503 lDestinationLength = f_filelength( ( const char * ) pcDestinationFile );
\r
505 if( lDestinationLength != 0 )
\r
507 sprintf( ( char * ) pcWriteBuffer, "Error: Destination file already exists" );
\r
511 /* Continue only if the source file exists and the destination file does
\r
513 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
515 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
517 sprintf( ( char * ) pcWriteBuffer, "Copy made" );
\r
521 sprintf( ( char * ) pcWriteBuffer, "Error during copy" );
\r
525 strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );
\r
529 /*-----------------------------------------------------------*/
\r
531 static portBASE_TYPE prvPerformCopy( int8_t *pcSourceFile,
\r
532 int32_t lSourceFileLength,
\r
533 int8_t *pcDestinationFile,
\r
534 int8_t *pxWriteBuffer,
\r
535 size_t xWriteBufferLen )
\r
537 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
539 portBASE_TYPE xReturn = pdPASS;
\r
541 /* NOTE: Error handling has been omitted for clarity. */
\r
543 while( lBytesRead < lSourceFileLength )
\r
545 /* How many bytes are left? */
\r
546 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
548 /* How many bytes should be read this time around the loop. Can't
\r
549 read more bytes than will fit into the buffer. */
\r
550 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
552 lBytesToRead = ( long ) xWriteBufferLen;
\r
556 lBytesToRead = lBytesRemaining;
\r
559 /* Open the source file, seek past the data that has already been
\r
560 read from the file, read the next block of data, then close the
\r
561 file again so the destination file can be opened. */
\r
562 pxFile = f_open( ( const char * ) pcSourceFile, "r" );
\r
563 if( pxFile != NULL )
\r
565 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
566 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
575 /* Open the destination file and write the block of data to the end of
\r
577 pxFile = f_open( ( const char * ) pcDestinationFile, "a" );
\r
578 if( pxFile != NULL )
\r
580 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
589 lBytesRead += lBytesToRead;
\r
594 /*-----------------------------------------------------------*/
\r
596 static void prvCreateFileInfoString( int8_t *pcBuffer, F_FIND *pxFindStruct )
\r
598 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
599 const char * pcAttrib;
\r
601 /* Point pcAttrib to a string that describes the file. */
\r
602 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
604 pcAttrib = pcDirectory;
\r
606 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
608 pcAttrib = pcReadOnlyFile;
\r
612 pcAttrib = pcWritableFile;
\r
615 /* Create a string that includes the file name, the file size and the
\r
616 attributes string. */
\r
617 sprintf( ( char * ) pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, pxFindStruct->filesize );
\r