⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faxsr.c

📁 传真示例
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************** 
 *  Fax Send/Receive Demonstration Program
 *  Copyright (c) 1996 by Dialogic Corporation. All rights reserved.
 **************************************************************************/


/***************************************************************************
  
  FAXSR is a demo program designed to send or receive faxes using the
  Dialogic fax API.
  
  FEATURES:  Send up to 4 files specified on the command line.  File
  extensions are used to determine data type, ".raw" for raw files and
  ".tif" for TIFF/F files.  Files with any other extension are assumed
  to be ASCII format.  The DF_ASCIIDATA structure is used to change the
  default settings for faxing ASCII information.
  
  FAXSR can also receive faxes and store them to disk in either raw or
  TIFF/F format.  The extension of the specified receive file is used
  to determine how the fax will be stored, ".raw" for raw format or ".tif"
  for TIFF/F format.  If raw is specified and the fax contains more than
  one page, additional pages will be stored in files with incrementing
  extensions, i.e. filename02.raw, filename03.raw, etc.
  
  In addition, the User Header field, Local ID, and channel can be
  specified on the command line.
  
  FLOW OF CONTROL:  FAXSR parses the command line parameters to determine
  what to do.  If the dial string and send file list are present, FAXSR
  builds an IOTT chain, dials the number and sends the fax.  If a receive
  file is specified, FAXSR waits for rings.  If rings are received, FAXSR
  goes offhook and receives the fax.
  
  DEFAULTS:
  Channel             First channel in system
  Local ID            "FAXSR CH: nn"  where nn is the channel number
  User Header         "" a null string
  
***************************************************************************/

#include <windows.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>

/* Dialogic Voice and Fax library header files */
#include <srllib.h>
#include <dxxxlib.h>
#include <faxlib.h>

/* Global Data */
short   channel=1;                  /* default channel number */
char *localidp=NULL;                /* pointer to local id if specified */
char *userheaderp="";               /* default user header field */
char *dialstringp=NULL;             /* phone number to dial */
char *version="3.50";

/* send information */
#define MAXIOTT 4
int txcount=0;                      /* number of filenames specified */
char    *txfiles[MAXIOTT];          /* pointers to filenames */
DF_IOTT iott[MAXIOTT]={0};      /* DF_IOTT array for sendfax command */
DF_ASCIIDATA    asciidata={0};      /* margin and font info for ASCII data */

/*
 * NOTE: DFC_EOM causes Phase B to be renegotiated between each
 * fax page.  If all pages to be sent are the same width and
 * resolution, the phase D continuation should be set to DFC_MPS for
 * faster transmission. Setting this value to DFC_AUTO causes the
 * Fax library to automatically determine the appropriate phase D value.
 */
int phdcont=DFC_MPS;            /* issue MPS between pages */

/* receive information */
#define MAXRCVNAME  128         /* max length of receive name */
char *rxfilep=NULL;             /* pointer to rcv file name */

char *optarg = NULL;            /* global data used by getopt() function */
   
/* function prototypes */
void main();
int fax_send();
int fax_receive();
void chkargs();
long phd_hndlr();
long phb_hndlr();
int call();
void dispstatus();
void disperror();
char    *Dcmd();
char    *Drpy();
int getopt();
void usage();
   
/***************************************************************************
 *        NAME: main(argc,argv)
 * DESCRIPTION: This is the demo entry point 
 *      INPUTS: argc = number of command line arguments
 *              argv = array of pointers to command line arguments 
 *     OUTPUTS: The FAX demo is executed.
 *    CAUTIONS: none. 
 *************************************************************************/
void main(argc,argv)
   int argc;
   char *argv[];
{
   printf("FAXSR Send/Receive Demo Version %s\n",version);
   printf("(C)opyright 1996, Dialogic Corp.\n");
   
   /* parse and validate command line */
   chkargs(argc,argv);
   
   /* if dialstring is present, send files */
   if (dialstringp!=NULL) {
      fax_send();
   } else {
      fax_receive();
   }
   
}

/****************************************************************
 *        NAME : fax_send()
 * DESCRIPTION : send a fax 
 *       INPUT : dialstringp,txfiles,channel
 *      OUTPUT : none.
 *     RETURNS : -1 if error
 *    CAUTIONS : none.
 ****************************************************************/
int fax_send()
{
   char buff[40];
   int  faxhandle,voxhandle,rc;
   int i;
   char    *strp;
   DF_IOTT *badiottptr;
   int flags=DF_PHASEB|DF_PHASED|EV_SYNC;
   int initascii=0;
   
   /* build IOTT chain for fx_sendfax() */
   for (i=0;i<txcount;i++) {

      /* open file using dx_fileopen, determine type, and setup iott */
      if ((rc=dx_fileopen(txfiles[i],O_RDONLY|O_BINARY, 0))==-1) {
	 printf("Error: Unable to open \"%s\"\n",txfiles[i]);
	 return(-1);
      }
      
      strp=strrchr(txfiles[i],'.');       /* find file extension */
      
      if (strp!=NULL && !strncmp(strp+1,"raw",3)) {
	 /* raw file format */
	 fx_setiott(&iott[i],rc,DF_RAW,phdcont);
      } else if (strp!=NULL && !strncmp(strp+1,"tif",3)) {
	 /* TIFF/F file format */
	 fx_setiott(&iott[i],rc,DF_TIFF,phdcont);
      } else {
	 /* no extension or other than TIF or RAW */
	 fx_setiott(&iott[i],rc,DF_ASCII,phdcont);
	 
	 /* Initialize Ascii information */
	 if (!initascii) {
	    /*
	     * First ASCII file, init ascii data structure.
	     * NOTE: This structure is optional, if it isn't specified,
	     * the FAX Library will use default values.
	     */
	    initascii=1; /* set flag so we only do this once */

	    asciidata.units=DF_UNITS_IN10;  /* values in 10th of an inch */
	    asciidata.font=DF_FONT_0;       /* normal font */
       asciidata.pagepad=DF_PAD;       /* pad short pages to pagelength */
	    asciidata.pagelength=110;       /* normal letter size paper */
	    asciidata.topmargin=10;         /* set all margins to 1 inch */
	    asciidata.botmargin=10;
	    asciidata.leftmargin=10;
	    asciidata.rightmargin=10;
	    asciidata.linespace=DF_SINGLESPACE;
	    asciidata.tabstops=10;
	 }
         /* set IOTT to use it */
	 iott[i].io_datap=(void *)&asciidata;
      }
      
   }   /* end of for */
   
   iott[txcount-1].io_type|=IO_EOT;    /* mark last IOTT as the last one */
   iott[txcount-1].io_phdcont=DFC_EOP; /* and set continuation to EOP */   
   
   /* open channel */
   sprintf(buff,"dxxxB%dC%d",((channel-1)/4)+1,((channel-1)%4)+1);
   if ((voxhandle=dx_open(buff,0)) ==-1) {
      printf("Error opening %s",buff);
      disperror(voxhandle, "dx_open failed");
      return(-1);
   }

   if ((faxhandle=fx_open(buff,0)) ==-1) {
      printf("Error opening %s",buff);
      disperror(faxhandle, "fx_open failed");
      dx_close(voxhandle);
      return(-1);
   }
   printf("Opened channel %s\n",buff);

#ifdef HANDLERS
   /* NOT supported in this release */

   /* Install phase B & D handler */
   if (sr_enbhdlr(faxhandle, TFX_PHASED, phd_hndlr) == -1) {
      printf("sr_enbhdlr for phase D failed\n");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   if (sr_enbhdlr(faxhandle, TFX_PHASEB, phb_hndlr) == -1) {
      printf("sr_enbhdlr for phase B failed\n");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
#endif   
   /* place on hook */
   if (dx_sethook(voxhandle,DX_ONHOOK, EV_SYNC) == -1) {
      disperror(voxhandle,"ONHOOK failed");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /* set inital FAX state for channel */
   if (fx_initstat(faxhandle,DF_TX)!=0) {
      disperror(faxhandle,"fx_initstat failed");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /* init parameters */
   if (localidp==NULL) {
      sprintf(buff,"FAXSR CH: %02d",channel);
      localidp=&buff[0];
   }
   
   if (fx_setparm(faxhandle,FC_LOCALID,localidp)!=0) {
      disperror(faxhandle,"Setting local id");
   }
   
   if (fx_getparm(faxhandle,FC_LOCALID,buff)!=0) {
      disperror(faxhandle,"Reading local id");
   } else {
      printf("Local ID set to \"%s\"\n",buff);
   }
   
   /* 
    * Set the User field in the fax header which appears at the top of every
    * transmitted page.  Not supported on the Fax/120. If the setparm returns
    * and 'EFX_UNSUPPORTED' error, don't try to read it back.
    */
   if (fx_setparm(faxhandle,FC_HDRUSER,userheaderp)!=0) {
      disperror(faxhandle, "Setting user header field");
   } else if (fx_getparm(faxhandle,FC_HDRUSER,buff)!=0) {
      disperror(faxhandle,"Reading user header field");
   } else {
      printf("User Header set to \"%s\"\n",buff);
   } 
   
   /* 
    * Set the inter-page Phase D continuation value for 
    * transmitting TIFF files. 
    */
   if (fx_setparm(faxhandle,FC_SENDCONT,(void *)&phdcont)!=0) {
      disperror(faxhandle, "Setting FC_SENDCONT"); 
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /* Set up to retry ONCE, then disconnect */
   i=DF_RETRY1|DF_RETRYDCN;    
   if (fx_setparm(faxhandle,FC_RETRYCNT,(void *)&i)!=0) {
      disperror(faxhandle,"Setting retry count");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /* call the number */
   printf("Dialing \"%s\"...",dialstringp);
   fflush(stdout);
   rc=call(voxhandle,dialstringp);
   if ((rc!=CR_CNCT)&&(rc!=CR_FAXTONE)) {
      if (rc==-1) {
	 disperror(voxhandle,"DIAL failed");
      } else {
	 switch (rc) {   /* display reason for failure */
	 case CR_BUSY:
	    printf("Busy\n");
	    break;
	 case CR_NOANS:
	    printf("No answer\n");
	    break;
	 case CR_NODIALTONE:
	    printf("No dialtone\n");
	    break;
	 default:
	    printf("Call failed, Call analysis %d\n",rc);
	    break;
	 }
      } 
      /* cleanup if we didn't get connected */
      if (dx_sethook(voxhandle,DX_ONHOOK, EV_SYNC) !=0) {
         disperror(voxhandle,"ONHOOK failed");
      }
      fx_close(faxhandle);
      dx_close(voxhandle);
      
      /* Close all Tx files. Use dx_fileclose () */
      for (i=0;i<txcount;i++)         
         dx_fileclose(iott[i].io_fhandle);
      
      return(-1);
   } else {
      printf("connect\n");
   }
   
   /* Dial complete -- send the fax */
   printf("\nSending");
   for (i=0;i<txcount;i++)
      printf(" \"%s\"",txfiles[i]);
   printf("...\n");
   
   if ((rc=fx_sendfax(faxhandle,&iott[0],flags))==-1) {
      disperror(faxhandle,"Sendfax failed");
      switch (ATDV_LASTERR(faxhandle)) {
      case EFX_BADTIF:
      case EFX_BADTAG:
      case EFX_NOPAGE:
      case EFX_BADPAGE:
	 printf("Bad Tag %x\n",ATFX_TFBADTAG(faxhandle));
	 printf("Missing Tag %x\n",ATFX_TFNOTAG(faxhandle));
	 /* Fall thru and display bad iott too */
      case EFX_BADIOTT:
	 badiottptr = ATFX_BADIOTT(faxhandle);
	 printf("Offending DF_IOTT entry, IOTT %x\n",badiottptr);
	 printf("io_type = %d\n",badiottptr->io_type);
	 printf("io_fhandle = %d\n",badiottptr->io_fhandle);
	 printf("io_offset = %d\n",badiottptr->io_offset);
	 printf("io_length = %d\n",badiottptr->io_length);
	 printf("io_datatype = %d\n",badiottptr->io_datatype);
	 printf("io_phdcont = %d\n",badiottptr->io_phdcont);
	 printf("io_width = %d\n",badiottptr->io_width);
	 printf("io_resln = %d\n",badiottptr->io_resln);
	 break;
      case EFX_DISCONNECT:
	 printf("Remote has disconnected\n");
	 printf("Phase E Status: %ld\n",ATFX_ESTAT(faxhandle));
	 break;
      case EFX_RETRYDCN:
	 printf("Disconnected after specified retries\n");
	 break;
      default:
         break;
      } /* end of switch */
   } /* end of if */
   
   dispstatus(faxhandle,"     -- Send fax complete --");
   
   /* place channel on hook */
   if (dx_sethook(voxhandle,DX_ONHOOK,EV_SYNC) == -1) {
      disperror(voxhandle,"ONHOOK failed");
   }
   
   /* close files */
   for (i=0;i<txcount;i++) {
      dx_fileclose(iott[i].io_fhandle);
   }

   fx_close(faxhandle);   
   dx_close(voxhandle);
   return rc;              /* return error code from sendfax call */
}

/****************************************************************
 *        NAME : fax_receive()
 * DESCRIPTION : wait for rings and receive a fax 
 *       INPUT : rxfilep,channel
 *      OUTPUT : none.
 *     RETURNS : none.
 *    CAUTIONS : none.
 ****************************************************************/
int fax_receive()
{
   char    buff[40];
   int     faxhandle,voxhandle,rc;
   char    *strp;
   long    phdcmd,phdrpy;
   int flags=DF_PHASEB|DF_PHASED|EV_SYNC;  /* default receive flags */
   char    rxfile[MAXRCVNAME];     /* buffer for receive file name */
   int     rxcount=1;              /* number of files */
   
   /* copy receive file name to internal buffer */
   rxfilep=strncpy(&rxfile[0],rxfilep,MAXRCVNAME);
   
   /* make sure it has an extension */
   if ((strp=strrchr(rxfilep,'.'))==NULL)
      return(-1);
   
   if (!strncmp(strp+1,"tif",3)) {
      /* TIFF/F file */
      flags|=DF_TIFF;    
   } else {
      /* For all others, receive as Raw */
      flags|=DF_RAW;     
   }
   
   /* Open channel */
   sprintf(buff,"dxxxB%dC%d",((channel-1)/4)+1,((channel-1)%4)+1);
   if ((voxhandle=dx_open(buff,0)) ==-1) {
      printf("Error opening %s",buff);
      disperror(voxhandle, "OPEN failed");
      return(-1);
   }

   if ((faxhandle=fx_open(buff,0)) ==-1) {
      printf("Error opening %s",buff);
      disperror(faxhandle, "fx_open failed");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   printf("Opened channel %s\n",buff);

#ifdef HANDLERS
   /* NOT supported in this release */

   /* Install phase B & D handler */
   if (sr_enbhdlr(faxhandle, TFX_PHASED, phd_hndlr) == -1) {
      printf("sr_enbhdlr for phase D failed\n");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   if (sr_enbhdlr(faxhandle, TFX_PHASEB, phb_hndlr) == -1) {
      printf("sr_enbhdlr for phase B failed\n");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
#endif

   /* Place on hook */
   if (dx_sethook(voxhandle,DX_ONHOOK, EV_SYNC) == -1) {
      disperror(voxhandle,"ONHOOK failed");
      fx_close(faxhandle);
      dx_close(voxhandle);
      return(-1);
   }
   
   /* set inital FAX state for channel */
   if (fx_initstat(faxhandle,DF_RX)!=0) {
      disperror(faxhandle,"fx_initstat failed");
      fx_close(faxhandle);
      dx_close(faxhandle);
      return(-1);
   }
   
   /* init parameters */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -