home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / WWIVSOR.ZIP / MODEM.C < prev    next >
C/C++ Source or Header  |  1995-04-25  |  5KB  |  284 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1995 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15.  
  16.  
  17. #include "vars.h"
  18.  
  19. #pragma hdrstop
  20.  
  21. #include <math.h>
  22.  
  23.  
  24. char cid_num[50];
  25. char cid_name[50];
  26.  
  27.  
  28. #define modem_time 3.5
  29.  
  30.  
  31. void pr1(unsigned char *s)
  32. /* This function ouputs a string to the com port.  This is mainly used
  33.  * for modem commands
  34.  */
  35. {
  36.   int i;
  37.  
  38.   for (i = 0; s[i] > 0; i++)
  39.     if (s[i] == 255)
  40.       wait1(9);
  41.     else
  42.       outcomch(s[i]);
  43. }
  44.  
  45.  
  46.  
  47. void get_modem_line(char *s, double d, int allowa)
  48. {
  49.   int i=0;
  50.   char ch=0, ch1;
  51.   double t;
  52.  
  53.   t = timer();
  54.   do {
  55.     ch = get1c();
  56.     if (kbhitb() && allowa) {
  57.       ch1 = getchd();
  58.       if (upcase(ch1) == 'H') {
  59.         ch = 13;
  60.         s[0] = i = 1;
  61.       }
  62.     } else if (!ch) {
  63.       giveup_timeslice();
  64.     }
  65.     if (ch >= 32)
  66.       s[i++] = upcase(ch);
  67.   } while ((ch != 13) && (fabs(timer() - t) < d) && (i<=40));
  68.   s[i] = 0;
  69. }
  70.  
  71.  
  72.  
  73.  
  74. void do_result(result_info *ri)
  75. {
  76.   if (ri->description[0])
  77.     if (ri->flag_value & flag_append)
  78.       strcat(curspeed, ri->description);
  79.     else
  80.       strcpy(curspeed, ri->description);
  81.  
  82.   if (ri->main_mode)
  83.     modem_mode = ri->main_mode;
  84.  
  85.   /* ignore the ringing mode, caller-id stuff */
  86.   if ((modem_mode == mode_ringing) ||
  87.       (modem_mode == mode_cid_num) ||
  88.       (modem_mode == mode_cid_name)) {
  89.     modem_mode=0;
  90.   }
  91.  
  92.   modem_flag = (modem_flag & ri->flag_mask) | ri->flag_value;
  93.   if (modem_flag & flag_fc)
  94.     flow_control = 1;
  95.  
  96.   if (ri->com_speed) {
  97.     com_speed = ri->com_speed;
  98.     set_baud(com_speed);
  99.   }
  100.  
  101.   if (ri->modem_speed)
  102.     modem_speed = ri->modem_speed;
  103.  
  104. }
  105.  
  106.  
  107. void process_full_result(char *s)
  108. {
  109.   char *ss;
  110.   int i,i1;
  111.  
  112.   /* first, check for caller-id info */
  113.   for (i=0; i<modem_i->num_resl; i++) {
  114.     i1=strlen(modem_i->resl[i].result);
  115.     if (strncmp(modem_i->resl[i].result, s, i1)==0) {
  116.       switch(modem_i->resl[i].main_mode) {
  117.         case mode_cid_num:
  118.           strcpy(cid_num, s+i1);
  119.           return;
  120.         case mode_cid_name:
  121.           strcpy(cid_name, s+i1);
  122.           return;
  123.       }
  124.     }
  125.   }
  126.  
  127.   ss=strtok(s,modem_i->sepr);
  128.  
  129.   while (ss) {
  130.     for (i=0; i<modem_i->num_resl; i++) {
  131.       if (strcmp(modem_i->resl[i].result,ss)==0) {
  132.         do_result(&(modem_i->resl[i]));
  133.         break;
  134.       }
  135.     }
  136.     ss=strtok(NULL,modem_i->sepr);
  137.   }
  138. }
  139.  
  140. int mode_switch(double d, int allowa)
  141. {
  142.   double t;
  143.   char s[81];
  144.   int abort=0;
  145.  
  146.   t=timer();
  147.   modem_mode = 0;
  148.  
  149.   while ((modem_mode==0) && (fabs(timer()-t)<d) && (!abort)) {
  150.     get_modem_line(s,d+t-timer(), allowa);
  151.     if (s[0]==1)
  152.       abort=1;
  153.     else if (s[0])
  154.       process_full_result(s);
  155.   }
  156.  
  157.   return(modem_mode);
  158. }
  159.  
  160. void holdphone(int d)
  161. {
  162.  
  163.   if (!ok_modem_stuff)
  164.     return;
  165.   if (no_hangup)
  166.     return;
  167.   if ((!(modem_i->pick)) || (!(modem_i->hang)))
  168.     return;
  169.  
  170.   dtr(1);
  171.   if (d) {
  172.     if (!global_xx) {
  173.       if (syscfg.sysconfig & sysconfig_off_hook) {
  174.         do_result(&(modem_i->defl));
  175.         pr1(modem_i->pick);
  176.         xtime=timer();
  177.         global_xx=1;
  178.       }
  179.     }
  180.   } else {
  181.     if (syscfg.sysconfig & sysconfig_off_hook) {
  182.       if (global_xx) {
  183.         dtr(1);
  184.         if (fabs(xtime-timer())<modem_time)
  185.           outs("\r\n\r\nWaiting for modem...");
  186.         while (fabs(xtime-timer())<modem_time)
  187.           ;
  188.         pr1(modem_i->hang);
  189.         imodem(0);
  190.         global_xx=0;
  191.       }
  192.     }
  193.   }
  194. }
  195.  
  196.  
  197.  
  198. void imodem(int x)
  199. {
  200.   int i,done;
  201.   char *is;
  202.   char s[81];
  203.  
  204.   if (!ok_modem_stuff) {
  205.     outs("\x0c");
  206.     return;
  207.   }
  208.  
  209.   if (x)
  210.     is=modem_i->setu;
  211.   else
  212.     is=modem_i->init;
  213.  
  214.   if (!(*is))
  215.     return;
  216.  
  217.   outs("\x0cWaiting...");
  218.   /* begin_crit(); */
  219.   rts(1);
  220.   dtr(1);
  221.   do_result(&(modem_i->defl));
  222.   i=0;
  223.   done=0;
  224.   wait1(9);
  225.   while (!done) {
  226.     initport(syscfgovr.primaryport);
  227.     pr1(is);
  228.     dump();
  229.     if (mode_switch(5.0,0)==mode_norm) {
  230.       done=1;
  231.     } else {
  232.       ++i;
  233.       sprintf(s,"(%d)...",modem_mode);
  234.       outs(s);
  235.     }
  236.     if (i>5)
  237.       done=1;
  238.   }
  239.   /* end_crit(); */
  240.   outs("\x0c");
  241. }
  242.  
  243.  
  244. void answer_phone(void)
  245. {
  246.   double d;
  247.  
  248.   if (!ok_modem_stuff)
  249.     return;
  250.   cid_num[0]=0;
  251.   cid_name[0]=0;
  252.   outs("Answering phone, 'H' to abort.\r\n");
  253.   do_result(&(modem_i->defl));
  254.   pr1(modem_i->ansr);
  255.   d=timer();
  256.   rts(1);
  257.   if ((mode_switch(45.0,1)!=mode_con) && (modem_mode!=mode_fax)) {
  258.     if (modem_mode == 0) {
  259.       outcomch(' ');
  260.       outcomch('\r');
  261.       wait1(18);
  262.       if (fabs(timer()-d)<modem_time) {
  263.         outs("\r\nWaiting for modem...");
  264.         while (fabs(timer()-d)<modem_time)
  265.           ;
  266.       }
  267.       imodem(0);
  268.       imodem(0);
  269.     } else {
  270.       outs(curspeed);
  271.       imodem(0);
  272.       imodem(0);
  273.     }
  274.   } else {
  275.     incom=outcom=1;
  276.     if (!(modem_flag & flag_ec))
  277.       wait1(72);
  278.     else
  279.       wait1(36);
  280.   }
  281. }
  282.  
  283.  
  284.