1 /* This FileX test concentrates on the file write and file notify operation. */
2
3 #ifndef FX_STANDALONE_ENABLE
4 #include "tx_api.h"
5 #endif
6 #include "fx_api.h"
7 #include <stdio.h>
8 #include "fx_ram_driver_test.h"
9
10 #define DEMO_STACK_SIZE 4096
11 #define CACHE_SIZE 128*128
12
13
14 /* Define the ThreadX and FileX object control blocks... */
15
16 #ifndef FX_STANDALONE_ENABLE
17 static TX_THREAD ftest_0;
18 #endif
19 static FX_MEDIA ram_disk;
20
21 /* Define the counters used in the test application... */
22
23 #ifndef FX_STANDALONE_ENABLE
24 static UCHAR *ram_disk_memory;
25 static UCHAR *cache_buffer;
26 #else
27 static UCHAR cache_buffer[CACHE_SIZE];
28 #endif
29 static UCHAR write_data[65535];
30 static UCHAR read_data[65535];
31 static ULONG notify_counter = 0;
32
33
34 /* Define thread prototypes. */
35
36 void filex_file_write_notify_application_define(void *first_unused_memory);
37 static void ftest_0_entry(ULONG thread_input);
38
39 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
40 void test_control_return(UINT status);
41 static VOID file_write_notify(FX_FILE *file_ptr);
42
43
44
45 /* Define what the initial system looks like. */
46
47 #ifdef CTEST
test_application_define(void * first_unused_memory)48 void test_application_define(void *first_unused_memory)
49 #else
50 void filex_file_write_notify_application_define(void *first_unused_memory)
51 #endif
52 {
53
54 #ifndef FX_STANDALONE_ENABLE
55 UCHAR *pointer;
56
57
58 /* Setup the working pointer. */
59 pointer = (UCHAR *) first_unused_memory;
60
61 /* Create the main thread. */
62 tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
63 pointer, DEMO_STACK_SIZE,
64 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
65
66 pointer = pointer + DEMO_STACK_SIZE;
67
68 /* Setup memory for the RAM disk and the sector cache. */
69 cache_buffer = pointer;
70 pointer = pointer + CACHE_SIZE;
71 ram_disk_memory = pointer;
72
73 #endif
74
75 /* Initialize the FileX system. */
76 fx_system_initialize();
77 #ifdef FX_STANDALONE_ENABLE
78 ftest_0_entry(0);
79 #endif
80 }
81
82
83
84 /* Define the test threads. */
85
ftest_0_entry(ULONG thread_input)86 static void ftest_0_entry(ULONG thread_input)
87 {
88
89 UINT status;
90 ULONG actual;
91 ULONG i;
92 FX_FILE my_file;
93
94 FX_PARAMETER_NOT_USED(thread_input);
95
96 /* Print out some test information banners. */
97 printf("FileX Test: File write notify test.................................");
98
99 /* Format the media. This needs to be done before opening it! */
100 status = fx_media_format(&ram_disk,
101 _fx_ram_driver, // Driver entry
102 ram_disk_memory, // RAM disk memory pointer
103 cache_buffer, // Media buffer pointer
104 CACHE_SIZE, // Media buffer size
105 "MY_RAM_DISK", // Volume Name
106 1, // Number of FATs
107 32, // Directory Entries
108 0, // Hidden sectors
109 512, // Total sectors
110 512, // Sector size
111 8, // Sectors per cluster
112 1, // Heads
113 1); // Sectors per track
114
115 /* Determine if the format had an error. */
116 if (status)
117 {
118
119 printf("ERROR!\n");
120 test_control_return(1);
121 }
122
123 /* Open the ram_disk. */
124 status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
125
126 /* Check the status. */
127 if (status != FX_SUCCESS)
128 {
129
130 /* Error, return error code. */
131 printf("ERROR!\n");
132 test_control_return(2);
133 }
134
135 /* Create a file called TEST.TXT in the root directory. */
136 status = fx_file_create(&ram_disk, "TEST.TXT");
137
138 /* Check the create status. */
139 if (status != FX_SUCCESS)
140 {
141
142 printf("ERROR!\n");
143 test_control_return(3);
144 }
145
146 /* Open the test file. */
147 status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
148
149 /* Check the file open status. */
150 if (status != FX_SUCCESS)
151 {
152
153 printf("ERROR!\n");
154 test_control_return(4);
155 }
156
157 /* Initialize the data for write and read. */
158 for (i = 0; i < sizeof(write_data); i++)
159 {
160 write_data[i] = (UCHAR)i;
161 read_data[i] = 0;
162 }
163
164 /* Write 4116 bytes to the file. */
165 status = fx_file_write(&my_file, write_data, 4116);
166
167 /* Check the file write status. */
168 if (status != FX_SUCCESS)
169 {
170
171 printf("ERROR!\n");
172 test_control_return(5);
173 }
174
175 /* Check the notify counter. */
176 if (notify_counter != 0)
177 {
178
179 printf("ERROR!\n");
180 test_control_return(6);
181 }
182
183 /* Setup write notify callback. */
184 status = fx_file_write_notify_set(&my_file, file_write_notify);
185
186 /* Check the file open status. */
187 if (status != FX_SUCCESS)
188 {
189
190 printf("ERROR!\n");
191 test_control_return(7);
192 }
193
194 /* Write 4116 bytes to the file. */
195 status = fx_file_write(&my_file, write_data + 4116, 4116);
196
197 /* Check the file write status. */
198 if (status != FX_SUCCESS)
199 {
200
201 printf("ERROR!\n");
202 test_control_return(8);
203 }
204
205 /* Check the notify counter. */
206 if (notify_counter != 1)
207 {
208
209 printf("ERROR!\n");
210 test_control_return(9);
211 }
212
213 /* Seek to the beginning of the test file. */
214 status = fx_file_seek(&my_file, 0);
215
216 /* Check the file seek status. */
217 if (status != FX_SUCCESS)
218 {
219
220 printf("ERROR!\n");
221 test_control_return(10);
222 }
223
224 /* Read as much as 4 sectors full of bytes from the file. */
225 status = fx_file_read(&my_file, (void *)read_data, sizeof(read_data), &actual);
226
227 /* Check the file read status. */
228 if ((status != FX_SUCCESS) || (actual != 4116 * 2))
229 {
230
231 printf("ERROR!\n");
232 test_control_return(11);
233 }
234
235 /* Close the file. */
236 status = fx_file_close(&my_file);
237
238 /* Check the file open status. */
239 if (status != FX_SUCCESS)
240 {
241
242 printf("ERROR!\n");
243 test_control_return(12);
244 }
245 else
246 {
247
248 printf("SUCCESS!\n");
249 test_control_return(0);
250 }
251 }
252
file_write_notify(FX_FILE * file_ptr)253 static VOID file_write_notify(FX_FILE *file_ptr)
254 {
255
256 FX_PARAMETER_NOT_USED(file_ptr);
257
258 /* Update the notify counter. */
259 notify_counter++;
260 }
261