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