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