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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Tue Sep 23 07:20:26 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.23