2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
96 /* FreeRTOS includes. */
\r
97 #include "FreeRTOS.h"
\r
100 /* Standard includes. */
\r
101 #include <stdint.h>
\r
103 #include <stdlib.h>
\r
104 #include <string.h>
\r
106 /* FreeRTOS+CLI includes. */
\r
107 #include "FreeRTOS_CLI.h"
\r
109 /* File system includes. */
\r
110 #include "fat_sl.h"
\r
111 #include "api_mdriver_ram.h"
\r
114 #define snprintf _snprintf
\r
117 #define cliNEW_LINE "\r\n"
\r
119 /*******************************************************************************
\r
120 * See the URL in the comments within main.c for the location of the online
\r
122 ******************************************************************************/
\r
125 * Print out information on a single file.
\r
127 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );
\r
130 * Copies an existing file into a newly created file.
\r
132 static portBASE_TYPE prvPerformCopy( char *pcSourceFile,
\r
133 int32_t lSourceFileLength,
\r
134 char *pcDestinationFile,
\r
135 char *pxWriteBuffer,
\r
136 size_t xWriteBufferLen );
\r
139 * Implements the DIR command.
\r
141 static portBASE_TYPE prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
144 * Implements the CD command.
\r
146 static portBASE_TYPE prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
149 * Implements the DEL command.
\r
151 static portBASE_TYPE prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
154 * Implements the TYPE command.
\r
156 static portBASE_TYPE prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
159 * Implements the COPY command.
\r
161 static portBASE_TYPE prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );
\r
164 * Registers the CLI commands that are specific to the files system with the
\r
165 * FreeRTOS+CLI command interpreter.
\r
167 void vRegisterFileSystemCLICommands( void );
\r
169 /* Structure that defines the DIR command line command, which lists all the
\r
170 files in the current directory. */
\r
171 static const CLI_Command_Definition_t xDIR =
\r
173 "dir", /* The command string to type. */
\r
174 "\r\ndir:\r\n Lists the files in the current directory\r\n",
\r
175 prvDIRCommand, /* The function to run. */
\r
176 0 /* No parameters are expected. */
\r
179 /* Structure that defines the CD command line command, which changes the
\r
180 working directory. */
\r
181 static const CLI_Command_Definition_t xCD =
\r
183 "cd", /* The command string to type. */
\r
184 "\r\ncd <dir name>:\r\n Changes the working directory\r\n",
\r
185 prvCDCommand, /* The function to run. */
\r
186 1 /* One parameter is expected. */
\r
189 /* Structure that defines the TYPE command line command, which prints the
\r
190 contents of a file to the console. */
\r
191 static const CLI_Command_Definition_t xTYPE =
\r
193 "type", /* The command string to type. */
\r
194 "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",
\r
195 prvTYPECommand, /* The function to run. */
\r
196 1 /* One parameter is expected. */
\r
199 /* Structure that defines the DEL command line command, which deletes a file. */
\r
200 static const CLI_Command_Definition_t xDEL =
\r
202 "del", /* The command string to type. */
\r
203 "\r\ndel <filename>:\r\n deletes a file or directory\r\n",
\r
204 prvDELCommand, /* The function to run. */
\r
205 1 /* One parameter is expected. */
\r
208 /* Structure that defines the COPY command line command, which deletes a file. */
\r
209 static const CLI_Command_Definition_t xCOPY =
\r
211 "copy", /* The command string to type. */
\r
212 "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",
\r
213 prvCOPYCommand, /* The function to run. */
\r
214 2 /* Two parameters are expected. */
\r
217 /*-----------------------------------------------------------*/
\r
219 void vRegisterFileSystemCLICommands( void )
\r
221 /* Register all the command line commands defined immediately above. */
\r
222 FreeRTOS_CLIRegisterCommand( &xDIR );
\r
223 FreeRTOS_CLIRegisterCommand( &xCD );
\r
224 FreeRTOS_CLIRegisterCommand( &xTYPE );
\r
225 FreeRTOS_CLIRegisterCommand( &xDEL );
\r
226 FreeRTOS_CLIRegisterCommand( &xCOPY );
\r
228 /*-----------------------------------------------------------*/
\r
230 static portBASE_TYPE prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
232 const char *pcParameter;
\r
233 portBASE_TYPE xParameterStringLength, xReturn = pdTRUE;
\r
234 static F_FILE *pxFile = NULL;
\r
237 size_t xColumns = 50U;
\r
239 /* Ensure there is always a null terminator after each character written. */
\r
240 memset( pcWriteBuffer, 0x00, xWriteBufferLen );
\r
242 /* Ensure the buffer leaves space for the \r\n. */
\r
243 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
244 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
246 if( xWriteBufferLen < xColumns )
\r
248 /* Ensure the loop that uses xColumns as an end condition does not
\r
249 write off the end of the buffer. */
\r
250 xColumns = xWriteBufferLen;
\r
253 if( pxFile == NULL )
\r
255 /* The file has not been opened yet. Find the file name. */
\r
256 pcParameter = FreeRTOS_CLIGetParameter
\r
258 pcCommandString, /* The command string itself. */
\r
259 1, /* Return the first parameter. */
\r
260 &xParameterStringLength /* Store the parameter string length. */
\r
263 /* Sanity check something was returned. */
\r
264 configASSERT( pcParameter );
\r
266 /* Attempt to open the requested file. */
\r
267 pxFile = f_open( pcParameter, "r" );
\r
270 if( pxFile != NULL )
\r
272 /* Read the next chunk of data from the file. */
\r
273 for( xByte = 0; xByte < xColumns; xByte++ )
\r
275 iChar = f_getc( pxFile );
\r
279 /* No more characters to return. */
\r
286 pcWriteBuffer[ xByte ] = ( char ) iChar;
\r
291 if( pxFile == NULL )
\r
293 /* Either the file was not opened, or all the data from the file has
\r
294 been returned and the file is now closed. */
\r
298 strcat( pcWriteBuffer, cliNEW_LINE );
\r
302 /*-----------------------------------------------------------*/
\r
304 static portBASE_TYPE prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
306 const char *pcParameter;
\r
307 portBASE_TYPE xParameterStringLength;
\r
308 unsigned char ucReturned;
\r
309 size_t xStringLength;
\r
311 /* Obtain the parameter string. */
\r
312 pcParameter = FreeRTOS_CLIGetParameter
\r
314 pcCommandString, /* The command string itself. */
\r
315 1, /* Return the first parameter. */
\r
316 &xParameterStringLength /* Store the parameter string length. */
\r
319 /* Sanity check something was returned. */
\r
320 configASSERT( pcParameter );
\r
322 /* Attempt to move to the requested directory. */
\r
323 ucReturned = f_chdir( pcParameter );
\r
325 if( ucReturned == F_NO_ERROR )
\r
327 sprintf( pcWriteBuffer, "In: " );
\r
328 xStringLength = strlen( pcWriteBuffer );
\r
329 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );
\r
333 sprintf( pcWriteBuffer, "Error" );
\r
336 strcat( pcWriteBuffer, cliNEW_LINE );
\r
340 /*-----------------------------------------------------------*/
\r
342 static portBASE_TYPE prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
344 static F_FIND *pxFindStruct = NULL;
\r
345 unsigned char ucReturned;
\r
346 portBASE_TYPE xReturn = pdFALSE;
\r
348 /* This assumes pcWriteBuffer is long enough. */
\r
349 ( void ) pcCommandString;
\r
351 /* Ensure the buffer leaves space for the \r\n. */
\r
352 configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );
\r
353 xWriteBufferLen -= strlen( cliNEW_LINE );
\r
355 if( pxFindStruct == NULL )
\r
357 /* This is the first time this function has been executed since the Dir
\r
358 command was run. Create the find structure. */
\r
359 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );
\r
361 if( pxFindStruct != NULL )
\r
363 ucReturned = f_findfirst( "*.*", pxFindStruct );
\r
365 if( ucReturned == F_NO_ERROR )
\r
367 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
372 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );
\r
377 snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );
\r
382 /* The find struct has already been created. Find the next file in
\r
384 ucReturned = f_findnext( pxFindStruct );
\r
386 if( ucReturned == F_NO_ERROR )
\r
388 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );
\r
393 /* There are no more files. Free the find structure. */
\r
394 vPortFree( pxFindStruct );
\r
395 pxFindStruct = NULL;
\r
397 /* No string to return. */
\r
398 pcWriteBuffer[ 0 ] = 0x00;
\r
402 strcat( pcWriteBuffer, cliNEW_LINE );
\r
406 /*-----------------------------------------------------------*/
\r
408 static portBASE_TYPE prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
410 const char *pcParameter;
\r
411 portBASE_TYPE xParameterStringLength;
\r
412 unsigned char ucReturned;
\r
414 /* This function assumes xWriteBufferLen is large enough! */
\r
415 ( void ) xWriteBufferLen;
\r
417 /* Obtain the parameter string. */
\r
418 pcParameter = FreeRTOS_CLIGetParameter
\r
420 pcCommandString, /* The command string itself. */
\r
421 1, /* Return the first parameter. */
\r
422 &xParameterStringLength /* Store the parameter string length. */
\r
425 /* Sanity check something was returned. */
\r
426 configASSERT( pcParameter );
\r
428 /* Attempt to delete the file. */
\r
429 ucReturned = f_delete( pcParameter );
\r
431 if( ucReturned == F_NO_ERROR )
\r
433 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );
\r
437 sprintf( pcWriteBuffer, "Error" );
\r
440 strcat( pcWriteBuffer, cliNEW_LINE );
\r
444 /*-----------------------------------------------------------*/
\r
446 static portBASE_TYPE prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )
\r
448 char *pcSourceFile, *pcDestinationFile;
\r
449 portBASE_TYPE xParameterStringLength;
\r
450 long lSourceLength, lDestinationLength = 0;
\r
452 /* Obtain the name of the destination file. */
\r
453 pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter
\r
455 pcCommandString, /* The command string itself. */
\r
456 2, /* Return the second parameter. */
\r
457 &xParameterStringLength /* Store the parameter string length. */
\r
460 /* Sanity check something was returned. */
\r
461 configASSERT( pcDestinationFile );
\r
463 /* Obtain the name of the source file. */
\r
464 pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter
\r
466 pcCommandString, /* The command string itself. */
\r
467 1, /* Return the first parameter. */
\r
468 &xParameterStringLength /* Store the parameter string length. */
\r
471 /* Sanity check something was returned. */
\r
472 configASSERT( pcSourceFile );
\r
474 /* Terminate the string. */
\r
475 pcSourceFile[ xParameterStringLength ] = 0x00;
\r
477 /* See if the source file exists, obtain its length if it does. */
\r
478 lSourceLength = f_filelength( pcSourceFile );
\r
480 if( lSourceLength == 0 )
\r
482 sprintf( pcWriteBuffer, "Source file does not exist" );
\r
486 /* See if the destination file exists. */
\r
487 lDestinationLength = f_filelength( pcDestinationFile );
\r
489 if( lDestinationLength != 0 )
\r
491 sprintf( pcWriteBuffer, "Error: Destination file already exists" );
\r
495 /* Continue only if the source file exists and the destination file does
\r
497 if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )
\r
499 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )
\r
501 sprintf( pcWriteBuffer, "Copy made" );
\r
505 sprintf( pcWriteBuffer, "Error during copy" );
\r
509 strcat( pcWriteBuffer, cliNEW_LINE );
\r
513 /*-----------------------------------------------------------*/
\r
515 static portBASE_TYPE prvPerformCopy( char *pcSourceFile,
\r
516 int32_t lSourceFileLength,
\r
517 char *pcDestinationFile,
\r
518 char *pxWriteBuffer,
\r
519 size_t xWriteBufferLen )
\r
521 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;
\r
523 portBASE_TYPE xReturn = pdPASS;
\r
525 /* NOTE: Error handling has been omitted for clarity. */
\r
527 while( lBytesRead < lSourceFileLength )
\r
529 /* How many bytes are left? */
\r
530 lBytesRemaining = lSourceFileLength - lBytesRead;
\r
532 /* How many bytes should be read this time around the loop. Can't
\r
533 read more bytes than will fit into the buffer. */
\r
534 if( lBytesRemaining > ( long ) xWriteBufferLen )
\r
536 lBytesToRead = ( long ) xWriteBufferLen;
\r
540 lBytesToRead = lBytesRemaining;
\r
543 /* Open the source file, seek past the data that has already been
\r
544 read from the file, read the next block of data, then close the
\r
545 file again so the destination file can be opened. */
\r
546 pxFile = f_open( pcSourceFile, "r" );
\r
547 if( pxFile != NULL )
\r
549 f_seek( pxFile, lBytesRead, F_SEEK_SET );
\r
550 f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
559 /* Open the destination file and write the block of data to the end of
\r
561 pxFile = f_open( pcDestinationFile, "a" );
\r
562 if( pxFile != NULL )
\r
564 f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );
\r
573 lBytesRead += lBytesToRead;
\r
578 /*-----------------------------------------------------------*/
\r
580 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )
\r
582 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";
\r
583 const char * pcAttrib;
\r
585 /* Point pcAttrib to a string that describes the file. */
\r
586 if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )
\r
588 pcAttrib = pcDirectory;
\r
590 else if( pxFindStruct->attr & F_ATTR_READONLY )
\r
592 pcAttrib = pcReadOnlyFile;
\r
596 pcAttrib = pcWritableFile;
\r
599 /* Create a string that includes the file name, the file size and the
\r
600 attributes string. */
\r
601 sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, pxFindStruct->filesize );
\r