/[PAMELA software]/chewbacca/PamOffLineSW/techmodel/McmdReader.cpp
ViewVC logotype

Diff of /chewbacca/PamOffLineSW/techmodel/McmdReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by mocchiut, Tue Sep 23 07:20:26 2008 UTC revision 1.5 by mocchiut, Thu Feb 11 09:02:12 2010 UTC
# Line 1  Line 1 
1  /** @file  /** @file
2   * $Source: /repository/PamOffLineSW/techmodel/McmdReader.cpp,v $   * $Source: /afs/ba.infn.it/user/pamela/src/CVS/chewbacca/PamOffLineSW/techmodel/McmdReader.cpp,v $
3   * $Id: McmdReader.cpp,v 1.7 2008-03-06 18:33:36 messineo Exp $   * $Id: McmdReader.cpp,v 1.4 2009/09/30 12:21:38 mocchiut Exp $
4   * $Author: messineo $   * $Author: mocchiut $
5   *   *
6   * Implementation of the McmdReader class.   * Implementation of the McmdReader class.
7   */   */
8    
9  extern "C" {  extern "C" {
10          #include "CRC.h"  #include "CRC.h"
11  }  }
12  #include "ReaderAlgorithms.h"  #include "ReaderAlgorithms.h"
13  using namespace pamela::techmodel;  using namespace pamela::techmodel;
14    
15  /**  /**
16   * Constructor.   * Constructor.
17   */   */
18  McmdReader::McmdReader(void):  McmdReader::McmdReader(void) :
19    TechmodelAlgorithm(PacketType::Mcmd, "Mcmd") {    TechmodelAlgorithm(PacketType::Mcmd, "Mcmd") {
20    Mcmd = new McmdEvent();    Mcmd = new McmdEvent();
21  }  }
22    
# Line 24  McmdReader::McmdReader(void): Line 24  McmdReader::McmdReader(void):
24   * Get a string with the version info of the algorithm.   * Get a string with the version info of the algorithm.
25   */   */
26  std::string McmdReader::GetVersionInfo(void) const {  std::string McmdReader::GetVersionInfo(void) const {
27    return    return "$Header: /afs/ba.infn.it/user/pamela/src/CVS/chewbacca/PamOffLineSW/techmodel/McmdReader.cpp,v 1.4 2009/09/30 12:21:38 mocchiut Exp $\n";
     "$Header: /repository/PamOffLineSW/techmodel/McmdReader.cpp,v 1.7 2008-03-06 18:33:36 messineo Exp $\n";  
28  }  }
29    
30  /**  /**
31   * Initialize the algorithm with a special run. This will initialize the   * Initialize the algorithm with a special run. This will initialize the
32   * event reader routines for all packet types.   * event reader routines for all packet types.
33   */   */
34  void McmdReader::Init(PamelaRun *run) {  void McmdReader::Init(PamelaRun *run) {
35   // logger->debug(_T("Initialize"));    // logger->debug(_T("Initialize"));
36   // SetInputStream(run);    // SetInputStream(run);
37    run->WriteSubPacket(this, &Mcmd, Mcmd->Class());    run->WriteSubPacket(this, &Mcmd, Mcmd->Class());
38  }  }
39    
40  /**  /**
41   * Unpack the Mcmd event   * Unpack the Mcmd event
42   *  Each subpacket is prafaceded by 4 OBT bytes.   *  Each subpacket is prafaceded by 4 OBT bytes.
43   *  The elementar structure is a kind of   *  The elementar structure is a kind of
44   * --------CPU - OBT---------------------------------   * --------CPU - OBT---------------------------------
45   *  OBT     - 4 Bytes   *  OBT     - 4 Bytes
46   * --------Start Sub-Packet---------------------------------   * --------Start Sub-Packet---------------------------------
# Line 61  void McmdReader::Init(PamelaRun *run) { Line 60  void McmdReader::Init(PamelaRun *run) {
60   * @Event Number   * @Event Number
61   * @length is the size in bytes of the event (or packet)   * @length is the size in bytes of the event (or packet)
62   */   */
63  void McmdReader::PKT_RunEvent(char* mysubData, long int length) throw (WrongCRCException_PKTUsed)  void McmdReader::PKT_RunEvent(char* mysubData, long int length) throw (WrongCRCException_PKTUsed) {
64  {              stringstream oss;
65          stringstream oss;    string msg;
66          string msg;      
67              int i = 0;
68          int i = 0;    const int obt_len = 4;
69          const int obt_len = 4;    const int subHeader_len = 10;
70          const int subHeader_len = 10;    const int subTrailer_len = 2;
71          const int subTrailer_len = 2;    const int subPckCRC_len = 2;
72          const int subPckCRC_len = 2;    long int dataLength = 0;
73          long int  dataLength= 0;  
74      char OBT[obt_len] = { 0 };
75          char OBT[obt_len]={0};    char subHeader[subHeader_len] = { 0 };
76          char subHeader[subHeader_len]={0};    char subTrailer[subTrailer_len] = { 0 };
77          char subTrailer[subTrailer_len]={0};    char subPckCRC[subPckCRC_len] = { 0 };
78          char subPckCRC[subPckCRC_len]={0};    char* subData = NULL;
79          char* subData=NULL;  
80      UINT16 subCRC = 0; //CRC of the subpacket (updated as subPckt is read)
81          UINT16    subCRC= 0;      //CRC of the subpacket (updated as subPckt is read)    UINT16 readCRC = 0; //CRC read from the subpacket
82          UINT16    readCRC= 0;     //CRC read from the subpacket    UINT16 partialCRC = 0; //partial CRC updated as mcmd packet is read (to compare with CRC read in the total Mcmd header)
83          UINT16    partialCRC= 0; //partial CRC updated as mcmd packet is read (to compare with CRC read in the total Mcmd header)    McmdRecord *rec;
84          McmdRecord *rec;  
85        Mcmd->Records->Clear();
86          Mcmd->Records->Clear();    TClonesArray &recs = *(Mcmd->Records);
87          TClonesArray &recs = *(Mcmd->Records);  
88      int start = 0;
89          int start=0;    //    while(start < (length)) {
90        //      printf(" %i => %X \n",start,(UINT8)mysubData[start]);
91          while(start < (length-2-obt_len-subTrailer_len-subTrailer_len-subPckCRC_len))    //      start++;
92          {    //    };
93                  //Read the OBT preceeding the subpacket then calculate a partial CRC for it    //    start=0;
94                  //and update the partialCRC  
95                  for(int m=start; m<obt_len; m++){    while (start < (length - 2 - obt_len - subTrailer_len - subTrailer_len - subPckCRC_len))
96                    OBT[m-start]=mysubData[m];    //    while(start < (length-2))
97                  }                  {
98                  start+=obt_len;      //    printf(" 1data %X \n",(UINT8)mysubData[start]);
99                        //Read the OBT preceeding the subpacket then calculate a partial CRC for it
100                  subCRC = CM_Compute_CRC16(0, (BYTE*)&OBT, obt_len);      //and update the partialCRC
101                  partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&OBT, obt_len);      for (int m = start; m < start + obt_len; m++) {
102                  //Read the header for the subPacket and read mcmdLength        OBT[m - start] = mysubData[m];
103                  //12 is the total lenght of subHeader + subTrailer      }
104                  for(int m=start; m<subHeader_len; m++){      start += obt_len;
105                     subHeader[m-start]=mysubData[m];  
106                  }                    //    printf(" 2data %X \n",(UINT8)mysubData[start]);
107                  start+=subHeader_len;      //    printf(" 2data %X \n",(UINT8)mysubData[start]+1);
108                subCRC = CM_Compute_CRC16(0, (BYTE*) &OBT, obt_len);
109              subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subHeader, subHeader_len);      partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &OBT, obt_len);
110              partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subHeader, subHeader_len);                //Read the header for the subPacket and read mcmdLength
111              dataLength = (((0x0fff)&((UINT16)subHeader[4]))<<8|((UINT16)subHeader[5])*2) - subHeader_len - subTrailer_len;        //12 is the total lenght of subHeader + subTrailer
112              //the line below is exactly how it was in the original version      for (int m = start; m < start + subHeader_len; m++) {
113              if (dataLength < 0) break; //it should throw an exception ***TBD***        subHeader[m - start] = mysubData[m];
114              //I add also this check      }
115              if (dataLength + start > (length-2-subTrailer_len-subPckCRC_len))      start += subHeader_len;
116              {  
117                   oss.str("");      //    printf(" 3data %X \n",(UINT8)mysubData[start]);
118                       oss<<"MCMD: Error in the MCMD Packet lenght, it is not " <<length;      //    printf(" 3data %X \n",(UINT8)mysubData[start+1]);
119                   msg=oss.str();      subCRC = CM_Compute_CRC16(subCRC, (BYTE*) &subHeader, subHeader_len);
120                   PamOffLineSW::mainLogUtil->logWarning(msg);                          partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subHeader, subHeader_len);
121                  break; //it should throw an exception ***TBD***      dataLength = (((0x0fff) & ((UINT16)subHeader[4])) << 8 | ((UINT16)subHeader[5]) * 2) - subHeader_len
122              }          - subTrailer_len;
123                    //      printf(" datalenght = %i  subh 4 %X subd 5 %X \n",dataLength,(UINT8)subHeader[4],(UINT8)subHeader[5]);
124              //read subpacket data according to data length then calculate partial CRC for data      //the line below is exactly how it was in the original version
125               //and update the partialCRC      if (dataLength < 0)
126                        break; //it should throw an exception ***TBD***
127             subData = new char[dataLength];      //I add also this check
128             for(int m=start; m<dataLength; m++){      if (dataLength + start > (length - 2 - subTrailer_len - subPckCRC_len)) {
129                     subData[m-start]=mysubData[m];        oss.str("");
130               }        oss << "MCMD: Error in the MCMD Packet lenght, it is not " << length;
131             start+=dataLength;        msg = oss.str();
132                    PamOffLineSW::mainLogUtil->logWarning(msg);
133             for (int jj = 0; jj < dataLength ; jj++){        break; //it should throw an exception ***TBD***
134                 subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(subData+jj), 1);      }
135                 partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)(subData+jj), 1);  
136             }      //read subpacket data according to data length then calculate partial CRC for data
137                //and update the partialCRC
138             //Read the CRC inside of MCMD  
139             //To check sum of all sub packet data, module 256 ****TBD****      subData = new char[dataLength];
140             for(int m=start; m<subTrailer_len; m++){      for (int m = start; m < start + dataLength; m++) {
141                     subTrailer[m-start]=mysubData[m];        subData[m - start] = mysubData[m];
142              }      }
143             start+=subTrailer_len;      start += dataLength;
144            
145             subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subTrailer, 2);      //    printf(" 4data %X \n",(UINT8)mysubData[start]);
146             partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subTrailer, 2);          //    printf(" 4data %X \n",(UINT8)mysubData[start+1]);
147             //Read the CRC of subPacket            for (int jj = 0; jj < dataLength; jj++) {
148             for(int m=start; m<subPckCRC_len; m++){        subCRC = CM_Compute_CRC16(subCRC, (BYTE*) (subData + jj), 1);
149                          subPckCRC[m-start]=mysubData[m];        partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) (subData + jj), 1);
150             }      }
151             start+=subPckCRC_len;  
152                //Read the CRC inside of MCMD
153             partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subPckCRC, 2);      //To check sum of all sub packet data, module 256 ****TBD****
154             readCRC = (((BYTE)subPckCRC[0])<<8) + ((BYTE)subPckCRC[1]);        for (int m = start; m < start + subTrailer_len; m++) {
155                  subTrailer[m - start] = mysubData[m];
156            //finally check if the RecordCRC is correct      }
157           //and finally update the partialCRC      start += subTrailer_len;
158           //TO DO - if one CRC is wrong also the total one will be corrupted      //     printf(" 5data %X \n",(UINT8)mysubData[start]);
159             if (subCRC == readCRC){      //     printf(" 5data %X \n",(UINT8)mysubData[start+1]);
160               rec                  = new(recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento  
161               rec->MCMD_RECORD_OBT = (((UINT32)OBT[0]<<24)&0xFF000000) + (((UINT32)OBT[1]<<16)&0x00FF0000) +  (((UINT32)OBT[2]<<8)&0x0000FF00) + (((UINT32)OBT[3])&0x000000FF);      subCRC = CM_Compute_CRC16(subCRC, (BYTE*) &subTrailer, 2);
162               rec->SeqID           = (((BYTE)subHeader[0]<<8)&0xFF00) + (((BYTE)subHeader[1])&0x00FF);      partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subTrailer, 2);
163               rec->Tbit            = ((BYTE)((subHeader[2]&0x80))>>7);      //Read the CRC of subPacket
164               rec->ID1             = (BYTE)subHeader[3];      for (int m = start; m < start + subPckCRC_len; m++) {
165               rec->McmdLength      = (0x0fff)&(((UINT16)(subHeader[4]<<8)) + ((UINT16)subHeader[5]));        subPckCRC[m - start] = mysubData[m];
166               rec->TimeTag         = (((UINT32)OBT[6]<<24)&0xFF000000) + (((UINT32)OBT[7]<<16)&0x00FF0000) +  (((UINT32)OBT[8]<<8)&0x0000FF00) + (((UINT32)OBT[9])&0x000000FF);      }
167               rec->endID           = (BYTE)subTrailer[1];      start += subPckCRC_len;
168            
169               rec->McmdData        = new TArrayC(dataLength, subData);      partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subPckCRC, 2);
170          //     delete [] subData;      readCRC = (((BYTE)subPckCRC[0]) << 8) + ((BYTE)subPckCRC[1]);
171             } else {  
172                     oss.str("");      //finally check if the RecordCRC is correct
173                     oss << "Wrong CRC on Subpacket internal to MCMD Packet ";      //and finally update the partialCRC
174                     msg=oss.str();      //TO DO - if one CRC is wrong also the total one will be corrupted
175                     PamOffLineSW::mainLogUtil->logAll(msg);            //     printf(" subCRC %X readCRC %X \n",subCRC,readCRC);
176             }      //    if (subCRC == readCRC) { // BUG?! Emiliano 2009 10 01
177                  rec = new (recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento
178             delete [] subData;        rec->MCMD_RECORD_OBT = (((UINT32)OBT[0] << 24) & 0xFF000000) + (((UINT32)OBT[1] << 16) & 0x00FF0000)
179          }            + (((UINT32)OBT[2] << 8) & 0x0000FF00) + (((UINT32)OBT[3]) & 0x000000FF);
180          rec->SeqID = (((BYTE)subHeader[0] << 8) & 0xFF00) + (((BYTE)subHeader[1]) & 0x00FF);
181          if(start!=length-2)        rec->Tbit = ((BYTE)((subHeader[2] & 0x80)) >> 7);
182          {            rec->ID1 = (BYTE)subHeader[3];
183                  oss.str("");        rec->McmdLength = (0x0fff) & (((UINT16)(subHeader[4] << 8)) + ((UINT16)subHeader[5]));
184                  oss<<"MCMD: Error in the MCMD Packet lenght, it is not " <<length;        rec->TimeTag = (((UINT32)subHeader[6] << 24) & 0xFF000000) + (((UINT32)subHeader[7] << 16) & 0x00FF0000) + (((UINT32)subHeader[8]
185                  msg=oss.str();            << 8) & 0x0000FF00) + (((UINT32)subHeader[9]) & 0x000000FF);
186                  PamOffLineSW::mainLogUtil->logWarning(msg);            rec->endID = (BYTE)subTrailer[1];
187          }  
188                  rec->McmdData = new TArrayC(dataLength, subData);
189          //in the end compare the calculated partial CRC with the MCMD packet CRC        //     delete [] subData;
190          readCRC = (((UINT16)(mysubData[length - 2]<<8))&0xFF00) + (((UINT16)mysubData[length - 1])&0x00FF);        if (subCRC == readCRC) {
191          rec->Mcmd_Block_crc_ok = 1;
192  //      if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet ");      }
193          if (partialCRC != readCRC)      else {
194          {        rec->Mcmd_Block_crc_ok = 0;
195                  oss.str("");        oss.str("");
196                  oss<<"Wrong CRC for MCMD Packet: "<<" CRC COMPUTED= "<< partialCRC<<" CRC READ=  "<< readCRC;        oss << "Wrong CRC on Subpacket internal to MCMD Packet ";
197                  msg=oss.str();        msg = oss.str();
198                  PamOffLineSW::mainLogUtil->logWarning(msg);            PamOffLineSW::mainLogUtil->logAll(msg);
199                  throw WrongCRCException_PKTUsed(" Wrong CRC for MCMD Packet. ");      }
200          }  
201        delete[] subData;
202      }
203    
204      if (start != length - 2) {
205        oss.str("");
206        oss << "MCMD: Error in the MCMD Packet lenght (start!=length-2), it is not " << length;
207        msg = oss.str();
208        PamOffLineSW::mainLogUtil->logWarning(msg);
209      }
210    
211      //in the end compare the calculated partial CRC with the MCMD packet CRC
212      readCRC = (((UINT16)(mysubData[length - 2] << 8)) & 0xFF00) + (((UINT16)mysubData[length - 1]) & 0x00FF);
213      //readCRC = (((UINT16)(mysubData[length - 1]<<8))&0xFF00) + (((UINT16)mysubData[length])&0x00FF);
214      //
215      //    printf(" CICCIOEND readCRC %X partialCRC %X \n",(UINT16)readCRC,(UINT16)partialCRC);
216      //    if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet ");
217      if (partialCRC != readCRC) {
218        Mcmd->Mcmd_crc_ok = 0;
219        oss.str("");
220        oss << "Wrong CRC for MCMD Packet: " << " CRC COMPUTED= " << partialCRC << " CRC READ=  " << readCRC;
221        msg = oss.str();
222        PamOffLineSW::mainLogUtil->logWarning(msg);
223        throw WrongCRCException_PKTUsed(" Wrong CRC for MCMD Packet. ");
224      } else {
225        Mcmd->Mcmd_crc_ok = 1;
226      }
227  }  }
228    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.23