filex_media_flush_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* This FileX test concentrates on the media flush operation. */
  2. #ifndef FX_STANDALONE_ENABLE
  3. #include "tx_api.h"
  4. #endif
  5. #include "fx_api.h"
  6. #include <stdio.h>
  7. #include "fx_ram_driver_test.h"
  8. #define DEMO_STACK_SIZE 4096
  9. #define CACHE_SIZE 16*128
  10. /* Define the ThreadX and FileX object control blocks... */
  11. #ifndef FX_STANDALONE_ENABLE
  12. static TX_THREAD ftest_0;
  13. #endif
  14. static FX_MEDIA ram_disk;
  15. static FX_FILE my_file;
  16. /* Define the counters used in the test application... */
  17. #ifndef FX_STANDALONE_ENABLE
  18. static UCHAR *ram_disk_memory;
  19. static UCHAR *cache_buffer;
  20. #else
  21. static UCHAR cache_buffer[CACHE_SIZE];
  22. #endif
  23. /* Define thread prototypes. */
  24. void filex_media_flush_application_define(void *first_unused_memory);
  25. static void ftest_0_entry(ULONG thread_input);
  26. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  27. void test_control_return(UINT status);
  28. /* Define what the initial system looks like. */
  29. #ifdef CTEST
  30. void test_application_define(void *first_unused_memory)
  31. #else
  32. void filex_media_flush_application_define(void *first_unused_memory)
  33. #endif
  34. {
  35. #ifndef FX_STANDALONE_ENABLE
  36. UCHAR *pointer;
  37. /* Setup the working pointer. */
  38. pointer = (UCHAR *) first_unused_memory;
  39. /* Create the main thread. */
  40. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  41. pointer, DEMO_STACK_SIZE,
  42. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  43. pointer = pointer + DEMO_STACK_SIZE;
  44. /* Setup memory for the RAM disk and the sector cache. */
  45. cache_buffer = pointer;
  46. pointer = pointer + CACHE_SIZE;
  47. ram_disk_memory = pointer;
  48. #endif
  49. /* Initialize the FileX system. */
  50. fx_system_initialize();
  51. #ifdef FX_STANDALONE_ENABLE
  52. ftest_0_entry(0);
  53. #endif
  54. }
  55. /* Define the test threads. */
  56. static void ftest_0_entry(ULONG thread_input)
  57. {
  58. UINT status;
  59. ULONG actual;
  60. ULONG read_value;
  61. ULONG write_value;
  62. ULONG available_bytes;
  63. ULONG i;
  64. FX_PARAMETER_NOT_USED(thread_input);
  65. /* Print out some test information banners. */
  66. printf("FileX Test: Media flush test.......................................");
  67. /* Only run this if error checking is enabled */
  68. #ifndef FX_DISABLE_ERROR_CHECKING
  69. /* send null pointer to generate an error */
  70. status = fx_media_format(FX_NULL, _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE, "MY_RAM_DISK", 1, 32, 0, 256, 128, 1, 1, 1);
  71. if (status != FX_PTR_ERROR)
  72. {
  73. printf("ERROR!\n");
  74. test_control_return(11);
  75. }
  76. #endif /* FX_DISABLE_ERROR_CHECKING */
  77. /* Format the media. This needs to be done before opening it! */
  78. status = fx_media_format(&ram_disk,
  79. _fx_ram_driver, // Driver entry
  80. ram_disk_memory, // RAM disk memory pointer
  81. cache_buffer, // Media buffer pointer
  82. CACHE_SIZE, // Media buffer size
  83. "MY_RAM_DISK", // Volume Name
  84. 1, // Number of FATs
  85. 32, // Directory Entries
  86. 0, // Hidden sectors
  87. 256, // Total sectors
  88. 128, // Sector size
  89. 1, // Sectors per cluster
  90. 1, // Heads
  91. 1); // Sectors per track
  92. /* Determine if the format had an error. */
  93. if (status)
  94. {
  95. printf("ERROR!\n");
  96. test_control_return(2);
  97. }
  98. /* try to flush the media before it is opened */
  99. status = fx_media_flush(&ram_disk);
  100. if (status != FX_MEDIA_NOT_OPEN)
  101. {
  102. printf("ERROR!\n");
  103. test_control_return(11);
  104. }
  105. /* Open the ram_disk. */
  106. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  107. /* Check the status. */
  108. if (status != FX_SUCCESS)
  109. {
  110. /* Error, return error code. */
  111. printf("ERROR!\n");
  112. test_control_return(21);
  113. }
  114. /* try to flush the media while it is write protected */
  115. ram_disk.fx_media_driver_write_protect = FX_TRUE;
  116. status = fx_media_flush(&ram_disk);
  117. if (status != FX_WRITE_PROTECT)
  118. {
  119. printf("ERROR!\n");
  120. test_control_return(11);
  121. }
  122. ram_disk.fx_media_driver_write_protect = FX_FALSE;
  123. /* Create a file called TEST.TXT in the root directory. */
  124. status = fx_file_create(&ram_disk, "TEST.TXT");
  125. /* Check the create status. */
  126. if (status != FX_SUCCESS)
  127. {
  128. printf("ERROR!\n");
  129. test_control_return(3);
  130. }
  131. /* Open the test file. */
  132. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  133. /* Check the file open status. */
  134. if (status != FX_SUCCESS)
  135. {
  136. printf("ERROR!\n");
  137. test_control_return(4);
  138. }
  139. /* Pickup the available bytes in the media. */
  140. status = fx_media_space_available(&ram_disk, &available_bytes);
  141. /* Check for available bytes error. */
  142. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  143. {
  144. printf("ERROR!\n");
  145. test_control_return(5);
  146. }
  147. /* Loop to write successive bytes out to the file.... to fill the media! */
  148. i = 0;
  149. write_value = 0;
  150. while (i < available_bytes)
  151. {
  152. /* Write 4 bytes to the file. */
  153. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  154. /* Check the file write status. */
  155. if (status != FX_SUCCESS)
  156. {
  157. printf("ERROR!\n");
  158. test_control_return(6);
  159. }
  160. /* Increment byte count. */
  161. i = i + sizeof(ULONG);
  162. /* Increment write value. */
  163. write_value++;
  164. }
  165. /* Pickup the available bytes in the media again. */
  166. status = fx_media_space_available(&ram_disk, &i);
  167. /* Check for available bytes error. */
  168. if ((status != FX_SUCCESS) || (i != 0))
  169. {
  170. printf("ERROR!\n");
  171. test_control_return(7);
  172. }
  173. /* Only run this if error checking is enabled */
  174. #ifndef FX_DISABLE_ERROR_CHECKING
  175. /* send null pointer to generate an error */
  176. status = fx_media_flush(FX_NULL);
  177. if (status != FX_PTR_ERROR)
  178. {
  179. printf("ERROR!\n");
  180. test_control_return(11);
  181. }
  182. #endif /* FX_DISABLE_ERROR_CHECKING */
  183. /* At this point, we should flush the media to ensure that all
  184. dirty sectors are written. */
  185. status = fx_media_flush(&ram_disk);
  186. #ifndef FX_DISABLE_CACHE
  187. /* Check for flush errors. */
  188. if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
  189. {
  190. printf("ERROR!\n");
  191. test_control_return(8);
  192. }
  193. #endif
  194. /* Seek to the beginning of the test file. */
  195. status = fx_file_seek(&my_file, 0);
  196. /* Check the file seek status. */
  197. if (status != FX_SUCCESS)
  198. {
  199. printf("ERROR!\n");
  200. test_control_return(9);
  201. }
  202. /* Now read in all the bytes again to make sure the file contents are really there. */
  203. i = 0;
  204. read_value = 0;
  205. while (i < available_bytes)
  206. {
  207. /* Read 4 bytes from the file. */
  208. status = fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
  209. /* Check the file read status. */
  210. if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
  211. {
  212. printf("ERROR!\n");
  213. test_control_return(10);
  214. }
  215. /* Increment byte count. */
  216. i = i + sizeof(ULONG);
  217. }
  218. /* Close the test file. */
  219. status = fx_file_close(&my_file);
  220. /* Check the file close status. */
  221. if (status != FX_SUCCESS)
  222. {
  223. printf("ERROR!\n");
  224. test_control_return(11);
  225. }
  226. /* Close the media. */
  227. status = fx_media_close(&ram_disk);
  228. /* Check the media close status. */
  229. if (status != FX_SUCCESS)
  230. {
  231. printf("ERROR!\n");
  232. test_control_return(12);
  233. }
  234. /* Reformat the media. This needs to be done before opening it! */
  235. status = fx_media_format(&ram_disk,
  236. _fx_ram_driver, // Driver entry
  237. ram_disk_memory, // RAM disk memory pointer
  238. cache_buffer, // Media buffer pointer
  239. CACHE_SIZE, // Media buffer size
  240. "MY_RAM_DISK", // Volume Name
  241. 1, // Number of FATs
  242. 32, // Directory Entries
  243. 0, // Hidden sectors
  244. 256, // Total sectors
  245. 128, // Sector size
  246. 1, // Sectors per cluster
  247. 1, // Heads
  248. 1); // Sectors per track
  249. /* Determine if the format had an error. */
  250. if (status)
  251. {
  252. printf("ERROR!\n");
  253. test_control_return(13);
  254. }
  255. /* Open the ram_disk, but do so to ensure non-hashed algorithm is used by supplying CACHE_SIZE-1. */
  256. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
  257. /* Check the status. */
  258. if (status != FX_SUCCESS)
  259. {
  260. /* Error, return error code. */
  261. printf("ERROR!\n");
  262. test_control_return(14);
  263. }
  264. /* Create a file called TEST.TXT in the root directory. */
  265. status = fx_file_create(&ram_disk, "TEST.TXT");
  266. /* Check the create status. */
  267. if (status != FX_SUCCESS)
  268. {
  269. printf("ERROR!\n");
  270. test_control_return(15);
  271. }
  272. /* Open the test file. */
  273. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  274. /* Check the file open status. */
  275. if (status != FX_SUCCESS)
  276. {
  277. printf("ERROR!\n");
  278. test_control_return(16);
  279. }
  280. /* Pickup the available bytes in the media. */
  281. status = fx_media_space_available(&ram_disk, &available_bytes);
  282. /* Check for available bytes error. */
  283. if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
  284. {
  285. printf("ERROR!\n");
  286. test_control_return(17);
  287. }
  288. /* Loop to write successive bytes out to the file.... to fill the media! */
  289. i = 0;
  290. write_value = 0;
  291. while (i < available_bytes)
  292. {
  293. /* Write 4 bytes to the file. */
  294. status = fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
  295. /* Check the file write status. */
  296. if (status != FX_SUCCESS)
  297. {
  298. printf("ERROR!\n");
  299. test_control_return(18);
  300. }
  301. /* Increment byte count. */
  302. i = i + sizeof(ULONG);
  303. /* Increment write value. */
  304. write_value++;
  305. }
  306. /* Pickup the available bytes in the media again. */
  307. status = fx_media_space_available(&ram_disk, &i);
  308. /* Check for available bytes error. */
  309. if ((status != FX_SUCCESS) || (i != 0))
  310. {
  311. printf("ERROR!\n");
  312. test_control_return(19);
  313. }
  314. /* At this point, we should flush the media to ensure that all
  315. dirty sectors are written. */
  316. status = fx_media_flush(&ram_disk);
  317. #ifndef FX_DISABLE_CACHE
  318. /* Check for flush errors. */
  319. if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
  320. {
  321. printf("ERROR!\n");
  322. test_control_return(20);
  323. }
  324. #endif
  325. /* Seek to the beginning of the test file. */
  326. status = fx_file_seek(&my_file, 0);
  327. /* Check the file seek status. */
  328. if (status != FX_SUCCESS)
  329. {
  330. printf("ERROR!\n");
  331. test_control_return(22);
  332. }
  333. /* Now read in all the bytes again to make sure the file contents are really there. */
  334. i = 0;
  335. read_value = 0;
  336. while (i < available_bytes)
  337. {
  338. /* Read 4 bytes from the file. */
  339. status = fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
  340. /* Check the file read status. */
  341. if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
  342. {
  343. printf("ERROR!\n");
  344. test_control_return(23);
  345. }
  346. /* Increment byte count. */
  347. i = i + sizeof(ULONG);
  348. }
  349. /* Close the test file. */
  350. status = fx_file_close(&my_file);
  351. /* Check the file close status. */
  352. if (status != FX_SUCCESS)
  353. {
  354. printf("ERROR!\n");
  355. test_control_return(24);
  356. }
  357. /* Close the media. */
  358. status = fx_media_close(&ram_disk);
  359. /* Check the media close status. */
  360. if (status != FX_SUCCESS)
  361. {
  362. printf("ERROR!\n");
  363. test_control_return(25);
  364. }
  365. /* Determine if the test was successful. */
  366. if (status != FX_SUCCESS)
  367. {
  368. printf("ERROR!\n");
  369. test_control_return(26);
  370. }
  371. else
  372. {
  373. printf("SUCCESS!\n");
  374. test_control_return(0);
  375. }
  376. }