1 /* $NetBSD: hayes.c,v 1.16 2009/01/18 07:12:39 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)hayes.c 8.1 (Berkeley) 6/6/93"; 36 #endif 37 __RCSID("$NetBSD: hayes.c,v 1.16 2009/01/18 07:12:39 lukem Exp $"); 38 #endif /* not lint */ 39 40 /* 41 * Routines for calling up on a Hayes Modem 42 * (based on the old VenTel driver). 43 * The modem is expected to be strapped for "echo". 44 * Also, the switches enabling the DTR and CD lines 45 * must be set correctly. 46 * NOTICE: 47 * The easy way to hang up a modem is always simply to 48 * clear the DTR signal. However, if the +++ sequence 49 * (which switches the modem back to local mode) is sent 50 * before modem is hung up, removal of the DTR signal 51 * has no effect (except that it prevents the modem from 52 * recognizing commands). 53 * (by Helge Skrivervik, Calma Company, Sunnyvale, CA. 1984) 54 */ 55 /* 56 * TODO: 57 * It is probably not a good idea to switch the modem 58 * state between 'verbose' and terse (status messages). 59 * This should be kicked out and we should use verbose 60 * mode only. This would make it consistent with normal 61 * interactive use thru the command 'tip dialer'. 62 */ 63 #include "tip.h" 64 65 #define min(a,b) ((a < b) ? a : b) 66 67 static int timeout = 0; 68 static jmp_buf timeoutbuf; 69 #define DUMBUFLEN 40 70 static char dumbuf[DUMBUFLEN]; 71 72 static void error_rep(char); 73 static char gobble(const char *); 74 static void goodbye(void); 75 static int hay_sync(void); 76 static void sigALRM(int); 77 78 int 79 /*ARGSUSED*/ 80 hay_dialer(char *num, char *acu __unused) 81 { 82 char *cp; 83 int connected = 0; 84 char dummy; 85 struct termios cntrl; 86 87 if (hay_sync() == 0) /* make sure we can talk to the modem */ 88 return(0); 89 if (boolean(value(VERBOSE))) 90 (void)printf("\ndialing..."); 91 (void)fflush(stdout); 92 (void)tcgetattr(FD, &cntrl); 93 cntrl.c_cflag |= HUPCL; 94 (void)tcsetattr(FD, TCSANOW, &cntrl); 95 (void)tcflush(FD, TCIOFLUSH); 96 (void)write(FD, "ATv0\r", 5); /* tell modem to use short status codes */ 97 (void)gobble("\r"); 98 (void)gobble("\r"); 99 (void)write(FD, "ATTD", 4); /* send dial command */ 100 for (cp = num; *cp; cp++) 101 if (*cp == '=') 102 *cp = ','; 103 (void)write(FD, num, strlen(num)); 104 (void)write(FD, "\r", 1); 105 connected = 0; 106 if (gobble("\r")) { 107 if ((dummy = gobble("01234")) != '1') 108 error_rep(dummy); 109 else 110 connected = 1; 111 } 112 if (!connected) 113 return (connected); /* lets get out of here.. */ 114 (void)tcflush(FD, TCIOFLUSH); 115 if (timeout) 116 hay_disconnect(); /* insurance */ 117 return (connected); 118 } 119 120 121 void 122 hay_disconnect(void) 123 { 124 125 /* first hang up the modem*/ 126 #ifdef DEBUG 127 (void)printf("\rdisconnecting modem....\n\r"); 128 #endif 129 (void)ioctl(FD, TIOCCDTR, 0); 130 (void)sleep(1); 131 (void)ioctl(FD, TIOCSDTR, 0); 132 goodbye(); 133 } 134 135 void 136 hay_abort(void) 137 { 138 139 (void)write(FD, "\r", 1); /* send anything to abort the call */ 140 hay_disconnect(); 141 } 142 143 static void 144 /*ARGSUSED*/ 145 sigALRM(int dummy __unused) 146 { 147 148 (void)printf("\07timeout waiting for reply\n\r"); 149 timeout = 1; 150 longjmp(timeoutbuf, 1); 151 } 152 153 static char 154 gobble(const char *match) 155 { 156 char c; 157 sig_t f; 158 size_t i; 159 volatile int status = 0; 160 161 f = signal(SIGALRM, sigALRM); 162 timeout = 0; 163 #ifdef DEBUG 164 (void)printf("\ngobble: waiting for %s\n", match); 165 #endif 166 do { 167 if (setjmp(timeoutbuf)) { 168 (void)signal(SIGALRM, f); 169 return (0); 170 } 171 (void)alarm((unsigned int)number(value(DIALTIMEOUT))); 172 (void)read(FD, &c, 1); 173 (void)alarm(0); 174 c &= 0177; 175 #ifdef DEBUG 176 (void)printf("%c 0x%x ", c, c); 177 #endif 178 for (i = 0; i < strlen(match); i++) 179 if (c == match[i]) 180 status = c; 181 } while (status == 0); 182 (void)signal(SIGALRM, SIG_DFL); 183 #ifdef DEBUG 184 (void)printf("\n"); 185 #endif 186 return (status); 187 } 188 189 static void 190 error_rep(char c) 191 { 192 193 (void)printf("\n\r"); 194 switch (c) { 195 196 case '0': 197 (void)printf("OK"); 198 break; 199 200 case '1': 201 (void)printf("CONNECT"); 202 break; 203 204 case '2': 205 (void)printf("RING"); 206 break; 207 208 case '3': 209 (void)printf("NO CARRIER"); 210 break; 211 212 case '4': 213 (void)printf("ERROR in input"); 214 break; 215 216 case '5': 217 (void)printf("CONNECT 1200"); 218 break; 219 220 default: 221 (void)printf("Unknown Modem error: %c (0x%x)", c, c); 222 } 223 (void)printf("\n\r"); 224 return; 225 } 226 227 /* 228 * set modem back to normal verbose status codes. 229 */ 230 void 231 goodbye(void) 232 { 233 int len; 234 char c; 235 236 (void)tcflush(FD, TCIOFLUSH); 237 if (hay_sync()) { 238 (void)sleep(1); 239 #ifndef DEBUG 240 (void)tcflush(FD, TCIOFLUSH); 241 #endif 242 (void)write(FD, "ATH0\r", 5); /* insurance */ 243 #ifndef DEBUG 244 c = gobble("03"); 245 if (c != '0' && c != '3') { 246 (void)printf("cannot hang up modem\n\r"); 247 (void)printf("please use 'tip dialer' to make sure the line is hung up\n\r"); 248 } 249 #endif 250 (void)sleep(1); 251 (void)ioctl(FD, FIONREAD, &len); 252 #ifdef DEBUG 253 (void)printf("goodbye1: len=%d -- ", len); 254 rlen = read(FD, dumbuf, min(len, DUMBUFLEN)); 255 dumbuf[rlen] = '\0'; 256 (void)printf("read (%d): %s\r\n", rlen, dumbuf); 257 #endif 258 (void)write(FD, "ATv1\r", 5); 259 (void)sleep(1); 260 #ifdef DEBUG 261 (void)ioctl(FD, FIONREAD, &len); 262 (void)printf("goodbye2: len=%d -- ", len); 263 rlen = read(FD, dumbuf, min(len, DUMBUFLEN)); 264 dumbuf[rlen] = '\0'; 265 (void)printf("read (%d): %s\r\n", rlen, dumbuf); 266 #endif 267 } 268 (void)tcflush(FD, TCIOFLUSH); 269 (void)ioctl(FD, TIOCCDTR, 0); /* clear DTR (insurance) */ 270 (void)close(FD); 271 } 272 273 #define MAXRETRY 5 274 275 int 276 hay_sync(void) 277 { 278 int len, retry = 0; 279 280 while (retry++ <= MAXRETRY) { 281 (void)write(FD, "AT\r", 3); 282 (void)sleep(1); 283 (void)ioctl(FD, FIONREAD, &len); 284 if (len) { 285 len = read(FD, dumbuf, (size_t)min(len, DUMBUFLEN)); 286 if (strchr(dumbuf, '0') || 287 (strchr(dumbuf, 'O') && strchr(dumbuf, 'K'))) 288 return(1); 289 #ifdef DEBUG 290 dumbuf[len] = '\0'; 291 (void)printf("hay_sync: (\"%s\") %d\n\r", dumbuf, retry); 292 #endif 293 } 294 (void)ioctl(FD, TIOCCDTR, 0); 295 (void)ioctl(FD, TIOCSDTR, 0); 296 } 297 (void)printf("Cannot synchronize with hayes...\n\r"); 298 return(0); 299 } 300