home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / dip337d-uri / modem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-31  |  2.7 KB  |  127 lines

  1. /*
  2.  * dip        A program for handling dialup IP connecions.
  3.  *        Modem driving module.  On systems that support the
  4.  *        dial(3) package, we (should) use that.  Otherwise,
  5.  *        we use a very rudimentary HAYES-type dialer.
  6.  *
  7.  * Version:    @(#)modem.c    3.3.3    08/16/93
  8.  *
  9.  * Author:      Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10.  *        Copyright 1988-1993 MicroWalt Corporation
  11.  *
  12.  *        This program is free software; you can redistribute it
  13.  *        and/or  modify it under  the terms of  the GNU General
  14.  *        Public  License as  published  by  the  Free  Software
  15.  *        Foundation;  either  version 2 of the License, or  (at
  16.  *        your option) any later version.
  17.  */
  18. #include "dip.h"
  19.  
  20.  
  21. /* Here are some HAYES modem commands. */
  22. #define AT_ATTN        "+++"            /* "attention" string    */
  23. #define AT_RSET        "ATZ"            /* reset the modem    */
  24. #define AT_INIT        "ATE0 Q0 V1 X1"        /* setup for dialing    */
  25. #define AT_DIAL        "ATDT"            /* start a dial        */
  26. #define AT_HANG        "ATH"            /* hang up the modem    */
  27. #define AT_EOL        "\r"            /* AT "CRLF" mark    */
  28.  
  29.  
  30. static char    mdm_type[32];
  31. static int    mdm_setup = 0;            /* can we do anything?    */
  32. static char    *mdm_init_s = AT_INIT;        /* current INIT string    */
  33.  
  34.  
  35. /* Set the desired type of modem. */
  36. int
  37. mdm_modem(char *modem)
  38. {
  39.   if (strcmp(modem, DEF_MODEM) != 0) {
  40.     fprintf(stderr, "dip: modem: unknown modem type %s !\n", modem);
  41.     return(-1);
  42.   }
  43.   strcpy(mdm_type, modem);
  44.   mdm_setup = 1;
  45.   if (opt_v == 1) printf("Modem set to \"%s\".\n", mdm_type);
  46.   return(0);
  47. }
  48.  
  49.  
  50. /* Setup an INIT string for the current type of modem. */
  51. int
  52. mdm_init(char *text)
  53. {
  54.   char *s;
  55.  
  56.   if ((s = (char *) malloc(strlen(text) + 1)) == NULL) {
  57.     fprintf(stderr, "Can't allocate memory for string");
  58.     return(-1);
  59.   }
  60.   mdm_init_s = s;
  61.   strcpy(mdm_init_s, text);
  62.   if (opt_v == 1) printf("Modem INIT string set to \"%s\"\n", mdm_init_s);
  63.   return(0);
  64. }
  65.  
  66.  
  67. /* Dial a phone number. */
  68. int
  69. mdm_dial(char *number)
  70. {
  71.   char buff[128];
  72.  
  73.   if (mdm_setup == 1) {
  74.     /* Setup to dial out. */
  75.     sprintf(buff, "%s%s", mdm_init_s, AT_EOL);
  76.     tty_puts(buff);
  77.     (void) sleep(1);
  78.  
  79.     /* Dial the phone number. */
  80.     sprintf(buff, "%s%s%s", AT_DIAL, number, AT_EOL);
  81.     tty_puts(buff);
  82.     return(0);
  83.   }
  84.   return(-1);
  85. }
  86.  
  87.  
  88. int
  89. mdm_reset(void)
  90. {
  91.   char buff[128];
  92.  
  93.   if (mdm_setup == 1) {
  94.     /* Make the modem listen to us. */
  95.     (void) sleep(1);
  96.     tty_puts(AT_ATTN);
  97.     (void) sleep(1);
  98.  
  99.     /* Reset the modem. */
  100.     sprintf(buff, "%s%s", AT_RSET, AT_EOL);
  101.     tty_puts(buff);
  102.     (void) sleep(3);
  103.     return(0);
  104.   }
  105.   return(-1);
  106. }
  107.  
  108. int
  109. mdm_hangup(void)
  110. {
  111.   char buff[128];
  112.  
  113.   if (mdm_setup == 1) {
  114.     /* Make the modem listen to us. */
  115.     (void) sleep(2);
  116.     tty_puts(AT_ATTN);
  117.     (void) sleep(2);
  118.  
  119.     /* Hang up the modem. */
  120.     sprintf(buff, "%s%s", AT_HANG, AT_EOL);
  121.     tty_puts(buff);
  122.     (void) sleep(3);
  123.     return(0);
  124.   }
  125.   return(-1);
  126. }
  127.