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

Contents of /rawreader/src/Verify.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Sun Jan 28 10:55:50 2007 UTC (17 years, 10 months ago) by cafagna
Branch: MAIN
CVS Tags: v1r15, HEAD
Changes since 1.4: +22 -10 lines
Error occurred while calculating annotation data.
Main two features of this version are:
1)  the estimation of absolute time is stored  in DB and in timesync.dat
file
2) There is CLN2_flag which gives possibility:
 a) to avoid creation of cln2 files Cln2_flag=0,
 b) if Cln2_flag=1 cln2 file is created with all data in raw file
 c) if Cln2_flag=2 cln2 file is created with only new data in raw file

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

  ViewVC Help
Powered by ViewVC 1.1.23