///////////////PAMELA PACKETS READER///////////////////////////////// #include "descript.h" #include "flag.h" #include "CRC.h" short int CRC_H(char [], char []); void verify(char fni[], char inf[], char log[], char pam[], char fno[], char path[], char name_ini[], char name_in[], int packet_type[], float IndexQ, int r_counter, int real_route[]) { FILE *fo_log, *fo_inf, *fo_work, *fo_miss, *fo_row; time_t seconds; struct tm *timeinfo; char time_year[5], time_month[5], time_day[5]; char data_time[80]; char cirquit_no[10], route_no[80], name_path[80], name_cln2[80]; bool file_ready, file_deleted, file_archived, file_proces; int flag_to_transmit=0; int flag_sec_trans=0; /*Second transmission flag*/ int num_rows=0; int current_route=0; char str_route[10]; char str_file[80]; char ch; char miss_file[80]; char Name_Buf[9]; /*Name buffer*/ char Name_Buf_add[9]; /*Header buffer*/ char buff[16]; char Name_Buf_old[9]; /*Old name buffer*/ char Name_Buf_add_old[9]; /*Old header buffer*/ short int CalcCheckSum=0; int num_type=0; /*Packet type number*/ int num_type1=0; int num_type2=0; int stop=0; int Hex[3]; int Byte_adr1[4]; /*Bytes for Packet begin packing*/ int Byte_adr2[4]; /*Bytes for Packet end packing*/ int packet_counter[NumberPack+1]; /*Counter for each packet type*/ int count_name_cor=0; /*Counter for packet with corrected name*/ int er_pack_count=0; /*Error packet counter*/ int count_miss=0; /*Missed packet counter*/ int P_er=0; /*Number of packet with counter error*/ int flag_CRC_H[3]; /*Flag for CRC header error*/ int flag_pack[3]; /*Flag for error in packet counter*/ int flag_len[3]; /*Flag for PAMELA packet length error*/ int flag_len_weak[3]; /*Suspicion flag for PAMELA packet length error*/ int flag_name[3]; /*Flag for name error*/ int flag_type[3]; /*Flag for packet type error*/ int flag_time[3]; /*Flag for time error*/ int flag_CRC_cor[3]; /*Flag for CRC error correction*/ int CRC_bad_pack[3]; /*Current header CRC bad packet number*/ int CRC_Header[3]; /*CRC in header for previous packets*/ int Mis_group=0; /*Number of missed packet group*/ short int flags=0; /*Number for all errors*/ long int tmpP=0; /*Temporary PAMELA packet number*/ long long int icount=0; /*Number of bytes in identified packets*/ long long int icount_total=0; /*Total number of bytes*/ long int icount_int=0; /*Entire part of total file size in Kbytes*/ long int icount_frac=0; /*Fractional part of total file size*/ long int N1=0; /*Number of identified PAMELA packets*/ long int numP[3]; /*Packet numbers for previous packets*/ long int LenP[3]; /*Packet length for previous packets*/ unsigned long int time_p[3]; /*Packet time*/ long int pack_length=0; /*PAMELA packet length*/ long long int adr_first[3]; /*Start displacement from file begin*/ long long int adr_last[3]; /*Finish displacement from file begin*/ long int temp[2]; char *buffer=new char[Nmax]; /*Buffer for PAMELA current packet*/ int FLAG_BIG=0; /*Flag for large packet*/ unsigned long int k=0, j=0; int i; //initialization , all =0 float process, transmit; int output; char str[80]; FILE *fpar; fpar=fopen("/home/pamelaprod/rawreader/bin/parameters.dat","r"); fscanf(fpar,"%f %s %s %s",&process,str,str,str); fscanf(fpar,"%f %s %s %s",&transmit,str,str,str); fscanf(fpar,"%i",&output); fclose(fpar); stringstream oss, oss1; TSQLServer *sqlServer = TSQLServer::Connect("mysql://pamela_1.ntsomz.ru:3306/rawreader", "pamelaprod", "rawprod"); oss.str(""); oss<<"select * from FILES1"; TSQLResult *res1=sqlServer->Query(oss.str().c_str()); num_rows=res1->GetRowCount(); if (num_rows>0) { oss.str(""); oss<<"select max(my_route) from FILES1"; TSQLResult *res2=sqlServer->Query(oss.str().c_str()); TSQLRow *row=res2->Next(); strcpy(str_route,row->GetField(0)); strcpy(str_file,inf); str_file[strlen(inf)-9]='\0'; strcat(str_file,"_tmp.dat"); fo_row=fopen(str_file,"w+"); fprintf(fo_row,"%s",str_route); fseek(fo_row,0,0); fscanf(fo_row,"%i",¤t_route); current_route+=1; fclose(fo_row); remove(str_file); } else { current_route=1; } for (i=0; iLimit) {adr_last[2]=adr_first[2]+Limit;} //end byte for previous packet /**************PAMELA PACKET LENGTH ERROR ANALYSIS*****************/ if ( (flag_CRC_H[2]==1)&&(GLOBAL_LEN_ER>0)&& ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16)) ||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) ) {flag_len[2]=1; // cout<<"pack num "<0)&& ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16))||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) ) {flag_len_weak[2]=1; // cout<<"weak length error; pack num "<0)&&(flag_len[2]==1)) { //Write data in file "xxx_mmm_nnnnn_cln2.pam" for (i=0;i0)&&(flag_len[2]!=1)&&(flag_len_weak[2]!=1)) { //Write data in file "xxx_mmm_nnnnn_cln2.pam" for (i=0;i0)&&(flag_len[2]!=1)&&(flag_len_weak[2]==1)) { //Write data in file "xxx_mmm_nnnnn_cln2.pam" for (i=0;i=0)) { for (i=0;i=1) {Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i))); temp[1]=temp[1]-(long int)pow(256.,3-i)* (int)(temp[1]/(long int)(pow(256.,3-i)));}} //Write previous packet end displacement in xxx_mmm_nnnnn_cln2.log file for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i])); } /*********************reading remaining header********************/ fin.read(Name_Buf_add, 8); if (fin.eof()) break; icount+=8; icount_total+=8; for (i=0;i<8;i++) buffer[i+8]=Name_Buf_add[i]; //packet length pack_length=(long int)(unsigned char)(Name_Buf_add[6])+ 256*(long int)(unsigned char)(Name_Buf_add[5])+ 256*256*(long int)(unsigned char)(Name_Buf_add[4]); /***********PACKET HEADER DATA ANALYSIS****************************/ switch (N1) { case 1:{ LenP[2]=pack_length; numP[2]=(long int)(unsigned char)(Name_Buf[7])+ 256*(long int)(unsigned char)(Name_Buf[6])+ 256*256*(long int)(unsigned char)(Name_Buf[5]); time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+ 256*(long int)(unsigned char)(Name_Buf_add[2])+ 256*256*(long int)(unsigned char)(Name_Buf_add[1])+ 256*256*256*(long int)(unsigned char)(Name_Buf_add[0]); CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]); adr_first[2]=icount-15; CRC_bad_pack[2]=0; flag_CRC_H[2]=0; flag_pack[2]=0; flag_name[2]=0; flag_type[2]=0; flag_time[2]=0; flag_CRC_cor[2]=0; for (i=0; i<2; i++) {numP[i]=numP[2]; time_p[i]=time_p[2]; CRC_Header[i]=CRC_Header[2]; LenP[i]=LenP[2]; adr_first[i]=adr_first[2]; CRC_bad_pack[i]=CRC_bad_pack[2]; flag_CRC_H[i]=flag_CRC_H[2]; flag_pack[i]=flag_pack[2]; flag_name[i]=flag_name[2]; flag_type[i]=flag_type[2]; flag_time[i]=flag_time[2]; flag_CRC_cor[i]=flag_CRC_cor[2];} break; } default:{ for (i=0; i<2; i++) {numP[i]=numP[i+1]; time_p[i]=time_p[i+1]; CRC_Header[i]=CRC_Header[i+1]; CRC_bad_pack[i]=CRC_bad_pack[i+1]; LenP[i]=LenP[i+1]; adr_first[i]=adr_first[i+1]; adr_last[i]=adr_last[i+1]; flag_CRC_H[i]=flag_CRC_H[i+1]; flag_len[i]=flag_len[i+1]; flag_len_weak[i]=flag_len_weak[i+1]; flag_pack[i]=flag_pack[i+1]; flag_name[i]=flag_name[i+1]; flag_type[i]=flag_type[i+1]; flag_time[i]=flag_time[i+1]; flag_CRC_cor[i]=flag_CRC_cor[i+1];} numP[2]=(long int)(unsigned char)(Name_Buf[7])+ 256*(long int)(unsigned char)(Name_Buf[6])+ 256*256*(long int)(unsigned char)(Name_Buf[5]); time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+ 256*(long int)(unsigned char)(Name_Buf_add[2])+ 256*256*(long int)(unsigned char)(Name_Buf_add[1])+ 256*256*256*(long int)(unsigned char)(Name_Buf_add[0]); CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]); CRC_bad_pack[2]=0; adr_first[2]=icount-15; flag_CRC_H[2]=0; flag_len[2]=0; flag_len_weak[2]=0; flag_pack[2]=0; flag_name[2]=0; flag_type[2]=0; flag_time[2]=0; flag_CRC_cor[2]=0; LenP[2]=pack_length; } } /********packet header CRC error analysis**************************/ // CalcCheckSum=CRC_H(Name_Buf, Name_Buf_add); for (i=0; i<8; i++) {buff[i]=Name_Buf[i]; buff[i+8]=Name_Buf_add[i];} CalcCheckSum=(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15); if (CalcCheckSum!=(short int)(unsigned char)(Name_Buf_add[7])) {cerr<<"checksum error "<(CalcCheckSum)<< " CRC="<((int)(unsigned char)(Name_Buf_add[7]))<<"\n"; CRC_bad_pack[2]=numP[2]; flag_CRC_H[2]=1;} /*********packet counter error analysis****************************/ if ((numP[2]!=(numP[1]+1))&&(N1!=1)&& (flag_len[1]==0)&&(CRC_bad_pack[2]==numP[2])) flag_pack[2]=1; if ((numP[1]!=(numP[0]+1))&&(numP[1]!=(numP[2]-1))&&(N1!=1)&& (CRC_bad_pack[1]==numP[1])) {P_er=N1-1; flag_pack[1]=1; //Correct error packet number// tmpP=numP[0]+1; for (i=0;i<3;i++) Hex[i]=0; for (i=0;i<3;i++) {if (tmpP/(long int) (pow(256.,2-i))>=1) {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i))); tmpP=tmpP-(long int)(pow(256.,2-i)* (int)(tmpP/(long int)(pow(256.,2-i)) ));}} for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i])); if (CRC_Header[1]==(int)(CRC_H(Name_Buf_old, Name_Buf_add_old))) {flag_CRC_cor[1]=1;} tmpP=numP[2]-1; for (i=0;i<3;i++) Hex[i]=0; for (i=0;i<3;i++) {if (tmpP/(long int)(pow(256.,2-i))>=1) {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i))); tmpP=tmpP-(long int)pow(256.,2-i)* (int)(tmpP/(long int)(pow(256.,2-i)));}} for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i])); if (CRC_Header[1]==(int)(CRC_H(Name_Buf_old, Name_Buf_add_old))) {flag_CRC_cor[1]=1;} } /*********************Missing packet analysis**********************/ /**packet numbers: 10 11 (12 15 19) 20 ****************************/ /***********missed packets: 16 17 18 ********************************/ if ((numP[1]>numP[0]+1)&&(numP[1]=numP[0]+1)&&(numP[1]=numP[0]+2)&&(flag_pack[2]==0)&&(flag_pack[0]==0)&& (flag_time[0]==0)&&(time_p[2]>time_p[0])&& ((time_p[1]time_p[2]))) {flag_time[1]=1;} /**************Error number calculation****************************/ if (N1!=1) {flags=(short int)((float)(flag_len_weak[1])+ pow((float)(2*flag_CRC_H[1]),1)+ pow((float)(2*flag_len[1]),2)+ pow((float)(2*flag_pack[1]),3)+ pow((float)(2*flag_name[1]),4)+ pow((float)(2*flag_type[1]),5)+ pow((float)(2*flag_time[1]),6)+ pow((float)(2*flag_CRC_cor[1]),7)); if (flags!=0) er_pack_count+=1; //Write error number in log and work.dat files fprintf(fo_log,"%1c",char(flags)); // fprintf(fo_work," error num.=%i CRC=%i Calc=%i\n", // flags,bitset<8>(CRC_Header[1]), // (int)(CRC_H(Name_Buf_old, Name_Buf_add_old))); } /***********************NAME ERROR*********************************/ if (((int)(unsigned char)Name_Buf[0]!=CODE_BYTE1)|| ((int)(unsigned char)Name_Buf[1]!=CODE_BYTE2)|| ((int)(unsigned char)Name_Buf[2]!=CODE_BYTE3)) { Name_Buf[0]=(char)(CODE_BYTE1); Name_Buf[1]=(char)(CODE_BYTE2); Name_Buf[2]=(char)(CODE_BYTE3); flag_name[2]=1; count_name_cor+=1; buffer[0]=(char)(CODE_BYTE1); buffer[1]=(char)(CODE_BYTE2); buffer[2]=(char)(CODE_BYTE3); if (CRC_Header[2]==(int)(CRC_H(Name_Buf, Name_Buf_add))) flag_CRC_cor[2]=1; } /***********************PACKET TYPE ERROR**************************/ if ((num_type==0)||(num_type1==0)||(num_type2==0)) flag_type[2]=1; /******************PAMELA BEGIN PACKET DISPLACEMENT****************/ temp[0]=adr_first[2]; for (i=0;i<4;i++) Byte_adr1[i]=0; for (i=0;i<4;i++) {if (temp[0]/(long int)(pow(256.,3-i))>=1) {Byte_adr1[3-i]=(int)(temp[0]/(long int)(pow(256.,3-i))); temp[0]=temp[0]-(long int)pow(256.,3-i)* (int)(temp[0]/(long int)(pow(256.,3-i)));} } //Write packet type and begin displacement in log and work.dat files // fprintf(fo_work,"Type %i: num. %i: first=%i:", // num_type, numP[2], adr_first[2]); fprintf(fo_log,"%1c",char(num_type)); for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr1[i])); /**************PAMELA PACKET OLD BUFFER***************************/ for (i=0;i<8;i++) {Name_Buf_old[i]=Name_Buf[i];Name_Buf_add_old[i]=Name_Buf_add[i];} /**********************data reading********************************/ fin.seekg(-1,ios::cur); icount-=1; icount_total-=1; fin.read(Name_Buf,8); if (fin.eof()) break; icount+=8; icount_total+=8; for (i=0;i<7;i++) buffer[i+16]=Name_Buf[i+1]; } /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /**********************data reading********************************/ fin.get(ch); if (fin.eof()) {break;} icount_total+=1; // if (fmod( double(icount_total), double(100000000.))==double(0.)) {cout<<"Mbytes="<0)&&(FLAG_BIG==0)) icount+=1; for (i=0;i<7;i++) Name_Buf[i]=Name_Buf[i+1]; Name_Buf[7]=ch; /*for(i=0;i<8;i++) printf("%x",(int)(unsigned char)Name_Buf[i]); printf("\n");*/ if ((icount-adr_first[2]-1)64)||(adr_last[2]-adr_first[2]+1-LenP[2]-16<0)) flag_len[2]=1; if ( (flag_len[2]!=1)&&(flag_CRC_H[2]!=1) ) {adr_last[2]=adr_first[2]+LenP[2]+15;} temp[1]=adr_last[2]; for (i=0;i<4;i++) Byte_adr2[i]=0; for (i=0;i<4;i++) {if (temp[1]/(long int)(pow(256.,3-i))>=1) {Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i))); temp[1]=temp[1]-(long int)pow(256.,3-i)* (int)(temp[1]/(long int)(pow(256.,3-i)));}} /*********PAMELA LAST PACKET LENGTH ERROR ANALYSIS*****************/ // if ((adr_last[2]-adr_first[2]+1)!=(LenP[2]+(64-LenP[2]%64))) // {flag_len[2]=1;} //Write last packet end displacement in log and work.dat files for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i])); // fprintf(fo_work," last=%i",adr_last[2]); //Write error number in log and work.dat files flags=(short int)(pow((float)(2*flag_CRC_H[2]),1)+ pow((float)(2*flag_len[2]),2)+ pow((float)(2*flag_pack[2]),3)+ pow((float)(2*flag_name[2]),4)+ pow((float)(2*flag_type[2]),5)+ pow((float)(2*flag_time[2]),6)+ pow((float)(2*flag_CRC_cor[2]),7)); if ((flags!=0)&&(N1>0)) er_pack_count+=1; fprintf(fo_log,"%1c",char(flags)); // fprintf(fo_work," error num.=%i CRC=%i Calc=%i\n", // flags,bitset<8>(CRC_Header[2]), // (int)(CRC_H(Name_Buf, Name_Buf_add))); //Write inf file fprintf(fo_inf,"file name=%s\n",name_in); fprintf(fo_inf,"%lli bytes\n",icount_total); icount_int = (int)(icount_total/1024); icount_frac= icount_total%1024; fprintf(fo_inf,"file size= %i,%i kByte \n", icount_int, icount_frac); fprintf(fo_inf,"number of identified packets= %i\n", N1); fprintf(fo_inf,"number of PAMELA missed packets= %i\n", count_miss); fprintf(fo_inf,"number of PAMELA packets with error= %i\n", er_pack_count); fprintf(fo_inf,"number of PAMELA packets with corrected name= %i\n", count_name_cor); fprintf(fo_inf,"number of PAMELA packets with non identified type"); fprintf(fo_inf," =%i\n",packet_counter[0]); for (i=1;iprocess) file_proces=1; if (IndexQ>transmit) {flag_to_transmit=1;} else {flag_to_transmit=0;} oss.str(""); oss << "INSERT INTO FILES (cirquit_n, route_n, counter, qual_index, ready, deleted, date," << " archived, path, cln2_name, proces_flag, transit_flag)" << " VALUES ('"<< cirquit_no << "','"<< route_no << "','"<Query(oss.str().c_str()); flag_sec_trans=0; oss1.str(""); oss1 <<"INSERT INTO FILES1 (my_route, raw_filename, pam_size, cirquit_n, route_n, route_real," <<" counter, board_time, Num_all, Num_macro, flag_twice)" <<" VALUES ('"<Query(oss1.str().c_str()); // if (conn->Query(oss.str().c_str()) == 0) // { // printf("ERROR!!!"); // } delete [] buffer; /***************************************************************/ fin.close(); fclose(fo_work); fclose(fo_log); fclose(fo_inf); fo_pam.close(); fclose(fo_miss); }