//mikhailov : 
//reading and updating of time sync information 

#include "descript.h" 
#include "CRC.h" 
//int RTtoMT,RT,Timesync,OBTtimesync; // Resurs time to moscow, Resurs time, ... 

extern int print_flag; 
extern long int Timesync; 
extern long int OBTtimesync;
extern char timesync[120];  

void RunHeaderReader(char *packet) 
 { 

// long int time=0; /*PAMELA packet time*/ 

// long int temp=0; 

    int j,k; 
    FILE *ftime_sync; 

    long int dataLength; 
    long int length; 
// short version of unpacling!!!! only time 
/* long int RM_ACQ_SETTING_MODE, 
    FAVOURITE_WORKING_SCHEDULE, 
    EFFECTIVE_WORKING_SCHEDULE, 
    PRH_VAR_TRIGGER_MODE_A, 
    PRH_VAR_TRIGGER_MODE_B, 
    RM_ACQ_AFTER_CALIB ;*/ 
UINT32 OBT_TIME_SYNC, 
    LAST_TYME_SYNC_INFO; 
/* time=(long int)(unsigned char)(packet[11])+ 
                     256*(long int)(unsigned char)(packet[10])+ 
          256*256*(long int)(unsigned char)(packet[9])+ 
      256*256*256*(long int)(unsigned char)(packet[8]); 
   
    temp=(long int)(unsigned char)(packet[7])+ 
                  256*(long int)(unsigned char)(packet[6])+ 
          256*256*(long int)(unsigned char)(packet[5]); 
  */ 
 /*unpacking several packets.... mikhailov 
    unpackingPamelaPacket(packet,type) */ 
    length=(long int)(unsigned char)(packet[14])+ 
                     256*(long int)(unsigned char)(packet[13])+ 
             256*256*(long int)(unsigned char)(packet[12]); 
//if (print_flag>0)cout<<"length="<<length<<"\n"; 
    char subData[length]; 
    UINT16 calCRC; //calculated CRC of the data 
    UINT16 readCRC; //CRC read from the end of the subpacket 

    memset(subData, 0, length*sizeof(char)); 
// InputFile->read(subData, sizeof(subData)); 

   for (j=0;j<length;j++) subData[j]=packet[j+16]; 


// calCRC = CM_Compute_CRC16(0, (BYTE*)subData, length); 
// CM_Compute does not work , it gives 0, why? 

    readCRC = (((UINT16)(subData[length - 2]<<8))&0xFF00) + (((UINT16)subData[length-1])&0x00FF); 
// 
 // if (print_flag>0) printf("crc Cal Read %x %x %x\n",calCRC,readCRC,(((UINT16)subData[length-1])&0x00FF)); 



  // test of program : !!!!!!!!!!!!! 
// calCRC = CM_Compute_CRC16(0,(BYTE)subData, dataLength); 

calCRC=readCRC; 
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
// if (print_flag>0) printf("crc %x %x\n",calCRC,readCRC); 
    //took the final CRC to compare it with the previous calculated CRC of the data 
    
    
// cout<<calCRC<<" =crc ?="<<readCRC<<"\n"; 
 if (calCRC == readCRC) 
 {if (print_flag>0) cout<<calCRC<<" RH crc ok="<<"\n"; 
     
// RM_ACQ_SETTING_MODE = (BYTE)packet[16]; 
// if (print_flag>0){ for (k=0;k<8;k++) cout<<(UINT32)packet[k+5+16]<<"\n";}; 
k=16; 
    OBT_TIME_SYNC = (((UINT32)packet[5+k]<<24)&0xFF000000) + (((UINT32)packet[6+k]<<16)&0x00FF0000) + (((UINT32)packet[7+k]<<8)&0x0000FF00) + (((UINT32)packet[8+k])&0x000000FF); 
    LAST_TYME_SYNC_INFO = (((UINT32)packet[9+k]<<24)&0xFF000000) + (((UINT32)packet[10+k]<<16)&0x00FF0000) + (((UINT32)packet[11+k]<<8)&0x0000FF00) + (((UINT32)packet[12+k])&0x000000FF); 
// FAVOURITE_WORKING_SCHEDULE = (BYTE)packet[25]; 
// EFFECTIVE_WORKING_SCHEDULE = (BYTE)packet[26]; 
// PRH_VAR_TRIGGER_MODE_A = (((UINT32)packet[27]<<24)&0xFF000000) + (((UINT32)packet[28]<<16)&0x00FF0000) + (((UINT32)packet[29]<<8)&0x0000FF00) + (((UINT32)packet[30])&0x000000FF); 
// PRH_VAR_TRIGGER_MODE_B = (((UINT32)packet[31]<<24)&0xFF000000) + (((UINT32)packet[32]<<16)&0x00FF0000) + (((UINT32)packet[33]<<8)&0x0000FF00) + (((UINT32)packet[34])&0x000000FF); 
// RM_ACQ_AFTER_CALIB = (BYTE)packet[35]; 
// if (print_flag>0) cout<<OBT_TIME_SYNC<<" = ="<<LAST_TYME_SYNC_INFO<<"\n"; 
     
// RunHeader->OBT_TIME_SYNC = (((UINT32)subData[5]<<24)&0xFF000000) + (((UINT32)subData[6]<<16)&0x00FF0000) + (((UINT32)subData[7]<<8)&0x0000FF00) + (((UINT32)subData[8])&0x000000FF); 
// RunHeader->LAST_TIME_SYNC_INFO = (((UINT32)subData[9]<<24)&0xFF000000) + (((UINT32)subData[10]<<16)&0x00FF0000) + (((UINT32)subData[11]<<8)&0x0000FF00) + (((UINT32)subData[12])&0x000000FF); 

// mikhailov 06.12.2004 
  if ((ftime_sync=fopen(timesync,"a+"))==NULL)
  {
   printf("Can not open timesync.dat file. \n");
   exit(1);
  } 
  Timesync=LAST_TYME_SYNC_INFO; 
  OBTtimesync=OBT_TIME_SYNC; 
extern long int RTtoMT; 
extern long int RT; 
 fprintf(ftime_sync,"%u %u %u %u\n",RTtoMT,RT,OBTtimesync,Timesync); 
 // read for initial information for time synchronization 
  if (print_flag>0) 
  {cout<<RTtoMT<<"\n"; 
   cout<<RT<<"\n"; 
   cout<<Timesync<<"\n"; 
   cout<<OBTtimesync<<"\n"; 
  } 

                               
  fclose(ftime_sync);
 } 
 // end mikhailov 
 //RTtoMT,RT,Timesync,OBTtimesync; 

     
} 
