3 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
9 int microcom_main(int argc, char **argv);
10 int microcom_main(int argc, char **argv)
13 #define sfd (pfd[1].fd)
14 char *device_lock_file = NULL;
16 const char *opt_s = "9600";
20 struct termios tio0, tiosfd, tio;
22 getopt32(argv, "s:", &opt_s);
29 // try to create lock file in /var/lock
30 s = bb_basename(argv[0]);
33 bb_perror_msg_and_die("can't lock device");
35 device_lock_file = xasprintf("/var/lock/LCK..%s", s);
36 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
38 if (ENABLE_FEATURE_CLEAN_UP)
39 free(device_lock_file);
40 device_lock_file = NULL;
42 bb_perror_msg_and_die("can't lock device");
43 // We don't abort on other errors: /var/lock can be
44 // non-writable or non-existent
46 // %4d to make mgetty happy. It treats 4-bytes lock files as binary,
47 // not text, PID. Making 5+ char file. Brrr...
48 s = xasprintf("%4d\n", getpid());
49 write(sfd, s, strlen(s));
50 if (ENABLE_FEATURE_CLEAN_UP)
56 sfd = open(argv[0], O_RDWR | O_NDELAY);
58 bb_perror_msg("can't open device");
61 fcntl(sfd, F_SETFL, O_RDWR); // why?
63 // put stdin to "raw mode", handle one character at a time
64 tcgetattr(STDIN_FILENO, &tio0);
66 tio.c_lflag &= ~(ICANON|ECHO);
67 tio.c_iflag &= ~(IXON|ICRNL);
68 tio.c_oflag &= ~(ONLCR);
71 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio)) {
72 bb_perror_msg("can't tcsetattr for %s", "stdin");
76 /* same thing for modem (plus: set baud rate) - TODO: make CLI option */
77 tcgetattr(sfd, &tiosfd);
79 tio.c_lflag &= ~(ICANON|ECHO);
80 tio.c_iflag &= ~(IXON|ICRNL);
81 tio.c_oflag &= ~(ONLCR);
84 cfsetispeed(&tio, tty_value_to_baud(speed));
85 cfsetospeed(&tio, tty_value_to_baud(speed));
86 if (tcsetattr(sfd, TCSANOW, &tio)) {
87 bb_perror_msg("can't tcsetattr for %s", "device");
92 signal(SIGINT, SIG_IGN);
95 tcflush(STDIN_FILENO, TCIFLUSH);
96 printf("connected to '%s' (%d bps), exit with ctrl-X...\r\n", argv[0], speed);
98 // main loop: check with poll(), then read/write bytes across
99 pfd[0].fd = STDIN_FILENO;
100 pfd[0].events = POLLIN;
102 pfd[1].events = POLLIN;
105 while (-1 == poll(pfd, 2, -1) && EINTR == errno)
107 for (i = 0; i < 2; ++i) {
108 if (pfd[i].revents & POLLIN) {
109 len = read(pfd[i].fd, bb_common_bufsiz1, COMMON_BUFSIZE);
111 if (!i && 24 == bb_common_bufsiz1[0])
112 goto done; // ^X exits
113 write(pfd[1-i].fd, bb_common_bufsiz1, len);
119 tcsetattr(sfd, TCSANOW, &tiosfd);
120 tcsetattr(STDIN_FILENO, TCSANOW, &tio0);
121 tcflush(STDIN_FILENO, TCIFLUSH);
123 if (ENABLE_FEATURE_CLEAN_UP)
129 if (device_lock_file) {
130 unlink(device_lock_file);
131 if (ENABLE_FEATURE_CLEAN_UP)
132 free(device_lock_file);