filex_file_write_notify_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* This FileX test concentrates on the file write and file notify 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 128*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. /* Define the counters used in the test application... */
  16. #ifndef FX_STANDALONE_ENABLE
  17. static UCHAR *ram_disk_memory;
  18. static UCHAR *cache_buffer;
  19. #else
  20. static UCHAR cache_buffer[CACHE_SIZE];
  21. #endif
  22. static UCHAR write_data[65535];
  23. static UCHAR read_data[65535];
  24. static ULONG notify_counter = 0;
  25. /* Define thread prototypes. */
  26. void filex_file_write_notify_application_define(void *first_unused_memory);
  27. static void ftest_0_entry(ULONG thread_input);
  28. VOID _fx_ram_driver(FX_MEDIA *media_ptr);
  29. void test_control_return(UINT status);
  30. static VOID file_write_notify(FX_FILE *file_ptr);
  31. /* Define what the initial system looks like. */
  32. #ifdef CTEST
  33. void test_application_define(void *first_unused_memory)
  34. #else
  35. void filex_file_write_notify_application_define(void *first_unused_memory)
  36. #endif
  37. {
  38. #ifndef FX_STANDALONE_ENABLE
  39. UCHAR *pointer;
  40. /* Setup the working pointer. */
  41. pointer = (UCHAR *) first_unused_memory;
  42. /* Create the main thread. */
  43. tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
  44. pointer, DEMO_STACK_SIZE,
  45. 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
  46. pointer = pointer + DEMO_STACK_SIZE;
  47. /* Setup memory for the RAM disk and the sector cache. */
  48. cache_buffer = pointer;
  49. pointer = pointer + CACHE_SIZE;
  50. ram_disk_memory = pointer;
  51. #endif
  52. /* Initialize the FileX system. */
  53. fx_system_initialize();
  54. #ifdef FX_STANDALONE_ENABLE
  55. ftest_0_entry(0);
  56. #endif
  57. }
  58. /* Define the test threads. */
  59. static void ftest_0_entry(ULONG thread_input)
  60. {
  61. UINT status;
  62. ULONG actual;
  63. ULONG i;
  64. FX_FILE my_file;
  65. FX_PARAMETER_NOT_USED(thread_input);
  66. /* Print out some test information banners. */
  67. printf("FileX Test: File write notify test.................................");
  68. /* Format the media. This needs to be done before opening it! */
  69. status = fx_media_format(&ram_disk,
  70. _fx_ram_driver, // Driver entry
  71. ram_disk_memory, // RAM disk memory pointer
  72. cache_buffer, // Media buffer pointer
  73. CACHE_SIZE, // Media buffer size
  74. "MY_RAM_DISK", // Volume Name
  75. 1, // Number of FATs
  76. 32, // Directory Entries
  77. 0, // Hidden sectors
  78. 512, // Total sectors
  79. 512, // Sector size
  80. 8, // Sectors per cluster
  81. 1, // Heads
  82. 1); // Sectors per track
  83. /* Determine if the format had an error. */
  84. if (status)
  85. {
  86. printf("ERROR!\n");
  87. test_control_return(1);
  88. }
  89. /* Open the ram_disk. */
  90. status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
  91. /* Check the status. */
  92. if (status != FX_SUCCESS)
  93. {
  94. /* Error, return error code. */
  95. printf("ERROR!\n");
  96. test_control_return(2);
  97. }
  98. /* Create a file called TEST.TXT in the root directory. */
  99. status = fx_file_create(&ram_disk, "TEST.TXT");
  100. /* Check the create status. */
  101. if (status != FX_SUCCESS)
  102. {
  103. printf("ERROR!\n");
  104. test_control_return(3);
  105. }
  106. /* Open the test file. */
  107. status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
  108. /* Check the file open status. */
  109. if (status != FX_SUCCESS)
  110. {
  111. printf("ERROR!\n");
  112. test_control_return(4);
  113. }
  114. /* Initialize the data for write and read. */
  115. for (i = 0; i < sizeof(write_data); i++)
  116. {
  117. write_data[i] = (UCHAR)i;
  118. read_data[i] = 0;
  119. }
  120. /* Write 4116 bytes to the file. */
  121. status = fx_file_write(&my_file, write_data, 4116);
  122. /* Check the file write status. */
  123. if (status != FX_SUCCESS)
  124. {
  125. printf("ERROR!\n");
  126. test_control_return(5);
  127. }
  128. /* Check the notify counter. */
  129. if (notify_counter != 0)
  130. {
  131. printf("ERROR!\n");
  132. test_control_return(6);
  133. }
  134. /* Setup write notify callback. */
  135. status = fx_file_write_notify_set(&my_file, file_write_notify);
  136. /* Check the file open status. */
  137. if (status != FX_SUCCESS)
  138. {
  139. printf("ERROR!\n");
  140. test_control_return(7);
  141. }
  142. /* Write 4116 bytes to the file. */
  143. status = fx_file_write(&my_file, write_data + 4116, 4116);
  144. /* Check the file write status. */
  145. if (status != FX_SUCCESS)
  146. {
  147. printf("ERROR!\n");
  148. test_control_return(8);
  149. }
  150. /* Check the notify counter. */
  151. if (notify_counter != 1)
  152. {
  153. printf("ERROR!\n");
  154. test_control_return(9);
  155. }
  156. /* Seek to the beginning of the test file. */
  157. status = fx_file_seek(&my_file, 0);
  158. /* Check the file seek status. */
  159. if (status != FX_SUCCESS)
  160. {
  161. printf("ERROR!\n");
  162. test_control_return(10);
  163. }
  164. /* Read as much as 4 sectors full of bytes from the file. */
  165. status = fx_file_read(&my_file, (void *)read_data, sizeof(read_data), &actual);
  166. /* Check the file read status. */
  167. if ((status != FX_SUCCESS) || (actual != 4116 * 2))
  168. {
  169. printf("ERROR!\n");
  170. test_control_return(11);
  171. }
  172. /* Close the file. */
  173. status = fx_file_close(&my_file);
  174. /* Check the file open status. */
  175. if (status != FX_SUCCESS)
  176. {
  177. printf("ERROR!\n");
  178. test_control_return(12);
  179. }
  180. else
  181. {
  182. printf("SUCCESS!\n");
  183. test_control_return(0);
  184. }
  185. }
  186. static VOID file_write_notify(FX_FILE *file_ptr)
  187. {
  188. FX_PARAMETER_NOT_USED(file_ptr);
  189. /* Update the notify counter. */
  190. notify_counter++;
  191. }