2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*******************************************************************************
\r
30 * See the URL in the comments within main.c for the location of the online
\r
32 ******************************************************************************/
\r
34 /* Standard includes. */
\r
37 /* FreeRTOS includes. */
\r
38 #include "FreeRTOS.h"
\r
40 /* File system includes. */
\r
41 #include <redposix.h>
\r
43 /* The number of bytes read/written to the example files at a time. */
\r
44 #define fsRAM_BUFFER_SIZE 200
\r
46 /* The volume prefix is an empty string, for convenience since there is only one
\r
47 volume in this demo.
\r
49 #define fsVOLUME_NAME ""
\r
51 /*-----------------------------------------------------------*/
\r
54 * Creates and verifies different files on the volume, demonstrating the use of
\r
55 * various different API functions.
\r
57 void vCreateAndVerifySampleFiles( void );
\r
60 * Create a set of example files in the root directory of the volume using
\r
63 static void prvCreateDemoFiles( void );
\r
66 * Use f_read() to read back and verify the files that were created by
\r
67 * prvCreateDemoFiles().
\r
69 static void prvVerifyDemoFiles( void );
\r
71 /*-----------------------------------------------------------*/
\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
77 /* Names of directories that are created. */
\r
78 static const char *pcDirectory1 = "/SUB1", *pcDirectory2 = "/SUB1/SUB2";
\r
80 /*-----------------------------------------------------------*/
\r
82 void vCreateAndVerifySampleFiles( void )
\r
86 /* First initialize the Reliance Edge driver. */
\r
87 lStatus = red_init();
\r
89 /* Format the volume. */
\r
92 lStatus = red_format( fsVOLUME_NAME );
\r
95 /* Mount the volume. */
\r
98 lStatus = red_mount( fsVOLUME_NAME );
\r
103 /* Create a set of files using red_write(). */
\r
104 prvCreateDemoFiles();
\r
106 /* Read back and verify the files that were created using red_write(). */
\r
107 prvVerifyDemoFiles();
\r
110 /*-----------------------------------------------------------*/
\r
112 static void prvCreateDemoFiles( void )
\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
121 /* Save the current transaction point settings. */
\r
122 lStatus = red_gettransmask( fsVOLUME_NAME, &ulEventMask );
\r
123 configASSERT( lStatus == 0 );
\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
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
135 /* Generate a file name. */
\r
136 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
138 /* Print out the file name and the directory into which the file is
\r
140 printf( "Creating file %s\r\n", cFilePath );
\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
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
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
155 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
156 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
159 /* Close the file so another file can be created. */
\r
160 lStatus = red_close( lFildes );
\r
161 configASSERT( lStatus == 0 );
\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
169 /* Create a sub directory. */
\r
170 printf( "Creating directory %s\r\n", pcDirectory1 );
\r
172 lStatus = red_mkdir( pcDirectory1 );
\r
173 configASSERT( lStatus == 0 );
\r
175 /* Create a subdirectory in the new directory. */
\r
176 printf( "Creating directory %s\r\n", pcDirectory2 );
\r
178 lStatus = red_mkdir( pcDirectory2 );
\r
179 configASSERT( lStatus == 0 );
\r
181 /* Generate the file name. */
\r
182 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
184 /* Print out the file name and the directory into which the file is being
\r
186 printf( "Writing file %s\r\n", cFilePath );
\r
188 lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );
\r
190 /* Write the file. It is filled with incrementing ascii characters starting
\r
192 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
194 cRAMBuffer[ iByte ] = ( char ) ( ( int ) '0' + iByte );
\r
197 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
198 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
200 /* Finished so close the file. */
\r
201 lStatus = red_close( lFildes );
\r
202 configASSERT( lStatus == 0 );
\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
209 /* Restore previous transaction point settings. */
\r
210 lStatus = red_settransmask( fsVOLUME_NAME, ulEventMask );
\r
211 configASSERT( lStatus == 0 );
\r
213 /*-----------------------------------------------------------*/
\r
215 static void prvVerifyDemoFiles( void )
\r
217 BaseType_t xFileNumber, xReadNumber;
\r
218 char cFilePath[ 64 ];
\r
219 const BaseType_t xMaxFiles = 5;
\r
221 int32_t lBytesRead, lFildes, lStatus;
\r
224 /* Read back the files that were created by prvCreateDemoFiles(). */
\r
225 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
227 /* Generate the file name. */
\r
228 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
230 /* Print out the file name and the directory from which the file is
\r
232 printf( "Reading file %s\r\n", cFilePath );
\r
234 /* Open the file for reading. */
\r
235 lFildes = red_open( cFilePath, RED_O_RDONLY );
\r
236 configASSERT( lFildes != -1 );
\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
242 /* Start with the RAM buffer clear. */
\r
243 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
245 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
246 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\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
253 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
257 /* Close the file. */
\r
258 lStatus = red_close( lFildes );
\r
259 configASSERT( lStatus == 0 );
\r
262 /* Generate the file name. */
\r
263 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
265 /* Print out the file name and the directory from which the file is being
\r
267 printf( "Reading file %s\r\n", cFilePath );
\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
273 /* Read the file. */
\r
274 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
275 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\r
277 /* Verify the file 1 byte at a time. */
\r
278 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
280 configASSERT( cRAMBuffer[ iByte ] == ( char ) ( ( int ) '0' + iByte ) );
\r
283 /* Finished so close the file. */
\r
284 lStatus = red_close( lFildes );
\r
285 configASSERT( lStatus == 0 );
\r