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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)unidialer.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.bin/tip/libacu/unidialer.c,v 1.7 1999/08/28 01:06:30 peter Exp $
34 * Generalized routines for calling up on a Hayes AT command set based modem.
35 * Control variables are pulled out of a modem caps-style database to
36 * configure the driver for a particular modem.
39 #include "pathnames.h"
41 #include <sys/times.h>
47 #include "acucommon.h"
55 mpt_notype
, mpt_string
, mpt_number
, mpt_boolean
59 modem_parm_type_t modem_parm_type
;
74 static char modem_name
[80];
75 static char *dial_command
;
76 static char *hangup_command
;
77 static char *echo_off_command
;
78 static char *reset_command
;
79 static char *init_string
;
80 static char *escape_sequence
;
81 static int hw_flow_control
;
83 static unsigned int intercharacter_delay
;
84 static unsigned int intercommand_delay
;
85 static unsigned int escape_guard_time
;
86 static unsigned int reset_delay
;
88 static int unidialer_dialer(char *num
, char *acu
);
89 static void unidialer_disconnect(void);
90 static void unidialer_abort(void);
91 static int unidialer_connect(void);
92 static int unidialer_swallow(char *);
94 static void unidialer_verbose_read (void);
96 static void unidialer_modem_cmd(int fd
, const char *cmd
);
97 static void unidialer_write(int fd
, const char *cp
, int n
);
98 static void unidialer_write_str(int fd
, const char *cp
);
99 static void sigALRM(int);
100 static int unidialersync(void);
102 static acu_t unidialer
=
106 unidialer_disconnect
,
111 Table of parameters kept in modem database
113 modem_parm_t modem_parms
[] = {
114 { mpt_string
, "dial_command", { &dial_command
}, { "ATDT%s\r" } },
115 { mpt_string
, "hangup_command", { &hangup_command
}, { "ATH\r", } },
116 { mpt_string
, "echo_off_command", { &echo_off_command
}, { "ATE0\r" } },
117 { mpt_string
, "reset_command", { &reset_command
}, { "ATZ\r" } },
118 { mpt_string
, "init_string", { &init_string
}, { "AT{ &F\r", } },
119 { mpt_string
, "escape_sequence", { &escape_sequence
}, { "+++" } },
120 { mpt_boolean
, "hw_flow_control", { (char **)&hw_flow_control
}, { NULL
} },
121 { mpt_boolean
, "lock_baud", { (char **)&lock_baud
}, { NULL
} },
122 { mpt_number
, "intercharacter_delay", { (char **)&intercharacter_delay
}, { (char *)50 } },
123 { mpt_number
, "intercommand_delay", { (char **)&intercommand_delay
}, { (char *)300 } },
124 { mpt_number
, "escape_guard_time", { (char **)&escape_guard_time
}, { (char *)300 } },
125 { mpt_number
, "reset_delay", { (char **)&reset_delay
}, { (char *)3000 } },
126 { mpt_notype
, NULL
, { NULL
}, { NULL
} }
132 static int timeout
= 0;
133 static int connected
= 0;
134 static jmp_buf timeoutbuf
;
136 #define cgetflag(f) (cgetcap(bp, f, ':') != NULL)
140 #define print_str(x) printf (#x " = %s\n", x)
141 #define print_num(x) printf (#x " = %d\n", x)
143 void dumpmodemparms (char *modem
)
145 printf ("modem parms for %s\n", modem
);
146 print_str (dial_command
);
147 print_str (hangup_command
);
148 print_str (echo_off_command
);
149 print_str (reset_command
);
150 print_str (init_string
);
151 print_str (escape_sequence
);
152 print_num (lock_baud
);
153 print_num (intercharacter_delay
);
154 print_num (intercommand_delay
);
155 print_num (escape_guard_time
);
156 print_num (reset_delay
);
161 static int getmodemparms (const char *modem
)
163 char *bp
, *db_array
[3], *modempath
;
167 modempath
= getenv ("MODEMS");
171 if (modempath
!= NULL
)
172 db_array
[ndx
++] = modempath
;
174 db_array
[ndx
++] = _PATH_MODEMS
;
175 db_array
[ndx
] = NULL
;
177 if ((stat
= cgetent (&bp
, db_array
, (char *)modem
)) < 0) {
180 warnx ("unknown modem %s", modem
);
183 warnx ("can't open modem description file");
186 warnx ("possible reference loop in modem description file");
191 for (mpp
= modem_parms
; mpp
->name
; mpp
++)
193 switch (mpp
->modem_parm_type
)
196 if (cgetstr (bp
, (char *)mpp
->name
, mpp
->value
.string
) == -1)
197 *mpp
->value
.string
= mpp
->default_value
.string
;
202 if (cgetnum (bp
, (char *)mpp
->name
, &l
) == -1)
203 *mpp
->value
.number
= mpp
->default_value
.number
;
205 *mpp
->value
.number
= (unsigned int)l
;
209 *mpp
->value
.number
= cgetflag ((char *)mpp
->name
);
215 strncpy (modem_name
, modem
, sizeof (modem_name
) - 1);
216 modem_name
[sizeof (modem_name
) - 1] = '\0';
223 unidialer_getmodem(const char *modem_name
)
226 if (getmodemparms (modem_name
))
232 unidialer_modem_ready(void)
235 ioctl (FD
, TIOCMGET
, &state
);
236 return (state
& TIOCM_DSR
) ? 1 : 0;
239 static int unidialer_waitfor_modem_ready (int ms
)
242 for (count
= 0; count
< ms
; count
+= 100)
244 if (unidialer_modem_ready ())
247 printf ("unidialer_waitfor_modem_ready: modem ready.\n");
257 unidialer_tty_clocal(int flag
)
264 t
.c_cflag
&= ~CLOCAL
;
265 tcsetattr (FD
, TCSANOW
, &t
);
269 unidialer_get_modem_response(char *buf
, int bufsz
, int response_timeout
)
272 char c
, *p
= buf
, *lid
= buf
+ bufsz
- 1;
277 f
= signal (SIGALRM
, sigALRM
);
281 if (setjmp (timeoutbuf
)) {
285 printf ("get_response: timeout buf=%s\n", buf
);
290 ualarm (response_timeout
* 1000, 0);
299 if (read (FD
, &c
, 1) == 1)
308 printf ("get_response: unexpected char %s.\n", ctrl (c
));
315 if (read (FD
, &c
, 1) == 1)
320 printf ("get_response: <CRLF> encountered.\n");
328 printf ("get_response: unexpected char %s.\n", ctrl (c
));
335 if (read (FD
, &c
, 1) == 1)
339 else if (c
>= ' ' && p
< lid
)
345 if (read (FD
, &c
, 1) == 1)
354 printf ("get_response: %s\n", buf
);
370 unidialer_get_okay(int ms
)
374 okay
= unidialer_get_modem_response (buf
, sizeof (buf
), ms
) &&
375 strcmp (buf
, "OK") == 0;
380 unidialer_dialer(char *num
, char *acu
)
383 char dial_string
[80];
387 dumpmodemparms (modem_name
);
392 if ((i
= speed(number(value(BAUDRATE
)))) == 0)
397 if (boolean(value(VERBOSE
)))
398 printf("Using \"%s\"\n", acu
);
405 if (!unidialersync()) {
407 printf("tip: can't synchronize with %s\n", modem_name
);
408 logent(value(HOST
), num
, modem_name
, "can't synch up");
412 unidialer_modem_cmd (FD
, echo_off_command
); /* turn off echoing */
417 if (boolean(value(VERBOSE
)))
418 unidialer_verbose_read();
421 acu_flush (); /* flush any clutter */
423 unidialer_modem_cmd (FD
, init_string
);
425 if (!unidialer_get_okay (reset_delay
))
430 for (cp
= num
; *cp
; cp
++)
434 (void) sprintf (dial_string
, dial_command
, num
);
436 unidialer_modem_cmd (FD
, dial_string
);
438 connected
= unidialer_connect ();
440 if (connected
&& hw_flow_control
) {
441 acu_hw_flow_control (hw_flow_control
);
445 sprintf(line
, "%d second dial timeout",
446 number(value(DIALTIMEOUT
)));
447 logent(value(HOST
), num
, modem_name
, line
);
451 unidialer_disconnect ();
457 unidialer_disconnect(void)
461 acu_flush (); /* flush any clutter */
463 unidialer_tty_clocal (TRUE
);
465 /* first hang up the modem*/
466 ioctl (FD
, TIOCCDTR
, 0);
468 ioctl (FD
, TIOCSDTR
, 0);
471 * If AT&D2, then dropping DTR *should* just hangup the modem. But
472 * some modems reset anyway; also, the modem may be programmed to reset
473 * anyway with AT&D3. Play it safe and wait for the full reset time before
476 acu_nap (reset_delay
);
478 if (!unidialer_waitfor_modem_ready (reset_delay
))
481 printf ("unidialer_disconnect: warning CTS low.\r\n");
486 * If not strapped for DTR control, try to get command mode.
488 for (retries
= okay
= 0; retries
< MAXRETRY
&& !okay
; retries
++)
491 /* flush any clutter */
495 printf ("unidialer_disconnect: warning flush failed.\r\n");
498 timeout_value
= escape_guard_time
;
499 timeout_value
+= (timeout_value
* retries
/ MAXRETRY
);
500 acu_nap (timeout_value
);
501 acu_flush (); /* flush any clutter */
502 unidialer_modem_cmd (FD
, escape_sequence
);
503 acu_nap (timeout_value
);
504 unidialer_modem_cmd (FD
, hangup_command
);
505 okay
= unidialer_get_okay (reset_delay
);
509 logent(value(HOST
), "", modem_name
, "can't hang up modem");
510 if (boolean(value(VERBOSE
)))
511 printf("hang up failed\n");
518 unidialer_abort(void)
520 unidialer_write_str (FD
, "\r"); /* send anything to abort the call */
521 unidialer_disconnect ();
527 (void) printf("\07timeout waiting for reply\n");
529 longjmp(timeoutbuf
, 1);
532 static int unidialer_swallow (char *match
)
537 f
= signal(SIGALRM
, sigALRM
);
541 if (setjmp(timeoutbuf
)) {
546 alarm(number(value(DIALTIMEOUT
)));
559 if (boolean(value(VERBOSE
)))
565 } while (c
== *match
++);
566 signal(SIGALRM
, SIG_DFL
);
569 if (boolean(value(VERBOSE
)))
575 static struct baud_msg
{
583 { " 9600/ARQ", B9600
},
588 unidialer_connect(void)
596 if (unidialer_swallow("\r\n") == 0)
598 f
= signal(SIGALRM
, sigALRM
);
600 nc
= 0; nl
= sizeof(dialer_buf
)-1;
601 bzero(dialer_buf
, sizeof(dialer_buf
));
603 for (nc
= 0, nl
= sizeof(dialer_buf
)-1 ; nl
> 0 ; nc
++, nl
--) {
604 if (setjmp(timeoutbuf
))
606 alarm(number(value(DIALTIMEOUT
)));
613 if (unidialer_swallow("\n") == 0)
617 if (strcmp(dialer_buf
, "RINGING") == 0 &&
618 boolean(value(VERBOSE
))) {
620 printf("%s\r\n", dialer_buf
);
624 if (strncmp(dialer_buf
, "CONNECT",
625 sizeof("CONNECT")-1) != 0)
630 if (boolean(value(VERBOSE
)))
631 printf("%s\r\n", dialer_buf
);
635 for (bm
= baud_msg
; bm
->msg
; bm
++)
636 if (strcmp(bm
->msg
, dialer_buf
+sizeof("CONNECT")-1) == 0) {
637 if (!acu_setspeed (bm
->baud
))
641 if (boolean(value(VERBOSE
)))
642 printf("%s\r\n", dialer_buf
);
650 printf("%s\r\n", dialer_buf
);
657 * This convoluted piece of code attempts to get
658 * the unidialer in sync.
667 while (already
++ < MAXRETRY
) {
668 acu_nap (intercommand_delay
);
669 acu_flush (); /* flush any clutter */
670 unidialer_write_str (FD
, reset_command
); /* reset modem */
671 bzero(buf
, sizeof(buf
));
672 acu_nap (reset_delay
);
673 ioctl (FD
, FIONREAD
, &len
);
675 len
= read(FD
, buf
, sizeof(buf
));
678 printf("unidialersync (%s): (\"%s\")\n\r", modem_name
, buf
);
680 if (index(buf
, '0') ||
681 (index(buf
, 'O') && index(buf
, 'K')))
685 * If not strapped for DTR control,
686 * try to get command mode.
688 acu_nap (escape_guard_time
);
689 unidialer_write_str (FD
, escape_sequence
);
690 acu_nap (escape_guard_time
);
691 unidialer_write_str (FD
, hangup_command
);
693 * Toggle DTR to force anyone off that might have left
694 * the modem connected.
696 acu_nap (escape_guard_time
);
697 ioctl (FD
, TIOCCDTR
, 0);
699 ioctl (FD
, TIOCSDTR
, 0);
701 acu_nap (intercommand_delay
);
702 unidialer_write_str (FD
, reset_command
);
707 Send commands to modem; impose delay between commands.
709 static void unidialer_modem_cmd (int fd
, const char *cmd
)
711 static struct timeval oldt
= { 0, 0 };
714 if (tod_lt (&newt
, &oldt
))
716 unsigned int naptime
;
717 tod_subfrom (&oldt
, newt
);
718 naptime
= oldt
.tv_sec
* 1000 + oldt
.tv_usec
/ 1000;
719 if (naptime
> intercommand_delay
)
722 printf ("unidialer_modem_cmd: suspicious naptime (%u ms)\r\n", naptime
);
724 naptime
= intercommand_delay
;
727 printf ("unidialer_modem_cmd: delaying %u ms\r\n", naptime
);
731 unidialer_write_str (fd
, cmd
);
734 newt
.tv_usec
= intercommand_delay
;
735 tod_addto (&oldt
, &newt
);
738 static void unidialer_write_str (int fd
, const char *cp
)
741 printf ("unidialer (%s): sending %s\n", modem_name
, cp
);
743 unidialer_write (fd
, cp
, strlen (cp
));
746 static void unidialer_write (int fd
, const char *cp
, int n
)
748 acu_nap (intercharacter_delay
);
749 for ( ; n
-- ; cp
++) {
751 acu_nap (intercharacter_delay
);
756 static void unidialer_verbose_read(void)
761 if (ioctl(FD
, FIONREAD
, &n
) < 0)
765 if (read(FD
, buf
, n
) != n
)
771 /* end of unidialer.c */