1 /* This FileX test concentrates on the Fault-Tolerant the file allocate related API test.
2  * The offset of file is not changed after file allocate operation. */
3 
4 #ifndef FX_STANDALONE_ENABLE
5 #include   "tx_api.h"
6 #endif
7 #include   "fx_api.h"
8 #include   "fx_system.h"
9 #include   "fx_fault_tolerant.h"
10 #include   <stdio.h>
11 #include   "fx_ram_driver_test.h"
12 extern void    test_control_return(UINT status);
13 void    filex_fault_tolerant_file_allocate_test_application_define(void *first_unused_memory);
14 
15 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
16 
17 #define     DEMO_STACK_SIZE         4096
18 #define CACHE_SIZE                  2048
19 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
20 
21 
22 
23 
24 /* Define the ThreadX and FileX object control blocks...  */
25 
26 #ifndef FX_STANDALONE_ENABLE
27 static TX_THREAD                ftest_0;
28 static TX_THREAD                ftest_1;
29 #endif
30 static FX_MEDIA                 ram_disk;
31 static FX_FILE                  my_file;
32 static UCHAR                    *pointer;
33 
34 /* Define the counters used in the test application...  */
35 
36 #ifndef FX_STANDALONE_ENABLE
37 static UCHAR                    *cache_buffer;
38 static UCHAR                    *fault_tolerant_buffer;
39 static UCHAR                    *thread_buffer;
40 #else
41 static UCHAR                    cache_buffer[CACHE_SIZE];
42 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
43 #endif
44 static UINT                     error_couter = 0;
45 static UINT                     i;
46 
47 #define SEEK_COUNT              5
48 #define FAT_COUNT               3            /* FAT12, 16, 32.  */
49 #define TEST_COUNT              FAT_COUNT * SEEK_COUNT
50 
51 /* Define thread prototypes.  */
52 
53 static void    ftest_0_entry(ULONG thread_input);
54 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
55 extern void    test_control_return(UINT status);
56 extern UINT    _filex_fault_tolerant_log_check(FX_MEDIA *media_ptr);
57 extern UINT    (*driver_write_callback)(FX_MEDIA *media_ptr, UINT sector_type, UCHAR *block_ptr, UINT *operation_ptr);
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_allocate_test_application_define(void *first_unused_memory)
66 #endif
67 {
68 
69 
70 #ifndef FX_STANDALONE_ENABLE
71     /* Setup the working pointer.  */
72     pointer =  (UCHAR *) first_unused_memory;
73 
74     /* Create the main thread.  */
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     pointer =  pointer + DEMO_STACK_SIZE;
79 
80     /* Setup memory for the RAM disk and the sector cache.  */
81     cache_buffer =  pointer;
82     pointer += CACHE_SIZE;
83     fault_tolerant_buffer = pointer;
84     pointer += FAULT_TOLERANT_SIZE;
85     thread_buffer = pointer;
86     pointer += DEMO_STACK_SIZE;
87 #endif
88 
89     /* Initialize the FileX system.  */
90     fx_system_initialize();
91 #ifdef FX_STANDALONE_ENABLE
92     ftest_0_entry(0);
93 #endif
94 }
95 
96 
97 
98 /* Define the test threads.  */
99 
ftest_0_entry(ULONG thread_input)100 static void    ftest_0_entry(ULONG thread_input)
101 {
102 
103 UINT        status;
104 ULONG64     allocated;
105 ULONG64     available_bytes_before_write;
106 ULONG64     available_bytes_after_write;
107 
108     FX_PARAMETER_NOT_USED(thread_input);
109 
110     /* Print out some test information banners.  */
111     printf("FileX Test:   Fault Tolerant File Allocate Test......................");
112 
113     /* Loop to test FAT 12, 16, 32.   */
114     for (i = 0; i < TEST_COUNT; i ++)
115     {
116         if (i < 5)
117         {
118             /* Format the media with FAT12.  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                                      256,                    // Total sectors
129                                      256,                    // Sector size
130                                      8,                      // Sectors per cluster
131                                      1,                      // Heads
132                                      1);                     // Sectors per track
133         }
134         else if (i < 10)
135         {
136             /* Format the media with FAT16.  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                                      4200 * 8,               // Total sectors
147                                      256,                    // Sector size
148                                      8,                      // Sectors per cluster
149                                      1,                      // Heads
150                                      1);                     // Sectors per track
151         }
152         else if (i < 15)
153         {
154             /* Format the media with FAT32.  This needs to be done before opening it!  */
155             status =  fx_media_format(&ram_disk,
156                                      _fx_ram_driver,         // Driver entry
157                                      ram_disk_memory_large,  // RAM disk memory pointer
158                                      cache_buffer,           // Media buffer pointer
159                                      CACHE_SIZE,             // Media buffer size
160                                      "MY_RAM_DISK",          // Volume Name
161                                      1,                      // Number of FATs
162                                      32,                     // Directory Entries
163                                      0,                      // Hidden sectors
164                                      70000 * 4,              // Total sectors
165                                      512,                    // Sector size
166                                      4,                      // Sectors per cluster
167                                      1,                      // Heads
168                                      1);                     // Sectors per track
169         }
170 
171         /* Determine if the format had an error.  */
172         if (status)
173         {
174 
175             printf("ERROR!\n");
176             test_control_return(1);
177         }
178 
179         /* Open the ram_disk.  */
180         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
181 
182         /* Check the status.  */
183         if (status != FX_SUCCESS)
184         {
185 
186             /* Error, return error code.  */
187             printf("ERROR!\n");
188             test_control_return(2);
189         }
190 
191         /* Get available bytes before write. */
192         fx_media_extended_space_available(&ram_disk, &available_bytes_before_write);
193 
194         /* Enable the Fault-tolerant feature.  */
195         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
196 
197         /* Check status.   */
198         if (status)
199         {
200 
201             printf("ERROR!\n");
202             test_control_return(3);
203         }
204 
205         /* Get available bytes before write. */
206         fx_media_extended_space_available(&ram_disk, &available_bytes_after_write);
207         if (available_bytes_before_write != (available_bytes_after_write +
208                     ram_disk.fx_media_fault_tolerant_clusters * ram_disk.fx_media_bytes_per_sector * ram_disk.fx_media_sectors_per_cluster))
209         {
210 
211             printf("ERROR!\n");
212             test_control_return(38);
213         }
214 
215         /* Create a file called TEST.TXT in the root directory.  */
216         status =  fx_file_create(&ram_disk, "TEST.TXT");
217 
218         /* Check the create status.  */
219         if (status != FX_SUCCESS)
220         {
221 
222             printf("ERROR!\n");
223             test_control_return(4);
224         }
225 
226         /* Get available bytes before write. */
227         fx_media_extended_space_available(&ram_disk, &available_bytes_before_write);
228 
229         /* Open the test file.  */
230         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
231 
232         /* Check the file open status.  */
233         if (status != FX_SUCCESS)
234         {
235 
236             printf("ERROR!\n");
237             test_control_return(5);
238         }
239 
240         if (i & 1)
241         {
242             status = fx_file_allocate(&my_file, 2048);
243         }
244         else
245         {
246             status = fx_file_extended_best_effort_allocate(&my_file, 2048, &allocated);
247         }
248 
249         /* Check the file write status.  */
250         if (status != FX_SUCCESS)
251         {
252 
253             printf("ERROR!\n");
254             test_control_return(7);
255         }
256 
257         /* Write a string to the test file.  */
258         status =  fx_file_write(&my_file, " ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 28);
259 
260         /* Check the file write status.  */
261         if (status != FX_SUCCESS)
262         {
263 
264             printf("ERROR!\n");
265             test_control_return(6);
266         }
267 
268         /* Get available bytes after write. */
269         fx_media_extended_space_available(&ram_disk, &available_bytes_after_write);
270         {
271             if ((available_bytes_before_write - available_bytes_after_write) != 2048)
272             {
273 
274                 printf("ERROR!\n");
275                 test_control_return(37);
276             }
277         }
278 
279         /* Close the test file.  */
280         status =  fx_file_close(&my_file);
281 
282         /* Check the file close status.  */
283         if (status != FX_SUCCESS)
284         {
285 
286             printf("ERROR!\n");
287             test_control_return(31);
288         }
289 
290         /* Close the media.  */
291         status =  fx_media_close(&ram_disk);
292 
293         /* Determine if the test was successful.  */
294         if ((status != FX_SUCCESS) || (error_couter))
295         {
296 
297             printf("ERROR!\n");
298             test_control_return(36);
299         }
300 
301         /* Delete the thread.  */
302 #ifndef FX_STANDALONE_ENABLE
303         tx_thread_delete(&ftest_1);
304 #endif
305     }
306 
307     /* Output successful.  */
308     printf("SUCCESS!\n");
309     test_control_return(0);
310 }
311 
312 #else
313 
314 #ifdef CTEST
test_application_define(void * first_unused_memory)315 void test_application_define(void *first_unused_memory)
316 #else
317 void    filex_fault_tolerant_file_allocate_test_application_define(void *first_unused_memory)
318 #endif
319 {
320 
321     FX_PARAMETER_NOT_USED(first_unused_memory);
322 
323     /* Print out some test information banners.  */
324     printf("FileX Test:   Fault Tolerant File Allocate Test......................N/A\n");
325 
326     test_control_return(255);
327 }
328 #endif
329 
330