filex_fault_tolerant_write_large_data_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* This FileX test concentrates on the Fault-Tolerant write large data test. */
  2. /*
  3. For FAT 12, 16, 32, one cluster size is 1024 bytes;
  4. Check media full operation:
  5. Step1: Format and open the media;
  6. Step2: Enable fault tolerant feature;
  7. Step3: Create new file called "TEST.TXT";
  8. Step4: Get the media available bytes;
  9. Step5: Write large bytes out of the file to fill the media;
  10. Step6: Check the media available bytes.
  11. Step7: Loop to read and check the file data from the media;
  12. */
  13. #ifndef FX_STANDALONE_ENABLE
  14. #include "tx_api.h"
  15. #endif
  16. #include "fx_api.h"
  17. #include "fx_fault_tolerant.h"
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include "fx_ram_driver_test.h"
  21. extern void test_control_return(UINT status);
  22. void filex_fault_tolerant_write_large_data_test_application_define(void *first_unused_memory);
  23. #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT)
  24. #define DEMO_STACK_SIZE 4096
  25. #define CACHE_SIZE 2048
  26. #define FAULT_TOLERANT_SIZE FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
  27. /* Define the ThreadX and FileX object control blocks... */
  28. #ifndef FX_STANDALONE_ENABLE
  29. static TX_THREAD ftest_0;
  30. #endif
  31. static FX_MEDIA ram_disk;
  32. static FX_FILE my_file;
  33. static UCHAR *pointer;
  34. /* Define the counters used in the test application... */
  35. #ifndef FX_STANDALONE_ENABLE
  36. static UCHAR *cache_buffer;
  37. static UCHAR *fault_tolerant_buffer;
  38. #else
  39. static UCHAR cache_buffer[CACHE_SIZE];
  40. static UCHAR fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
  41. #endif
  42. #define TEST_COUNT 3
  43. /* Define thread prototypes. */
  44. static void ftest_0_entry(ULONG thread_input);
  45. extern void _fx_ram_driver(FX_MEDIA *media_ptr);
  46. extern void test_control_return(UINT status);
  47. /* Define what the initial system looks like. */
  48. #ifdef CTEST
  49. void test_application_define(void *first_unused_memory)
  50. #else
  51. void filex_fault_tolerant_write_large_data_test_application_define(void *first_unused_memory)
  52. #endif
  53. {
  54. #ifndef FX_STANDALONE_ENABLE
  55. /* Setup the working pointer. */
  56. pointer = (UCHAR *) first_unused_memory;
  57. /* Create the main thread. */
  58. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  59. pointer, DEMO_STACK_SIZE,
  60. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  61. pointer = pointer + DEMO_STACK_SIZE;
  62. /* Setup memory for the RAM disk and the sector cache. */
  63. cache_buffer = pointer;
  64. pointer += CACHE_SIZE;
  65. fault_tolerant_buffer = pointer;
  66. pointer += FAULT_TOLERANT_SIZE;
  67. #endif
  68. /* Initialize the FileX system. */
  69. fx_system_initialize();
  70. #ifdef FX_STANDALONE_ENABLE
  71. ftest_0_entry(0);
  72. #endif
  73. }
  74. /* Define the test threads. */
  75. static void ftest_0_entry(ULONG thread_input)
  76. {
  77. UINT status;
  78. ULONG actual;
  79. ULONG available_bytes;
  80. ULONG i, j;
  81. ULONG data_value;
  82. FX_PARAMETER_NOT_USED(thread_input);
  83. /* Print out some test information banners. */
  84. printf("FileX Test: Fault Tolerant Write Large Data Test...................");
  85. /* Generate a random number for write data. */
  86. srand((ULONG)time(NULL));
  87. data_value = (ULONG)(rand() & 0xFF) | (ULONG)((rand() & 0xFF) << 8) | (ULONG)((rand() & 0xFF) << 16) | (ULONG)((rand() & 0xFF) << 24);
  88. /* Genearte the write data. */
  89. for (j = 0; j < large_data_buffer_size / sizeof(ULONG); j ++)
  90. {
  91. ((ULONG*)large_data_buffer)[j] = data_value++;
  92. }
  93. /* Roll back the value for later verification use. */
  94. data_value -= (large_data_buffer_size / sizeof(ULONG));
  95. /* Loop to test FAT 12, 16, 32. */
  96. for (i = 0; i < TEST_COUNT; i ++)
  97. {
  98. if (i == 0)
  99. {
  100. /* Format the media with FAT12. This needs to be done before opening it! */
  101. status = fx_media_format(&ram_disk,
  102. _fx_ram_driver, // Driver entry
  103. ram_disk_memory_large, // RAM disk memory pointer
  104. cache_buffer, // Media buffer pointer
  105. CACHE_SIZE, // Media buffer size
  106. "MY_RAM_DISK", // Volume Name
  107. 1, // Number of FATs
  108. 32, // Directory Entries
  109. 0, // Hidden sectors
  110. 4000 * 8, // Total sectors
  111. 256, // Sector size
  112. 8, // Sectors per cluster
  113. 1, // Heads
  114. 1); // Sectors per track
  115. }
  116. else if (i == 1)
  117. {
  118. /* Format the media with FAT16. This needs to be done before opening it! */
  119. status = fx_media_format(&ram_disk,
  120. _fx_ram_driver, // Driver entry
  121. ram_disk_memory_large, // RAM disk memory pointer
  122. cache_buffer, // Media buffer pointer
  123. CACHE_SIZE, // Media buffer size
  124. "MY_RAM_DISK", // Volume Name
  125. 1, // Number of FATs
  126. 32, // Directory Entries
  127. 0, // Hidden sectors
  128. 60000 * 8, // Total sectors
  129. 256, // Sector size
  130. 8, // Sectors per cluster
  131. 1, // Heads
  132. 1); // Sectors per track
  133. }
  134. else if (i == 2)
  135. {
  136. /* Format the media with FAT32. This needs to be done before opening it! */
  137. status = fx_media_format(&ram_disk,
  138. _fx_ram_driver, // Driver entry
  139. ram_disk_memory_large, // RAM disk memory pointer
  140. cache_buffer, // Media buffer pointer
  141. CACHE_SIZE, // Media buffer size
  142. "MY_RAM_DISK", // Volume Name
  143. 1, // Number of FATs
  144. 32, // Directory Entries
  145. 0, // Hidden sectors
  146. 400000 * 8, // Total sectors
  147. 256, // Sector size
  148. 8, // Sectors per cluster
  149. 1, // Heads
  150. 1); // Sectors per track
  151. }
  152. /* Determine if the format had an error. */
  153. if (status)
  154. {
  155. printf("ERROR!\n");
  156. test_control_return(1);
  157. }
  158. /* Open the ram_disk. */
  159. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
  160. /* Check the status. */
  161. if (status != FX_SUCCESS)
  162. {
  163. /* Error, return error code. */
  164. printf("ERROR!\n");
  165. test_control_return(2);
  166. }
  167. /* Enable the Fault-tolerant feature. */
  168. status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
  169. /* Check status. */
  170. if (status)
  171. {
  172. printf("ERROR!\n");
  173. test_control_return(3);
  174. }
  175. /* Create a file called TEST.TXT in the root directory. */
  176. status = fx_file_create(&ram_disk, "TEST.TXT");
  177. /* Check the create status. */
  178. if (status != FX_SUCCESS)
  179. {
  180. printf("ERROR!\n");
  181. test_control_return(4);
  182. }
  183. /* Open the test file. */
  184. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  185. /* Check the file open status. */
  186. if (status != FX_SUCCESS)
  187. {
  188. printf("ERROR!\n");
  189. test_control_return(5);
  190. }
  191. /* Pickup the available bytes in the media. */
  192. status = fx_media_space_available(&ram_disk, &available_bytes);
  193. /* Check for available bytes error. */
  194. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  195. {
  196. printf("ERROR!\n");
  197. test_control_return(6);
  198. }
  199. /* Write the data to fill the media one time. */
  200. status = fx_file_write(&my_file, (void *) large_data_buffer, available_bytes);
  201. /* Check the file write status. */
  202. if (status != FX_SUCCESS)
  203. {
  204. printf("ERROR!\n");
  205. test_control_return(7);
  206. }
  207. /* Pickup the available bytes in the media again. */
  208. status = fx_media_space_available(&ram_disk, &j);
  209. /* Check for available bytes error. */
  210. if ((status != FX_SUCCESS) || (j != 0))
  211. {
  212. printf("ERROR!\n");
  213. test_control_return(8);
  214. }
  215. #ifndef FX_DISABLE_CACHE
  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. /* Check for flush errors. */
  220. if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
  221. {
  222. printf("ERROR!\n");
  223. test_control_return(9);
  224. }
  225. /* See if any sectors are still valid in the cache. */
  226. for (j = 0; j < ram_disk.fx_media_sector_cache_size; j++)
  227. {
  228. /* Determine if this cache entry is still valid. */
  229. if (ram_disk.fx_media_sector_cache[j].fx_cached_sector_valid)
  230. {
  231. printf("ERROR!\n");
  232. test_control_return(10);
  233. }
  234. }
  235. #endif /* FX_DISABLE_CACHE */
  236. /* Seek to the beginning of the test file. */
  237. status = fx_file_seek(&my_file, 0);
  238. /* Check the file seek status. */
  239. if (status != FX_SUCCESS)
  240. {
  241. printf("ERROR!\n");
  242. test_control_return(11);
  243. }
  244. /* Now read in all the bytes again to make sure the file contents are really there. */
  245. status = fx_file_read(&my_file, (void *) large_data_buffer, large_data_buffer_size, &actual);
  246. /* Check the file read status. */
  247. if ((status != FX_SUCCESS) || (actual != available_bytes))
  248. {
  249. printf("ERROR!\n");
  250. test_control_return(12);
  251. }
  252. /* Close the test file. */
  253. status = fx_file_close(&my_file);
  254. /* Check the file close status. */
  255. if (status != FX_SUCCESS)
  256. {
  257. printf("ERROR!\n");
  258. test_control_return(13);
  259. }
  260. /* Close the media. */
  261. status = fx_media_close(&ram_disk);
  262. /* Determine if the test was successful. */
  263. if (status != FX_SUCCESS)
  264. {
  265. printf("ERROR!\n");
  266. test_control_return(14);
  267. }
  268. }
  269. /* Output successful. */
  270. printf("SUCCESS!\n");
  271. test_control_return(0);
  272. }
  273. #else
  274. #ifdef CTEST
  275. void test_application_define(void *first_unused_memory)
  276. #else
  277. void filex_fault_tolerant_write_large_data_test_application_define(void *first_unused_memory)
  278. #endif
  279. {
  280. FX_PARAMETER_NOT_USED(first_unused_memory);
  281. /* Print out some test information banners. */
  282. printf("FileX Test: Fault Tolerant Write Large Data Test...................N/A\n");
  283. test_control_return(255);
  284. }
  285. #endif