]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/File-Related-CLI-commands.c
Update version numbers in FreeRTOS-Plus demo files.
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / File-Related-CLI-commands.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /* FreeRTOS includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 /* Standard includes. */\r
72 #include <stdint.h>\r
73 #include <stdio.h>\r
74 #include <stdlib.h>\r
75 #include <string.h>\r
76 \r
77 /* FreeRTOS+CLI includes. */\r
78 #include "FreeRTOS_CLI.h"\r
79 \r
80 /* File system includes. */\r
81 #include "fat_sl.h"\r
82 #include "api_mdriver_ram.h"\r
83 \r
84 #ifdef _WINDOWS_\r
85         #define snprintf _snprintf\r
86 #endif\r
87 \r
88 #define cliNEW_LINE             "\r\n"\r
89 \r
90 /*******************************************************************************\r
91  * See the URL in the comments within main.c for the location of the online\r
92  * documentation.\r
93  ******************************************************************************/\r
94 \r
95 /*\r
96  * Print out information on a single file.\r
97  */\r
98 static void prvCreateFileInfoString( int8_t *pcBuffer, F_FIND *pxFindStruct );\r
99 \r
100 /*\r
101  * Copies an existing file into a newly created file.\r
102  */\r
103 static portBASE_TYPE prvPerformCopy( int8_t *pcSourceFile,\r
104                                                         int32_t lSourceFileLength,\r
105                                                         int8_t *pcDestinationFile,\r
106                                                         int8_t *pxWriteBuffer,\r
107                                                         size_t xWriteBufferLen );\r
108 \r
109 /*\r
110  * Implements the DIR command.\r
111  */\r
112 static portBASE_TYPE prvDIRCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
113 \r
114 /*\r
115  * Implements the CD command.\r
116  */\r
117 static portBASE_TYPE prvCDCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
118 \r
119 /*\r
120  * Implements the DEL command.\r
121  */\r
122 static portBASE_TYPE prvDELCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
123 \r
124 /*\r
125  * Implements the TYPE command.\r
126  */\r
127 static portBASE_TYPE prvTYPECommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
128 \r
129 /*\r
130  * Implements the COPY command.\r
131  */\r
132 static portBASE_TYPE prvCOPYCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );\r
133 \r
134 /* Structure that defines the DIR command line command, which lists all the\r
135 files in the current directory. */\r
136 static const CLI_Command_Definition_t xDIR =\r
137 {\r
138         ( const int8_t * const ) "dir", /* The command string to type. */\r
139         ( const int8_t * const ) "\r\ndir:\r\n Lists the files in the current directory\r\n",\r
140         prvDIRCommand, /* The function to run. */\r
141         0 /* No parameters are expected. */\r
142 };\r
143 \r
144 /* Structure that defines the CD command line command, which changes the\r
145 working directory. */\r
146 static const CLI_Command_Definition_t xCD =\r
147 {\r
148         ( const int8_t * const ) "cd", /* The command string to type. */\r
149         ( const int8_t * const ) "\r\ncd <dir name>:\r\n Changes the working directory\r\n",\r
150         prvCDCommand, /* The function to run. */\r
151         1 /* One parameter is expected. */\r
152 };\r
153 \r
154 /* Structure that defines the TYPE command line command, which prints the\r
155 contents of a file to the console. */\r
156 static const CLI_Command_Definition_t xTYPE =\r
157 {\r
158         ( const int8_t * const ) "type", /* The command string to type. */\r
159         ( const int8_t * const ) "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",\r
160         prvTYPECommand, /* The function to run. */\r
161         1 /* One parameter is expected. */\r
162 };\r
163 \r
164 /* Structure that defines the DEL command line command, which deletes a file. */\r
165 static const CLI_Command_Definition_t xDEL =\r
166 {\r
167         ( const int8_t * const ) "del", /* The command string to type. */\r
168         ( const int8_t * const ) "\r\ndel <filename>:\r\n deletes a file or directory\r\n",\r
169         prvDELCommand, /* The function to run. */\r
170         1 /* One parameter is expected. */\r
171 };\r
172 \r
173 /* Structure that defines the COPY command line command, which deletes a file. */\r
174 static const CLI_Command_Definition_t xCOPY =\r
175 {\r
176         ( const int8_t * const ) "copy", /* The command string to type. */\r
177         ( const int8_t * const ) "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",\r
178         prvCOPYCommand, /* The function to run. */\r
179         2 /* Two parameters are expected. */\r
180 };\r
181 \r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vRegisterFileSystemCLICommands( void )\r
186 {\r
187         /* Register all the command line commands defined immediately above. */\r
188         FreeRTOS_CLIRegisterCommand( &xDIR );\r
189         FreeRTOS_CLIRegisterCommand( &xCD );\r
190         FreeRTOS_CLIRegisterCommand( &xTYPE );\r
191         FreeRTOS_CLIRegisterCommand( &xDEL );\r
192         FreeRTOS_CLIRegisterCommand( &xCOPY );\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static portBASE_TYPE prvTYPECommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
197 {\r
198 int8_t *pcParameter;\r
199 portBASE_TYPE xParameterStringLength, xReturn = pdTRUE;\r
200 static F_FILE *pxFile = NULL;\r
201 int iChar;\r
202 size_t xByte;\r
203 size_t xColumns = 50U;\r
204 \r
205         /* Ensure there is always a null terminator after each character written. */\r
206         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
207 \r
208         /* Ensure the buffer leaves space for the \r\n. */\r
209         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
210         xWriteBufferLen -= strlen( cliNEW_LINE );\r
211 \r
212         if( xWriteBufferLen < xColumns )\r
213         {\r
214                 /* Ensure the loop that uses xColumns as an end condition does not\r
215                 write off the end of the buffer. */\r
216                 xColumns = xWriteBufferLen;\r
217         }\r
218 \r
219         if( pxFile == NULL )\r
220         {\r
221                 /* The file has not been opened yet.  Find the file name. */\r
222                 pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
223                                                                         (\r
224                                                                                 pcCommandString,                /* The command string itself. */\r
225                                                                                 1,                                              /* Return the first parameter. */\r
226                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
227                                                                         );\r
228 \r
229                 /* Sanity check something was returned. */\r
230                 configASSERT( pcParameter );\r
231 \r
232                 /* Attempt to open the requested file. */\r
233                 pxFile = f_open( ( const char * ) pcParameter, "r" );\r
234         }\r
235 \r
236         if( pxFile != NULL )\r
237         {\r
238                 /* Read the next chunk of data from the file. */\r
239                 for( xByte = 0; xByte < xColumns; xByte++ )\r
240                 {\r
241                         iChar = f_getc( pxFile );\r
242 \r
243                         if( iChar == -1 )\r
244                         {\r
245                                 /* No more characters to return. */\r
246                                 f_close( pxFile );\r
247                                 pxFile = NULL;\r
248                                 break;\r
249                         }\r
250                         else\r
251                         {\r
252                                 pcWriteBuffer[ xByte ] = ( int8_t ) iChar;\r
253                         }\r
254                 }\r
255         }\r
256 \r
257         if( pxFile == NULL )\r
258         {\r
259                 /* Either the file was not opened, or all the data from the file has\r
260                 been returned and the file is now closed. */\r
261                 xReturn = pdFALSE;\r
262         }\r
263 \r
264         strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );\r
265 \r
266         return xReturn;\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static portBASE_TYPE prvCDCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
271 {\r
272 int8_t *pcParameter;\r
273 portBASE_TYPE xParameterStringLength;\r
274 unsigned char ucReturned;\r
275 size_t xStringLength;\r
276 \r
277         /* Obtain the parameter string. */\r
278         pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
279                                                                 (\r
280                                                                         pcCommandString,                /* The command string itself. */\r
281                                                                         1,                                              /* Return the first parameter. */\r
282                                                                         &xParameterStringLength /* Store the parameter string length. */\r
283                                                                 );\r
284 \r
285         /* Sanity check something was returned. */\r
286         configASSERT( pcParameter );\r
287 \r
288         /* Attempt to move to the requested directory. */\r
289         ucReturned = f_chdir( ( char * ) pcParameter );\r
290 \r
291         if( ucReturned == F_NO_ERROR )\r
292         {\r
293                 sprintf( ( char * ) pcWriteBuffer, "In: " );\r
294                 xStringLength = strlen( ( const char * ) pcWriteBuffer );\r
295                 f_getcwd( ( char * ) &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );\r
296         }\r
297         else\r
298         {\r
299                 sprintf( ( char * ) pcWriteBuffer, "Error" );\r
300         }\r
301 \r
302         strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );\r
303 \r
304         return pdFALSE;\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 static portBASE_TYPE prvDIRCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
309 {\r
310 static F_FIND *pxFindStruct = NULL;\r
311 unsigned char ucReturned;\r
312 portBASE_TYPE xReturn = pdFALSE;\r
313 \r
314         /* This assumes pcWriteBuffer is long enough. */\r
315         ( void ) pcCommandString;\r
316 \r
317         /* Ensure the buffer leaves space for the \r\n. */\r
318         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
319         xWriteBufferLen -= strlen( cliNEW_LINE );\r
320 \r
321         if( pxFindStruct == NULL )\r
322         {\r
323                 /* This is the first time this function has been executed since the Dir\r
324                 command was run.  Create the find structure. */\r
325                 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );\r
326 \r
327                 if( pxFindStruct != NULL )\r
328                 {\r
329                         ucReturned = f_findfirst( "*.*", pxFindStruct );\r
330 \r
331                         if( ucReturned == F_NO_ERROR )\r
332                         {\r
333                                 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
334                                 xReturn = pdPASS;\r
335                         }\r
336                         else\r
337                         {\r
338                                 snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );\r
339                         }\r
340                 }\r
341                 else\r
342                 {\r
343                         snprintf( ( char * ) pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );\r
344                 }\r
345         }\r
346         else\r
347         {\r
348                 /* The find struct has already been created.  Find the next file in\r
349                 the directory. */\r
350                 ucReturned = f_findnext( pxFindStruct );\r
351 \r
352                 if( ucReturned == F_NO_ERROR )\r
353                 {\r
354                         prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
355                         xReturn = pdPASS;\r
356                 }\r
357                 else\r
358                 {\r
359                         /* There are no more files.  Free the find structure. */\r
360                         vPortFree( pxFindStruct );\r
361                         pxFindStruct = NULL;\r
362 \r
363                         /* No string to return. */\r
364                         pcWriteBuffer[ 0 ] = 0x00;\r
365                 }\r
366         }\r
367 \r
368         strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );\r
369 \r
370         return xReturn;\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 static portBASE_TYPE prvDELCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
375 {\r
376 int8_t *pcParameter;\r
377 portBASE_TYPE xParameterStringLength;\r
378 unsigned char ucReturned;\r
379 \r
380         /* This function assumes xWriteBufferLen is large enough! */\r
381         ( void ) xWriteBufferLen;\r
382 \r
383         /* Obtain the parameter string. */\r
384         pcParameter = ( int8_t * ) FreeRTOS_CLIGetParameter\r
385                                                                 (\r
386                                                                         pcCommandString,                /* The command string itself. */\r
387                                                                         1,                                              /* Return the first parameter. */\r
388                                                                         &xParameterStringLength /* Store the parameter string length. */\r
389                                                                 );\r
390 \r
391         /* Sanity check something was returned. */\r
392         configASSERT( pcParameter );\r
393 \r
394         /* Attempt to delete the file. */\r
395         ucReturned = f_delete( ( const char * ) pcParameter );\r
396 \r
397         if( ucReturned == F_NO_ERROR )\r
398         {\r
399                 sprintf( ( char * ) pcWriteBuffer, "%s was deleted", pcParameter );\r
400         }\r
401         else\r
402         {\r
403                 sprintf( ( char * ) pcWriteBuffer, "Error" );\r
404         }\r
405 \r
406         strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );\r
407 \r
408         return pdFALSE;\r
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r
412 static portBASE_TYPE prvCOPYCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )\r
413 {\r
414 int8_t *pcSourceFile, *pcDestinationFile;\r
415 portBASE_TYPE xParameterStringLength;\r
416 long lSourceLength, lDestinationLength = 0;\r
417 \r
418         /* Obtain the name of the destination file. */\r
419         pcDestinationFile = ( int8_t * ) FreeRTOS_CLIGetParameter\r
420                                                                 (\r
421                                                                         pcCommandString,                /* The command string itself. */\r
422                                                                         2,                                              /* Return the second parameter. */\r
423                                                                         &xParameterStringLength /* Store the parameter string length. */\r
424                                                                 );\r
425 \r
426         /* Sanity check something was returned. */\r
427         configASSERT( pcDestinationFile );\r
428 \r
429         /* Obtain the name of the source file. */\r
430         pcSourceFile = ( int8_t * ) FreeRTOS_CLIGetParameter\r
431                                                                 (\r
432                                                                         pcCommandString,                /* The command string itself. */\r
433                                                                         1,                                              /* Return the first parameter. */\r
434                                                                         &xParameterStringLength /* Store the parameter string length. */\r
435                                                                 );\r
436 \r
437         /* Sanity check something was returned. */\r
438         configASSERT( pcSourceFile );\r
439 \r
440         /* Terminate the string. */\r
441         pcSourceFile[ xParameterStringLength ] = 0x00;\r
442 \r
443         /* See if the source file exists, obtain its length if it does. */\r
444         lSourceLength = f_filelength( ( const char * ) pcSourceFile );\r
445 \r
446         if( lSourceLength == 0 )\r
447         {\r
448                 sprintf( ( char * ) pcWriteBuffer, "Source file does not exist" );\r
449         }\r
450         else\r
451         {\r
452                 /* See if the destination file exists. */\r
453                 lDestinationLength = f_filelength( ( const char * ) pcDestinationFile );\r
454 \r
455                 if( lDestinationLength != 0 )\r
456                 {\r
457                         sprintf( ( char * ) pcWriteBuffer, "Error: Destination file already exists" );\r
458                 }\r
459         }\r
460 \r
461         /* Continue only if the source file exists and the destination file does\r
462         not exist. */\r
463         if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )\r
464         {\r
465                 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )\r
466                 {\r
467                         sprintf( ( char * ) pcWriteBuffer, "Copy made" );\r
468                 }\r
469                 else\r
470                 {\r
471                         sprintf( ( char * ) pcWriteBuffer, "Error during copy" );\r
472                 }\r
473         }\r
474 \r
475         strcat( ( char * ) pcWriteBuffer, cliNEW_LINE );\r
476 \r
477         return pdFALSE;\r
478 }\r
479 /*-----------------------------------------------------------*/\r
480 \r
481 static portBASE_TYPE prvPerformCopy( int8_t *pcSourceFile,\r
482                                                         int32_t lSourceFileLength,\r
483                                                         int8_t *pcDestinationFile,\r
484                                                         int8_t *pxWriteBuffer,\r
485                                                         size_t xWriteBufferLen )\r
486 {\r
487 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;\r
488 F_FILE *pxFile;\r
489 portBASE_TYPE xReturn = pdPASS;\r
490 \r
491         /* NOTE:  Error handling has been omitted for clarity. */\r
492 \r
493         while( lBytesRead < lSourceFileLength )\r
494         {\r
495                 /* How many bytes are left? */\r
496                 lBytesRemaining = lSourceFileLength - lBytesRead;\r
497 \r
498                 /* How many bytes should be read this time around the loop.  Can't\r
499                 read more bytes than will fit into the buffer. */\r
500                 if( lBytesRemaining > ( long ) xWriteBufferLen )\r
501                 {\r
502                         lBytesToRead = ( long ) xWriteBufferLen;\r
503                 }\r
504                 else\r
505                 {\r
506                         lBytesToRead = lBytesRemaining;\r
507                 }\r
508 \r
509                 /* Open the source file, seek past the data that has already been\r
510                 read from the file, read the next block of data, then close the\r
511                 file again so the destination file can be opened. */\r
512                 pxFile = f_open( ( const char * ) pcSourceFile, "r" );\r
513                 if( pxFile != NULL )\r
514                 {\r
515                         f_seek( pxFile, lBytesRead, F_SEEK_SET );\r
516                         f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
517                         f_close( pxFile );\r
518                 }\r
519                 else\r
520                 {\r
521                         xReturn = pdFAIL;\r
522                         break;\r
523                 }\r
524 \r
525                 /* Open the destination file and write the block of data to the end of\r
526                 the file. */\r
527                 pxFile = f_open( ( const char * ) pcDestinationFile, "a" );\r
528                 if( pxFile != NULL )\r
529                 {\r
530                         f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
531                         f_close( pxFile );\r
532                 }\r
533                 else\r
534                 {\r
535                         xReturn = pdFAIL;\r
536                         break;\r
537                 }\r
538 \r
539                 lBytesRead += lBytesToRead;\r
540         }\r
541 \r
542         return xReturn;\r
543 }\r
544 /*-----------------------------------------------------------*/\r
545 \r
546 static void prvCreateFileInfoString( int8_t *pcBuffer, F_FIND *pxFindStruct )\r
547 {\r
548 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";\r
549 const char * pcAttrib;\r
550 \r
551         /* Point pcAttrib to a string that describes the file. */\r
552         if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )\r
553         {\r
554                 pcAttrib = pcDirectory;\r
555         }\r
556         else if( pxFindStruct->attr & F_ATTR_READONLY )\r
557         {\r
558                 pcAttrib = pcReadOnlyFile;\r
559         }\r
560         else\r
561         {\r
562                 pcAttrib = pcWritableFile;\r
563         }\r
564 \r
565         /* Create a string that includes the file name, the file size and the\r
566         attributes string. */\r
567         sprintf( ( char * ) pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, ( int ) pxFindStruct->filesize );\r
568 }\r