/[PAMELA software]/rawreader/src/Verify.cpp
ViewVC logotype

Contents of /rawreader/src/Verify.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Sun Dec 10 12:21:27 2006 UTC (17 years, 11 months ago) by cafagna
Branch: MAIN
CVS Tags: v1r12
Changes since 1.1: +55 -56 lines
*** empty log message ***

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

  ViewVC Help
Powered by ViewVC 1.1.23