| 1 | ///////////////PAMELA PACKETS READER///////////////////////////////// | 
| 2 | #include "descript.h" | 
| 3 |  | 
| 4 | #include "CRC.h" | 
| 5 |  | 
| 6 | short int CRC_H(char [], char []); | 
| 7 |  | 
| 8 | void verify(char fni[], char inf[], char log[], char pam[], | 
| 9 | char fno[], char path[], char name_ini[], char name_in[], int packet_type[], | 
| 10 | int GLOBAL_LEN_ER, float IndexQ, int r_counter, int real_route[]) | 
| 11 | { | 
| 12 | FILE *fo_log, *fo_inf, *fo_work, *fo_miss, *fo_row; | 
| 13 |  | 
| 14 | time_t seconds; | 
| 15 | struct tm *timeinfo; | 
| 16 | char time_year[5], time_month[5], time_day[5]; | 
| 17 | char data_time[80]; | 
| 18 | char cirquit_no[10], route_no[80], name_path[80], name_cln2[80]; | 
| 19 | bool file_ready, file_deleted, file_archived, file_proces; | 
| 20 | int  flag_to_transmit=0; | 
| 21 | int flag_sec_trans=0;             /*Second transmission flag*/ | 
| 22 | int num_rows=0; | 
| 23 | int current_route=0; | 
| 24 |  | 
| 25 | char str_route[10]; | 
| 26 | char str_file[80]; | 
| 27 |  | 
| 28 | char ch; | 
| 29 | char miss_file[80]; | 
| 30 | char Name_Buf[9];                 /*Name buffer*/ | 
| 31 | char Name_Buf_add[9];             /*Header buffer*/ | 
| 32 | char buff[16]; | 
| 33 | char Name_Buf_old[9];             /*Old name buffer*/ | 
| 34 | char Name_Buf_add_old[9];         /*Old header buffer*/ | 
| 35 |  | 
| 36 | short int CalcCheckSum=0; | 
| 37 | int num_type=0;                   /*Packet type number*/ | 
| 38 | int num_type1=0; | 
| 39 | int num_type2=0; | 
| 40 | int stop=0; | 
| 41 | int Hex[3]; | 
| 42 | int Byte_adr1[4];                 /*Bytes for Packet begin packing*/ | 
| 43 | int Byte_adr2[4];                 /*Bytes for Packet end packing*/ | 
| 44 | int packet_counter[NumberPack+1]; /*Counter for each packet type*/ | 
| 45 | int count_name_cor=0;             /*Counter for packet with corrected name*/ | 
| 46 | int er_pack_count=0;              /*Error packet counter*/ | 
| 47 | int count_miss=0;                 /*Missed packet counter*/ | 
| 48 | int P_er=0;                       /*Number of packet with counter error*/ | 
| 49 | int flag_CRC_H[3];                /*Flag for CRC header error*/ | 
| 50 | int flag_pack[3];                 /*Flag for error in packet counter*/ | 
| 51 | int flag_len[3];                  /*Flag for PAMELA packet length error*/ | 
| 52 | int flag_len_weak[3];             /*Suspicion flag for PAMELA packet length error*/ | 
| 53 | int flag_name[3];                 /*Flag for name error*/ | 
| 54 | int flag_type[3];                 /*Flag for packet type error*/ | 
| 55 | int flag_time[3];                 /*Flag for time error*/ | 
| 56 | int flag_CRC_cor[3];              /*Flag for CRC error correction*/ | 
| 57 | int CRC_bad_pack[3];              /*Current header CRC bad packet number*/ | 
| 58 | int CRC_Header[3];                /*CRC in header for previous packets*/ | 
| 59 | int Mis_group=0;                  /*Number of missed packet group*/ | 
| 60 | short int flags=0;                /*Number for all errors*/ | 
| 61 | long int tmpP=0;                  /*Temporary PAMELA packet number*/ | 
| 62 | long long int icount=0;           /*Number of bytes in identified packets*/ | 
| 63 | long long int icount_total=0;     /*Total number of bytes*/ | 
| 64 | long int icount_int=0;            /*Entire part of total file size in Kbytes*/ | 
| 65 | long int icount_frac=0;           /*Fractional part of total file size*/ | 
| 66 | long int N1=0;                    /*Number of identified PAMELA packets*/ | 
| 67 | long int numP[3];                     /*Packet numbers for previous packets*/ | 
| 68 | long int LenP[3];                 /*Packet length for previous packets*/ | 
| 69 | unsigned long int time_p[3];      /*Packet time*/ | 
| 70 | long int pack_length=0;           /*PAMELA packet length*/ | 
| 71 | long long int adr_first[3];       /*Start displacement from file begin*/ | 
| 72 | long long int adr_last[3];        /*Finish displacement from file begin*/ | 
| 73 | long int temp[2]; | 
| 74 | char *buffer=new char[Nmax];      /*Buffer for PAMELA current packet*/ | 
| 75 | int FLAG_BIG=0;                   /*Flag for large packet*/ | 
| 76 | unsigned long int k=0, j=0; | 
| 77 | int i; | 
| 78 | //initialization  , all =0 | 
| 79 |  | 
| 80 | stringstream oss, oss1; | 
| 81 |  | 
| 82 | TSQLServer *sqlServer = TSQLServer::Connect("mysql://pamela_1.ntsomz.ru:3306/rawreader", "pamelaprod", "rawprod"); | 
| 83 |  | 
| 84 | oss.str(""); | 
| 85 | oss<<"select * from FILES1"; | 
| 86 | TSQLResult *res1=sqlServer->Query(oss.str().c_str()); | 
| 87 | num_rows=res1->GetRowCount(); | 
| 88 | if (num_rows>0) | 
| 89 | { | 
| 90 | oss.str(""); | 
| 91 | oss<<"select max(my_route) from FILES1"; | 
| 92 | TSQLResult *res2=sqlServer->Query(oss.str().c_str()); | 
| 93 | TSQLRow *row=res2->Next(); | 
| 94 | strcpy(str_route,row->GetField(0)); | 
| 95 |  | 
| 96 | strcpy(str_file,inf); | 
| 97 | str_file[strlen(inf)-9]='\0'; | 
| 98 | strcat(str_file,"_tmp.dat"); | 
| 99 | fo_row=fopen(str_file,"w+"); | 
| 100 | fprintf(fo_row,"%s",str_route); | 
| 101 | fseek(fo_row,0,0); | 
| 102 | fscanf(fo_row,"%i",¤t_route); | 
| 103 | current_route+=1; | 
| 104 | fclose(fo_row); | 
| 105 | remove(str_file); | 
| 106 | } | 
| 107 | else | 
| 108 | { | 
| 109 | current_route=1; | 
| 110 | } | 
| 111 |  | 
| 112 | for (i=0; i<NumberPack+1; i++) packet_counter[i]=0; | 
| 113 | for (i=0; i<3; i++) | 
| 114 | {numP[i]=0; time_p[i]=0; CRC_Header[i]=0; CRC_bad_pack[i]=0; | 
| 115 | flag_CRC_H[i]=0; flag_len[i]=0; flag_len_weak[i]=0; flag_pack[i]=0; flag_CRC_cor[i]=0; | 
| 116 | flag_name[i]=0; flag_type[i]=0; flag_time[i]=0; | 
| 117 | LenP[i]=0; | 
| 118 | adr_first[i]=0; adr_last[i]=0; | 
| 119 | Hex[i]=0;} | 
| 120 | for (i=0; i<4; i++) {Byte_adr1[i]=0; Byte_adr2[i]=0;} | 
| 121 | for (i=0; i<2; i++) temp[i]=0; | 
| 122 |  | 
| 123 | ifstream fin; | 
| 124 | ofstream fo_pam; | 
| 125 |  | 
| 126 | /**********************OPEN NECESSARY FILES************************/ | 
| 127 | /******************************************************************/ | 
| 128 | fin.open(fni, ios::binary);     // open input file "mmm_counter_nnnnn__cln1.pam" | 
| 129 | if(fin.good()==0) {cerr<<"can not open input file"; exit(1);} | 
| 130 |  | 
| 131 | fo_work=fopen(fno, "wb");       //open output help file "work.dat" | 
| 132 | if(fo_work==NULL) {cerr<<"can not open outputfile"; exit(1);} | 
| 133 |  | 
| 134 | fo_log=fopen(log, "wb");        //open output file "Route_cln2.log" | 
| 135 | if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);} | 
| 136 |  | 
| 137 | fo_pam.open(pam, ios::binary);  //open output file "Route_cln2.pam" | 
| 138 | if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);} | 
| 139 |  | 
| 140 | fo_inf=fopen(inf, "wb");        //open output file "mmm_conter_nnnnn_cln2.inf" | 
| 141 | if(fo_inf==NULL) {cerr<<"can not open inf file"; exit(1);} | 
| 142 |  | 
| 143 | strcpy(miss_file,path); | 
| 144 | strncat(miss_file,name_in,strlen(name_in)-8); | 
| 145 | fo_miss=fopen(strcat(miss_file,"mis_cln2.dat"), "wb"); | 
| 146 | //open output file "mmm_nnnnn_mis_cln2.dat" | 
| 147 | if(fo_miss==NULL) {cerr<<"can not open miss file"; exit(1);} | 
| 148 | /******************************************************************/ | 
| 149 | /******************************************************************/ | 
| 150 |  | 
| 151 | //Write proccessed filename in help file | 
| 152 | fprintf(fo_work,"file name=%s\n",name_in); | 
| 153 |  | 
| 154 | /******************************************************************/ | 
| 155 | /******************MAIN READING CYCLE BEGIN************************/ | 
| 156 | /******************************************************************/ | 
| 157 |  | 
| 158 | while(fin.eof()==0) | 
| 159 | { | 
| 160 | num_type=0; num_type1=0;  num_type2=0; | 
| 161 |  | 
| 162 | /**********************data reading********************************/ | 
| 163 | if ((N1==0)&&(icount_total==0)) {fin.read(Name_Buf,8); icount_total+=8;} | 
| 164 |  | 
| 165 | /*******************temporary packet number************************/ | 
| 166 | tmpP=(long int)(unsigned char)(Name_Buf[7])+ | 
| 167 | 256*(long int)(unsigned char)(Name_Buf[6])+ | 
| 168 | 256*256*(long int)(unsigned char)(Name_Buf[5]); | 
| 169 |  | 
| 170 | if (N1==0) numP[2]=tmpP-1;    //initial packet identification | 
| 171 |  | 
| 172 | /******************packet type identification**********************/ | 
| 173 | for (i=0;i<NumberPack;i++) | 
| 174 | {if (packet_type[i]==(int)(unsigned char)(Name_Buf[3])) | 
| 175 | num_type1=i+1; | 
| 176 | if (packet_type[i]==(int)(unsigned char)(Name_Buf[4])) | 
| 177 | num_type2=i+1;} | 
| 178 | if ((num_type1==num_type2)&&(num_type1!=0)) num_type=num_type1; | 
| 179 | if ((num_type1==0)&&(num_type2!=0)) num_type=num_type2; | 
| 180 | if ((num_type1!=0)&&(num_type2==0)) num_type=num_type1; | 
| 181 |  | 
| 182 | /////////////////////////////////////////////////////////////////// | 
| 183 | ////////////////// PAMELA PACKET IDENTIFICATION//////////////////// | 
| 184 | /////////////////////////////////////////////////////////////////// | 
| 185 | if (((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&& | 
| 186 | (tmpP==numP[2]+1)&&num_type!=0)|| | 
| 187 | ((int)(unsigned char)Name_Buf[1]==CODE_BYTE2&& | 
| 188 | (tmpP==numP[2]+1)&&num_type!=0)|| | 
| 189 | ((int)(unsigned char)Name_Buf[2]==CODE_BYTE3&& | 
| 190 | (tmpP==numP[2]+1)&&num_type!=0)|| | 
| 191 | ((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&& | 
| 192 | (int)(unsigned char)Name_Buf[1]==CODE_BYTE2&& | 
| 193 | (int)(unsigned char)Name_Buf[2]==CODE_BYTE3&&num_type1!=0&&num_type2!=0)|| | 
| 194 | ((int)(unsigned char)Name_Buf[0]==CODE_BYTE1&& | 
| 195 | (int)(unsigned char)Name_Buf[1]==CODE_BYTE2&& | 
| 196 | (int)(unsigned char)Name_Buf[2]==CODE_BYTE3&& | 
| 197 | (tmpP==numP[2]+1))) | 
| 198 | {if (N1==0) {for (i=0;i<8;i++) buffer[i]=Name_Buf[i]; } | 
| 199 | N1+=1; FLAG_BIG=0; | 
| 200 | if (N1==1) {icount+=8;} | 
| 201 | packet_counter[num_type]+=1;  //count packet number for each type | 
| 202 | if ((fmod((float)(tmpP),(float)(1000.))==0.)&&(tmpP!=0)) cout<<"verifying "<<tmpP<<"\n"; | 
| 203 |  | 
| 204 | if (N1!=1) | 
| 205 | { | 
| 206 | /**************PAMELA PREVIOUS PACKET END DISPLACEMENT*************/ | 
| 207 | if ((icount-8-adr_first[2]+1-LenP[2]-16)<=Limit) {adr_last[2]=icount-8;}               //end byte for previous packet | 
| 208 | else if ((icount-8-adr_first[2]+1-LenP[2]-16)>Limit) {adr_last[2]=adr_first[2]+Limit;} //end byte for previous packet | 
| 209 |  | 
| 210 | /**************PAMELA PACKET LENGTH ERROR ANALYSIS*****************/ | 
| 211 | if ( (flag_CRC_H[2]==1)&&(GLOBAL_LEN_ER>0)&& | 
| 212 | ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16)) | 
| 213 | ||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) ) | 
| 214 | {flag_len[2]=1; | 
| 215 | cout<<"pack num "<<numP[2]<<" len="<<LenP[2]<<" true len="<< | 
| 216 | adr_last[2]-adr_first[2]-16<<" Type="<<num_type<<"\n";} | 
| 217 | if ( (flag_CRC_H[2]==0)&&(GLOBAL_LEN_ER>0)&& | 
| 218 | ( ((adr_last[2]-adr_first[2]+1)<(LenP[2]+16))||((adr_last[2]-adr_first[2]+1-LenP[2]-16)>64) ) ) | 
| 219 | {flag_len_weak[2]=1; | 
| 220 | cout<<"weak length error; pack num "<<numP[2]<<" len="<<LenP[2]<<" true len="<< | 
| 221 | adr_last[2]-adr_first[2]-16<<" Type="<<num_type<<"\n";} | 
| 222 |  | 
| 223 | if ((GLOBAL_LEN_ER>0)&&(flag_len[2]==1)) | 
| 224 | { | 
| 225 | //Write data in file "mmm_counter_nnnnn_cln2.pam" | 
| 226 | for (i=0;i<adr_last[2]-adr_first[2]+1;i++) | 
| 227 | {fo_pam<< buffer[i];} | 
| 228 | } | 
| 229 | else if ((GLOBAL_LEN_ER>0)&&(flag_len[2]!=1)) | 
| 230 | { | 
| 231 | //Write data in file "mmm_counter_nnnnn_cln2.pam" | 
| 232 | for (i=0;i<LenP[2]+16;i++) | 
| 233 | {fo_pam<< buffer[i];} | 
| 234 | adr_last[2]=adr_first[2]+LenP[2]+15; | 
| 235 | icount=adr_first[2]+15+LenP[2]+8; | 
| 236 | } | 
| 237 | else if ((GLOBAL_LEN_ER==0)&&(adr_last[2]-adr_first[2]+1-LenP[2]-16>=0)) | 
| 238 | { | 
| 239 | for (i=0;i<LenP[2]+16;i++) | 
| 240 | {fo_pam<< buffer[i];} | 
| 241 | adr_last[2]=adr_first[2]+LenP[2]+15; | 
| 242 | icount=adr_first[2]+15+LenP[2]+8; | 
| 243 | } | 
| 244 | delete [] buffer; | 
| 245 | buffer=new char[Nmax]; | 
| 246 | for (i=0;i<8;i++) buffer[i]=Name_Buf[i]; | 
| 247 |  | 
| 248 | temp[1]=adr_last[2]; | 
| 249 | for (i=0;i<4;i++) Byte_adr2[i]=0; | 
| 250 | for (i=0;i<4;i++) | 
| 251 | {if (temp[1]/(long int)(pow(256.,3-i))>=1) | 
| 252 | {Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i))); | 
| 253 | temp[1]=temp[1]-(long int)pow(256.,3-i)* | 
| 254 | (int)(temp[1]/(long int)(pow(256.,3-i)));}} | 
| 255 | //Write previous packet end displacement in mmm_counter_nnnnn_cln2.log file | 
| 256 | for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i])); | 
| 257 | } | 
| 258 | /*********************reading remaining header********************/ | 
| 259 | fin.read(Name_Buf_add, 8); | 
| 260 | if (fin.eof()) break; | 
| 261 | icount+=8; icount_total+=8; | 
| 262 | for (i=0;i<8;i++) buffer[i+8]=Name_Buf_add[i]; | 
| 263 |  | 
| 264 | //packet length | 
| 265 | pack_length=(long int)(unsigned char)(Name_Buf_add[6])+ | 
| 266 | 256*(long int)(unsigned char)(Name_Buf_add[5])+ | 
| 267 | 256*256*(long int)(unsigned char)(Name_Buf_add[4]); | 
| 268 | //   cout<<pack_length<<"  "<<tmpP<<" "<<icount_total<<"\n"; | 
| 269 | /***********PACKET HEADER DATA ANALYSIS****************************/ | 
| 270 | switch (N1) | 
| 271 | { | 
| 272 | case 1:{ | 
| 273 | LenP[2]=pack_length; | 
| 274 | numP[2]=(long int)(unsigned char)(Name_Buf[7])+ | 
| 275 | 256*(long int)(unsigned char)(Name_Buf[6])+ | 
| 276 | 256*256*(long int)(unsigned char)(Name_Buf[5]); | 
| 277 | time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+ | 
| 278 | 256*(long int)(unsigned char)(Name_Buf_add[2])+ | 
| 279 | 256*256*(long int)(unsigned char)(Name_Buf_add[1])+ | 
| 280 | 256*256*256*(long int)(unsigned char)(Name_Buf_add[0]); | 
| 281 |  | 
| 282 | CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]); | 
| 283 | adr_first[2]=icount-15; | 
| 284 | CRC_bad_pack[2]=0; | 
| 285 | flag_CRC_H[2]=0; | 
| 286 | flag_pack[2]=0; | 
| 287 | flag_name[2]=0; | 
| 288 | flag_type[2]=0; | 
| 289 | flag_time[2]=0; | 
| 290 | flag_CRC_cor[2]=0; | 
| 291 | for (i=0; i<2; i++) | 
| 292 | {numP[i]=numP[2]; time_p[i]=time_p[2]; | 
| 293 | CRC_Header[i]=CRC_Header[2]; | 
| 294 | LenP[i]=LenP[2]; | 
| 295 | adr_first[i]=adr_first[2]; | 
| 296 | CRC_bad_pack[i]=CRC_bad_pack[2]; | 
| 297 | flag_CRC_H[i]=flag_CRC_H[2]; | 
| 298 | flag_pack[i]=flag_pack[2]; | 
| 299 | flag_name[i]=flag_name[2]; | 
| 300 | flag_type[i]=flag_type[2]; | 
| 301 | flag_time[i]=flag_time[2]; | 
| 302 | flag_CRC_cor[i]=flag_CRC_cor[2];} | 
| 303 | break; | 
| 304 | } | 
| 305 | default:{ | 
| 306 | for (i=0; i<2; i++) | 
| 307 | {numP[i]=numP[i+1]; | 
| 308 | time_p[i]=time_p[i+1]; | 
| 309 | CRC_Header[i]=CRC_Header[i+1]; | 
| 310 | CRC_bad_pack[i]=CRC_bad_pack[i+1]; | 
| 311 | LenP[i]=LenP[i+1]; | 
| 312 | adr_first[i]=adr_first[i+1]; | 
| 313 | adr_last[i]=adr_last[i+1]; | 
| 314 | flag_CRC_H[i]=flag_CRC_H[i+1]; | 
| 315 | flag_len[i]=flag_len[i+1]; | 
| 316 | flag_len_weak[i]=flag_len_weak[i+1]; | 
| 317 | flag_pack[i]=flag_pack[i+1]; | 
| 318 | flag_name[i]=flag_name[i+1]; | 
| 319 | flag_type[i]=flag_type[i+1]; | 
| 320 | flag_time[i]=flag_time[i+1]; | 
| 321 | flag_CRC_cor[i]=flag_CRC_cor[i+1];} | 
| 322 | numP[2]=(long int)(unsigned char)(Name_Buf[7])+ | 
| 323 | 256*(long int)(unsigned char)(Name_Buf[6])+ | 
| 324 | 256*256*(long int)(unsigned char)(Name_Buf[5]); | 
| 325 | time_p[2]=(long int)(unsigned char)(Name_Buf_add[3])+ | 
| 326 | 256*(long int)(unsigned char)(Name_Buf_add[2])+ | 
| 327 | 256*256*(long int)(unsigned char)(Name_Buf_add[1])+ | 
| 328 | 256*256*256*(long int)(unsigned char)(Name_Buf_add[0]); | 
| 329 |  | 
| 330 | CRC_Header[2]=(int)(unsigned char)(Name_Buf_add[7]); | 
| 331 | CRC_bad_pack[2]=0; | 
| 332 | adr_first[2]=icount-15; | 
| 333 | flag_CRC_H[2]=0; | 
| 334 | flag_len[2]=0; | 
| 335 | flag_len_weak[2]=0; | 
| 336 | flag_pack[2]=0; | 
| 337 | flag_name[2]=0; | 
| 338 | flag_type[2]=0; | 
| 339 | flag_time[2]=0; | 
| 340 | flag_CRC_cor[2]=0; | 
| 341 | LenP[2]=pack_length; | 
| 342 | } | 
| 343 | } | 
| 344 | /********packet header CRC error analysis**************************/ | 
| 345 | //   CalcCheckSum=CRC_H(Name_Buf, Name_Buf_add); | 
| 346 |  | 
| 347 | for (i=0; i<8; i++) | 
| 348 | {buff[i]=Name_Buf[i]; | 
| 349 | buff[i+8]=Name_Buf_add[i];} | 
| 350 |  | 
| 351 | CalcCheckSum=(short int)(unsigned char)CM_Compute_CRC16(0, (unsigned char*)&buff, 15); | 
| 352 | if (CalcCheckSum!=(short int)(unsigned char)(Name_Buf_add[7])) | 
| 353 | {cerr<<"checksum error "<<N1<<" Pack="<<numP[2]<< | 
| 354 | " "<<num_type<<" "<<bitset<8>(CalcCheckSum)<< | 
| 355 | " CRC="<<bitset<8>((int)(unsigned char)(Name_Buf_add[7]))<<"\n"; | 
| 356 | //    cout<<(int)(unsigned char)(Name_Buf[0])<<" " | 
| 357 | //              <<(int)(unsigned char)(Name_Buf[1])<<" " | 
| 358 | //              <<(int)(unsigned char)(Name_Buf[2]) | 
| 359 | //              <<" "<<num_type<<" "<<num_type1<<" "<<num_type2<<" "<<tmpP<<"\n"; | 
| 360 | CRC_bad_pack[2]=numP[2]; | 
| 361 | flag_CRC_H[2]=1;} | 
| 362 | /*********packet counter error analysis****************************/ | 
| 363 | if ((numP[2]!=(numP[1]+1))&&(N1!=1)&& | 
| 364 | (flag_len[1]==0)&&(CRC_bad_pack[2]==numP[2])) flag_pack[2]=1; | 
| 365 |  | 
| 366 | if ((numP[1]!=(numP[0]+1))&&(numP[1]!=(numP[2]-1))&&(N1!=1)&& | 
| 367 | (CRC_bad_pack[1]==numP[1])) | 
| 368 | {P_er=N1-1; flag_pack[1]=1; | 
| 369 | //Correct error packet number// | 
| 370 | tmpP=numP[0]+1; | 
| 371 | for (i=0;i<3;i++) Hex[i]=0; | 
| 372 | for (i=0;i<3;i++) | 
| 373 | {if (tmpP/(long int)      (pow(256.,2-i))>=1) | 
| 374 | {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i))); | 
| 375 | tmpP=tmpP-(long int)(pow(256.,2-i)* | 
| 376 | (int)(tmpP/(long int)(pow(256.,2-i)) ));}} | 
| 377 | for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i])); | 
| 378 | if (CRC_Header[1]==(int)(CRC_H(Name_Buf_old, Name_Buf_add_old))) | 
| 379 | {flag_CRC_cor[1]=1;} | 
| 380 |  | 
| 381 | tmpP=numP[2]-1; | 
| 382 | for (i=0;i<3;i++) Hex[i]=0; | 
| 383 | for (i=0;i<3;i++) | 
| 384 | {if (tmpP/(long int)(pow(256.,2-i))>=1) | 
| 385 | {Hex[2-i]=(int)(tmpP/(long int)(pow(256.,2-i))); | 
| 386 | tmpP=tmpP-(long int)pow(256.,2-i)* | 
| 387 | (int)(tmpP/(long int)(pow(256.,2-i)));}} | 
| 388 | for (i=5;i<8;i++) (Name_Buf_old[i]=(char)(Hex[7-i])); | 
| 389 | if (CRC_Header[1]==(int)(CRC_H(Name_Buf_old, Name_Buf_add_old))) | 
| 390 | {flag_CRC_cor[1]=1;} | 
| 391 | } | 
| 392 | /*********************Missing packet analysis**********************/ | 
| 393 | /**packet numbers: 10 11 (12 15 19) 20 ****************************/ | 
| 394 | /***********missed kadrs: 16 17 18 ********************************/ | 
| 395 | if ((numP[1]>numP[0]+1)&&(numP[1]<numP[2]-1)&& | 
| 396 | (flag_pack[0]==0)&&(flag_pack[1]==0)&&(flag_pack[2]==0)) | 
| 397 | {Mis_group+=1; | 
| 398 | fprintf(fo_miss,"%9i%9i\n",numP[1]+1,numP[2]-1); | 
| 399 | count_miss+=numP[2]-numP[1]-1;} | 
| 400 | /**packet numbers: 10 (11 12 15) 19 20 ****************************/ | 
| 401 | /***********missed kadrs: 13 14 ***********************************/ | 
| 402 | else if ((numP[1]>=numP[0]+1)&&(numP[1]<numP[2]-1)&& | 
| 403 | (flag_pack[2]==0)&&(flag_pack[1]==0)&&(flag_pack[0]==0)) | 
| 404 | {Mis_group+=1; | 
| 405 | fprintf(fo_miss,"%9i%9i\n",numP[0]+2,numP[2]-1); | 
| 406 | count_miss+=numP[2]-numP[1]-1;} | 
| 407 | /**packet numbers: 10 (11 20 15) 16 17 ****************************/ | 
| 408 | /********missed kadrs number =2 ***********************************/ | 
| 409 | else if ((numP[0]<numP[2]-2)&& | 
| 410 | (flag_pack[2]==0)&&(flag_pack[1]==1)&&(flag_pack[0]==0)) | 
| 411 |  | 
| 412 | {Mis_group+=1; | 
| 413 | fprintf(fo_miss,"%9i%9i%9i\n",numP[0]+1,numP[1],numP[2]-1); | 
| 414 | count_miss+=numP[2]-numP[0]-2;} | 
| 415 |  | 
| 416 | /**************Time error******************************************/ | 
| 417 | if ((numP[2]>=numP[0]+2)&&(flag_pack[2]==0)&&(flag_pack[0]==0)&& | 
| 418 | (flag_time[0]==0)&&(time_p[2]>time_p[0])&& | 
| 419 | ((time_p[1]<time_p[0])||(time_p[1]>time_p[2]))) | 
| 420 | {flag_time[1]=1;} | 
| 421 |  | 
| 422 | /**************Error number calculation****************************/ | 
| 423 | if (N1!=1) | 
| 424 | {flags=(short int)((float)(flag_len_weak[1])+ | 
| 425 | pow((float)(2*flag_CRC_H[1]),1)+ | 
| 426 | pow((float)(2*flag_len[1]),2)+ | 
| 427 | pow((float)(2*flag_pack[1]),3)+ | 
| 428 | pow((float)(2*flag_name[1]),4)+ | 
| 429 | pow((float)(2*flag_type[1]),5)+ | 
| 430 | pow((float)(2*flag_time[1]),6)+ | 
| 431 | pow((float)(2*flag_CRC_cor[1]),7)); | 
| 432 | if (flags!=0) er_pack_count+=1; | 
| 433 | //Write error number in log and work.dat files | 
| 434 | fprintf(fo_log,"%1c",char(flags)); | 
| 435 | //    fprintf(fo_work," error num.=%i CRC=%i Calc=%i\n", | 
| 436 | //                  flags,bitset<8>(CRC_Header[1]), | 
| 437 | //                      (int)(CRC_H(Name_Buf_old, Name_Buf_add_old))); | 
| 438 | } | 
| 439 | /***********************NAME ERROR*********************************/ | 
| 440 | if (((int)(unsigned char)Name_Buf[0]!=CODE_BYTE1)|| | 
| 441 | ((int)(unsigned char)Name_Buf[1]!=CODE_BYTE2)|| | 
| 442 | ((int)(unsigned char)Name_Buf[2]!=CODE_BYTE3)) | 
| 443 | { | 
| 444 | Name_Buf[0]=(char)(CODE_BYTE1); | 
| 445 | Name_Buf[1]=(char)(CODE_BYTE2); | 
| 446 | Name_Buf[2]=(char)(CODE_BYTE3); | 
| 447 | flag_name[2]=1; | 
| 448 | count_name_cor+=1; | 
| 449 | buffer[0]=(char)(CODE_BYTE1); | 
| 450 | buffer[1]=(char)(CODE_BYTE2); | 
| 451 | buffer[2]=(char)(CODE_BYTE3); | 
| 452 | if (CRC_Header[2]==(int)(CRC_H(Name_Buf, Name_Buf_add))) | 
| 453 | flag_CRC_cor[2]=1; | 
| 454 | } | 
| 455 |  | 
| 456 | /***********************PACKET TYPE ERROR**************************/ | 
| 457 | if ((num_type==0)||(num_type1==0)||(num_type2==0)) flag_type[2]=1; | 
| 458 |  | 
| 459 | /******************PAMELA BEGIN PACKET DISPLACEMENT****************/ | 
| 460 | temp[0]=adr_first[2]; | 
| 461 | for (i=0;i<4;i++) Byte_adr1[i]=0; | 
| 462 | for (i=0;i<4;i++) | 
| 463 | {if (temp[0]/(long int)(pow(256.,3-i))>=1) | 
| 464 | {Byte_adr1[3-i]=(int)(temp[0]/(long int)(pow(256.,3-i))); | 
| 465 | temp[0]=temp[0]-(long int)pow(256.,3-i)* | 
| 466 | (int)(temp[0]/(long int)(pow(256.,3-i)));} | 
| 467 | } | 
| 468 |  | 
| 469 | //Write packet type and begin displacement in log and work.dat files | 
| 470 | //   fprintf(fo_work,"Type %i: num. %i: first=%i:", | 
| 471 | //           num_type, numP[2], adr_first[2]); | 
| 472 | fprintf(fo_log,"%1c",char(num_type)); | 
| 473 | for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr1[i])); | 
| 474 |  | 
| 475 | /**************PAMELA PACKET OLD BUFFER***************************/ | 
| 476 | for (i=0;i<8;i++) | 
| 477 | {Name_Buf_old[i]=Name_Buf[i];Name_Buf_add_old[i]=Name_Buf_add[i];} | 
| 478 |  | 
| 479 | /**********************data reading********************************/ | 
| 480 | fin.seekg(-1,ios::cur); icount-=1; icount_total-=1; | 
| 481 | fin.read(Name_Buf,8); | 
| 482 | if (fin.eof()) break; | 
| 483 | icount+=8; icount_total+=8; | 
| 484 | for (i=0;i<7;i++) buffer[i+16]=Name_Buf[i+1]; | 
| 485 | } | 
| 486 | /////////////////////////////////////////////////////////////////// | 
| 487 | /////////////////////////////////////////////////////////////////// | 
| 488 | /////////////////////////////////////////////////////////////////// | 
| 489 | /**********************data reading********************************/ | 
| 490 | fin.get(ch); | 
| 491 | if (fin.eof()) {break;} | 
| 492 | icount_total+=1; | 
| 493 | if (fmod( double(icount_total), double(100000000.))==double(0.)) {cout<<"Mbytes="<<icount_total/1000000.<<"\n";} | 
| 494 | if ((N1>0)&&(FLAG_BIG==0)) icount+=1; | 
| 495 | for (i=0;i<7;i++) Name_Buf[i]=Name_Buf[i+1]; | 
| 496 | Name_Buf[7]=ch; /*for(i=0;i<8;i++) printf("%x",(int)(unsigned char)Name_Buf[i]); printf("\n");*/ | 
| 497 | if ((icount-adr_first[2]-1)<Nmax) | 
| 498 | {buffer[icount-adr_first[2]]=ch;} | 
| 499 | else | 
| 500 | {flag_len[2]=1; | 
| 501 | if (FLAG_BIG==0) {cout<<"too long packet "<<numP[2]<<"\n";} | 
| 502 | FLAG_BIG=1; | 
| 503 | } | 
| 504 | } | 
| 505 | /******************************************************************/ | 
| 506 | /********************MAIN READING CYCLE END************************/ | 
| 507 | /******************************************************************/ | 
| 508 |  | 
| 509 | /*************PAMELA LAST PACKET END DISPLACEMENT******************/ | 
| 510 | if (FLAG_BIG==0) {adr_last[2]=icount;} | 
| 511 | else {adr_last[2]=adr_first[2]+Limit;} | 
| 512 | 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; | 
| 513 | if ( (flag_len[2]!=1)&&(flag_CRC_H[2]!=1) ) | 
| 514 | {adr_last[2]=adr_first[2]+LenP[2]+15;} | 
| 515 | temp[1]=adr_last[2]; | 
| 516 | for (i=0;i<4;i++) Byte_adr2[i]=0; | 
| 517 | for (i=0;i<4;i++) | 
| 518 | {if (temp[1]/(long int)(pow(256.,3-i))>=1) | 
| 519 | {Byte_adr2[3-i]=(int)(temp[1]/(long int)(pow(256.,3-i))); | 
| 520 | temp[1]=temp[1]-(long int)pow(256.,3-i)* | 
| 521 | (int)(temp[1]/(long int)(pow(256.,3-i)));}} | 
| 522 |  | 
| 523 | /*********PAMELA LAST PACKET LENGTH ERROR ANALYSIS*****************/ | 
| 524 | // if ((adr_last[2]-adr_first[2]+1)!=(LenP[2]+(64-LenP[2]%64))) | 
| 525 | // {flag_len[2]=1;} | 
| 526 |  | 
| 527 | //Write last packet end displacement in log and work.dat files | 
| 528 | for (i=0;i<4;i++) fprintf(fo_log,"%1c",char(Byte_adr2[i])); | 
| 529 | // fprintf(fo_work," last=%i",adr_last[2]); | 
| 530 |  | 
| 531 | //Write error number in log and work.dat files | 
| 532 | flags=(short int)(pow((float)(2*flag_CRC_H[2]),1)+ | 
| 533 | pow((float)(2*flag_len[2]),2)+ | 
| 534 | pow((float)(2*flag_pack[2]),3)+ | 
| 535 | pow((float)(2*flag_name[2]),4)+ | 
| 536 | pow((float)(2*flag_type[2]),5)+ | 
| 537 | pow((float)(2*flag_time[2]),6)+ | 
| 538 | pow((float)(2*flag_CRC_cor[2]),7)); | 
| 539 | if ((flags!=0)&&(N1>0)) er_pack_count+=1; | 
| 540 | fprintf(fo_log,"%1c",char(flags)); | 
| 541 | // fprintf(fo_work," error num.=%i CRC=%i Calc=%i\n", | 
| 542 | //           flags,bitset<8>(CRC_Header[2]), | 
| 543 | //               (int)(CRC_H(Name_Buf, Name_Buf_add))); | 
| 544 |  | 
| 545 | //Write inf file | 
| 546 | fprintf(fo_inf,"file name=%s\n",name_in); | 
| 547 | fprintf(fo_inf,"%lli bytes\n",icount_total); | 
| 548 | icount_int = (int)(icount_total/1024); | 
| 549 | icount_frac= icount_total%1024; | 
| 550 | fprintf(fo_inf,"file size= %i,%i kByte \n", icount_int, | 
| 551 | icount_frac); | 
| 552 |  | 
| 553 | fprintf(fo_inf,"number of identified packets= %i\n", N1); | 
| 554 | fprintf(fo_inf,"number of PAMELA missed packets= %i\n", | 
| 555 | count_miss); | 
| 556 | fprintf(fo_inf,"number of PAMELA packets with error= %i\n", | 
| 557 | er_pack_count); | 
| 558 | fprintf(fo_inf,"number of PAMELA packets with corrected name= %i\n", | 
| 559 | count_name_cor); | 
| 560 | fprintf(fo_inf,"number of PAMELA packets with non identified type"); | 
| 561 | fprintf(fo_inf," =%i\n",packet_counter[0]); | 
| 562 |  | 
| 563 | for (i=1;i<NumberPack;i++) | 
| 564 | { | 
| 565 | fprintf(fo_inf,"Type %x  number of PAMELA packets= %i\n", | 
| 566 | packet_type[i-1], packet_counter[i]); | 
| 567 | } | 
| 568 | //Write data in file "mmm_counter_nnnnn_cln2.pam" | 
| 569 | for (i=0;i<adr_last[2]-adr_first[2]+1;i++) | 
| 570 | {fo_pam<<buffer[i];} | 
| 571 |  | 
| 572 | time(&seconds); | 
| 573 | timeinfo=localtime(&seconds); | 
| 574 | strftime(time_year,5,"%Y",timeinfo); | 
| 575 | strftime(time_month,5,"%m",timeinfo); | 
| 576 | strftime(time_day,5,"%d",timeinfo); | 
| 577 | strcat( strcat(strcat(strcat(strcpy(data_time,time_year),"-"),time_month),"-"),time_day); | 
| 578 |  | 
| 579 | for (i=strlen(pam)-22;i<=strlen(pam);i++) | 
| 580 | {name_cln2[i-(strlen(pam)-22)]=pam[i];} | 
| 581 | for (i=0;i<strlen(pam)-22;i++) | 
| 582 | {name_path[i]=pam[i];} | 
| 583 | name_path[strlen(pam)-22]='\0'; | 
| 584 | for (i=8;i<13;i++) | 
| 585 | {cirquit_no[i-8]=name_cln2[i];} | 
| 586 | cirquit_no[5]='\0'; | 
| 587 | for (i=0;i<3;i++) | 
| 588 | {route_no[i]=name_cln2[i];} | 
| 589 | route_no[3]='\0'; | 
| 590 | file_ready=1; | 
| 591 | file_deleted=0; | 
| 592 | file_archived=0; | 
| 593 | file_proces=0; | 
| 594 | if (IndexQ>0.9) file_proces=1; | 
| 595 |  | 
| 596 | /* printf("cirquit=%s \n", cirquit_no); | 
| 597 | printf("route=%s \n", route_no); | 
| 598 | printf("quality=%f \n", IndexQ); | 
| 599 | printf("ready=%i \n", file_ready); | 
| 600 | printf("deleted=%i \n", file_deleted); | 
| 601 | printf("data=%s \n", data_time); | 
| 602 | printf("achived=%i \n", file_archived); | 
| 603 | printf("path=%s \n", name_path); | 
| 604 | printf("name=%s \n", name_cln2); | 
| 605 | printf("processed=%i \n", file_proces);*/ | 
| 606 |  | 
| 607 | if (IndexQ>0.999) {flag_to_transmit=1;} | 
| 608 | else {flag_to_transmit=0;} | 
| 609 | oss.str(""); | 
| 610 | oss << "INSERT INTO FILES (cirquit_n, route_n, counter, qual_index, ready, deleted, date," | 
| 611 | << " archived, path, cln2_name, proces_flag, transit_flag)" | 
| 612 | << " VALUES ('"<< cirquit_no << "','"<< route_no << "','"<<r_counter<<"','"<< IndexQ << "','" | 
| 613 | << file_ready << "','"<< file_deleted << "','"<< data_time << "','"<< file_archived << "','" | 
| 614 | << name_path << "', '" << name_cln2 << "', '" << file_proces << "','" | 
| 615 | << flag_to_transmit << "')"; | 
| 616 | cout<<oss.str().c_str()<<" table FILES\n"; | 
| 617 | TSQLResult *res=sqlServer->Query(oss.str().c_str()); | 
| 618 |  | 
| 619 | flag_sec_trans=0; | 
| 620 |  | 
| 621 | oss1.str(""); | 
| 622 | oss1 <<"INSERT INTO FILES1 (my_route, raw_filename, pam_size, cirquit_n, route_n, route_real," | 
| 623 | <<" counter, board_time, Num_all, Num_macro, flag_twice)" | 
| 624 | <<" VALUES ('"<<current_route<<"','"<<name_ini<<"','"<<icount<<"','"<<cirquit_no<<"','" | 
| 625 | <<route_no<<"','"<<real_route[r_counter]<<"','"<<r_counter | 
| 626 | <<"','"<<time_p[2]<<"','"<<N1<<"','"<<packet_counter[28]<<"','"<<flag_sec_trans<<"')"; | 
| 627 | cout<<oss1.str().c_str()<<" table FILES1\n"; | 
| 628 | TSQLResult *res3=sqlServer->Query(oss1.str().c_str()); | 
| 629 |  | 
| 630 | // if (conn->Query(oss.str().c_str()) == 0) | 
| 631 | // { | 
| 632 | //  printf("ERROR!!!"); | 
| 633 | // } | 
| 634 |  | 
| 635 | delete [] buffer; | 
| 636 |  | 
| 637 | /***************************************************************/ | 
| 638 | fin.close(); | 
| 639 | fclose(fo_work); | 
| 640 | fclose(fo_log); | 
| 641 | fclose(fo_inf); | 
| 642 | fo_pam.close(); | 
| 643 | fclose(fo_miss); | 
| 644 | } | 
| 645 |  |