148651Sbostic /*-
2*62384Sbostic * Copyright (c) 1985, 1993
3*62384Sbostic * The Regents of the University of California. All rights reserved.
448651Sbostic *
548651Sbostic * %sccs.include.proprietary.c%
648651Sbostic */
748651Sbostic
817774Sralph #ifndef lint
9*62384Sbostic static char sccsid[] = "@(#)hys.c 8.1 (Berkeley) 06/06/93";
1048651Sbostic #endif /* not lint */
1117774Sralph
1246875Sbostic #include "condevs.h"
1317774Sralph
1425154Sbloom #ifdef USR2400
1525154Sbloom #define DROPDTR
1625885Sbloom /*
1725885Sbloom * The "correct" switch settings for a USR Courier 2400 are
1825885Sbloom * Dialin/out: 0 0 1 1 0 0 0 1 0 0
1925885Sbloom * Dialout only: 0 0 1 1 1 1 0 1 0 0
2025885Sbloom * where 0 = off and 1 = on
2125885Sbloom */
2225154Sbloom #endif USR2400
2325154Sbloom
2423698Sbloom /*
2517774Sralph * hyspopn(telno, flds, dev) connect to hayes smartmodem (pulse call)
2617774Sralph * hystopn(telno, flds, dev) connect to hayes smartmodem (tone call)
2717774Sralph * char *flds[], *dev[];
2817774Sralph *
2917774Sralph * return codes:
3017774Sralph * >0 - file number - ok
3117774Sralph * CF_DIAL,CF_DEVICE - failed
3217774Sralph */
3317774Sralph
hyspopn(telno,flds,dev)3417774Sralph hyspopn(telno, flds, dev)
3517774Sralph char *telno, *flds[];
3617774Sralph struct Devices *dev;
3717774Sralph {
3817774Sralph return hysopn(telno, flds, dev, 0);
3917774Sralph }
4017774Sralph
hystopn(telno,flds,dev)4117774Sralph hystopn(telno, flds, dev)
4217774Sralph char *telno, *flds[];
4317774Sralph struct Devices *dev;
4417774Sralph {
4517774Sralph return hysopn(telno, flds, dev, 1);
4617774Sralph }
4717774Sralph
4817774Sralph /* ARGSUSED */
hysopn(telno,flds,dev,toneflag)4917774Sralph hysopn(telno, flds, dev, toneflag)
5017774Sralph char *telno;
5117774Sralph char *flds[];
5217774Sralph struct Devices *dev;
5317774Sralph int toneflag;
5417774Sralph {
5517774Sralph extern errno;
5617774Sralph char dcname[20];
5723731Sbloom char cbuf[MAXPH];
5823731Sbloom register char *cp;
5923731Sbloom register int i;
6025885Sbloom int dh = -1, nrings = 0;
6117774Sralph
6217774Sralph sprintf(dcname, "/dev/%s", dev->D_line);
6317774Sralph DEBUG(4, "dc - %s\n", dcname);
6417774Sralph if (setjmp(Sjbuf)) {
6517774Sralph logent(dcname, "TIMEOUT");
6617774Sralph if (dh >= 0)
6725885Sbloom hyscls(dh);
6817774Sralph return CF_DIAL;
6917774Sralph }
7017774Sralph signal(SIGALRM, alarmtr);
7117774Sralph getnextfd();
7217774Sralph alarm(10);
7317774Sralph dh = open(dcname, 2); /* read/write */
7417774Sralph alarm(0);
7517774Sralph
7617774Sralph /* modem is open */
7717774Sralph next_fd = -1;
7817774Sralph if (dh >= 0) {
7917774Sralph fixline(dh, dev->D_speed);
8017774Sralph if (dochat(dev, flds, dh)) {
8117774Sralph logent(dcname, "CHAT FAILED");
8225885Sbloom hyscls(dh);
8317774Sralph return CF_DIAL;
8417774Sralph }
8525154Sbloom write(dh, "ATV1E0H\r", 8);
8617774Sralph if (expect("OK\r\n", dh) != 0) {
8717774Sralph logent(dcname, "HSM seems dead");
8825154Sbloom hyscls(dh);
8917774Sralph return CF_DIAL;
9017774Sralph }
9125154Sbloom #ifdef USR2400
9225885Sbloom write(dh, "ATX6S7=44\r", 10);
9325154Sbloom if (expect("OK\r\n", dh) != 0) {
9425154Sbloom logent(dcname, "HSM seems dead");
9525154Sbloom hyscls(dh);
9625154Sbloom return CF_DIAL;
9725154Sbloom }
9825154Sbloom #endif USR2400
9917774Sralph if (toneflag)
10017774Sralph write(dh, "\rATDT", 5);
10117774Sralph else
10225885Sbloom #ifdef USR2400
10325885Sbloom write(dh, "\rATD", 4);
10425885Sbloom #else HAYES
10517774Sralph write(dh, "\rATDP", 5);
10625885Sbloom #endif HAYES
10717774Sralph write(dh, telno, strlen(telno));
10817774Sralph write(dh, "\r", 1);
10917774Sralph
11023731Sbloom if (setjmp(Sjbuf)) {
11123731Sbloom logent(dcname, "TIMEOUT");
11223731Sbloom strcpy(devSel, dev->D_line);
11323731Sbloom hyscls(dh);
11423731Sbloom return CF_DIAL;
11523731Sbloom }
11623731Sbloom signal(SIGALRM, alarmtr);
11725885Sbloom alarm(2*MAXMSGTIME);
11825154Sbloom do {
11925154Sbloom cp = cbuf;
12025154Sbloom while (read(dh, cp ,1) == 1)
12125154Sbloom if (*cp >= ' ')
12225154Sbloom break;
12325154Sbloom while (++cp < &cbuf[MAXPH] && read(dh, cp, 1) == 1 && *cp != '\n')
12425154Sbloom ;
12525154Sbloom alarm(0);
12625154Sbloom *cp-- = '\0';
12725154Sbloom if (*cp == '\r')
12825154Sbloom *cp = '\0';
12925154Sbloom DEBUG(4,"\nGOT: %s", cbuf);
13025885Sbloom alarm(MAXMSGTIME);
13133585Srick } while ((strncmp(cbuf, "RING", 4) == 0 ||
13233585Srick strncmp(cbuf, "RRING", 5) == 0) && nrings++ < 5);
13323731Sbloom if (strncmp(cbuf, "CONNECT", 7) != 0) {
13425154Sbloom logent(cbuf, _FAILED);
13517774Sralph strcpy(devSel, dev->D_line);
13617774Sralph hyscls(dh);
13717774Sralph return CF_DIAL;
13817774Sralph }
13933585Srick #undef DONTRESETBAUDRATE
14033585Srick #ifndef DONTRESETBAUDRATE
14123731Sbloom i = atoi(&cbuf[8]);
14223731Sbloom if (i > 0 && i != dev->D_speed) {
14323731Sbloom DEBUG(4,"Baudrate reset to %d\n", i);
14423731Sbloom fixline(dh, i);
14523731Sbloom }
14633585Srick #endif /* DONTRESETBAUDRATE */
14717774Sralph
14817774Sralph }
14917774Sralph if (dh < 0) {
15017774Sralph logent(dcname, "CAN'T OPEN");
15117774Sralph return dh;
15217774Sralph }
15317774Sralph DEBUG(4, "hayes ok\n", CNULL);
15417774Sralph return dh;
15517774Sralph }
15617774Sralph
hyscls(fd)15717774Sralph hyscls(fd)
15817774Sralph int fd;
15917774Sralph {
16017774Sralph char dcname[20];
16117774Sralph #ifdef DROPDTR
16217774Sralph struct sgttyb hup, sav;
16317774Sralph #endif
16417774Sralph
16517774Sralph if (fd > 0) {
16617774Sralph sprintf(dcname, "/dev/%s", devSel);
16717774Sralph DEBUG(4, "Hanging up fd = %d\n", fd);
16817774Sralph #ifdef DROPDTR
16917774Sralph /*
17017774Sralph * code to drop DTR -- change to 0 baud then back to default.
17117774Sralph */
17217774Sralph gtty(fd, &hup);
17317774Sralph gtty(fd, &sav);
17417774Sralph hup.sg_ispeed = B0;
17517774Sralph hup.sg_ospeed = B0;
17617774Sralph stty(fd, &hup);
17717774Sralph sleep(2);
17817774Sralph stty(fd, &sav);
17917774Sralph /*
18017774Sralph * now raise DTR -- close the device & open it again.
18117774Sralph */
18217774Sralph sleep(2);
18317774Sralph close(fd);
18417774Sralph sleep(2);
18517774Sralph fd = open(dcname, 2);
18625154Sbloom stty(fd, &sav);
18717774Sralph #else
18817774Sralph sleep(3);
18917774Sralph write(fd, "+++", 3);
19017774Sralph #endif
19117774Sralph sleep(3);
19233585Srick write(fd, "ATH\r", 4);
19333585Srick /*
19423698Sbloom if (expect("OK",fd) != 0)
19517774Sralph logent(devSel, "HSM did not respond to ATZ");
19633585Srick */
19717774Sralph sleep(1);
19833585Srick write(fd, "ATZ\r", 4);
19933585Srick sleep(1);
20017774Sralph close(fd);
20117774Sralph delock(devSel);
20217774Sralph }
20317774Sralph }
204