comparison chipsetsw/drivers/drv_app/ffs/board/test.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /******************************************************************************
2 * Flash File System (ffs)
3 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
4 *
5 * ffs test core
6 *
7 * $Id: test.c 1.43.1.105.1.7 Thu, 18 Dec 2003 10:50:52 +0100 tsj $
8 *
9 ******************************************************************************/
10
11 #include "ffs/ffs.h"
12 #include "ffs/board/tffs.h"
13 #include "ffs/board/core.h"
14 #include "ffs/board/tmffs.h"
15 #include "ffs/board/ffstrace.h"
16
17 #define COMPILE_ON_WIN32 1
18
19 #if (COMPILE_ON_WIN32 == 0)
20 #include "ffs/board/pcm.h"
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <sys/types.h>
27 #include <stdarg.h>
28 #include "ffs/board/win32/getopt.h"
29 #include <errno.h>
30 #include <stdlib.h>
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #endif //nWIN32
35
36 #ifndef WIN32
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #endif //nWIN32
41
42 extern char *ffs_strerror(effs_t error);
43
44 /******************************************************************************
45 * Prototypes and Globals
46 ******************************************************************************/
47
48 static void main_args(int argc, char *argv[]);
49 static void main_usage(void);
50 static long arg_long_get(char *s);
51
52 #ifndef WIN32
53 static int ffs_server(char type);
54 #endif //nWIN32
55
56 char *arg_imagename = "image"; // flash image file to use for test
57 char *arg_test = "all"; // default test case to run
58 int arg_keepgoing = 0; // keep going on error
59 int arg_removeimage = 0; // remove image file first
60 char arg_server = 0;
61 char arg_pctm = 1;
62
63 int arg_trmask = TrTest; // trace mask
64 int arg_trspaces = 2; // indentation multiplier
65
66
67 /******************************************************************************
68 * Globals and Main
69 ******************************************************************************/
70
71 int main(int argc, char *argv[])
72 {
73 int result;
74
75 main_args(argc, argv);
76
77 tr_init(arg_trmask, arg_trspaces, NULL );
78
79 if (arg_server == 't' || arg_server == 'u') {
80 #ifdef WIN32
81 fprintf(stdout, "FFS_server not supported in WIN32");
82 #else //WIN32
83 result = ffs_server(arg_server);
84 #endif //WIN32
85 }
86 else {
87 test_init(arg_keepgoing);
88 result = test_run(arg_test);
89 test_exit();
90 fprintf(stdout, "TEST TOTAL cases failed: %d\n", result);
91 if (result > 0)
92 fprintf(stderr, "TEST TOTAL cases failed: %d\n", result);
93 }
94
95 exit(0);
96 }
97
98
99 /******************************************************************************
100 * Command line parsing
101 ******************************************************************************/
102
103 static void main_args_debug(char *optarg)
104 {
105 char sign;
106 unsigned int mask;
107
108 while (*optarg) {
109 sign = '+';
110 mask = 0;
111 if (*optarg == '+' || *optarg == '-')
112 sign = *optarg++;
113 if (*optarg) {
114 switch (*optarg) {
115 case 't': mask = TrTest; break;
116 case 'x': mask = TrTestHigh; break;
117 case 'y': mask = TrTestLow; break;
118 case 'b': mask = TrBstat; break;
119 case 'a': mask = TrApi; break;
120 case 'f': mask = TrFsck; break;
121 case 'F': mask = TrFsckLow; break;
122 case 'o': mask = TrObject; break;
123 case 'i': mask = TrIReclaim; break;
124 case 'd': mask = TrDReclaim; break;
125 case 'r': mask = TrReclaimLow; break;
126 case 'w': mask = TrDrvWrite; break;
127 case 'e': mask = TrDrvErase; break;
128 case 'j': mask = TrJournal; break;
129 case 'h': mask = TrUtil; break;
130 case 's': mask = TrServer; break;
131 case '?': mask = TrTrace; break;
132 }
133 switch (*optarg) {
134 case '*':
135 arg_trmask = TrAll &
136 // ~(TrTrace|TrTmffs|TrDrvWrite|TrDrvErase|TrJournal|TrUtil|TrBstat);
137 ~(TrTrace|TrDrvWrite|TrDrvErase|TrJournal|TrUtil|TrBstat);
138 break;
139 default:
140 if (sign == '+')
141 arg_trmask |= mask;
142 else
143 arg_trmask &= ~mask;
144 break;
145 }
146 optarg++;
147 }
148 }
149 printf("arg_trmask = 0x%X\n", arg_trmask);
150 }
151
152 static void main_usage(void)
153 {
154 printf("Usage: ffs [OPTIONS...]\n"
155 "\n"
156 /* $Format: "\"Revision $ProjectVersion$, \""$ */
157 "Revision 5.53, "
158 __DATE__ ".\n"
159 "\n"
160 "OPTIONS:\n"
161 " -c <tcase> Semi-colon separated list of test cases to run.\n"
162 " Each test case name can be suffixed with one or more\n"
163 " numerical parameter(s).\n"
164 " Default is 'all'.\n"
165 " -f <n> Flash image file name. Default is 'image'.\n"
166 " -l List all test cases\n"
167 " -t <char> Trace mask in human format. Same as '-t <char>'.\n"
168 " Default is 't'.\n"
169 " * = All\n"
170 " t = Test\n"
171 " x = TestHigh\n"
172 " y = TestLow\n"
173 " b = Bstat\n"
174 " a = Api\n"
175 " f = Fsck\n"
176 " F = FsckLow\n"
177 " i = IReclaim\n"
178 " d = DReclaim\n"
179 " r = ReclaimLow\n"
180 " j = Journalling\n"
181 " w = DrvWrite\n"
182 " e = DrvErase\n"
183 " h = Helper/Utility Functions\n"
184 " s = FFS Server\n"
185 " 0 = None\n"
186 " -d i<n> Trace indentation multiplier. Default is 4.\n"
187 " -d m<n> Trace mask. Default is 0.\n"
188 " -d s<n> Trace indentation multiplier (spaces). Default is 4.\n"
189 " -s <char> Start as an FFS server, using either TCP or UDP:\n"
190 " t = tcp (default)\n"
191 " u = udp\n"
192 " -k Keep going on error\n"
193 " -r Remove flash image file first\n"
194 " -h Display this help\n");
195
196 exit(0);
197 }
198
199 static void main_args(int argc, char *argv[])
200 {
201 char c;
202
203 while ( (c = getopt(argc, argv, "c:f:krls:t:d:h")) != -1)
204 {
205 switch (c)
206 {
207 case 'c': arg_test = optarg; break;
208 case 'f': arg_imagename = optarg; break;
209 case 'k': arg_keepgoing = 1; break;
210 case 'r': arg_removeimage = 1; break;
211 case 'l': test_listall(); exit(0); break;
212 case 's': arg_server = *optarg; break;
213 case 't':
214 main_args_debug(optarg);
215 break;
216 case 'd':
217 switch(*optarg) {
218 case 's': arg_trspaces = arg_long_get(optarg); break;
219 case 'm': arg_trmask = arg_long_get(optarg); break;
220 }
221 break;
222 case 'h': main_usage(); break;
223 default:
224 main_usage();
225 }
226 }
227 }
228
229 static long arg_long_get(char *s)
230 {
231 long value;
232 char *endp;
233
234 errno = 0;
235 value = strtol(optarg, &endp, 0);
236
237 if (errno == ERANGE || *endp != 0) {
238 fprintf(stderr, "Invalid command line argument value: '%s'", s);
239 exit(1);
240 }
241 return value;
242 }
243
244
245 /******************************************************************************
246 * Globals and Main
247 ******************************************************************************/
248 #ifndef WIN32
249
250 void server_error(char *msg)
251 {
252 perror(msg);
253 exit(1);
254 }
255
256 #define FFS_SERVER_PORT (21588)
257
258 #define INBUF_SIZE (2 * 65536)
259 #define OUTBUF_SIZE (2 * 65536)
260
261
262 static int ffs_server(char type)
263 {
264 char inbuf[INBUF_SIZE], *inbufp;
265 char outbuf[OUTBUF_SIZE], *outbufp;
266 int insize, outsize;
267 int error;
268
269 struct sockaddr_in myaddr;
270 struct sockaddr_in clientaddr;
271 int myfd, clientfd, addr_len;
272
273 error = ffs_initialize();
274
275 if (type == 'u')
276 {
277 // Code for using UDP datagrams
278
279 /* get an internet domain socket */
280 if ((myfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
281 server_error("socket()");
282 tr(TR_FUNC, TrServer, "socket()\n");
283
284 /* complete the socket structure */
285 memset(&myaddr, 0, sizeof(myaddr));
286 myaddr.sin_family = AF_INET;
287 myaddr.sin_addr.s_addr = INADDR_ANY;
288 myaddr.sin_port = htons(FFS_SERVER_PORT);
289
290 /* bind the socket to the port number */
291 if (bind(myfd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1)
292 server_error("bind()");
293 tr(TR_FUNC, TrServer, "bind(%d)\n", FFS_SERVER_PORT);
294
295 tr(TR_FUNC, TrServer,
296 "FFS UDP/IP server Waiting for client requests...\n");
297 for (;;)
298 {
299 addr_len = sizeof(struct sockaddr);
300 if ((insize = recvfrom(myfd, inbuf, sizeof(inbuf), 0,
301 (struct sockaddr *) &clientaddr,
302 &addr_len)) < 0)
303 server_error("recvfrom()");
304 tr(TR_FUNC, TrServer, "recv() %d bytes from %s\n",
305 insize, inet_ntoa(clientaddr.sin_addr));
306
307 outsize = tm_ffs(outbuf, OUTBUF_SIZE, inbuf, insize);
308
309 if(sendto(myfd, outbuf, outsize, 0,
310 (struct sockaddr *) &clientaddr,
311 sizeof(struct sockaddr)) < 0)
312 server_error("sendto()");
313 tr(TR_FUNC, TrServer, "send(%d)\n", outsize);
314 }
315 close(clientfd);
316 close(myfd);
317 }
318 else
319 {
320 // Code for using TCP/IP
321
322 /* get an internet domain socket */
323 if ((myfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
324 server_error("socket()");
325 tr(TR_FUNC, TrServer, "socket()\n");
326
327 /* complete the socket structure */
328 memset(&myaddr, 0, sizeof(myaddr));
329 myaddr.sin_family = AF_INET;
330 myaddr.sin_addr.s_addr = INADDR_ANY;
331 myaddr.sin_port = htons(FFS_SERVER_PORT);
332
333 /* bind the socket to the port number */
334 if (bind(myfd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1)
335 server_error("bind()");
336 tr(TR_FUNC, TrServer, "bind(%d)\n", FFS_SERVER_PORT);
337
338 /* show that we are willing to listen */
339 if (listen(myfd, 5) == -1)
340 server_error("listen()");
341 tr(TR_FUNC, TrServer, "listen()\n");
342
343 error = 0;
344 while (error == 0 || error == ESHUTDOWN)
345 {
346 tr(TR_FUNC, TrServer,
347 "FFS TCP/IP server waiting for client connection...\n");
348
349 if ((clientfd = accept(myfd, 0, 0)) < 0)
350 server_error("accept()");
351 tr(TR_FUNC, TrServer, "accept()\n");
352
353 error = 0;
354 while (error == 0)
355 {
356 inbufp = inbuf;
357 outbufp = outbuf;
358
359 if ((insize = read(clientfd, inbufp, sizeof(inbuf))) < 0) {
360 if (errno == ESHUTDOWN) {
361 error = ESHUTDOWN;
362 continue;
363 }
364 else {
365 server_error("recv()");
366 }
367 }
368 tr(TR_FUNC, TrServer, "recv() %d bytes\n", insize);
369
370 if (insize == 1 && inbuf[0] == 0)
371 break;
372
373 if (arg_pctm) {
374 inbufp++;
375 insize--;
376 outbufp = outbuf;
377 *outbufp++ = 0x70;
378 *outbufp++ = 0x00;
379 }
380
381 outsize = tm_ffs(outbufp, OUTBUF_SIZE, inbufp, insize);
382
383 if (arg_pctm) {
384 outsize += 2;
385 }
386 if (write(clientfd, outbuf, outsize) < 0)
387 server_error("send()");
388 tr(TR_FUNC, TrServer, "send(%d)\n", outsize);
389 }
390 close(clientfd);
391 }
392 close(myfd);
393 }
394
395 return(0);
396 }
397 #endif //nWIN32
398
399 /******************************************************************************
400 * Prototypes and Globals
401 ******************************************************************************/
402
403 void test_fatal_printf(char *format, ...)
404 {
405 va_list args;
406 static char buf[1024];
407
408 va_start(args, format);
409 vsprintf(buf, format, args);
410
411 fprintf(stderr, "%s", buf);
412
413 exit(1);
414 }
415
416