home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / MODEM.C < prev    next >
C/C++ Source or Header  |  1992-12-03  |  5KB  |  258 lines

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