home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / stefanb_src / private_projects / fax / receivefax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-06  |  3.7 KB  |  173 lines

  1. #include "serio.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <clib/dos_protos.h>
  6.  
  7. static struct ReadArgs *rda;
  8. static struct SerialStream *ss;
  9. static char Template[]="DEVICE/K,UNIT/K/N,BAUD/K/N,FILE/K/A";
  10. static struct {
  11.                char *device;
  12.                long *unit;
  13.                long *baud;
  14.                char *file;
  15.               } def={"serial.device",NULL,NULL,NULL};
  16. #define BUFLEN 1024
  17. static UBYTE buffer[BUFLEN];
  18. static BOOL notend;
  19.  
  20. /* Read one reply line from modem */
  21. static void ReadLine(void *buf)
  22. {
  23.  char c='\0',*cp=buf;
  24.  
  25.  /* Read until EOL is reached */
  26.  while (c!='\n')
  27.   {
  28.    if (ReadSerialSynch(ss,&c,1)==0) break;
  29.    *cp++=c;
  30.   }
  31.  
  32.  /* Terminate string */
  33.  *cp='\0';
  34. }
  35.  
  36. /* FAX data receive routine */
  37. static int faxreceive(void)
  38. {
  39.  char *faxname,*infoname;
  40.  FILE *fhfax,*fhinfo;
  41.  ULONG rc=5;
  42.  
  43.  /* Build file names */
  44.  if (!(faxname=malloc(strlen(def.file)+4))) goto fre1;
  45.  strcpy(faxname,def.file);
  46.  strcat(faxname,".g3");
  47.  if (!(infoname=malloc(strlen(def.file)+5))) goto fre2;
  48.  strcpy(infoname,def.file);
  49.  strcat(infoname,".txt");
  50.  
  51.  /* Open files */
  52.  if (!(fhfax=fopen(faxname,"w"))) goto fre3;
  53.  if (!(fhinfo=fopen(infoname,"w"))) goto fre4;
  54.  
  55.  /* Wait until modem is ready */
  56.  while (TRUE)
  57.   {
  58.    /* Get status bits */
  59.    if (!QuerySerial(ss)) goto fre5; /* Can't query device */
  60.  
  61.    /* Check Carrier detect */
  62.    if (ss->ss_Status&0x0020) goto fre5; /* Carrier lost */
  63.  
  64.    /* Check CTS becomes low */
  65.    if (!(ss->ss_Status&0x0010)) break;
  66.  
  67.    /* Short Delay */
  68.    Delay(5);
  69.   }
  70.  
  71.  /* Modem ready for transfer, send DC2 */
  72.  WriteSerialSynch(ss,"\x12",1);
  73.  
  74.  /* Read parameter line and write it to FAX info file */
  75.  ReadLine(buffer);
  76.  ReadLine(buffer);
  77.  fprintf(fhinfo,"%s",buffer);
  78.  
  79.  /* Receive FAX Data */
  80.  while (TRUE)
  81.   {
  82.    /* Get number of bytes in buffer */
  83.    QuerySerial(ss);
  84.  
  85.    /* Any bytes to read? */
  86.    if (ss->ss_Unread>0)
  87.     {
  88.      /* Yes, get them and process them */
  89.      LONG len=(ss->ss_Unread>BUFLEN)?BUFLEN:ss->ss_Unread;
  90.      UBYTE *cp=buffer;
  91.  
  92.      /* Read bytes into buffer */
  93.      if (ReadSerialSynch(ss,buffer,len)!=len) break;
  94.      fwrite(buffer,1,len,fhfax);
  95.     }
  96.    else
  97.     {
  98.      /* No, carrier lost? */
  99.      if (ss->ss_Status&0x0020)
  100.       {
  101.        rc=0; /* Yes, FAX probably received OK */
  102.        break;
  103.       }
  104.  
  105.      /* No. Wait a little while */
  106.      Delay(5);
  107.     }
  108.   }
  109.  
  110.  /* Free resources */
  111. fre5:fclose(fhinfo);
  112. fre4:fclose(fhfax);
  113. fre3:free(infoname);
  114. fre2:free(faxname);
  115. fre1:return(rc);
  116. }
  117.  
  118. /* CTRL-C shut down routine */
  119. static int Shutdown(void)
  120. {
  121.  notend=FALSE;
  122.  return(0);
  123. }
  124.  
  125. int main(int argc, char *argv[])
  126. {
  127.  int (*oldbreak)();
  128.  ULONG rc=5;
  129.  ULONG Unit,Baud;
  130.  
  131.  /* Read command line parameters */
  132.  if (!(rda=ReadArgs(Template,(LONG *) &def,NULL)))
  133.   {
  134.    PrintFault(IoErr(),argv[0]);
  135.    exit(20);
  136.   }
  137.  Unit=(def.unit)?(*def.unit):0;
  138.  Baud=(def.baud)?(*def.baud):19200;
  139.  
  140.  /* Install CTRL-C shut down routine */
  141.  notend=TRUE;
  142.  oldbreak=onbreak(Shutdown);
  143.  printf("'%s' %ld (%ldbps) file '%s'\n",def.device,Unit,Baud,def.file);
  144.  
  145.  if (ss=CreateSerialStream(def.device,Unit,
  146.                            SERF_SHARED|SERF_7WIRE|SERF_RAD_BOOGIE))
  147.   {
  148.    if (SetSerialParamsTags(ss,SIO_Baud,Baud,TAG_DONE))
  149.     while (notend)
  150.      {
  151.       /* Wait on modem reply */
  152.       ReadLine(buffer);
  153.       printf("%s",buffer);
  154.  
  155.       /* Analyse modem reply */
  156.       if (strlen(buffer)==2) continue; /* Ignore empty replies */
  157.       if (!strncmp("ZyXEL",buffer, 5)) rc=faxreceive(); /* Read FAX data */
  158.       notend=FALSE; /* Every other reply is errornous */
  159.      }
  160.    else printf("Couldn't set parameters!\n");
  161.  
  162.    DeleteSerialStream(ss);
  163.   }
  164.  else printf("Couldn't open stream!\n");
  165.  
  166.  /* Free command line parameters */
  167.  FreeArgs(rda);
  168.  
  169.  /* Leave program */
  170.  onbreak(oldbreak);
  171.  return(0);
  172. }
  173.