1 /* This FileX test concentrates on the Fault-Tolerant data loop write operation.  */
2 /*
3 For FAT 12, 16, 32, one cluster size is 1024 bytes;
4 
5          1024        1024          1024       1024         1024         1024
6     |------------|------------|-----------|------------|------------|------------|
7     |            |  TEST.TXT  | TEST.TXT  |            |            |            |
8     |------------|------------|-----------|------------|------------|------------|
9 
10 Check loop write data operation:
11 Step1: Format and open the media;
12 Step2: Enable fault tolerant feature;
13 Step3: Create new file called "TEST.TXT";
14 Step4: Check the media available bytes;
15 Step5: Write 1500 bytes to "TEST.TXT"(two clusters);
16 Step6: Check the media available bytes;
17 Step7: Loop to write random size bytes to "TEST.TXT"(less than two clusters);
18 Step8: Check the media available bytes every write operation.
19 */
20 
21 #ifndef FX_STANDALONE_ENABLE
22 #include   "tx_api.h"
23 #endif
24 #include   "fx_api.h"
25 #include   "fx_system.h"
26 #include   "fx_fault_tolerant.h"
27 #include   <stdio.h>
28 #include   "fx_ram_driver_test.h"
29 extern void    test_control_return(UINT status);
30 void    filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory);
31 
32 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
33 
34 #define     DEMO_STACK_SIZE         4096
35 #define CACHE_SIZE                  2048
36 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
37 
38 
39 /* Define the ThreadX and FileX object control blocks...  */
40 
41 #ifndef FX_STANDALONE_ENABLE
42 static TX_THREAD                ftest_0;
43 #endif
44 static FX_MEDIA                 ram_disk;
45 static FX_FILE                  my_file;
46 static UCHAR                    *pointer;
47 
48 /* Define the counters used in the test application...  */
49 
50 #ifndef FX_STANDALONE_ENABLE
51 static UCHAR                    *cache_buffer;
52 static UCHAR                    *fault_tolerant_buffer;
53 #else
54 static UCHAR                    cache_buffer[CACHE_SIZE];
55 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
56 #endif
57 static CHAR                     write_buffer[2048];
58 static UINT                     write_buffer_size = 2048;
59 static UINT                     write_index;
60 static CHAR                     read_buffer[4096];
61 static UINT                     read_buffer_size = 4096;
62 static UINT                     i;
63 
64 #define TEST_COUNT              3            /* FAT12, 16, 32.  */
65 
66 /* Define thread prototypes.  */
67 
68 static void    ftest_0_entry(ULONG thread_input);
69 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
70 extern void    test_control_return(UINT status);
71 extern UINT    (*driver_write_callback)(FX_MEDIA *media_ptr, UINT sector_type, UCHAR *block_ptr, UINT *operation_ptr);
72 
73 
74 /* Define what the initial system looks like.  */
75 
76 #ifdef CTEST
test_application_define(void * first_unused_memory)77 void test_application_define(void *first_unused_memory)
78 #else
79 void    filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory)
80 #endif
81 {
82 
83 
84 #ifndef FX_STANDALONE_ENABLE
85     /* Setup the working pointer.  */
86     pointer =  (UCHAR *) first_unused_memory;
87 
88     /* Create the main thread.  */
89 
90     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
91             pointer, DEMO_STACK_SIZE,
92             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
93 
94     pointer =  pointer + DEMO_STACK_SIZE;
95 
96     /* Setup memory for the RAM disk and the sector cache.  */
97     cache_buffer =  pointer;
98     pointer += CACHE_SIZE;
99     fault_tolerant_buffer = pointer;
100     pointer += FAULT_TOLERANT_SIZE;
101 #endif
102 
103     /* Initialize the FileX system.  */
104     fx_system_initialize();
105 #ifdef FX_STANDALONE_ENABLE
106     ftest_0_entry(0);
107 #endif
108 }
109 
110 
111 /* Define the test threads.  */
112 
ftest_0_entry(ULONG thread_input)113 static void    ftest_0_entry(ULONG thread_input)
114 {
115 
116 UINT        status;
117 ULONG       actual;
118 UINT        loop_count;
119 UINT        seek_index;
120 UINT        write_size;
121 UINT        two_cluster_size;
122 ULONG       file_size;
123 ULONG       current_available_bytes;
124 ULONG       available_bytes;
125 
126     FX_PARAMETER_NOT_USED(thread_input);
127 
128     /* Print out some test information banners.  */
129     printf("FileX Test:   Fault Tolerant Loop Write DATA Test....................");
130 
131     /* Random genearte the write data.  */
132     for (write_index = 0; write_index < write_buffer_size; write_index ++)
133         write_buffer[write_index] = (CHAR)rand();
134 
135     /* Loop to test FAT 12, 16, 32.   */
136     for (i = 0; i < TEST_COUNT; i ++)
137     {
138         if (i == 0)
139         {
140             /* Format the media with FAT12.  This needs to be done before opening it!  */
141             status =  fx_media_format(&ram_disk,
142                                      _fx_ram_driver,         // Driver entry
143                                      ram_disk_memory_large,  // RAM disk memory pointer
144                                      cache_buffer,           // Media buffer pointer
145                                      CACHE_SIZE,             // Media buffer size
146                                      "MY_RAM_DISK",          // Volume Name
147                                      1,                      // Number of FATs
148                                      32,                     // Directory Entries
149                                      0,                      // Hidden sectors
150                                      256,                    // Total sectors
151                                      256,                    // Sector size
152                                      8,                      // Sectors per cluster
153                                      1,                      // Heads
154                                      1);                     // Sectors per track
155         }
156         else if (i == 1)
157         {
158             /* Format the media with FAT16.  This needs to be done before opening it!  */
159             status =  fx_media_format(&ram_disk,
160                                      _fx_ram_driver,         // Driver entry
161                                      ram_disk_memory_large,  // RAM disk memory pointer
162                                      cache_buffer,           // Media buffer pointer
163                                      CACHE_SIZE,             // Media buffer size
164                                      "MY_RAM_DISK",          // Volume Name
165                                      1,                      // Number of FATs
166                                      32,                     // Directory Entries
167                                      0,                      // Hidden sectors
168                                      4200 * 8,               // Total sectors
169                                      256,                    // Sector size
170                                      8,                      // Sectors per cluster
171                                      1,                      // Heads
172                                      1);                     // Sectors per track
173         }
174         else if (i == 2)
175         {
176             /* Format the media with FAT32.  This needs to be done before opening it!  */
177             status =  fx_media_format(&ram_disk,
178                                      _fx_ram_driver,         // Driver entry
179                                      ram_disk_memory_large,  // RAM disk memory pointer
180                                      cache_buffer,           // Media buffer pointer
181                                      CACHE_SIZE,             // Media buffer size
182                                      "MY_RAM_DISK",          // Volume Name
183                                      1,                      // Number of FATs
184                                      32,                     // Directory Entries
185                                      0,                      // Hidden sectors
186                                      70000 * 8,              // Total sectors
187                                      256,                    // Sector size
188                                      8,                      // Sectors per cluster
189                                      1,                      // Heads
190                                      1);                     // Sectors per track
191         }
192 
193         /* Determine if the format had an error.  */
194         if (status)
195         {
196 
197             printf("ERROR!\n");
198             test_control_return(1);
199         }
200 
201         /* Initialize the parmeters.  */
202         file_size = 1500;
203         two_cluster_size = 2048;
204 
205         /* Open the ram_disk.  */
206         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
207 
208         /* Check the status.  */
209         if (status != FX_SUCCESS)
210         {
211 
212             /* Error, return error code.  */
213             printf("ERROR!\n");
214             test_control_return(2);
215         }
216 
217         /* Enable the Fault-tolerant feature.  */
218         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
219 
220         /* Check status.   */
221         if (status)
222         {
223 
224             printf("ERROR!\n");
225             test_control_return(3);
226         }
227 
228         /* Pickup the available bytes in the media.  */
229         status =  fx_media_space_available(&ram_disk, &available_bytes);
230 
231         /* Check for available bytes error.  */
232         if (status != FX_SUCCESS)
233         {
234 
235             printf("ERROR!\n");
236             test_control_return(4);
237         }
238 
239         /* Create a file called TEST.TXT in the root directory.  */
240         status =  fx_file_create(&ram_disk, "TEST.TXT");
241 
242         /* Check the create status.  */
243         if (status != FX_SUCCESS)
244         {
245 
246             printf("ERROR!\n");
247             test_control_return(5);
248         }
249 
250         /* Open the test file.  */
251         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
252 
253         /* Check the file open status.  */
254         if (status != FX_SUCCESS)
255         {
256 
257             printf("ERROR!\n");
258             test_control_return(6);
259         }
260 
261         /* Write a string to the test file, (two cluster).  */
262         status =  fx_file_write(&my_file, (void *) write_buffer, file_size);
263 
264         /* Check the file write status.  */
265         if (status != FX_SUCCESS)
266         {
267 
268             printf("ERROR!\n");
269             test_control_return(7);
270         }
271 
272         /* Pickup the available bytes in the media.  */
273         status =  fx_media_space_available(&ram_disk, &current_available_bytes);
274 
275         /* Check for available bytes error.  */
276         if ((status != FX_SUCCESS) && (available_bytes != (current_available_bytes + two_cluster_size * 2)))
277         {
278 
279             printf("ERROR!\n");
280             test_control_return(8);
281         }
282 
283         /* Update the avalibale_bytes.  */
284         available_bytes = current_available_bytes;
285 
286         /* Loop to write the data to test file.  */
287         for (loop_count = 0; loop_count < 1000; loop_count ++)
288         {
289 
290             /* Random generate the seek index (0~file_size).  */
291             seek_index = (ULONG)rand() % (file_size + 1);
292 
293             /* Seek to the beginning of the test file.  */
294             status =  fx_file_seek(&my_file, seek_index);
295 
296             /* Check the file seek status.  */
297             if (status != FX_SUCCESS)
298             {
299 
300                 printf("ERROR!\n");
301                 test_control_return(9);
302             }
303 
304             /* Random generate the write size, (0 ~ two cluster size - seek_index);  */
305             write_size = (UINT)rand() % (two_cluster_size - seek_index + 1);
306 
307             /* Write a string to the test file.  */
308             status =  fx_file_write(&my_file, (void *) write_buffer, write_size);
309 
310             /* Check the file write status.  */
311             if (status != FX_SUCCESS)
312             {
313 
314                 printf("ERROR!\n");
315                 test_control_return(10);
316             }
317 
318             /* Update the file_size.  */
319             if (seek_index + write_size > file_size)
320                 file_size = seek_index + write_size;
321 
322             /* Seek to the beginning of the test file.  */
323             status =  fx_file_seek(&my_file, 0);
324 
325             /* Check the file seek status.  */
326             if (status != FX_SUCCESS)
327             {
328 
329                 printf("ERROR!\n");
330                 test_control_return(11);
331             }
332 
333             /* Read the bytes of the test file.  */
334             status =  fx_file_read(&my_file, read_buffer, read_buffer_size, &actual);
335 
336             /* Check the file read status.  */
337             if ((status != FX_SUCCESS) || (actual != file_size))
338             {
339 
340                 printf("ERROR!\n");
341                 test_control_return(12);
342             }
343 
344             /* Pickup the available bytes in the media.  */
345             status =  fx_media_space_available(&ram_disk, &available_bytes);
346 
347             /* Check for available bytes error.  */
348             if ((status != FX_SUCCESS) || (available_bytes != current_available_bytes))
349             {
350 
351                 printf("ERROR!\n");
352                 test_control_return(13);
353             }
354         }
355 
356         /* Close the test file.  */
357         status =  fx_file_close(&my_file);
358 
359         /* Check the file close status.  */
360         if (status != FX_SUCCESS)
361         {
362 
363             printf("ERROR!\n");
364             test_control_return(14);
365         }
366 
367         /* Close the media.  */
368         status =  fx_media_close(&ram_disk);
369 
370         /* Determine if the test was successful.  */
371         if (status != FX_SUCCESS)
372         {
373 
374             printf("ERROR!\n");
375             test_control_return(15);
376         }
377     }
378 
379     /* Output successful.  */
380     printf("SUCCESS!\n");
381     test_control_return(0);
382 }
383 
384 #else
385 
386 #ifdef CTEST
test_application_define(void * first_unused_memory)387 void test_application_define(void *first_unused_memory)
388 #else
389 void    filex_fault_tolerant_loop_write_data_test_application_define(void *first_unused_memory)
390 #endif
391 {
392 
393     FX_PARAMETER_NOT_USED(first_unused_memory);
394 
395     /* Print out some test information banners.  */
396     printf("FileX Test:   Fault Tolerant Loop Write DATA Test....................N/A\n");
397 
398     test_control_return(255);
399 }
400 #endif
401 
402