summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Demo/FreeRTOS_Plus_Reliance_Edge_and_CLI_Windows_Simulator/File-system-demo.c
blob: cb077f3c44a2266623afb069d1347fdc4c0c317e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * FreeRTOS Kernel V10.4.0
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

/*******************************************************************************
 * See the URL in the comments within main.c for the location of the online
 * documentation.
 ******************************************************************************/

/* Standard includes. */
#include <stdio.h>

/* FreeRTOS includes. */
#include "FreeRTOS.h"

/* File system includes. */
#include <redposix.h>

/* The number of bytes read/written to the example files at a time. */
#define fsRAM_BUFFER_SIZE	200

/* The volume prefix is an empty string, for convenience since there is only one
volume in this demo.
*/
#define fsVOLUME_NAME		""

/*-----------------------------------------------------------*/

/*
 * Creates and verifies different files on the volume, demonstrating the use of
 * various different API functions.
 */
void vCreateAndVerifySampleFiles( void );

/*
 * Create a set of example files in the root directory of the volume using
 * f_write().
 */
static void prvCreateDemoFiles( void );

/*
 * Use f_read() to read back and verify the files that were created by
 * prvCreateDemoFiles().
 */
static void prvVerifyDemoFiles( void );

/*-----------------------------------------------------------*/

/* A buffer used to both create content to write to disk, and read content back
from a disk.  Note there is no mutual exclusion on this buffer. */
static char cRAMBuffer[ fsRAM_BUFFER_SIZE ];

/* Names of directories that are created. */
static const char *pcDirectory1 = "/SUB1", *pcDirectory2 = "/SUB1/SUB2";

/*-----------------------------------------------------------*/

void vCreateAndVerifySampleFiles( void )
{
int32_t lStatus;

	/* First initialize the Reliance Edge driver. */
	lStatus = red_init();

	/* Format the volume. */
	if( lStatus == 0 )
	{
		lStatus = red_format( fsVOLUME_NAME );
	}

	/* Mount the volume. */
	if( lStatus == 0 )
	{
		lStatus = red_mount( fsVOLUME_NAME );
	}

	if( lStatus == 0 )
	{
		/* Create a set of files using red_write(). */
		prvCreateDemoFiles();

		/* Read back and verify the files that were created using red_write(). */
		prvVerifyDemoFiles();
	}
}
/*-----------------------------------------------------------*/

static void prvCreateDemoFiles( void )
{
BaseType_t xFileNumber, xWriteNumber;
char cFilePath[ 64 ];
const BaseType_t xMaxFiles = 5;
uint32_t ulEventMask;
int32_t lBytesWritten, lFildes, lStatus;
int iByte;

	/* Save the current transaction point settings. */
	lStatus = red_gettransmask( fsVOLUME_NAME, &ulEventMask );
	configASSERT( lStatus == 0 );

	/* Disable automatic transaction points so that all of the files can be
	created in one atomic operation. */
	lStatus = red_settransmask( fsVOLUME_NAME, RED_TRANSACT_MANUAL );
	configASSERT( lStatus == 0 );

	/* Create xMaxFiles files.  Each created file will be
	( xFileNumber * fsRAM_BUFFER_SIZE ) bytes in length, and filled
	with a different repeating character. */
	for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
	{
		/* Generate a file name. */
		sprintf( cFilePath, "/root%03d.txt", xFileNumber );

		/* Print out the file name and the directory into which the file is
		being written. */
		printf( "Creating file %s\r\n", cFilePath );

		/* Open the file, creating the file if it does not already exist. */
		lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );
		configASSERT( lFildes != -1 );

		/* Fill the RAM buffer with data that will be written to the file.  This
		is just a repeating ascii character that indicates the file number. */
		memset( cRAMBuffer, ( int ) ( '0' + xFileNumber ), fsRAM_BUFFER_SIZE );

		/* Write the RAM buffer to the opened file a number of times.  The
		number of times the RAM buffer is written to the file depends on the
		file number, so the length of each created file will be different. */
		for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
		{
			lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
			configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );
		}

		/* Close the file so another file can be created. */
		lStatus = red_close( lFildes );
		configASSERT( lStatus == 0 );
	}

	/* Commit a transaction point, atomically adding the set of files to the
	transacted state. */
	lStatus = red_transact( fsVOLUME_NAME );
	configASSERT( lStatus == 0 );

	/* Create a sub directory. */
	printf( "Creating directory %s\r\n", pcDirectory1 );

	lStatus = red_mkdir( pcDirectory1 );
	configASSERT( lStatus == 0 );

	/* Create a subdirectory in the new directory. */
	printf( "Creating directory %s\r\n", pcDirectory2 );

	lStatus = red_mkdir( pcDirectory2 );
	configASSERT( lStatus == 0 );

	/* Generate the file name. */
	sprintf( cFilePath, "%s/file.txt", pcDirectory2 );

	/* Print out the file name and the directory into which the file is being
	written. */
	printf( "Writing file %s\r\n", cFilePath );

	lFildes = red_open( cFilePath, RED_O_CREAT|RED_O_TRUNC|RED_O_WRONLY );

	/* Write the file.  It is filled with incrementing ascii characters starting
	from '0'. */
	for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
	{
		cRAMBuffer[ iByte ] = ( char ) ( ( int ) '0' + iByte );
	}

	lBytesWritten = red_write( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
	configASSERT( lBytesWritten == fsRAM_BUFFER_SIZE );

	/* Finished so close the file. */
	lStatus = red_close( lFildes );
	configASSERT( lStatus == 0 );

	/* Commit a transaction point, atomically adding the set of files and
	directories to the transacted state. */
	lStatus = red_transact( fsVOLUME_NAME );
	configASSERT( lStatus == 0 );

	/* Restore previous transaction point settings. */
	lStatus = red_settransmask( fsVOLUME_NAME, ulEventMask );
	configASSERT( lStatus == 0 );
}
/*-----------------------------------------------------------*/

static void prvVerifyDemoFiles( void )
{
BaseType_t xFileNumber, xReadNumber;
char cFilePath[ 64 ];
const BaseType_t xMaxFiles = 5;
long lChar;
int32_t lBytesRead, lFildes, lStatus;
int iByte;

	/* Read back the files that were created by prvCreateDemoFiles(). */
	for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
	{
		/* Generate the file name. */
		sprintf( cFilePath, "/root%03d.txt", xFileNumber );

		/* Print out the file name and the directory from which the file is
		being read. */
		printf( "Reading file %s\r\n", cFilePath );

		/* Open the file for reading. */
		lFildes = red_open( cFilePath, RED_O_RDONLY );
		configASSERT( lFildes != -1 );

		/* Read the file into the RAM buffer, checking the file contents are as
		expected.  The size of the file depends on the file number. */
		for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
		{
			/* Start with the RAM buffer clear. */
			memset( cRAMBuffer, 0x00, fsRAM_BUFFER_SIZE );

			lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
			configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );

			/* Check the RAM buffer is filled with the expected data.  Each
			file contains a different repeating ascii character that indicates
			the number of the file. */
			for( lChar = 0; lChar < fsRAM_BUFFER_SIZE; lChar++ )
			{
				configASSERT( cRAMBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
			}
		}

		/* Close the file. */
		lStatus = red_close( lFildes );
		configASSERT( lStatus == 0 );
	}

	/* Generate the file name. */
	sprintf( cFilePath, "%s/file.txt", pcDirectory2 );

	/* Print out the file name and the directory from which the file is being
	read. */
	printf( "Reading file %s\r\n", cFilePath );

	/* This time the file is opened for reading. */
	lFildes = red_open( cFilePath, RED_O_RDONLY );
	configASSERT( lFildes != -1 );

	/* Read the file. */
	lBytesRead = red_read( lFildes, cRAMBuffer, fsRAM_BUFFER_SIZE );
	configASSERT( lBytesRead == fsRAM_BUFFER_SIZE );

	/* Verify the file 1 byte at a time. */
	for( iByte = 0; iByte < fsRAM_BUFFER_SIZE; iByte++ )
	{
		configASSERT( cRAMBuffer[ iByte ] == ( char ) ( ( int ) '0' + iByte ) );
	}

	/* Finished so close the file. */
	lStatus = red_close( lFildes );
	configASSERT( lStatus == 0 );
}