1 |
/** @file |
2 |
* $Source: /home/cvsmanager/yoda/techmodel/McmdReader.cpp,v $ |
3 |
* $Id: McmdReader.cpp,v 6.1 2006/05/30 19:10:02 kusanagi Exp $ |
4 |
* $Author: kusanagi $ |
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 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.McmdReader")); |
16 |
static std::stringstream oss; |
17 |
/** |
18 |
* Constructor. |
19 |
*/ |
20 |
McmdReader::McmdReader(void): |
21 |
TechmodelAlgorithm(PacketType::Mcmd, "Mcmd") { |
22 |
logger->debug(_T("Constructor")); |
23 |
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 |
"$Header: /home/cvsmanager/yoda/techmodel/McmdReader.cpp,v 6.1 2006/05/30 19:10:02 kusanagi Exp $\n"; |
32 |
} |
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 |
logger->debug(_T("Initialize")); |
40 |
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 |
void McmdReader::RunEvent(int EventNumber, long int length) throw (WrongCRCException){ |
68 |
|
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 |
InputFile->read(subData, sizeof(unsigned char)*dataLength); |
123 |
for (int jj = 0; jj < dataLength ; jj++){ |
124 |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)(subData+jj), 1); |
125 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)(subData+jj), 1); |
126 |
} |
127 |
//subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subData, dataLength); |
128 |
|
129 |
//} else { |
130 |
// subData = "0"; |
131 |
//} |
132 |
|
133 |
//Read the CRC inside of MCMD |
134 |
//To check sum of all sub packet data, module 256 ****TBD**** |
135 |
InputFile->read(subTrailer, sizeof(subTrailer)); |
136 |
subCRC = CM_Compute_CRC16(subCRC, (BYTE*)&subTrailer, 2); |
137 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subTrailer, 2); |
138 |
|
139 |
//Read the CRC of subPacket |
140 |
InputFile->read(subPckCRC, sizeof(subPckCRC)); |
141 |
partialCRC = CM_Compute_CRC16(partialCRC, (BYTE*)&subPckCRC, 2); |
142 |
readCRC = (((BYTE)subPckCRC[0])<<8) + ((BYTE)subPckCRC[1]); |
143 |
|
144 |
//finally check if the RecordCRC is correct |
145 |
//and finally update the partialCRC |
146 |
//TO DO - if one CRC is wrong also the total one will be corrupted |
147 |
if (subCRC == readCRC){ |
148 |
rec = new(recs[i++]) McmdRecord(); //aggiungo un nuovo McmdRecord all'evento |
149 |
rec->MCMD_RECORD_OBT = (((UINT32)OBT[0]<<24)&0xFF000000) + (((UINT32)OBT[1]<<16)&0x00FF0000) + (((UINT32)OBT[2]<<8)&0x0000FF00) + (((UINT32)OBT[3])&0x000000FF); |
150 |
rec->SeqID = (((BYTE)subHeader[0]<<8)&0xFF00) + (((BYTE)subHeader[1])&0x00FF); |
151 |
rec->Tbit = ((BYTE)((subHeader[2]&0x80))>>7); |
152 |
rec->ID1 = (BYTE)subHeader[3]; |
153 |
rec->McmdLength = (0x0fff)&(((UINT16)(subHeader[4]<<8)) + ((UINT16)subHeader[5])); |
154 |
rec->TimeTag = (((UINT32)OBT[6]<<24)&0xFF000000) + (((UINT32)OBT[7]<<16)&0x00FF0000) + (((UINT32)OBT[8]<<8)&0x0000FF00) + (((UINT32)OBT[9])&0x000000FF); |
155 |
rec->endID = (BYTE)subTrailer[1]; |
156 |
|
157 |
rec->McmdData = new TArrayC(dataLength, subData); |
158 |
delete [] subData; |
159 |
} else { |
160 |
oss.str(""); |
161 |
oss << "Wrong CRC on Subpacket internal to TMTC Packet starting at position" |
162 |
<< start; |
163 |
logger->warn(oss.str().c_str()); |
164 |
} |
165 |
} |
166 |
//in the end compare the calculated partial CRC with the MCMD packet CRC |
167 |
InputFile->read(eventCRC, sizeof(eventCRC)); |
168 |
readCRC = (((UINT16)(eventCRC[0]<<8))&0xFF00) + (((UINT16)eventCRC[1])&0x00FF); |
169 |
|
170 |
if(partialCRC != readCRC) throw WrongCRCException(" Wrong Global CRC for MCMD Packet "); |
171 |
/*if(!(partialCRC == readCRC)) { |
172 |
throw WrongCRCException(); |
173 |
}*/ |
174 |
} |
175 |
|
176 |
|