home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / COM.C < prev    next >
Text File  |  1995-01-09  |  54KB  |  1,106 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   com.c                                                                   */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Handle async communications between debugger and debuggee.               */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   11/30/92 Created.                                                       */
  12. /*                                                                           */
  13. /*...Release 1.02 (10/22/92)                                                 */
  14. /*...                                                                        */
  15. /*... 03/18/92  803   Joe       Add support for remote debug.                */
  16. /*...                                                                        */
  17. /*****************************************************************************/
  18.  
  19. #include "all.h"
  20.  
  21. static LHANDLE ComHandle;
  22.  
  23. LHANDLE GetComHandle( void ) { return(ComHandle); }
  24. void    SetComHandle( LHANDLE handle ) { ComHandle = handle; }
  25.  
  26. /*****************************************************************************/
  27. /* AsyncInit()                                                               */
  28. /*                                                                           */
  29. /* Description:                                                              */
  30. /*                                                                           */
  31. /*   Set up the com port for remote debug.                                   */
  32. /*                                                                           */
  33. /* Parameters:                                                               */
  34. /*                                                                           */
  35. /*   pConnection   -> to the structure defining the remote connection.       */
  36. /*                                                                           */
  37. /* Return:                                                                   */
  38. /*                                                                           */
  39. /* Assumptions:                                                              */
  40. /*                                                                           */
  41. /*****************************************************************************/
  42. void AsyncInit( CONNECTION *pConnection )
  43. {
  44.  FILE   *fpm;
  45.  APIRET  rc;
  46.  
  47.  /****************************************************************************/
  48.  /* - initialize the selected comport at the user defined bitrate.           */
  49.  /****************************************************************************/
  50.  AsyncSetupComPort(pConnection->BitRate,pConnection->ComPort);
  51.  
  52.  if( pConnection->DbgOrEsp == _DBG )
  53.  {
  54.   /***************************************************************************/
  55.   /* - if we're using a modem, then read a file of modem commands            */
  56.   /*   specifically defined for the Dbg end of the connection and then       */
  57.   /*   make the connection to esp through the modem.                         */
  58.   /***************************************************************************/
  59.   if( pConnection->modem == TRUE )
  60.   {
  61.    fpm = NULL;
  62.    if( pConnection->pModemFile != NULL )
  63.    {
  64.     fpm = fopen( pConnection->pModemFile, "r" );
  65.     if( fpm == NULL )
  66.      SayMsg(ERR_CANT_OPEN_MODEM);
  67.    }
  68.    Connect( fpm, pConnection );
  69.  
  70.    rc = AsyncGetComMsg("Esp Ready");
  71.    if( rc == FALSE )
  72.     SayMsg(ERR_ESP_NOT_READY);
  73.   }
  74.  }
  75.  else /* pConnection->DbgOrEsp == _ESP */
  76.  {
  77.   /***************************************************************************/
  78.   /* - if we're using a modem there are two possibilities:                   */
  79.   /*    - the modem is in auto-answer with hardware handshaking turned on, or*/
  80.   /*    - the user has specified a response file of modem commands. These    */
  81.   /*      commands will be sent to the modem one line at a time.             */
  82.   /***************************************************************************/
  83.   if( pConnection->modem == TRUE )
  84.   {
  85.    FILE  *fp         = NULL;
  86.    char   EspReady[] = "Esp Ready";
  87.  
  88.    if( pConnection->pModemFile != NULL )
  89.    {
  90.     fp = fopen( pConnection->pModemFile, "r" );
  91.     if( fp == NULL )
  92.      SayMsg(ERR_CANT_OPEN_MODEM);
  93.    }
  94.  
  95.    /**************************************************************************/
  96.    /* - Make the connection.                                                 */
  97.    /* - Flush the modem.                                                     */
  98.    /* - Flush the receive and transmit buffers.                              */
  99.    /* - Send a probe ready message.                                          */
  100.    /**************************************************************************/
  101.    Connect(fp, pConnection );
  102.    AsyncFlushModem();
  103.    AsyncFlushBuffers();
  104.    AsyncSend( EspReady,strlen(EspReady) );
  105.   }
  106.  }
  107. }
  108.  
  109. /*****************************************************************************/
  110. /* AsyncSetupComPort()                                                       */
  111. /*                                                                           */
  112. /* Description:                                                              */
  113. /*                                                                           */
  114. /*   Set up the com port for remote debug.                                   */
  115. /*                                                                           */
  116. /* Parameters:                                                               */
  117. /*                                                                           */
  118. /*   pcmd        -> to user invocation options.                              */
  119. /*                                                                           */
  120. /* Return:                                                                   */
  121. /*                                                                           */
  122. /* Assumptions:                                                              */
  123. /*                                                                           */
  124. /*****************************************************************************/
  125. void AsyncSetupComPort(int BitRate, int ComPort)
  126. {
  127.  APIRET rc;
  128.  USHORT ReadTimeOut  = 0xFFFF;
  129.  USHORT WriteTimeOut = 30000;
  130.  
  131.  /****************************************************************************/
  132.  /* - Try to open the com port.                                              */
  133.  /****************************************************************************/
  134.  rc = AsyncOpen(ComPort);
  135.  if(rc != 0)
  136.   SayMsg(ERR_COM_PORT_OPEN);
  137.  
  138.  /****************************************************************************/
  139.  /* - If a BitRate has been specified                                        */
  140.  /*    - Set up the device control block for the com port                    */
  141.  /*    - Set the parity, stop bits, and data bits                            */
  142.  /*    - Set the bit rate.                                                   */
  143.  /*    - Raise the DTR line.                                                 */
  144.  /*                                                                          */
  145.  /****************************************************************************/
  146.  if( ( BitRate != 0)  &&
  147.      ( AsyncSetDCB(ReadTimeOut, WriteTimeOut)     ||
  148.        AsyncSetLineCtrl()                         ||
  149.        AsyncSetBitRate(BitRate)                   ||
  150.        AsyncSetClearDTR(TRUE)
  151.      )
  152.    )
  153.   SayMsg(ERR_COM_PORT_PARMS);
  154. }
  155.  
  156. /*****************************************************************************/
  157. /* AsyncOpen()                                                               */
  158. /*                                                                           */
  159. /* Description:                                                              */
  160. /*                                                                           */
  161. /*   Open the COM port.                                                      */
  162. /*                                                                           */
  163. /* Parameters:                                                               */
  164. /*                                                                           */
  165. /*   none                                                                    */
  166. /*                                                                           */
  167. /* Return:                                                                   */
  168. /*                                                                           */
  169. /*   rc             DosOpen return code.                                     */
  170. /*                                                                           */
  171. /* Assumptions:                                                              */
  172. /*                                                                           */
  173. /*   ComPort is 0,1,2,3,4.                                                   */
  174. /*   A value of 0 indicates that the user wants to take the default com      */
  175. /*   port.                                                                   */
  176. /*                                                                           */
  177. /*****************************************************************************/
  178.  
  179. static char *port[4] =  {
  180.                          "COM1",
  181.                          "COM2",
  182.                          "COM3",
  183.                          "COM4"
  184.                         };
  185.  
  186. APIRET AsyncOpen(int ComPort)
  187. {
  188.  ULONG      Action;
  189.  APIRET     rc;
  190.  
  191.  /****************************************************************************/
  192.  /* - Set the default com port if the user didn't specify one.               */
  193.  /****************************************************************************/
  194.  if( ComPort == 0 )
  195.   ComPort = 1;
  196.  
  197.  rc = DosOpen(port[ComPort-1], &ComHandle, &Action, 0, FILE_NORMAL, FILE_OPEN,
  198.                OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE, NULL);
  199.  return(rc);
  200. }
  201.  
  202. /*****************************************************************************/
  203. /* AsyncClose()                                                              */
  204. /*                                                                           */
  205. /* Description:                                                              */
  206. /*                                                                           */
  207. /*   Close the COM1 port.                                                    */
  208. /*                                                                           */
  209. /* Parameters:                                                               */
  210. /*                                                                           */
  211. /*   pConnection   -> to the structure defining the remote connection.       */
  212. /*                                                                           */
  213. /* Return:                                                                   */
  214. /*                                                                           */
  215. /*                  DosOpen return code.                                     */
  216. /*                                                                           */
  217. /* Assumptions:                                                              */
  218. /*                                                                           */
  219. /*   none.                                                                   */
  220. /*                                                                           */
  221. /*****************************************************************************/
  222. void AsyncClose( CONNECTION *pConnection )
  223. {
  224.  AsyncSetClearDTR(FALSE);
  225.  
  226.  if( pConnection->modem == TRUE )
  227.   AsyncFlushModem();
  228.  DosClose(ComHandle);
  229. }
  230.  
  231. /*****************************************************************************/
  232. /* AsyncSetDCB()                                                             */
  233. /*                                                                           */
  234. /* Description:                                                              */
  235. /*                                                                           */
  236. /*   Setup the com port device control block.                                */
  237. /*                                                                           */
  238. /* Parameters:                                                               */
  239. /*                                                                           */
  240. /*   ReadTimeOut   how long to wait for the DosRead to return.               */
  241. /*   WriteTimeOut  how long to wait for the DosWrite to return.              */
  242. /*                                                                           */
  243. /* Return:                                                                   */
  244. /*                                                                           */
  245. /*   rc             DosDevIOCtl return code.                                 */
  246. /*                                                                           */
  247. /* Assumptions:                                                              */
  248. /*                                                                           */
  249. /*   none.                                                                   */
  250. /*                                                                           */
  251. /*****************************************************************************/
  252. APIRET AsyncSetDCB( USHORT ReadTimeOut, USHORT WriteTimeOut)
  253. {
  254.  APIRET rc=0;
  255.  DCB    Dcb;
  256.  ULONG  DcbLenInOut = sizeof(Dcb);
  257.  
  258.  /****************************************************************************/
  259.  /* Query DCB.                                                               */
  260.  /****************************************************************************/
  261.  {
  262.   rc = DosDevIOCtl( ComHandle, IOCTL_ASYNC, ASYNC_GETDCBINFO, NULL, 0, NULL,
  263.                     &Dcb, DcbLenInOut, &DcbLenInOut );
  264.  
  265.   if( rc ) return( rc );
  266.  
  267.   /***************************************************************************/
  268.   /* Set read/write timeout values. ( see flags3).                           */
  269.   /***************************************************************************/
  270.   Dcb.WriteTimeOut = WriteTimeOut;      /* write timeout.                    */
  271.   Dcb.ReadTimeOut  = ReadTimeOut;       /* max USHORTx.01 = 10.92 minutes.   */
  272.  
  273.   /***************************************************************************/
  274.   /* Set flags1.                                                             */
  275.   /*                                                                         */
  276.   /* - disable DTR control mode.                                             */
  277.   /* - enable  CTS output handshaking.                                       */
  278.   /* - disable DSR output handshaking.                                       */
  279.   /* - disable DCD output handshaking.                                       */
  280.   /* - disable DSR input sensitivity.                                        */
  281.   /***************************************************************************/
  282.   Dcb.flags1.DTR_Ctrl=0;
  283.   Dcb.flags1.CTS_Enable = 1;
  284.   Dcb.flags1.DSR_Enable = 0;
  285.   Dcb.flags1.DCD_Enable = 0;
  286.   Dcb.flags1.DSR_Sensi  = 0;
  287.  
  288.   /***************************************************************************/
  289.   /* Set flags2.                                                             */
  290.   /*                                                                         */
  291.   /* - disable automatic transmit flow control.                              */
  292.   /* - disable automatic receive flow control.                               */
  293.   /* - disable error replacement character.                                  */
  294.   /* - disable null stripping.                                               */
  295.   /* - disable break replacement character.                                  */
  296.   /* - set automatic receive flow control to normal.( not full duplex )      */
  297.   /* - enable RTS input handshaking.                                         */
  298.   /***************************************************************************/
  299.  
  300.   Dcb.flags2.Tx_Auto_Flow_Enable = 0;
  301.   Dcb.flags2.Rx_Auto_Flow_Enable = 0;
  302.   Dcb.flags2.Error_Replacement   = 0;
  303.   Dcb.flags2.Null_Stripping      = 0;
  304.   Dcb.flags2.Break_Replacement   = 0;
  305.   Dcb.flags2.Rx_Auto_Flow        = 0;
  306.   Dcb.flags2.RTS_Ctrl            = 2;
  307.  
  308.   /***************************************************************************/
  309.   /* Set flags3.                                                             */
  310.   /*                                                                         */
  311.   /* - enable write timeout processing.                                      */
  312.   /* - set Normal read timeout processing.                                   */
  313.   /* - set Extended hardware bufferring.                                     */
  314.   /***************************************************************************/
  315.  
  316.   Dcb.flags3.WriteInfiniteEnable = 0;
  317.   Dcb.flags3.ReadTimeOut = 1;
  318.   Dcb.flags3.ExtHdweBuffering = 0;
  319.  
  320.   DcbLenInOut = sizeof(Dcb);
  321.   rc = DosDevIOCtl(ComHandle, IOCTL_ASYNC, ASYNC_SETDCBINFO, &Dcb, DcbLenInOut,
  322.                    &DcbLenInOut, NULL, 0, NULL );
  323.   return( rc );
  324.  }
  325. }
  326.  
  327. /*****************************************************************************/
  328. /* AsyncSetLineCtrl()                                                        */
  329. /*                                                                           */
  330. /* Description:                                                              */
  331. /*                                                                           */
  332. /*   Set the data bits,parity,and stop bits.                                 */
  333. /*                                                                           */
  334. /* Parameters:                                                               */
  335. /*                                                                           */
  336. /*   none                                                                    */
  337. /*                                                                           */
  338. /* Return:                                                                   */
  339. /*                                                                           */
  340. /*   rc             DosDevIOCtl return code.                                 */
  341. /*                                                                           */
  342. /* Assumptions:                                                              */
  343. /*                                                                           */
  344. /*   none.                                                                   */
  345. /*                                                                           */
  346. /*****************************************************************************/
  347. APIRET AsyncSetLineCtrl()
  348. {
  349.  APIRET   rc=0;
  350.  LINECTRL LineCtrl;
  351.  ULONG    LineCtrlInOut;
  352.  
  353.  /****************************************************************************/
  354.  /* Set 8 data bits, no parity, and 1 stop bit.                              */
  355.  /****************************************************************************/
  356.  LineCtrl.DataBits = 8;
  357.  LineCtrl.Parity   = 0;
  358.  LineCtrl.StopBits = 0;
  359.  
  360.  LineCtrlInOut = sizeof(LineCtrl);
  361.  rc = DosDevIOCtl(ComHandle, IOCTL_ASYNC, ASYNC_SETLINECTRL,
  362.                   &LineCtrl, LineCtrlInOut, &LineCtrlInOut, NULL, 0, NULL );
  363.  return(rc);
  364. }
  365.  
  366. /*****************************************************************************/
  367. /* AsyncSetBitRate()                                                         */
  368. /*                                                                           */
  369. /* Description:                                                              */
  370. /*                                                                           */
  371. /*   Set the bit(baud) rate.                                                 */
  372. /*                                                                           */
  373. /* Parameters:                                                               */
  374. /*                                                                           */
  375. /*   BitRate        set the bit rate of the com port.                        */
  376. /*                                                                           */
  377. /* Return:                                                                   */
  378. /*                                                                           */
  379. /*   rc             DosDevIOCtl return code.                                 */
  380. /*                                                                           */
  381. /* Assumptions:                                                              */
  382. /*                                                                           */
  383. /*   none.                                                                   */
  384. /*                                                                           */
  385. /*****************************************************************************/
  386. APIRET AsyncSetBitRate( int BitRate )
  387. {
  388.  APIRET rc=0;
  389.  ULONG  BitRateLenInOut = sizeof(BitRate);
  390.  
  391.  rc = DosDevIOCtl( ComHandle, IOCTL_ASYNC, ASYNC_SETBAUDRATE,
  392.                    &BitRate, BitRateLenInOut, &BitRateLenInOut, NULL,0,NULL);
  393.  return(rc);
  394. }
  395.  
  396. /*****************************************************************************/
  397. /* AsyncSetClearDTR()                                                        */
  398. /*                                                                           */
  399. /* Description:                                                              */
  400. /*                                                                           */
  401. /*   Set/Clear the DTR line.                                                 */
  402. /*                                                                           */
  403. /* Parameters:                                                               */
  404. /*                                                                           */
  405. /*   TRUE or FALSE  TRUE==>Set FALSE=>Clear.                                 */
  406. /*                                                                           */
  407. /* Return:                                                                   */
  408. /*                                                                           */
  409. /*   rc             DosDevIOCtl return code.                                 */
  410. /*                                                                           */
  411. /* Assumptions:                                                              */
  412. /*                                                                           */
  413. /*   none.                                                                   */
  414. /*                                                                           */
  415. /*****************************************************************************/
  416. APIRET AsyncSetClearDTR( int TorF )
  417. {
  418.  APIRET rc=0;
  419.  MODEMCTRL ModemCtrl;
  420.  ULONG     ModemCtrlLenInOut;
  421.  COMERROR  ComError;
  422.  ULONG     ComErrorInOut;
  423.  
  424.  if( TorF == TRUE )
  425.  {
  426.   ModemCtrl.OnMask = 1;
  427.   ModemCtrl.OffMask = 0xFF;
  428.  }
  429.  else
  430.  {
  431.   ModemCtrl.OnMask = 0;
  432.   ModemCtrl.OffMask = 0xFE;
  433.  }
  434.  
  435.  ModemCtrlLenInOut = sizeof(ModemCtrl);
  436.  
  437.  memset(&ComError,0,sizeof(ComError));
  438.  ComErrorInOut = sizeof(ComError);
  439.  rc = DosDevIOCtl( ComHandle, IOCTL_ASYNC, ASYNC_SETMODEMCTRL,
  440.                    &ModemCtrl, ModemCtrlLenInOut, &ModemCtrlLenInOut,
  441.                    &ComError , ComErrorInOut,     &ComErrorInOut);
  442.  return(rc);
  443. }
  444.  
  445. /*****************************************************************************/
  446. /* AsyncSend()                                                               */
  447. /*                                                                           */
  448. /* Description:                                                              */
  449. /*                                                                           */
  450. /*   Send a buffer to the com port.                                          */
  451. /*                                                                           */
  452. /* Parameters:                                                               */
  453. /*                                                                           */
  454. /*   pBuf       -> our transmit buffer.                                      */
  455. /*   Len        number of bytes to transmit.                                 */
  456. /*                                                                           */
  457. /* Return:                                                                   */
  458. /*                                                                           */
  459. /*   none.      If an error occurs, we're dead,so just exit.                 */
  460. /*                                                                           */
  461. /* Assumptions:                                                              */
  462. /*                                                                           */
  463. /*   none.                                                                   */
  464. /*                                                                           */
  465. /*****************************************************************************/
  466. void AsyncSend( PVOID pBuf, ULONG Len)
  467. {
  468.  ULONG  BytesWritten;
  469.  
  470.  if( DosWrite(ComHandle, pBuf, Len,&BytesWritten) ||
  471.      (BytesWritten != Len) ||
  472.      AsyncCheckComError() )
  473.   {
  474.    printf("\nCommunications send error or write timeout.");
  475.    printf("\nLen=%d",Len);
  476.    {
  477.     UCHAR *cp = (UCHAR*)pBuf;
  478.     int i;
  479.  
  480.     printf("\n");
  481.     for( i=1; i<=Len ; i++, cp++)
  482.     {
  483.      printf("%2x",*cp);
  484.     }
  485.    }
  486.    fflush(0);
  487.    exit(0);
  488.   }
  489. }
  490.  
  491. /*****************************************************************************/
  492. /* AsyncRecv()                                                               */
  493. /*                                                                           */
  494. /* Description:                                                              */
  495. /*                                                                           */
  496. /*   Get a buffer from the com port.                                         */
  497. /*                                                                           */
  498. /* Parameters:                                                               */
  499. /*                                                                           */
  500. /*   pBuf       -> our transmit buffer.                                      */
  501. /*   Len        number of bytes to transmit.                                 */
  502. /*                                                                           */
  503. /* Return:                                                                   */
  504. /*                                                                           */
  505. /*   none.      If an error occurs, we're dead,so just exit.                 */
  506. /*                                                                           */
  507. /* Assumptions:                                                              */
  508. /*                                                                           */
  509. /*   Len >= 0.                                                               */
  510. /*                                                                           */
  511. /*****************************************************************************/
  512. void AsyncRecv( PVOID pBuf, ULONG Len)
  513. {
  514.  ULONG  BytesRead = 0;
  515.  APIRET rc = 0;
  516.  
  517.  /****************************************************************************/
  518.  /* - currently we equate (rc==0 && BytesRead) as a receive timeout.         */
  519.  /****************************************************************************/
  520. again:
  521.  rc = DosRead(ComHandle, pBuf, Len,&BytesRead);
  522.  if( rc==0 && BytesRead!=Len )
  523.   goto again;
  524.  
  525.  if( rc == ERROR_INTERRUPT )
  526.   goto again;
  527.  
  528.  if( rc ||
  529.      (BytesRead != Len) ||
  530.      AsyncCheckComError() )
  531.   {printf("\nCommunications receive error.");
  532.    exit(0);
  533.   }
  534. }
  535.  
  536. /*****************************************************************************/
  537. /* AsyncCheckComError()                                                      */
  538. /*                                                                           */
  539. /* Description:                                                              */
  540. /*                                                                           */
  541. /*   Check the com port to see if an error occurred.                         */
  542. /*                                                                           */
  543. /* Parameters:                                                               */
  544. /*                                                                           */
  545. /*   none                                                                    */
  546. /*                                                                           */
  547. /* Return:                                                                   */
  548. /*                                                                           */
  549. /*   0              no error.                                                */
  550. /*   1              error.                                                   */
  551. /*                                                                           */
  552. /* Assumptions:                                                              */
  553. /*                                                                           */
  554. /*   none.                                                                   */
  555. /*                                                                           */
  556. /*****************************************************************************/
  557. ULONG AsyncCheckComError()
  558. {
  559.  COMERROR ComError;
  560.  ULONG    ComErrorInOut;
  561.  
  562.  memset(&ComError,0,sizeof(ComError));
  563.  ComErrorInOut = sizeof(ComError);
  564.  if(  DosDevIOCtl ( ComHandle, IOCTL_ASYNC, ASYNC_GETCOMMERROR,
  565.                     NULL, 0, NULL, &ComError, ComErrorInOut, &ComErrorInOut) ||
  566.      ComError.RxQueueOverrun   ||
  567.      ComError.RxHdweOverrun    ||
  568.      ComError.HdweParityError  ||
  569.      ComError.HdweFrameError )
  570.  {
  571.   return(1);
  572.  }
  573.  return(0);
  574. }
  575.  
  576. /*****************************************************************************/
  577. /* AsyncFlushBuffers()                                                       */
  578. /*                                                                           */
  579. /* Description:                                                              */
  580. /*                                                                           */
  581. /*   Flush the input and output buffers.                                     */
  582. /*                                                                           */
  583. /* Parameters:                                                               */
  584. /*                                                                           */
  585. /* Return:                                                                   */
  586. /*                                                                           */
  587. /* Assumptions:                                                              */
  588. /*                                                                           */
  589. /*****************************************************************************/
  590. void AsyncFlushBuffers()
  591. {
  592.  UCHAR  InByte;
  593.  UCHAR  OutByte;
  594.  ULONG  InByteLenInOut;
  595.  ULONG  OutByteLenInOut;
  596.  
  597.  InByte = 0;
  598.  InByteLenInOut = sizeof(UCHAR);
  599.  OutByte = 0;
  600.  OutByteLenInOut = sizeof(UCHAR);
  601.  DosDevIOCtl( ComHandle, IOCTL_GENERAL, DEV_FLUSHINPUT,
  602.                &InByte, InByteLenInOut,  &InByteLenInOut,
  603.                &OutByte,OutByteLenInOut, &OutByteLenInOut );
  604.  InByte = 0;
  605.  InByteLenInOut = sizeof(UCHAR);
  606.  OutByte = 0;
  607.  OutByteLenInOut = sizeof(UCHAR);
  608.  DosDevIOCtl( ComHandle, IOCTL_GENERAL, DEV_FLUSHOUTPUT,
  609.                &InByte, InByteLenInOut,  &InByteLenInOut,
  610.                &OutByte,OutByteLenInOut, &OutByteLenInOut );
  611.  return;
  612. }
  613.  
  614. /*****************************************************************************/
  615. /* AsyncPeekRecvBuf()                                                     917*/
  616. /*                                                                           */
  617. /* Description:                                                              */
  618. /*                                                                           */
  619. /*   Peek the receive buffer to see if a message has come in.                */
  620. /*                                                                           */
  621. /* Parameters:                                                               */
  622. /*                                                                           */
  623. /*   pNumChars     ->to receiver of the number of characters.                */
  624. /*                                                                           */
  625. /* Return:                                                                   */
  626. /*                                                                           */
  627. /*   rc             DosDevIOCtl return code.                                 */
  628. /*                                                                           */
  629. /* Assumptions:                                                              */
  630. /*                                                                           */
  631. /*   none.                                                                   */
  632. /*                                                                           */
  633. /*****************************************************************************/
  634. APIRET AsyncPeekRecvBuf(USHORT *pNumChars)
  635. {
  636.  APIRET   rc=0;
  637.  PEEKBUF  PeekBuf;
  638.  ULONG    PeekBufInOut;
  639.  
  640.  PeekBufInOut = sizeof(PeekBuf);
  641.  rc =  DosDevIOCtl ( ComHandle, IOCTL_ASYNC, ASYNC_GETINQUECOUNT,
  642.                      NULL, 0, NULL, &PeekBuf, PeekBufInOut, &PeekBufInOut);
  643.  
  644.  *pNumChars = PeekBuf.NumCharsInBuf;
  645.  return(rc);
  646. }
  647.  
  648. /*****************************************************************************/
  649. /* Connect()                                                                 */
  650. /*                                                                           */
  651. /* Description:                                                              */
  652. /*                                                                           */
  653. /*   - Establish a connection to ESP.                                        */
  654. /*                                                                           */
  655. /* Parameters:                                                               */
  656. /*                                                                           */
  657. /*   fp        file handle of the modem command file.                        */
  658. /*   pConnection   -> to the structure defining the remote connection.       */
  659. /*                                                                           */
  660. /* Return:                                                                   */
  661. /*                                                                           */
  662. /* Assumptions:                                                              */
  663. /*                                                                           */
  664. /*****************************************************************************/
  665. void Connect( FILE *fp, CONNECTION *pConnection )
  666. {
  667.  APIRET rc;
  668.  char   ResponseString[128];
  669.  char   line[256];
  670.  char  *cp;
  671.  ULONG  BytesRead;
  672.  char   ExpectedResponseString[128];
  673.  int    i;
  674.  
  675.  /****************************************************************************/
  676.  /* -Set up a 5  second wait for a read/write to the modem.                  */
  677.  /****************************************************************************/
  678.  AsyncSetDCB(500, 500);
  679.  
  680.  /****************************************************************************/
  681.  /* - Send the AT command to the modem.                                      */
  682.  /****************************************************************************/
  683.  {
  684.   ULONG BytesWritten;
  685.   char  ATstring[4];
  686.   ULONG Len;
  687.  
  688.   strcpy(ATstring,"AT\r");
  689.   Len = strlen(ATstring);
  690.   if( DosWrite(ComHandle, ATstring, Len, &BytesWritten) ||
  691.       (BytesWritten != Len) ||
  692.       AsyncCheckComError()
  693.     )
  694.    SayMsg(ERR_CANT_GET_MODEM_ATTENTION);
  695.  }
  696.  
  697.  /****************************************************************************/
  698.  /* - We should get two response strings.                                    */
  699.  /*    - AT is an echo of the command.                                       */
  700.  /*    - OK is the result code.                                              */
  701.  /****************************************************************************/
  702.  strcpy(ExpectedResponseString,"AT");
  703.  for(i=1;i<=2;i++)
  704.  {
  705.   memset(ResponseString,0,sizeof(ResponseString) );
  706.   cp = ResponseString;
  707.   for(;;cp++)
  708.   {
  709.    rc = DosRead(ComHandle, cp, 1,&BytesRead);
  710.    if( rc==0 && BytesRead!=1 )
  711.     SayMsg(ERR_CANT_GET_MODEM_ATTENTION);
  712.  
  713.    /**************************************************************************/
  714.    /* - test for end of response string.                                     */
  715.    /**************************************************************************/
  716.    if(*cp == '\n')
  717.     break;
  718.   }
  719.  
  720.   *strrchr(ResponseString,'\r') = '\0';
  721.   printf("\n%s",ResponseString);fflush(0);
  722.  
  723.   if( strstr(ResponseString,ExpectedResponseString) == NULL )
  724.     SayMsg(ERR_CANT_GET_MODEM_ATTENTION);
  725.   strcpy(ExpectedResponseString,"OK");
  726.  }
  727.  
  728.  /****************************************************************************/
  729.  /* - At this point, the modem is in command mode.                           */
  730.  /****************************************************************************/
  731.  if( pConnection->DbgOrEsp == _ESP )
  732.  {
  733.   /***************************************************************************/
  734.   /* - Esp will be called from the debugger.                                 */
  735.   /* - Turn on auto-answer and answer after one ring.                        */
  736.   /***************************************************************************/
  737.   SendAT_CommandToModem( "ATS0=1", TRUE );
  738.   ReadAT_CommandResponse();
  739.  }
  740.  else /* debugger end of the connection. */
  741.  {
  742.   char  PhoneNumber[32];
  743.  
  744.   /***************************************************************************/
  745.   /* - Tell the modem to ignore the escape code sequence.                    */
  746.   /* - Tell the modem to wait 60 seconds for a connection.                   */
  747.   /***************************************************************************/
  748.   SendAT_CommandToModem( "ATS2=255", TRUE );
  749.   ReadAT_CommandResponse();
  750.   SendAT_CommandToModem( "ATS7=60", TRUE );
  751.   ReadAT_CommandResponse();
  752.  
  753.   /***************************************************************************/
  754.   /* - Now get the phone number.                                             */
  755.   /* - Since dialing takes a long time we need to tell the com port          */
  756.   /*   not to get excited and return on a read timeout before the modem      */
  757.   /*   has had time to process its own timeout value specified in the        */
  758.   /*   ATS7=... above.                                                       */
  759.   /***************************************************************************/
  760.   AsyncSetDCB(12000, 30000);
  761.  
  762. tryagain:
  763.   memset( PhoneNumber, 0, sizeof(PhoneNumber) );
  764.   strcpy( PhoneNumber, "ATDT");
  765.   printf( "\nEnter the phone number:");fflush(0);
  766.   DosBeep( 1800, 1000 );
  767.   if( gets( PhoneNumber + strlen(PhoneNumber) ) == NULL )
  768.   {
  769.    DosBeep( 1800, 1000 );
  770.    printf("\nPhone number error, try again");fflush(0);
  771.    goto tryagain;
  772.   }
  773.   SendAT_CommandToModem( PhoneNumber, FALSE );
  774.  
  775.   /***************************************************************************/
  776.   /* - Reset the read/write timeout values.                                  */
  777.   /***************************************************************************/
  778.   AsyncSetDCB(500, 500);
  779.  }
  780.  
  781.  /****************************************************************************/
  782.  /* - At this point, the modem is in command mode.                           */
  783.  /* - The standard originate/answer commands have been sent.                 */
  784.  /* - The  the file and process command strings.                             */
  785.  /* - Clock the commands in at 1/2 second intervals.                         */
  786.  /****************************************************************************/
  787.  if( fp != NULL )
  788.  {
  789.   for(;;)
  790.   {
  791.    int   len;
  792.    char *cpx;
  793.  
  794.    fgets(line,256,fp);
  795.    if(feof(fp))
  796.     break;
  797.    else
  798.    {
  799.     *strrchr(line,'\n') = '\r';
  800.     cpx = line;len = 0;
  801.     while(*cpx++ != '\r') len++;
  802.     len++;
  803.     AsyncSend(line,len );
  804.     DosSleep(500);
  805.    }
  806.   }
  807.  }
  808.  
  809.  /****************************************************************************/
  810.  /* - At this point, if we're at the probe end of the connection then        */
  811.  /*   the modem has been placed in auto-answer mode. If we're at the         */
  812.  /*   debugger end of the connection then a phone number has been sent       */
  813.  /*   to the modem and it should be in the process of dialing the probe.     */
  814.  /* - Both the probe and the debugger will get a "CONNECT" message back      */
  815.  /*   from the modem.                                                        */
  816.  /* - Poll the modem until the "CONNECT" message is received.                */
  817.  /****************************************************************************/
  818.  {
  819.   BOOL Connected;
  820.   int  Retries;
  821.  
  822.   AsyncSetDCB(500, 30000);
  823.   printf("\nStarting the connection\n");fflush(0);
  824.   for( Connected = FALSE; Connected == FALSE ; )
  825.   {
  826.    /**************************************************************************/
  827.    /* - Read a response string from the modem.                               */
  828.    /**************************************************************************/
  829.    memset(ResponseString,0,sizeof(ResponseString) );
  830.    cp = ResponseString;
  831.    for( Retries = 80 ; Retries > 0 ; )
  832.    {
  833.     rc = DosRead(ComHandle, cp, 1,&BytesRead);
  834.     if( rc==0 && BytesRead!=1 )
  835.     {
  836.      printf(".");fflush(0);
  837.      Retries--;
  838.      continue;
  839.     }
  840.  
  841.     /**************************************************************************/
  842.     /* - test for end of response string.                                     */
  843.     /**************************************************************************/
  844.     if(*cp == '\n')
  845.      break;
  846.  
  847.     /*************************************************************************/
  848.     /* - Set cp for the next character to be put into the response string.   */
  849.     /*************************************************************************/
  850.     cp++;
  851.    }
  852.  
  853.    /**************************************************************************/
  854.    /* - Test for timeout while trying to read a response string.             */
  855.    /**************************************************************************/
  856.    if( Retries == 0 )
  857.     SayMsg(ERR_CONNECT_TIMEOUT);
  858.  
  859.    /**************************************************************************/
  860.    /* - If we get here, then we have a valid response string.                */
  861.    /* - Show the response string to the user.                                */
  862.    /**************************************************************************/
  863.    *strrchr(ResponseString,'\r') = '\0';
  864.    printf("\n%s",ResponseString);fflush(0);
  865.  
  866.    /***************************************************************************/
  867.    /* - test for a connection.                                                */
  868.    /* - sleep for a second so the user can see the connect message.           */
  869.    /***************************************************************************/
  870.    if( strstr(ResponseString,"CONNECT") != NULL )
  871.    {
  872.     DosSleep(1000);
  873.     Connected = TRUE;
  874.    }
  875.    /***************************************************************************/
  876.    /* - test for no carrier.                                                  */
  877.    /* - exit.                                                                 */
  878.    /***************************************************************************/
  879.    if( (strstr(ResponseString,"NO CARRIER")  != NULL ) ||
  880.        (strstr(ResponseString,"NO DIALTONE") != NULL )
  881.      )
  882.     exit(0);
  883.   }
  884.  }
  885.  
  886.  /****************************************************************************/
  887.  /* - set read timeout back to max.                                          */
  888.  /****************************************************************************/
  889.  AsyncSetDCB(0xFFFF, 30000);
  890. }
  891.  
  892. /*****************************************************************************/
  893. /* AsyncGetComMsg()                                                          */
  894. /*                                                                           */
  895. /* Description:                                                              */
  896. /*                                                                           */
  897. /*   - Get a message fromt the com port.                                     */
  898. /*                                                                           */
  899. /* Parameters:                                                               */
  900. /*                                                                           */
  901. /*   pMsgToGet   -> to the message we want to get.                           */
  902. /*                                                                           */
  903. /*                                                                           */
  904. /* Return:                                                                   */
  905. /*                                                                           */
  906. /*   TRUE or FALSE                                                           */
  907. /*                                                                           */
  908. /* Assumptions:                                                              */
  909. /*                                                                           */
  910. /*****************************************************************************/
  911. APIRET AsyncGetComMsg( char* pMsgToGet )
  912. {
  913.  APIRET rc;
  914.  char   c;
  915.  char  *cp = pMsgToGet;
  916.  int    n;
  917.  ULONG  BytesRead=0;
  918.  
  919.  
  920.  AsyncSetDCB(6000, 30000);
  921.  printf("\n");
  922.  for(;;)
  923.  {
  924.   /***************************************************************************/
  925.   /* - read a character or timeout.                                          */
  926.   /***************************************************************************/
  927.   rc = DosRead(ComHandle, &c, 1,&BytesRead);
  928.   if( rc==0 && BytesRead!=1 )
  929.    return(FALSE);
  930.   printf("%c",c);
  931.   fflush(0);
  932.  
  933.   /***************************************************************************/
  934.   /* - test for character match to first message character.                  */
  935.   /* - read characters until a string match occurs or a timeout.             */
  936.   /***************************************************************************/
  937.   if( *cp == c )
  938.    for(n = strlen(cp)-1,cp++; n > 0; n--)
  939.    {
  940.     rc = DosRead(ComHandle, &c, 1,&BytesRead);
  941.     if( rc==0 && BytesRead!=1 )
  942.      return(FALSE);
  943.     printf("%c",c);
  944.     fflush(0);
  945.     if(*cp++ != c )
  946.      {cp = pMsgToGet;break;}
  947.    }
  948.    /**************************************************************************/
  949.    /* - n will be 0 if we got the message.                                   */
  950.    /**************************************************************************/
  951.    if( n == 0 )
  952.     break;
  953.  }
  954.  /****************************************************************************/
  955.  /* - set read timeout back to max.                                          */
  956.  /****************************************************************************/
  957.  AsyncSetDCB(0xFFFF, 30000);
  958.  return(TRUE);
  959. }
  960.  
  961. /*****************************************************************************/
  962. /* FlushModem()                                                              */
  963. /*                                                                           */
  964. /* Description:                                                              */
  965. /*                                                                           */
  966. /*   - Read bytes from the modem until a timeout.                            */
  967. /*                                                                           */
  968. /* Parameters:                                                               */
  969. /*                                                                           */
  970. /* Return:                                                                   */
  971. /*                                                                           */
  972. /* Assumptions:                                                              */
  973. /*                                                                           */
  974. /*****************************************************************************/
  975. void AsyncFlushModem( void )
  976. {
  977.  APIRET rc;
  978.  char   c;
  979.  ULONG  BytesRead=0;
  980.  
  981.  AsyncSetDCB(100, 30000);
  982.  for(;;)
  983.  {
  984.   rc = DosRead(ComHandle, &c, 1,&BytesRead);
  985.   if( rc==0 && BytesRead!=1 )
  986.    break;
  987.   printf("%c",c);
  988.   fflush(0);
  989.  }
  990.  /****************************************************************************/
  991.  /* - set read timeout back to max.                                          */
  992.  /****************************************************************************/
  993.  AsyncSetDCB(0xFFFF, 30000);
  994. }
  995.  
  996. /*****************************************************************************/
  997. /* This function checks the com port to see if there are any messages        */
  998. /* waiting to be read.                                                       */
  999. /*****************************************************************************/
  1000. int AsyncPeekComPort( void )
  1001. {
  1002.  APIRET rc;
  1003.  USHORT NumChars;
  1004.  
  1005.  rc = FALSE;
  1006.  if( ( AsyncPeekRecvBuf(&NumChars)==0 ) &&
  1007.      ( NumChars != 0 )
  1008.    )
  1009.   rc = TRUE;
  1010.  return(rc);
  1011. }
  1012.  
  1013. /*****************************************************************************/
  1014. /* - Send an AT command string to the modem and read the echo of the         */
  1015. /*   the command.                                                            */
  1016. /*****************************************************************************/
  1017. void SendAT_CommandToModem( char* pszCommand, BOOL WaitForEcho )
  1018. {
  1019.  char   Command[256];
  1020.  char   CommandLength;
  1021.  
  1022.  /****************************************************************************/
  1023.  /* - Send the command.                                                      */
  1024.  /****************************************************************************/
  1025.  CommandLength = strlen(pszCommand);
  1026.  
  1027.  memcpy(Command, pszCommand, CommandLength);
  1028.  Command[CommandLength] =  '\r';
  1029.  CommandLength += 1;
  1030.  AsyncSend(Command, CommandLength);
  1031.  
  1032.  /****************************************************************************/
  1033.  /* - Read the echo.                                                         */
  1034.  /* - Wait at most 5 seconds for any character of the command.               */
  1035.  /****************************************************************************/
  1036.  if( WaitForEcho == TRUE )
  1037.  {
  1038.   char    EchoString[256];
  1039.   char   *cp;
  1040.   APIRET  rc;
  1041.   ULONG   BytesRead = 0;
  1042.  
  1043.   memset(EchoString, 0, sizeof(EchoString) );
  1044.  
  1045.   for( cp = EchoString; ;cp++ )
  1046.   {
  1047.    rc = DosRead(ComHandle, cp, 1,&BytesRead);
  1048.    if( rc==0 && BytesRead!=1 )
  1049.     SayMsg(ERR_AT_COMMAND);
  1050.  
  1051.    /**************************************************************************/
  1052.    /* - test for end of echo string.                                         */
  1053.    /**************************************************************************/
  1054.    if(*cp == '\n')
  1055.     break;
  1056.   }
  1057.  
  1058.   *strrchr(EchoString,'\r') = '\0';
  1059.   printf("\n%s", EchoString);fflush(0);
  1060.  }
  1061. }
  1062.  
  1063.  
  1064. /*****************************************************************************/
  1065. /* - Read the modem response to an AT command.                               */
  1066. /*****************************************************************************/
  1067. void ReadAT_CommandResponse( void )
  1068. {
  1069.  char    AT_ResponseString[256];
  1070.  char   *cp;
  1071.  APIRET  rc;
  1072.  ULONG   BytesRead = 0;
  1073.  BOOL    Done;
  1074.  
  1075.  for( Done = FALSE; Done == FALSE; )
  1076.  {
  1077.  
  1078.   memset(AT_ResponseString, 0, sizeof(AT_ResponseString) );
  1079.  
  1080.   for( cp = AT_ResponseString; ;cp++ )
  1081.   {
  1082.    rc = DosRead(ComHandle, cp, 1,&BytesRead);
  1083.    if( rc==0 && BytesRead!=1 )
  1084.     SayMsg(ERR_AT_COMMAND);
  1085.  
  1086.    /**************************************************************************/
  1087.    /* - test for end of echo string.                                         */
  1088.    /**************************************************************************/
  1089.    if(*cp == '\n')
  1090.     break;
  1091.   }
  1092.  
  1093.   /***************************************************************************/
  1094.   /* - Print the echo string.                                                */
  1095.   /***************************************************************************/
  1096.   *strrchr(AT_ResponseString,'\r') = '\0';
  1097.   printf("\n%s", AT_ResponseString);fflush(0);
  1098.  
  1099.   /***************************************************************************/
  1100.   /* - If it's the OK string, then we're done.                               */
  1101.   /***************************************************************************/
  1102.   if( strcmp(AT_ResponseString, "OK") == 0 )
  1103.     Done = TRUE;
  1104.  }
  1105. }
  1106.