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