]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_CLI_Demos/File-Related-CLI-commands.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_CLI_Demos / File-Related-CLI-commands.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /* FreeRTOS includes. */\r
31 #include "FreeRTOS.h"\r
32 #include "task.h"\r
33 \r
34 /* Standard includes. */\r
35 #include <stdint.h>\r
36 #include <stdio.h>\r
37 #include <stdlib.h>\r
38 #include <string.h>\r
39 \r
40 /* FreeRTOS+CLI includes. */\r
41 #include "FreeRTOS_CLI.h"\r
42 \r
43 /* File system includes. */\r
44 #include "fat_sl.h"\r
45 #include "api_mdriver_ram.h"\r
46 \r
47 #ifdef _WINDOWS_\r
48         #define snprintf _snprintf\r
49 #endif\r
50 \r
51 #define cliNEW_LINE             "\r\n"\r
52 \r
53 /*******************************************************************************\r
54  * See the URL in the comments within main.c for the location of the online\r
55  * documentation.\r
56  ******************************************************************************/\r
57 \r
58 /*\r
59  * Print out information on a single file.\r
60  */\r
61 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct );\r
62 \r
63 /*\r
64  * Copies an existing file into a newly created file.\r
65  */\r
66 static BaseType_t prvPerformCopy( const char *pcSourceFile,\r
67                                                                         int32_t lSourceFileLength,\r
68                                                                         const char *pcDestinationFile,\r
69                                                                         char *pxWriteBuffer,\r
70                                                                         size_t xWriteBufferLen );\r
71 \r
72 /*\r
73  * Implements the DIR command.\r
74  */\r
75 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
76 \r
77 /*\r
78  * Implements the CD command.\r
79  */\r
80 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
81 \r
82 /*\r
83  * Implements the DEL command.\r
84  */\r
85 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
86 \r
87 /*\r
88  * Implements the TYPE command.\r
89  */\r
90 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
91 \r
92 /*\r
93  * Implements the COPY command.\r
94  */\r
95 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString );\r
96 \r
97 /* Structure that defines the DIR command line command, which lists all the\r
98 files in the current directory. */\r
99 static const CLI_Command_Definition_t xDIR =\r
100 {\r
101         "dir", /* The command string to type. */\r
102         "\r\ndir:\r\n Lists the files in the current directory\r\n",\r
103         prvDIRCommand, /* The function to run. */\r
104         0 /* No parameters are expected. */\r
105 };\r
106 \r
107 /* Structure that defines the CD command line command, which changes the\r
108 working directory. */\r
109 static const CLI_Command_Definition_t xCD =\r
110 {\r
111         "cd", /* The command string to type. */\r
112         "\r\ncd <dir name>:\r\n Changes the working directory\r\n",\r
113         prvCDCommand, /* The function to run. */\r
114         1 /* One parameter is expected. */\r
115 };\r
116 \r
117 /* Structure that defines the TYPE command line command, which prints the\r
118 contents of a file to the console. */\r
119 static const CLI_Command_Definition_t xTYPE =\r
120 {\r
121         "type", /* The command string to type. */\r
122         "\r\ntype <filename>:\r\n Prints file contents to the terminal\r\n",\r
123         prvTYPECommand, /* The function to run. */\r
124         1 /* One parameter is expected. */\r
125 };\r
126 \r
127 /* Structure that defines the DEL command line command, which deletes a file. */\r
128 static const CLI_Command_Definition_t xDEL =\r
129 {\r
130         "del", /* The command string to type. */\r
131         "\r\ndel <filename>:\r\n deletes a file or directory\r\n",\r
132         prvDELCommand, /* The function to run. */\r
133         1 /* One parameter is expected. */\r
134 };\r
135 \r
136 /* Structure that defines the COPY command line command, which deletes a file. */\r
137 static const CLI_Command_Definition_t xCOPY =\r
138 {\r
139         "copy", /* The command string to type. */\r
140         "\r\ncopy <source file> <dest file>:\r\n Copies <source file> to <dest file>\r\n",\r
141         prvCOPYCommand, /* The function to run. */\r
142         2 /* Two parameters are expected. */\r
143 };\r
144 \r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vRegisterFileSystemCLICommands( void )\r
149 {\r
150         /* Register all the command line commands defined immediately above. */\r
151         FreeRTOS_CLIRegisterCommand( &xDIR );\r
152         FreeRTOS_CLIRegisterCommand( &xCD );\r
153         FreeRTOS_CLIRegisterCommand( &xTYPE );\r
154         FreeRTOS_CLIRegisterCommand( &xDEL );\r
155         FreeRTOS_CLIRegisterCommand( &xCOPY );\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 static BaseType_t prvTYPECommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
160 {\r
161 const char *pcParameter;\r
162 BaseType_t xParameterStringLength, xReturn = pdTRUE;\r
163 static F_FILE *pxFile = NULL;\r
164 int iChar;\r
165 size_t xByte;\r
166 size_t xColumns = 50U;\r
167 \r
168         /* Ensure there is always a null terminator after each character written. */\r
169         memset( pcWriteBuffer, 0x00, xWriteBufferLen );\r
170 \r
171         /* Ensure the buffer leaves space for the \r\n. */\r
172         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
173         xWriteBufferLen -= strlen( cliNEW_LINE );\r
174 \r
175         if( xWriteBufferLen < xColumns )\r
176         {\r
177                 /* Ensure the loop that uses xColumns as an end condition does not\r
178                 write off the end of the buffer. */\r
179                 xColumns = xWriteBufferLen;\r
180         }\r
181 \r
182         if( pxFile == NULL )\r
183         {\r
184                 /* The file has not been opened yet.  Find the file name. */\r
185                 pcParameter = FreeRTOS_CLIGetParameter\r
186                                                 (\r
187                                                         pcCommandString,                /* The command string itself. */\r
188                                                         1,                                              /* Return the first parameter. */\r
189                                                         &xParameterStringLength /* Store the parameter string length. */\r
190                                                 );\r
191 \r
192                 /* Sanity check something was returned. */\r
193                 configASSERT( pcParameter );\r
194 \r
195                 /* Attempt to open the requested file. */\r
196                 pxFile = f_open( pcParameter, "r" );\r
197         }\r
198 \r
199         if( pxFile != NULL )\r
200         {\r
201                 /* Read the next chunk of data from the file. */\r
202                 for( xByte = 0; xByte < xColumns; xByte++ )\r
203                 {\r
204                         iChar = f_getc( pxFile );\r
205 \r
206                         if( iChar == -1 )\r
207                         {\r
208                                 /* No more characters to return. */\r
209                                 f_close( pxFile );\r
210                                 pxFile = NULL;\r
211                                 break;\r
212                         }\r
213                         else\r
214                         {\r
215                                 pcWriteBuffer[ xByte ] = ( char ) iChar;\r
216                         }\r
217                 }\r
218         }\r
219 \r
220         if( pxFile == NULL )\r
221         {\r
222                 /* Either the file was not opened, or all the data from the file has\r
223                 been returned and the file is now closed. */\r
224                 xReturn = pdFALSE;\r
225         }\r
226 \r
227         strcat( pcWriteBuffer, cliNEW_LINE );\r
228 \r
229         return xReturn;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static BaseType_t prvCDCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
234 {\r
235 const char *pcParameter;\r
236 BaseType_t xParameterStringLength;\r
237 unsigned char ucReturned;\r
238 size_t xStringLength;\r
239 \r
240         /* Obtain the parameter string. */\r
241         pcParameter = FreeRTOS_CLIGetParameter\r
242                                         (\r
243                                                 pcCommandString,                /* The command string itself. */\r
244                                                 1,                                              /* Return the first parameter. */\r
245                                                 &xParameterStringLength /* Store the parameter string length. */\r
246                                         );\r
247 \r
248         /* Sanity check something was returned. */\r
249         configASSERT( pcParameter );\r
250 \r
251         /* Attempt to move to the requested directory. */\r
252         ucReturned = f_chdir( pcParameter );\r
253 \r
254         if( ucReturned == F_NO_ERROR )\r
255         {\r
256                 sprintf( pcWriteBuffer, "In: " );\r
257                 xStringLength = strlen( pcWriteBuffer );\r
258                 f_getcwd( &( pcWriteBuffer[ xStringLength ] ), ( unsigned char ) ( xWriteBufferLen - xStringLength ) );\r
259         }\r
260         else\r
261         {\r
262                 sprintf( pcWriteBuffer, "Error" );\r
263         }\r
264 \r
265         strcat( pcWriteBuffer, cliNEW_LINE );\r
266 \r
267         return pdFALSE;\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static BaseType_t prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
272 {\r
273 static F_FIND *pxFindStruct = NULL;\r
274 unsigned char ucReturned;\r
275 BaseType_t xReturn = pdFALSE;\r
276 \r
277         /* This assumes pcWriteBuffer is long enough. */\r
278         ( void ) pcCommandString;\r
279 \r
280         /* Ensure the buffer leaves space for the \r\n. */\r
281         configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) );\r
282         xWriteBufferLen -= strlen( cliNEW_LINE );\r
283 \r
284         if( pxFindStruct == NULL )\r
285         {\r
286                 /* This is the first time this function has been executed since the Dir\r
287                 command was run.  Create the find structure. */\r
288                 pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) );\r
289 \r
290                 if( pxFindStruct != NULL )\r
291                 {\r
292                         ucReturned = f_findfirst( "*.*", pxFindStruct );\r
293 \r
294                         if( ucReturned == F_NO_ERROR )\r
295                         {\r
296                                 prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
297                                 xReturn = pdPASS;\r
298                         }\r
299                         else\r
300                         {\r
301                                 snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." );\r
302                         }\r
303                 }\r
304                 else\r
305                 {\r
306                         snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." );\r
307                 }\r
308         }\r
309         else\r
310         {\r
311                 /* The find struct has already been created.  Find the next file in\r
312                 the directory. */\r
313                 ucReturned = f_findnext( pxFindStruct );\r
314 \r
315                 if( ucReturned == F_NO_ERROR )\r
316                 {\r
317                         prvCreateFileInfoString( pcWriteBuffer, pxFindStruct );\r
318                         xReturn = pdPASS;\r
319                 }\r
320                 else\r
321                 {\r
322                         /* There are no more files.  Free the find structure. */\r
323                         vPortFree( pxFindStruct );\r
324                         pxFindStruct = NULL;\r
325 \r
326                         /* No string to return. */\r
327                         pcWriteBuffer[ 0 ] = 0x00;\r
328                 }\r
329         }\r
330 \r
331         strcat( pcWriteBuffer, cliNEW_LINE );\r
332 \r
333         return xReturn;\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 static BaseType_t prvDELCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
338 {\r
339 const char *pcParameter;\r
340 BaseType_t xParameterStringLength;\r
341 unsigned char ucReturned;\r
342 \r
343         /* This function assumes xWriteBufferLen is large enough! */\r
344         ( void ) xWriteBufferLen;\r
345 \r
346         /* Obtain the parameter string. */\r
347         pcParameter = FreeRTOS_CLIGetParameter\r
348                                         (\r
349                                                 pcCommandString,                /* The command string itself. */\r
350                                                 1,                                              /* Return the first parameter. */\r
351                                                 &xParameterStringLength /* Store the parameter string length. */\r
352                                         );\r
353 \r
354         /* Sanity check something was returned. */\r
355         configASSERT( pcParameter );\r
356 \r
357         /* Attempt to delete the file. */\r
358         ucReturned = f_delete( pcParameter );\r
359 \r
360         if( ucReturned == F_NO_ERROR )\r
361         {\r
362                 sprintf( pcWriteBuffer, "%s was deleted", pcParameter );\r
363         }\r
364         else\r
365         {\r
366                 sprintf( pcWriteBuffer, "Error" );\r
367         }\r
368 \r
369         strcat( pcWriteBuffer, cliNEW_LINE );\r
370 \r
371         return pdFALSE;\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 static BaseType_t prvCOPYCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString )\r
376 {\r
377 char *pcSourceFile, *pcDestinationFile;\r
378 BaseType_t xParameterStringLength;\r
379 long lSourceLength, lDestinationLength = 0;\r
380 \r
381         /* Obtain the name of the destination file. */\r
382         pcDestinationFile = ( char * ) FreeRTOS_CLIGetParameter\r
383                                                                         (\r
384                                                                                 pcCommandString,                /* The command string itself. */\r
385                                                                                 2,                                              /* Return the second parameter. */\r
386                                                                                 &xParameterStringLength /* Store the parameter string length. */\r
387                                                                         );\r
388 \r
389         /* Sanity check something was returned. */\r
390         configASSERT( pcDestinationFile );\r
391 \r
392         /* Obtain the name of the source file. */\r
393         pcSourceFile = ( char * ) FreeRTOS_CLIGetParameter\r
394                                                                 (\r
395                                                                         pcCommandString,                /* The command string itself. */\r
396                                                                         1,                                              /* Return the first parameter. */\r
397                                                                         &xParameterStringLength /* Store the parameter string length. */\r
398                                                                 );\r
399 \r
400         /* Sanity check something was returned. */\r
401         configASSERT( pcSourceFile );\r
402 \r
403         /* Terminate the string. */\r
404         pcSourceFile[ xParameterStringLength ] = 0x00;\r
405 \r
406         /* See if the source file exists, obtain its length if it does. */\r
407         lSourceLength = f_filelength( pcSourceFile );\r
408 \r
409         if( lSourceLength == 0 )\r
410         {\r
411                 sprintf( pcWriteBuffer, "Source file does not exist" );\r
412         }\r
413         else\r
414         {\r
415                 /* See if the destination file exists. */\r
416                 lDestinationLength = f_filelength( pcDestinationFile );\r
417 \r
418                 if( lDestinationLength != 0 )\r
419                 {\r
420                         sprintf( pcWriteBuffer, "Error: Destination file already exists" );\r
421                 }\r
422         }\r
423 \r
424         /* Continue only if the source file exists and the destination file does\r
425         not exist. */\r
426         if( ( lSourceLength != 0 ) && ( lDestinationLength == 0 ) )\r
427         {\r
428                 if( prvPerformCopy( pcSourceFile, lSourceLength, pcDestinationFile, pcWriteBuffer, xWriteBufferLen ) == pdPASS )\r
429                 {\r
430                         sprintf( pcWriteBuffer, "Copy made" );\r
431                 }\r
432                 else\r
433                 {\r
434                         sprintf( pcWriteBuffer, "Error during copy" );\r
435                 }\r
436         }\r
437 \r
438         strcat( pcWriteBuffer, cliNEW_LINE );\r
439 \r
440         return pdFALSE;\r
441 }\r
442 /*-----------------------------------------------------------*/\r
443 \r
444 static BaseType_t prvPerformCopy( const char *pcSourceFile,\r
445                                                                         int32_t lSourceFileLength,\r
446                                                                         const char *pcDestinationFile,\r
447                                                                         char *pxWriteBuffer,\r
448                                                                         size_t xWriteBufferLen )\r
449 {\r
450 int32_t lBytesRead = 0, lBytesToRead, lBytesRemaining;\r
451 F_FILE *pxFile;\r
452 BaseType_t xReturn = pdPASS;\r
453 \r
454         /* NOTE:  Error handling has been omitted for clarity. */\r
455 \r
456         while( lBytesRead < lSourceFileLength )\r
457         {\r
458                 /* How many bytes are left? */\r
459                 lBytesRemaining = lSourceFileLength - lBytesRead;\r
460 \r
461                 /* How many bytes should be read this time around the loop.  Can't\r
462                 read more bytes than will fit into the buffer. */\r
463                 if( lBytesRemaining > ( long ) xWriteBufferLen )\r
464                 {\r
465                         lBytesToRead = ( long ) xWriteBufferLen;\r
466                 }\r
467                 else\r
468                 {\r
469                         lBytesToRead = lBytesRemaining;\r
470                 }\r
471 \r
472                 /* Open the source file, seek past the data that has already been\r
473                 read from the file, read the next block of data, then close the\r
474                 file again so the destination file can be opened. */\r
475                 pxFile = f_open( pcSourceFile, "r" );\r
476                 if( pxFile != NULL )\r
477                 {\r
478                         f_seek( pxFile, lBytesRead, F_SEEK_SET );\r
479                         f_read( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
480                         f_close( pxFile );\r
481                 }\r
482                 else\r
483                 {\r
484                         xReturn = pdFAIL;\r
485                         break;\r
486                 }\r
487 \r
488                 /* Open the destination file and write the block of data to the end of\r
489                 the file. */\r
490                 pxFile = f_open( pcDestinationFile, "a" );\r
491                 if( pxFile != NULL )\r
492                 {\r
493                         f_write( pxWriteBuffer, lBytesToRead, 1, pxFile );\r
494                         f_close( pxFile );\r
495                 }\r
496                 else\r
497                 {\r
498                         xReturn = pdFAIL;\r
499                         break;\r
500                 }\r
501 \r
502                 lBytesRead += lBytesToRead;\r
503         }\r
504 \r
505         return xReturn;\r
506 }\r
507 /*-----------------------------------------------------------*/\r
508 \r
509 static void prvCreateFileInfoString( char *pcBuffer, F_FIND *pxFindStruct )\r
510 {\r
511 const char *pcWritableFile = "writable file", *pcReadOnlyFile = "read only file", *pcDirectory = "directory";\r
512 const char * pcAttrib;\r
513 \r
514         /* Point pcAttrib to a string that describes the file. */\r
515         if( ( pxFindStruct->attr & F_ATTR_DIR ) != 0 )\r
516         {\r
517                 pcAttrib = pcDirectory;\r
518         }\r
519         else if( pxFindStruct->attr & F_ATTR_READONLY )\r
520         {\r
521                 pcAttrib = pcReadOnlyFile;\r
522         }\r
523         else\r
524         {\r
525                 pcAttrib = pcWritableFile;\r
526         }\r
527 \r
528         /* Create a string that includes the file name, the file size and the\r
529         attributes string. */\r
530         sprintf( pcBuffer, "%s [%s] [size=%d]", pxFindStruct->filename, pcAttrib, ( int ) pxFindStruct->filesize );\r
531 }\r