comparison rvinterf/lowlevel/rvifmain.c @ 176:7f727aaf5cd4

rvinterf: beginning of server implementation
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 07:40:13 +0000
parents rvinterf/lowlevel/rvtdump.c@3256dc6e84ae
children fef035264dd4
comparison
equal deleted inserted replaced
175:2f214bd03119 176:7f727aaf5cd4
1 /*
2 * This module contains the main() function for rvinterf
3 */
4
5 #include <sys/types.h>
6 #include <sys/errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <time.h>
11 #include "../pktmux.h"
12
13 extern int target_fd;
14 extern char *baudrate_name;
15
16 extern u_char rxpkt[];
17 extern size_t rxpkt_len;
18
19 char *logfname;
20 FILE *logF;
21 time_t logtime;
22 int background;
23
24 main(argc, argv)
25 char **argv;
26 {
27 extern char *optarg;
28 extern int optind;
29 int c;
30 fd_set fds;
31
32 while ((c = getopt(argc, argv, "bB:d:l:")) != EOF)
33 switch (c) {
34 case 'b':
35 background++;
36 continue;
37 case 'B':
38 baudrate_name = optarg;
39 continue;
40 case 'd':
41 target_fd = atoi(optarg);
42 continue;
43 case 'l':
44 logfname = optarg;
45 continue;
46 case '?':
47 default:
48 usage: fprintf(stderr,
49 "usage: %s [options] ttyport\n", argv[0]);
50 exit(1);
51 }
52 if (target_fd <= 0) {
53 if (argc - optind != 1)
54 goto usage;
55 open_target_serial(argv[optind]);
56 }
57
58 set_serial_nonblock(0);
59 setlinebuf(stdout);
60 if (logfname) {
61 logF = fopen(logfname, "w");
62 if (!logF) {
63 perror(logfname);
64 exit(1);
65 }
66 fprintf(logF, "*** Log of rvinterf session ***\n");
67 setlinebuf(logF);
68 }
69 if (background) {
70 c = fork();
71 if (c < 0) {
72 perror("fork");
73 exit(1);
74 }
75 if (c) {
76 printf("rvinterf forked into background (pid %d)\n", c);
77 exit(0);
78 }
79 }
80 for (;;) {
81 FD_ZERO(&fds);
82 FD_SET(target_fd, &fds);
83 c = select(target_fd+1, &fds, 0, 0, 0);
84 time(&logtime);
85 if (c < 0) {
86 if (errno == EINTR)
87 continue;
88 perror("select");
89 exit(1);
90 }
91 if (FD_ISSET(target_fd, &fds))
92 process_serial_rx();
93 }
94 }
95
96 handle_rx_packet()
97 {
98 switch (rxpkt[0]) {
99 case RVT_RV_HEADER:
100 if (rxpkt_len < 6)
101 goto unknown;
102 print_rv_trace();
103 return;
104 case RVT_L1_HEADER:
105 print_l1_trace();
106 return;
107 case RVT_L23_HEADER:
108 print_g23_trace();
109 return;
110 case RVT_TM_HEADER:
111 print_etm_output_raw();
112 return;
113 default:
114 unknown:
115 print_unknown_packet();
116 }
117 }