Home | History | Annotate | Line # | Download | only in aculib
v831.c revision 1.11
      1 /*	$NetBSD: v831.c,v 1.11 2006/04/03 00:51:14 perry 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[] = "@(#)v831.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: v831.c,v 1.11 2006/04/03 00:51:14 perry Exp $");
     38 #endif /* not lint */
     39 
     40 /*
     41  * Routines for dialing up on Vadic 831
     42  */
     43 #include "tip.h"
     44 
     45 static jmp_buf jmpbuf;
     46 static int child = -1;
     47 
     48 static	void	alarmtr(int);
     49 static	int	dialit(char *, char *);
     50 static	char   *sanitize(char *);
     51 
     52 int
     53 v831_dialer(char *num, char *acu)
     54 {
     55         int status, mypid;
     56         int timelim;
     57 
     58         if (boolean(value(VERBOSE)))
     59                 printf("\nstarting call...");
     60 #ifdef DEBUG
     61         printf ("(acu=%s)\n", acu);
     62 #endif
     63         if ((AC = open(acu, O_RDWR)) < 0) {
     64                 if (errno == EBUSY)
     65                         printf("line busy...");
     66                 else
     67                         printf("acu open error...");
     68                 return (0);
     69         }
     70         if (setjmp(jmpbuf)) {
     71                 kill(child, SIGKILL);
     72                 close(AC);
     73                 return (0);
     74         }
     75         signal(SIGALRM, alarmtr);
     76         timelim = 5 * strlen(num);
     77         alarm(timelim < 30 ? 30 : timelim);
     78         if ((child = fork()) == 0) {
     79                 /*
     80                  * ignore this stuff for aborts
     81                  */
     82                 signal(SIGALRM, SIG_IGN);
     83 		signal(SIGINT, SIG_IGN);
     84                 signal(SIGQUIT, SIG_IGN);
     85                 sleep(2);
     86                 exit(dialit(num, acu) != 'A');
     87         }
     88         /*
     89          * open line - will return on carrier
     90          */
     91         if ((FD = open(DV, O_RDWR)) < 0) {
     92 #ifdef DEBUG
     93                 printf("(after open, errno=%d)\n", errno);
     94 #endif
     95                 if (errno == EIO)
     96                         printf("lost carrier...");
     97                 else
     98                         printf("dialup line open failed...");
     99                 alarm(0);
    100                 kill(child, SIGKILL);
    101                 close(AC);
    102                 return (0);
    103         }
    104         alarm(0);
    105         signal(SIGALRM, SIG_DFL);
    106         while ((mypid = wait(&status)) != child && mypid != -1)
    107                 ;
    108         if (status) {
    109                 close(AC);
    110                 return (0);
    111         }
    112         return (1);
    113 }
    114 
    115 static void
    116 alarmtr(int dummy)
    117 {
    118 
    119         alarm(0);
    120         longjmp(jmpbuf, 1);
    121 }
    122 
    123 /*
    124  * Insurance, for some reason we don't seem to be
    125  *  hanging up...
    126  */
    127 void
    128 v831_disconnect(void)
    129 {
    130 	struct termios	cntrl;
    131 
    132         sleep(2);
    133 #ifdef DEBUG
    134         printf("[disconnect: FD=%d]\n", FD);
    135 #endif
    136         if (FD > 0) {
    137                 ioctl(FD, TIOCCDTR, 0);
    138 		tcgetattr(FD, &cntrl);
    139 		cfsetospeed(&cntrl, 0);
    140 		cfsetispeed(&cntrl, 0);
    141 		tcsetattr(FD, TCSAFLUSH, &cntrl);
    142                 ioctl(FD, TIOCNXCL, NULL);
    143         }
    144         close(FD);
    145 }
    146 
    147 void
    148 v831_abort(void)
    149 {
    150 
    151 #ifdef DEBUG
    152         printf("[abort: AC=%d]\n", AC);
    153 #endif
    154         sleep(2);
    155         if (child > 0)
    156                 kill(child, SIGKILL);
    157         if (AC > 0)
    158                 ioctl(FD, TIOCNXCL, NULL);
    159                 close(AC);
    160         if (FD > 0)
    161                 ioctl(FD, TIOCCDTR, 0);
    162         close(FD);
    163 }
    164 
    165 /*
    166  * Sigh, this probably must be changed at each site.
    167  */
    168 struct vaconfig {
    169 	const char	*vc_name;
    170 	char	vc_rack;
    171 	char	vc_modem;
    172 } vaconfig[] = {
    173 	{ "/dev/cua0",'4','0' },
    174 	{ "/dev/cua1",'4','1' },
    175 	{ 0 }
    176 };
    177 
    178 #define pc(x)	(c = x, write(AC,&c,1))
    179 #define ABORT	01
    180 #define SI	017
    181 #define STX	02
    182 #define ETX	03
    183 
    184 static int
    185 dialit(char *phonenum, char *acu)
    186 {
    187         struct vaconfig *vp;
    188 	struct termios cntrl;
    189         char c;
    190         int i;
    191 
    192         phonenum = sanitize(phonenum);
    193 #ifdef DEBUG
    194         printf ("(dial phonenum=%s)\n", phonenum);
    195 #endif
    196         if (*phonenum == '<' && phonenum[1] == 0)
    197                 return ('Z');
    198 	for (vp = vaconfig; vp->vc_name; vp++)
    199 		if (strcmp(vp->vc_name, acu) == 0)
    200 			break;
    201 	if (vp->vc_name == 0) {
    202 		printf("Unable to locate dialer (%s)\n", acu);
    203 		return ('K');
    204 	}
    205 	tcgetattr(AC, &cntrl);
    206 	cfsetospeed(&cntrl, B2400);
    207 	cfsetispeed(&cntrl, B2400);
    208 	cntrl.c_cflag |= PARODD | PARENB;
    209 	cntrl.c_lflag &= ~(ISIG | ICANON);
    210 	tcsetattr(AC, TCSANOW, &cntrl);
    211 	tcflush(AC, TCIOFLUSH);
    212         pc(STX);
    213 	pc(vp->vc_rack);
    214 	pc(vp->vc_modem);
    215 	while (*phonenum && *phonenum != '<')
    216 		pc(*phonenum++);
    217         pc(SI);
    218 	pc(ETX);
    219         sleep(1);
    220         i = read(AC, &c, 1);
    221 #ifdef DEBUG
    222         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
    223 #endif
    224         if (i != 1)
    225 		c = 'M';
    226         if (c == 'B' || c == 'G') {
    227                 char cc, oc = c;
    228 
    229                 pc(ABORT);
    230                 read(AC, &cc, 1);
    231 #ifdef DEBUG
    232                 printf("abort response=%c\n", cc);
    233 #endif
    234                 c = oc;
    235                 v831_disconnect();
    236         }
    237         close(AC);
    238 #ifdef DEBUG
    239         printf("dialit: returns %c\n", c);
    240 #endif
    241         return (c);
    242 }
    243 
    244 static char *
    245 sanitize(char *s)
    246 {
    247         static char buf[128];
    248         char *cp;
    249 
    250         for (cp = buf; *s && buf + sizeof buf - cp > 1; s++) {
    251 		if (!isdigit((unsigned char)*s) && *s == '<' && *s != '_')
    252 			continue;
    253 		if (*s == '_')
    254 			*s = '=';
    255 		*cp++ = *s;
    256 	}
    257         *cp++ = 0;
    258         return (buf);
    259 }
    260