Comments and some minor fixes.
[lwext4.git] / demos / stm32f429_disco / main.c
1 /*\r
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * - Redistributions of source code must retain the above copyright\r
10  *   notice, this list of conditions and the following disclaimer.\r
11  * - Redistributions in binary form must reproduce the above copyright\r
12  *   notice, this list of conditions and the following disclaimer in the\r
13  *   documentation and/or other materials provided with the distribution.\r
14  * - The name of the author may not be used to endorse or promote products\r
15  *   derived from this software without specific prior written permission.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  */\r
28 \r
29 #include <config.h>\r
30 #include <hw_init.h>\r
31 \r
32 #include <stdio.h>\r
33 #include <stdlib.h>\r
34 #include <string.h>\r
35 #include <unistd.h>\r
36 #include <getopt.h>\r
37 #include <stdbool.h>\r
38 #include <time.h>\r
39 #include <unistd.h>\r
40 \r
41 #include <usb_msc_lwext4.h>\r
42 #include <ext4.h>\r
43 \r
44 /**@brief   Read-write size*/\r
45 #define READ_WRITE_SZIZE 1024 * 8\r
46 \r
47 /**@brief   Delay test (slower LCD scroll)*/\r
48 #define TEST_DELAY_MS    2000\r
49 \r
50 /**@brief   Input stream name.*/\r
51 char input_name[128] = "ext2";\r
52 \r
53 /**@brief   Read-write size*/\r
54 static int rw_szie  = READ_WRITE_SZIZE;\r
55 \r
56 /**@brief   Read-write size*/\r
57 static int rw_count = 100;\r
58 \r
59 /**@brief   Directory test count*/\r
60 static int dir_cnt  = 10;\r
61 \r
62 /**@brief   Static or dynamic cache mode*/\r
63 static bool cache_mode = false;\r
64 \r
65 /**@brief   Cleanup after test.*/\r
66 static bool cleanup_flag = false;\r
67 \r
68 /**@brief   Block device stats.*/\r
69 static bool bstat = false;\r
70 \r
71 /**@brief   Superblock stats.*/\r
72 static bool sbstat = false;\r
73 \r
74 /**@brief   File write buffer*/\r
75 static uint8_t wr_buff[READ_WRITE_SZIZE];\r
76 \r
77 /**@brief   File read buffer.*/\r
78 static uint8_t rd_buff[READ_WRITE_SZIZE];\r
79 \r
80 /**@brief   Block device handle.*/\r
81 static struct ext4_blockdev *bd;\r
82 \r
83 /**@brief   Block cache handle.*/\r
84 static struct ext4_bcache   *bc;\r
85 \r
86 static char* entry_to_str(uint8_t type)\r
87 {\r
88     switch(type){\r
89     case EXT4_DIRENTRY_UNKNOWN:\r
90         return "[UNK] ";\r
91     case EXT4_DIRENTRY_REG_FILE:\r
92         return "[FIL] ";\r
93     case EXT4_DIRENTRY_DIR:\r
94         return "[DIR] ";\r
95     case EXT4_DIRENTRY_CHRDEV:\r
96         return "[CHA] ";\r
97     case EXT4_DIRENTRY_BLKDEV:\r
98         return "[BLK] ";\r
99     case EXT4_DIRENTRY_FIFO:\r
100         return "[FIF] ";\r
101     case EXT4_DIRENTRY_SOCK:\r
102         return "[SOC] ";\r
103     case EXT4_DIRENTRY_SYMLINK:\r
104         return "[SYM] ";\r
105     default:\r
106         break;\r
107     }\r
108     return "[???]";\r
109 }\r
110 \r
111 static void dir_ls(const char *path)\r
112 {\r
113     int j = 0;\r
114     char sss[255];\r
115     ext4_dir d;\r
116     ext4_direntry *de;\r
117 \r
118     printf("********************\n");\r
119 \r
120     ext4_dir_open(&d, path);\r
121     de = ext4_dir_entry_get(&d, j++);\r
122     printf("ls %s\n", path);\r
123 \r
124     while(de){\r
125         memcpy(sss, de->name, de->name_length);\r
126         sss[de->name_length] = 0;\r
127         printf("%s", entry_to_str(de->inode_type));\r
128         printf("%s", sss);\r
129         printf("\n");\r
130         de = ext4_dir_entry_get(&d, j++);\r
131     }\r
132     printf("********************\n");\r
133     ext4_dir_close(&d);\r
134 }\r
135 \r
136 static void mp_stats(void)\r
137 {\r
138     struct ext4_mount_stats stats;\r
139     ext4_mount_point_stats("/mp/", &stats);\r
140 \r
141     printf("********************\n");\r
142     printf("ext4_mount_point_stats\n");\r
143     printf("inodes_count = %u\n", stats.inodes_count);\r
144     printf("free_inodes_count = %u\n", stats.free_inodes_count);\r
145     printf("blocks_count = %u\n", (uint32_t)stats.blocks_count);\r
146     printf("free_blocks_count = %u\n", (uint32_t)stats.free_blocks_count);\r
147     printf("block_size = %u\n", stats.block_size);\r
148     printf("block_group_count = %u\n", stats.block_group_count);\r
149     printf("blocks_per_group= %u\n", stats.blocks_per_group);\r
150     printf("inodes_per_group = %u\n", stats.inodes_per_group);\r
151     printf("volume_name = %s\n", stats.volume_name);\r
152 \r
153     printf("********************\n");\r
154 \r
155 }\r
156 \r
157 static void block_stats(void)\r
158 {\r
159     uint32_t i;\r
160 \r
161     printf("********************\n");\r
162     printf("ext4 blockdev stats\n");\r
163     printf("bdev->bread_ctr = %u\n", bd->bread_ctr);\r
164     printf("bdev->bwrite_ctr = %u\n", bd->bwrite_ctr);\r
165 \r
166 \r
167     printf("bcache->ref_blocks = %u\n", bc->ref_blocks);\r
168     printf("bcache->max_ref_blocks = %u\n", bc->max_ref_blocks);\r
169     printf("bcache->lru_ctr = %u\n", bc->lru_ctr);\r
170 \r
171     printf("\n");\r
172     for (i = 0; i < bc->cnt; ++i) {\r
173         printf("bcache->refctr[%d]= %u\n", i, bc->refctr[i]);\r
174     }\r
175 \r
176     printf("\n");\r
177     for (i = 0; i < bc->cnt; ++i) {\r
178         printf("bcache->lru_id[%d] = %u\n", i, bc->lru_id[i]);\r
179     }\r
180 \r
181     printf("\n");\r
182     for (i = 0; i < bc->cnt; ++i) {\r
183         printf("bcache->free_delay[%d] = %d\n", i, bc->free_delay[i]);\r
184     }\r
185 \r
186     printf("\n");\r
187     for (i = 0; i < bc->cnt; ++i) {\r
188         printf("bcache->lba[%d] = %u\n", i, (uint32_t)bc->lba[i]);\r
189     }\r
190 \r
191     printf("********************\n");\r
192 }\r
193 \r
194 static clock_t get_ms(void)\r
195 {\r
196     return hw_get_ms();\r
197 }\r
198 \r
199 static bool dir_test(int len)\r
200 {\r
201     ext4_file f;\r
202     int       r;\r
203     int       i;\r
204     char path[64];\r
205     clock_t diff;\r
206     clock_t stop;\r
207     clock_t start;\r
208     start = get_ms();\r
209 \r
210     printf("Directory create: /mp/dir1\n");\r
211     r = ext4_dir_mk("/mp/dir1");\r
212     if(r != EOK){\r
213         printf("Unable to create directory: /mp/dir1\n");\r
214         return false;\r
215     }\r
216 \r
217 \r
218     printf("Add files to: /mp/dir1\n");\r
219     for (i = 0; i < len; ++i) {\r
220         sprintf(path, "/mp/dir1/f%d", i);\r
221         r = ext4_fopen(&f, path, "wb");\r
222         if(r != EOK){\r
223             printf("Unable to create file in directory: /mp/dir1\n");\r
224             return false;\r
225         }\r
226     }\r
227 \r
228     stop =  get_ms();\r
229     diff = stop - start;\r
230     dir_ls("/mp/dir1");\r
231     printf("dir_test time: %d ms\n", (int)diff);\r
232     return true;\r
233 }\r
234 \r
235 \r
236 static bool file_test(void)\r
237 {\r
238     int r;\r
239     uint32_t  size;\r
240     ext4_file f;\r
241     int i;\r
242     clock_t start;\r
243     clock_t stop;\r
244     clock_t diff;\r
245     uint32_t kbps;\r
246     uint64_t size_bytes;\r
247     /*Add hello world file.*/\r
248     r = ext4_fopen(&f, "/mp/hello.txt", "wb");\r
249     r = ext4_fwrite(&f, "Hello World !\n", strlen("Hello World !\n"), 0);\r
250     r = ext4_fclose(&f);\r
251 \r
252 \r
253     printf("ext4_fopen: test1\n");\r
254 \r
255     start = get_ms();\r
256     r = ext4_fopen(&f, "/mp/test1", "wb");\r
257     if(r != EOK){\r
258         printf("ext4_fopen ERROR = %d\n", r);\r
259         return false;\r
260     }\r
261 \r
262     printf("ext4_write: %d * %d ...\n" , rw_szie, rw_count);\r
263     for (i = 0; i < rw_count; ++i) {\r
264 \r
265         memset(wr_buff, i % 10 + '0', rw_szie);\r
266 \r
267         r = ext4_fwrite(&f, wr_buff, rw_szie, &size);\r
268 \r
269         if((r != EOK) || (size != rw_szie))\r
270             break;\r
271 \r
272         if(!(i % (rw_count >> 3))){\r
273             printf("*");\r
274             fflush(stdout);\r
275         }\r
276     }\r
277 \r
278     if(i != rw_count){\r
279         printf("ERROR: rw_count = %d\n", i);\r
280         return false;\r
281     }\r
282 \r
283     printf(" OK\n");\r
284     stop = get_ms();\r
285     diff = stop - start;\r
286     size_bytes = rw_szie * rw_count;\r
287     size_bytes = (size_bytes * 1000) / 1024;\r
288     kbps = (size_bytes) / (diff + 1);\r
289     printf("file_test write time: %d ms\n", (int)diff);\r
290     printf("file_test write speed: %d KB/s\n", (int)kbps);\r
291     r = ext4_fclose(&f);\r
292     printf("ext4_fopen: test1\n");\r
293 \r
294 \r
295     start = get_ms();\r
296     r = ext4_fopen(&f, "/mp/test1", "r+");\r
297     if(r != EOK){\r
298         printf("ext4_fopen ERROR = %d\n", r);\r
299         return false;\r
300     }\r
301 \r
302     printf("ext4_read: %d * %d ...\n" , rw_szie, rw_count);\r
303 \r
304     for (i = 0; i < rw_count; ++i) {\r
305         memset(wr_buff, i % 10 + '0', rw_szie);\r
306         r = ext4_fread(&f, rd_buff, rw_szie, &size);\r
307 \r
308         if((r != EOK) || (size != rw_szie))\r
309             break;\r
310 \r
311         if(memcmp(rd_buff, wr_buff, rw_szie))\r
312             break;\r
313 \r
314 \r
315         if(!(i % (rw_count >> 3)))\r
316             printf("*");\r
317     }\r
318     if(i != rw_count){\r
319         printf("ERROR: rw_count = %d\n", i);\r
320         return false;\r
321     }\r
322     printf(" OK\n");\r
323     stop = get_ms();\r
324     diff = stop - start;\r
325     size_bytes = rw_szie * rw_count;\r
326     size_bytes = (size_bytes * 1000) / 1024;\r
327     kbps = (size_bytes) / (diff + 1);\r
328     printf("file_test read time: %d ms\n", (int)diff);\r
329     printf("file_test read speed: %d KB/s\n", kbps);\r
330     r = ext4_fclose(&f);\r
331 \r
332     return true;\r
333 \r
334 }\r
335 static void cleanup(void)\r
336 {\r
337     clock_t start;\r
338     clock_t stop;\r
339     clock_t diff;\r
340 \r
341     ext4_fremove("/mp/hello.txt");\r
342 \r
343     printf("cleanup: remove /mp/test1\n");\r
344     start = get_ms();\r
345     ext4_fremove("/mp/test1");\r
346     stop = get_ms();\r
347     diff = stop - start;\r
348     printf("cleanup: time: %d ms\n", (int)diff);\r
349 \r
350 \r
351     printf("cleanup: remove /mp/dir1\n");\r
352     start = get_ms();\r
353     ext4_dir_rm("/mp/dir1");\r
354     stop = get_ms();\r
355     diff = stop - start;\r
356     printf("cleanup: time: %d ms\n", (int)diff);\r
357 }\r
358 \r
359 static bool open_filedev(void)\r
360 {\r
361 \r
362     bd = ext4_usb_msc_get();\r
363     bc = ext4_usb_msc_cache_get();\r
364     if(!bd || !bc){\r
365         printf("Block device ERROR\n");\r
366         return false;\r
367     }\r
368     return true;\r
369 }\r
370 \r
371 static bool mount(void)\r
372 {\r
373     int r;\r
374 \r
375     if(!open_filedev())\r
376         return false;\r
377 \r
378     ext4_dmask_set(EXT4_DEBUG_ALL);\r
379 \r
380     r = ext4_device_register(bd, cache_mode ? 0 : bc, "ext4_filesim");\r
381     if(r != EOK){\r
382         printf("ext4_device_register ERROR = %d\n", r);\r
383         return false;\r
384     }\r
385 \r
386     r = ext4_mount("ext4_filesim", "/mp/");\r
387     if(r != EOK){\r
388         printf("ext4_mount ERROR = %d\n", r);\r
389         return false;\r
390     }\r
391 \r
392     return true;\r
393 }\r
394 \r
395 static bool umount(void)\r
396 {\r
397     int r = ext4_umount("/mp/");\r
398     if(r != EOK){\r
399         printf("ext4_umount: FAIL %d", r);\r
400         return false;\r
401     }\r
402     return true;\r
403 }\r
404 \r
405 \r
406 int main(void)\r
407 {\r
408     hw_init();\r
409 \r
410     setbuf(stdout, 0);\r
411     printf("Connect USB drive...\n");\r
412 \r
413     while(!hw_usb_connected())\r
414         hw_usb_process();\r
415     printf("USB drive connected\n");\r
416 \r
417     while(!hw_usb_enum_done())\r
418         hw_usb_process();\r
419     printf("USB drive enum done\n");\r
420 \r
421     hw_led_red(1);\r
422 \r
423     printf("Test conditions:\n");\r
424     printf("Imput name: %s\n", input_name);\r
425     printf("RW size: %d\n",  rw_szie);\r
426     printf("RW count: %d\n", rw_count);\r
427     printf("Cache mode: %s\n", cache_mode ? "dynamic" : "static");\r
428 \r
429 \r
430     hw_wait_ms(TEST_DELAY_MS);\r
431     if(!mount())\r
432         return EXIT_FAILURE;\r
433 \r
434     cleanup();\r
435 \r
436     if(sbstat){\r
437         hw_wait_ms(TEST_DELAY_MS);\r
438         mp_stats();\r
439     }\r
440 \r
441     hw_wait_ms(TEST_DELAY_MS);\r
442     dir_ls("/mp/");\r
443     if(!dir_test(dir_cnt))\r
444         return EXIT_FAILURE;\r
445 \r
446     hw_wait_ms(TEST_DELAY_MS);\r
447     if(!file_test())\r
448         return EXIT_FAILURE;\r
449 \r
450     dir_ls("/mp/");\r
451 \r
452     if(sbstat){\r
453         hw_wait_ms(TEST_DELAY_MS);\r
454         mp_stats();\r
455     }\r
456 \r
457     if(cleanup_flag){\r
458         hw_wait_ms(TEST_DELAY_MS);\r
459         cleanup();\r
460     }\r
461 \r
462     if(bstat){\r
463         hw_wait_ms(TEST_DELAY_MS);\r
464         block_stats();\r
465     }\r
466 \r
467     if(!umount())\r
468         return EXIT_FAILURE;\r
469 \r
470     printf("\nTest finished: OK\n");\r
471     printf("Press RESET to restart\n");\r
472 \r
473     while (1) {\r
474         hw_wait_ms(500);\r
475         hw_led_green(1);\r
476         hw_wait_ms(500);\r
477         hw_led_green(0);\r
478 \r
479     }\r
480 }\r
481 \r