]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/File-Related-CLI-commands.c
Update FreeRTOS+ version number ready for version 9 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / File-Related-CLI-commands.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 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     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* FreeRTOS includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 /* Standard includes. */\r
76 #include <stdint.h>\r
77 #include <stdio.h>\r
78 #include <stdlib.h>\r
79 #include <string.h>\r
80 \r
81 /* FreeRTOS+CLI includes. */\r
82 #include "FreeRTOS_CLI.h"\r
83 \r
84 /* File system includes. */\r
85 #include "fat_sl.h"\r
86 #include "api_mdriver_ram.h"\r
87 \r
88 #ifdef _WINDOWS_\r
89         #define snprintf _snprintf\r
90 #endif\r
91 \r
92 #define cliNEW_LINE             "\r\n"\r
93 \r
94 /*******************************************************************************\r
95  * See the URL in the comments within main.c for the location of the online\r
96  * documentation.\r
97  ******************************************************************************/\r
98 \r
99 /*\r
100  * Print out information on a single file.\r
101  */\r
102 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );\r
103 \r
104 /*\r
105  * Copies an existing file into a newly created file.\r
106  */\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
112 \r
113 /*\r
114  * Implements the DIR command.\r
115  */\r
116 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
117 \r
118 /*\r
119  * Implements the CD command.\r
120  */\r
121 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
122 \r
123 /*\r
124  * Implements the DEL command.\r
125  */\r
126 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
127 \r
128 /*\r
129  * Implements the TYPE command.\r
130  */\r
131 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
132 \r
133 /*\r
134  * Implements the COPY command.\r
135  */\r
136 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
137 \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
141 {\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
146 };\r
147 \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
151 {\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
156 };\r
157 \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
161 {\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
166 };\r
167 \r
168 /* Structure that defines the DEL command line command, which deletes a file. */\r
169 static const CLI_Command_Definition_t xDEL =\r
170 {\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
175 };\r
176 \r
177 /* Structure that defines the COPY command line command, which deletes a file. */\r
178 static const CLI_Command_Definition_t xCOPY =\r
179 {\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
184 };\r
185 \r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void vRegisterFileSystemCLICommands( void )\r
190 {\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
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
201 {\r
202 const char *pcParameter;\r
203 BaseType_t xParameterStringLength, xReturn = pdTRUE;\r
204 static F_FILE *pxFile = NULL;\r
205 int iChar;\r
206 size_t xByte;\r
207 size_t xColumns = 50U;\r
208 \r
209         /* Ensure there is always a null terminator after each character written. */\r
210         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
211 \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
215 \r
216         if( xWriteBufferLen < xColumns )\r
217         {\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
221         }\r
222 \r
223         if( pxFile == NULL )\r
224         {\r
225                 /* The file has not been opened yet.  Find the file name. */\r
226                 pcParameter = FreeRTOS_CLIGetParameter\r
227                                                 (\r
228                                                         pcCommandString,                /* The command string itself. */\r
229                                                         1,                                              /* Return the first parameter. */\r
230                                                         &xParameterStringLength /* Store the parameter string length. */\r
231                                                 );\r
232 \r
233                 /* Sanity check something was returned. */\r
234                 configASSERT( pcParameter );\r
235 \r
236                 /* Attempt to open the requested file. */\r
237                 pxFile = f_open( pcParameter, "r" );\r
238         }\r
239 \r
240         if( pxFile != NULL )\r
241         {\r
242                 /* Read the next chunk of data from the file. */\r
243                 for( xByte = 0; xByte < xColumns; xByte++ )\r
244                 {\r
245                         iChar = f_getc( pxFile );\r
246 \r
247                         if( iChar == -1 )\r
248                         {\r
249                                 /* No more characters to return. */\r
250                                 f_close( pxFile );\r
251                                 pxFile = NULL;\r
252                                 break;\r
253                         }\r
254                         else\r
255                         {\r
256                                 pcWriteBuffer[ xByte ] = ( char ) iChar;\r
257                         }\r
258                 }\r
259         }\r
260 \r
261         if( pxFile == NULL )\r
262         {\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
265                 xReturn = pdFALSE;\r
266         }\r
267 \r
268         strcat( pcWriteBuffer, cliNEW_LINE );\r
269 \r
270         return xReturn;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
275 {\r
276 const char *pcParameter;\r
277 BaseType_t xParameterStringLength;\r
278 unsigned char ucReturned;\r
279 size_t xStringLength;\r
280 \r
281         /* Obtain the parameter string. */\r
282         pcParameter = FreeRTOS_CLIGetParameter\r
283                                         (\r
284                                                 pcCommandString,                /* The command string itself. */\r
285                                                 1,                                              /* Return the first parameter. */\r
286                                                 &xParameterStringLength /* Store the parameter string length. */\r
287                                         );\r
288 \r
289         /* Sanity check something was returned. */\r
290         configASSERT( pcParameter );\r
291 \r
292         /* Attempt to move to the requested directory. */\r
293         ucReturned = f_chdir( pcParameter );\r
294 \r
295         if( ucReturned == F_NO_ERROR )\r
296         {\r
297                 sprintf( pcWriteBuffer, "In: " );\r
298                 xStringLength = strlen( pcWriteBuffer );\r
299                 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );\r
300         }\r
301         else\r
302         {\r
303                 sprintf( pcWriteBuffer, "Error" );\r
304         }\r
305 \r
306         strcat( pcWriteBuffer, cliNEW_LINE );\r
307 \r
308         return pdFALSE;\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
313 {\r
314 static F_FIND *pxFindStruct = NULL;\r
315 unsigned char ucReturned;\r
316 BaseType_t xReturn = pdFALSE;\r
317 \r
318         /* This assumes pcWriteBuffer is long enough. */\r
319         ( void ) pcCommandString;\r
320 \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
324 \r
325         if( pxFindStruct == NULL )\r
326         {\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
330 \r
331                 if( pxFindStruct != NULL )\r
332                 {\r
333                         ucReturned = f_findfirst( "*.*", pxFindStruct );\r
334 \r
335                         if( ucReturned == F_NO_ERROR )\r
336                         {\r
337                                 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
338                                 xReturn = pdPASS;\r
339                         }\r
340                         else\r
341                         {\r
342                                 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );\r
343                         }\r
344                 }\r
345                 else\r
346                 {\r
347                         snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );\r
348                 }\r
349         }\r
350         else\r
351         {\r
352                 /* The find struct has already been created.  Find the next file in\r
353                 the directory. */\r
354                 ucReturned = f_findnext( pxFindStruct );\r
355 \r
356                 if( ucReturned == F_NO_ERROR )\r
357                 {\r
358                         prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
359                         xReturn = pdPASS;\r
360                 }\r
361                 else\r
362                 {\r
363                         /* There are no more files.  Free the find structure. */\r
364                         vPortFree( pxFindStruct );\r
365                         pxFindStruct = NULL;\r
366 \r
367                         /* No string to return. */\r
368                         pcWriteBuffer[ 0 ] = 0x00;\r
369                 }\r
370         }\r
371 \r
372         strcat( pcWriteBuffer, cliNEW_LINE );\r
373 \r
374         return xReturn;\r
375 }\r
376 /*-----------------------------------------------------------*/\r
377 \r
378 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
379 {\r
380 const char *pcParameter;\r
381 BaseType_t xParameterStringLength;\r
382 unsigned char ucReturned;\r
383 \r
384         /* This function assumes xWriteBufferLen is large enough! */\r
385         ( void ) xWriteBufferLen;\r
386 \r
387         /* Obtain the parameter string. */\r
388         pcParameter = FreeRTOS_CLIGetParameter\r
389                                         (\r
390                                                 pcCommandString,                /* The command string itself. */\r
391                                                 1,                                              /* Return the first parameter. */\r
392                                                 &xParameterStringLength /* Store the parameter string length. */\r
393                                         );\r
394 \r
395         /* Sanity check something was returned. */\r
396         configASSERT( pcParameter );\r
397 \r
398         /* Attempt to delete the file. */\r
399         ucReturned = f_delete( pcParameter );\r
400 \r
401         if( ucReturned == F_NO_ERROR )\r
402         {\r
403                 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );\r
404         }\r
405         else\r
406         {\r
407                 sprintf( pcWriteBuffer, "Error" );\r
408         }\r
409 \r
410         strcat( pcWriteBuffer, cliNEW_LINE );\r
411 \r
412         return pdFALSE;\r
413 }\r
414 /*-----------------------------------------------------------*/\r
415 \r
416 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
417 {\r
418 char *pcSourceFile, *pcDestinationFile;\r
419 BaseType_t xParameterStringLength;\r
420 long lSourceLength, lDestinationLength = 0;\r
421 \r
422         /* Obtain the name of the destination file. */\r
423         pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter\r
424                                                                         (\r
425                                                                                 pcCommandString,                /* The command string itself. */\r
426                                                                                 2,                                              /* Return the second parameter. */\r
427                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
428                                                                         );\r
429 \r
430         /* Sanity check something was returned. */\r
431         configASSERT( pcDestinationFile );\r
432 \r
433         /* Obtain the name of the source file. */\r
434         pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter\r
435                                                                 (\r
436                                                                         pcCommandString,                /* The command string itself. */\r
437                                                                         1,                                              /* Return the first parameter. */\r
438                                                                         &xParameterStringLength /* Store the parameter string length. */\r
439                                                                 );\r
440 \r
441         /* Sanity check something was returned. */\r
442         configASSERT( pcSourceFile );\r
443 \r
444         /* Terminate the string. */\r
445         pcSourceFile[ xParameterStringLength ] = 0x00;\r
446 \r
447         /* See if the source file exists, obtain its length if it does. */\r
448         lSourceLength = f_filelength( pcSourceFile );\r
449 \r
450         if( lSourceLength == 0 )\r
451         {\r
452                 sprintf( pcWriteBuffer, "Source file does not exist" );\r
453         }\r
454         else\r
455         {\r
456                 /* See if the destination file exists. */\r
457                 lDestinationLength = f_filelength( pcDestinationFile );\r
458 \r
459                 if( lDestinationLength != 0 )\r
460                 {\r
461                         sprintf( pcWriteBuffer, "Error: Destination file already exists" );\r
462                 }\r
463         }\r
464 \r
465         /* Continue only if the source file exists and the destination file does\r
466         not exist. */\r
467         if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )\r
468         {\r
469                 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )\r
470                 {\r
471                         sprintf( pcWriteBuffer, "Copy made" );\r
472                 }\r
473                 else\r
474                 {\r
475                         sprintf( pcWriteBuffer, "Error during copy" );\r
476                 }\r
477         }\r
478 \r
479         strcat( pcWriteBuffer, cliNEW_LINE );\r
480 \r
481         return pdFALSE;\r
482 }\r
483 /*-----------------------------------------------------------*/\r
484 \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
490 {\r
491 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;\r
492 F_FILE *pxFile;\r
493 BaseType_t xReturn = pdPASS;\r
494 \r
495         /* NOTE:  Error handling has been omitted for clarity. */\r
496 \r
497         while( lBytesRead < lSourceFileLength )\r
498         {\r
499                 /* How many bytes are left? */\r
500                 lBytesRemaining = lSourceFileLength - lBytesRead;\r
501 \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
505                 {\r
506                         lBytesToRead = ( long ) xWriteBufferLen;\r
507                 }\r
508                 else\r
509                 {\r
510                         lBytesToRead = lBytesRemaining;\r
511                 }\r
512 \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
518                 {\r
519                         f_seek( pxFile, lBytesRead, F_SEEK_SET );\r
520                         f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
521                         f_close( pxFile );\r
522                 }\r
523                 else\r
524                 {\r
525                         xReturn = pdFAIL;\r
526                         break;\r
527                 }\r
528 \r
529                 /* Open the destination file and write the block of data to the end of\r
530                 the file. */\r
531                 pxFile = f_open( pcDestinationFile, "a" );\r
532                 if( pxFile != NULL )\r
533                 {\r
534                         f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
535                         f_close( pxFile );\r
536                 }\r
537                 else\r
538                 {\r
539                         xReturn = pdFAIL;\r
540                         break;\r
541                 }\r
542 \r
543                 lBytesRead += lBytesToRead;\r
544         }\r
545 \r
546         return xReturn;\r
547 }\r
548 /*-----------------------------------------------------------*/\r
549 \r
550 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )\r
551 {\r
552 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";\r
553 const char * pcAttrib;\r
554 \r
555         /* Point pcAttrib to a string that describes the file. */\r
556         if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )\r
557         {\r
558                 pcAttrib = pcDirectory;\r
559         }\r
560         else if( pxFindStruct->attr & F_ATTR_READONLY )\r
561         {\r
562                 pcAttrib = pcReadOnlyFile;\r
563         }\r
564         else\r
565         {\r
566                 pcAttrib = pcWritableFile;\r
567         }\r
568 \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
572 }\r