2 FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* FreeRTOS includes. */
\r
67 #include "FreeRTOS.h"
\r
70 /* Standard includes. */
\r
75 /* FreeRTOS+CLI includes. */
\r
76 #include "FreeRTOS_CLI.h"
\r
78 /* File system includes. */
\r
80 #include "api_mdriver_ram.h"
\r
84 #define snprintf _snprintf
\r
87 #define cliNEW_LINE "\r\n"
\r
89 /*******************************************************************************
\r
90 * See the URL in the comments within main.c for the location of the online
\r
92 ******************************************************************************/
\r
95 * Print out information on a single file.
\r
97 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );
\r
100 * Copies an existing file into a newly created file.
\r
102 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
103 int32_t lSourceFileLength,
\r
104 const char *pcDestinationFile,
\r
105 char *pxWriteBuffer,
\r
106 size_t xWriteBufferLen );
\r
109 * Implements the DIR command.
\r
111 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
114 * Implements the CD command.
\r
116 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
119 * Implements the DEL command.
\r
121 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
124 * Implements the TYPE command.
\r
126 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
129 * Implements the COPY command.
\r
131 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
134 * Implements the TEST command.
\r
136 static BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
138 /* Structure that defines the DIR command line command, which lists all the
\r
139 files in the current directory. */
\r
140 static const CLI_Command_Definition_t xDIR =
\r
142 "dir", /* The command string to type. */
\r
143 "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
144 prvDIRCommand, /* The function to run. */
\r
145 0 /* No parameters are expected. */
\r
148 /* Structure that defines the CD command line command, which changes the
\r
149 working directory. */
\r
150 static const CLI_Command_Definition_t xCD =
\r
152 "cd", /* The command string to type. */
\r
153 "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
154 prvCDCommand, /* The function to run. */
\r
155 1 /* One parameter is expected. */
\r
158 /* Structure that defines the TYPE command line command, which prints the
\r
159 contents of a file to the console. */
\r
160 static const CLI_Command_Definition_t xTYPE =
\r
162 "type", /* The command string to type. */
\r
163 "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
164 prvTYPECommand, /* The function to run. */
\r
165 1 /* One parameter is expected. */
\r
168 /* Structure that defines the DEL command line command, which deletes a file. */
\r
169 static const CLI_Command_Definition_t xDEL =
\r
171 "del", /* The command string to type. */
\r
172 "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
173 prvDELCommand, /* The function to run. */
\r
174 1 /* One parameter is expected. */
\r
177 /* Structure that defines the COPY command line command, which deletes a file. */
\r
178 static const CLI_Command_Definition_t xCOPY =
\r
180 "copy", /* The command string to type. */
\r
181 "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
182 prvCOPYCommand, /* The function to run. */
\r
183 2 /* Two parameters are expected. */
\r
186 /* Structure that defines the TEST command line command, which executes some
\r
187 file system driver tests. */
\r
188 static const CLI_Command_Definition_t xTEST_FS =
\r
190 "test-fs", /* The command string to type. */
\r
191 "\r\ntest-fs:\r\n Executes file system tests. ALL FILES WILL BE DELETED!!!\r\n",
\r
192 prvTESTFSCommand, /* The function to run. */
\r
193 0 /* No parameters are expected. */
\r
196 /*-----------------------------------------------------------*/
\r
198 void vRegisterFileSystemCLICommands( void )
\r
200 /* Register all the command line commands defined immediately above. */
\r
201 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
202 FreeRTOS_CLIRegisterCommand( &xCD );
\r
203 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
204 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
205 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
206 FreeRTOS_CLIRegisterCommand( &xTEST_FS );
\r
208 /*-----------------------------------------------------------*/
\r
210 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
212 const char *pcParameter;
\r
213 BaseType_t 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 BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
286 const char *pcParameter;
\r
287 BaseType_t 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 BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
324 static F_FIND *pxFindStruct = NULL;
\r
325 unsigned char ucReturned;
\r
326 BaseType_t 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 BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
390 const char *pcParameter;
\r
391 BaseType_t 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 BaseType_t prvTESTFSCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
428 UBaseType_t uxOriginalPriority;
\r
430 /* Avoid compiler warnings. */
\r
431 ( void ) xWriteBufferLen;
\r
432 ( void ) pcCommandString;
\r
434 /* Limitations in the interaction with the Windows TCP/IP stack require
\r
435 the command console to run at the idle priority. Raise the priority for
\r
436 the duration of the tests to ensure there are not multiple switches to the
\r
437 idle task as in the simulated environment the idle task hook function may
\r
438 include a (relatively) long delay. */
\r
439 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
440 vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
\r
444 /* Reset back to the original priority. */
\r
445 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
447 sprintf( pcWriteBuffer, "%s", "Test results were sent to Windows console" );
\r
451 /*-----------------------------------------------------------*/
\r
453 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
455 char *pcSourceFile;
\r
456 const char *pcDestinationFile;
\r
457 BaseType_t xParameterStringLength;
\r
458 long lSourceLength, lDestinationLength = 0;
\r
460 /* Obtain the name of the destination file. */
\r
461 pcDestinationFile = FreeRTOS_CLIGetParameter
\r
463 pcCommandString, /* The command string itself. */
\r
464 2, /* Return the second parameter. */
\r
465 &xParameterStringLength /* Store the parameter string length. */
\r
468 /* Sanity check something was returned. */
\r
469 configASSERT( pcDestinationFile );
\r
471 /* Obtain the name of the source file. */
\r
472 pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
\r
474 pcCommandString, /* The command string itself. */
\r
475 1, /* Return the first parameter. */
\r
476 &xParameterStringLength /* Store the parameter string length. */
\r
479 /* Sanity check something was returned. */
\r
480 configASSERT( pcSourceFile );
\r
482 /* Terminate the string. */
\r
483 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
485 /* See if the source file exists, obtain its length if it does. */
\r
486 lSourceLength = f_filelength( pcSourceFile );
\r
488 if( lSourceLength == 0 )
\r
490 sprintf( pcWriteBuffer, "Source file does not exist" );
\r
494 /* See if the destination file exists. */
\r
495 lDestinationLength = f_filelength( pcDestinationFile );
\r
497 if( lDestinationLength != 0 )
\r
499 sprintf( pcWriteBuffer, "Error: Destination file already exists" );
\r
503 /* Continue only if the source file exists and the destination file does
\r
505 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
507 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
509 sprintf( pcWriteBuffer, "Copy made" );
\r
513 sprintf( pcWriteBuffer, "Error during copy" );
\r
517 strcat( pcWriteBuffer, cliNEW_LINE );
\r
521 /*-----------------------------------------------------------*/
\r
523 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
524 int32_t lSourceFileLength,
\r
525 const char *pcDestinationFile,
\r
526 char *pxWriteBuffer,
\r
527 size_t xWriteBufferLen )
\r
529 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
531 BaseType_t xReturn = pdPASS;
\r
533 /* NOTE: Error handling has been omitted for clarity. */
\r
535 while( lBytesRead < lSourceFileLength )
\r
537 /* How many bytes are left? */
\r
538 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
540 /* How many bytes should be read this time around the loop. Can't
\r
541 read more bytes than will fit into the buffer. */
\r
542 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
544 lBytesToRead = ( long ) xWriteBufferLen;
\r
548 lBytesToRead = lBytesRemaining;
\r
551 /* Open the source file, seek past the data that has already been
\r
552 read from the file, read the next block of data, then close the
\r
553 file again so the destination file can be opened. */
\r
554 pxFile = f_open( pcSourceFile, "r" );
\r
555 if( pxFile != NULL )
\r
557 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
558 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
567 /* Open the destination file and write the block of data to the end of
\r
569 pxFile = f_open( pcDestinationFile, "a" );
\r
570 if( pxFile != NULL )
\r
572 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
581 lBytesRead += lBytesToRead;
\r
586 /*-----------------------------------------------------------*/
\r
588 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )
\r
590 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
591 const char * pcAttrib;
\r
593 /* Point pcAttrib to a string that describes the file. */
\r
594 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
596 pcAttrib = pcDirectory;
\r
598 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
600 pcAttrib = pcReadOnlyFile;
\r
604 pcAttrib = pcWritableFile;
\r
607 /* Create a string that includes the file name, the file size and the
\r
608 attributes string. */
\r
609 sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, pxFindStruct->filesize );
\r