]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator/File-system-demo.c
Update FreeRTOS+ version number ready for version 9 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator / File-system-demo.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  * See the URL in the comments within main.c for the location of the online\r
72  * documentation.\r
73  ******************************************************************************/\r
74 \r
75 /* Standard includes. */\r
76 #include <stdio.h>\r
77 \r
78 /* FreeRTOS includes. */\r
79 #include "FreeRTOS.h"\r
80 \r
81 /* File system includes. */\r
82 #include <redposix.h>\r
83 \r
84 /* The number of bytes read/written to the example files at a time. */\r
85 #define fsRAM_BUFFER_SIZE       200\r
86 \r
87 /* The volume prefix is an empty string, for convenience since there is only one\r
88 volume in this demo.\r
89 */\r
90 #define fsVOLUME_NAME           ""\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /*\r
95  * Creates and verifies different files on the volume, demonstrating the use of\r
96  * various different API functions.\r
97  */\r
98 void vCreateAndVerifySampleFiles( void );\r
99 \r
100 /*\r
101  * Create a set of example files in the root directory of the volume using\r
102  * f_write().\r
103  */\r
104 static void prvCreateDemoFiles( void );\r
105 \r
106 /*\r
107  * Use f_read() to read back and verify the files that were created by\r
108  * prvCreateDemoFiles().\r
109  */\r
110 static void prvVerifyDemoFiles( void );\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /* A buffer used to both create content to write to disk, and read content back\r
115 from a disk.  Note there is no mutual exclusion on this buffer. */\r
116 static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];\r
117 \r
118 /* Names of directories that are created. */\r
119 static const char *pcDirectory1 = "/SUB1", *pcDirectory2 = "/SUB1/SUB2";\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void vCreateAndVerifySampleFiles( void )\r
124 {\r
125 int32_t lStatus;\r
126 \r
127         /* First initialize the Reliance Edge driver. */\r
128         lStatus = red_init();\r
129 \r
130         /* Format the volume. */\r
131         if( lStatus == 0 )\r
132         {\r
133                 lStatus = red_format( fsVOLUME_NAME );\r
134         }\r
135 \r
136         /* Mount the volume. */\r
137         if( lStatus == 0 )\r
138         {\r
139                 lStatus = red_mount( fsVOLUME_NAME );\r
140         }\r
141 \r
142         if( lStatus == 0 )\r
143         {\r
144                 /* Create a set of files using red_write(). */\r
145                 prvCreateDemoFiles();\r
146 \r
147                 /* Read back and verify the files that were created using red_write(). */\r
148                 prvVerifyDemoFiles();\r
149         }\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvCreateDemoFiles( void )\r
154 {\r
155 BaseType_t xFileNumber, xWriteNumber;\r
156 char cFilePath[ 64 ];\r
157 const BaseType_t xMaxFiles = 5;\r
158 uint32_t ulEventMask;\r
159 int32_t lBytesWritten, lFildes, lStatus;\r
160 int iByte;\r
161 \r
162         /* Save the current transaction point settings. */\r
163         lStatus = red_gettransmask( fsVOLUME_NAME, &ulEventMask );\r
164         configASSERT( lStatus == 0 );\r
165 \r
166         /* Disable automatic transaction points so that all of the files can be\r
167         created in one atomic operation. */\r
168         lStatus = red_settransmask( fsVOLUME_NAME, RED_TRANSACT_MANUAL );\r
169         configASSERT( lStatus == 0 );\r
170 \r
171         /* Create xMaxFiles files.  Each created file will be\r
172         ( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled\r
173         with a different repeating character. */\r
174         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
175         {\r
176                 /* Generate a file name. */\r
177                 sprintf( cFilePath, "/root%03d.txt", xFileNumber );\r
178 \r
179                 /* Print out the file name and the directory into which the file is\r
180                 being written. */\r
181                 printf( "Creating file %s\r\n", cFilePath );\r
182 \r
183                 /* Open the file, creating the file if it does not already exist. */\r
184                 lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );\r
185                 configASSERT( lFildes != -1 );\r
186 \r
187                 /* Fill the RAM buffer with data that will be written to the file.  This\r
188                 is just a repeating ascii character that indicates the file number. */\r
189                 memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );\r
190 \r
191                 /* Write the RAM buffer to the opened file a number of times.  The\r
192                 number of times the RAM buffer is written to the file depends on the\r
193                 file number, so the length of each created file will be different. */\r
194                 for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )\r
195                 {\r
196                         lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );\r
197                         configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );\r
198                 }\r
199 \r
200                 /* Close the file so another file can be created. */\r
201                 lStatus = red_close( lFildes );\r
202                 configASSERT( lStatus == 0 );\r
203         }\r
204 \r
205         /* Commit a transaction point, atomically adding the set of files to the\r
206         transacted state. */\r
207         lStatus = red_transact( fsVOLUME_NAME );\r
208         configASSERT( lStatus == 0 );\r
209 \r
210         /* Create a sub directory. */\r
211         printf( "Creating directory %s\r\n", pcDirectory1 );\r
212 \r
213         lStatus = red_mkdir( pcDirectory1 );\r
214         configASSERT( lStatus == 0 );\r
215 \r
216         /* Create a subdirectory in the new directory. */\r
217         printf( "Creating directory %s\r\n", pcDirectory2 );\r
218 \r
219         lStatus = red_mkdir( pcDirectory2 );\r
220         configASSERT( lStatus == 0 );\r
221 \r
222         /* Generate the file name. */\r
223         sprintf( cFilePath, "%s/file.txt", pcDirectory2 );\r
224 \r
225         /* Print out the file name and the directory into which the file is being\r
226         written. */\r
227         printf( "Writing file %s\r\n", cFilePath );\r
228 \r
229         lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );\r
230 \r
231         /* Write the file.  It is filled with incrementing ascii characters starting\r
232         from '0'. */\r
233         for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )\r
234         {\r
235                 cRAMBuffer[ iByte ] = ( char ) ( ( int ) '0' + iByte );\r
236         }\r
237 \r
238         lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );\r
239         configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );\r
240 \r
241         /* Finished so close the file. */\r
242         lStatus = red_close( lFildes );\r
243         configASSERT( lStatus == 0 );\r
244 \r
245         /* Commit a transaction point, atomically adding the set of files and\r
246         directories to the transacted state. */\r
247         lStatus = red_transact( fsVOLUME_NAME );\r
248         configASSERT( lStatus == 0 );\r
249 \r
250         /* Restore previous transaction point settings. */\r
251         lStatus = red_settransmask( fsVOLUME_NAME, ulEventMask );\r
252         configASSERT( lStatus == 0 );\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvVerifyDemoFiles( void )\r
257 {\r
258 BaseType_t xFileNumber, xReadNumber;\r
259 char cFilePath[ 64 ];\r
260 const BaseType_t xMaxFiles = 5;\r
261 long lChar;\r
262 int32_t lBytesRead, lFildes, lStatus;\r
263 int iByte;\r
264 \r
265         /* Read back the files that were created by prvCreateDemoFiles(). */\r
266         for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )\r
267         {\r
268                 /* Generate the file name. */\r
269                 sprintf( cFilePath, "/root%03d.txt", xFileNumber );\r
270 \r
271                 /* Print out the file name and the directory from which the file is\r
272                 being read. */\r
273                 printf( "Reading file %s\r\n", cFilePath );\r
274 \r
275                 /* Open the file for reading. */\r
276                 lFildes = red_open( cFilePath, RED_O_RDONLY );\r
277                 configASSERT( lFildes != -1 );\r
278 \r
279                 /* Read the file into the RAM buffer, checking the file contents are as\r
280                 expected.  The size of the file depends on the file number. */\r
281                 for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )\r
282                 {\r
283                         /* Start with the RAM buffer clear. */\r
284                         memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );\r
285 \r
286                         lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );\r
287                         configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );\r
288 \r
289                         /* Check the RAM buffer is filled with the expected data.  Each\r
290                         file contains a different repeating ascii character that indicates\r
291                         the number of the file. */\r
292                         for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )\r
293                         {\r
294                                 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );\r
295                         }\r
296                 }\r
297 \r
298                 /* Close the file. */\r
299                 lStatus = red_close( lFildes );\r
300                 configASSERT( lStatus == 0 );\r
301         }\r
302 \r
303         /* Generate the file name. */\r
304         sprintf( cFilePath, "%s/file.txt", pcDirectory2 );\r
305 \r
306         /* Print out the file name and the directory from which the file is being\r
307         read. */\r
308         printf( "Reading file %s\r\n", cFilePath );\r
309 \r
310         /* This time the file is opened for reading. */\r
311         lFildes = red_open( cFilePath, RED_O_RDONLY );\r
312         configASSERT( lFildes != -1 );\r
313 \r
314         /* Read the file. */\r
315         lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );\r
316         configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );\r
317 \r
318         /* Verify the file 1 byte at a time. */\r
319         for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )\r
320         {\r
321                 configASSERT( cRAMBuffer[ iByte ] == ( char ) ( ( int ) '0' + iByte ) );\r
322         }\r
323 \r
324         /* Finished so close the file. */\r
325         lStatus = red_close( lFildes );\r
326         configASSERT( lStatus == 0 );\r
327 }\r
328 \r
329 \r
330 \r
331 \r