home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / convergent / ctdial.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  329 lines

  1. char *dialv = "Dialing functions CTOS Version-2.00, May 92";
  2.  
  3. /*  C K D I A L  --  Dialing program for connection to remote system */
  4.  
  5. /*
  6.  Author: Herm Fischer, Litton Data Systems, Van Nuys CA (HFISCHER@USC-ECLB)
  7. */ 
  8.  
  9. /* modified for CTOS C2.0 by Joel Dunn, UNC-CH, October 1986 */
  10. /*
  11.    * modified to add support for RACAL modem by Doug Drury - ITT-FSC,
  12.      May 1991
  13.  
  14.    * modified to remove forced tone dialing from hayes modem
  15.      December 1991
  16. */
  17. #include "ctermi.h"
  18. #include "ctcmd.h"
  19.  
  20. extern int local, speed, flow, mdmtyp, techo;
  21. extern char ttname[], sesfil[];
  22.  
  23. #define HAYES 1            /* for mdmtyp settings */
  24. #define VENTEL 2
  25. #define HAYESNV 3        /* internal use, non-verbal V0 setting */
  26. #define RACAL 4            
  27.  
  28. struct keytab mdmtab[] = {        /* Modem types for command parsing */
  29.     "direct",    0, 0,
  30.     "hayes",    HAYES, 0,
  31.     "ventel",    VENTEL, 0,
  32.     "racal",    RACAL, 0
  33. };
  34. int nmdm = (sizeof(mdmtab) / sizeof(struct keytab));
  35.  
  36. #define DIALING 4        /* for ttvt parameter */
  37. #define CONNECT 5
  38.  
  39. #define CONNECTED 1        /* for completion status */
  40. #define FAILED      2
  41. #define BUSY      3
  42.  
  43. static int tries = 0;
  44.  
  45. #define LBUFL 100
  46. static char lbuf[LBUFL];
  47. static char *lbp;
  48.  
  49. static
  50. stripp(s) char *s; {    /* parity stripper aid */
  51.     for ( ; *s ; *s++ &= 0177 );
  52. }
  53.  
  54. /*  D I A L  --  Dial up the remote system */
  55.  
  56. dial(telnbr) char *telnbr; {
  57.  
  58.     char c;
  59.     char *i;
  60.     int waitct, status;
  61.     char errmsg[50], *erp;
  62.     int augMdm;        /* mdmtyp with switch settings added */
  63.     int mdmEcho = 0;    /* assume modem does not echo */
  64.     int n,m;
  65.  
  66.     if (!mdmtyp) {
  67.         printf("Sorry, you must 'set modem' first\n");
  68.         return(-2);
  69.     }
  70.     augMdm = mdmtyp;    /* internal use, to add dialer switches info*/
  71.  
  72.     if (!local) {
  73.         printf("Sorry, you must 'set line' first\n");
  74.         return(-2);
  75.     }
  76.     if (speed < 0) {
  77.         printf("Sorry, you must 'set speed' first\n");
  78.         return(-2);
  79.         } 
  80.         
  81.     if (ttopen(ttname,local,mdmtyp) < 0) {/* Open, no wait for carrier */
  82.         erp = errmsg;
  83.         sprintf(erp,"Sorry, can't open %s",ttname);
  84.         perror(errmsg);
  85.         return(-2);
  86.         }
  87. /* cont'd... */
  88.                     /* interdigit waits for tone dial */
  89. /* ...dial, cont'd */
  90.  
  91.  
  92.     waitct = 1*strlen(telnbr) ;    /* compute time to dial worst case */
  93.     switch (augMdm) {
  94.         case HAYES:
  95.         case HAYESNV:
  96.         waitct += 35;        /* dial tone + carrier waits + slop */
  97.         for (i=telnbr; *i; i++) if (*i == ',') waitct += 2;
  98.         break;
  99.         case VENTEL:
  100.         waitct += 10;        /* guess actual time for dialtones */
  101.         waitct += 10;    /* ventel's apparent patience for carrier */
  102.         for (i=telnbr; *i; i++) if (*i == '%') waitct += 5;
  103.         break;
  104.         case RACAL:
  105.         waitct += 35;
  106.         break;
  107.         }
  108.        if (techo) {
  109.        printf("Dialing thru %s, speed %d, number %s.\r\n",ttname,speed,telnbr);
  110.        printf("The timeout for completing the call is %d seconds.\r\n",waitct);
  111.        }
  112. /* Condition console terminal and communication line */        
  113. /* place line into "clocal" dialing state */
  114. if ( ttpkt(speed,flow) < 0 )  {
  115.     printf("Sorry, Can't condition communication line\n");
  116.     return(-2);
  117.     }
  118.  
  119. /* Put modem into command state */
  120.  
  121. ttflui();            /* flush input buffer if any */
  122.  
  123. #define OKAY 1            /* modem attention attempt status */
  124. #define IGNORE 2
  125. #define GOT_O -2
  126. #define GOT_A -3
  127.  
  128. switch (augMdm) {
  129.     case HAYES:
  130.     case HAYESNV:
  131.     while(tries++ < 4) {
  132.         ttol("AT\r",3);    /* signal for attention, look for response */
  133.         delay(10);    /* give modem a chance to echo */
  134.         status = 0;
  135.         while ( status <= 0 ) {
  136.         switch (ttinc(0) & 0177) {
  137.             case 'A':            /* echoing, ignore */
  138.             status = GOT_A;
  139.             break;
  140.             case 'T':
  141.             if (status == GOT_A) {
  142.                 mdmEcho = 1;    /* expect echoing later */
  143.                 status = 0;
  144.                 break;
  145.             }
  146.             status = IGNORE;
  147.             break;
  148.             case '\n':
  149.             case '\r':
  150.             status = 0;
  151.             break;
  152.             case '0':            /* numeric result code */
  153.             augMdm = HAYESNV;    /* nonverbal result codes */
  154.             status = OKAY;
  155.             break;
  156.             case 'O':            /* maybe English result code*/
  157.             status = GOT_O;
  158.             break;
  159.             case 'K':
  160.             if (status == GOT_O) {
  161.                 augMdm = HAYES;
  162.                 status = OKAY;
  163.                 break;
  164.             }            /* else its default anyway */
  165.             default:
  166.             status = IGNORE;
  167.             break;
  168.             }
  169.         }
  170.         if (status == OKAY) break;
  171.         if (status == IGNORE) ttflui();
  172.         delay(10);        /* wait before retrying */
  173.     }
  174.         if (status == OKAY) break;
  175.         printf("Sorry, can't initialize modem\n");
  176.     ttpkt(speed,flow);        /* cancel dialing state ioctl */
  177.     return(-2);
  178.  
  179. /* cont'd... */
  180.                     /* interdigit waits for tone dial */
  181. /* ...dial, cont'd */
  182.  
  183.     case VENTEL:
  184.     ttoc('\r');        /* Put Ventel into command mode */
  185.     delay(10);
  186.     ttoc('\r');
  187.     delay(10);
  188.     ttoc('\r');
  189.     while( (ttinc(0) & 0177) != '$');
  190.     break;
  191.  
  192.     case RACAL:
  193.         for (n=0; n < LBUFL; n++) lbuf[n] = '\0';
  194.         lbuf[0] = 0005;
  195.         lbuf[1] = '\r';
  196.     ttol(lbuf,2);    /* Get 's attention */
  197.  
  198.         for (n=0; n < LBUFL; n++) lbuf[n] = '\0';
  199.         n = ttinl(lbuf,LBUFL,waitct,'*'); /* look for prompt    */ 
  200.         if (n < 0) {    
  201.             printf("Sorry, can't initialize modem\n");
  202.             status = FAILED;
  203.             ttpkt(speed,flow);
  204.             return(-2);
  205.         }
  206.     break;                   /* Got modems attention! */
  207.  
  208.     default:
  209.     exit(0);        /* should never get here */
  210.     }
  211.     delay(10);            /* give things settling time */
  212.  
  213. /* Dial the number */
  214.  
  215. switch (augMdm) {
  216.     case HAYES:
  217.     case HAYESNV:          
  218.         sprintf(lbuf,"AT D %s\r",telnbr);
  219.     break;
  220.     case VENTEL:
  221.     sprintf(lbuf,"<K%s\r>",telnbr);
  222.     break;
  223.     case RACAL:
  224.     sprintf(lbuf,"D\r");  /* just send dial command modem will */
  225.     break;                /* prompt for number                 */
  226.     }
  227.  
  228.     ttflui();            /* clear out stuff from waking modem up */
  229.     ttol(lbuf,strlen(lbuf));    /* send dialing string */
  230.     ttflui();            /* clear out stuff from dialing */
  231.  
  232. /* cont'd... */
  233. /* interdigit waits for tone dial */
  234. /* ...dial, cont'd */
  235.  
  236.  
  237. /* Check for connection */
  238.  
  239.     status = 0;
  240.     while (status == 0) {
  241.       switch (augMdm) {
  242.     case HAYES:
  243.         for (n = 0; n < LBUFL; n++) lbuf[n] = '\0';
  244.         n = ttinl(lbuf,LBUFL,waitct,'\n');
  245.         if (n > 2) {
  246.         lbp = lbuf;
  247.         while ((*lbp == '\r') || (*lbp == '\n')) lbp++;
  248.         stripp(lbp);
  249.         if (strncmp(lbp,"CONNECT",7) == 0) status = CONNECTED;
  250.         else if (strncmp(lbp,"BUSY",4) == 0) status = BUSY;
  251.         else if (strncmp(lbp,"NO ",3) == 0) status = FAILED;
  252.         }
  253.         else if (n < 0) status = FAILED;
  254.         break;
  255.  
  256.     case HAYESNV:
  257.         c = ttinc(0) & 0177;
  258.         if (mdmEcho) {        /* sponge up dialing string */
  259.         mdmEcho = c!='\r';    /* until return is echoed */
  260.         break;
  261.         }
  262.         if        (c == '1') status = CONNECTED;
  263.         else if (c == '5') status = CONNECTED;
  264.         else if (c == '9') status = CONNECTED;
  265.         else if (c == '7') status = BUSY;
  266.         else if (c == '3') status = FAILED;
  267.         else if (c == '4') status = FAILED;
  268.         else if (c == '6') status = FAILED;
  269.         else if (c == '8') status = FAILED;
  270.         break;
  271.  
  272.     case VENTEL:
  273.         delay(waitct*10);        /* time to allow for connecting */ 
  274.         if ( (ttinc(0) & 0177) == '!') status = CONNECTED;
  275.         break;
  276.  
  277.     case RACAL:
  278.             printf("\n");
  279.           for (n=0; n < LBUFL; n++) lbuf[n] = '\0';
  280.         n = ttinl(lbuf,LBUFL,waitct,'?'); /* look for NUMBER? prompt */
  281.                     
  282.             for (n = 0; n < LBUFL; n++) { 
  283.                 if (lbuf[n] == 'N') break;
  284.         }                            
  285.             for (m=0; lbuf[n] != '\0'; n++) {
  286.                 lbuf[m] = lbuf[n];        
  287.                 m++;
  288.             }
  289.         lbp=lbuf;
  290.         stripp(lbp);
  291.             if (strncmp(lbp,"NUMBER?",7) != 0) {
  292.         status = FAILED;    /* didn't get the NUMBER? prompt */
  293.         break;
  294.         }
  295.         sprintf(lbuf, "%s\r", telnbr); 
  296.         ttflui();
  297.         ttol(lbuf,strlen(lbuf));    /* now DIAL */
  298.         ttflui();
  299.  
  300.             for (n=0; n < LBUFL; n++) lbuf[n] = '\0';
  301.         n = ttinl(lbuf,LBUFL,waitct,'!'); /* look for ON LINE! */
  302.             
  303.             for (n = 0; n < LBUFL; n++) {
  304.             if (lbuf[n] == '!') break;
  305.         }
  306.         for (m=0, n=n-7; m < 8; m++) {
  307.                  lbuf[m] = lbuf[n];
  308.                  n++;
  309.             }
  310.             lbp = lbuf;
  311.             stripp(lbp);     
  312.             if (!strncmp(lbp, "ON LINE!", 8)) status = CONNECTED; 
  313.         else status = FAILED;  
  314.         break;
  315.     }
  316.     }
  317.     
  318. /* Place line into modem-control (non-clocal) state */
  319.  
  320.     if (status == 0) printf("Sorry, Can't get response from modem\r\n");
  321.     else if (status == CONNECTED) printf("Connected!\n");
  322.     else if (status == FAILED) printf("Sorry, No Carrier\r\n");
  323.     else if (status == BUSY) printf("Sorry, Line Busy\r\n");
  324.     else printf("Failed to complete call\r\n");
  325.  
  326.     ttpkt(speed,flow);        /* cancel dialing state ioctl */
  327.     return((status==CONNECTED) ? 0 : -2);
  328. }
  329.