Metropoli BBS
VIEWER: zyl2.c MODE: TEXT (CP437)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>

#include "typedef.h"
#include "zyl1.h"
#include "zyascii.h"
#include "zyl2.h"
#include "zyvar2.h"

UINT ZyReadMemoryBlock(int, LPSTR, UINT);
int stoprecord(int);
NPSTR strSubMatch( NPSTR, NPSTR);
int FAR ZyGenerateDtmf(int, int );

char szVFHeader[16] = { 'Z', 'y', 'X', 'E', 'L', 0x02, 0x00, 0x00,
			0x0, 0x0, 0x0, 0x0, 0x0, 0x00, 0x00, 0x00};


/*
────────────────────────────────────────────────────────────────────────────
int ZyOpenCommPort()

function:
     Open a communication port and initialize the comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LONG lSpeed - indicates the communicate baud rate
     char cParity - 'O', 'E', 'N', 'M', 'S' to determine the
		    parity bit
     int nWordLen - Word length for communications
     int nStopBits - Stop bit for communications

return code:
     return code >= 0 - the communication port number
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyOpenCommPort(int nComId, LONG lSpeed, char cParity,
		   int nWordLen, int nStopBits)
{
    /* local variable */



    if (COM1 > zyiopen(nComId, lSpeed, cParity,
		       nStopBits, nWordLen))
	return (-1);

    /* start to RTS CTS flow control as default setting */

    zyiflow(nComId, ON);

    /* clear buffer */

    zyiclear(nComId, ASINOUT);

    /* install the new ctrl-break ISR */

    breakInstall();
    breakReset();

    return (nComId);
}


/*
────────────────────────────────────────────────────────────────────────────
int ZySetCommPortParameter()

function:
     Open a communication port and initialize the comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LONG lSpeed - indicates the communicate baud rate
     char cParity - 'O', 'E', 'N', 'M', 'S' to determine the
		    parity bit
     int nWordLen - Word length for communications
     int nStopBits - Stop bit for communications

return code:
     return code = 0 - successful

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZySetCommPortParameter(int nComId, LONG lSpeed, char cParity,
			   int nWordLen, int nStopBits)
{
    /* local variable */

    zyiinit( nComId, lSpeed,
	     cParity, nStopBits, nWordLen);

    return (0);
}



/*
────────────────────────────────────────────────────────────────────────────
int ZyCloseCommPort()

function:
     Close a communication port

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code = 0 - successful
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyCloseCommPort(int nComId)
{
    int nResult;

    /* close the port */

    zydtr(nComId, OFF); 	/* clear the DTR signal */

    nResult = zyiquit(nComId);

    breakUnInstall();

    return (nResult);
}

/*
────────────────────────────────────────────────────────────────────────────
int ZyCheckState()

function:
     check state of comm port by index

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nIndex - the index to define check state
		     1:  Check Alert flag isalert()
		     2:  Is RX-Buffer empty
		     3:  Is RX-Buffer full?
		     4:  Is RX-Buffer overflow?
		     5:  Is TX-Buffer empty?
		     6:  Is TX-Buffer full?
		     7:  Is there a line error?
		     8:  Is there a modem error
		     9:  Are TX-Interrupts running
		     10:  Are RX-Interrupts running
		     11:  Is alert being ignored AlertFlagStopsRXAndTX()
		     12:  Is CTS being ignored CTSLowHoldsTXInterrupts()
		     13:  Is DSR being ignored DSRLowDiscardsRXData()
		     14:  Is CD being ignored CDLowDiscardsRXData()
		     15:  Are modem status errors being
		     16:  Are receiver errors being ignored?
		     17:  Has CTS gone low this block ?
		     18:  Has DSR gone low this block
		     19:  Has CD gone low this block
		     20:  Has Ring indicator been asserted
		     21:  is receiver count > 0
		     22:  isxoffblocked(p)
		     23:  isctsblocked(p)
		     24:  is16550(p)

return code:
     return code TRUE / FALSE - successful
     return code <0 - the error code

note : not support
────────────────────────────────────────────────────────────────────────────
*/

/*
────────────────────────────────────────────────────────────────────────────
int ZyCheckCommState()

function:
     check state of comm port by index

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nIndex - the index to define check state
		  1: Has a receiver overrun error occurred
		  2: Has a parity error occurred
		  3: Has a framing error occurred
		  4: Has a break signal been received
		  5: Return the state of CTS
		  6: Return the state of DSR
		  7: Return the state of Carrier Detect
		  8: Return the state of Ring Indicator
		  9: Has there been a change in CTS
		  10: Has there been a change in DSR
		  11: Has there been a change in CD
		  12: Has there been a change in RI

return code:
     return code TRUE / FALSE - successful
     return code <0 - the error code

note not support
────────────────────────────────────────────────────────────────────────────
*/

/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteChar()

function:
     Write a character to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int c	- the char to be writted

return code:
     return code >= 0 - successful
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteChar(int nComId, int nChar)
{
    /* write char to the port */

    return(zyiputc(nComId, nChar));
}


/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteCharAbs()

function:
     Write a character to comm port absolute

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int c	- the char to be writted

return code:
     return code = 0 - successful

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteCharAbs(int nComId, int nChar)
{

    /* write char to the port */

    return(zyiputcabs( nComId, nChar));
}

/*
────────────────────────────────────────────────────────────────────────────
int ZyReadChar()

function:
     Read a character from comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code >= 0 - char to be read from comm port
     return code = IDR_BUFREMPTY - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyReadChar(int nComId)
{
    /* read a char from the port */

    return(zyigetc(nComId));
}


/*
────────────────────────────────────────────────────────────────────────────
int ZyReadCharAbs()

function:
     Read a character from comm port absolute

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code : data read from comm port

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyReadCharAbs(int nComId)
{
    int nCh;

    /* read a char from the port */

    nCh = -1;
    while(IDR_SUCCESS > nCh)
       nCh = zyigetc(nComId);

    return (nCh);
}

/*
────────────────────────────────────────────────────────────────────────────
int ZyPeekChar()

function:
     Read a character from comm port absolute

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code >= 0 - successful
     return code = IDR_BUFREMPTY - the error code

────────────────────────────────────────────────────────────────────────────
*/


int FAR ZyPeekChar(int nComId)
{

    return( zyipeek(nComId));

}

/*
────────────────────────────────────────────────────────────────────────────
int ZyHardWareFlowControl()

function:
     setup comm port to hardware flow control

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nCtrl - ON : to setup
		 OFF: to reset

return code:
     return code >= 0 - successful
     return code < 0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyHardWareFlowControl(int nComId, int nCtrl)
{

    return(zyiflow(nComId, nCtrl));	 /* zyc100 */

}


/*
────────────────────────────────────────────────────────────────────────────
int ZyXonXoffFlowControl()

function:
     setup comm port to XON XOFF flow control

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nCtrl - ON : to setup
		 OFF: to reset

return code:
     return code >= 0 - successful
     return code < 0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyXonXoffFlowControl(int nComId, int nCtrl)
{

    if (nCtrl == ON)
       return (zyiusexonxoff(nComId, ASINOUT));   /* zyc100 */
    else
       return (zyiusexonxoff(nComId, ASNOFL));	/* zyc100 */

}




/*
────────────────────────────────────────────────────────────────────────────
int initZyModem()

function:
     initialize modem

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nFlctrl - FC_HARDWARE : to setup modem flow control to hardware
		   FC_XONXOFF  : to setup modem flow control to xon xoff
		   FC_NONE     : no flow control

return code:
     return code >= 0 - successful
     return code < 0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR initZyModem(int nComId, int nFlctrl)
{

  bEchoOn = TRUE;
  bVerbose = TRUE;
  bEnResponse = TRUE;
  nDialMethod = DM_TONE;
  ZyWriteCommand(nComId, "AT&FB0E1L4M1Q0V1\0", ON);
  ZySetUpFlowControl(nComId, nFlctrl);

  return (TRUE);
}



/*
────────────────────────────────────────────────────────────────────────────
int ZyResetModem()

function:
     Reset Modem By Command "ATZ"

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code = 0 - successful
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyResetModem(int nComId)
{
    /* check the commport Id */

    if ( nComId != nComId) return (IDR_NOTSETUP);

    /* write char to the port */

    return(ZyWriteCommand(nComId, "ATZ\0", ON));
}




/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteCommand()

function:
     Write a command to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpCommand - the command to be writted
		       the command does not contain the "\r"
     BOOL bWaitFlag - the response to be wait ON for waiting
		      and OFF for not waiting

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteCommand(int nComId, LPSTR lpCommand, BOOL bWaitFlag)
{
  /* local variable */

  int i;

  /* check the commport Id */

  strcpy ((LPSTR) szTempBuffer, lpCommand);
  strcat (szTempBuffer, "\r\0");

  i = 0;
  while ( szTempBuffer[i] != '\0')
  {
     ZyWriteCharAbs(nComId, szTempBuffer[i]);	   /* read echo command */
     if (bEchoOn)
	ZyReadCharAbs(nComId);
     i++;
  }


  /* chaek bEcho & bEnResponse */


  if (NULL != (strSubMatch(szTempBuffer, "&F")))
  {
     bEchoOn = TRUE;
     bEnResponse = TRUE;
  }
  if (NULL != (strSubMatch(szTempBuffer, "Q1")))
     bEchoOn = FALSE;
  if (NULL != (strSubMatch(szTempBuffer, "E0")))
     bEnResponse = FALSE;


  if (!bEnResponse || (bWaitFlag == OFF))
     return (IDR_SUCCESS);

  /* read response first '\r\n' */


  while (0x0A != (char) ZyReadCharAbs(nComId));

  i = 0;

  for (;;)
  {
     szTempBuffer[i] = (char) ZyReadCharAbs(nComId);
     if (0x0A == (int) szTempBuffer[i])
	break;
  }

  szTempBuffer[i+1] = '\0';

  delay(10);

  if (bVerbose)
  {
     return (FetchRespCode((LPSTR) szTempBuffer));
  }
  else
  {
     if ('\r' == (i = szTempBuffer[1]))
	return (i-'0');
     else
	return ((szTempBuffer[0]-'0') * 10 + (szTempBuffer[1] - '0'));
  }
}



/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteByteCommand()

function:
     Write a command to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpCommand - the command to be writted
     BOOL bWaitFlag - the response to be wait ON for waiting
		      and OFF for not waiting

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteByteCommand(int nComId, LPSTR lpCommand, BOOL bWaitFlag)
{
  /* local variable */

  int i;

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  strcpy ((LPSTR)szTempBuffer, lpCommand);

  i = 0;
  while ( szTempBuffer[i] != '\0')
  {
     ZyWriteCharAbs(nComId, szTempBuffer[i]);
     i++;
  }

  if (!bEnResponse || (bWaitFlag == OFF))
     return (IDR_SUCCESS);

  /* read response first '\r\n' */


  while (0x0A != ZyReadCharAbs(nComId));

  i=0;

  for ( ; ; )
  {
     if (0x0A == (szTempBuffer[i] = (char) ZyReadCharAbs(nComId)))
	 break;
     i++;
  }
  szTempBuffer[i+1] = '\0';

  delay(10);

  if (bVerbose)
     return (FetchRespCode((LPSTR) szTempBuffer));

  else
  {
     if (0x0D == (i = szTempBuffer[1]))
	return (i-'0');
     else
	return ((szTempBuffer[0]-'0') * 10 + (szTempBuffer[1] - '0'));
  }
}


/*
────────────────────────────────────────────────────────────────────────────
int FetchRespCode()

function:
     get the response code by response string

parameters:
     LPSTR lpResponse - response from modem but without first two byte
			'\r\n'

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/


int FAR FetchRespCode(LPSTR cResponse)

{
  int i;
  BOOL bFind;

  if (cResponse == NULL )
     return (IDR_INVBUFSIZE);

  i = 0;
  bFind = FALSE;
  while ( (i < MAX_RESPONSE_NUMBER) && !bFind)
  {
    if ( 0 == ( strcmp( (LPSTR)cResponse, ResponseTbl[i])))
       bFind = TRUE;
    i++;
  }
  if (bFind)
    return  (i-1);
  else
    return (IDR_GENERALERROR);
}





/*
────────────────────────────────────────────────────────────────────────────
NPSTR strSubMatch()

function:
     find the sub string in the main string

parameters:

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/


NPSTR strSubMatch( NPSTR cMainStr, NPSTR cSubStr)
{
  NPSTR cTempStr;
  int nSubStrLen;

  cTempStr = cMainStr;
  if ((nSubStrLen = (int) strlen (cSubStr)) > (int) strlen( cMainStr))
     return NULL;

  while (( cTempStr = strchr ( cTempStr, (int) *cSubStr)) != NULL)
  {
     if (strncmp( cTempStr, cSubStr, nSubStrLen) == 0)
     return cTempStr;

     cTempStr++;
  }

  return NULL;
}



/*
────────────────────────────────────────────────────────────────────────────
int ZySetUpFlowControl()

function:
     Set up flow control with buffer and modem

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nFlctrl - the flow control method : FC_XONXOFF, FC_HARDWARE, and
					     FC_NONE

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZySetUpFlowControl(int nComId, int nFlctrl)
{

  /* local variable */

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  nFCMethod= nFlctrl;

  switch(nFlctrl)
  {
     case FC_XONXOFF:
	  ZyWriteCommand(nComId, "AT&H4\0", ON);
	  ZyHardWareFlowControl(nComId,OFF);
	  ZyXonXoffFlowControl(nComId, ON);
	  break;
     case FC_HARDWARE:
	  ZyXonXoffFlowControl(nComId, OFF);
	  ZyHardWareFlowControl(nComId,ON);
	  ZyWriteCommand(nComId, "AT&H3\0", ON);
	  break;
     case FC_NONE:
	  ZyXonXoffFlowControl(nComId, OFF);
	  ZyHardWareFlowControl(nComId,OFF);
	  ZyWriteCommand(nComId, "AT&H0\0", ON);
	  break;
     default:
	  return (IDR_INVPAR);

  }
  return (IDR_SUCCESS);

}


/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteMemoryBlockWithDtmf()

function:
     Write a Block to comm port with detect DTMF

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     NPSTR npMBlock - the pointer of memory block
     UINT uSize - the size of memory block
     int nFlctrl - the flow control method : FC_XONXOFF, FC_HARDWARE, and
					     FC_NONE

return code:
     return code >= 0 - the write byte number
     return code <0 - the error code or DTMF code defined in zyl2.h

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteMemoryBlockWithDtmf(int nComId, LPSTR lpMBpt, UINT uSize)
{
  /* local variable */

  UINT i;
  int c;

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  i = 0;
  while ( i < uSize)
  {
     c = ZyDetectDTMF(nComId);
     if (c <= DTMFs ) return (c);  /* DTMF had been detected */

     ZyWriteCharAbs(nComId, *(lpMBpt+i));
     if ( *(lpMBpt+i) == DLE)
	ZyWriteCharAbs(nComId, *(lpMBpt+i));
     i++;
  }

  return ((int) i);
}


/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteMemoryBlock()

function:
     Write a Block to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     NPSTR npMBlock - the pointer of memory block
     UINT uSize - the size of memory block
     int nFlctrl - the flow control method : FC_XONXOFF, FC_HARDWARE, and
					     FC_NONE

return code:
     return code >= 0 - the response code
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteMemoryBlock(int nComId, LPSTR lpMBpt, UINT uSize)
{
  /* local variable */

  UINT i;

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  i = 0;
  while ( i < uSize)
  {
     ZyWriteCharAbs(nComId, *(lpMBpt+i));
     if ( *(lpMBpt+i) == DLE)
	ZyWriteCharAbs(nComId, *(lpMBpt+i));
     i++;
  }

  return ((int) i);
}

/*
────────────────────────────────────────────────────────────────────────────
long ZyWriteFile()

function:
     Write a File to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpFile - the pointer of File name
     int nFlctrl - the flow control method : FC_XONXOFF, FC_HARDWARE, and
					     FC_NONE

return code:
     return code >= 0 - the byte of read
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

long FAR ZyWriteFile(int nComId, LPSTR lpFile)
{

  /* local variable */

  FILE *fp;
  long lFileLen;
  int nReadNum;
  int nBufSize;

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  nBufSize = 256;
  strcpy((LPSTR)szTempBuffer, lpFile);

  if ((fp = fopen((NPSTR)szTempBuffer, "r+b")) == NULL)
     return (IDR_GENERALERROR);


  fread((NPSTR) szTempBuffer, sizeof (char), 16, fp);	/* read header */

  /* chech header of ZyXEL format */

  if (NULL == strSubMatch((NPSTR) szTempBuffer,"ZyXEL"))
     return (IDR_GENERALERROR);


  lFileLen = 0;

  while ((0 < (nReadNum = fread((NPSTR) szTempBuffer,
	  sizeof(char), nBufSize, fp))) ||
	 !(feof(fp)))
  {

     if (breakPressed())
	 break;
     nReadNum = ZyWriteMemoryBlock(nComId, (LPSTR) szTempBuffer,
				   (UINT) nBufSize);
     lFileLen = lFileLen + (long) nReadNum;
  }
/*
//while (!istxempty(nComId));
*/
  fclose(fp);
  return (lFileLen);
}


/*
────────────────────────────────────────────────────────────────────────────
long ZyWriteFileWithDtmf()

function:
     Write a File to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpFile - the pointer of File name
     int nSizeofDtmfBuf - identifies the DTMF buffer size
     LPSTR lpDtmfBuf - a buffer save the DTMF value

return code:
     return code >= 0 - the byte of read
			lpDtmfBuf = Dtmf string
     return code <0 - the error code or DTMF detect

────────────────────────────────────────────────────────────────────────────
*/

long FAR ZyWriteFileWithDtmf(int nComId, LPSTR lpFile, int nSizeofDtmfBuf,
			     LPSTR lpDtmfBuf)
{

  /* local variable */

  FILE *fp;
  long lFileLen;
  int nReadNum;
  int nBufSize;
  int nTempDtmfSize;

  /* check the commport Id */

  if ( nComId != nComId) return (IDR_NOTSETUP);

  nBufSize = 256;
  strcpy((LPSTR)szTempBuffer, lpFile);

  if ((fp = fopen((NPSTR)szTempBuffer, "r+b")) == NULL)
     return (IDR_GENERALERROR);


  fread((NPSTR) szTempBuffer, sizeof (char), 16, fp);	/* read header */

  /* chech header of ZyXEL format */

  if (NULL == strSubMatch((NPSTR) szTempBuffer,"ZyXEL"))
     return (IDR_GENERALERROR);


  lFileLen = 0;
  nTempDtmfSize = 0;

  while ((0 < (nReadNum = fread((NPSTR) szTempBuffer,
	  sizeof(char), nBufSize, fp))) ||
	 !(feof(fp)))
  {

     if (breakPressed())
	 break;
     nReadNum = ZyWriteMemoryBlockWithDtmf(nComId, (LPSTR) szTempBuffer,
				   (UINT) nBufSize);
     switch(nReadNum)
     {
	case DTMF0:
	case DTMF1:
	case DTMF2:
	case DTMF3:
	case DTMF4:
	case DTMF5:
	case DTMF6:
	case DTMF7:
	case DTMF8:
	case DTMF9:
	     lpDtmfBuf[nTempDtmfSize] = nReadNum + '0' - DTMF0;
	     break;
	case DTMFnum:
	     lpDtmfBuf[nTempDtmfSize] = '#';
	     break;
	case DTMFstar:
	     lpDtmfBuf[nTempDtmfSize] = '*';
	     break;
	case DTMFs:
	     lpDtmfBuf[nTempDtmfSize] = 's';
	     break;
	case DTMFb:
	     lpDtmfBuf[nTempDtmfSize] = 'b';
	     break;
	case DTMFq:
	     lpDtmfBuf[nTempDtmfSize] = 'q';
	     break;
	case DTMFc:
	     lpDtmfBuf[nTempDtmfSize] = 'c';
	     break;
	default:
	     lFileLen = lFileLen + (nReadNum);
	     break;
     }
     if (nReadNum <= DTMFstar)
     {
	lFileLen = lFileLen + (long) nBufSize;
	nTempDtmfSize ++;
     }
     else if (nReadNum <= DTMFs)
     {
	lFileLen = lFileLen + (long) nBufSize;
	return (lFileLen);
     }
     if (nTempDtmfSize >= nSizeofDtmfBuf)
	break;
  }
/*
//while (!istxempty(nComId));
*/
  fclose(fp);
  return (lFileLen);
}









int FAR startVoicePlay(int nComId, int nMode, int nDest)

{

  switch(nMode)
  {
     case IDV_CELP:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     case IDV_ADPCM2:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     case IDV_ADPCM3:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     default:
	  return (IDR_GENERALERROR);

  }


  ZyWriteCommand(nComId,"AT&F+FCLASS=8\0",ON);

  /* start XON XOFF flow control */

  zyiflow(nComId,OFF);
  zyiusexonxoff(nComId, ASOUT);
  nFCMethod = FC_XONXOFF;

  switch(nMode)
  {
     case IDV_CELP:
	  ZyWriteCommand(nComId, "AT+VSM=1\0", ON);
	  break;
     case IDV_ADPCM2:
	  ZyWriteCommand(nComId, "AT+VSM=2\0", ON);
	  break;
     case IDV_ADPCM3:
	  ZyWriteCommand(nComId, "AT+VSM=3\0", ON);
	  break;
     default:
	  ZyWriteCommand(nComId, "AT+FCLASS=0\0", ON);
	  return(IDR_INVPAR);

  }

  switch(nDest)
  {
     case IDV_SPEAKER:
	  ZyWriteCommand(nComId, "AT+VLS=16\0", ON);
	  break;
     case IDV_LINE:
	  ZyWriteCommand(nComId, "AT+VLS=2\0", ON);
	  break;
     default:
	  ZyWriteCommand(nComId, "AT+FCLASS=0\0", ON);
	  return(IDR_INVPAR);

  }

  ZyWriteCommand(nComId, "AT+VTX\0", ON);
  return (IDR_SUCCESS);

}





/*
────────────────────────────────────────────────────────────────────────────
int ZyWriteVoiceFile()

function:
     Write a File to comm port
     if DTMF detect the function will return the dtmf value

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpFile - the pointer of File name
     int nMode - the compression mode IDV_CELP, IDV_ADPCM2, IDV_ADPCM3
     int nDest - the destination IDV_SPAEKER, IDV_LINE

return code:
     return code >= 0 - success
     return code <0 - the error code or DTMF detect

────────────────────────────────────────────────────────────────────────────
*/

int FAR ZyWriteVoiceFile(int nComId, LPSTR lpFile,
			 int nMode, int nDest)

{

  /* local variable */

  long lResult;
  char szDtmf;

  /* startVoicePlay () */

  startVoicePlay(nComId, nMode, nDest);

  /* <DATA>, <DLE><ETX> to comm port */

  lResult = ZyWriteFileWithDtmf(nComId, lpFile, 1, &szDtmf);
  ZyWriteByteCommand(nComId, EndVoiceCmd, ON);

  /* disconnect line or speaker */

  disconnectVoice(nComId);

  if (lResult >= IDR_SUCCESS)
     return (IDR_SUCCESS);
  return (IDR_GENERALERROR);
}




/*
────────────────────────────────────────────────────────────────────────────
int stoprecord()

function:
     stop the record procedure

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code >= 0 - success
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int stoprecord(int nComId)

{
    int i;
    char szTempResp[7];

	/* stop recording */

    ZyWriteCharAbs(nComId, 'A');
    ZyWriteCharAbs(nComId, 'T');
    ZyWriteCharAbs(nComId, '\r');

    /* check out left data and wait <DLE> <ETX> VCON */

    for( ; ; )
    {
      if (DLE == ZyReadCharAbs(nComId)) /* check <DLE> */
      {
	switch(ZyReadCharAbs(nComId))
	{
	   case ETX:
		/* ignor response 0x0d, 0x0a */
		while (0x0a != ZyReadCharAbs(nComId));
		/* read VCON */
		i = 0;
		while (i<6)
		{
		  szTempResp[i] = ZyReadCharAbs(nComId);
		  i++;
		}
		szTempResp[6] = 0;
		delay(100);
		return(FetchRespCode((LPSTR)szTempResp));

	    default:	 /* ignor left data */
		break;
	}
      }
    }
}





/*
────────────────────────────────────────────────────────────────────────────
UINT ZyReadMemoryBlock()

function:
     read a Block to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpMBlock - the pointer of memory block
     UINT uSize - the size of memory block

return code:
     return code >= 0 - the real read byte number
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

UINT FAR ZyReadMemoryBlock(int nComId, LPSTR lpMBpt,
			    UINT uSize)
{
  /* local variable */

  UINT i;

  i = 0;
  while ( i < uSize)
  {
     if (breakPressed())
     {
	/* stop recording */
	if ( stoprecord(nComId) != FetchRespCode("VCON\r\n"))
	   return (IDR_GENERALERROR);
	return (i);
     }

     (char) lpMBpt[i] = (char) zyigetc(nComId);
     if ( *(lpMBpt + i) == DLE)
     {
	switch((char) lpMBpt[i] = (char) zyigetc(nComId))
	{
	   case 'c':
	   case 'b':
	   case 'q':
	   case 's':
		i--;
		if ( stoprecord(nComId) != FetchRespCode("VCON\r\n"))
		   return (IDR_GENERALERROR);
		return(i);
	   case DLE:
	   default:
		break;
	}
     }
     i++;
  }
  return (i);
}






/*
────────────────────────────────────────────────────────────────────────────
int startVoiceRecord()

function:
     start the record procedure

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code >= 0 - success
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/

int FAR startVoiceRecord(int nComId, int nMode, int nDest)
{


  switch(nMode)
  {
     case IDV_CELP:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     case IDV_ADPCM2:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     case IDV_ADPCM3:
	  ZySetCommPortParameter(nComId, 38400L, 'N', 8, 1);
	  break;
     default:
	  return (IDR_GENERALERROR);

  }


  ZyWriteCommand(nComId,"AT&F+FCLASS=8\0",ON);

  /* start XON XOFF flow control */

  zyiflow(nComId,OFF);
  zyiusexonxoff(nComId, ASIN);
  nFCMethod = FC_XONXOFF;

  switch(nMode)
  {
     case IDV_CELP:
	  ZyWriteCommand(nComId, "AT+VSM=1\0", ON);
	  break;
     case IDV_ADPCM2:
	  ZyWriteCommand(nComId, "AT+VSM=2\0", ON);
	  break;
     case IDV_ADPCM3:
	  ZyWriteCommand(nComId, "AT+VSM=3\0", ON);
	  break;
     default:
	  ZyWriteCommand(nComId, "AT+FCLASS=0\0", ON);
	  return(IDR_INVPAR);

  }

  switch(nDest)
  {
     case IDV_SPEAKER:
	  ZyWriteCommand(nComId, "AT+VLS=8\0", ON);
	  break;
     case IDV_LINE:
	  ZyWriteCommand(nComId, "AT+VLS=2\0", ON);
	  break;
     default:
	  ZyWriteCommand(nComId, "AT+FCLASS=0\0", ON);
	  zyiusexonxoff(nComId, ASNOFL);
	  zyiflow(nComId,ON);
	  nFCMethod = FC_HARDWARE;
	  return(IDR_INVPAR);

  }
  return (0);
}






/*
────────────────────────────────────────────────────────────────────────────
int ZyRecordVoiceFile()

function:
     Record a File to comm port

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpFile - the pointer of File name
     int nMode - the compression mode IDV_CELP, IDV_ADPCM
     int nDest - recording from IDV_SPAEKER, IDV_LINE

return code:
     return code >= 0 - success
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/


int FAR ZyRecordVoiceFile(int nComId, LPSTR lpFile, int nMode, int nDest)
{

  /* local variable */

  int nReadNum;
  int nBufSize;
  FILE *fp;

  strcpy((LPSTR)szTempBuffer, lpFile);

  if ((fp = fopen((NPSTR)szTempBuffer, "w+b")) == NULL)
     return (IDR_GENERALERROR);

  fwrite((NPSTR) szVFHeader, sizeof (char), 16, fp);   /* write header */

  /* startVoiceRecord() */

  startVoiceRecord(nComId, nMode, nDest);

  ZyWriteCommand(nComId, "AT+VTS=[933,0,10]\0", ON);

  ZyWriteCommand(nComId, "AT+VRX\0", ON);

  /* read  <DATA>,   <DLE><ETX> to comm port */

  nBufSize = 256;

  while (0 != (nReadNum = (int) ZyReadMemoryBlock(nComId,
						  (NPSTR) szTempBuffer,
						  (UINT) nBufSize)))
  {
     fwrite((NPSTR) szTempBuffer, sizeof(char), nReadNum, fp);
     if (nReadNum < nBufSize)
	break;
  }

  disconnectVoice(nComId);

  fclose(fp);
  return(IDR_SUCCESS);
}


/*
────────────────────────────────────────────────────────────────────────────
int disconnectVoice()

function:
     disconnect the line or internal speaker

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     LPSTR lpFile - the pointer of File name
     int nMode - the compression mode IDV_CELP, IDV_ADPCM
     int nDest - recording from IDV_SPAEKER, IDV_LINE

return code:
     return code >= 0 - success
     return code <0 - the error code

────────────────────────────────────────────────────────────────────────────
*/


void FAR disconnectVoice(int nComId)
{

  ZyWriteCommand(nComId, "AT+VLS=0\0",ON);
  ZyWriteCommand(nComId, "AT&F+FCLASS=0\0",ON);

  zyiusexonxoff(nComId, ASNOFL);
  zyiflow(nComId,ON);
  nFCMethod = FC_HARDWARE;

  return;
}






/*
────────────────────────────────────────────────────────────────────────────
int ZyDetectDTMF()

function:
     detect DTMF

parameters:
     int nComId - the comm port number : COM1, COM2, ......

return code:
     return code = DTMF0 - DTMFs
     return code = -1 - error

────────────────────────────────────────────────────────────────────────────
*/



int FAR ZyDetectDTMF(int nComId)
{
   int nResult;

   /* checking buffer empty */

   if ((nResult = zyipeek(nComId)) < IDR_SUCCESS) return (nResult);

   if (nResult != DLE)
   {
      zyiclear(nComId, ASIN);
      return (-1);
   }
   nResult = ZyReadChar(nComId);
   nResult = ZyReadChar(nComId);
   switch (nResult)
   {
       case '0':
	       nResult = DTMF0;
	       break;
       case '1':
	       nResult = DTMF1;
	       break;
       case '2':
	       nResult = DTMF2;
	       break;
       case '3':
	       nResult = DTMF3;
	       break;
       case '4':
	       nResult = DTMF4;
	       break;
       case '5':
	       nResult = DTMF5;
	       break;
       case '6':
	       nResult = DTMF6;
	       break;
       case '7':
	       nResult = DTMF7;
	       break;
       case '8':
	       nResult = DTMF8;
	       break;
       case '9':
	       nResult = DTMF9;
	       break;
       case '#':
	       nResult = DTMFnum;
	       break;
       case '*':
	       nResult = DTMFstar;
	       break;
       case 'c':
	       nResult = DTMFc;
	       break;
       case 'b':
	       nResult = DTMFb;
	       break;
       case 'q':
	       nResult = DTMFq;
	       break;
       case 's':
	       nResult = DTMFs;
	       break;
       default:
	       zyiclear(nComId, ASIN);
	       return (-1);
   }
   return (nResult);
}

/*
────────────────────────────────────────────────────────────────────────────
int ZyGenerateDtmf()

function:
     send DTMF code to modem

parameters:
     int nComId - the comm port number : COM1, COM2, ......
     int nCode - the DTMF code - '1', '2', '3',........

return code:
     return code = DTMF0 - DTMFs
     return code = -1 - error

────────────────────────────────────────────────────────────────────────────
*/



int FAR ZyGenerateDtmf(int nComId, int nCode)
{
  char szDtmf[80];




  strcpy (szDtmf, "AT+VTS=");
  strcat (szDtmf, & (char) nCode);
  return (ZyWriteCommand(nComId, szDtmf, ON));
}




[ RETURN TO DIRECTORY ]