diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2007-09-22 21:18:46 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2007-09-22 21:18:46 +0000 |
commit | 73f56bb76d36ab95a1f9b73d686c71625e4c86b1 (patch) | |
tree | 62bd8d2bea291f514bd6f33a02bb389e5bb3e2b5 /miscutils/microcom.c | |
parent | 3a252b8c12a7d5e1c0e6d08fb43979c23ee4030e (diff) | |
download | busybox-w32-73f56bb76d36ab95a1f9b73d686c71625e4c86b1.tar.gz busybox-w32-73f56bb76d36ab95a1f9b73d686c71625e4c86b1.tar.bz2 busybox-w32-73f56bb76d36ab95a1f9b73d686c71625e4c86b1.zip |
- actually mv microcom.c to miscutils
Diffstat (limited to 'miscutils/microcom.c')
-rw-r--r-- | miscutils/microcom.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/miscutils/microcom.c b/miscutils/microcom.c new file mode 100644 index 000000000..7a746a311 --- /dev/null +++ b/miscutils/microcom.c | |||
@@ -0,0 +1,138 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * bare bones 'talk to modem' program - similar to 'cu -l $device' | ||
4 | * inspired by mgetty's microcom | ||
5 | * | ||
6 | * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru> | ||
7 | * | ||
8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | #include "busybox.h" | ||
11 | |||
12 | int microcom_main(int argc, char **argv); | ||
13 | int microcom_main(int argc, char **argv) | ||
14 | { | ||
15 | struct pollfd pfd[2]; | ||
16 | #define sfd (pfd[1].fd) | ||
17 | char *device_lock_file = NULL; | ||
18 | const char *s; | ||
19 | const char *opt_s = "9600"; | ||
20 | unsigned speed; | ||
21 | int len; | ||
22 | int exitcode = 1; | ||
23 | struct termios tio0, tiosfd, tio; | ||
24 | |||
25 | getopt32(argv, "s:", &opt_s); | ||
26 | argc -= optind; | ||
27 | argv += optind; | ||
28 | if (!argv[0]) | ||
29 | bb_show_usage(); | ||
30 | speed = xatou(opt_s); | ||
31 | |||
32 | // try to create lock file in /var/lock | ||
33 | s = bb_basename(argv[0]); | ||
34 | if (!s[0]) { | ||
35 | errno = ENODEV; | ||
36 | bb_perror_msg_and_die("can't lock device"); | ||
37 | } | ||
38 | device_lock_file = xasprintf("/var/lock/LCK..%s", s); | ||
39 | sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); | ||
40 | if (sfd < 0) { | ||
41 | if (ENABLE_FEATURE_CLEAN_UP) | ||
42 | free(device_lock_file); | ||
43 | device_lock_file = NULL; | ||
44 | if (errno == EEXIST) | ||
45 | bb_perror_msg_and_die("can't lock device"); | ||
46 | // We don't abort on other errors: /var/lock can be | ||
47 | // non-writable or non-existent | ||
48 | } else { | ||
49 | // %4d to make mgetty happy. It treats 4-bytes lock files as binary, | ||
50 | // not text, PID. Making 5+ char file. Brrr... | ||
51 | s = xasprintf("%4d\n", getpid()); | ||
52 | write(sfd, s, strlen(s)); | ||
53 | if (ENABLE_FEATURE_CLEAN_UP) | ||
54 | free((char*)s); | ||
55 | close(sfd); | ||
56 | } | ||
57 | |||
58 | // open device | ||
59 | sfd = open(argv[0], O_RDWR | O_NDELAY); | ||
60 | if (sfd < 0) { | ||
61 | bb_perror_msg("can't open device"); | ||
62 | goto unlock_and_exit; | ||
63 | } | ||
64 | fcntl(sfd, F_SETFL, O_RDWR); // why? | ||
65 | |||
66 | // put stdin to "raw mode", handle one character at a time | ||
67 | tcgetattr(STDIN_FILENO, &tio0); | ||
68 | tio = tio0; | ||
69 | tio.c_lflag &= ~(ICANON|ECHO); | ||
70 | tio.c_iflag &= ~(IXON|ICRNL); | ||
71 | tio.c_oflag &= ~(ONLCR); | ||
72 | tio.c_cc[VMIN] = 1; | ||
73 | tio.c_cc[VTIME] = 0; | ||
74 | if (tcsetattr(STDIN_FILENO, TCSANOW, &tio)) { | ||
75 | bb_perror_msg("can't tcsetattr for %s", "stdin"); | ||
76 | goto unlock_and_exit; | ||
77 | } | ||
78 | |||
79 | /* same thing for modem (plus: set baud rate) - TODO: make CLI option */ | ||
80 | tcgetattr(sfd, &tiosfd); | ||
81 | tio = tiosfd; | ||
82 | tio.c_lflag &= ~(ICANON|ECHO); | ||
83 | tio.c_iflag &= ~(IXON|ICRNL); | ||
84 | tio.c_oflag &= ~(ONLCR); | ||
85 | tio.c_cc[VMIN] = 1; | ||
86 | tio.c_cc[VTIME] = 0; | ||
87 | cfsetispeed(&tio, tty_value_to_baud(speed)); | ||
88 | cfsetospeed(&tio, tty_value_to_baud(speed)); | ||
89 | if (tcsetattr(sfd, TCSANOW, &tio)) { | ||
90 | bb_perror_msg("can't tcsetattr for %s", "device"); | ||
91 | goto unlock_and_exit; | ||
92 | } | ||
93 | |||
94 | // disable SIGINT | ||
95 | signal(SIGINT, SIG_IGN); | ||
96 | |||
97 | // drain stdin | ||
98 | tcflush(STDIN_FILENO, TCIFLUSH); | ||
99 | printf("connected to '%s' (%d bps), exit with ctrl-X...\r\n", argv[0], speed); | ||
100 | |||
101 | // main loop: check with poll(), then read/write bytes across | ||
102 | pfd[0].fd = STDIN_FILENO; | ||
103 | pfd[0].events = POLLIN; | ||
104 | /*pfd[1].fd = sfd;*/ | ||
105 | pfd[1].events = POLLIN; | ||
106 | while (1) { | ||
107 | int i; | ||
108 | while (-1 == poll(pfd, 2, -1) && EINTR == errno) | ||
109 | continue; | ||
110 | for (i = 0; i < 2; ++i) { | ||
111 | if (pfd[i].revents & POLLIN) { | ||
112 | len = read(pfd[i].fd, bb_common_bufsiz1, COMMON_BUFSIZE); | ||
113 | if (len > 0) { | ||
114 | if (!i && 24 == bb_common_bufsiz1[0]) | ||
115 | goto done; // ^X exits | ||
116 | write(pfd[1-i].fd, bb_common_bufsiz1, len); | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | done: | ||
122 | tcsetattr(sfd, TCSANOW, &tiosfd); | ||
123 | tcsetattr(STDIN_FILENO, TCSANOW, &tio0); | ||
124 | tcflush(STDIN_FILENO, TCIFLUSH); | ||
125 | |||
126 | if (ENABLE_FEATURE_CLEAN_UP) | ||
127 | close(sfd); | ||
128 | exitcode = 0; | ||
129 | |||
130 | unlock_and_exit: | ||
131 | // delete lock file | ||
132 | if (device_lock_file) { | ||
133 | unlink(device_lock_file); | ||
134 | if (ENABLE_FEATURE_CLEAN_UP) | ||
135 | free(device_lock_file); | ||
136 | } | ||
137 | return exitcode; | ||
138 | } | ||