1 |
/** @file |
/** @file |
2 |
* $Source: /afs/ba.infn.it/user/pamela/src/CVS/chewbacca/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.1.1.1 2008/09/23 07:20:26 mocchiut Exp $ |
* $Id: McmdReader.cpp,v 1.2 2008-10-01 09:53:42 mocchiut Exp $ |
4 |
* $Author: mocchiut $ |
* $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 |
|
|
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.2 2008-10-01 09:53:42 mocchiut Exp $\n"; |
|
"$Header: /afs/ba.infn.it/user/pamela/src/CVS/chewbacca/PamOffLineSW/techmodel/McmdReader.cpp,v 1.1.1.1 2008/09/23 07:20:26 mocchiut 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--------------------------------- |
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 |
// while(start < (length)) { |
// printf(" %i => %X \n",start,(UINT8)mysubData[start]); |
91 |
// printf(" %i => %X \n",start,(UINT8)mysubData[start]); |
// start++; |
92 |
// start++; |
// }; |
93 |
// }; |
// start=0; |
94 |
// start=0; |
|
95 |
|
while (start < (length - 2 - obt_len - subTrailer_len - subTrailer_len - subPckCRC_len)) |
96 |
while(start < (length-2-obt_len-subTrailer_len-subTrailer_len-subPckCRC_len)) |
// while(start < (length-2)) |
97 |
// while(start < (length-2)) |
{ |
98 |
{ |
// printf(" 1data %X \n",(UINT8)mysubData[start]); |
99 |
// printf(" 1data %X \n",(UINT8)mysubData[start]); |
//Read the OBT preceeding the subpacket then calculate a partial CRC for it |
100 |
//Read the OBT preceeding the subpacket then calculate a partial CRC for it |
//and update the partialCRC |
101 |
//and update the partialCRC |
for (int m = start; m < start + obt_len; m++) { |
102 |
for(int m=start; m<start+obt_len; m++){ |
OBT[m - start] = mysubData[m]; |
103 |
OBT[m-start]=mysubData[m]; |
} |
104 |
} |
start += obt_len; |
105 |
start+=obt_len; |
|
106 |
|
// printf(" 2data %X \n",(UINT8)mysubData[start]); |
107 |
// printf(" 2data %X \n",(UINT8)mysubData[start]); |
// printf(" 2data %X \n",(UINT8)mysubData[start]+1); |
108 |
// printf(" 2data %X \n",(UINT8)mysubData[start]+1); |
subCRC = CM_Compute_CRC16(0, (BYTE*) &OBT, obt_len); |
109 |
subCRC = CM_Compute_CRC16(0, (BYTE*)&OBT, obt_len); |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &OBT, obt_len); |
110 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&OBT, obt_len); |
//Read the header for the subPacket and read mcmdLength |
111 |
//Read the header for the subPacket and read mcmdLength |
//12 is the total lenght of subHeader + subTrailer |
112 |
//12 is the total lenght of subHeader + subTrailer |
for (int m = start; m < start + subHeader_len; m++) { |
113 |
for(int m=start; m<start+subHeader_len; m++){ |
subHeader[m - start] = mysubData[m]; |
114 |
subHeader[m-start]=mysubData[m]; |
} |
115 |
} |
start += subHeader_len; |
116 |
start+=subHeader_len; |
|
117 |
|
// printf(" 3data %X \n",(UINT8)mysubData[start]); |
118 |
// printf(" 3data %X \n",(UINT8)mysubData[start]); |
// printf(" 3data %X \n",(UINT8)mysubData[start+1]); |
119 |
// printf(" 3data %X \n",(UINT8)mysubData[start+1]); |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*) &subHeader, subHeader_len); |
120 |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subHeader, subHeader_len); |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subHeader, subHeader_len); |
121 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subHeader, subHeader_len); |
dataLength = (((0x0fff) & ((UINT16)subHeader[4])) << 8 | ((UINT16)subHeader[5]) * 2) - subHeader_len |
122 |
dataLength = (((0x0fff)&((UINT16)subHeader[4]))<<8|((UINT16)subHeader[5])*2) - subHeader_len - subTrailer_len; |
- subTrailer_len; |
123 |
// printf(" datalenght = %i subh 4 %X subd 5 %X \n",dataLength,(UINT8)subHeader[4],(UINT8)subHeader[5]); |
// printf(" datalenght = %i subh 4 %X subd 5 %X \n",dataLength,(UINT8)subHeader[4],(UINT8)subHeader[5]); |
124 |
//the line below is exactly how it was in the original version |
//the line below is exactly how it was in the original version |
125 |
if (dataLength < 0) break; //it should throw an exception ***TBD*** |
if (dataLength < 0) |
126 |
//I add also this check |
break; //it should throw an exception ***TBD*** |
127 |
if (dataLength + start > (length-2-subTrailer_len-subPckCRC_len)) |
//I add also this check |
128 |
{ |
if (dataLength + start > (length - 2 - subTrailer_len - subPckCRC_len)) { |
129 |
oss.str(""); |
oss.str(""); |
130 |
oss<<"MCMD: Error in the MCMD Packet lenght, it is not " <<length; |
oss << "MCMD: Error in the MCMD Packet lenght, it is not " << length; |
131 |
msg=oss.str(); |
msg = oss.str(); |
132 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
PamOffLineSW::mainLogUtil->logWarning(msg); |
133 |
break; //it should throw an exception ***TBD*** |
break; //it should throw an exception ***TBD*** |
134 |
} |
} |
135 |
|
|
136 |
//read subpacket data according to data length then calculate partial CRC for data |
//read subpacket data according to data length then calculate partial CRC for data |
137 |
//and update the partialCRC |
//and update the partialCRC |
138 |
|
|
139 |
subData = new char[dataLength]; |
subData = new char[dataLength]; |
140 |
for(int m=start; m<start+dataLength; m++){ |
for (int m = start; m < start + dataLength; m++) { |
141 |
subData[m-start]=mysubData[m]; |
subData[m - start] = mysubData[m]; |
142 |
} |
} |
143 |
start+=dataLength; |
start += dataLength; |
144 |
|
|
145 |
// printf(" 4data %X \n",(UINT8)mysubData[start]); |
// printf(" 4data %X \n",(UINT8)mysubData[start]); |
146 |
// printf(" 4data %X \n",(UINT8)mysubData[start+1]); |
// printf(" 4data %X \n",(UINT8)mysubData[start+1]); |
147 |
for (int jj = 0; jj < dataLength ; jj++){ |
for (int jj = 0; jj < dataLength; jj++) { |
148 |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(subData+jj), 1); |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*) (subData + jj), 1); |
149 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)(subData+jj), 1); |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) (subData + jj), 1); |
150 |
} |
} |
151 |
|
|
152 |
//Read the CRC inside of MCMD |
//Read the CRC inside of MCMD |
153 |
//To check sum of all sub packet data, module 256 ****TBD**** |
//To check sum of all sub packet data, module 256 ****TBD**** |
154 |
for(int m=start; m<start+subTrailer_len; m++){ |
for (int m = start; m < start + subTrailer_len; m++) { |
155 |
subTrailer[m-start]=mysubData[m]; |
subTrailer[m - start] = mysubData[m]; |
156 |
} |
} |
157 |
start+=subTrailer_len; |
start += subTrailer_len; |
158 |
// printf(" 5data %X \n",(UINT8)mysubData[start]); |
// printf(" 5data %X \n",(UINT8)mysubData[start]); |
159 |
// printf(" 5data %X \n",(UINT8)mysubData[start+1]); |
// printf(" 5data %X \n",(UINT8)mysubData[start+1]); |
160 |
|
|
161 |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subTrailer, 2); |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*) &subTrailer, 2); |
162 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subTrailer, 2); |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subTrailer, 2); |
163 |
//Read the CRC of subPacket |
//Read the CRC of subPacket |
164 |
for(int m=start; m<start+subPckCRC_len; m++){ |
for (int m = start; m < start + subPckCRC_len; m++) { |
165 |
subPckCRC[m-start]=mysubData[m]; |
subPckCRC[m - start] = mysubData[m]; |
166 |
} |
} |
167 |
start+=subPckCRC_len; |
start += subPckCRC_len; |
168 |
|
|
169 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subPckCRC, 2); |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*) &subPckCRC, 2); |
170 |
readCRC = (((BYTE)subPckCRC[0])<<8) + ((BYTE)subPckCRC[1]); |
readCRC = (((BYTE)subPckCRC[0]) << 8) + ((BYTE)subPckCRC[1]); |
171 |
|
|
172 |
//finally check if the RecordCRC is correct |
//finally check if the RecordCRC is correct |
173 |
//and finally update the partialCRC |
//and finally update the partialCRC |
174 |
//TO DO - if one CRC is wrong also the total one will be corrupted |
//TO DO - if one CRC is wrong also the total one will be corrupted |
175 |
// printf(" subCRC %X readCRC %X \n",subCRC,readCRC); |
// printf(" subCRC %X readCRC %X \n",subCRC,readCRC); |
176 |
if (subCRC == readCRC){ |
if (subCRC == readCRC) { |
177 |
rec = new(recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento |
rec = new (recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento |
178 |
rec->MCMD_RECORD_OBT = (((UINT32)OBT[0]<<24)&0xFF000000) + (((UINT32)OBT[1]<<16)&0x00FF0000) + (((UINT32)OBT[2]<<8)&0x0000FF00) + (((UINT32)OBT[3])&0x000000FF); |
rec->MCMD_RECORD_OBT = (((UINT32)OBT[0] << 24) & 0xFF000000) + (((UINT32)OBT[1] << 16) & 0x00FF0000) |
179 |
rec->SeqID = (((BYTE)subHeader[0]<<8)&0xFF00) + (((BYTE)subHeader[1])&0x00FF); |
+ (((UINT32)OBT[2] << 8) & 0x0000FF00) + (((UINT32)OBT[3]) & 0x000000FF); |
180 |
rec->Tbit = ((BYTE)((subHeader[2]&0x80))>>7); |
rec->SeqID = (((BYTE)subHeader[0] << 8) & 0xFF00) + (((BYTE)subHeader[1]) & 0x00FF); |
181 |
rec->ID1 = (BYTE)subHeader[3]; |
rec->Tbit = ((BYTE)((subHeader[2] & 0x80)) >> 7); |
182 |
rec->McmdLength = (0x0fff)&(((UINT16)(subHeader[4]<<8)) + ((UINT16)subHeader[5])); |
rec->ID1 = (BYTE)subHeader[3]; |
183 |
rec->TimeTag = (((UINT32)OBT[6]<<24)&0xFF000000) + (((UINT32)OBT[7]<<16)&0x00FF0000) + (((UINT32)OBT[8]<<8)&0x0000FF00) + (((UINT32)OBT[9])&0x000000FF); |
rec->McmdLength = (0x0fff) & (((UINT16)(subHeader[4] << 8)) + ((UINT16)subHeader[5])); |
184 |
rec->endID = (BYTE)subTrailer[1]; |
rec->TimeTag = (((UINT32)subHeader[6] << 24) & 0xFF000000) + (((UINT32)subHeader[7] << 16) & 0x00FF0000) + (((UINT32)subHeader[8] |
185 |
|
<< 8) & 0x0000FF00) + (((UINT32)subHeader[9]) & 0x000000FF); |
186 |
rec->McmdData = new TArrayC(dataLength, subData); |
rec->endID = (BYTE)subTrailer[1]; |
187 |
// delete [] subData; |
|
188 |
} else { |
rec->McmdData = new TArrayC(dataLength, subData); |
189 |
oss.str(""); |
// delete [] subData; |
190 |
oss << "Wrong CRC on Subpacket internal to MCMD Packet "; |
} |
191 |
msg=oss.str(); |
else { |
192 |
PamOffLineSW::mainLogUtil->logAll(msg); |
oss.str(""); |
193 |
} |
oss << "Wrong CRC on Subpacket internal to MCMD Packet "; |
194 |
|
msg = oss.str(); |
195 |
delete [] subData; |
PamOffLineSW::mainLogUtil->logAll(msg); |
196 |
} |
} |
197 |
|
|
198 |
if(start!=length-2) |
delete[] subData; |
199 |
{ |
} |
200 |
oss.str(""); |
|
201 |
oss<<"MCMD: Error in the MCMD Packet lenght (start!=length-2), it is not " <<length; |
if (start != length - 2) { |
202 |
msg=oss.str(); |
oss.str(""); |
203 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
oss << "MCMD: Error in the MCMD Packet lenght (start!=length-2), it is not " << length; |
204 |
} |
msg = oss.str(); |
205 |
|
PamOffLineSW::mainLogUtil->logWarning(msg); |
206 |
//in the end compare the calculated partial CRC with the MCMD packet CRC |
} |
207 |
readCRC = (((UINT16)(mysubData[length - 2]<<8))&0xFF00) + (((UINT16)mysubData[length - 1])&0x00FF); |
|
208 |
//readCRC = (((UINT16)(mysubData[length - 1]<<8))&0xFF00) + (((UINT16)mysubData[length])&0x00FF); |
//in the end compare the calculated partial CRC with the MCMD packet CRC |
209 |
// |
readCRC = (((UINT16)(mysubData[length - 2] << 8)) & 0xFF00) + (((UINT16)mysubData[length - 1]) & 0x00FF); |
210 |
// printf(" CICCIOEND readCRC %X partialCRC %X \n",(UINT16)readCRC,(UINT16)partialCRC); |
//readCRC = (((UINT16)(mysubData[length - 1]<<8))&0xFF00) + (((UINT16)mysubData[length])&0x00FF); |
211 |
// if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet "); |
// |
212 |
if (partialCRC != readCRC) |
// printf(" CICCIOEND readCRC %X partialCRC %X \n",(UINT16)readCRC,(UINT16)partialCRC); |
213 |
{ |
// if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet "); |
214 |
oss.str(""); |
if (partialCRC != readCRC) { |
215 |
oss<<"Wrong CRC for MCMD Packet: "<<" CRC COMPUTED= "<< partialCRC<<" CRC READ= "<< readCRC; |
oss.str(""); |
216 |
msg=oss.str(); |
oss << "Wrong CRC for MCMD Packet: " << " CRC COMPUTED= " << partialCRC << " CRC READ= " << readCRC; |
217 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
msg = oss.str(); |
218 |
throw WrongCRCException_PKTUsed(" Wrong CRC for MCMD Packet. "); |
PamOffLineSW::mainLogUtil->logWarning(msg); |
219 |
}; |
throw WrongCRCException_PKTUsed(" Wrong CRC for MCMD Packet. "); |
220 |
|
}; |
221 |
} |
} |
222 |
|
|