/* $Id: get_gsII_ev.c,v 1.66 2010/07/16 17:36:49 tl Exp $ */ /* this function only handles reads a block level */ /* decomposition of the data in the block is done */ /* in the gsII_bufp function */ /* now handles reads both from tape and network */ #include #include #include #include #include #include #include "efftape.h" #include "gsII.h" /* global variables */ extern int have_get_gsII_in_drive; extern char get_gsII_in_drive[128]; static int numBlocksRead = TAPE_REOPEN; /* block counter, force read */ static int ForceRecordVer = -1; static int doSwapBytes = 0; /*------------------------------------------------------------*/ #ifdef DISK void SetRecordVer_disk(int nn) { ForceRecordVer = nn; printf("SetRecordVer: will force RecordVer to be %i\n", ForceRecordVer); } void swapBufferBytes() { doSwapBytes = 1; } #endif #ifdef NET void SetRecordVer_net(int nn) { ForceRecordVer = nn; printf("SetRecordVer: will force RecordVer to be %i\n", ForceRecordVer); } #endif #ifdef TAPE void SetRecordVer_tape(int nn) { ForceRecordVer = nn; printf("SetRecordVer: will force RecordVer to be %i\n", ForceRecordVer); } #endif /*-------------------------------------------------------*/ #ifdef DISK /* one one copy of this function */ int GetBufferNo_disk() { /* return the current buffer number */ return (numBlocksRead); } #endif /*-------------------------------------------------------*/ #ifdef NET /* one one copy of this function */ int GetBufferNo_net() { /* return the current buffer number */ return (numBlocksRead); } #endif /*-------------------------------------------------------*/ #ifdef TAPE /* one one copy of this function */ int GetBufferNo_tape() { /* return the current buffer number */ return (numBlocksRead); } #endif /*-------------------------------------------------------*/ #ifdef NET int get_net_ev(struct EVHDR * hdr, struct EVENT * ev, unsigned int ext[]) #endif #ifdef DISK int get_disk_ev(struct EVHDR * hdr, struct EVENT * ev, unsigned int ext[]) #endif #ifdef TAPE int get_gsII_ev(struct EVHDR * hdr, struct EVENT * ev, unsigned int ext[]) #endif { /* declarations */ static int exa = -1; /* exabyte dev handle */ static int newbuf = 1; /* read new buffer */ static unsigned short int buffer[NETSIZE]; int siz; int i; EVENT_BUFFER *eb; unsigned short int t1, t2; #ifdef TAPE FILE *fp; #endif /* proto types */ int rew_tape(int); int status_tape(int); int time_stamp(); int print_file_header(FILE_HEADER *); int print_tape_header(TAPE_HEADER *); int gsII_bufp(EVENT_BUFFER *, struct EVHDR *, struct EVENT *, unsigned int *); int GSudpGetBuf(EVENT_BUFFER *); /* printf("numBlocksRead= %i\n",numBlocksRead); */ #if(0) printf("get_gsII_ev: newbuf=%i\n", newbuf); fflush(stdout); #endif /* force a tape rewind (e.g., for auto gain) ? */ #ifdef TAPE if (hdr->len > 200) { printf("rewinding tape... (exa=%i)", exa); fflush(stdout); rew_tape(exa); printf(" - done \n"); printf("closing tape... "); close(exa); printf(" - done\n"); hdr->len = 0; newbuf = 1; numBlocksRead = TAPE_REOPEN; return (0); }; #endif /* reopen input */ if (numBlocksRead >= TAPE_REOPEN) { #ifdef TAPE printf("tape_reopen...: %i\n", TAPE_REOPEN); numBlocksRead = 0; /* first close tapedrive (ignore error if not open) */ if (exa >= 0) { printf("(re)open tape drive, closing..."); fflush(stdout); status_tape(exa); fflush(stdout); close(exa); printf(" tape closed\n"); fflush(stdout); }; /* what tape drive should we use ? */ if (have_get_gsII_in_drive != 1) { printf("no input tape drive was set with\n"); printf("function: set_gsII_indrive(\"name\")\n"); printf("so attempting to read from file:\n"); printf(" GSII_IN_DRIVE\n"); printf("\n"); if ((fp = fopen("GSII_IN_DRIVE", "r")) == NULL) { printf(".... file not found! \n"); printf("you must set the input tape drive name\n"); printf("either by using the set_gsII_indrive()\n"); printf("function or by putting the tape drive name \n"); printf("in the file: GSII_IN_DRIVE \n"); printf("\n"); printf("quitting...\n"); exit(0); } else { fscanf(fp, "%s", get_gsII_in_drive); have_get_gsII_in_drive = 1; fclose(fp); }; }; #endif #ifdef TAPE /* open input tape drive */ if ((exa = open(get_gsII_in_drive, O_RDONLY, 0)) == -1) { printf("get_gsII_ev/could not open "); printf("in-tape drive <%s>\n", get_gsII_in_drive); fflush(stdout); close(exa); return (-1); } else { printf("in-tape drive open (%i) @ ", exa); time_stamp(); printf("drive: %s\n", get_gsII_in_drive); status_tape(exa); fflush(stdout); }; #endif #ifdef DISK /* open disk file */ if ((exa = open(get_gsII_in_drive, O_RDONLY, 0)) == -1) { printf("get_gsII_ev/could not open "); printf("diskfile <%s>\n", get_gsII_in_drive); fflush(stdout); close(exa); return (-1); } else { printf("disk file open (%i) @ ", exa); time_stamp(); printf("disk file: %s\n", get_gsII_in_drive); fflush(stdout); }; #endif #ifdef NET /* force read of new buffer */ newbuf = 1; numBlocksRead = 0; #endif #ifdef DISK /* force read of new buffer */ newbuf = 1; numBlocksRead = 0; #endif }; /* read a new buffer ? */ if (newbuf != 0) { #ifdef TAPE /* read a new buffer from tape */ siz = read(exa, (char *) buffer, DATASIZE); #ifdef LINUX /* swap the bytes.......Note that udp */ /* packets are swapped in GSudpGetBuf */ /* so do not move this entry down!!!! */ for (i = 0; i < siz/2; i++) { t1 = (buffer[i] & 0x00ff) << 8; t2 = (buffer[i] & 0xff00) >> 8; buffer[i] = t1 + t2; }; #endif #endif #ifdef DISK /* read a new buffer from disk */ #if(0) printf("reading buffer\n"); fflush(stdout); #endif siz = read(exa, (char *) buffer, DATASIZE); #ifdef LINUX /* swap the bytes.......Note that udp */ /* packets are swapped in GSudpGetBuf */ /* so do not move this entry down!!!! */ for (i = 0; i < siz/2; i++) { t1 = (buffer[i] & 0x00ff) << 8; t2 = (buffer[i] & 0xff00) >> 8; buffer[i] = t1 + t2; }; #endif #endif #ifdef NET /* get a 16K buffer from network */ siz = GSudpGetBuf((EVENT_BUFFER *) buffer); #endif /* also swap if we are asked to */ if (doSwapBytes) for (i = 0; i < siz/2; i++) { t1 = (buffer[i] & 0x00ff) << 8; t2 = (buffer[i] & 0xff00) >> 8; buffer[i] = t1 + t2; }; /* force a new buffer RecordVer version number? */ if (ForceRecordVer > -1) { eb = (EVENT_BUFFER *) buffer; eb->RecordVer = ForceRecordVer; }; numBlocksRead++; assert(numBlocksRead > 0); /* inspect buffer */ if (siz == DATASIZE && *buffer == 1) { printf("TAPEHEADER\n"); print_tape_header((TAPE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIITAPEHEAD); } else if (siz == DATASIZE && *buffer == 2) { printf("FILEHEADER\n"); print_file_header((FILE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIIERRFILEHEAD); } else { switch (siz) { case DATASIZE: newbuf = 0; /* prevent new buffer read in next call */ break; #ifdef TAPE case FILEHEADER: printf("FILEHEADER\n"); print_file_header((FILE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIIERRFILEHEAD); case EOFSIZE: printf("eof encountered, siz=%i\n", siz); fflush(stdout); newbuf = 1; /* force new buffer read */ return (GSIIGETNEWBUFF); case TAPEHEADER: printf("TAPEHEADER\n"); print_tape_header((TAPE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIITAPEHEAD); case ERRORSIZE: printf("get_gsII_ev.c/error, siz=-1, numBlocksRead= %i\n", numBlocksRead); fflush(stdout); newbuf = 1; return (-1); default: newbuf = 1; /* force new buffer read */ return (GSIIERRCONT); #endif #ifdef DISK case FILEHEADER: printf("FILEHEADER\n"); print_file_header((FILE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIIERRFILEHEAD); case EOFSIZE: printf("eof encountered, siz=%i\n", siz); fflush(stdout); newbuf = 1; /* force new buffer read */ return (GSIIGETNEWBUFF); case TAPEHEADER: printf("TAPEHEADER\n"); print_tape_header((TAPE_HEADER *) buffer); newbuf = 1; /* force new buffer read */ return (GSIITAPEHEAD); case ERRORSIZE: printf("get_gsII_ev.c/error, siz=-1, numBlocksRead= %i\n", numBlocksRead); fflush(stdout); newbuf = 1; return (-1); case GSIIERRBLOCK: printf("get_gsII_ev.c/error, GSIIERRBLOCK, numBlocksRead= %i\n", numBlocksRead); fflush(stdout); newbuf = 1; return (GSIIERRBLOCK); default: printf("get_gsII_ev.c/error, GSIIERRCONT siz=%i , numBlocksRead= %i\n", siz, numBlocksRead); fflush(stdout); newbuf = 1; /* force new buffer read */ return (GSIIERRCONT); #endif #ifdef NET default: newbuf = 1; /* force new buffer read */ return (siz); #endif } }; }; /* process buffer (note: we only get here if databuffer) */ newbuf = gsII_bufp((EVENT_BUFFER *) buffer, hdr, ev, ext); #if(0) if (newbuf != 0) printf("get_gsII_ev: newbuf=%i\n", newbuf); #endif /* done */ return (newbuf); }