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