Пример #1
0
int main(int argc, char **argv)
{
  const int MaxFrameSize = 16*MAX_VDIF_FRAME_BYTES;
  char buffer[MaxFrameSize];
  FILE * input;
  int readbytes, framebytes, framemjd, framesecond, framenumber, frameinvalid, datambps, framespersecond;
  long long framesread;
  vdif_header *header;

  if(argc != 3)
  {
    usage();

    return EXIT_FAILURE;
  }
  
  input = fopen(argv[1], "r");
  if(input == NULL)
  {
    fprintf(stderr, "Cannot open input file %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  datambps = atoi(argv[2]);
  readbytes = fread(buffer, 1, MaxFrameSize, input); //read the VDIF header
  header = (vdif_header*)buffer;
  framebytes = getVDIFFrameBytes(header);
  if(framebytes > MaxFrameSize) {
    fprintf(stderr, "Cannot read frame with %d bytes > max (%d), formal max %d\n", framebytes, MaxFrameSize, MAX_VDIF_FRAME_BYTES);
    exit(EXIT_FAILURE);
  }
  framemjd = getVDIFFrameMJD(header);
  framesecond = getVDIFFrameSecond(header);
  framenumber = getVDIFFrameNumber(header);
  frameinvalid = getVDIFFrameInvalid(header);
  framespersecond = (int)((((long long)datambps)*1000000)/(8*(framebytes-VDIF_HEADER_BYTES)));
  printf("Frames per second is %d\n", framespersecond);
 
  rewind(input); //go back to the start

  framesread = 0;
  while(!feof(input)) {
    readbytes = fread(buffer, 1, framebytes, input); //read the whole VDIF packet
    if (readbytes < framebytes) {
      fprintf(stderr, "Header read failed - probably at end of file.\n");
      break;
    }
    header = (vdif_header*)buffer;
    framemjd = getVDIFFrameMJD(header);
    framesecond = getVDIFFrameSecond(header);
    framenumber = getVDIFFrameNumber(header);
    frameinvalid = getVDIFFrameInvalid(header);
    printf("MJD is %d, second is %d, framenumber is %d, frameinvalid is %d\n", framemjd, framesecond, framenumber, frameinvalid);
    printf("Threadid is %d, stationid is %d, numbits is %d\n", getVDIFThreadID(header), getVDIFStationID(header), getVDIFBitsPerSample(header));
    if(getVDIFFrameBytes(header) != framebytes) { 
      fprintf(stderr, "Framebytes has changed! Can't deal with this, aborting\n");
      break;
    }
    framesread++;
  }

  printf("Read %lld frames\n", framesread);
  fclose(input);

  return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc, char **argv)
{
  int VERBOSE = 0;
  int FORCE_VALID = 1;
  char buffer[MAX_VDIF_FRAME_BYTES];
  FILE * input;
  FILE * output;
  int readbytes, framebytes, framemjd, framesecond, framenumber, frameinvalid, datambps, framespersecond;
  int nextmjd, nextsecond, nextnumber;
  int overwritemjd, overwritesecond, overwritenumber;
  int offsetmjd=0, offsetsecond=0, offsetnumber=0;
  long long framesread, frameswrote;
  vdif_header *header;

  if(argc != 4 && argc != 5)
  {
    usage();

    return EXIT_FAILURE;
  }

  overwritemjd = 0;
  overwritesecond = 0;
  overwritenumber = 0;
  if(argc == 5) {
    overwritemjd = atoi(argv[4]);
    overwritesecond = (int)(86400.0*(atof(argv[4]) - overwritemjd));
  }
  
  input = fopen(argv[1], "r");
  if(input == NULL)
  {
    fprintf(stderr, "Cannot open input file %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }

  output = fopen(argv[2], "w");
  if(output == NULL)
  {
    fprintf(stderr, "Cannot open output file %s\n", argv[2]);
    exit(EXIT_FAILURE);
  }

  if(FORCE_VALID) {
    printf("Forcing all read frames to be VALID! (Temp workaround for invalid tagged EVLA VDIF data)\n");
  }
  datambps = atoi(argv[3]);
  readbytes = fread(buffer, 1, VDIF_HEADER_BYTES, input); //read the VDIF header
  header = (vdif_header*)buffer;
  framebytes = getVDIFFrameBytes(header);
  if(framebytes > MAX_VDIF_FRAME_BYTES) {
    fprintf(stderr, "Cannot read frame with %d bytes > max (%d)\n", framebytes, MAX_VDIF_FRAME_BYTES);
    exit(EXIT_FAILURE);
  }
  nextmjd = getVDIFFrameMJD(header);
  nextsecond = getVDIFFrameSecond(header);
  nextnumber = getVDIFFrameNumber(header);
  if(overwritemjd > 0) {
    offsetmjd = overwritemjd - nextmjd;
    offsetsecond = overwritesecond - nextsecond;
    offsetnumber = overwritenumber - nextnumber;
  }
  framespersecond = (int)((((long long)datambps)*1000000)/(8*(framebytes-VDIF_HEADER_BYTES)));
  printf("Frames per second is %d\n", framespersecond);
 
  rewind(input); //go back to the start

  framesread = 0;
  frameswrote = 0;
  while(!feof(input)) {
    readbytes = fread(buffer, 1, framebytes, input); //read the whole VDIF packet
    if (readbytes < framebytes) {
      fprintf(stderr, "Header read failed - probably at end of file.\n");
      break;
    }
    header = (vdif_header*)buffer;
    framemjd = getVDIFFrameMJD(header);
    framesecond = getVDIFFrameSecond(header);
    framenumber = getVDIFFrameNumber(header);
    frameinvalid = getVDIFFrameInvalid(header);
    if(getVDIFFrameBytes(header) != framebytes) { 
      fprintf(stderr, "Framebytes has changed! Can't deal with this, aborting\n");
      break;
    }
    framesread++;
    //check for missing frames
    while(framemjd != nextmjd || framesecond != nextsecond || framenumber != nextnumber) {
      if(VERBOSE)
        fprintf(stderr, "Missed a packet! We were expecting MJD %d, sec %d, frame %d, and the next one came in MJD %d, sec %d, frame %d\n",
                nextmjd, nextsecond, nextnumber, framemjd, framesecond, framenumber);
      //write out an invalid frame for the next frame
      overwritenumber = nextnumber + offsetnumber;
      overwritesecond = nextsecond + offsetsecond;
      overwritemjd    = nextmjd    + offsetmjd;
      while (overwritenumber < 0) {
        overwritenumber += framespersecond;
        overwritesecond++;
      }
      while (overwritenumber >= framespersecond) {
        overwritenumber -= framespersecond;
        overwritesecond--;
      }
      while (overwritesecond < 0) {
        overwritesecond += 86400;
        overwritemjd++;
      }
      while (overwritesecond >= 86400) {
        overwritesecond -= 86400;
        overwritemjd--;
      }
      setVDIFFrameMJD(header, overwritemjd);
      setVDIFFrameSecond(header, overwritesecond);
      setVDIFFrameNumber(header, overwritenumber);
      setVDIFFrameInvalid(header, 1);
      readbytes = fwrite(buffer, 1, framebytes, output); //write out the VDIF packet
      if(readbytes < framebytes) {
        fprintf(stderr, "Problem writing %lldth frame - only wrote %d bytes\n", framesread, readbytes);
        break;
      }
      frameswrote++;
      nextnumber++;
      if(nextnumber == framespersecond) {
        nextnumber = 0;
        nextsecond++;
        if(nextsecond == 86400) {
          nextsecond = 0;
          nextmjd++;
        }
      }
    }
    overwritenumber = framenumber + offsetnumber;
    overwritesecond = framesecond + offsetsecond;
    overwritemjd    = framemjd    + offsetmjd;
    while (overwritenumber < 0) {
      overwritenumber += framespersecond;
      overwritesecond++;
    }
    while (overwritenumber >= framespersecond) {
      overwritenumber -= framespersecond;
      overwritesecond--;
    }
    while (overwritesecond < 0) {
      overwritesecond += 86400;
      overwritemjd++;
    }
    while (overwritesecond >= 86400) {
      overwritesecond -= 86400;
      overwritemjd--;
    }
    header = (vdif_header*)buffer;
    setVDIFFrameMJD(header, overwritemjd);
    setVDIFFrameSecond(header, overwritesecond);
    setVDIFFrameNumber(header, overwritenumber);
    if(FORCE_VALID)
      frameinvalid = 0;
    setVDIFFrameInvalid(header, frameinvalid);
    readbytes = fwrite(buffer, 1, framebytes, output); //write out the VDIF packet
    if(readbytes < framebytes) {
      fprintf(stderr, "Problem writing %lldth frame - only wrote %d bytes\n", framesread, readbytes);
      break;
    }
    frameswrote++;
    nextnumber++;
    if(nextnumber == framespersecond) {
      nextnumber = 0;
      nextsecond++;
      if(nextsecond == 86400) {
        nextsecond = 0;
        nextmjd++;
      }
    }
  }

  printf("Read %lld and wrote %lld frames\n", framesread, frameswrote);
  fclose(input);
  fclose(output);

  return EXIT_SUCCESS;
}
Пример #3
0
int main(int argc, char **argv)
{
  //UDP socket related variables
  int serversock, status, socketnumber, receivedbytes, udpbufbytes, udpport;
  int currentframes;
  int done;
  vdif_header *header;
  struct sockaddr_in server;    /* Socket address */
  struct msghdr msg;
  struct iovec iov[1];
  char* iobase;

  //non-UDP socket related variables
  int skipbytesfront, skipbytesback;
  int perr, i;
  writethreaddata wtd;
  pthread_t writethread;
  long long framesread;

  //check the command line arguments
  if(argc < 3 || argc > 5)
  {
    usage();

    return EXIT_FAILURE;
  }

  //store some variables, allocate some arrays
  skipbytesfront   = 0;
  skipbytesback    = 0;
  udpport          = atoi(argv[1]);
  wtd.filename     = argv[2];
  wtd.udpframesize = -1;
  pthread_cond_init(&(wtd.writeinitcond), NULL);
  for(i=0;i<NUMSEGMENTS;++i) {
    wtd.receivebuffers[i] = (char*)malloc(BUFFERSEGBYTES);
    pthread_mutex_init(&(wtd.locks[i]), NULL);
  }
  if(argc > 3)
    skipbytesfront = atoi(argv[3]);
  if(argc > 4)
    skipbytesback = atoi(argv[4]);

  //lock the first segment, launch the writer thread on the last segment
  perr = pthread_mutex_lock(&(wtd.locks[0]));
  if(perr != 0) {
    fprintf(stderr, "Error doing initial mutex locking - aborting!\n");
    exit(EXIT_FAILURE);
  }
  perr = pthread_mutex_lock(&(wtd.locks[1]));
  if(perr != 0) {
    fprintf(stderr, "Error doing initial mutex locking - aborting!\n");
    exit(EXIT_FAILURE);
  }
  wtd.keepwriting = 1;
  wtd.ready       = 0;
  wtd.filename    = argv[2];
  //launch the writer thread
  perr = pthread_create(&writethread, NULL, launchNewWriteThread, (void *)(&wtd));
  if(perr != 0) {
    fprintf(stderr, "Error in launching writethread!!!");
    exit(EXIT_FAILURE);
  }
  while(!wtd.ready) {
    perr = pthread_cond_wait(&(wtd.writeinitcond), &(wtd.locks[1]));
    if (perr != 0)
      fprintf(stderr, "Error waiting on writethreadstarted condition!!!!");
  }
  perr = pthread_mutex_unlock(&(wtd.locks[1]));
  if(perr != 0) {
    fprintf(stderr, "Error doing initial mutex unlocking - aborting!\n");
    exit(EXIT_FAILURE);
  }

  //open the UDP socket
  udpbufbytes = 32*1024*1024;
  memset((char *)&server,0,sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons((unsigned short)udpport); /* Which port number to use */  
  serversock = socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP); 
  if (serversock==-1) {
    fprintf(stderr, "Cannot create UDP socket to read VDIF packets!\n");
    exit(EXIT_FAILURE);
  }
  status = setsockopt(serversock, SOL_SOCKET, SO_RCVBUF, (char *) &udpbufbytes, sizeof(udpbufbytes));
  if (status!=0) {
    fprintf(stderr, "Cannot setsocket SO_RCVBUF socket\n");
    close(serversock);
    exit(EXIT_FAILURE);
  } 
  status = bind(serversock, (struct sockaddr *)&server, sizeof(server));
  if (status!=0) {
    fprintf(stderr, "Cannot bind UDP socket\n");
    close(serversock);
    exit(EXIT_FAILURE);
  } 
  socketnumber = serversock;
  done = 0;
  wtd.fillsegment = 0;
  currentframes = 0;
  wtd.udpframesize = 0;

  //loop through reading frames
  printf("Finished initialising socket etc - about to start reading data\n");
  framesread = 0;
  iobase = wtd.receivebuffers[wtd.fillsegment];
  while(!done) {
    memset(&msg, 0, sizeof(msg));
    msg.msg_iov     = &iov[0];
    msg.msg_iovlen  = 1;
    iov[0].iov_base = iobase;
    iov[0].iov_len  = BUFFERSEGBYTES - currentframes*wtd.udpframesize;

    receivedbytes = recvmsg(socketnumber,&msg,MSG_WAITALL);
    if (receivedbytes==-1) { // Error reading socket
      if (errno == EINTR)
        continue;
      fprintf(stderr, "Some problem reading socket - aborting\n");
      done = 1;
      continue;
    } else if (receivedbytes==0) {  // Socket closed remotely
      fprintf(stderr, "Remote socket closed - aborting\n");
      done=1;
      continue;
    } else if (receivedbytes!=(wtd.udpframesize + skipbytesfront + skipbytesback)) {
      if(wtd.udpframesize != 0) { //not the first packet - something bad happened
        fprintf(stderr, "Received %d bytes vs framesize %d+%d+%d - aborting\n", receivedbytes, wtd.udpframesize, skipbytesfront, skipbytesback);
        done = 1;
        continue;
      }
      else { //was the first time - set framebytes
        wtd.udpframesize = receivedbytes - skipbytesfront - skipbytesback;
        wtd.framespersegment = BUFFERSEGBYTES/wtd.udpframesize;
	header = (vdif_header*)iobase;
        printf("Setting framebytes to %d, framespersegment to %d\n", wtd.udpframesize, wtd.framespersegment);
        printf("Starting from front, framebytes is %d, MJD is %d, seconds is %d, num channels is %d\n", 
               getVDIFFrameBytes(header),
               getVDIFFrameMJD(header),
               getVDIFFrameSecond(header),
               getVDIFNumChannels(header));
	header = (vdif_header*)(iobase+8);
        printf("Starting from 8 bytes in, framebytes is %d, MJD is %d, seconds is %d, num channels is %d\n",
               getVDIFFrameBytes(header),
               getVDIFFrameMJD(header),
               getVDIFFrameSecond(header),
               getVDIFNumChannels(header));
	header = (vdif_header*)(iobase+skipbytesfront);
        printf("Starting from 'skipbytesfront' in, framebytes is %d, MJD is %d, seconds is %d, num channels is %d\n",
               getVDIFFrameBytes(header),
               getVDIFFrameMJD(header),
               getVDIFFrameSecond(header),
               getVDIFNumChannels(header));
	printf("The hex value of that first byte is %llx\n", 
               *((unsigned long long*)(iobase)));
      }
    }
    //if we get here its ok
    if(skipbytesfront != 0) //move the stuff in memory if needed
      memmove(iobase, iobase+skipbytesfront, wtd.udpframesize);
    ++currentframes;
    ++framesread;
    iobase += wtd.udpframesize;
    if(currentframes >= wtd.framespersegment) {
      perr = pthread_mutex_lock(&(wtd.locks[(wtd.fillsegment+1)%NUMSEGMENTS]));
      if(perr != 0) {
        fprintf(stderr, "Main thread error locking segment %d - aborting!\n", (wtd.fillsegment+1)%NUMSEGMENTS);
        wtd.keepwriting = 0;
        done = 1;
      }
      perr = pthread_mutex_unlock(&(wtd.locks[wtd.fillsegment]));
      if(perr != 0) {
        fprintf(stderr, "Main thread error unlocking segment %d - aborting!\n", wtd.fillsegment);
        wtd.keepwriting = 0;
        done = 1;
      }
      wtd.fillsegment = (wtd.fillsegment+1)%NUMSEGMENTS;
      iobase = wtd.receivebuffers[wtd.fillsegment];
      currentframes = 0;
    }
  }

  pthread_mutex_unlock(&(wtd.locks[wtd.fillsegment]));
  printf("Read and wrote %lld frames\n", framesread);
  perr = pthread_join(writethread, NULL);
  if(perr != 0)
    fprintf(stderr, "Error in joining writethread!!!");

    return EXIT_SUCCESS;
}
Пример #4
0
int main(int argc, char **argv) {

    int c=0;
    int check = 0;
    int fix = 0;
    int verbose = 0;
    int test = 0;
    char *infile = NULL;
    FILE *input = stdin;
    FILE *output = stdout;
    int rate = 10000;
    
	if (argc > 1) {

		while ((c = getopt(argc, argv, "i:ctfr:v")) != -1) {
			switch(c) {
				case 'c':
					check = 1;
					break;
                    
				case 'f':
                    fix = 1;
                    break;
                case 'i':
                    infile = strdup(optarg);
                    input = fopen(infile,"r");
                    break;
                case 'r':
                    rate = atoi(optarg);
                    break;
                case 't':
                    test = 1;
                    break;
                case 'v':
                    verbose = 1;
                    break;
                default:
                    usage();
                    break;
                    
            }
        }
	}
	else {
		usage();
	}
	
    vdif_header vhdr;
    vdif_header last_vhdr;
    
	unsigned int frame = 0;
	int finished = 0;
	unsigned int rtn = 0;
    int got_first = 0;
    char *buffer = NULL;
    char *pattern = NULL;
    int second = 0;
    int last_second = 0;
    int last_frame=0;
    double MJD = 0.0;
    unsigned long frame_count = 0;
    int failed = 0;
    int diff_frame=0;
    int diff_seconds=0;
    
	if (check || fix || test) {
        
		while (!finished) {
            
			rtn = fread(&vhdr,32,1,input);
            
			if (feof(input) || rtn != 1) {
                if (verbose)
				fprintf(stderr,"chk_vdif finished:\n");
				finished = 1;
				continue;
			}
            else {
                frame_count = frame_count + 1;
            }
            if (got_first == 0) {
                if (check && verbose)
                    fprintf(stdout,"## Frame length: %d",vhdr.framelength8*8);
    
                buffer = malloc(vhdr.framelength8*8);
                assert(buffer);
                if (fix) {
                    // initialise to a mean zero 2bit offset binary pattern
                    pattern = malloc(vhdr.framelength8*8);
                    assert(pattern);
                    uint16_t mean_zero = 0xAA55;
                    size_t count = 0;
                    while (count < vhdr.framelength8*8) {
                        memcpy(pattern+count,&mean_zero,2);
                        count = count+2;
                    }
                    
                }
                got_first = 1;
            }
            else {
                
                frame = getVDIFFrameNumber(&vhdr);
                if (check && verbose)
                    fprintf(stdout,"Frame: %d\n",frame);
                second = getVDIFFrameSecond(&vhdr);
                if (check && verbose)
                    fprintf(stdout,"Second: %d\n", second) ;
                MJD = getVDIFDMJD(&vhdr,10000);
                if (check && verbose)
                    fprintf(stdout,"MJD %f\n",MJD);
                
                if (frame_count > 2) {
                    diff_frame = frame-last_frame;
                    if (diff_frame > 1) {
                        if (check)
                            fprintf(stderr,"Skipped a frame between %d and %d\n",last_frame,frame);
                        
                        failed = 1;
                    }
                    
                    diff_seconds = second-last_second;
                    if (diff_seconds > 1) {
                            if (check)
                                fprintf(stderr,"Skipped a second between %d and %d\n",last_second,second);
                        if (fix) {
                            while (diff_seconds > 1) {
                                nextVDIFHeader(&last_vhdr,rate);
                                fwrite(&last_vhdr,VDIF_HEADER_BYTES,1,stdout);
                                // repeat the last buffer
                                fwrite(&buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,output);
                                last_second = getVDIFFrameSecond(&last_vhdr);
                                diff_seconds = second-last_second;
                            }
                        }
                        if (fix == 0)
                            failed = 1;
                    }
                   
                    
                }
                
            }
            
            
            if (failed == 1) {
                fprintf(stdout,"failed\n");
                finished = 1;
                continue;
            }
            rtn = fread(buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,input);
            if (feof(input) || rtn != 1) {
                if (verbose)
                fprintf(stderr,"chk_vdif finished:\n");
                finished = 1;
                continue;
            }
            
            last_frame = frame;
            last_second = second;
            last_vhdr = vhdr;
            
            if (fix) {
                fwrite(&vhdr,VDIF_HEADER_BYTES,1,output);
                fwrite(buffer,(vhdr.framelength8*8-VDIF_HEADER_BYTES),1,output);
            }
			
		}
	}
    free(buffer);
    if (test && failed == 0) {
        fprintf(stdout,"passed\n");
    }
    if (infile) {
        fclose(input);
    }
}