home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / n / n002 / 4.ddi / DEMOS.ZIP / RES_ANSW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-16  |  4.6 KB  |  191 lines

  1. /* 
  2.    This program will answer an incomming call in the background, send a brief
  3.    message to the other caller, and then hang-up and wait for the next call.
  4.    In order to "unload" this TSR, the user should hit ALT-U.  Note that the
  5.    different functions of this program are broken into "states" and that only
  6.    one state is executed each time that the function is invoked by the timer
  7.    tick.
  8. */
  9.  
  10. #include  <stdio.h>
  11. #include  "resproto.h"
  12. #include  "eclproto.h"
  13. #include  "tsr.h"
  14. #include  "xc.h"
  15. #include  "im.h"
  16.  
  17. #define  SCAN_U  0X16
  18.  
  19. /* Answer COMM States */
  20. #define  COMMINIT     0   /* Initialize the serial port */
  21. #define  OPENMODEM    1   /* Initialize the modem */
  22. #define  WAIT_RING    2   /* Wait for the phone to ring */
  23. #define  SETTIMER     3   /* Wait for the line to stablize after making a connection */
  24. #define  SENDINFO     4   /* Send messag to other modem */
  25. #define  COMMHANGUP   5      /* Hang-up the line */
  26. #define  COMMEXIT     6   /* exit the Comm library */
  27. #define  COMMFIN      7   /* Unload the TSR */
  28.  
  29. extern int  _hotkey_hit;
  30.  
  31. void res_answ(void);  /* TSR to answer the phone */
  32. void display_message(char *buffer);
  33.  
  34. /* program globals */
  35. int  comm_state =  COMMINIT;  /* Holds the current state of the TSR */
  36. char buffer[50]="";
  37. char sav_buf[320];
  38. int s_len;
  39. int port = COM1;
  40.  
  41.  
  42. void main(void)
  43.  
  44. {
  45.     unsigned int r_sig1 = 0X9EDC;
  46.     int stat;
  47.     int toggle = 0X08;     /* ALT key */
  48.     int sc_code = SCAN_U;  /* U key */
  49.     int  pars = 500;
  50.  
  51.  
  52.  
  53.     stat = tsrloaded(r_sig1);
  54.     if (stat!=1)  { /* tsr not loaded */
  55.         stat = inittsr2(toggle,sc_code,res_answ,pars,r_sig1,SIG,(void far *)NULL);
  56.         if (stat!=0)  { /* TSR not loaded */
  57.             switch (stat)  {
  58.                 case 1:
  59.                     printf("\nNot enough memory to satisfy PARS request\n");
  60.                     break;
  61.                 case 2:
  62.                     printf("\nNot compatible with MS/PC DOS system\n");
  63.                     break;
  64.                 default:
  65.                     printf("\nError installing TSR, stat = %d\n",stat);
  66.                     break;
  67.             }
  68.         }
  69.         else
  70.             printf("\nTSR ALREADY LOADED : Hit ALT-U to Unload\n");
  71.     }
  72. }
  73.  
  74. void  res_answ()
  75. {
  76.     int stat;
  77.     
  78.     /* Unload TSR if ALT-U hit */
  79.     if (_hotkey_hit) {
  80.         comm_state = COMMEXIT;
  81.         _hotkey_hit = 0;
  82.     }
  83.  
  84.      switch (comm_state)  {
  85.         case COMMINIT:
  86.             savwindo(23,0,24,79,sav_buf);
  87.             display_message("Initializing COMM port...");
  88.             stat = xc_entr(4);
  89.             if (stat == 0)  {
  90.                 xc_link(port,0);
  91.                 xc_dtr(port,1);
  92.                 xc_rts(port,1);
  93.                 xc_init(port,BAUD1200,NOPAR,DATA8,STOP1);
  94.                 xct_link();
  95.                 comm_state = OPENMODEM;
  96.             }
  97.             else {
  98.                 printf("Error during xc_entr()");
  99.                 comm_state = COMMFIN;
  100.             }
  101.             break;
  102.  
  103.         case OPENMODEM:
  104.             display_message("OPENING MODEM ...");
  105.             stat = im_open(port);
  106.             sprintf(buffer,"MODEM OPEN STATUS = %d",stat);
  107.             display_message(buffer);
  108.          im_spkr(port,IM_SPKR_OFF);
  109.             xct_set(10800); /* 10 min countdown */
  110.             display_message("WAITING FOR PHONE TO RING...  HIT ALT-U TO EXIT");
  111.             comm_state = WAIT_RING;
  112.             break;
  113.  
  114.         case WAIT_RING:
  115.             if ((xc_ri(port))&&(xct_test()))  {  /* phone is ringing */
  116.                 display_message("PHONE RANG!  NOW WAIT IN BETWEEN RINGS...");
  117.                 while(stat=xc_ri(port))
  118.                 ;
  119.                 xct_pend(8);
  120.                 display_message("ANSWER THE PHONE...");
  121.                 if((stat = im_answ(port))>0)  {
  122.                     sprintf(buffer,"CONNECTED AT BAUD RATE : %d",stat);
  123.                     display_message(buffer);
  124.                     s_len = strlen(buffer);
  125.                     comm_state = SETTIMER;
  126.                 }
  127.                 else  {
  128.                     sprintf(buffer,"UNABLE TO CONNECT, stat = %d",stat);
  129.                     display_message(buffer);
  130.                     comm_state = COMMHANGUP;
  131.                 }
  132.             }
  133.             else  {
  134.                 if (!(xct_test()))  {
  135.                     display_message("TIMEOUT PERIOD HAS EXPIRED");
  136.                     comm_state = COMMEXIT;
  137.                 }
  138.             }
  139.             break;
  140.  
  141.         case  SETTIMER:
  142.             xct_set(18);
  143.             comm_state = SENDINFO;
  144.             break;
  145.  
  146.         case  SENDINFO:
  147.             if (!(xct_test()))  {
  148.                     display_message("Sending Info to other PC");
  149.                     stat = xc_put(port,buffer,&s_len);
  150.                     comm_state = COMMHANGUP;
  151.             }
  152.             break;
  153.  
  154.         case COMMHANGUP:
  155.             display_message("HANGUP THE PHONE AND WAIT FOR NEXT CALL...  HIT ALT-U TO EXIT");
  156.             im_hangu(port);
  157.             xct_set(10800); /* 10 min countdown */
  158.             comm_state = WAIT_RING;
  159.             break;
  160.  
  161.         case COMMEXIT:
  162.             xct_unlk();
  163.             display_message("HANGUP THE PHONE AND EXIT...");
  164.             im_hangu(port);
  165.             im_close(port);
  166.             xc_dtr(port,0);
  167.             xc_rts(port,0);
  168.             xc_unlk(port);
  169.             comm_state = COMMFIN;
  170.             break;
  171.  
  172.         case COMMFIN:
  173.             xc_exit();
  174.             stat = freetsr();
  175.             rstwindo(23,0,24,79,sav_buf);
  176.             break;            
  177.     }                                                    
  178. }
  179.  
  180.  
  181.  
  182.  
  183. void display_message(char *buffer)
  184. {
  185.     scroll_vid(1,15,1,23,0,24,79,0);  /* scroll up 1 line */
  186.     savcur();
  187.     loc_cur(24,0);
  188.     printf("%s",buffer);
  189.     rstcur();
  190. }
  191.