filex_file_write_seek_test.c 14 KB

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