1 |
kusanagi |
1.1 |
/** @file |
2 |
mocchiut |
6.3 |
* $Source: /afs/ba.infn.it/user/pamela/src/CVS/yoda/techmodel/McmdReader.cpp,v $ |
3 |
|
|
* $Id: McmdReader.cpp,v 6.2 2006/05/30 19:10:03 kusanagi Exp $ |
4 |
kusanagi |
6.0 |
* $Author: kusanagi $ |
5 |
kusanagi |
1.1 |
* |
6 |
|
|
* Implementation of the McmdReader class. |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
extern "C" { |
10 |
kusanagi |
6.1 |
#include "CRC.h" |
11 |
kusanagi |
1.1 |
} |
12 |
|
|
#include "ReaderAlgorithms.h" |
13 |
|
|
using namespace pamela::techmodel; |
14 |
|
|
|
15 |
kusanagi |
1.5 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.McmdReader")); |
16 |
|
|
static std::stringstream oss; |
17 |
kusanagi |
1.1 |
/** |
18 |
|
|
* Constructor. |
19 |
|
|
*/ |
20 |
|
|
McmdReader::McmdReader(void): |
21 |
|
|
TechmodelAlgorithm(PacketType::Mcmd, "Mcmd") { |
22 |
kusanagi |
1.5 |
logger->debug(_T("Constructor")); |
23 |
kusanagi |
1.1 |
Mcmd = new McmdEvent(); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* Get a string with the version info of the algorithm. |
28 |
|
|
*/ |
29 |
|
|
std::string McmdReader::GetVersionInfo(void) const { |
30 |
|
|
return |
31 |
mocchiut |
6.3 |
"$Header: /afs/ba.infn.it/user/pamela/src/CVS/yoda/techmodel/McmdReader.cpp,v 6.2 2006/05/30 19:10:03 kusanagi Exp $\n"; |
32 |
kusanagi |
1.1 |
} |
33 |
|
|
|
34 |
|
|
/** |
35 |
|
|
* Initialize the algorithm with a special run. This will initialize the |
36 |
|
|
* event reader routines for all packet types. |
37 |
|
|
*/ |
38 |
|
|
void McmdReader::Init(PamelaRun *run) { |
39 |
kusanagi |
1.5 |
logger->debug(_T("Initialize")); |
40 |
kusanagi |
1.1 |
SetInputStream(run); |
41 |
|
|
run->WriteSubPacket(this, &Mcmd, Mcmd->Class()); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* Unpack the Mcmd event from an input file. |
46 |
|
|
* Each subpacket is prafaceded by 4 OBT bytes. |
47 |
|
|
* The elementar structure is a kind of |
48 |
|
|
* --------CPU - OBT--------------------------------- |
49 |
|
|
* OBT - 4 Bytes |
50 |
|
|
* --------Start Sub-Packet--------------------------------- |
51 |
|
|
* SeqID - 2 Bytes |
52 |
|
|
* T - 1 bit | spare - 7 bits | Mcmd-ID - 1 byte |
53 |
|
|
* Spare - 4 bits | Mcmd Lenght 12 bits |
54 |
|
|
* Time Tag - 4 Bytes |
55 |
|
|
* DATA - McmdLength * 2 Bytes |
56 |
|
|
* don't care - 1 Byte |
57 |
|
|
* END-ID - 1 Byte is the sum of subPacket Bytes module 256 |
58 |
|
|
* --------End Sub-Packet--------------------------------- |
59 |
|
|
* --------CPU CRC-16 on OBT + subPacket--------------------------------- |
60 |
|
|
* subCRC - 2 Bytes |
61 |
|
|
* this structure is repeated one or more times. |
62 |
|
|
* The last 2 Bytes are CRC16 computed by CPU on all previous (OBT + subpacket data + subCRC) repetitions |
63 |
|
|
|
64 |
|
|
* @Event Number |
65 |
|
|
* @length is the size in bytes of the event (or packet) |
66 |
|
|
*/ |
67 |
kusanagi |
2.1 |
void McmdReader::RunEvent(int EventNumber, long int length) throw (WrongCRCException){ |
68 |
kusanagi |
1.1 |
|
69 |
|
|
int i = 0; |
70 |
|
|
char OBT[4]; |
71 |
|
|
char subHeader[10]; |
72 |
|
|
char *subData; |
73 |
|
|
char subTrailer[2]; |
74 |
|
|
char subPckCRC[2]; |
75 |
|
|
char eventCRC[2]; |
76 |
|
|
long int start; |
77 |
|
|
unsigned short headerCRC; |
78 |
|
|
UINT16 subCRC; //CRC of the subpacket (updated as subPckt is read) |
79 |
|
|
UINT16 readCRC; //CRC read from the subpacket |
80 |
|
|
UINT16 partialCRC; //partial CRC updated as mcmd packet is read (to compare with CRC read in the total Mcmd header) |
81 |
|
|
|
82 |
|
|
long int lastPosition, dataLength; |
83 |
|
|
McmdRecord *rec; |
84 |
|
|
|
85 |
|
|
//the 2 bytes subtracted belong to the final event CRC bytes |
86 |
|
|
lastPosition = (long int)InputFile->tellg() + length - 2; |
87 |
|
|
Mcmd->Records->Clear(); |
88 |
|
|
TClonesArray &recs = *(Mcmd->Records); |
89 |
|
|
partialCRC = 0; |
90 |
|
|
while(InputFile->tellg() < lastPosition) { |
91 |
|
|
|
92 |
|
|
//Just to test crc |
93 |
|
|
/* char tempbuff[length]; |
94 |
|
|
InputFile->read(tempbuff, sizeof(tempbuff)); |
95 |
|
|
subCRC = 0; |
96 |
|
|
for (int jj = 0; jj < length ; jj++){ |
97 |
|
|
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(tempbuff+jj), 1); |
98 |
|
|
}*/ |
99 |
|
|
|
100 |
|
|
//Read the OBT preceeding the subpacket then calculate a partial CRC for it |
101 |
|
|
//and update the partialCRC |
102 |
|
|
start = InputFile->tellg(); |
103 |
|
|
InputFile->read(OBT, sizeof(OBT)); |
104 |
|
|
subCRC = CM_Compute_CRC16(0, (BYTE*)&OBT, 4); |
105 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&OBT, 4); |
106 |
|
|
|
107 |
|
|
//Read the header for the subPacket and read mcmdLength |
108 |
|
|
//12 is the total lenght of subHeader + subTrailer |
109 |
|
|
InputFile->read(subHeader, sizeof(subHeader)); |
110 |
|
|
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subHeader, 10); |
111 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subHeader, 10); |
112 |
|
|
dataLength = (((0x0fff)&((UINT16)subHeader[4]))<<8|((UINT16)subHeader[5])*2) - 12; |
113 |
|
|
|
114 |
|
|
if (dataLength < 0) break; //it should throw an exception ***TBD*** |
115 |
|
|
//read subpacket data according to data length then calculate partial CRC for data |
116 |
|
|
//and update the partialCRC |
117 |
|
|
|
118 |
|
|
// If dataLength > 0 reads the data and compute again a partial CRC |
119 |
|
|
// else set subData to 0 |
120 |
|
|
//if(dataLength > 0){ |
121 |
|
|
subData = new char[dataLength]; |
122 |
mocchiut |
6.3 |
memset(subData, 0, dataLength*sizeof(char)); |
123 |
kusanagi |
1.1 |
InputFile->read(subData, sizeof(unsigned char)*dataLength); |
124 |
|
|
for (int jj = 0; jj < dataLength ; jj++){ |
125 |
|
|
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(subData+jj), 1); |
126 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)(subData+jj), 1); |
127 |
|
|
} |
128 |
|
|
//subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subData, dataLength); |
129 |
|
|
|
130 |
|
|
//} else { |
131 |
|
|
// subData = "0"; |
132 |
|
|
//} |
133 |
|
|
|
134 |
|
|
//Read the CRC inside of MCMD |
135 |
|
|
//To check sum of all sub packet data, module 256 ****TBD**** |
136 |
|
|
InputFile->read(subTrailer, sizeof(subTrailer)); |
137 |
|
|
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subTrailer, 2); |
138 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subTrailer, 2); |
139 |
|
|
|
140 |
|
|
//Read the CRC of subPacket |
141 |
|
|
InputFile->read(subPckCRC, sizeof(subPckCRC)); |
142 |
|
|
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subPckCRC, 2); |
143 |
|
|
readCRC = (((BYTE)subPckCRC[0])<<8) + ((BYTE)subPckCRC[1]); |
144 |
|
|
|
145 |
|
|
//finally check if the RecordCRC is correct |
146 |
|
|
//and finally update the partialCRC |
147 |
|
|
//TO DO - if one CRC is wrong also the total one will be corrupted |
148 |
|
|
if (subCRC == readCRC){ |
149 |
|
|
rec = new(recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento |
150 |
|
|
rec->MCMD_RECORD_OBT = (((UINT32)OBT[0]<<24)&0xFF000000) + (((UINT32)OBT[1]<<16)&0x00FF0000) + (((UINT32)OBT[2]<<8)&0x0000FF00) + (((UINT32)OBT[3])&0x000000FF); |
151 |
|
|
rec->SeqID = (((BYTE)subHeader[0]<<8)&0xFF00) + (((BYTE)subHeader[1])&0x00FF); |
152 |
|
|
rec->Tbit = ((BYTE)((subHeader[2]&0x80))>>7); |
153 |
|
|
rec->ID1 = (BYTE)subHeader[3]; |
154 |
|
|
rec->McmdLength = (0x0fff)&(((UINT16)(subHeader[4]<<8)) + ((UINT16)subHeader[5])); |
155 |
|
|
rec->TimeTag = (((UINT32)OBT[6]<<24)&0xFF000000) + (((UINT32)OBT[7]<<16)&0x00FF0000) + (((UINT32)OBT[8]<<8)&0x0000FF00) + (((UINT32)OBT[9])&0x000000FF); |
156 |
|
|
rec->endID = (BYTE)subTrailer[1]; |
157 |
kusanagi |
1.4 |
|
158 |
|
|
rec->McmdData = new TArrayC(dataLength, subData); |
159 |
kusanagi |
1.3 |
delete [] subData; |
160 |
kusanagi |
1.1 |
} else { |
161 |
kusanagi |
2.1 |
oss.str(""); |
162 |
kusanagi |
1.5 |
oss << "Wrong CRC on Subpacket internal to TMTC Packet starting at position" |
163 |
|
|
<< start; |
164 |
|
|
logger->warn(oss.str().c_str()); |
165 |
kusanagi |
1.1 |
} |
166 |
|
|
} |
167 |
|
|
//in the end compare the calculated partial CRC with the MCMD packet CRC |
168 |
|
|
InputFile->read(eventCRC, sizeof(eventCRC)); |
169 |
|
|
readCRC = (((UINT16)(eventCRC[0]<<8))&0xFF00) + (((UINT16)eventCRC[1])&0x00FF); |
170 |
|
|
|
171 |
kusanagi |
2.1 |
if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet "); |
172 |
|
|
/*if(!(partialCRC == readCRC)) { |
173 |
|
|
throw WrongCRCException(); |
174 |
|
|
}*/ |
175 |
kusanagi |
1.1 |
} |
176 |
|
|
|
177 |
|
|
|