///////////////PAMELA PACKETS READER///////////////////////////////// #include "descript.h" #include "flag.h" #include "CRC.h" short int CRC_H(char [], char []); extern char MYSQL[120]; extern char database[120]; extern float YODA_flag; extern float transmit_flag; extern int print_flag; 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_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]; char connection[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, ttt[80]; 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_CRC=0; /*CRC error packet counter*/ int er_pack_Len=0; /*Length error packet counter*/ int er_pack_count=0; /*Errors counter*/ int count_miss=0; /*Missed packet counter*/ int count_sec_miss=0; 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 PackType[3]; /*Packet type for previous packets*/ long int LenP[3]; /*Packet length for previous packets*/ unsigned long int time_p[3]; /*Packet time*/ unsigned long int time_max=0; unsigned long int time_min=0; 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 ratioCln1Cln2; int output; char str[80]; stringstream oss, oss1; strcpy(connection,MYSQL); strcat(connection,database); TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod"); if (sqlServer==0) {cerr<<"can not connect with MYSQL server\n"; exit(1);} 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; i0) && (fmod((float)(tmpP),(float)(1000.))==0.)&&(tmpP!=0)) cout<<"verifying "<Limit) {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; er_pack_Len+=1; if (print_flag>0) {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; if (print_flag>0) {cout<<"weak length error; pack num "<0)&&(flag_len[2]==1)) { //Write data in file "nnnnn_mmm_xxx_cln2.pam" for (i=0;i0)&&(flag_len[2]!=1)&&(flag_len_weak[2]!=1)) { //Write data in file "nnnnn_mmm_xxx_cln2.pam" for (i=0;i0)&&(flag_len[2]!=1)&&(flag_len_weak[2]==1)) { //Write data in file "nnnnn_mmm_xxx_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 nnnnn_mmm_xxx_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]); time_max=time_p[2]; time_min=time_p[2]; 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]; PackType[i]=PackType[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]; PackType[i]=PackType[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]); PackType[2]=num_type-1; 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])) {er_pack_CRC+=1; CRC_bad_pack[2]=numP[2]; flag_CRC_H[2]=1; fo_work<<"CRC error : PAMELA packet="<=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])); for (i=0; i<8; i++) {buff[i]=Name_Buf_old[i]; buff[i+8]=Name_Buf_add_old[i];} if (CRC_Header[1]==(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15)) {flag_CRC_cor[1]=1;} //Correct error packet number// 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])); for (i=0; i<8; i++) {buff[i]=Name_Buf_old[i]; buff[i+8]=Name_Buf_add_old[i];} if (CRC_Header[1]==(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15)) {flag_CRC_cor[1]=1;} } /*********************Missing packet analysis**********************/ /**packet numbers: 21 (22 17 18) 19 ******************************/ /***missed packets: 21 20 19 18 17 ******************************/ 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; fo_work<<"Time error : PAMELA packet="<=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 file 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 ((print_flag>0) && 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; if ((icount-adr_first[2]-1) Nmax="<0) {cout<<"too long packet "<Limit) {adr_last[2]=adr_first[2]+Limit;} //end byte for previous packet if ((adr_last[2]-adr_first[2]+1-LenP[2]-16>64)||(adr_last[2]-adr_first[2]+1-LenP[2]-16<0)) { flag_len[2]=1; er_pack_Len+=1; fo_work<<"Length error : packet number="<=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 last packet end displacement in log file for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i])); //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)); //Write error number in work.dat file if (flags!=0) fo_work<<"PAMELA packet="<YODA_flag) file_proces=1; if (IndexQ>transmit_flag) {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 << "','"<0) cout<Query(oss.str().c_str()); flag_sec_trans=0; oss1.str(""); oss1 <<"INSERT INTO FILES1 (my_route, raw_filename, pam_size, ratioCln1Cln2, cirquit_n, route_n, route_real," <<" counter, board_time, Num_all, Num_macro, flag_twice, OBT_first, OBT_last)" <<" VALUES ('"<0) cout<Query(oss1.str().c_str()); // if (conn->Query(oss.str().c_str()) == 0) // { // printf("ERROR!!!"); // } delete [] buffer; /***************************************************************/ fin.close(); fo_work.close(); fclose(fo_log); fclose(fo_inf); fo_pam.close(); fclose(fo_miss); }