
/* decode the TRK_CALIB_USED field and the part of ACQ_BUILD_INFO
   concerning the tracker, in the runheader packet */


#include <stdio.h>       /* include standard i/o library */
#include <stdlib.h>      /* include standard library */
#include <string.h>      /* include string library */
#include <unistd.h>      /* include unix standard library */
#include <sys/types.h>   /* */
#include <sys/stat.h>    /* */
#include <fcntl.h>       /* */
#include <errno.h>       /* error simbol definitions */
#include <time.h>        /* system time definitions */
#include <math.h>        /* math library */



void getbytes_(int *nbytes, char *buffer, int *p, unsigned int *value);

void runheaderpkt_(char *buffer,int *len, unsigned int *trk_calib_used_out, unsigned int *acq_build_info_out) {


/*     int i; */
/*     printf("runheaderpkt: \n %i \n %i \n",*len,*error); */
/*     for(i=0;i<*len;i++){ */
/* 	printf("%i %hhx \n",buff, *buff);//buffer e' integer*1 (=1 byte), quindi devo dividere per 4 con hh */
/* 	printf("%i %hhx \n \n",buffer, *buffer); */
/* //	printf("\n %i \n",i); */
/* 	buff++; */
/*     }; */
/*     printf(" \n"); */



    char *buff=buffer;
    int i1=1;
    int nbytes1=1;
    int nbytes2=2;
    int nbytes4=4;
    unsigned int out;

    unsigned int compilationtimestamp;
    char rm_acq_setting_mode;
    unsigned int obt_time_sync;
    unsigned int last_time_sync_info;
    char favourite_working_schedule;
    char effective_working_schedule;
    unsigned int prh_var_trigger_mode_a;
    unsigned int prh_var_trigger_mode_b;
    char rm_acq_after_calib;
    unsigned int trk_calib_used;
    unsigned int acq_build_info;
/* ground data format has changed at a certain moment: before acq_var_info was
   UINT8 (--> len=36), later UINT16 (--> len=37) */
    char acq_var_info_OLD;
    unsigned short int acq_var_info_NEW; //this will be the final flight data format
    char cal_dsp_mask;
    unsigned short int crcdsp;

    
	

    
    getbytes_(&nbytes4,buff,&i1,&out);
    compilationtimestamp=out;
    
    getbytes_(&nbytes1,buff,&i1,&out);
    rm_acq_setting_mode=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    obt_time_sync=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    last_time_sync_info=out;

    getbytes_(&nbytes1,buff,&i1,&out);
    favourite_working_schedule=out;

    getbytes_(&nbytes1,buff,&i1,&out);
    effective_working_schedule=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    prh_var_trigger_mode_a=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    prh_var_trigger_mode_b=out;

    getbytes_(&nbytes1,buff,&i1,&out);
    rm_acq_after_calib=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    trk_calib_used=out;

    getbytes_(&nbytes4,buff,&i1,&out);
    acq_build_info=out;
    
    if(*len==36) {
	getbytes_(&nbytes1,buff,&i1,&out);
	acq_var_info_OLD=out;
    }
    else if(*len==37) {
	getbytes_(&nbytes2,buff,&i1,&out);
	acq_var_info_NEW=out;
    }
    else {
	printf("runheaderpkt: error: wrong runheader packet length: %i \n",*len);
    }
    
    getbytes_(&nbytes1,buff,&i1,&out);
    cal_dsp_mask=out;

    getbytes_(&nbytes2,buff,&i1,&out);
    crcdsp=out;
	

/*     printf("compilationtimestamp: \t\t %u \n",compilationtimestamp); */
/*     printf("rm_acq_setting_mode:  \t\t %x \n",rm_acq_setting_mode); */
/*     printf("obt_time_sync: \t\t\t %u \n",obt_time_sync); */
/*     printf("last_time_sync_info: \t\t %u \n",last_time_sync_info); */
/*     printf("favourite_working_schedule: \t %x \n",favourite_working_schedule); */
/*     printf("effective_working_schedule: \t %x \n",effective_working_schedule); */
/*     printf("prh_var_trigger_mode_a: \t %u \n",prh_var_trigger_mode_a); */
/*     printf("prh_var_trigger_mode_b: \t %u \n",prh_var_trigger_mode_b); */
/*     printf("rm_acq_after_calib: \t\t %x \n",rm_acq_after_calib); */
/*     printf("trk_calib_used: \t\t %i \n",trk_calib_used); */
/*     printf("acq_build_info: \t\t %x \n",acq_build_info); */
/*     if(*len==36) { */
/* 	printf("acq_var_info_OLD: \t\t %x \n",acq_var_info_OLD); */
/*     } */
/*     else if(*len==37) { */
/* 	printf("acq_var_info_NEW: \t\t %x \n",acq_var_info_NEW); */
/*     } */
/*     printf("cal_dsp_mask: \t\t\t %x \n",cal_dsp_mask); */
/*     printf("crcdsp: \t\t\t\t %x \n",crcdsp); */


    *trk_calib_used_out=trk_calib_used;
    *acq_build_info_out=acq_build_info;

/*     printf("%x %x \n",*trk_calib_used_out,*acq_build_info_out); */


}
