1 /* This FileX test concentrates on the media cache invalidate 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 void  test_control_return(UINT status);
11 
12 #ifndef FX_DISABLE_CACHE
13 #define     DEMO_STACK_SIZE         4096
14 #define     CACHE_SIZE              16*128
15 
16 
17 /* Define the ThreadX and FileX object control blocks...  */
18 
19 #ifndef FX_STANDALONE_ENABLE
20 static TX_THREAD               ftest_0;
21 #endif
22 static FX_MEDIA                ram_disk;
23 static FX_FILE                 my_file;
24 
25 
26 /* Define the counters used in the test application...  */
27 
28 #ifndef FX_STANDALONE_ENABLE
29 static UCHAR                  *ram_disk_memory;
30 static UCHAR                  *cache_buffer;
31 #else
32 static UCHAR                   cache_buffer[CACHE_SIZE];
33 #endif
34 
35 
36 /* Define thread prototypes.  */
37 
38 void    filex_media_cache_invalidate_application_define(void *first_unused_memory);
39 static void    ftest_0_entry(ULONG thread_input);
40 
41 VOID  _fx_ram_driver(FX_MEDIA *media_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_media_cache_invalidate_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       read_value;
92 ULONG       write_value;
93 ULONG       available_bytes;
94 ULONG       i;
95 
96     FX_PARAMETER_NOT_USED(thread_input);
97 
98     /* Print out some test information banners.  */
99     printf("FileX Test:   Media cache invalidate test............................");
100 
101     /* Format the media.  This needs to be done before opening it!  */
102     status =  fx_media_format(&ram_disk,
103                             _fx_ram_driver,         // Driver entry
104                             ram_disk_memory,        // RAM disk memory pointer
105                             cache_buffer,           // Media buffer pointer
106                             CACHE_SIZE,             // Media buffer size
107                             "MY_RAM_DISK",          // Volume Name
108                             1,                      // Number of FATs
109                             32,                     // Directory Entries
110                             0,                      // Hidden sectors
111                             256,                    // Total sectors
112                             128,                    // Sector size
113                             1,                      // Sectors per cluster
114                             1,                      // Heads
115                             1);                     // Sectors per track
116 
117     /* Determine if the format had an error.  */
118     if (status)
119     {
120 
121         printf("ERROR!\n");
122         test_control_return(2);
123     }
124 
125     /* Open the ram_disk.  */
126     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
127 
128     /* Check the status.  */
129     if (status != FX_SUCCESS)
130     {
131 
132         /* Error, return error code.  */
133         printf("ERROR!\n");
134         test_control_return(21);
135     }
136 
137 #ifndef FX_DISABLE_ERROR_CHECKING
138 
139     /* send a null pointer to generate an error */
140     status =  fx_media_cache_invalidate(FX_NULL);
141     if (status != FX_PTR_ERROR)
142     {
143         printf("ERROR!\n");
144         test_control_return(8);
145     }
146 #endif
147 
148     /* Create a file called TEST.TXT in the root directory.  */
149     status =  fx_file_create(&ram_disk, "TEST.TXT");
150 
151     /* Check the create status.  */
152     if (status != FX_SUCCESS)
153     {
154 
155         printf("ERROR!\n");
156         test_control_return(3);
157     }
158 
159     /* Open the test file.  */
160     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
161 
162     /* Check the file open status.  */
163     if (status != FX_SUCCESS)
164     {
165 
166         printf("ERROR!\n");
167         test_control_return(4);
168     }
169 
170     /* Pickup the available bytes in the media.  */
171     status =  fx_media_space_available(&ram_disk, &available_bytes);
172 
173     /* Check for available bytes error.  */
174     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
175     {
176 
177         printf("ERROR!\n");
178         test_control_return(5);
179     }
180 
181     /* Loop to write successive bytes out to the file.... to fill the media!  */
182     i =  0;
183     write_value =  0;
184     while (i < available_bytes)
185     {
186 
187         /* Write 4 bytes to the file.  */
188         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
189 
190         /* Check the file write status.  */
191         if (status != FX_SUCCESS)
192         {
193 
194             printf("ERROR!\n");
195             test_control_return(6);
196         }
197 
198         /* Increment byte count.  */
199         i =  i + sizeof(ULONG);
200 
201         /* Increment write value.  */
202         write_value++;
203     }
204 
205     /* Pickup the available bytes in the media again.  */
206     status =  fx_media_space_available(&ram_disk, &i);
207 
208     /* Check for available bytes error.  */
209     if ((status != FX_SUCCESS) || (i != 0))
210     {
211 
212         printf("ERROR!\n");
213         test_control_return(7);
214     }
215 
216     /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
217        dirty sectors are written.  */
218     status =  fx_media_cache_invalidate(&ram_disk);
219 
220     /* Check for flush errors.  */
221     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
222     {
223 
224         printf("ERROR!\n");
225         test_control_return(8);
226     }
227 
228     /* See if any sectors are still valid in the cache.  */
229     for (i = 0; i < FX_MAX_SECTOR_CACHE; i++)
230     {
231 
232         /* Determine if this cache entry is still valid.  */
233         if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
234         {
235 
236             printf("ERROR!\n");
237             test_control_return(81);
238         }
239     }
240 
241     /* Seek to the beginning of the test file.  */
242     status =  fx_file_seek(&my_file, 0);
243 
244     /* Check the file seek status.  */
245     if (status != FX_SUCCESS)
246     {
247 
248         printf("ERROR!\n");
249         test_control_return(9);
250     }
251 
252     /* Now read in all the bytes again to make sure the file contents are really there.  */
253     i =  0;
254     read_value =  0;
255     while (i < available_bytes)
256     {
257 
258         /* Read 4 bytes from the file.  */
259         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
260 
261         /* Check the file read status.  */
262         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
263         {
264 
265             printf("ERROR!\n");
266             test_control_return(10);
267         }
268 
269         /* Increment byte count.  */
270         i =  i + sizeof(ULONG);
271     }
272 
273     /* Close the test file.  */
274     status =  fx_file_close(&my_file);
275 
276     /* Check the file close status.  */
277     if (status != FX_SUCCESS)
278     {
279 
280         printf("ERROR!\n");
281         test_control_return(11);
282     }
283 
284     /* Close the media.  */
285     status =  fx_media_close(&ram_disk);
286 
287     /* Check the media close status.  */
288     if (status != FX_SUCCESS)
289     {
290 
291         printf("ERROR!\n");
292         test_control_return(12);
293     }
294 
295     /* Reformat the media.  This needs to be done before opening it!  */
296     status =  fx_media_format(&ram_disk,
297                             _fx_ram_driver,         // Driver entry
298                             ram_disk_memory,        // RAM disk memory pointer
299                             cache_buffer,           // Media buffer pointer
300                             CACHE_SIZE,             // Media buffer size
301                             "MY_RAM_DISK",          // Volume Name
302                             1,                      // Number of FATs
303                             32,                     // Directory Entries
304                             0,                      // Hidden sectors
305                             256,                    // Total sectors
306                             128,                    // Sector size
307                             1,                      // Sectors per cluster
308                             1,                      // Heads
309                             1);                     // Sectors per track
310 
311     /* Determine if the format had an error.  */
312     if (status)
313     {
314 
315         printf("ERROR!\n");
316         test_control_return(13);
317     }
318 
319     /* Open the ram_disk, but do so to ensure non-hashed algorithm is used by supplying CACHE_SIZE-1.  */
320     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
321 
322     /* Check the status.  */
323     if (status != FX_SUCCESS)
324     {
325 
326         /* Error, return error code.  */
327         printf("ERROR!\n");
328         test_control_return(14);
329     }
330 
331     /* Create a file called TEST.TXT in the root directory.  */
332     status =  fx_file_create(&ram_disk, "TEST.TXT");
333 
334     /* Check the create status.  */
335     if (status != FX_SUCCESS)
336     {
337 
338         printf("ERROR!\n");
339         test_control_return(15);
340     }
341 
342     /* Open the test file.  */
343     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
344 
345     /* Check the file open status.  */
346     if (status != FX_SUCCESS)
347     {
348 
349         printf("ERROR!\n");
350         test_control_return(16);
351     }
352 
353     /* Pickup the available bytes in the media.  */
354     status =  fx_media_space_available(&ram_disk, &available_bytes);
355 
356     /* Check for available bytes error.  */
357     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
358     {
359 
360         printf("ERROR!\n");
361         test_control_return(17);
362     }
363 
364     /* Loop to write successive bytes out to the file.... to fill the media!  */
365     i =  0;
366     write_value =  0;
367     while (i < available_bytes)
368     {
369 
370         /* Write 4 bytes to the file.  */
371         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
372 
373         /* Check the file write status.  */
374         if (status != FX_SUCCESS)
375         {
376 
377             printf("ERROR!\n");
378             test_control_return(18);
379         }
380 
381         /* Increment byte count.  */
382         i =  i + sizeof(ULONG);
383 
384         /* Increment write value.  */
385         write_value++;
386     }
387 
388     /* Pickup the available bytes in the media again.  */
389     status =  fx_media_space_available(&ram_disk, &i);
390 
391     /* Check for available bytes error.  */
392     if ((status != FX_SUCCESS) || (i != 0))
393     {
394 
395         printf("ERROR!\n");
396         test_control_return(19);
397     }
398 
399     /* At this point, we should invalidate the media to ensure that all
400        dirty sectors are written.  */
401     status =  fx_media_cache_invalidate(&ram_disk);
402 
403     /* Check for flush errors.  */
404     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
405     {
406 
407         printf("ERROR!\n");
408         test_control_return(20);
409     }
410 
411     /* See if any sectors are still valid in the cache.  */
412     for (i = 0; i < ram_disk.fx_media_sector_cache_size; i++)
413     {
414 
415         /* Determine if this cache entry is still valid.  */
416         if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
417         {
418 
419             printf("ERROR!\n");
420             test_control_return(81);
421         }
422     }
423 
424     /* Seek to the beginning of the test file.  */
425     status =  fx_file_seek(&my_file, 0);
426 
427     /* Check the file seek status.  */
428     if (status != FX_SUCCESS)
429     {
430 
431         printf("ERROR!\n");
432         test_control_return(22);
433     }
434 
435     /* Now read in all the bytes again to make sure the file contents are really there.  */
436     i =  0;
437     read_value =  0;
438     while (i < available_bytes)
439     {
440 
441         /* Read 4 bytes from the file.  */
442         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
443 
444         /* Check the file read status.  */
445         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
446         {
447 
448             printf("ERROR!\n");
449             test_control_return(23);
450         }
451 
452         /* Increment byte count.  */
453         i =  i + sizeof(ULONG);
454     }
455 
456     /* Close the test file.  */
457     status =  fx_file_close(&my_file);
458 
459     /* Check the file close status.  */
460     if (status != FX_SUCCESS)
461     {
462 
463         printf("ERROR!\n");
464         test_control_return(24);
465     }
466 
467     /* Close the media.  */
468     status =  fx_media_close(&ram_disk);
469 
470     /* Check the media close status.  */
471     if (status != FX_SUCCESS)
472     {
473 
474         printf("ERROR!\n");
475         test_control_return(25);
476     }
477 
478     /* Determine if the test was successful.  */
479     if (status != FX_SUCCESS)
480     {
481 
482         printf("ERROR!\n");
483         test_control_return(26);
484     }
485     else
486     {
487 
488         printf("SUCCESS!\n");
489         test_control_return(0);
490     }
491 }
492 
493 
494 #else
495 #ifdef CTEST
test_application_define(void * first_unused_memory)496 void test_application_define(void *first_unused_memory)
497 #else
498 void    filex_media_cache_invalidate_application_define(void *first_unused_memory)
499 #endif
500 {
501 
502     FX_PARAMETER_NOT_USED(first_unused_memory);
503 
504     /* Print out some test information banners.  */
505     printf("FileX Test:   Media cache invalidate test............................N/A\n");
506 
507     test_control_return(255);
508 }
509 #endif
510