home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ISC366.ZIP / SERIAL / MODEM.CPP < prev    next >
Text File  |  1993-09-01  |  5KB  |  165 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //                                                                       //
  3. //            File: modem.cpp                                            //
  4. //            started on: 24/2/92                                        //
  5. //                                                                       //
  6. ///////////////////////////////////////////////////////////////////////////
  7. //                                                                       //
  8. //  This class submits to DSR and CTS requests and so is compatible      //
  9. //  with modems. Useful for hardware flow control, also... Also          //
  10. //  transmits MODEM init and hangup strings.                            //
  11. //                                                                       //
  12. ///////////////////////////////////////////////////////////////////////////
  13. //                                                                       //
  14. //                    by Ofer Laor (AKA LeucroTTA)                       //
  15. //                                                                       //
  16. ///////////////////////////////////////////////////////////////////////////
  17.  
  18. #include <bios.h>;   // biostime.
  19. #include <dos.h>;    // inportb, delay.
  20. #include "modem.h";  // MODEM.
  21.  
  22. // this class is serial port compatible submitting to DSRs and CTSs from
  23. // modems on receive.
  24.  
  25.  
  26. // extending the init.
  27. //
  28. void MODEM::extended_init(void)
  29. {
  30.     // turn on DTR, RTS.
  31.     //
  32.     set_controls(0x3);
  33. }
  34.  
  35. // destructor.
  36. //
  37. MODEM::~MODEM()
  38. {
  39.     // turn off DTR, RTS.
  40.     //
  41.     set_controls(0);
  42. }
  43.  
  44. void MODEM::flow_check(void)
  45. {
  46.     // check flow control.
  47.     // if both DSR && CTS are activated:- flow is enabled.
  48.     //
  49.     flow_enabled= ((get_MSR()& 0x30)== 0x30)?TRUE: FALSE;
  50. }
  51.  
  52. void MODEM::wait(unsigned miliseconds)
  53. {
  54.     unsigned long start= biostime(0, 0L);
  55.  
  56.     do {
  57.        if (!(get_MSR()& 0x80))
  58.           return;
  59.     } while ((biostime(0, 0L)- start* 55)< miliseconds);
  60. }
  61.  
  62. void MODEM::disconnect( BOOLEAN boot_enable,
  63.                         const char *escape_sequence,
  64.                         const char *disconnect_string,
  65.                         const char *disconnect_reply_string,
  66.                         const unsigned timeout)
  67. {
  68.     INDEX i;
  69.  
  70.     empty_io_buffers();
  71.  
  72.     for (i=0; escape_sequence[i]!='\x0'; i++) {
  73.         if (escape_sequence[i]!='~')
  74.            *this<< escape_sequence[i];
  75.         else
  76.             delay (500);
  77.     }
  78.     for (i=0; disconnect_string[i]!='\x0'; i++) {
  79.         if (disconnect_string[i]!='~')
  80.            *this<< disconnect_string[i];
  81.         else
  82.             delay(500);
  83.     }
  84.  
  85.     if ((get_MSR()& 0x80)) {
  86.        set_controls(0);
  87.        wait (2000);
  88.        set_controls(0x3);
  89.     }
  90.     else { // disconnected...
  91.          empty_io_buffers();
  92.          return;
  93.     }
  94.  
  95.  
  96.     // WAIT FOR OK_STRING.
  97.     long begin_clock, timeout_begin_clock;
  98.     BYTE temp;
  99.  
  100.     i= 0;
  101.     timeout_begin_clock= begin_clock= biostime(0, 0L);
  102.  
  103.     do {
  104.        if (!disconnect_reply_string[i]) // finished receiving OK string.
  105.           break;
  106.  
  107.        if (*this>> temp) { // if a byte is available check it.
  108.           if (temp== disconnect_reply_string[i])
  109.              i++;
  110.           else {
  111.                i= 0;
  112.                if (temp== disconnect_reply_string[i]) // could be that the char was the start of the string...
  113.                   i++;
  114.           }
  115.  
  116.           begin_clock= biostime(0,0L); // a byte was received (reset pend_ticks timeout).
  117.        }
  118.     } while (((biostime(0,0L)- begin_clock)< timeout)&&
  119.              ((biostime(0,0L)- timeout_begin_clock)< timeout));
  120.  
  121.     if (disconnect_reply_string[i]) { // did not receive OK string.
  122.        // now, if line is still up, boot!
  123.        //
  124.        if ((get_MSR()& 0x80))
  125.           if (boot_enable)
  126.              ; // boot();
  127.     }
  128. }
  129.  
  130. void MODEM::msr_int(const BYTE msr)
  131. {
  132.     if ((msr& 0x44)== 0x44) // ring is up and the interrupt is because of it.
  133.        ring_int (msr);
  134.  
  135.     if (msr& 0x3)   // DSR or CTS changed!
  136.        modem_status_change (msr);
  137.  
  138.     if ((msr& 0x8)) // DCD changed.
  139.        dcd_status_change (msr);
  140. }
  141.  
  142. void MODEM::dcd_status_change(BYTE /* msr */)
  143. {
  144.     // if DCD or CTS changed...
  145.  
  146.     // to be overloaded.
  147. }
  148.  
  149. void MODEM::modem_status_change(BYTE /* msr */)
  150. {
  151.     // if DSR or CTS changed...
  152.  
  153.     // to be overloaded.
  154. }
  155.  
  156. void MODEM::ring_int(BYTE /* msr */)
  157. {
  158.     // to be overloaded.
  159. }
  160.  
  161. BYTE MODEM::get_MSR(void)
  162. {
  163.     return ((BYTE) inportb(com_port+ MSR));
  164. }
  165.