EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packet_A.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file packet_A.cc
1 #include "packet_A.h"
2 #include "packetHeaders.h"
3 #include <string.h>
4 #include "buffer.h"
5 
7 {
8 
9  data1_length = 0;
10  data2_length = 0;
11  data3_length = 0;
12  data4_length = 0;
13  data5_length = 0;
14  data6_length = 0;
15  data7_length = 0;
16  data8_length = 0;
17  data9_length = 0;
18  data10_length = 0;
19 
20  decoded_data1 = NULL;
21  decoded_data2 = NULL;
22  decoded_data3 = NULL;
23  decoded_data4 = NULL;
24  decoded_data5 = NULL;
25  decoded_data6 = NULL;
26  decoded_data7 = NULL;
27  decoded_data8 = NULL;
28  decoded_data9 = NULL;
29  decoded_data10 = NULL;
30 
31  is_data_type = 0; //assume "p" type first.
32 }
33 
35 {
36 
37  packet = packet_ptr;
38  // packetHdr = ( PACKETHDR_ptr ) packet;
39 
40  data1_length = 0;
41  data2_length = 0;
42  data3_length = 0;
43  data4_length = 0;
44  data5_length = 0;
45  data6_length = 0;
46  data7_length = 0;
47  data8_length = 0;
48  data9_length = 0;
49  data10_length = 0;
50 
51  decoded_data1 = NULL;
52  decoded_data2 = NULL;
53  decoded_data3 = NULL;
54  decoded_data4 = NULL;
55  decoded_data5 = NULL;
56  decoded_data6 = NULL;
57  decoded_data7 = NULL;
58  decoded_data8 = NULL;
59  decoded_data9 = NULL;
60  decoded_data10 = NULL;
61 
62 
63  is_data_type = 0; //assume "p" type first.
64 }
65 
66 // ----------------------------------------------
67 
69 {
70 
71  if (decoded_data1 != NULL)
72  {
73  delete [] decoded_data1;
74  decoded_data1 = 0;
75  }
76  if (decoded_data2 != NULL)
77  {
78  delete [] decoded_data2;
79  decoded_data2 = 0;
80  }
81  if (decoded_data3 != NULL)
82  {
83  delete [] decoded_data3;
84  decoded_data3 = 0;
85  }
86  if (decoded_data4 != NULL)
87  {
88  delete [] decoded_data4;
89  decoded_data4 = 0;
90  }
91  if (decoded_data5 != NULL)
92  {
93  delete [] decoded_data5;
94  decoded_data5 = 0;
95  }
96  if (decoded_data6 != NULL)
97  {
98  delete [] decoded_data6;
99  decoded_data6 = 0;
100  }
101  if (decoded_data7 != NULL)
102  {
103  delete [] decoded_data7;
104  decoded_data7 = 0;
105  }
106  if (decoded_data8 != NULL)
107  {
108  delete [] decoded_data8;
109  decoded_data8 = 0;
110  }
111  if (decoded_data9 != NULL)
112  {
113  delete [] decoded_data9;
114  decoded_data9 = 0;
115  }
116  if (decoded_data10 != NULL)
117  {
118  delete [] decoded_data10;
119  decoded_data10 = 0;
120  }
121 
122  if (is_data_type) delete [] packet;
123 }
124 
125 
127 {
128  if (is_data_type) return 0;
129  else return 1;
130 }
131 
132 // ----------------------------------------------
133 
135 {
136  if (is_data_type) return -1;
137 
138  PHDWORD *tmp;
139 
140  tmp = new PHDWORD[getLength()];
141  PHDWORD *from= packet;
142  PHDWORD *to=tmp;
143  for (int k=0; k< getLength(); k++)
144  {
145  *to++ = *from++;
146  }
147  packet = tmp;
148  is_data_type = 1;
149  return 0;
150 
151 }
152 
153 // ------------------------------------------------------
155 {
156  return getPacketLength (packet) ;
157 }
158 
159 // ------------------------------------------------------
160 int Packet_A::getErrorLength() const { return
162 
163 //------------------------------------------------------
164 int Packet_A::getDebugLength() const { return
166 
167 //------------------------------------------------------
168 int Packet_A::getIdentifier() const { return getPacketId (packet); }
169 
170 //------------------------------------------------------
171 int Packet_A::getPadding() const {
172 return
174 }
175 
176 // ------------------------------------------------------
178 {
179  return getPacketStructure (packet);
180 }
181 
182 // ------------------------------------------------------
184 {
186 }
187 
188 // ------------------------------------------------------
189 //int Packet_A::getWordSize() const
190 //{
191 // return ??? // ** action
192 //
193 //}
194 
195 // ------------------------------------------------------
197 {
198  return getPacketDataLength (packet);
199 }
200 
201 // ------------------------------------------------------
202 
203 void Packet_A::fullIdentify( OSTREAM& out ) const
204 {
205  out << std::dec;
206  out << "*** Packet id " << getPacketId (packet) << std::endl;
207  out << " Hdr version " << getPacketHdrVersion (packet) << std::endl;
208  out << " Hdr length " << getPacketHdrLength (packet) << std::endl;
209  out << " Packet Length " << getPacketLength (packet) << std::endl;
210  out << " Packet Status " << getPacketStatus (packet) << std::endl;
211  out << " Debug Length " << getPacketDebugLength (packet) << std::endl;
212  out << " Error Length " << getPacketErrorLength (packet) << std::endl;
213  out << " Structure " << getStructure() << " (";
214 
215  switch (getStructure())
216  {
217 
218  case Unstructured:
219  out << "Unformatted) ";
220  break;
221 
222  case Packets:
223  out << "Unformatted) ";
224  out << "Packets ) ";
225  break;
226 
227  case hitArray:
228  out << "HitArray ) ";
229  break;
230 
231  case hitList:
232  out << "HitList ) ";
233  break;
234  }
235  out << std::endl;
236 
237  out << " Descriptor Words: " << getPacketDataDescrLength (packet) << std::endl;
238  out << " Endianism: " << getPacketEndianism (packet);
239 
240  switch (getPacketEndianism (packet) )
241  {
242 
243  case 1:
244  out << " (Little Endian) " << std::endl;
245  break;
246 
247  case 2:
248  out << " (Big Endian) " << std::endl;
249  break;
250 
251  default:
252  out << " (Unknown) " << std::endl;
253  break;
254  }
255 
256  out << " Padding: " << getPacketPadding (packet) << std::endl;
257 
258 
259 }
260 
261 void Packet_A::identify( OSTREAM& out ) const
262 {
263  // out << "identify packet type A" << std::endl;
264  out << std::dec
265  << "Packet " << SETW(6) << getIdentifier()
266  << " " << SETW(5) << getLength()
267  << " " << SETW(2) << getStructure() << " (";
268 
269  switch (getStructure())
270  {
271 
272  case Unstructured:
273  out << "Unformatted) ";
274  break;
275 
276  case Packets:
277  out << "Unformatted) ";
278  out << "Packets ) ";
279  break;
280 
281  case hitArray:
282  out << "HitArray ) ";
283  break;
284 
285  case hitList:
286  out << "HitList ) ";
287  break;
288  }
289 
290  out << SETW(4) << getHitFormat()
291  << " (" << get_mnemonic( getStructure(), getHitFormat()) << ")";
292 
293  if (getErrorLength() ) out << " ** Error Block";
294 
295  out << std::endl;
296 }
297 
298 
299 // ------------------------------------------------------
300 
301 int Packet_A::iValue(const int ich)
302 {
303  // now let's derefence the proxy array. If we didn't decode
304  // the data until now, we do it now
305  if (decoded_data1 == NULL )
306  {
307  if ( (decoded_data1 = decode(&data1_length))==NULL)
308  return 0;
309  }
310 
311  // see if our array is long enough
312  if (ich < 0 || ich >= data1_length) return 0;
313 
314  return decoded_data1[ich];
315 }
316 
317 // ------------------------------------------------------
318 
319 int Packet_A::iValue(const int ich, const char *what)
320 {
321  // now let's derefence the proxy array. If we didn't decode
322  // the data until now, we do it now
323  if (decoded_data1 == NULL )
324  {
325  if ( (decoded_data1 = decode(&data1_length))==NULL)
326  return 0;
327  }
328 
329  // see if our array is long enough
330  if (ich >= data1_length) return 0;
331 
332  return decoded_data1[ich];
333 }
334 
335 // ------------------------------------------------------
336 
337 int Packet_A::iValue(const int ich, const int iy)
338 {
339  // now let's derefence the proxy array. If we didn't decode
340  // the data until now, we do it now
341  if (decoded_data1 == NULL )
342  {
343  if ( (decoded_data1 = decode(&data1_length))==NULL)
344  return 0;
345  }
346 
347  // see if our array is long enough
348  if (ich > data1_length) return 0;
349 
350  return decoded_data1[ich];
351 }
352 
353 // ------------------------------------------------------
354 
355 float Packet_A::rValue(const int ich)
356 {
357  // now let's derefence the proxy array. If we didn't decode
358  // the data until now, we do it now
359  if (decoded_data1 == NULL )
360  {
361  if ( (decoded_data1 = decode(&data1_length))==NULL)
362  return 0;
363  }
364 
365  // see if our array is long enough
366  if (ich > data1_length) return 0;
367 
368  return float(decoded_data1[ich]);
369 }
370 
371 // ------------------------------------------------------
372 
373 float Packet_A::rValue(const int ich, const char *what)
374 {
375  // now let's derefence the proxy array. If we didn't decode
376  // the data until now, we do it now
377  if (decoded_data1 == NULL )
378  {
379  if ( (decoded_data1 = decode(&data1_length))==NULL)
380  return 0;
381  }
382 
383  // see if our array is long enough
384  if (ich > data1_length) return 0;
385 
386  return float(decoded_data1[ich]);
387 }
388 
389 // ------------------------------------------------------
390 
391 float Packet_A::rValue(const int ich, const int iy)
392 {
393  // now let's derefence the proxy array. If we didn't decode
394  // the data until now, we do it now
395  if (decoded_data1 == NULL )
396  {
397  if ( (decoded_data1 = decode(&data1_length))==NULL)
398  return 0;
399  }
400 
401  // see if our array is long enough
402  if (ich > data1_length) return 0;
403 
404  return float(decoded_data1[ich]);
405 }
406 
407 // ----------------------------------------------
408 
409 int
410 Packet_A::getArraylength(const char *what)
411 {
412  // now let's derefence the proxy array. If we didn't decode
413  // the data until now, we do it now
414  if (decoded_data1 == NULL )
415  {
416  if ( (decoded_data1 = decode(&data1_length))==NULL)
417  return -1;
418  }
419 
420  return data1_length;
421 }
422 
423 // ----------------------------------------------
424 
425 int
427  const int nlen, int *nwout,
428  const char *what)
429 {
430  return standardIntArray(iarr, nlen, nwout, what);
431 
432 }
433 
434 
435 
436 // ----------------------------------------------
437 
438 int
440  const int nlen, int *nwout,
441  const char *what)
442 {
443 
444  int *from;
445  int howmuch;
446 
447  *nwout = 0;
448 
449  if (strcmp(what,"") == 0)
450  {
451  // now let's derefence the proxy array. If we didn't decode
452  // the data until now, we do it now
453  if (decoded_data1 == NULL )
454  {
455  if ( (decoded_data1 = decode(&data1_length))==NULL)
456  {
457  *nwout=0;
458  return -1;
459  }
460  }
461  howmuch = data1_length;
462  from = decoded_data1;
463  }
464 
465  else if (strcmp(what,"RAW") == 0)
466  {
467  howmuch = getLength();
468  from = (int *) packet;
469  }
470 
471  else if (strcmp(what,"DATA") == 0)
472  {
473  howmuch = getDataLength();
474  from = (int *) findPacketDataStart(packet);
475  }
476 
477  else
478  {
479  *nwout = 0;
480  return 0;
481  }
482 
483  // see if by any chance we got a negative length (happens)
484  if (howmuch < 0)
485  {
486  *nwout = 0;
487  return -3;
488  }
489 
490  // see if our array is long enough
491  if (nlen < howmuch)
492  {
493  *nwout = 0;
494  return -2;
495  }
496 
497  if ( from == 0)
498  {
499  *nwout = 0;
500  return -1;
501  }
502 
503  // and copy the data to the output array
504  memcpy(iarr, from, 4*howmuch);
505 
506  // tell how much we copied
507  *nwout = howmuch;
508  return 0;
509 }
510 
511 // ------------------------------------------------------
512 
513 
514 int Packet_A::copyMe(int dest[], const int maxlength) const
515 {
516 
517  if ( getLength() > maxlength )
518  {
519  return 0;
520  }
521 
522  memcpy((void *) dest, (void *) packet, 4*getLength() );
523  return getLength();
524 }
525 
526 
527 
528 // ------------------------------------------------------
529 
530 
531 
532 int
534  const int nlen, int *nwout,
535  const char *what)
536 {
537 
538  // now let's derefence the proxy array. If we didn't decode
539  // the data until now, we do it now
540  if (decoded_data1 == NULL )
541  {
542  if ( (decoded_data1 = decode(&data1_length))==NULL)
543  {
544  *nwout=0;
545  return -1;
546  }
547  }
548 
549  // see if our array is long enough
550  if (nlen < data1_length) return -2;
551 
552  // and copy the data to the output array
553  int *from = decoded_data1;
554  for (int i=0; i<data1_length; i++) *rarr++ = float (*from++);
555 
556  // tell how much we copied
557  *nwout = data1_length;
558  return 0;
559 }
560 
561 // ------------------------------------------------------
562 int*
563 Packet_A::getIntArray (int *nwout, const char *what)
564 {
565 
566  // now let's derefence the proxy array. If we didn't decode
567  // the data until now, we do it now
568  if (decoded_data1 == NULL )
569  {
570  if ( (decoded_data1 = decode(&data1_length))==NULL)
571  {
572  *nwout=0;
573  return NULL;
574  }
575  }
576 
577  int *temp=new int[data1_length];
578  int is = fillIntArray(temp, data1_length, nwout);
579  if (is)
580  {
581  *nwout=0;
582  return NULL;
583  }
584  return temp;
585 }
586 
587 // ------------------------------------------------------
588 float* Packet_A::getFloatArray (int *nwout, const char *what)
589 {
590  // now let's derefence the proxy array. If we didn't decode
591  // the data until now, we do it now
592  if (decoded_data1 == NULL )
593  {
594  if ( (decoded_data1 = decode(&data1_length))==NULL)
595  {
596  *nwout=0;
597  return NULL;
598  }
599  }
600 
601  float *temp=new float[data1_length];
602  int is = fillFloatArray(temp, data1_length, nwout);
603  if (is)
604  {
605  *nwout=0;
606  return NULL;
607  }
608  return temp;
609 }
610 
611 // ------------------------------------------------------
613 {
614  int el = getErrorLength();
615  if (!el )
616  {
617  // out << "--- No error block" << std::endl;
618  return;
619  }
620 
621  int j,l;
622 
623  int * data = (int *) findPacketErrorStart (packet);
624 
625  // here we make sure that the packet isn't obviously bogus,
626  // although it's not a comprehensive check.
627  if ( el > getLength() || data < (int *) packet ) return;
628 
629 
630  j = 0;
631  COUT << "--- Error block:" << std::endl;
632  while (1)
633  {
634  out << SETW(5) << j << " | ";
635  for (l=0;l<4;l++)
636  {
637  out << std::hex << SETW(8) << data[j++] << " " ;
638  if (j >= el ) break;
639  }
640  if (j >= el ) break;
641  out << std::dec<< std::endl;
642  }
643  out << std::dec << std::endl;
644 }
645 
646 
647 
648 // ------------------------------------------------------
650 {
651  int el = getDebugLength();
652  if (!el )
653  {
654  // out << "--- No Debug block" << std::endl;
655  return;
656  }
657 
658  int j,l;
659 
660  int * data = (int *) findPacketDebugStart (packet);
661 
662  if ( el > getLength() || data < (int *) packet ) return;
663 
664  j = 0;
665  COUT << "--- Debug block:" << std::endl;
666  while (1)
667  {
668  out << SETW(5) << j << " | ";
669  for (l=0;l<4;l++)
670  {
671  out << std::hex << SETW(8) << data[j++] << " " ;
672  if (j >= el ) break;
673  }
674  if (j >= el ) break;
675  out << std::dec<< std::endl;
676  }
677  out << std::dec<< std::endl;
678 
679  out << "--- DCM Checksum: " << getCheckSumStatus() << std::endl;
680 
681 }
682 
683 #ifdef LVL2_WINNT
684  void fix_endianess ( LONGLONG *x)
685 #else
686  void fix_endianess ( long long *x)
687 #endif
688 {
689  unsigned int *i = (unsigned int *) x;
690  unsigned int r = i[0];
691  i[0] = i[1];
692  i[1] = r;
693 }
694 
695 
696 void Packet_A::fix_endianess ( double *x)
697 {
698  unsigned int *i = (unsigned int *) x;
699  unsigned int r = i[0];
700  i[0] = i[1];
701  i[1] = r;
702 }
703 
704 void Packet_A::fix_endianess ( char *str, const int length)
705 {
706  int j;
707  unsigned int *i = (unsigned int *) str;
708  for (j = 0; j< length/4; j++) i[j] = buffer::i4swap(i[j]);
709 
710 }
711 
712 
713 
715 {
716 
717  // if we don't have a debug block, wearedone (but don't
718  // know what the answer is...
719 
720  if ( getDebugLength() == 0 ) return 0;
721  int i;
722 
723  int *d = (int *) packet;
724 
725  int checksum_recalc = 0x0;
726  for (i = 0;i < getLength() - 2;i++)
727  {
728  checksum_recalc ^= *d++;
729  }
730  d++;
731 
732  // COUT << "Checksum : " << std::hex << checksum_recalc << " " << *d << std::dec << ENDL;
733 
734  if (checksum_recalc != *d) return -1;
735  return 1;
736 }
737 
738