www.pudn.com > opengpssim.zip > ogsinout.c


/* ************************************************************************  
   *                                                                      * 
   *                          GPS Simulation                              * 
   *                                                                      * 
   * -------------------------------------------------------------------- * 
   *                                                                      * 
   *    Module:   ogsinout.cpp                                            * 
   *                                                                      * 
   *   Version:   0.1                                                     * 
   *                                                                      * 
   *      Date:   17.02.02                                                * 
   *                                                                      * 
   *    Author:   G. Beyerle                                              * 
   *                                                                      * 
   * -------------------------------------------------------------------- * 
   *                                                                      * 
   * Copyright (C) 2002  Georg Beyerle                                    * 
   *                                                                      * 
   * This program is free software; you can redistribute it and/or modify * 
   * it under the terms of the GNU General Public License as published by * 
   * the Free Software Foundation; either version 2 of the License, or    * 
   * (at your option) any later version.                                  * 
   *                                                                      * 
   * This program is distributed in the hope that it will be useful,      * 
   * but WITHOUT ANY WARRANTY; without even the implied warranty of       * 
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        * 
   * GNU General Public License for more details.                         * 
   *                                                                      * 
   * You should have received a copy of the GNU General Public License    * 
   * along with this program; if not, write to the Free Software          * 
   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            * 
   *                                                                      * 
   * -------------------------------------------------------------------- * 
   *                                                                      * 
   * The files 'gpsfuncs.cpp', 'gpsrcvr.cpp' are modified versions of     * 
   * the files with the same name from Clifford Kelley's OpenSourceGPS    * 
   * distribution. The unmodified files can be obtained from              * 
   *   http://www.home.earthlink.net/~cwkelley                            * 
   *                                                                      * 
   * -------------------------------------------------------------------- * 
   *                                                                      * 
   *                         I/O - Procedures                             * 
   *                                                                      * 
   ************************************************************************ */ 
 
/* ******************************* changes ******************************** 
 
   dd.mm.yy - 
 
   ************************************************************************ */ 
 
/* ------------------------------- includes ------------------------------- */ 
 
#include  
#include  
#include  
#include  
#include  
#include  
 
#include "ogsdefine.h" 
#include "ogsstructs.h" 
#include "ogsextern.h" 
#include "ogsprototypes.h" 
#include "ogslibrary.h" 
 
/* ------------------------------- defines -------------------------------- */ 
 
#define OUTPUTBUFLEN      500 
#define DATABUFLEN   5000000L    // 10 MB, size input data buffer 
 
/* ------------------------------- globals -------------------------------- */ 
 
// output buffers and pointers 
static long *OutputBufIQ[NOFSAT+1]; 
static int   OutputBufIQPos[NOFSAT+1]; 
 
static long *OutputBufCF[NOFSAT+1]; 
static int   OutputBufCFPos[NOFSAT+1]; 
 
// 32 file pointers for output files 
FILE *FpIQ[NOFSAT+1], 
     *FpCF[NOFSAT+1]; 
static FILE *FpNav[12]; 
FILE *FpIn;        // input file 
 
extern char *InputFileName; 
extern int OverWrite; 
 
char *Buffer;      // data input buffer 
long BufEnd;       // last position in data input buffer  
 
 
/* -------------------------- prototypen (global) ------------------------- */ 
 
/* ------------------------------ procedures ------------------------------ */ 
 
// 
//  open output files 
// 
static void open_iq_outfile( int prn) 
{ 
  char FileFmt[] = "iq-out-%02d"; 
  char File[sizeof( FileFmt)];      // d <= 2 digits! 
  char *buf; 
 
  assert( prn <= NOFSAT && prn >= 0); 
 
  sprintf( File, FileFmt, prn); 
 
  buf = conmalloc( strlen( OGSDataDir) + strlen( File) + 1); 
  strcpy( buf, OGSDataDir); 
  strcat( buf, File); 
 
  if ( !OverWrite) 
  { 
    FpIQ[prn] = fopen( buf, "rb"); 
    if ( FpIQ[prn]) 
    { 
      printf( "File '%s' already exists.\n", buf); 
      exit(0); 
    } 
  } 
 
  FpIQ[prn] = fopen( buf, "wb"); 
  if ( !FpIQ[prn]) 
  { 
    printf( "Error opening file '%s'.\n", buf); 
    exit(-1); 
  } 
 
  printf( "Opened file '%s'.\n", buf); 
  return; 
} 
 
static void open_carfrq_outfile( int prn) 
{ 
  char FileFmt[] = "carfrq-out-%02d"; 
  char File[sizeof( FileFmt)];      // d <= 2 digits! 
  char *buf; 
 
  assert( prn <= NOFSAT && prn >= 0); 
 
  sprintf( File, FileFmt, prn); 
 
  buf = conmalloc( strlen( OGSDataDir) + strlen( File) + 1); 
  strcpy( buf, OGSDataDir); 
  strcat( buf, File); 
 
  if ( !OverWrite) 
  { 
    FpCF[prn] = fopen( buf, "rb"); 
    if ( FpCF[prn]) 
    { 
      printf( "File '%s' already exists.\n", buf); 
      exit(0); 
    } 
  } 
 
  FpCF[prn] = fopen( buf, "wb"); 
  if ( !FpCF[prn]) 
  { 
    printf( "Error opening file '%s'.\n", buf); 
    exit(-1); 
  } 
 
  printf( "Opened file '%s'.\n", buf); 
  return; 
} 
 
// 
// 
// 
void close_open_files( void) 
{ 
  int i; 
   
  for (i=1; i<=NOFSAT; i++) 
  { 
    if ( FpIQ[i])  
      fclose( FpIQ[i]); 
    if ( FpCF[i])  
      fclose( FpCF[i]); 
  } 
 
  for ( i=0; i<12; i++) 
    if ( FpNav[i]) 
      fclose( FpNav[i]); 
 
  return; 
} 
 
// 
//  write data to files 
// 
void write_to_file_carfrq( long car_frq, int prn) 
{ 
  int pos; 
   
  if ( !OutputBufCF[prn]) 
  { 
    OutputBufCF[prn] = (long*) conmalloc( sizeof( long)*OUTPUTBUFLEN); 
    open_carfrq_outfile( prn); 
  }   
 
  if ( OutputBufCFPos[prn] >= OUTPUTBUFLEN) 
  { 
    assert( FpCF[prn]); 
    fwrite( OutputBufCF[prn], sizeof( long), OUTPUTBUFLEN, FpCF[prn]); 
    OutputBufCFPos[prn] = 0; 
  } 
 
  pos = OutputBufCFPos[prn]; 
 
  assert( pos < OUTPUTBUFLEN); 
 
  OutputBufCF[prn][pos]   = car_frq; 
  OutputBufCFPos[prn] += 1; 
 
  return; 
} 
 
void write_to_file_IQ( long Ip, long Qp, int prn) 
{ 
  int pos; 
   
  assert( prn <= NOFSAT && prn >= 0); 
   
  if ( !OutputBufIQ[prn]) 
  { 
    OutputBufIQ[prn] = (long*) conmalloc( 2*sizeof( long)*OUTPUTBUFLEN); 
    open_iq_outfile( prn); 
  }   
 
  if ( OutputBufIQPos[prn] >= 2*OUTPUTBUFLEN) 
  { 
    assert( FpIQ[prn]); 
    fwrite( OutputBufIQ[prn], sizeof( long), 2*OUTPUTBUFLEN, FpIQ[prn]); 
    OutputBufIQPos[prn] = 0; 
  } 
 
  pos = OutputBufIQPos[prn]; 
   
  assert( pos+1 < 2*OUTPUTBUFLEN); 
   
  OutputBufIQ[prn][pos]   = Ip; 
  OutputBufIQ[prn][pos+1] = Qp; 
  OutputBufIQPos[prn] += 2; 
 
  return; 
} 
 
// 
// read GPS raw data from file 
// 
void update_readbuffer( void) 
{ 
  assert( FpIn); 
 
  BufEnd = fread( Buffer, sizeof( char), DATABUFLEN, FpIn); 
  if ( BufEnd < DATABUFLEN) 
  { 
    fclose( FpIn); 
    FpIn = NULL; 
  } 
  return; 
} 
 
// 
// read GPS raw data from file 
// 
void init_inputbuffer( void) 
{ 
  long Len; 
  char infile[] = "ogsraw.dat"; 
  char *tmpstr, *ptr; 
 
  if ( InputFileName) 
    ptr = InputFileName; 
  else   
    ptr = infile; 
 
  tmpstr = conmalloc( strlen( OGSDataDir) + strlen( ptr) + 1); 
  strcpy( tmpstr, OGSDataDir); 
  strcat( tmpstr, ptr); 
   
//  printf( "raw file = %s\n", ptr); 
//  getchar(); 
 
// 
//  read GPS raw data from file (file written by OGSXMIT) 
// 
  FpIn = fopen( tmpstr, "rb"); 
  if ( !FpIn) 
  { 
    printf( "Error opening data file '%s'.\n", tmpstr); 
    exit(-1); 
  } 
 
  fseek( FpIn, 0L, SEEK_END); 
 
  Len = ftell( FpIn);   
 
  fseek( FpIn, 0L, SEEK_SET); 
 
  printf( "Reading %d bytes from data file\n '%s'.\n",  
    Len, tmpstr); 
 
//  if ( Verbose) 
//  { 
//    printf( "... may take a while... press ENTER\n"); 
//    getchar();   
//  } 
//  else 
//  { 
//    printf( "... may take a while...\n"); 
//  }   
 
  Buffer = conmalloc( DATABUFLEN * sizeof( char)); 
 
//  fread( Buffer, sizeof( char), BufferLen, Fp); 
  BufEnd = fread( Buffer, sizeof( char), DATABUFLEN, FpIn); 
 
//  fclose( Fp); 
//  printf( "Read %d bytes from data file '%s'.\n", Len, tmpstr); 
 
  if ( tmpstr) 
    free( tmpstr); 
 
  return; 
} 
 
// 
//  write nav data to file 
// 
void write_to_file_navbit( int ch) 
{ 
  int nof, ofs, one = 1, eno = -1; 
  char NavFileFmt[] = "nav-out-%02d"; 
  char NavFile[sizeof( NavFileFmt)];      // d <= 2 digits! 
  char *buf = NULL; 
 
  if ( !FpNav[ch]) 
  { 
    sprintf( NavFile, NavFileFmt, chan[ch].prn); 
    buf = conmalloc( strlen( OGSDataDir) + strlen( NavFile) + 1); 
    strcpy( buf, OGSDataDir); 
    strcat( buf, NavFile); 
 
    if ( !OverWrite) 
    { 
      FpNav[ch] = fopen( buf, "rb"); 
      if ( FpNav[ch]) 
      { 
        printf( "File '%s' already exists.\n", buf); 
        exit( 0); 
      } 
    } 
 
    FpNav[ch] = fopen( buf, "wb"); 
    if ( !FpNav) 
    { 
      printf( "Error opening file '%s'.\n", buf); 
      exit(-1); 
    } 
    printf( "Opened file '%s'.\n", buf); 
    if ( buf) 
      free( buf); 
  } 
 
  fwrite( ((chan[ch].bit==1)?&one:&eno), sizeof( char), 1, FpNav[ch]); 
     
  return; 
} 
 
/* ------------------------------ end of file ----------------------------- */