blob: e097dc4374006ec9241a6ad96785b080cf197076 [file] [log] [blame]
Bernhard Reutner-Fischerf588f702007-09-22 20:35:32 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko70766952007-09-21 17:42:40 +00003 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
5 *
Denis Vlasenkof34e8212007-09-22 20:23:57 +00006 * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
7 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Denis Vlasenko70766952007-09-21 17:42:40 +00009 */
Denis Vlasenko9a4e08e2007-09-22 21:37:27 +000010#include "libbb.h"
Denis Vlasenko70766952007-09-21 17:42:40 +000011
12int microcom_main(int argc, char **argv);
13int 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
Denis Vlasenkof60d6262007-09-29 23:26:52 +000059 sfd = open(argv[0], O_RDWR);
Denis Vlasenko70766952007-09-21 17:42:40 +000060 if (sfd < 0) {
61 bb_perror_msg("can't open device");
62 goto unlock_and_exit;
63 }
Denis Vlasenko70766952007-09-21 17:42:40 +000064
65 // put stdin to "raw mode", handle one character at a time
66 tcgetattr(STDIN_FILENO, &tio0);
67 tio = tio0;
68 tio.c_lflag &= ~(ICANON|ECHO);
69 tio.c_iflag &= ~(IXON|ICRNL);
70 tio.c_oflag &= ~(ONLCR);
71 tio.c_cc[VMIN] = 1;
72 tio.c_cc[VTIME] = 0;
73 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio)) {
74 bb_perror_msg("can't tcsetattr for %s", "stdin");
75 goto unlock_and_exit;
76 }
77
78 /* same thing for modem (plus: set baud rate) - TODO: make CLI option */
79 tcgetattr(sfd, &tiosfd);
80 tio = tiosfd;
81 tio.c_lflag &= ~(ICANON|ECHO);
82 tio.c_iflag &= ~(IXON|ICRNL);
83 tio.c_oflag &= ~(ONLCR);
84 tio.c_cc[VMIN] = 1;
85 tio.c_cc[VTIME] = 0;
86 cfsetispeed(&tio, tty_value_to_baud(speed));
87 cfsetospeed(&tio, tty_value_to_baud(speed));
88 if (tcsetattr(sfd, TCSANOW, &tio)) {
89 bb_perror_msg("can't tcsetattr for %s", "device");
90 goto unlock_and_exit;
91 }
92
93 // disable SIGINT
94 signal(SIGINT, SIG_IGN);
95
96 // drain stdin
97 tcflush(STDIN_FILENO, TCIFLUSH);
98 printf("connected to '%s' (%d bps), exit with ctrl-X...\r\n", argv[0], speed);
99
100 // main loop: check with poll(), then read/write bytes across
101 pfd[0].fd = STDIN_FILENO;
102 pfd[0].events = POLLIN;
103 /*pfd[1].fd = sfd;*/
104 pfd[1].events = POLLIN;
105 while (1) {
106 int i;
Denis Vlasenko5d61e712007-09-27 10:09:59 +0000107 safe_poll(pfd, 2, -1);
Denis Vlasenko70766952007-09-21 17:42:40 +0000108 for (i = 0; i < 2; ++i) {
Bernhard Reutner-Fischerf588f702007-09-22 20:35:32 +0000109 if (pfd[i].revents & POLLIN) {
Denis Vlasenko70766952007-09-21 17:42:40 +0000110 len = read(pfd[i].fd, bb_common_bufsiz1, COMMON_BUFSIZE);
111 if (len > 0) {
112 if (!i && 24 == bb_common_bufsiz1[0])
113 goto done; // ^X exits
114 write(pfd[1-i].fd, bb_common_bufsiz1, len);
115 }
116 }
117 }
118 }
119 done:
120 tcsetattr(sfd, TCSANOW, &tiosfd);
121 tcsetattr(STDIN_FILENO, TCSANOW, &tio0);
122 tcflush(STDIN_FILENO, TCIFLUSH);
123
124 if (ENABLE_FEATURE_CLEAN_UP)
125 close(sfd);
126 exitcode = 0;
127
128 unlock_and_exit:
129 // delete lock file
130 if (device_lock_file) {
131 unlink(device_lock_file);
132 if (ENABLE_FEATURE_CLEAN_UP)
133 free(device_lock_file);
134 }
135 return exitcode;
136}