Demo apps refactoring
[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 #include <time.h>\r
41 \r
42 #include <usb_msc_lwext4.h>\r
43 #include <ext4.h>\r
44 #include "../../blockdev/test_lwext4.h"\r
45 \r
46 \r
47 /**@brief   Read-write size*/\r
48 #define READ_WRITE_SZIZE 1024 * 16\r
49 \r
50 /**@brief   Delay test (slower LCD scroll)*/\r
51 #define TEST_DELAY_MS    1000\r
52 \r
53 /**@brief   Input stream name.*/\r
54 char input_name[128] = "ext2";\r
55 \r
56 /**@brief   Read-write size*/\r
57 static int rw_szie  = READ_WRITE_SZIZE;\r
58 \r
59 /**@brief   Read-write size*/\r
60 static int rw_count = 100;\r
61 \r
62 /**@brief   Directory test count*/\r
63 static int dir_cnt  = 100;\r
64 \r
65 /**@brief   Static or dynamic cache mode*/\r
66 static bool cache_mode = false;\r
67 \r
68 /**@brief   Cleanup after test.*/\r
69 static bool cleanup_flag = false;\r
70 \r
71 /**@brief   Block device stats.*/\r
72 static bool bstat = false;\r
73 \r
74 /**@brief   Superblock stats.*/\r
75 static bool sbstat = false;\r
76 \r
77 /**@brief   Block device handle.*/\r
78 static struct ext4_blockdev *bd;\r
79 \r
80 /**@brief   Block cache handle.*/\r
81 static struct ext4_bcache   *bc;\r
82 \r
83 static bool open_filedev(void)\r
84 {\r
85 \r
86         bd = ext4_usb_msc_get();\r
87         bc = ext4_usb_msc_cache_get();\r
88         if(!bd || !bc){\r
89                 printf("open_filedev: fail\n");\r
90                 return false;\r
91         }\r
92         return true;\r
93 }\r
94 \r
95 int main(void)\r
96 {\r
97         hw_init();\r
98 \r
99         setbuf(stdout, 0);\r
100         printf("connect usb drive...\n");\r
101 \r
102         while(!hw_usb_connected())\r
103                 hw_usb_process();\r
104         printf("usb drive connected\n");\r
105 \r
106         while(!hw_usb_enum_done())\r
107                 hw_usb_process();\r
108         printf("usb drive enum done\n");\r
109 \r
110         hw_led_red(1);\r
111 \r
112         printf("test conditions:\n");\r
113         printf("  rw size: %d\n",  rw_szie);\r
114         printf("  rw count: %d\n", rw_count);\r
115         printf("  cache mode: %s\n", cache_mode ? "dynamic" : "static");\r
116 \r
117         if (!open_filedev())\r
118                 goto Finish;\r
119 \r
120         tim_wait_ms(TEST_DELAY_MS);\r
121         if(!test_lwext4_mount(bd, bc))\r
122                 return EXIT_FAILURE;\r
123 \r
124         tim_wait_ms(TEST_DELAY_MS);\r
125 \r
126         ext4_cache_write_back("/mp/", 1);\r
127         test_lwext4_cleanup();\r
128 \r
129         if(sbstat){\r
130                 tim_wait_ms(TEST_DELAY_MS);\r
131                 test_lwext4_mp_stats();\r
132         }\r
133 \r
134         tim_wait_ms(TEST_DELAY_MS);\r
135         test_lwext4_dir_ls("/mp/");\r
136         if(!test_lwext4_dir_test(dir_cnt))\r
137                 return EXIT_FAILURE;\r
138 \r
139         tim_wait_ms(TEST_DELAY_MS);\r
140         if(!test_lwext4_file_test(rw_szie, rw_count))\r
141                 return EXIT_FAILURE;\r
142 \r
143         if(sbstat){\r
144                 tim_wait_ms(TEST_DELAY_MS);\r
145                 test_lwext4_mp_stats();\r
146         }\r
147 \r
148         if(cleanup_flag){\r
149                 tim_wait_ms(TEST_DELAY_MS);\r
150                 test_lwext4_cleanup();\r
151         }\r
152 \r
153         if(bstat){\r
154                 tim_wait_ms(TEST_DELAY_MS);\r
155                 test_lwext4_block_stats();\r
156         }\r
157 \r
158         ext4_cache_write_back("/mp/", 0);\r
159         if(!test_lwext4_umount())\r
160                 return EXIT_FAILURE;\r
161 \r
162         printf("press RESET button to restart\n");\r
163         Finish:\r
164         while (1) {\r
165                 tim_wait_ms(500);\r
166                 hw_led_green(1);\r
167                 tim_wait_ms(500);\r
168                 hw_led_green(0);\r
169 \r
170         }\r
171 }\r
172 \r