1 /* vi: set sw=4 ts=4: */
3 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
6 * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 int microcom_main(int argc, char **argv);
13 int microcom_main(int argc, char **argv)
16 #define sfd (pfd[1].fd)
17 char *device_lock_file = NULL;
19 const char *opt_s = "9600";
23 struct termios tio0, tiosfd, tio;
25 getopt32(argv, "s:", &opt_s);
32 // try to create lock file in /var/lock
33 s = bb_basename(argv[0]);
36 bb_perror_msg_and_die("can't lock device");
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);
41 if (ENABLE_FEATURE_CLEAN_UP)
42 free(device_lock_file);
43 device_lock_file = NULL;
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
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)
59 sfd = open(argv[0], O_RDWR | O_NDELAY);
61 bb_perror_msg("can't open device");
64 fcntl(sfd, F_SETFL, O_RDWR); // why?
66 // put stdin to "raw mode", handle one character at a time
67 tcgetattr(STDIN_FILENO, &tio0);
69 tio.c_lflag &= ~(ICANON|ECHO);
70 tio.c_iflag &= ~(IXON|ICRNL);
71 tio.c_oflag &= ~(ONLCR);
74 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio)) {
75 bb_perror_msg("can't tcsetattr for %s", "stdin");
79 /* same thing for modem (plus: set baud rate) - TODO: make CLI option */
80 tcgetattr(sfd, &tiosfd);
82 tio.c_lflag &= ~(ICANON|ECHO);
83 tio.c_iflag &= ~(IXON|ICRNL);
84 tio.c_oflag &= ~(ONLCR);
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");
95 signal(SIGINT, SIG_IGN);
98 tcflush(STDIN_FILENO, TCIFLUSH);
99 printf("connected to '%s' (%d bps), exit with ctrl-X...\r\n", argv[0], speed);
101 // main loop: check with poll(), then read/write bytes across
102 pfd[0].fd = STDIN_FILENO;
103 pfd[0].events = POLLIN;
105 pfd[1].events = POLLIN;
108 safe_poll(pfd, 2, -1);
109 for (i = 0; i < 2; ++i) {
110 if (pfd[i].revents & POLLIN) {
111 len = read(pfd[i].fd, bb_common_bufsiz1, COMMON_BUFSIZE);
113 if (!i && 24 == bb_common_bufsiz1[0])
114 goto done; // ^X exits
115 write(pfd[1-i].fd, bb_common_bufsiz1, len);
121 tcsetattr(sfd, TCSANOW, &tiosfd);
122 tcsetattr(STDIN_FILENO, TCSANOW, &tio0);
123 tcflush(STDIN_FILENO, TCIFLUSH);
125 if (ENABLE_FEATURE_CLEAN_UP)
131 if (device_lock_file) {
132 unlink(device_lock_file);
133 if (ENABLE_FEATURE_CLEAN_UP)
134 free(device_lock_file);