/* LISTEN.C: To test receiving data from the amiga
 * 1. on the PC,    type: listen
 * 2. on the Amiga, type: machaddr 5
 *                        talk 1 4
 *
 * Port address can be changed, it won't be interpreted yet
 */
#undef DEBUG

#include <stdio.h>
#include <signal.h>
#include "pardev.h"
#include "dos.h"    /* only for crtlbrk() */

Header hdr;

char buffer[255];
int Length=0;

/* Activated on a CTRL-C or end of program */
int ParAbort(void)
{
  par_stop();
  printf("Aborted.\n");
  exit(1);
}

/* interrupt service routine, called from parvec() */
int parint(dev)
int dev;
{
 int numread=0;

#ifdef DEBUG
 printf("i");
#endif
 if (pardataready()==1)
 {
#ifdef DEBUG
   printf("d");
#endif
   numread=parreadV(&hdr,sizeof(hdr),buffer,255,NULL,NULL);
#ifdef DEBUG
   printf("%d",numread);
#endif
   /* swap lo & hi bits in the packet header, since PC's are
      'Little Endian' machines */
   hdr.port=get16((char *)&hdr.port);
   hdr.chksum=get16((char *)&hdr.chksum);
   hdr.length=get32((char *)&hdr.length);

   if ((numread > 0) && (!Length)) Length=numread;
 }
 return 0;
}

main(int argc,char **argv)
{
 int cnt;

 par_init(1,LPTADDR);
 ctrlbrk(ParAbort); /* be sure interrupts will be restored when aborted */

 printf("port: chksum: length: data:\n");
 while(1)
 {
  if (Length)      /* will be set in parint */
  {
    /* print the header content */
    printf("%04x  %04x    %04x    ",hdr.port,hdr.chksum,hdr.length);

    /* print the data content (this works only when data was send with the
       TALK command) */
      printf("%s",buffer);

    Length=0; /* done, ISR may fill structure again */
  }
 }
}
