]>
Commit | Line | Data |
---|---|---|
1e8d11c4 TS |
1 | /* line discipline loading daemon |
2 | * open a serial device and attach a line discipline on it | |
3 | * | |
4 | * Usage: | |
5 | * ldattach GIGASET_M101 /dev/ttyS0 | |
6 | * | |
7 | * ===================================================================== | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * ===================================================================== | |
13 | */ | |
14 | ||
28e91b83 SK |
15 | #include <errno.h> |
16 | #include <fcntl.h> | |
17 | #include <getopt.h> | |
1e8d11c4 | 18 | #include <stdio.h> |
28e91b83 | 19 | #include <stdlib.h> |
1e8d11c4 | 20 | #include <string.h> |
1e8d11c4 | 21 | #include <sys/ioctl.h> |
28e91b83 SK |
22 | #include <sys/stat.h> |
23 | #include <sys/types.h> | |
f8132752 | 24 | #include <termios.h> |
1e8d11c4 | 25 | #include <unistd.h> |
1e8d11c4 | 26 | |
becc79d5 | 27 | #include "c.h" |
8596f639 | 28 | #include "all-io.h" |
fcba4577 | 29 | #include "nls.h" |
7b2d3de4 | 30 | #include "strutils.h" |
efb8854f | 31 | #include "closestream.h" |
fcba4577 | 32 | |
8596f639 KZ |
33 | #include <signal.h> |
34 | #include <sys/socket.h> | |
35 | #include <linux/if.h> | |
36 | ||
37 | #include <linux/tty.h> /* for N_GSM0710 */ | |
38 | ||
39 | #ifdef LINUX_GSMMUX_H | |
40 | # include <linux/gsmmux.h> /* Add by guowenxue */ | |
41 | #else | |
42 | struct gsm_config | |
43 | { | |
44 | unsigned int adaption; | |
45 | unsigned int encapsulation; | |
46 | unsigned int initiator; | |
47 | unsigned int t1; | |
48 | unsigned int t2; | |
49 | unsigned int t3; | |
50 | unsigned int n2; | |
51 | unsigned int mru; | |
52 | unsigned int mtu; | |
53 | unsigned int k; | |
54 | unsigned int i; | |
55 | unsigned int unused[8]; /* Padding for expansion without | |
56 | breaking stuff */ | |
57 | }; | |
58 | # define GSMIOC_GETCONF _IOR('G', 0, struct gsm_config) | |
59 | # define GSMIOC_SETCONF _IOW('G', 1, struct gsm_config) | |
60 | #endif | |
61 | ||
1e8d11c4 | 62 | #ifndef N_GIGASET_M101 |
f8132752 | 63 | # define N_GIGASET_M101 16 |
1e8d11c4 TS |
64 | #endif |
65 | ||
d5f36dec TS |
66 | #ifndef N_PPS |
67 | # define N_PPS 18 | |
68 | #endif | |
69 | ||
58c17460 TS |
70 | #ifndef N_GSM0710 |
71 | # define N_GSM0710 21 | |
72 | #endif | |
73 | ||
8596f639 KZ |
74 | #define MAXINTROPARMLEN 32 |
75 | ||
f8132752 KZ |
76 | /* attach a line discipline ioctl */ |
77 | #ifndef TIOCSETD | |
78 | # define TIOCSETD 0x5423 | |
1e8d11c4 TS |
79 | #endif |
80 | ||
1e8d11c4 TS |
81 | static int debug = 0; |
82 | ||
7b549aff | 83 | struct ld_table { |
28e91b83 SK |
84 | const char *name; |
85 | int value; | |
7b549aff KZ |
86 | }; |
87 | ||
1e8d11c4 | 88 | /* currently supported line disciplines, plus some aliases */ |
28e91b83 SK |
89 | static const struct ld_table ld_discs[] = { |
90 | { "TTY", N_TTY }, | |
91 | { "SLIP", N_SLIP }, | |
92 | { "MOUSE", N_MOUSE }, | |
93 | { "PPP", N_PPP }, | |
94 | { "STRIP", N_STRIP }, | |
95 | { "AX25", N_AX25 }, | |
96 | { "X25", N_X25 }, | |
97 | { "6PACK", N_6PACK }, | |
98 | { "R3964", N_R3964 }, | |
99 | { "IRDA", N_IRDA }, | |
100 | { "HDLC", N_HDLC }, | |
101 | { "SYNC_PPP", N_SYNC_PPP }, | |
102 | { "SYNCPPP", N_SYNC_PPP }, | |
103 | { "HCI", N_HCI }, | |
58c17460 | 104 | { "GIGASET_M101", N_GIGASET_M101 }, |
a8023b9a SK |
105 | { "M101", N_GIGASET_M101 }, |
106 | { "GIGASET", N_GIGASET_M101 }, | |
58c17460 TS |
107 | { "PPS", N_PPS }, |
108 | { "GSM0710", N_GSM0710}, | |
8596f639 | 109 | { NULL, 0 } |
1e8d11c4 TS |
110 | }; |
111 | ||
b091b880 TS |
112 | /* known c_iflag names */ |
113 | static const struct ld_table ld_iflags[] = | |
114 | { | |
115 | { "IGNBRK", IGNBRK }, | |
116 | { "BRKINT", BRKINT }, | |
117 | { "IGNPAR", IGNPAR }, | |
118 | { "PARMRK", PARMRK }, | |
119 | { "INPCK", INPCK }, | |
120 | { "ISTRIP", ISTRIP }, | |
121 | { "INLCR", INLCR }, | |
122 | { "IGNCR", IGNCR }, | |
123 | { "ICRNL", ICRNL }, | |
124 | { "IUCLC", IUCLC }, | |
125 | { "IXON", IXON }, | |
126 | { "IXANY", IXANY }, | |
127 | { "IXOFF", IXOFF }, | |
128 | { "IMAXBEL", IMAXBEL }, | |
129 | { "IUTF8", IUTF8 }, | |
28e91b83 | 130 | { NULL, 0 } |
b091b880 TS |
131 | }; |
132 | ||
d91757a0 | 133 | static void dbg(char *fmt, ...) |
3a134012 SK |
134 | { |
135 | va_list args; | |
136 | ||
137 | if (debug == 0) | |
138 | return; | |
139 | fflush(NULL); | |
140 | fprintf(stderr, "%s: ", program_invocation_short_name); | |
141 | va_start(args, fmt); | |
142 | vfprintf(stderr, fmt, args); | |
143 | va_end(args); | |
144 | fprintf(stderr, "\n"); | |
145 | fflush(NULL); | |
146 | return; | |
147 | } | |
148 | ||
7b549aff | 149 | static int lookup_table(const struct ld_table *tab, const char *str) |
1e8d11c4 | 150 | { |
28e91b83 | 151 | const struct ld_table *t; |
1e8d11c4 | 152 | |
28e91b83 SK |
153 | for (t = tab; t && t->name; t++) |
154 | if (!strcasecmp(t->name, str)) | |
155 | return t->value; | |
156 | return -1; | |
1e8d11c4 TS |
157 | } |
158 | ||
28e91b83 | 159 | static void print_table(FILE * out, const struct ld_table *tab) |
1e8d11c4 | 160 | { |
28e91b83 SK |
161 | const struct ld_table *t; |
162 | int i; | |
163 | ||
164 | for (t = tab, i = 1; t && t->name; t++, i++) { | |
b5225af3 TS |
165 | fprintf(out, " %-12s", t->name); |
166 | if (!(i % 5)) | |
28e91b83 SK |
167 | fputc('\n', out); |
168 | } | |
7b549aff KZ |
169 | } |
170 | ||
b091b880 TS |
171 | static int parse_iflag(char *str, int *set_iflag, int *clr_iflag) |
172 | { | |
28e91b83 | 173 | int iflag; |
7b2d3de4 | 174 | char *s; |
28e91b83 SK |
175 | |
176 | for (s = strtok(str, ","); s != NULL; s = strtok(NULL, ",")) { | |
177 | if (*s == '-') | |
178 | s++; | |
7b2d3de4 | 179 | if ((iflag = lookup_table(ld_iflags, s)) < 0) |
20a39982 | 180 | iflag = strtos32_or_err(s, _("invalid iflag")); |
28e91b83 SK |
181 | if (s > str && *(s - 1) == '-') |
182 | *clr_iflag |= iflag; | |
183 | else | |
184 | *set_iflag |= iflag; | |
b091b880 | 185 | } |
28e91b83 SK |
186 | dbg("iflag (set/clear): %d/%d", *set_iflag, *clr_iflag); |
187 | return 0; | |
b091b880 TS |
188 | } |
189 | ||
190 | ||
28e91b83 | 191 | static void __attribute__ ((__noreturn__)) usage(int exitcode) |
7b549aff | 192 | { |
28e91b83 | 193 | FILE *out = exitcode == EXIT_SUCCESS ? stdout : stderr; |
b5b28b43 | 194 | |
a8023b9a SK |
195 | fputs(USAGE_HEADER, out); |
196 | fprintf(out, _(" %s [options] <ldisc> <device>\n"), program_invocation_short_name); | |
a8023b9a | 197 | |
451dbcfa BS |
198 | fputs(USAGE_SEPARATOR, out); |
199 | fputs(_("Attach a line discipline to a serial line.\n"), out); | |
200 | ||
201 | fputs(USAGE_OPTIONS, out); | |
a8023b9a SK |
202 | fputs(_(" -d, --debug print verbose messages to stderr\n"), out); |
203 | fputs(_(" -s, --speed <value> set serial line speed\n"), out); | |
8596f639 KZ |
204 | fputs(_(" -c, --intro-command <string> intro sent before ldattach\n"), out); |
205 | fputs(_(" -p, --pause <seconds> pause between intro and ldattach\n"), out); | |
a8023b9a SK |
206 | fputs(_(" -7, --sevenbits set character size to 7 bits\n"), out); |
207 | fputs(_(" -8, --eightbits set character size to 8 bits\n"), out); | |
208 | fputs(_(" -n, --noparity set parity to none\n"), out); | |
209 | fputs(_(" -e, --evenparity set parity to even\n"), out); | |
210 | fputs(_(" -o, --oddparity set parity to odd\n"), out); | |
211 | fputs(_(" -1, --onestopbit set stop bits to one\n"), out); | |
212 | fputs(_(" -2, --twostopbits set stop bits to two\n"), out); | |
213 | fputs(_(" -i, --iflag [-]<iflag> set input mode flag\n"), out); | |
5ff20423 | 214 | |
a8023b9a SK |
215 | fputs(USAGE_SEPARATOR, out); |
216 | fputs(USAGE_HELP, out); | |
217 | fputs(USAGE_VERSION, out); | |
158e4182 | 218 | |
28e91b83 SK |
219 | fputs(_("\nKnown <ldisc> names:\n"), out); |
220 | print_table(out, ld_discs); | |
158e4182 KZ |
221 | fputs(USAGE_SEPARATOR, out); |
222 | ||
28e91b83 SK |
223 | fputs(_("\nKnown <iflag> names:\n"), out); |
224 | print_table(out, ld_iflags); | |
158e4182 | 225 | |
a8023b9a | 226 | fprintf(out, USAGE_MAN_TAIL("ldattach(8)")); |
28e91b83 | 227 | exit(exitcode); |
1e8d11c4 TS |
228 | } |
229 | ||
f8132752 KZ |
230 | static int my_cfsetspeed(struct termios *ts, int speed) |
231 | { | |
232 | /* Standard speeds | |
233 | * -- cfsetspeed() is able to translate number to Bxxx constants | |
234 | */ | |
235 | if (cfsetspeed(ts, speed) == 0) | |
236 | return 0; | |
237 | ||
238 | /* Nonstandard speeds | |
28e91b83 SK |
239 | * -- we have to bypass glibc and set the speed manually (because glibc |
240 | * checks for speed and supports Bxxx bit rates only)... | |
f8132752 KZ |
241 | */ |
242 | #ifdef _HAVE_STRUCT_TERMIOS_C_ISPEED | |
28e91b83 | 243 | # define BOTHER 0010000 /* non standard rate */ |
f8132752 KZ |
244 | dbg("using non-standard speeds"); |
245 | ts->c_ospeed = ts->c_ispeed = speed; | |
246 | ts->c_cflag &= ~CBAUD; | |
247 | ts->c_cflag |= BOTHER; | |
248 | return 0; | |
249 | #else | |
250 | return -1; | |
251 | #endif | |
252 | } | |
253 | ||
8596f639 KZ |
254 | static void handler(int s) |
255 | { | |
256 | dbg("got SIG %i -> exiting\n", s); | |
257 | exit(EXIT_SUCCESS); | |
258 | } | |
259 | ||
260 | static void gsm0710_set_conf(int tty_fd) | |
261 | { | |
262 | struct gsm_config c; | |
263 | ||
264 | /* Add by guowenxue */ | |
265 | /* get n_gsm configuration */ | |
266 | ioctl(tty_fd, GSMIOC_GETCONF, &c); | |
267 | /* we are initiator and need encoding 0 (basic) */ | |
268 | c.initiator = 1; | |
269 | c.encapsulation = 0; | |
270 | /* our modem defaults to a maximum size of 127 bytes */ | |
271 | c.mru = 127; | |
272 | c.mtu = 127; | |
273 | /* set the new configuration */ | |
274 | ioctl(tty_fd, GSMIOC_SETCONF, &c); | |
275 | /* Add by guowenxue end*/ | |
276 | } | |
277 | ||
1e8d11c4 TS |
278 | int main(int argc, char **argv) |
279 | { | |
28e91b83 SK |
280 | int tty_fd; |
281 | struct termios ts; | |
282 | int speed = 0, bits = '-', parity = '-', stop = '-'; | |
283 | int set_iflag = 0, clr_iflag = 0; | |
284 | int ldisc; | |
285 | int optc; | |
28e91b83 | 286 | char *dev; |
8596f639 KZ |
287 | int intropause = 1; |
288 | char *introparm = NULL; | |
289 | ||
28e91b83 SK |
290 | static const struct option opttbl[] = { |
291 | {"speed", required_argument, NULL, 's'}, | |
292 | {"sevenbits", no_argument, NULL, '7'}, | |
293 | {"eightbits", no_argument, NULL, '8'}, | |
294 | {"noparity", no_argument, NULL, 'n'}, | |
295 | {"evenparity", no_argument, NULL, 'e'}, | |
296 | {"oddparity", no_argument, NULL, 'o'}, | |
297 | {"onestopbit", no_argument, NULL, '1'}, | |
298 | {"twostopbits", no_argument, NULL, '2'}, | |
299 | {"iflag", required_argument, NULL, 'i'}, | |
300 | {"help", no_argument, NULL, 'h'}, | |
301 | {"version", no_argument, NULL, 'V'}, | |
302 | {"debug", no_argument, NULL, 'd'}, | |
8596f639 KZ |
303 | {"intro-command", no_argument, NULL, 'c'}, |
304 | {"pause", no_argument, NULL, 'p'}, | |
28e91b83 SK |
305 | {NULL, 0, NULL, 0} |
306 | }; | |
307 | ||
8596f639 KZ |
308 | signal(SIGKILL, handler); |
309 | signal(SIGINT, handler); | |
310 | ||
28e91b83 SK |
311 | setlocale(LC_ALL, ""); |
312 | bindtextdomain(PACKAGE, LOCALEDIR); | |
313 | textdomain(PACKAGE); | |
efb8854f | 314 | atexit(close_stdout); |
28e91b83 SK |
315 | |
316 | /* parse options */ | |
28e91b83 SK |
317 | if (argc == 0) |
318 | usage(EXIT_SUCCESS); | |
319 | while ((optc = | |
8596f639 | 320 | getopt_long(argc, argv, "dhV78neo12s:i:c:p:", opttbl, |
28e91b83 SK |
321 | NULL)) >= 0) { |
322 | switch (optc) { | |
323 | case 'd': | |
3a134012 | 324 | debug = 1; |
28e91b83 SK |
325 | break; |
326 | case '1': | |
327 | case '2': | |
328 | stop = optc; | |
329 | break; | |
330 | case '7': | |
331 | case '8': | |
332 | bits = optc; | |
333 | break; | |
334 | case 'n': | |
335 | case 'e': | |
336 | case 'o': | |
337 | parity = optc; | |
338 | break; | |
339 | case 's': | |
20a39982 | 340 | speed = strtos32_or_err(optarg, _("invalid speed argument")); |
28e91b83 | 341 | break; |
8596f639 KZ |
342 | case 'p': |
343 | intropause = strtou32_or_err(optarg, _("invalid pause argument")); | |
344 | if (intropause > 10) | |
345 | errx(EXIT_FAILURE, "invalid pause: %s", optarg); | |
346 | break; | |
347 | case 'c': | |
348 | introparm = optarg; | |
349 | break; | |
28e91b83 SK |
350 | case 'i': |
351 | parse_iflag(optarg, &set_iflag, &clr_iflag); | |
352 | break; | |
353 | case 'V': | |
a8023b9a SK |
354 | printf(UTIL_LINUX_VERSION); |
355 | return EXIT_SUCCESS; | |
28e91b83 SK |
356 | case 'h': |
357 | usage(EXIT_SUCCESS); | |
358 | default: | |
677ec86c | 359 | errtryhelp(EXIT_FAILURE); |
28e91b83 SK |
360 | } |
361 | } | |
362 | ||
363 | if (argc - optind != 2) | |
364 | usage(EXIT_FAILURE); | |
365 | ||
366 | /* parse line discipline specification */ | |
367 | ldisc = lookup_table(ld_discs, argv[optind]); | |
7b2d3de4 | 368 | if (ldisc < 0) |
20a39982 | 369 | ldisc = strtos32_or_err(argv[optind], _("invalid line discipline argument")); |
28e91b83 | 370 | |
5687494a TS |
371 | /* ldisc specific option settings */ |
372 | if (ldisc == N_GIGASET_M101) { | |
373 | /* device specific defaults for line speed and data format */ | |
158e4182 KZ |
374 | if (speed == 0) |
375 | speed = 115200; | |
376 | if (bits == '-') | |
377 | bits = '8'; | |
378 | if (parity == '-') | |
379 | parity = 'n'; | |
380 | if (stop == '-') | |
381 | stop = '1'; | |
5687494a TS |
382 | } |
383 | ||
28e91b83 SK |
384 | /* open device */ |
385 | dev = argv[optind + 1]; | |
386 | if ((tty_fd = open(dev, O_RDWR | O_NOCTTY)) < 0) | |
387 | err(EXIT_FAILURE, _("cannot open %s"), dev); | |
388 | if (!isatty(tty_fd)) | |
389 | errx(EXIT_FAILURE, _("%s is not a serial line"), dev); | |
390 | ||
391 | dbg("opened %s", dev); | |
392 | ||
393 | /* set line speed and format */ | |
394 | if (tcgetattr(tty_fd, &ts) < 0) | |
395 | err(EXIT_FAILURE, | |
396 | _("cannot get terminal attributes for %s"), dev); | |
397 | cfmakeraw(&ts); | |
398 | if (speed && my_cfsetspeed(&ts, speed) < 0) | |
399 | errx(EXIT_FAILURE, _("speed %d unsupported"), speed); | |
400 | ||
401 | switch (stop) { | |
1e8d11c4 | 402 | case '1': |
28e91b83 SK |
403 | ts.c_cflag &= ~CSTOPB; |
404 | break; | |
1e8d11c4 | 405 | case '2': |
28e91b83 SK |
406 | ts.c_cflag |= CSTOPB; |
407 | break; | |
ba64bbd2 SK |
408 | case '-': |
409 | break; | |
410 | default: | |
411 | abort(); | |
28e91b83 SK |
412 | } |
413 | switch (bits) { | |
1e8d11c4 | 414 | case '7': |
28e91b83 SK |
415 | ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS7; |
416 | break; | |
1e8d11c4 | 417 | case '8': |
28e91b83 SK |
418 | ts.c_cflag = (ts.c_cflag & ~CSIZE) | CS8; |
419 | break; | |
ba64bbd2 SK |
420 | case '-': |
421 | break; | |
422 | default: | |
423 | abort(); | |
28e91b83 SK |
424 | } |
425 | switch (parity) { | |
1e8d11c4 | 426 | case 'n': |
28e91b83 SK |
427 | ts.c_cflag &= ~(PARENB | PARODD); |
428 | break; | |
1e8d11c4 | 429 | case 'e': |
28e91b83 SK |
430 | ts.c_cflag |= PARENB; |
431 | ts.c_cflag &= ~PARODD; | |
432 | break; | |
1e8d11c4 | 433 | case 'o': |
28e91b83 SK |
434 | ts.c_cflag |= (PARENB | PARODD); |
435 | break; | |
ba64bbd2 SK |
436 | case '-': |
437 | break; | |
438 | default: | |
439 | abort(); | |
1e8d11c4 | 440 | } |
28e91b83 SK |
441 | |
442 | ts.c_cflag |= CREAD; /* just to be on the safe side */ | |
443 | ts.c_iflag |= set_iflag; | |
444 | ts.c_iflag &= ~clr_iflag; | |
445 | ||
446 | if (tcsetattr(tty_fd, TCSAFLUSH, &ts) < 0) | |
447 | err(EXIT_FAILURE, | |
448 | _("cannot set terminal attributes for %s"), dev); | |
449 | ||
450 | dbg("set to raw %d %c%c%c: cflag=0x%x", | |
451 | speed, bits, parity, stop, ts.c_cflag); | |
452 | ||
8596f639 KZ |
453 | if (introparm && *introparm) |
454 | { | |
455 | dbg("intro command is '%s'", introparm); | |
456 | if (write_all(tty_fd, introparm, strlen(introparm)) != 0) | |
457 | err(EXIT_FAILURE, | |
458 | _("cannot write intro command to %s"), dev); | |
459 | ||
460 | if (intropause) { | |
461 | dbg("waiting for %d seconds", intropause); | |
462 | sleep(intropause); | |
463 | } | |
464 | } | |
465 | ||
5687494a | 466 | /* Attach the line discipline. */ |
28e91b83 SK |
467 | if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0) |
468 | err(EXIT_FAILURE, _("cannot set line discipline")); | |
469 | ||
470 | dbg("line discipline set to %d", ldisc); | |
471 | ||
5687494a | 472 | /* ldisc specific post-attach actions */ |
8596f639 KZ |
473 | if (ldisc == N_GSM0710) |
474 | gsm0710_set_conf(tty_fd); | |
475 | ||
28e91b83 SK |
476 | /* Go into background if not in debug mode. */ |
477 | if (!debug && daemon(0, 0) < 0) | |
478 | err(EXIT_FAILURE, _("cannot daemonize")); | |
479 | ||
480 | /* Sleep to keep the line discipline active. */ | |
481 | pause(); | |
482 | ||
483 | exit(EXIT_SUCCESS); | |
1e8d11c4 | 484 | } |