Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:39

0001 #include "packet_A.h"
0002 #include "packetHeaders.h"
0003 #include  <string.h>
0004 #include "buffer.h"
0005 
0006 Packet_A::Packet_A()
0007 {
0008 
0009   data1_length = 0;
0010   data2_length = 0;
0011   data3_length = 0;
0012   data4_length = 0;
0013   data5_length = 0;
0014   data6_length = 0;
0015   data7_length = 0;
0016   data8_length = 0;
0017   data9_length = 0;
0018   data10_length = 0;
0019 
0020   decoded_data1 = NULL;
0021   decoded_data2 = NULL;
0022   decoded_data3 = NULL;
0023   decoded_data4 = NULL;
0024   decoded_data5 = NULL;
0025   decoded_data6 = NULL;
0026   decoded_data7 = NULL;
0027   decoded_data8 = NULL;
0028   decoded_data9 = NULL;
0029   decoded_data10 = NULL;
0030 
0031   is_data_type = 0;  //assume "p" type first.
0032 }
0033 
0034 Packet_A::Packet_A(PACKET_ptr packet_ptr)
0035 {
0036   
0037   packet = packet_ptr;
0038   //  packetHdr = ( PACKETHDR_ptr ) packet;
0039 
0040   data1_length = 0;
0041   data2_length = 0;
0042   data3_length = 0;
0043   data4_length = 0;
0044   data5_length = 0;
0045   data6_length = 0;
0046   data7_length = 0;
0047   data8_length = 0;
0048   data9_length = 0;
0049   data10_length = 0;
0050 
0051   decoded_data1 = NULL;
0052   decoded_data2 = NULL;
0053   decoded_data3 = NULL;
0054   decoded_data4 = NULL;
0055   decoded_data5 = NULL;
0056   decoded_data6 = NULL;
0057   decoded_data7 = NULL;
0058   decoded_data8 = NULL;
0059   decoded_data9 = NULL;
0060   decoded_data10 = NULL;
0061 
0062 
0063   is_data_type = 0;  //assume "p" type first.
0064 }
0065 
0066 // ----------------------------------------------
0067 
0068 Packet_A::~Packet_A()
0069 {
0070 
0071   if (decoded_data1 != NULL) 
0072     {
0073       delete [] decoded_data1;
0074       decoded_data1 = 0;
0075     }
0076   if (decoded_data2 != NULL) 
0077     {
0078       delete [] decoded_data2;
0079       decoded_data2 = 0;
0080     }
0081   if (decoded_data3 != NULL) 
0082     {
0083       delete [] decoded_data3;
0084       decoded_data3 = 0;
0085     }
0086   if (decoded_data4 != NULL) 
0087     {
0088       delete [] decoded_data4;
0089       decoded_data4 = 0;
0090     }
0091   if (decoded_data5 != NULL) 
0092     {
0093       delete [] decoded_data5;
0094       decoded_data5 = 0;
0095     }
0096   if (decoded_data6 != NULL) 
0097     {
0098       delete [] decoded_data6;
0099       decoded_data6 = 0;
0100     }
0101   if (decoded_data7 != NULL) 
0102     {
0103       delete [] decoded_data7;
0104       decoded_data7 = 0;
0105     }
0106   if (decoded_data8 != NULL) 
0107     {
0108       delete [] decoded_data8;
0109       decoded_data8 = 0;
0110     }
0111   if (decoded_data9 != NULL) 
0112     {
0113       delete [] decoded_data9;
0114       decoded_data9 = 0;
0115     }
0116   if (decoded_data10 != NULL) 
0117     {
0118       delete [] decoded_data10;
0119       decoded_data10 = 0;
0120     }
0121 
0122   if (is_data_type) delete [] packet;
0123 }
0124 
0125 
0126 int Packet_A::is_pointer_type() const
0127 {
0128   if (is_data_type) return 0;
0129   else return 1;
0130 }
0131 
0132 // ----------------------------------------------
0133 
0134 int Packet_A::convert()
0135 {
0136   if (is_data_type) return -1;
0137 
0138   PHDWORD *tmp;
0139 
0140   tmp = new PHDWORD[getLength()];
0141   PHDWORD *from=  packet;
0142   PHDWORD *to=tmp;
0143   for (int k=0; k< getLength(); k++) 
0144     { 
0145       *to++ = *from++;
0146     }
0147   packet = tmp;
0148   is_data_type = 1;
0149   return 0;
0150 
0151 }
0152 
0153 // ------------------------------------------------------
0154 int Packet_A::getLength() const
0155 {
0156   return getPacketLength (packet) ;
0157 }
0158 
0159 // ------------------------------------------------------ 
0160 int Packet_A::getErrorLength() const { return
0161 getPacketErrorLength(packet); } 
0162 
0163 //------------------------------------------------------ 
0164 int Packet_A::getDebugLength() const { return
0165 getPacketDebugLength(packet); } 
0166 
0167 //------------------------------------------------------ 
0168 int Packet_A::getIdentifier() const { return getPacketId (packet); } 
0169 
0170 //------------------------------------------------------ 
0171 int Packet_A::setIdentifier(const int newid) 
0172 {
0173    // check that we have short range; we want the parameter to be int
0174   if ( newid & 0xffff0000) return -1;
0175   setPacketId (packet, newid);
0176   return 0;
0177 }
0178 
0179 
0180 //------------------------------------------------------ 
0181 int Packet_A::getPadding() const { 
0182 return 
0183 getPacketPadding(packet); 
0184 }
0185 
0186 // ------------------------------------------------------
0187 int Packet_A::getStructure() const
0188 {
0189   return getPacketStructure (packet);
0190 }
0191 
0192 // ------------------------------------------------------
0193 int Packet_A::getHitFormat() const
0194 {
0195   return getUnstructPacketHitFormat (packet);
0196 }
0197 
0198 // ------------------------------------------------------
0199 //int Packet_A::getWordSize() const
0200 //{
0201 //  return ???  // ** action
0202 //
0203 //}
0204 
0205 // ------------------------------------------------------
0206 int Packet_A::getDataLength() const
0207 {
0208   return getPacketDataLength (packet);
0209 }
0210 
0211 // ------------------------------------------------------
0212 
0213 void  Packet_A::fullIdentify( OSTREAM& out ) const
0214 {
0215   out << std::dec;
0216   out << "*** Packet id         " <<  getPacketId (packet) << std::endl;
0217   out << "    Hdr version       " <<  getPacketHdrVersion (packet) << std::endl;
0218   out << "    Hdr length        " <<  getPacketHdrLength (packet) << std::endl;
0219   out << "    Packet Length     " <<  getPacketLength (packet) << std::endl;
0220   out << "    Packet Status     " <<  getPacketStatus (packet) << std::endl;
0221   out << "    Debug Length      " <<  getPacketDebugLength (packet) << std::endl;
0222   out << "    Error Length      " <<  getPacketErrorLength (packet) << std::endl;
0223   out << "    Structure         " <<  getStructure() << " (";
0224 
0225   switch (getStructure()) 
0226     {
0227       
0228     case Unstructured:
0229       out << "Unformatted)  "; 
0230       break;
0231       
0232     case Packets:
0233       out << "Unformatted)  "; 
0234       out << "Packets    )  "; 
0235       break;
0236       
0237     case hitArray:
0238       out << "HitArray   )  "; 
0239       break;
0240       
0241     case hitList:
0242       out << "HitList    )  "; 
0243       break;
0244     }
0245   out << std::endl;
0246   
0247   out << "    Descriptor Words: " <<  getPacketDataDescrLength (packet) << std::endl;
0248   out << "    Endianism:        " <<  getPacketEndianism (packet);
0249   
0250   switch (getPacketEndianism (packet) )
0251     {
0252       
0253     case 1:
0254       out << " (Little Endian) " << std::endl;
0255       break;
0256       
0257     case 2:
0258       out << " (Big Endian) " << std::endl;
0259       break;
0260       
0261     default:
0262       out << " (Unknown) " << std::endl;
0263       break;
0264     }
0265 
0266   out << "    Padding:          " <<  getPacketPadding (packet) << std::endl;
0267 
0268 
0269 }
0270 
0271 void  Packet_A::identify( OSTREAM& out ) const
0272 {
0273   // out << "identify packet type A" << std::endl;
0274   out << std::dec
0275       << "Packet " << SETW(6) <<  getIdentifier() 
0276       << " " << SETW(5) << getLength() 
0277       << " " << SETW(2) << getStructure() << " (";
0278 
0279   switch (getStructure()) 
0280     {
0281 
0282     case Unstructured:
0283       out << "Unformatted)  "; 
0284       break;
0285       
0286     case Packets:
0287       out << "Unformatted)  "; 
0288       out << "Packets    )  "; 
0289       break;
0290       
0291     case hitArray:
0292       out << "HitArray   )  "; 
0293       break;
0294 
0295     case hitList:
0296       out << "HitList    )  "; 
0297       break;
0298     }
0299 
0300   out << SETW(4) << getHitFormat() 
0301       << " (" << get_mnemonic( getStructure(), getHitFormat()) << ")";
0302 
0303   if (getErrorLength() ) out << " ** Error Block";
0304 
0305   out << std::endl;
0306 }
0307 
0308 
0309 // ------------------------------------------------------
0310 
0311 int Packet_A::iValue(const int ich)
0312 {
0313   // now let's derefence the proxy array. If we didn't decode
0314   // the data until now, we do it now
0315   if (decoded_data1 == NULL )
0316     {
0317       if ( (decoded_data1 = decode(&data1_length))==NULL)
0318     return 0;
0319     }
0320 
0321   // see if our array is long enough
0322   if (ich < 0 || ich >= data1_length) return 0;
0323 
0324   return decoded_data1[ich];
0325 }
0326 
0327 // ------------------------------------------------------
0328 
0329 int   Packet_A::iValue(const int ich, const char *what)
0330 {
0331   // now let's derefence the proxy array. If we didn't decode
0332   // the data until now, we do it now
0333   if (decoded_data1 == NULL )
0334     {
0335       if ( (decoded_data1 = decode(&data1_length))==NULL)
0336     return 0;
0337     }
0338 
0339   // see if our array is long enough
0340   if (ich >= data1_length) return 0;
0341 
0342   return decoded_data1[ich];
0343 }
0344 
0345 // ------------------------------------------------------
0346 
0347 int   Packet_A::iValue(const int ich, const int iy)
0348 {
0349   // now let's derefence the proxy array. If we didn't decode
0350   // the data until now, we do it now
0351   if (decoded_data1 == NULL )
0352     {
0353       if ( (decoded_data1 = decode(&data1_length))==NULL)
0354     return 0;
0355     }
0356 
0357   // see if our array is long enough
0358   if (ich > data1_length) return 0;
0359 
0360   return decoded_data1[ich];
0361 }
0362 
0363 // ------------------------------------------------------
0364 
0365 float Packet_A::rValue(const int ich)
0366 {
0367   // now let's derefence the proxy array. If we didn't decode
0368   // the data until now, we do it now
0369   if (decoded_data1 == NULL )
0370     {
0371       if ( (decoded_data1 = decode(&data1_length))==NULL)
0372     return 0;
0373     }
0374 
0375   // see if our array is long enough
0376   if (ich > data1_length) return 0;
0377 
0378   return float(decoded_data1[ich]);
0379 }
0380 
0381 // ------------------------------------------------------
0382 
0383 float Packet_A::rValue(const int ich, const char *what)
0384 {
0385   // now let's derefence the proxy array. If we didn't decode
0386   // the data until now, we do it now
0387   if (decoded_data1 == NULL )
0388     {
0389       if ( (decoded_data1 = decode(&data1_length))==NULL)
0390     return 0;
0391     }
0392 
0393   // see if our array is long enough
0394   if (ich > data1_length) return 0;
0395 
0396   return float(decoded_data1[ich]);
0397 }
0398 
0399 // ------------------------------------------------------
0400 
0401 float Packet_A::rValue(const int ich, const int iy)
0402 {
0403   // now let's derefence the proxy array. If we didn't decode
0404   // the data until now, we do it now
0405   if (decoded_data1 == NULL )
0406     {
0407       if ( (decoded_data1 = decode(&data1_length))==NULL)
0408     return 0;
0409     }
0410 
0411   // see if our array is long enough
0412   if (ich > data1_length) return 0;
0413 
0414   return float(decoded_data1[ich]);
0415 }
0416 
0417 // ----------------------------------------------
0418 
0419 int   
0420 Packet_A::getArraylength(const char *what)
0421 {
0422   // now let's derefence the proxy array. If we didn't decode
0423   // the data until now, we do it now
0424   if (decoded_data1 == NULL )
0425     {
0426       if ( (decoded_data1 = decode(&data1_length))==NULL)
0427     return -1;
0428     }
0429 
0430   return data1_length;
0431 }
0432 
0433 // ----------------------------------------------
0434 
0435 int
0436 Packet_A::fillIntArray (int iarr[],
0437             const int nlen, int *nwout,
0438             const char *what)
0439 {
0440   return standardIntArray(iarr, nlen, nwout, what);
0441 
0442 }
0443 
0444 
0445 
0446 // ----------------------------------------------
0447 
0448 int
0449 Packet_A::standardIntArray (int iarr[],
0450             const int nlen, int *nwout,
0451             const char *what)
0452 {
0453 
0454   int *from;
0455   int howmuch;
0456 
0457   *nwout = 0;
0458 
0459   if (strcmp(what,"") == 0)
0460     {
0461       // now let's derefence the proxy array. If we didn't decode
0462       // the data until now, we do it now
0463       if (decoded_data1 == NULL )
0464     {
0465       if ( (decoded_data1 = decode(&data1_length))==NULL)
0466         {
0467           *nwout=0;
0468           return -1;
0469         }
0470     }
0471       howmuch = data1_length;
0472       from = decoded_data1;
0473     }
0474 
0475   else if (strcmp(what,"RAW") == 0)
0476     {
0477       howmuch = getLength();
0478       from = (int *) packet;
0479     }
0480 
0481   else if (strcmp(what,"DATA") == 0)
0482     {
0483       howmuch = getDataLength();
0484       from = (int *) findPacketDataStart(packet);
0485     }
0486 
0487   else 
0488     {
0489       *nwout = 0;
0490       return 0;
0491     }
0492 
0493   // see if by any chance we got a negative length (happens)
0494   if (howmuch < 0)
0495     {
0496       *nwout = 0;
0497       return -3;
0498     }
0499 
0500   // see if our array is long enough
0501   if (nlen < howmuch) 
0502     {
0503       *nwout = 0;
0504       return -2;
0505     }
0506 
0507   if ( from == 0) 
0508     { 
0509       *nwout = 0;
0510       return -1;
0511     }
0512   
0513   // and copy the data to the output array
0514   memcpy(iarr, from, 4*howmuch);
0515 
0516   // tell how much we copied
0517   *nwout = howmuch;
0518   return 0;
0519 }
0520 
0521 // ------------------------------------------------------
0522 
0523 
0524 int Packet_A::copyMe(int dest[],  const int maxlength) const
0525 {
0526   
0527   if ( getLength() > maxlength )
0528     {
0529       return 0;
0530     }
0531 
0532   memcpy((void *) dest, (void *) packet, 4*getLength() );
0533   return getLength();
0534 }
0535 
0536 
0537 
0538 // ------------------------------------------------------
0539 
0540 
0541 
0542 int
0543 Packet_A::fillFloatArray (float rarr[],
0544               const int nlen, int *nwout,
0545               const char *what)
0546 {
0547 
0548   // now let's derefence the proxy array. If we didn't decode
0549   // the data until now, we do it now
0550   if (decoded_data1 == NULL )
0551     {
0552       if ( (decoded_data1 = decode(&data1_length))==NULL)
0553     {
0554       *nwout=0;
0555       return -1;
0556     }
0557     }
0558 
0559   // see if our array is long enough
0560   if (nlen < data1_length) return -2;
0561 
0562   // and copy the data to the output array
0563   int *from = decoded_data1;
0564   for (int i=0; i<data1_length; i++) *rarr++ = float (*from++);
0565 
0566   // tell how much we copied
0567   *nwout = data1_length;
0568   return 0;
0569 }
0570 
0571 // ------------------------------------------------------
0572 int*   
0573 Packet_A::getIntArray (int *nwout, const char *what)
0574 {
0575 
0576   // now let's derefence the proxy array. If we didn't decode
0577   // the data until now, we do it now
0578   if (decoded_data1 == NULL )
0579     {
0580       if ( (decoded_data1 = decode(&data1_length))==NULL)
0581     {
0582       *nwout=0;
0583       return NULL;
0584     }
0585     }
0586 
0587   int *temp=new int[data1_length];
0588   int is = fillIntArray(temp, data1_length, nwout);
0589   if (is)
0590     {
0591       *nwout=0;
0592       return NULL;
0593     }
0594   return temp;
0595 }
0596 
0597 // ------------------------------------------------------
0598 float*   Packet_A::getFloatArray (int *nwout, const char *what)
0599 {
0600   // now let's derefence the proxy array. If we didn't decode
0601   // the data until now, we do it now
0602   if (decoded_data1 == NULL )
0603     {
0604       if ( (decoded_data1 = decode(&data1_length))==NULL)
0605     {
0606       *nwout=0;
0607       return NULL;
0608     }
0609     }
0610 
0611   float *temp=new float[data1_length];
0612   int is = fillFloatArray(temp, data1_length, nwout);
0613   if (is)
0614     {
0615       *nwout=0;
0616       return NULL;
0617     }
0618   return temp;
0619 }
0620 
0621 // ------------------------------------------------------
0622 void  Packet_A::dumpErrorBlock ( OSTREAM& out ) 
0623 {
0624   int el = getErrorLength();
0625   if (!el ) 
0626     {
0627       //     out << "--- No error block" << std::endl;
0628       return;
0629     }
0630 
0631   int j,l;
0632 
0633   int * data = (int *) findPacketErrorStart (packet);
0634 
0635   // here we make sure that the packet isn't obviously bogus,
0636   // although it's not a comprehensive check. 
0637   if ( el > getLength()  || data < (int *) packet ) return;
0638 
0639 
0640   j = 0;
0641   COUT << "--- Error block:" << std::endl;
0642   while (1)
0643     {
0644       out << SETW(5) << j << " |  ";
0645       for (l=0;l<4;l++)
0646     {
0647       out << std::hex << SETW(8) << data[j++] << " " ;
0648       if (j >= el ) break;
0649     }
0650       if (j >= el ) break;
0651       out << std::dec<< std::endl;
0652     }
0653   out << std::dec << std::endl;
0654 }
0655 
0656 
0657 
0658 // ------------------------------------------------------
0659 void   Packet_A::dumpDebugBlock ( OSTREAM& out ) 
0660 {
0661   int el = getDebugLength();
0662   if (!el ) 
0663     {
0664       //      out << "--- No Debug block" << std::endl;
0665       return;
0666     }
0667 
0668   int j,l;
0669 
0670   int * data = (int *) findPacketDebugStart (packet);
0671 
0672   if ( el > getLength()  || data < (int *) packet ) return;
0673 
0674   j = 0;
0675   COUT << "--- Debug block:" << std::endl;
0676   while (1)
0677     {
0678       out << SETW(5) << j << " |  ";
0679       for (l=0;l<4;l++)
0680     {
0681       out << std::hex << SETW(8) << data[j++] << " " ;
0682       if (j >= el ) break;
0683     }
0684       if (j >= el ) break;
0685       out << std::dec<< std::endl;
0686     }
0687   out <<  std::dec<<  std::endl;
0688 
0689   out << "--- DCM Checksum: " << getCheckSumStatus() << std::endl;
0690 
0691 }
0692 
0693 #ifdef LVL2_WINNT
0694   void fix_endianess ( LONGLONG *x)
0695 #else
0696   void fix_endianess ( long long *x)
0697 #endif
0698 {
0699     unsigned int *i = (unsigned int *) x;
0700     unsigned int r = i[0];
0701     i[0] = i[1];
0702     i[1] = r;
0703 }
0704 
0705 
0706 void Packet_A::fix_endianess ( double *x)
0707 {
0708     unsigned int *i = (unsigned int *) x;
0709     unsigned int r = i[0];
0710     i[0] = i[1];
0711     i[1] = r;
0712 }
0713 
0714 void Packet_A::fix_endianess ( char *str, const int length)
0715 {
0716   int j;
0717   unsigned int *i = (unsigned int *) str;
0718   for (j = 0; j< length/4; j++) i[j] = buffer::i4swap(i[j]);
0719 
0720 }
0721 
0722 
0723 
0724 int Packet_A::getCheckSumStatus() const
0725 {
0726 
0727   // if we don't have a debug block, wearedone (but don't
0728   // know what the answer is...
0729 
0730   if ( getDebugLength() == 0 ) return 0;
0731   int i;
0732 
0733   int *d = (int *) packet;
0734 
0735   int checksum_recalc = 0x0;
0736   for (i = 0;i < getLength() - 2;i++)
0737     {
0738       checksum_recalc ^= *d++;
0739     }
0740   d++;
0741 
0742   //  COUT << "Checksum : " << std::hex << checksum_recalc << "   " << *d << std::dec << ENDL;
0743 
0744   if (checksum_recalc != *d) return -1;
0745   return 1;
0746 }
0747 
0748