1 /* This FileX test concentrates on the Fault-Tolerant file seek operation.  */
2 /*
3 1. Format and open the media;
4 2. Enable fault tolerant feature;
5 3. Create and write 1024 bytes into TEST.TXT;
6 4. Seek to the beginning of the file;
7 5. Write 640 bytes into TEST.TXT;
8 6. Verify the data;
9 */
10 
11 #ifndef FX_STANDALONE_ENABLE
12 #include   "tx_api.h"
13 #endif
14 #include   "fx_api.h"
15 #include   "fx_system.h"
16 #include   "fx_fault_tolerant.h"
17 #include   <stdio.h>
18 #include   "fx_ram_driver_test.h"
19 extern void    test_control_return(UINT status);
20 void    filex_fault_tolerant_file_seek_test_application_define(void *first_unused_memory);
21 
22 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
23 
24 #define     DEMO_STACK_SIZE         4096
25 #define CACHE_SIZE                   (16 * 1024)
26 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
27 #define TOTAL_SIZE                  1024
28 #define OVERWRITE_SIZE              512
29 
30 
31 
32 
33 /* Define the ThreadX and FileX object control blocks...  */
34 
35 #ifndef FX_STANDALONE_ENABLE
36 static TX_THREAD                ftest_0;
37 #endif
38 static FX_MEDIA                 ram_disk;
39 static FX_FILE                  my_file;
40 static UCHAR                    *pointer;
41 
42 /* Define the counters used in the test application...  */
43 
44 static UCHAR                    cache_buffer[CACHE_SIZE];
45 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
46 static UCHAR                    *thread_buffer;
47 static CHAR                     write_buffer[2][TOTAL_SIZE];
48 static CHAR                     read_buffer[TOTAL_SIZE];
49 
50 #define TEST_COUNT              3            /* FAT12, 16, 32.  */
51 
52 /* Define thread prototypes.  */
53 
54 static void    ftest_0_entry(ULONG thread_input);
55 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
56 extern void    test_control_return(UINT status);
57 
58 
59 
60 /* Define what the initial system looks like.  */
61 
62 #ifdef CTEST
test_application_define(void * first_unused_memory)63 void test_application_define(void *first_unused_memory)
64 #else
65 void    filex_fault_tolerant_file_seek_test_application_define(void *first_unused_memory)
66 #endif
67 {
68 
69 #ifndef FX_STANDALONE_ENABLE
70     /* Setup the working pointer.  */
71     pointer =  (UCHAR *) first_unused_memory;
72 
73     /* Create the main thread.  */
74 
75     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
76             pointer, DEMO_STACK_SIZE,
77             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
78 
79     pointer =  pointer + DEMO_STACK_SIZE;
80 
81     /* Setup memory for the RAM disk and the sector cache.  */
82     thread_buffer = pointer;
83     pointer += DEMO_STACK_SIZE;
84 #endif
85 
86     /* Initialize the FileX system.  */
87     fx_system_initialize();
88 #ifdef FX_STANDALONE_ENABLE
89     ftest_0_entry(0);
90 #endif
91 }
92 
93 
94 
95 /* Define the test threads.  */
96 
ftest_0_entry(ULONG thread_input)97 static void    ftest_0_entry(ULONG thread_input)
98 {
99 
100 UINT        status;
101 ULONG       actual;
102 UINT        i;
103 UINT        j;
104 
105     FX_PARAMETER_NOT_USED(thread_input);
106 
107     /* Print out some test information banners.  */
108     printf("FileX Test:   Fault Tolerant File Seek Test..........................");
109 
110     /* Loop to test FAT 12, 16, 32.   */
111     for (i = 0; i < TEST_COUNT; i ++)
112     {
113 
114         /* Initialize the write and read buffer. */
115         for (j = 0; j < sizeof(write_buffer[0]); j++)
116         {
117             write_buffer[0][j] = (CHAR)(rand() & 0xFF);
118             write_buffer[1][j] = (CHAR)(~write_buffer[0][j] & 0xFF);
119         }
120 
121         if (i == 0)
122         {
123             /* Format the media with FAT12.  This needs to be done before opening it!  */
124             status =  fx_media_format(&ram_disk,
125                                      _fx_ram_driver,         // Driver entry
126                                      ram_disk_memory_large,  // RAM disk memory pointer
127                                      cache_buffer,           // Media buffer pointer
128                                      CACHE_SIZE,             // Media buffer size
129                                      "MY_RAM_DISK",          // Volume Name
130                                      1,                      // Number of FATs
131                                      32,                     // Directory Entries
132                                      0,                      // Hidden sectors
133                                      256,                    // Total sectors
134                                      512,                    // Sector size
135                                      8,                      // Sectors per cluster
136                                      1,                      // Heads
137                                      1);                     // Sectors per track
138         }
139         else if (i == 1)
140         {
141             /* Format the media with FAT16.  This needs to be done before opening it!  */
142             status =  fx_media_format(&ram_disk,
143                                      _fx_ram_driver,         // Driver entry
144                                      ram_disk_memory_large,  // RAM disk memory pointer
145                                      cache_buffer,           // Media buffer pointer
146                                      CACHE_SIZE,             // Media buffer size
147                                      "MY_RAM_DISK",          // Volume Name
148                                      1,                      // Number of FATs
149                                      32,                     // Directory Entries
150                                      0,                      // Hidden sectors
151                                      4200 * 8,               // Total sectors
152                                      512,                    // Sector size
153                                      8,                      // Sectors per cluster
154                                      1,                      // Heads
155                                      1);                     // Sectors per track
156         }
157         else if (i == 2)
158         {
159             /* Format the media with FAT32.  This needs to be done before opening it!  */
160             status =  fx_media_format(&ram_disk,
161                                      _fx_ram_driver,         // Driver entry
162                                      ram_disk_memory_large,  // RAM disk memory pointer
163                                      cache_buffer,           // Media buffer pointer
164                                      CACHE_SIZE,             // Media buffer size
165                                      "MY_RAM_DISK",          // Volume Name
166                                      1,                      // Number of FATs
167                                      32,                     // Directory Entries
168                                      0,                      // Hidden sectors
169                                      70000 * 8,              // Total sectors
170                                      512,                    // Sector size
171                                      8,                      // Sectors per cluster
172                                      1,                      // Heads
173                                      1);                     // Sectors per track
174         }
175         return_if_fail(status == FX_SUCCESS);
176 
177         /* Open the ram_disk.  */
178         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
179         return_if_fail(status == FX_SUCCESS);
180 
181         /* Enable the Fault-tolerant feature.  */
182         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
183         return_if_fail(status == FX_SUCCESS);
184 
185         /* Prepare a file for test. */
186         status = fx_file_create(&ram_disk, "TEST.TXT");
187         return_if_fail(status == FX_SUCCESS);
188 
189         status = fx_file_create(&ram_disk, "TEST.TXT");
190         return_if_fail(status == FX_ALREADY_CREATED);
191 
192         status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
193         return_if_fail(status == FX_SUCCESS);
194 
195         /* Write data. */
196         status = fx_file_write(&my_file, write_buffer[0], TOTAL_SIZE);
197         return_if_fail(status == FX_SUCCESS);
198 
199         /* Seek to the beginning of file. */
200         status = fx_file_relative_seek(&my_file, 0, FX_SEEK_BEGIN);
201         return_if_fail(status == FX_SUCCESS);
202 
203         /* Overwrite the beginning data. */
204         status = fx_file_write(&my_file, write_buffer[1], OVERWRITE_SIZE);
205         return_if_fail(status == FX_SUCCESS);
206 
207         status = fx_file_relative_seek(&my_file, 0, FX_SEEK_BEGIN);
208         return_if_fail(status == FX_SUCCESS);
209 
210         /* Read the bytes of the test file.  */
211         memset(read_buffer, 0xFF, sizeof(read_buffer));
212         status = fx_file_read(&my_file, read_buffer, sizeof(read_buffer), &actual);
213         return_if_fail((status == FX_SUCCESS) && (actual == sizeof(read_buffer)));
214 
215         /* Check the data of overwrite part. */
216         return_if_fail(memcmp(&read_buffer[0], write_buffer[1], OVERWRITE_SIZE) == 0);
217 
218         /* Check the data of remaining part. */
219         return_if_fail(memcmp(&read_buffer[OVERWRITE_SIZE],
220                               &write_buffer[0][OVERWRITE_SIZE],
221                               (TOTAL_SIZE - OVERWRITE_SIZE)) == 0);
222 
223         /* Close the file. */
224         status = fx_file_close(&my_file);
225         return_if_fail(status == FX_SUCCESS);
226 
227         /* Close the media.  */
228         status =  fx_media_close(&ram_disk);
229         return_if_fail(status == FX_SUCCESS);
230     }
231 
232     /* Output successful.  */
233     printf("SUCCESS!\n");
234     test_control_return(0);
235 }
236 #else
237 
238 #ifdef CTEST
test_application_define(void * first_unused_memory)239 void test_application_define(void *first_unused_memory)
240 #else
241 void    filex_fault_tolerant_file_seek_test_application_define(void *first_unused_memory)
242 #endif
243 {
244 
245     FX_PARAMETER_NOT_USED(first_unused_memory);
246 
247     /* Print out some test information banners.  */
248     printf("FileX Test:   Fault Tolerant File Seek Test..........................N/A\n");
249 
250     test_control_return(255);
251 }
252 #endif
253 
254