2 * Copyright (c) 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)unidialer.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/tip/libacu/unidialer.c,v 1.7 1999/08/28 01:06:30 peter Exp $
35 * $DragonFly: src/usr.bin/tip/libacu/unidialer.c,v 1.3 2005/04/19 18:55:24 swildner Exp $
39 * Generalized routines for calling up on a Hayes AT command set based modem.
40 * Control variables are pulled out of a modem caps-style database to
41 * configure the driver for a particular modem.
45 #include "pathnames.h"
47 #include <sys/times.h>
53 #include "acucommon.h"
61 mpt_notype, mpt_string, mpt_number, mpt_boolean
65 modem_parm_type_t modem_parm_type;
80 static char modem_name [80];
81 static char *dial_command;
82 static char *hangup_command;
83 static char *echo_off_command;
84 static char *reset_command;
85 static char *init_string;
86 static char *escape_sequence;
87 static int hw_flow_control;
89 static unsigned int intercharacter_delay;
90 static unsigned int intercommand_delay;
91 static unsigned int escape_guard_time;
92 static unsigned int reset_delay;
94 static int unidialer_dialer (register char *num, char *acu);
95 static void unidialer_disconnect ();
96 static void unidialer_abort ();
98 static acu_t unidialer =
102 unidialer_disconnect,
107 Table of parameters kept in modem database
109 modem_parm_t modem_parms [] = {
110 { mpt_string, "dial_command", &dial_command, "ATDT%s\r" },
111 { mpt_string, "hangup_command", &hangup_command, "ATH\r", },
112 { mpt_string, "echo_off_command", &echo_off_command, "ATE0\r" },
113 { mpt_string, "reset_command", &reset_command, "ATZ\r" },
114 { mpt_string, "init_string", &init_string, "AT&F\r", },
115 { mpt_string, "escape_sequence", &escape_sequence, "+++" },
116 { mpt_boolean, "hw_flow_control", (char **)&hw_flow_control, NULL },
117 { mpt_boolean, "lock_baud", (char **)&lock_baud, NULL },
118 { mpt_number, "intercharacter_delay", (char **)&intercharacter_delay, (char *)50 },
119 { mpt_number, "intercommand_delay", (char **)&intercommand_delay, (char *)300 },
120 { mpt_number, "escape_guard_time", (char **)&escape_guard_time, (char *)300 },
121 { mpt_number, "reset_delay", (char **)&reset_delay, (char *)3000 },
122 { mpt_notype, NULL, NULL, NULL }
128 static void unidialer_verbose_read ();
129 static void unidialer_modem_cmd (int fd, CONST char *cmd);
130 static void unidialer_write (int fd, CONST char *cp, int n);
131 static void unidialer_write_str (int fd, CONST char *cp);
132 static void unidialer_disconnect ();
133 static void sigALRM ();
134 static int unidialersync ();
135 static int unidialer_swallow (register char *match);
140 static int timeout = 0;
141 static int connected = 0;
142 static jmp_buf timeoutbuf, intbuf;
144 #define cgetflag(f) (cgetcap(bp, f, ':') != NULL)
148 #define print_str(x) printf (#x " = %s\n", x)
149 #define print_num(x) printf (#x " = %d\n", x)
151 void dumpmodemparms (char *modem)
153 printf ("modem parms for %s\n", modem);
154 print_str (dial_command);
155 print_str (hangup_command);
156 print_str (echo_off_command);
157 print_str (reset_command);
158 print_str (init_string);
159 print_str (escape_sequence);
160 print_num (lock_baud);
161 print_num (intercharacter_delay);
162 print_num (intercommand_delay);
163 print_num (escape_guard_time);
164 print_num (reset_delay);
169 static int getmodemparms (const char *modem)
171 char *bp, *db_array [3], *modempath;
175 modempath = getenv ("MODEMS");
179 if (modempath != NULL)
180 db_array [ndx++] = modempath;
182 db_array [ndx++] = _PATH_MODEMS;
183 db_array [ndx] = NULL;
185 if ((stat = cgetent (&bp, db_array, (char *)modem)) < 0) {
188 warnx ("unknown modem %s", modem);
191 warnx ("can't open modem description file");
194 warnx ("possible reference loop in modem description file");
199 for (mpp = modem_parms; mpp->name; mpp++)
201 switch (mpp->modem_parm_type)
204 if (cgetstr (bp, (char *)mpp->name, mpp->value.string) == -1)
205 *mpp->value.string = mpp->default_value.string;
211 if (cgetnum (bp, (char *)mpp->name, &l) == -1)
212 *mpp->value.number = mpp->default_value.number;
214 *mpp->value.number = (unsigned int)l;
219 *mpp->value.number = cgetflag ((char *)mpp->name);
223 strncpy (modem_name, modem, sizeof (modem_name) - 1);
224 modem_name [sizeof (modem_name) - 1] = '\0';
230 acu_t* unidialer_getmodem (const char *modem_name)
233 if (getmodemparms (modem_name))
238 static int unidialer_modem_ready ()
242 ioctl (FD, TIOCMGET, &state);
243 return (state & TIOCM_DSR) ? 1 : 0;
249 static int unidialer_waitfor_modem_ready (int ms)
253 for (count = 0; count < ms; count += 100)
255 if (unidialer_modem_ready ())
258 printf ("unidialer_waitfor_modem_ready: modem ready.\n");
271 int unidialer_tty_clocal (int flag)
279 t.c_cflag &= ~CLOCAL;
280 tcsetattr (FD, TCSANOW, &t);
281 #elif defined(TIOCMSET)
284 Don't have CLOCAL so raise CD in software to
287 ioctl (FD, TIOCMGET, &state);
292 ioctl (FD, TIOCMSET, &state);
296 int unidialer_get_modem_response (char *buf, int bufsz, int response_timeout)
299 char c, *p = buf, *lid = buf + bufsz - 1;
304 f = signal (SIGALRM, sigALRM);
308 if (setjmp (timeoutbuf)) {
312 printf ("get_response: timeout buf=%s, state=%d\n", buf, state);
317 ualarm (response_timeout * 1000, 0);
326 if (read (FD, &c, 1) == 1)
335 printf ("get_response: unexpected char %s.\n", ctrl (c));
342 if (read (FD, &c, 1) == 1)
347 printf ("get_response: <CRLF> encountered.\n", buf);
355 printf ("get_response: unexpected char %s.\n", ctrl (c));
362 if (read (FD, &c, 1) == 1)
366 else if (c >= ' ' && p < lid)
372 if (read (FD, &c, 1) == 1)
381 printf ("get_response: %s\n", buf);
396 int unidialer_get_okay (int ms)
400 okay = unidialer_get_modem_response (buf, sizeof (buf), ms) &&
401 strcmp (buf, "OK") == 0;
405 static int unidialer_dialer (register char *num, char *acu)
408 char dial_string [80];
412 static int unidialer_connect(), unidialer_swallow();
415 dumpmodemparms (modem_name);
420 if ((i = speed(number(value(BAUDRATE)))) == 0)
425 if (boolean(value(VERBOSE)))
426 printf("Using \"%s\"\n", acu);
433 if (!unidialersync()) {
435 printf("tip: can't synchronize with %s\n", modem_name);
437 logent(value(HOST), num, modem_name, "can't synch up");
442 unidialer_modem_cmd (FD, echo_off_command); /* turn off echoing */
447 if (boolean(value(VERBOSE)))
448 unidialer_verbose_read();
451 acu_flush (); /* flush any clutter */
453 unidialer_modem_cmd (FD, init_string);
455 if (!unidialer_get_okay (reset_delay))
460 for (cp = num; *cp; cp++)
464 (void) sprintf (dial_string, dial_command, num);
466 unidialer_modem_cmd (FD, dial_string);
468 connected = unidialer_connect ();
470 if (connected && hw_flow_control) {
471 acu_hw_flow_control (hw_flow_control);
476 sprintf(line, "%d second dial timeout",
477 number(value(DIALTIMEOUT)));
478 logent(value(HOST), num, modem_name, line);
483 unidialer_disconnect ();
488 static void unidialer_disconnect ()
492 acu_flush (); /* flush any clutter */
494 unidialer_tty_clocal (TRUE);
496 /* first hang up the modem*/
497 ioctl (FD, TIOCCDTR, 0);
499 ioctl (FD, TIOCSDTR, 0);
502 * If AT&D2, then dropping DTR *should* just hangup the modem. But
503 * some modems reset anyway; also, the modem may be programmed to reset
504 * anyway with AT&D3. Play it safe and wait for the full reset time before
507 acu_nap (reset_delay);
509 if (!unidialer_waitfor_modem_ready (reset_delay))
512 printf ("unidialer_disconnect: warning CTS low.\r\n");
517 * If not strapped for DTR control, try to get command mode.
519 for (retries = okay = 0; retries < MAXRETRY && !okay; retries++)
522 /* flush any clutter */
526 printf ("unidialer_disconnect: warning flush failed.\r\n");
529 timeout_value = escape_guard_time;
530 timeout_value += (timeout_value * retries / MAXRETRY);
531 acu_nap (timeout_value);
532 acu_flush (); /* flush any clutter */
533 unidialer_modem_cmd (FD, escape_sequence);
534 acu_nap (timeout_value);
535 unidialer_modem_cmd (FD, hangup_command);
536 okay = unidialer_get_okay (reset_delay);
541 logent(value(HOST), "", modem_name, "can't hang up modem");
543 if (boolean(value(VERBOSE)))
544 printf("hang up failed\n");
550 static void unidialer_abort ()
552 unidialer_write_str (FD, "\r"); /* send anything to abort the call */
553 unidialer_disconnect ();
556 static void sigALRM ()
558 (void) printf("\07timeout waiting for reply\n");
560 longjmp(timeoutbuf, 1);
563 static int unidialer_swallow (register char *match)
568 f = signal(SIGALRM, sigALRM);
572 if (setjmp(timeoutbuf)) {
577 alarm(number(value(DIALTIMEOUT)));
590 if (boolean(value(VERBOSE)))
596 } while (c == *match++);
597 signal(SIGALRM, SIG_DFL);
600 if (boolean(value(VERBOSE)))
606 static struct baud_msg {
618 static int unidialer_connect ()
626 if (unidialer_swallow("\r\n") == 0)
628 f = signal(SIGALRM, sigALRM);
630 nc = 0; nl = sizeof(dialer_buf)-1;
631 bzero(dialer_buf, sizeof(dialer_buf));
633 for (nc = 0, nl = sizeof(dialer_buf)-1 ; nl > 0 ; nc++, nl--) {
634 if (setjmp(timeoutbuf))
636 alarm(number(value(DIALTIMEOUT)));
643 if (unidialer_swallow("\n") == 0)
647 if (strcmp(dialer_buf, "RINGING") == 0 &&
648 boolean(value(VERBOSE))) {
650 printf("%s\r\n", dialer_buf);
654 if (strncmp(dialer_buf, "CONNECT",
655 sizeof("CONNECT")-1) != 0)
660 if (boolean(value(VERBOSE)))
661 printf("%s\r\n", dialer_buf);
665 for (bm = baud_msg ; bm->msg ; bm++)
666 if (strcmp(bm->msg, dialer_buf+sizeof("CONNECT")-1) == 0) {
667 if (!acu_setspeed (bm->baud))
671 if (boolean(value(VERBOSE)))
672 printf("%s\r\n", dialer_buf);
681 printf("%s\r\n", dialer_buf);
688 * This convoluted piece of code attempts to get
689 * the unidialer in sync.
691 static int unidialersync ()
697 while (already++ < MAXRETRY) {
698 acu_nap (intercommand_delay);
699 acu_flush (); /* flush any clutter */
700 unidialer_write_str (FD, reset_command); /* reset modem */
701 bzero(buf, sizeof(buf));
702 acu_nap (reset_delay);
703 ioctl (FD, FIONREAD, &len);
705 len = read(FD, buf, sizeof(buf));
708 printf("unidialersync (%s): (\"%s\")\n\r", modem_name, buf);
710 if (index(buf, '0') ||
711 (index(buf, 'O') && index(buf, 'K')))
715 * If not strapped for DTR control,
716 * try to get command mode.
718 acu_nap (escape_guard_time);
719 unidialer_write_str (FD, escape_sequence);
720 acu_nap (escape_guard_time);
721 unidialer_write_str (FD, hangup_command);
723 * Toggle DTR to force anyone off that might have left
724 * the modem connected.
726 acu_nap (escape_guard_time);
727 ioctl (FD, TIOCCDTR, 0);
729 ioctl (FD, TIOCSDTR, 0);
731 acu_nap (intercommand_delay);
732 unidialer_write_str (FD, reset_command);
737 Send commands to modem; impose delay between commands.
739 static void unidialer_modem_cmd (int fd, const char *cmd)
741 static struct timeval oldt = { 0, 0 };
744 if (tod_lt (&newt, &oldt))
746 unsigned int naptime;
747 tod_subfrom (&oldt, newt);
748 naptime = oldt.tv_sec * 1000 + oldt.tv_usec / 1000;
749 if (naptime > intercommand_delay)
752 printf ("unidialer_modem_cmd: suspicious naptime (%u ms)\r\n", naptime);
754 naptime = intercommand_delay;
757 printf ("unidialer_modem_cmd: delaying %u ms\r\n", naptime);
761 unidialer_write_str (fd, cmd);
764 newt.tv_usec = intercommand_delay;
765 tod_addto (&oldt, &newt);
768 static void unidialer_write_str (int fd, const char *cp)
771 printf ("unidialer (%s): sending %s\n", modem_name, cp);
773 unidialer_write (fd, cp, strlen (cp));
776 static void unidialer_write (int fd, const char *cp, int n)
778 acu_nap (intercharacter_delay);
779 for ( ; n-- ; cp++) {
781 acu_nap (intercharacter_delay);
786 static void unidialer_verbose_read()
791 if (ioctl(FD, FIONREAD, &n) < 0)
795 if (read(FD, buf, n) != n)
801 /* end of unidialer.c */