comparison rvinterf/old/rvtdump.c @ 173:f42854da4563

rvinterf: beginning of refactoring
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 22 Nov 2013 05:56:07 +0000
parents rvinterf/rvtdump.c@4d8e4c58df71
children
comparison
equal deleted inserted replaced
172:019120585a1c 173:f42854da4563
1 /*
2 * This program reads bytes from a serial port, parses them assuming
3 * TI's RVT MUX format, and prints every decoded packet.
4 */
5
6 #include <sys/types.h>
7 #include <sys/errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <time.h>
12
13 extern int target_fd;
14 extern char *baudrate_name;
15
16 extern char pr_item[];
17 extern int sendsock_fd;
18
19 char *logfname;
20 FILE *logF;
21 time_t logtime;
22 int background, sendsock_enable;
23
24 main(argc, argv)
25 char **argv;
26 {
27 extern char *optarg;
28 extern int optind;
29 int c, maxfd;
30 fd_set fds;
31
32 while ((c = getopt(argc, argv, "bB:d:l:s")) != 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 's':
47 sendsock_enable++;
48 continue;
49 case '?':
50 default:
51 usage: fprintf(stderr,
52 "usage: %s [options] ttyport\n", argv[0]);
53 exit(1);
54 }
55 if (background && !logfname) {
56 fprintf(stderr, "%s: -b is meaningless without -l\n", argv[0]);
57 exit(1);
58 }
59 if (target_fd <= 0) {
60 if (argc - optind != 1)
61 goto usage;
62 open_target_serial(argv[optind]);
63 }
64
65 set_serial_nonblock(0);
66 setlinebuf(stdout);
67 if (logfname) {
68 logF = fopen(logfname, "w");
69 if (!logF) {
70 perror(logfname);
71 exit(1);
72 }
73 fprintf(logF, "*** Log of decoded RVT output ***\n");
74 setlinebuf(logF);
75 }
76 if (sendsock_enable)
77 create_send_socket();
78 if (background) {
79 c = fork();
80 if (c < 0) {
81 perror("fork");
82 exit(1);
83 }
84 if (c) {
85 printf("rvtdump forked into background (pid %d)\n", c);
86 exit(0);
87 }
88 }
89 maxfd = target_fd;
90 if (sendsock_fd > maxfd)
91 maxfd = sendsock_fd;
92 for (;;) {
93 FD_ZERO(&fds);
94 FD_SET(target_fd, &fds);
95 if (sendsock_enable)
96 FD_SET(sendsock_fd, &fds);
97 c = select(maxfd+1, &fds, 0, 0, 0);
98 time(&logtime);
99 if (c < 0) {
100 if (errno == EINTR)
101 continue;
102 perror("select");
103 exit(1);
104 }
105 if (FD_ISSET(target_fd, &fds))
106 process_serial_rx();
107 if (FD_ISSET(sendsock_fd, &fds))
108 handle_sendsock();
109 }
110 }
111
112 handle_rx_packet()
113 {
114 print_rx_packet();
115 }
116
117 print_item()
118 {
119 if (!background)
120 printf("%s\n", pr_item);
121 if (logF)
122 log_item();
123 }