2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 /* FreeRTOS includes. */
\r
72 #include "FreeRTOS.h"
\r
75 /* Standard includes. */
\r
81 /* FreeRTOS+CLI includes. */
\r
82 #include "FreeRTOS_CLI.h"
\r
84 /* File system includes. */
\r
86 #include "api_mdriver_ram.h"
\r
89 #define snprintf _snprintf
\r
92 #define cliNEW_LINE "\r\n"
\r
94 /*******************************************************************************
\r
95 * See the URL in the comments within main.c for the location of the online
\r
97 ******************************************************************************/
\r
100 * Print out information on a single file.
\r
102 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );
\r
105 * Copies an existing file into a newly created file.
\r
107 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
108 int32_t lSourceFileLength,
\r
109 const char *pcDestinationFile,
\r
110 char *pxWriteBuffer,
\r
111 size_t xWriteBufferLen );
\r
114 * Implements the DIR command.
\r
116 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
119 * Implements the CD command.
\r
121 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
124 * Implements the DEL command.
\r
126 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
129 * Implements the TYPE command.
\r
131 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
134 * Implements the COPY command.
\r
136 static BaseType_t prvCOPYCommand( 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
187 /*-----------------------------------------------------------*/
\r
189 void vRegisterFileSystemCLICommands( void )
\r
191 /* Register all the command line commands defined immediately above. */
\r
192 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
193 FreeRTOS_CLIRegisterCommand( &xCD );
\r
194 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
195 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
196 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
198 /*-----------------------------------------------------------*/
\r
200 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
202 const char *pcParameter;
\r
203 BaseType_t xParameterStringLength, xReturn = pdTRUE;
\r
204 static F_FILE *pxFile = NULL;
\r
207 size_t xColumns = 50U;
\r
209 /* Ensure there is always a null terminator after each character written. */
\r
210 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
212 /* Ensure the buffer leaves space for the \r\n. */
\r
213 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
214 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
216 if( xWriteBufferLen < xColumns )
\r
218 /* Ensure the loop that uses xColumns as an end condition does not
\r
219 write off the end of the buffer. */
\r
220 xColumns = xWriteBufferLen;
\r
223 if( pxFile == NULL )
\r
225 /* The file has not been opened yet. Find the file name. */
\r
226 pcParameter = FreeRTOS_CLIGetParameter
\r
228 pcCommandString, /* The command string itself. */
\r
229 1, /* Return the first parameter. */
\r
230 &xParameterStringLength /* Store the parameter string length. */
\r
233 /* Sanity check something was returned. */
\r
234 configASSERT( pcParameter );
\r
236 /* Attempt to open the requested file. */
\r
237 pxFile = f_open( pcParameter, "r" );
\r
240 if( pxFile != NULL )
\r
242 /* Read the next chunk of data from the file. */
\r
243 for( xByte = 0; xByte < xColumns; xByte++ )
\r
245 iChar = f_getc( pxFile );
\r
249 /* No more characters to return. */
\r
256 pcWriteBuffer[ xByte ] = ( char ) iChar;
\r
261 if( pxFile == NULL )
\r
263 /* Either the file was not opened, or all the data from the file has
\r
264 been returned and the file is now closed. */
\r
268 strcat( pcWriteBuffer, cliNEW_LINE );
\r
272 /*-----------------------------------------------------------*/
\r
274 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
276 const char *pcParameter;
\r
277 BaseType_t xParameterStringLength;
\r
278 unsigned char ucReturned;
\r
279 size_t xStringLength;
\r
281 /* Obtain the parameter string. */
\r
282 pcParameter = FreeRTOS_CLIGetParameter
\r
284 pcCommandString, /* The command string itself. */
\r
285 1, /* Return the first parameter. */
\r
286 &xParameterStringLength /* Store the parameter string length. */
\r
289 /* Sanity check something was returned. */
\r
290 configASSERT( pcParameter );
\r
292 /* Attempt to move to the requested directory. */
\r
293 ucReturned = f_chdir( pcParameter );
\r
295 if( ucReturned == F_NO_ERROR )
\r
297 sprintf( pcWriteBuffer, "In: " );
\r
298 xStringLength = strlen( pcWriteBuffer );
\r
299 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
303 sprintf( pcWriteBuffer, "Error" );
\r
306 strcat( pcWriteBuffer, cliNEW_LINE );
\r
310 /*-----------------------------------------------------------*/
\r
312 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
314 static F_FIND *pxFindStruct = NULL;
\r
315 unsigned char ucReturned;
\r
316 BaseType_t xReturn = pdFALSE;
\r
318 /* This assumes pcWriteBuffer is long enough. */
\r
319 ( void ) pcCommandString;
\r
321 /* Ensure the buffer leaves space for the \r\n. */
\r
322 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
323 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
325 if( pxFindStruct == NULL )
\r
327 /* This is the first time this function has been executed since the Dir
\r
328 command was run. Create the find structure. */
\r
329 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
331 if( pxFindStruct != NULL )
\r
333 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
335 if( ucReturned == F_NO_ERROR )
\r
337 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
342 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
347 snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
352 /* The find struct has already been created. Find the next file in
\r
354 ucReturned = f_findnext( pxFindStruct );
\r
356 if( ucReturned == F_NO_ERROR )
\r
358 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
363 /* There are no more files. Free the find structure. */
\r
364 vPortFree( pxFindStruct );
\r
365 pxFindStruct = NULL;
\r
367 /* No string to return. */
\r
368 pcWriteBuffer[ 0 ] = 0x00;
\r
372 strcat( pcWriteBuffer, cliNEW_LINE );
\r
376 /*-----------------------------------------------------------*/
\r
378 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
380 const char *pcParameter;
\r
381 BaseType_t xParameterStringLength;
\r
382 unsigned char ucReturned;
\r
384 /* This function assumes xWriteBufferLen is large enough! */
\r
385 ( void ) xWriteBufferLen;
\r
387 /* Obtain the parameter string. */
\r
388 pcParameter = FreeRTOS_CLIGetParameter
\r
390 pcCommandString, /* The command string itself. */
\r
391 1, /* Return the first parameter. */
\r
392 &xParameterStringLength /* Store the parameter string length. */
\r
395 /* Sanity check something was returned. */
\r
396 configASSERT( pcParameter );
\r
398 /* Attempt to delete the file. */
\r
399 ucReturned = f_delete( pcParameter );
\r
401 if( ucReturned == F_NO_ERROR )
\r
403 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );
\r
407 sprintf( pcWriteBuffer, "Error" );
\r
410 strcat( pcWriteBuffer, cliNEW_LINE );
\r
414 /*-----------------------------------------------------------*/
\r
416 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
418 char *pcSourceFile, *pcDestinationFile;
\r
419 BaseType_t xParameterStringLength;
\r
420 long lSourceLength, lDestinationLength = 0;
\r
422 /* Obtain the name of the destination file. */
\r
423 pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter
\r
425 pcCommandString, /* The command string itself. */
\r
426 2, /* Return the second parameter. */
\r
427 &xParameterStringLength /* Store the parameter string length. */
\r
430 /* Sanity check something was returned. */
\r
431 configASSERT( pcDestinationFile );
\r
433 /* Obtain the name of the source file. */
\r
434 pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
\r
436 pcCommandString, /* The command string itself. */
\r
437 1, /* Return the first parameter. */
\r
438 &xParameterStringLength /* Store the parameter string length. */
\r
441 /* Sanity check something was returned. */
\r
442 configASSERT( pcSourceFile );
\r
444 /* Terminate the string. */
\r
445 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
447 /* See if the source file exists, obtain its length if it does. */
\r
448 lSourceLength = f_filelength( pcSourceFile );
\r
450 if( lSourceLength == 0 )
\r
452 sprintf( pcWriteBuffer, "Source file does not exist" );
\r
456 /* See if the destination file exists. */
\r
457 lDestinationLength = f_filelength( pcDestinationFile );
\r
459 if( lDestinationLength != 0 )
\r
461 sprintf( pcWriteBuffer, "Error: Destination file already exists" );
\r
465 /* Continue only if the source file exists and the destination file does
\r
467 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
469 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
471 sprintf( pcWriteBuffer, "Copy made" );
\r
475 sprintf( pcWriteBuffer, "Error during copy" );
\r
479 strcat( pcWriteBuffer, cliNEW_LINE );
\r
483 /*-----------------------------------------------------------*/
\r
485 static BaseType_t prvPerformCopy( const char *pcSourceFile,
\r
486 int32_t lSourceFileLength,
\r
487 const char *pcDestinationFile,
\r
488 char *pxWriteBuffer,
\r
489 size_t xWriteBufferLen )
\r
491 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
493 BaseType_t xReturn = pdPASS;
\r
495 /* NOTE: Error handling has been omitted for clarity. */
\r
497 while( lBytesRead < lSourceFileLength )
\r
499 /* How many bytes are left? */
\r
500 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
502 /* How many bytes should be read this time around the loop. Can't
\r
503 read more bytes than will fit into the buffer. */
\r
504 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
506 lBytesToRead = ( long ) xWriteBufferLen;
\r
510 lBytesToRead = lBytesRemaining;
\r
513 /* Open the source file, seek past the data that has already been
\r
514 read from the file, read the next block of data, then close the
\r
515 file again so the destination file can be opened. */
\r
516 pxFile = f_open( pcSourceFile, "r" );
\r
517 if( pxFile != NULL )
\r
519 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
520 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
529 /* Open the destination file and write the block of data to the end of
\r
531 pxFile = f_open( pcDestinationFile, "a" );
\r
532 if( pxFile != NULL )
\r
534 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
543 lBytesRead += lBytesToRead;
\r
548 /*-----------------------------------------------------------*/
\r
550 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )
\r
552 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
553 const char * pcAttrib;
\r
555 /* Point pcAttrib to a string that describes the file. */
\r
556 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
558 pcAttrib = pcDirectory;
\r
560 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
562 pcAttrib = pcReadOnlyFile;
\r
566 pcAttrib = pcWritableFile;
\r
569 /* Create a string that includes the file name, the file size and the
\r
570 attributes string. */
\r
571 sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, ( int ) pxFindStruct->filesize );
\r