-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunslip.c
305 lines (283 loc) · 8.11 KB
/
tunslip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include "io.h"
#include "options.h"
#include "rs232.h"
#include "tun.h"
static void error(const char *e)
{
fprintf(stderr, "%s\n", e);
exit(1);
}
static int is_command(const char *a, const char *b)
{
int i = 0, la = 0, lb = 0;
while (a[i]) {
if (a[i] == '/') la = i + 1;
++i;
}
i = 0;
while (b[i]) {
if (b[i] == '/') lb = i + 1;
++i;
}
return !strcmp(a + la, b + lb);
}
static const char *program = "tunslip";
static void usage(const char *opt, const char *arg, int l)
{
if (is_command(program, "trillium")) {
fprintf(stderr,
"Serial-Tools Version " VERSION "\n"
"\n"
"Trillium opens a connection to a Nanometrics seismometer.\n"
"\n"
"Upon startup, the IP address and model info is printed to the console.\n"
"While the program is running, you can type 'NMX' to display the IP address\n"
"of the currently connected seismometer.\n"
"\n"
"# Usage\n"
"\n"
" %s [TTY]\n"
"\n"
" TTY defaults to /dev/ttyUSB0.\n"
"\n"
"# Troubleshooting\n"
"\n"
"You must probably be root in order to run the program.\n"
"\n",
program);
} else {
fprintf(stderr,
"\n"
"Usage: %s [-f|--fast] [-s|--slow] [-t|--trillium] /dev/ttyXXX\n",
program);
}
exit(1);
}
#define END 192 /* indicates end of packet */
#define ESC 219 /* indicates byte stuffing */
#define ESC_END 220 /* ESC ESC_END means END data byte */
#define ESC_ESC 221 /* ESC ESC_ESC means ESC data byte */
static const char *trillium_model(int model_number)
{
switch (model_number) {
case 20: return "Trillium 120";
case 21: return "Trillium 240";
case 22: return "Trillium 240 OBS";
case 23: return "Trillium Compact";
case 24: return "Titan";
case 28: return "Trillium Compact OBS";
case 29: return "Trillium Compact All Terrain";
case 43: return "Trillium Compact OBS";
default: return "Unknown Model";
}
}
enum {
COMMAND_DEFAULT = 0,
COMMAND_INVALID,
COMMAND_N,
COMMAND_NM,
COMMAND_NMX
};
int main(int argc, char **argv)
{
char dev[32] = "slip%d";
char *tty_path = "/dev/ttyUSB0";
int tty, tun;
int speed = 19200;
int trillium = 0;
ssize_t r;
unsigned char ip_packet[4096], slip_packet[4096], buffer[4096];
int received = 0, esc = 0, i, j;
int stdin_state = COMMAND_DEFAULT;
int found_trillium = 0;
time_t t = time(0), tt;
/* Parse options. */
program = argv[0];
if (is_command(argv[0], "trillium")) {
trillium = 1;
}
parse_options(&argc, &argv, OPTIONS(
FLAG('f', "fast", speed, 115200),
FLAG('s', "slow", speed, 9600),
FLAG('t', "trillium", trillium, 1),
FLAG_CALLBACK('h', "help", usage)
));
if (trillium) {
speed = 9600;
}
/* Check for TTY argument. */
if (argc >= 2) {
tty_path = argv[1];
}
/* Open TTY. */
if ((tty = rs232_open(tty_path, speed)) == -1) {
fprintf(stderr, "%s\n", "Could not open TTY.");
if (geteuid()) {
fprintf(stderr, "%s\n", "Please try again as root!");
}
return 1;
}
/* Open TUN. */
if ((tun = tun_alloc(dev, "2.1.0.0", "255.0.0.0")) < 0) {
fprintf(stderr, "%s\n", "Could not open TUN.");
if (geteuid()) {
fprintf(stderr, "%s\n", "Please try again as root!");
}
return 1;
}
io_nonblock(0);
io_nonblock(tun);
io_nonblock(tty);
if (trillium) {
fprintf(stderr,
"This is Serial-Tools Version " VERSION ".\n"
"\n"
"Leave this program running to communicate with the Trillium Seismometer.\n"
"Once you are finished, you can close this program with Ctrl+C.\n"
"\n"
"Searching for Trillium Seismometer ...\n");
io_write_sync(tty, "\300NMX\300", 5);
}
/* IO loop. */
while (1) {
io_wantread(0);
io_wantread(tun);
io_wantread(tty);
if (io_wait(1000000) < 0) error("IO error. Could not wait.");
/* Read commands from stdin. */
if (io_canread(0)) {
r = io_read(0, buffer, sizeof(buffer));
if (r == -3) {
error("IO error. Could not read stdin.");
} else if (r > 0) {
/* Data. */
for (i = 0; i < r; ++i) {
switch (stdin_state) {
case COMMAND_DEFAULT:
stdin_state = (buffer[i] == 'N' || buffer[i] == 'n') ? COMMAND_N : COMMAND_INVALID;
break;
case COMMAND_N:
stdin_state = (buffer[i] == 'M' || buffer[i] == 'm') ? COMMAND_NM : COMMAND_INVALID;
break;
case COMMAND_NM:
stdin_state = (buffer[i] == 'X' || buffer[i] == 'x') ? COMMAND_NMX : COMMAND_INVALID;
break;
case COMMAND_NMX:
if (buffer[i] == '\n') {
io_write_sync(tty, "\300NMX\300", 5);
stdin_state = COMMAND_DEFAULT;
} else if (buffer[i] != '\r') {
stdin_state = COMMAND_INVALID;
}
}
if (buffer[i] == '\n') {
stdin_state = COMMAND_DEFAULT;
}
}
} else if (r == 0) {
/* Closed. */
printf("\033[KClosed.\n");
goto end;
}
}
/* Write TUN data to RS232. */
if (io_canread(tun)) {
r = io_read(tun, buffer, sizeof(buffer));
if (r == -3) {
error("IO error. Could not read TUN.");
} else if (r > 0) {
/* Data. */
j = 0;
if (j < sizeof(slip_packet)) slip_packet[j++] = END;
for (i = 0; i < r; ++i) {
switch (buffer[i]) {
case END:
if (j < sizeof(slip_packet)) slip_packet[j++] = ESC;
if (j < sizeof(slip_packet)) slip_packet[j++] = ESC_END;
break;
case ESC:
if (j < sizeof(slip_packet)) slip_packet[j++] = ESC;
if (j < sizeof(slip_packet)) slip_packet[j++] = ESC_ESC;
break;
default:
if (j < sizeof(slip_packet)) slip_packet[j++] = buffer[i];
break;
}
}
if (j < sizeof(slip_packet)) slip_packet[j++] = END;
if (io_write_sync(tty, slip_packet, j) < 0) error("IO error. Could not write to TTY.");
} else if (r == 0) {
/* Closed. */
printf("\033[KClosed.\n");
goto end;
}
}
/* Write RS232 data to TUN. */
if (io_canread(tty)) {
r = io_read(tty, buffer, sizeof(buffer));
if (r == -3) {
error("IO error. Could not read TTY.");
} else if (r > 0) {
/* Data. */
for (i = 0; i < r; ++i) {
switch (buffer[i]) {
case END:
if (received) {
if (received == 8 && !memcmp(ip_packet, "NMX", 3)) {
printf("%s http://2.%d.%d.%d/\n", trillium_model(ip_packet[3]), (int) ip_packet[3], (int) ip_packet[4], (int) ip_packet[5]);
fflush(stdout);
found_trillium = 1;
} else {
if (io_write_sync(tun, ip_packet, received) < 0) error("IO error. Could not write to TUN.");
}
received = 0;
}
esc = 0;
break;
case ESC:
esc = 1;
break;
case ESC_END:
if (received < sizeof(ip_packet)) {
ip_packet[received++] = esc ? END : ESC_END;
}
esc = 0;
break;
case ESC_ESC:
if (received < sizeof(ip_packet)) {
ip_packet[received++] = esc ? ESC : ESC_ESC;
}
esc = 0;
break;
default:
if (received < sizeof(ip_packet)) {
ip_packet[received++] = buffer[i];
}
esc = 0;
break;
}
}
} else if (r == 0) {
/* Closed. */
printf("\033[KClosed.\n");
goto end;
}
}
/* Send NMX if no Trillium found. */
if (trillium && !found_trillium) {
if ((tt = time(0)) > t) {
io_write_sync(tty, "\300NMX\300", 5);
t = tt;
}
}
}
end:
io_close(tty);
return 0;
}