Rtksvrthread

rtksvr.c

````
#ifdef WIN32
static DWORD WINAPI rtksvrthread(void *arg)
#else
static void *rtksvrthread(void *arg)
#endif
{
   rtksvr_t *svr=(rtksvr_t *)arg;
   obs_t obs;
   obsd_t data[MAXOBS*2];
   double tt;
   unsigned int tick,ticknmea;
   unsigned char *p,*q;
   int i,j,n,fobs[3]={0},cycle,cputime;

   tracet(3,"rtksvrthread:\n");

   svr->state=1; obs.data=data;
   svr->tick=tickget();
   ticknmea=svr->tick-1000;

   for (cycle=0;svr->state;cycle++) {
       tick=tickget();

       for (i=0;i<3;i++) {
           p=svr->buff[i]+svr->nb[i]; q=svr->buff[i]+svr->buffsize;

           /* read receiver raw/rtcm data from input stream */
           if ((n=strread(svr->stream+i,p,q-p))<=0) {
               continue;
           }
           /* write receiver raw/rtcm data to log stream */
           strwrite(svr->stream+i+5,p,n);
           svr->nb[i]+=n;

           /* save peek buffer */
           rtksvrlock(svr);
           n=n<svr->buffsize-svr->npb[i]?n:svr->buffsize-svr->npb[i];
           memcpy(svr->pbuf[i]+svr->npb[i],p,n);
           svr->npb[i]+=n;
           rtksvrunlock(svr);
       }
       for (i=0;i<3;i++) {
           if (svr->format[i]==STRFMT_SP3||svr->format[i]==STRFMT_RNXCLK) {
               /* decode download file */
               decodefile(svr,i);
           }
           else {
               /* decode receiver raw/rtcm data */
               fobs[i]=decoderaw(svr,i);
           }
       }
       for (i=0;i<fobs[0];i++) { /* for each rover observation data */
           obs.n=0;
           for (j=0;j<svr->obs[0][i].n&&obs.n<MAXOBS*2;j++) {
               obs.data[obs.n++]=svr->obs[0][i].data[j];
           }
           for (j=0;j<svr->obs[1][0].n&&obs.n<MAXOBS*2;j++) {
               obs.data[obs.n++]=svr->obs[1][0].data[j];
           }
           /* carrier phase bias correction */
           if (!strstr(svr->rtk.opt.pppopt,"-DIS_FCB")) {
               corr\_phase\_bias(obs.data,obs.n,&svr->nav);
           }
           /* rtk positioning */
           rtksvrlock(svr);
           rtkpos(&svr->rtk,obs.data,obs.n,&svr->nav);
           rtksvrunlock(svr);

           if (svr->rtk.sol.stat!=SOLQ_NONE) {

               /* adjust current time */
               tt=(int)(tickget()-tick)/1000.0+DTTOL;
               timeset(gpst2utc(timeadd(svr->rtk.sol.time,tt)));

               /* write solution */
               writesol(svr,i);
           }
           /* if cpu overload, inclement obs outage counter and break */
           if ((int)(tickget()-tick)>=svr->cycle) {
               svr->prcout+=fobs[0]-i-1;
#if 0 /* omitted v.2.4.1 */
               break;
#endif
           }
       }
       /* send null solution if no solution (1hz) */
       if (svr->rtk.sol.stat==SOLQ_NONE&&cycle%(1000/svr->cycle)==0) {
           writesol(svr,0);
       }
       /* send nmea request to base/nrtk input stream */
       if (svr->nmeacycle>0&&(int)(tick-ticknmea)>=svr->nmeacycle) {
           if (svr->stream[1].state==1) {
               if (svr->nmeareq==1) {
                   strsendnmea(svr->stream+1,svr->nmeapos);
               }
               else if (svr->nmeareq==2&&norm(svr->rtk.sol.rr,3)>0.0) {
                   strsendnmea(svr->stream+1,svr->rtk.sol.rr);
               }
           }
           ticknmea=tick;
       }
       if ((cputime=(int)(tickget()-tick))>0) svr->cputime=cputime;

       /* sleep until next cycle */
       sleepms(svr->cycle-cputime);
   }
   for (i=0;i<MAXSTRRTK;i++) strclose(svr->stream+i);
   for (i=0;i<3;i++) {
       svr->nb[i]=svr->npb[i]=0;
       free(svr->buff[i]); svr->buff[i]=NULL;
       free(svr->pbuf[i]); svr->pbuf[i]=NULL;
       free\_raw (svr->raw +i);
       free\_rtcm(svr->rtcm+i);
   }
   for (i=0;i<2;i++) {
       svr->nsb[i]=0;
       free(svr->sbuf[i]); svr->sbuf[i]=NULL;
   }
   return 0;
}

````