forked from jesseschalken/simple-serial-console
/
main.c
executable file
·347 lines (302 loc) · 6.41 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdbool.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <getopt.h>
#define ESCAPE_CHAR 0x1d
#define EXIT_CHAR '.'
#define CTRL_C_CHAR 3
#define RESET_CHAR 'x'
#define DTR_TOGGLE_CHAR 'd'
#define RTS_TOGGLE_CHAR 'r'
#define BREAK_CHAR 'b'
#define RESET_SEQUENCE "\033c"
#define DTR_INIT_NONE 0
#define DTR_INIT_HIGH 1
#define DTR_INIT_LOW 2
#define DTR_PULSE_WIDTH 100
#define RTS_INIT_NONE 0
#define RTS_INIT_HIGH 1
#define RTS_INIT_LOW 2
#define RTS_PULSE_WIDTH 100
static int in;
static int out;
static int terminal;
static struct termios old_input_options;
static int escape_state;
static int speed;
static char *terminal_device;
static int opt_ascii = 0;
static int opt_reset = 0;
static int opt_exit_ctrl_c = 0;
static int opt_translate = 0;
static int opt_flow_control = 0;
static int opt_dtr = DTR_INIT_NONE;
static int opt_rts = RTS_INIT_NONE;
static char *options = "acxtdDrRf";
void set_modem_lines(int on, int lines)
{
unsigned int status;
ioctl(terminal, TIOCMGET, &status);
if (on)
status |= lines;
else
status &= ~lines;
ioctl(terminal, TIOCMSET, &status);
}
int clear_to_send(void)
{
unsigned int status;
ioctl(terminal, TIOCMGET, &status);
return !(status & TIOCM_CTS);
}
void toggle(int line, int active_low, int delay_ms)
{
set_modem_lines(!active_low, line);
usleep(delay_ms * 1000);
set_modem_lines(active_low, line);
usleep(delay_ms * 1000);
set_modem_lines(!active_low, line);
}
void send_break(void)
{
tcsendbreak(terminal, 0);
}
void send_data(char *data, unsigned int count)
{
while (opt_flow_control && !clear_to_send());
write(terminal, data, count);
}
bool transfer_to_terminal(void)
{
char c;
bool r = true;
int byte_count;
byte_count = read(in, &c, sizeof(c));
if (byte_count > 0) {
if (!escape_state) {
if (opt_exit_ctrl_c && c == CTRL_C_CHAR) {
r = false;
} else if (c != ESCAPE_CHAR) {
if (!opt_translate || c != '\n') {
send_data(&c, byte_count);
} else {
send_data("\r\n", 2);
}
} else {
escape_state = 1;
}
} else {
switch (c) {
case EXIT_CHAR:
r = false;
break;
case RESET_CHAR:
write(out, RESET_SEQUENCE,
sizeof(RESET_SEQUENCE));
break;
case DTR_TOGGLE_CHAR:
toggle(TIOCM_DTR, opt_dtr == DTR_INIT_HIGH, DTR_PULSE_WIDTH);
break;
case RTS_TOGGLE_CHAR:
toggle(TIOCM_RTS, opt_rts == RTS_INIT_HIGH, RTS_PULSE_WIDTH);
break;
case BREAK_CHAR:
send_break();
break;
default:
send_data(&c, byte_count);
}
escape_state = 0;
}
} else {
r = false;
}
return r;
}
bool transfer_from_terminal(void)
{
unsigned char buffer[512];
int i;
int byte_count;
bool r = true;
byte_count = read(terminal, buffer, sizeof(buffer));
if (byte_count > 0) {
if (opt_ascii) {
for (i = 0; i < byte_count; i++) {
if (buffer[i] > 128 ||
(buffer[i] < ' ' &&
buffer[i] != '\t' && buffer[i] != '\r' && buffer[i] != '\n'))
buffer[i] = '.';
}
}
write(out, buffer, byte_count);
} else {
r = false;
}
return r;
}
void configure_input(void)
{
struct termios options;
tcgetattr(in, &options);
old_input_options = options;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
options.c_cflag |= CLOCAL | CREAD | HUPCL;
options.c_cflag &= ~(IXON | IXOFF);
options.c_lflag = 0;
tcflush(in, TCIFLUSH);
tcsetattr(in, TCSANOW, &options);
}
void configure_terminal(int baud)
{
struct termios options;
tcgetattr(terminal, &options);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~INLCR & ~ICRNL & ~INPCK;
options.c_cflag &= ~(IXON | IXOFF);
options.c_iflag |= IGNPAR;
options.c_cflag &= ~PARENB & ~CSTOPB & ~CSIZE & ~CRTSCTS & ~IXANY;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
options.c_cflag |= CLOCAL | CREAD | HUPCL | CS8;
options.c_lflag = 0;
cfsetspeed(&options, baud);
tcflush(terminal, TCIFLUSH);
tcsetattr(terminal, TCSANOW, &options);
set_modem_lines(0,
(opt_dtr == DTR_INIT_HIGH ? TIOCM_DTR : 0) |
(opt_rts == RTS_INIT_HIGH ? TIOCM_RTS : 0));
set_modem_lines(1,
(opt_dtr == DTR_INIT_LOW ? TIOCM_DTR : 0) |
(opt_rts == RTS_INIT_LOW ? TIOCM_RTS : 0));
}
void unconfigure_input(void)
{
tcsetattr(in, TCSANOW, &old_input_options);
}
void signal_handler(int signum)
{
switch (signum) {
case SIGQUIT:
case SIGINT:
case SIGTERM:
unconfigure_input();
break;
default:
break;
}
}
void setup_signal_handlers(void)
{
signal(SIGQUIT, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
}
void handle_cmd_line(int argc, char **argv)
{
int c;
struct stat st;
while ((c = getopt(argc, argv, options)) != -1) {
switch (c) {
case 'a':
opt_ascii = 1;
break;
case 'x':
opt_reset = 1;
break;
case 'c':
opt_exit_ctrl_c = 1;
break;
case 'f':
opt_flow_control = 1;
break;
case 't':
opt_translate = 1;
break;
case 'd':
opt_dtr = DTR_INIT_HIGH;
break;
case 'D':
opt_dtr = DTR_INIT_LOW;
break;
case 'r':
opt_rts = RTS_INIT_HIGH;
break;
case 'R':
opt_rts = RTS_INIT_LOW;
break;
case '?':
exit(1);
break;
default:
break;
}
}
if (optind < argc) {
terminal_device = argv[optind];
if (stat(terminal_device, &st) != 0) {
perror(terminal_device);
exit(2);
}
optind++;
} else {
printf("Please specify a terminal.\n");
exit(1);
}
if (optind < argc)
speed = atoi(argv[optind]);
else
speed = 115200;
}
int main(int argc, char **argv)
{
struct timeval timeout;
fd_set readfds;
handle_cmd_line(argc, argv);
in = dup(0);
out = dup(1);
terminal = open(terminal_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (terminal == -1) {
perror("Failed to open terminal");
exit(3);
}
configure_terminal(speed);
configure_input();
setup_signal_handlers();
escape_state = 0;
for (;;) {
FD_ZERO(&readfds);
FD_SET(terminal, &readfds);
FD_SET(in, &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
if (select(FD_SETSIZE, &readfds, NULL, NULL, &timeout) == -1) {
break;
} else {
if (FD_ISSET(terminal, &readfds) &&
transfer_from_terminal() == false)
break;
if (FD_ISSET(in, &readfds) &&
transfer_to_terminal() == false)
break;
}
}
unconfigure_input();
if (opt_reset)
write(out, RESET_SEQUENCE, sizeof(RESET_SEQUENCE));
close(terminal);
close(in);
close(out);
return 0;
}