2 * FreeRTOS Kernel V10.0.1
\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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /*******************************************************************************
\r
29 * See the URL in the comments within main.c for the location of the online
\r
31 ******************************************************************************/
\r
33 /* Standard includes. */
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
39 /* File system includes. */
\r
40 #include <redposix.h>
\r
42 /* The number of bytes read/written to the example files at a time. */
\r
43 #define fsRAM_BUFFER_SIZE 200
\r
45 /* The volume prefix is an empty string, for convenience since there is only one
\r
46 volume in this demo.
\r
48 #define fsVOLUME_NAME ""
\r
50 /*-----------------------------------------------------------*/
\r
53 * Creates and verifies different files on the volume, demonstrating the use of
\r
54 * various different API functions.
\r
56 void vCreateAndVerifySampleFiles( void );
\r
59 * Create a set of example files in the root directory of the volume using
\r
62 static void prvCreateDemoFiles( void );
\r
65 * Use f_read() to read back and verify the files that were created by
\r
66 * prvCreateDemoFiles().
\r
68 static void prvVerifyDemoFiles( void );
\r
70 /*-----------------------------------------------------------*/
\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
76 /* Names of directories that are created. */
\r
77 static const char *pcDirectory1 = "/SUB1", *pcDirectory2 = "/SUB1/SUB2";
\r
79 /*-----------------------------------------------------------*/
\r
81 void vCreateAndVerifySampleFiles( void )
\r
85 /* First initialize the Reliance Edge driver. */
\r
86 lStatus = red_init();
\r
88 /* Format the volume. */
\r
91 lStatus = red_format( fsVOLUME_NAME );
\r
94 /* Mount the volume. */
\r
97 lStatus = red_mount( fsVOLUME_NAME );
\r
102 /* Create a set of files using red_write(). */
\r
103 prvCreateDemoFiles();
\r
105 /* Read back and verify the files that were created using red_write(). */
\r
106 prvVerifyDemoFiles();
\r
109 /*-----------------------------------------------------------*/
\r
111 static void prvCreateDemoFiles( void )
\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
120 /* Save the current transaction point settings. */
\r
121 lStatus = red_gettransmask( fsVOLUME_NAME, &ulEventMask );
\r
122 configASSERT( lStatus == 0 );
\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
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
134 /* Generate a file name. */
\r
135 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
137 /* Print out the file name and the directory into which the file is
\r
139 printf( "Creating file %s\r\n", cFilePath );
\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
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
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
154 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
155 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
158 /* Close the file so another file can be created. */
\r
159 lStatus = red_close( lFildes );
\r
160 configASSERT( lStatus == 0 );
\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
168 /* Create a sub directory. */
\r
169 printf( "Creating directory %s\r\n", pcDirectory1 );
\r
171 lStatus = red_mkdir( pcDirectory1 );
\r
172 configASSERT( lStatus == 0 );
\r
174 /* Create a subdirectory in the new directory. */
\r
175 printf( "Creating directory %s\r\n", pcDirectory2 );
\r
177 lStatus = red_mkdir( pcDirectory2 );
\r
178 configASSERT( lStatus == 0 );
\r
180 /* Generate the file name. */
\r
181 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
183 /* Print out the file name and the directory into which the file is being
\r
185 printf( "Writing file %s\r\n", cFilePath );
\r
187 lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );
\r
189 /* Write the file. It is filled with incrementing ascii characters starting
\r
191 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
193 cRAMBuffer[ iByte ] = ( char ) ( ( int ) '0' + iByte );
\r
196 lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
197 configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
\r
199 /* Finished so close the file. */
\r
200 lStatus = red_close( lFildes );
\r
201 configASSERT( lStatus == 0 );
\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
208 /* Restore previous transaction point settings. */
\r
209 lStatus = red_settransmask( fsVOLUME_NAME, ulEventMask );
\r
210 configASSERT( lStatus == 0 );
\r
212 /*-----------------------------------------------------------*/
\r
214 static void prvVerifyDemoFiles( void )
\r
216 BaseType_t xFileNumber, xReadNumber;
\r
217 char cFilePath[ 64 ];
\r
218 const BaseType_t xMaxFiles = 5;
\r
220 int32_t lBytesRead, lFildes, lStatus;
\r
223 /* Read back the files that were created by prvCreateDemoFiles(). */
\r
224 for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
\r
226 /* Generate the file name. */
\r
227 sprintf( cFilePath, "/root%03d.txt", xFileNumber );
\r
229 /* Print out the file name and the directory from which the file is
\r
231 printf( "Reading file %s\r\n", cFilePath );
\r
233 /* Open the file for reading. */
\r
234 lFildes = red_open( cFilePath, RED_O_RDONLY );
\r
235 configASSERT( lFildes != -1 );
\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
241 /* Start with the RAM buffer clear. */
\r
242 memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );
\r
244 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
245 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\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
252 configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
\r
256 /* Close the file. */
\r
257 lStatus = red_close( lFildes );
\r
258 configASSERT( lStatus == 0 );
\r
261 /* Generate the file name. */
\r
262 sprintf( cFilePath, "%s/file.txt", pcDirectory2 );
\r
264 /* Print out the file name and the directory from which the file is being
\r
266 printf( "Reading file %s\r\n", cFilePath );
\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
272 /* Read the file. */
\r
273 lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
\r
274 configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );
\r
276 /* Verify the file 1 byte at a time. */
\r
277 for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
\r
279 configASSERT( cRAMBuffer[ iByte ] == ( char ) ( ( int ) '0' + iByte ) );
\r
282 /* Finished so close the file. */
\r
283 lStatus = red_close( lFildes );
\r
284 configASSERT( lStatus == 0 );
\r