-
Notifications
You must be signed in to change notification settings - Fork 13
/
seriald.c
399 lines (346 loc) · 9.19 KB
/
seriald.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/eventfd.h>
#include <sys/types.h>
#include <unistd.h>
#include "fdio.h"
#include "seriald.h"
#include "term.h"
#include "ubus.h"
#include "ubus_loop.h"
int efd_notify_tty = -1;
int efd_signal = -1;
int ubus_pipefd[2];
static int fd_tty;
#define STO STDOUT_FILENO
#define STI STDIN_FILENO
#define TTY_WRITE_SZ_DIV 10
#define TTY_WRITE_SZ_MIN 8
int tty_write_sz;
#define set_tty_write_sz(baud) \
do { \
tty_write_sz = (baud) / TTY_WRITE_SZ_DIV; \
if (tty_write_sz < TTY_WRITE_SZ_MIN) tty_write_sz = TTY_WRITE_SZ_MIN; \
} while (0)
struct tty_q tty_q;
pthread_mutex_t tty_q_mutex;
int sig_exit = 0;
static struct {
char port[128];
int baud;
enum flowcntrl_e flow;
enum parity_e parity;
int databits;
int stopbits;
int noreset;
char *socket;
} opts = {
.port = "",
.baud = 115200,
.flow = FC_NONE,
.parity = P_NONE,
.databits = 8,
.stopbits = 1,
.noreset = 0,
.socket = NULL, /* the library fall back to default socket when it is NULL */
};
char ubus_path[sizeof("serial.")+sizeof(opts.port)];
static void show_usage(void);
static void parse_args(int argc, char *argv[]);
static void deadly_handler(int signum);
static void register_signal_handlers(void);
static void loop(void);
static void tty_read_line_splitter(const int n, const char *buff_rd);
static void tty_read_line_cb(const char *line);
int main(int argc, char *argv[]);
static void show_usage()
{
printf("Usage: seriald [options] <TTY device>\n");
printf("\n");
printf("Options:\n");
printf(" -b <baudrate>\n");
printf(" baudrate should be one of: 0, 50, 75, 110, 134, 150, 200, 300, 600,\n");
printf(" 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400\n");
printf(" default to 115200\n");
printf(" -f s (=soft) | h (=hard) | n (=none)\n");
printf(" default to n\n");
printf(" -s <ubus socket>\n");
printf(" no need to give if you use the default one\n");
printf("\n");
printf("Examples:\n");
printf(" Get data:\n");
printf(" ubus listen serial\n");
printf(" Send data (replace <data> with the data you want to send):\n");
printf(" ubus call serial send '{\"data\": \"<data>\"}'\n");
}
void fatal(const char *format, ...)
{
char *s, buf[256];
va_list args;
int len;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
buf[sizeof(buf)-1] = '\0';
va_end(args);
s = "\r\nFATAL: ";
writen_ni(STO, s, strlen(s));
writen_ni(STO, buf, len);
s = "\r\n";
writen_ni(STO, s, strlen(s));
/* wait a bit for output to drain */
sleep(1);
exit(EXIT_FAILURE);
}
static void parse_args(int argc, char *argv[])
{
int c;
int r = 0;
while ((c = getopt(argc, argv, "hf:b:s:")) != -1) {
switch (c) {
case 'f':
switch (optarg[0]) {
case 'X':
case 'x':
opts.flow = FC_XONXOFF;
break;
case 'H':
case 'h':
opts.flow = FC_RTSCTS;
break;
case 'N':
case 'n':
opts.flow = FC_NONE;
break;
default:
DPRINTF("Invalid flow control: %c\n", optarg[0]);
r = -1;
break;
}
break;
case 'b':
opts.baud = atoi(optarg);
if (opts.baud == 0 || !term_baud_ok(opts.baud)) {
DPRINTF("Invalid baud rate: %d\n", opts.baud);
r = -1;
}
break;
case 's':
opts.socket = optarg;
break;
case 'h':
r = 1;
break;
default:
r = -1;
break;
}
}
if (r) {
show_usage();
exit((r > 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}
if ((argc - optind) < 1) {
DPRINTF("No port given\n");
show_usage();
exit(EXIT_FAILURE);
}
strncpy(opts.port, argv[optind], sizeof(opts.port) - 1);
opts.port[sizeof(opts.port)-1] = '\0';
}
static void deadly_handler(int signum)
{
DPRINTF("seriald is signaled with TERM\n");
if (!sig_exit) {
seriald_ubus_loop_stop();
sig_exit = 1;
if (efd_notify_tty >= 0) eventfd_write(efd_notify_tty, 1);
if (efd_signal >= 0) eventfd_write(efd_signal, 1);
}
}
static void register_signal_handlers(void)
{
struct sigaction exit_action, ign_action;
/* Set up the structure to specify the exit action. */
exit_action.sa_handler = deadly_handler;
sigemptyset (&exit_action.sa_mask);
exit_action.sa_flags = 0;
/* Set up the structure to specify the ignore action. */
ign_action.sa_handler = SIG_IGN;
sigemptyset (&ign_action.sa_mask);
ign_action.sa_flags = 0;
sigaction(SIGTERM, &exit_action, NULL);
sigaction(SIGALRM, &ign_action, NULL);
sigaction(SIGHUP, &ign_action, NULL);
sigaction(SIGINT, &ign_action, NULL);
sigaction(SIGPIPE, &ign_action, NULL);
sigaction(SIGQUIT, &ign_action, NULL);
sigaction(SIGUSR1, &ign_action, NULL);
sigaction(SIGUSR2, &ign_action, NULL);
}
static void loop(void)
{
fd_set rdset, wrset;
int r;
int n;
char buff_rd[TTY_RD_SZ];
int write_sz;
int max_fd;
eventfd_t efd_value;
max_fd = (fd_tty > efd_notify_tty) ? fd_tty : efd_notify_tty;
tty_q.len = 0;
while (!sig_exit) {
FD_ZERO(&rdset);
FD_ZERO(&wrset);
FD_SET(fd_tty, &rdset);
FD_SET(efd_notify_tty, &rdset);
pthread_mutex_lock(&tty_q_mutex);
if (tty_q.len) FD_SET(fd_tty, &wrset);
pthread_mutex_unlock(&tty_q_mutex);
r = select(max_fd + 1, &rdset, &wrset, NULL, NULL);
if (r < 0) {
if (errno == EINTR) continue;
else fatal("select failed: %d : %s", errno, strerror(errno));
}
if (FD_ISSET(fd_tty, &rdset)) {
/* read from port */
do {
n = read(fd_tty, &buff_rd, sizeof(buff_rd));
} while (n < 0 && errno == EINTR);
if (n == 0) {
fatal("term closed");
} else if (n < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
fatal("read from term failed: %s", strerror(errno));
} else {
tty_read_line_splitter(n, buff_rd);
}
}
if (FD_ISSET(efd_notify_tty, &rdset)) {
/* Being notified we have something to write to TTY */
if (eventfd_read(efd_notify_tty, &efd_value)) {
fatal("failed to read efd_notify_tty");
}
}
if (FD_ISSET(fd_tty, &wrset)) {
/* write to port */
pthread_mutex_lock(&tty_q_mutex);
write_sz = (tty_q.len < tty_write_sz) ? tty_q.len : tty_write_sz;
do {
n = write(fd_tty, tty_q.buff, write_sz);
} while (n < 0 && errno == EINTR);
if (n <= 0) fatal("write to term failed: %s", strerror(errno));
memmove(tty_q.buff, tty_q.buff + n, tty_q.len - n);
tty_q.len -= n;
pthread_mutex_unlock(&tty_q_mutex);
}
}
}
static void tty_read_line_splitter(const int n, const char *buff_rd)
{
static char buff[TTY_RD_SZ+1] = "";
static int buff_len = 0;
const char *p;
p = buff_rd;
while (p - buff_rd < n) {
if (buff_len == sizeof(buff) - 1) {
tty_read_line_cb(buff);
*buff = '\0';
buff_len = 0;
}
if (*p && *p != '\r' && *p != '\n') {
buff[buff_len] = *p;
buff[++buff_len] = '\0';
} else if ((!*p || *p == '\n') && buff_len > 0) {
tty_read_line_cb(buff);
*buff = '\0';
buff_len = 0;
}
p++;
}
}
static void tty_read_line_cb(const char *line)
{
char format[] = "{\"data\": \"%s\"}\n";
char json[sizeof(format)+TTY_RD_SZ];
char *p;
int n;
int sz;
sprintf(json, format, line);
p = json;
sz = strlen(json);
while (sz > 0) {
do {
n = write(ubus_pipefd[1], p, sz);
} while (n < 0 && errno == EINTR);
if (n <= 0) fatal("write to pipe failed: %s", strerror(errno));
p += n;
sz -= n;
}
}
int main(int argc, char *argv[])
{
int r;
pthread_t uloop_tid;
parse_args(argc, argv);
register_signal_handlers();
sprintf(ubus_path, "serial.%s", basename(opts.port));
r = pipe2(ubus_pipefd, O_CLOEXEC);
if (r < 0) fatal("cannot create pipe to ubus: %s", strerror(errno));
/* Seems like you cannot have multiple ubus connections in single process. */
/* So we fork. */
switch(fork()) {
case 0:
efd_signal = eventfd(0, EFD_CLOEXEC);
if (efd_signal < 0) {
fatal("cannot create efd_signal: %s", strerror(errno));
}
close(ubus_pipefd[1]);
seriald_ubus_run(opts.socket);
return EXIT_SUCCESS;
case -1:
fatal("cannot fork ubus_event_loop");
}
close(ubus_pipefd[0]);
efd_notify_tty = eventfd(0, EFD_CLOEXEC);
if (efd_notify_tty < 0) {
fatal("cannot create efd_notify_tty: %s", strerror(errno));
}
r = term_lib_init();
if (r < 0) fatal("term_init failed: %s", term_strerror(term_errno, errno));
fd_tty = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd_tty < 0) fatal("cannot open %s: %s", opts.port, strerror(errno));
r = term_set(fd_tty,
1, /* raw mode. */
opts.baud, /* baud rate. */
opts.parity, /* parity. */
opts.databits, /* data bits. */
opts.stopbits, /* stop bits. */
opts.flow, /* flow control. */
1, /* local or modem */
!opts.noreset); /* hup-on-close. */
if (r < 0) {
fatal("failed to add device %s: %s",
opts.port, term_strerror(term_errno, errno));
}
r = term_apply(fd_tty, 0);
if (r < 0) {
fatal("failed to config device %s: %s",
opts.port, term_strerror(term_errno, errno));
}
set_tty_write_sz(term_get_baudrate(fd_tty, NULL));
r = seriald_ubus_loop_init(opts.socket);
if (r) fatal("failed to connect to ubus");
r = pthread_create(&uloop_tid, NULL, &seriald_ubus_loop, NULL);
if (r) fatal("can't create thread for uloop: %s", strerror(r));
loop();
seriald_ubus_loop_done();
return EXIT_SUCCESS;
}