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