comparison rvinterf/lowlevel/rvifmain.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 7168f63fc3b8
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
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 <string.h>
9 #include <strings.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <time.h>
14 #include "../include/pktmux.h"
15 #include "../include/localsock.h"
16 #include "client.h"
17
18 extern int target_fd, listener;
19 extern char *baudrate_name;
20 extern char *extlcd_program;
21 extern u_char extlcd_invert;
22
23 extern u_char rxpkt[];
24 extern size_t rxpkt_len;
25
26 struct client *client_head;
27 int socketpair_fd;
28
29 char *logfname;
30 FILE *logF;
31 time_t logtime;
32 int background, no_output;
33 int max_fd;
34
35 char *socket_pathname = "/tmp/rvinterf_socket";
36
37 int wakeup_after_sec = 7;
38
39 main(argc, argv)
40 char **argv;
41 {
42 extern char *optarg;
43 extern int optind;
44 int c;
45 fd_set fds;
46 struct client *cli, **clip;
47
48 while ((c = getopt(argc, argv, "bB:d:l:ns:S:vw:X:")) != EOF)
49 switch (c) {
50 case 'b':
51 background++;
52 /* FALL THRU */
53 case 'n':
54 no_output++;
55 continue;
56 case 'B':
57 baudrate_name = optarg;
58 continue;
59 case 'd':
60 target_fd = atoi(optarg);
61 continue;
62 case 'l':
63 logfname = optarg;
64 continue;
65 case 's':
66 socket_pathname = optarg;
67 continue;
68 case 'S':
69 socketpair_fd = atoi(optarg);
70 continue;
71 case 'v':
72 extlcd_invert = 0xFF;
73 continue;
74 case 'w':
75 wakeup_after_sec = strtoul(optarg, 0, 0);
76 continue;
77 case 'X':
78 extlcd_program = optarg;
79 continue;
80 case '?':
81 default:
82 usage: fprintf(stderr,
83 "usage: %s [options] ttyport\n", argv[0]);
84 exit(1);
85 }
86 if (target_fd <= 0) {
87 if (argc - optind != 1)
88 goto usage;
89 open_target_serial(argv[optind]);
90 }
91 max_fd = target_fd;
92 if (extlcd_program)
93 open_extlcd_pipe();
94
95 set_serial_nonblock(0);
96 setlinebuf(stdout);
97 if (logfname) {
98 logF = fopen(logfname, "w");
99 if (!logF) {
100 perror(logfname);
101 exit(1);
102 }
103 setlinebuf(logF);
104 fprintf(logF, "*** Log of rvinterf session ***\n");
105 }
106 if (socketpair_fd)
107 create_socketpair_client();
108 else
109 create_listener_socket();
110 if (background) {
111 c = fork();
112 if (c < 0) {
113 perror("fork");
114 exit(1);
115 }
116 if (c) {
117 printf("rvinterf forked into background (pid %d)\n", c);
118 exit(0);
119 }
120 }
121 signal(SIGPIPE, SIG_IGN);
122 for (;;) {
123 FD_ZERO(&fds);
124 FD_SET(target_fd, &fds);
125 if (listener)
126 FD_SET(listener, &fds);
127 for (clip = &client_head; cli = *clip; ) {
128 if (cli->rx_state == 2) {
129 close(cli->fd);
130 *clip = cli->next;
131 free(cli);
132 continue;
133 }
134 FD_SET(cli->fd, &fds);
135 clip = &cli->next;
136 }
137 if (socketpair_fd && !client_head)
138 exit(0);
139 c = select(max_fd+1, &fds, 0, 0, 0);
140 time(&logtime);
141 if (c < 0) {
142 if (errno == EINTR)
143 continue;
144 perror("select");
145 exit(1);
146 }
147 if (FD_ISSET(target_fd, &fds))
148 process_serial_rx();
149 if (listener && FD_ISSET(listener, &fds))
150 handle_listener_select();
151 for (cli = client_head; cli; cli = cli->next)
152 if (FD_ISSET(cli->fd, &fds))
153 handle_client_select(cli);
154 }
155 }
156
157 handle_rx_packet()
158 {
159 switch (rxpkt[0]) {
160 case RVT_RV_HEADER:
161 if (rxpkt_len < 6)
162 goto unknown;
163 if (!no_output || logF)
164 print_rv_trace();
165 if (client_head)
166 forward_rv_trace();
167 return;
168 case RVT_L1_HEADER:
169 if (!no_output || logF)
170 print_l1_trace();
171 if (client_head)
172 forward_nonrvt_pkt();
173 return;
174 case RVT_L23_HEADER:
175 if (!no_output || logF)
176 print_g23_trace();
177 if (client_head)
178 forward_nonrvt_pkt();
179 return;
180 case RVT_TM_HEADER:
181 if (!no_output || logF)
182 print_tm_output_raw();
183 if (client_head)
184 forward_nonrvt_pkt();
185 return;
186 case RVT_AT_HEADER:
187 if (!no_output || logF)
188 print_ati_output();
189 if (client_head)
190 forward_nonrvt_pkt();
191 return;
192 case RVT_EXTUI_HEADER:
193 if (rxpkt_len < 5 || !(rxpkt_len & 1))
194 goto unknown;
195 if (extlcd_program)
196 output_to_extlcd();
197 else
198 report_extui_packet();
199 return;
200 case RVT_TCH_HEADER:
201 if (!no_output || logF)
202 print_tch_output_raw();
203 if (client_head)
204 forward_nonrvt_pkt();
205 return;
206 case '*':
207 print_fc_lld_msg();
208 return;
209 default:
210 unknown:
211 print_unknown_packet();
212 }
213 }