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