//->$.Fax.!Arcfax.Driver.Test
// Copyright (c) David Pilling 1994
//


// The first section of the modem driver, contains general low level
// modem functions


int modemrxrate;
int modemtxrate;
int modembits;
int modemstop;
int modemparity;
int modemport;
int modemanswer;
int modemtonedial;
int modemflow;
int modemrings;
int modemmode;
int modemanswermode;




// count or purge buffer

int modem_countpurge(int rxtx,int code)
{
 return(devcon(Device_CountPurge,modemport,rxtx,code));
}


// tell modem to use a given sort of flow control

void modem_setflow(int flow)
{
 devcon(Device_Flow,modemport,flow,1);
 modemflow=flow;
}


// set rx rate

void modem_setrxrate(int rate)
{
 devcon(Device_RxRate,modemport,rate,1);
 modemrxrate=devcon(Device_RxRate,modemport,0,0);
}


// set tx rate

void modem_settxrate(int rate)
{
 devcon(Device_TxRate,modemport,rate,1);
 modemtxrate=devcon(Device_TxRate,modemport,0,0);
}


// set number of data bits

void modem_setbits(int bits)
{
 devcon(Device_DataBits,modemport,bits,1);

 modembits=devcon(Device_DataBits,modemport,0,0);
 modemparity=devcon(Device_ParityBits,modemport,0,0);
 modemstop=devcon(Device_StopBits,modemport,0,0);
}


// set parity bits

void modem_setparity(int parity)
{
 devcon(Device_ParityBits,modemport,parity,1);

 modembits=devcon(Device_DataBits,modemport,0,0);
 modemparity=devcon(Device_ParityBits,modemport,0,0);
 modemstop=devcon(Device_StopBits,modemport,0,0);
}


// set stop bits

void modem_setstop(int stop)
{
 devcon(Device_StopBits,modemport,stop,1);

 modembits=devcon(Device_DataBits,modemport,0,0);
 modemparity=devcon(Device_ParityBits,modemport,0,0);
 modemstop=devcon(Device_StopBits,modemport,0,0);
}


// set to tone/pulse dial

int modem_tonedial(int tonedial)
{
 modemtonedial=tonedial;
 return(modemtonedial);
}


void modem_setanswer(int answer)
{
 modemanswer=answer;
}


void modem_setmode(int mode)
{
 modemmode=mode;
}

   
int modem_cap(string & s)
{
 s="";
 return(FAX|DATA);
}


// is the modem on line ?

int modem_online(void)
{
 return(devcon(Device_Status,modemport,0,-1) & SERIAL_DCD);
}









// put modem off line

void modem_disconnect(void)
{

}

/*****************************************************************************/
// This section is concerned with Fax handling

int faxmodemretries;
int faxmodemresolution;
int faxmodemmaxbitrate;
int faxmodemminbitrate;
int faxmodemlength;
int faxmodemwidth;
int faxmodemdatacompression;
int faxmodemerrorcorrection;
int faxmodembinaryftp;


string faxmodemid;








/****************************************************************************/
// Fax transmit logic

// Parameters for session

int txptr;
int txresln;             // resolution
int txbitrate;           // bit rate code
int txpagewidth;         // page width code
int txpagelength;        // page height code
int txdatacompression;   // data compression
int txerrorcorrection;   // error correction
int txbinaryftp;         // binary ftp
int txscantime;          // min scan time

// Parameters for document

int txdocpages;
int txdocresolution;
int txdocwidth;


// Transmit page transfer status



// called by main program, to set parameters for next doc to send

void faxmodem_txparams(int pages,int resolution,int width)
{
 txdocpages=pages;
 txdocresolution=resolution;
 txdocwidth=width;
}


// called by main program to transmit documents
// return 0 if transmission completed correctly

int faxmodem_txdocument(int txdocuments)
{

 return(0);
}



// called by main program to set up parameters

void faxmodem_params(int res,int rate,int length,int width)
{
 faxmodemresolution=res;
 faxmodemmaxbitrate=rate;
 faxmodemlength=length;
 faxmodemwidth=width;
}

void faxmodem_params2(int data,int error,int binary,int rate)
{
 faxmodemdatacompression=data;
 faxmodemerrorcorrection=error;
 faxmodembinaryftp=binary;
 faxmodemminbitrate=rate;
}




// program the fax modem identifier

void faxmodem_id(string & id)
{
 faxmodemid=id;
}


// set number of page retries

int faxmodem_retries(int retries)
{
 faxmodemretries=retries;
 return(faxmodemretries);
}


/****************************************************************************/
// Fax receive logic
//
//

int rxgooddocument;      // return code from FHNG
int rxresln;             // resolution 0==low 1==high
int rxbitrate;           // bit rate code
int rxpagewidth;         // page width code
int rxpagelength;        // page height code
int rxdatacompression;   // data compression
int rxerrorcorrection;   // error correction
int rxbinaryftp;         // binary ftp
int rxscantime;          // min scan time





// return 0 OK, 1 Error

int faxmodem_rxdocument(void)
{
 string s;
 string result;
 int    docok;
 int    newdocument;
 int    badpage; 
 int    page;

 newdocument=1;
 rxgooddocument=1;
 rxresln=1;
 rxpagewidth=0;

 faxrxparams(0,0,0,0);
 page=0;

 while(1)
 {

  if(newdocument)
  {
   newdocument=0;
   page=0;
   faxrxopendocument();
  }

  badpage=faxrxpage(page,rxpagewidth,rxresln);

  break;
 }
 if(!newdocument) faxrxclosedocument(1);
 return(rxgooddocument);
}


/****************************************************************************/
// return 0 if OK, 1 on fail



// called when we want modem driver to sort out the incoming call
// return 0 on success


int modem_receive(int mode)
{
 return(faxmodem_rxdocument());
}


int modem_send(int mode,int files)
{
 return(faxmodem_txdocument(files));
}





int modem_dial(string number)
{
 return(0);
}


// return 0 on fail, or connection type

int modem_connect(void)
{
 string s;
 int    code;

 code=FAX;

 return(code);
}




// enter auto answer mode 
// return 0 on success

int modem_autoanswer(int rings,int mode)
{
 return(0);
}


int oneshot;


int modem_poll(void)
{
 if(oneshot) return(0);
 oneshot=1;
 return(FAX);
}


/*****************************************************************************/

// cancel modem operation


void modem_interrupt(void)
{


}


int modem_voicedial(string & number)
{
 return(0);
}



// called when ArcFax is about to terminate
// good time to hang up line etc.

void modem_terminate(void)
{

}


int gfh;


int getbyte(void)
{
 return(filegetc(gfh));
}

int putbyte(void)
{

 return(-1);
}




int modem_initiate(int port)
{
 fileseek(0,gfh);
 setchannel(TX,1,0,port);
 setchannel(RX,1,0,port);

 settxbor(1);
 setrxbor(1);

 return(0);
}


void main(void)
{
 gfh=fileopen("$.fax.input","rb");

}

