00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "parser/DifReader.hh"
00022 #include "parser/CrcCheck.hh"
00023
00024
00025 #include "tools/Log.hh"
00026 #include "tools/MicroException.hh"
00027
00028 #include <stdio.h>
00029 #include <errno.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <string>
00033 #include <sstream>
00034 #include <iostream>
00035 #include <iomanip>
00036
00037 using namespace std;
00038
00039
00040 DifReader::DifReader(Run& aRun, FILE *aFile, ui32 firstEventId) : AcquisitionParser(aRun, aFile, firstEventId)
00041 {
00042 fileError = 0;
00043 displayData = (FILELog::ReportingLevel() >= logDEBUG) ? 1 : 0;
00044 level = 0;
00045 line = 0;
00046 bitBuffer = (unsigned char *)NULL;
00047 };
00048
00049
00050 unsigned int DifReader::getBinData(const int nBytes) {
00051 unsigned int result = 0;
00052 fileError = 0;
00053 for (int byte = 0; byte < nBytes; ++byte) {
00054 unsigned char data = 0;
00055 if (fread((void *)&data, sizeof(unsigned char), 1, inputFile) != 1) {
00056
00057 fileError = EOF;
00058 return(0);
00059 }
00060 result = (result << 8) + data;
00061 }
00062
00063 putData(result, nBytes);
00064 return(result);
00065 };
00066
00067
00068
00069 unsigned char *DifReader::getBinData(const int nBytes, unsigned char *buf) {
00070 unsigned int result = 0;
00071 fileError = 0;
00072 if (fread(buf, sizeof(unsigned char), nBytes, inputFile) != nBytes) {
00073 *buf = 0;
00074 FILE_LOG(logERROR) << "getBinData : error " << errno << endl;
00075 fileError = EOF;
00076 return(0);
00077 }
00078 buf[nBytes] = 0;
00079 putBinData(buf, nBytes);
00080 return(buf);
00081 };
00082
00083
00084 unsigned int DifReader::getData(const int nBytes) {
00085 unsigned int result = 0;
00086 fileError = 0;
00087 for (int byte = 0; byte < nBytes; ++byte) {
00088 int data = 0;
00089 if (fscanf(inputFile, "%2x", &data) != 1) {
00090
00091 fileError = EOF;
00092 return 0;
00093 }
00094 if ( computeCrc ) {
00095 crc->compute(data) ;
00096 }
00097 result = (result << 8) + data;
00098 }
00099
00100 putData(result, nBytes);
00101 return(result);
00102 }
00103
00104
00105 unsigned char *DifReader::getData(const int nBytes, unsigned char *buf) {
00106 unsigned char *bufStr = buf;
00107 fileError = 0;
00108 for (int byte = 0; byte < nBytes; ++byte) {
00109 int data = 0;
00110 if (fscanf(inputFile, "%2x", &data) != 1) {
00111 FILE_LOG(logERROR) << "getData : error " << errno << endl;
00112 *bufStr = 0;
00113 fileError = EOF;
00114 return(0);
00115 }
00116 *bufStr++ = data;
00117
00118 if ( computeCrc ) {
00119 crc->compute(data) ;
00120 }
00121
00122 }
00123 *bufStr = 0;
00124 putBinData(buf, nBytes);
00125 return(buf);
00126 }
00127
00128
00129 unsigned char *DifReader::getBitData() {
00130 unsigned char *res;
00131 res = (bitBinaryFile) ? getBinData(bitBufCount, bitBuffer)
00132 : getData(bitBufCount, bitBuffer);
00133 display();
00134 return(res);
00135 }
00136
00137
00138
00139 unsigned char *DifReader::initGetBits(const int bufLen, const bool binaryFile) {
00140 bitBuffer = (unsigned char*)malloc(bufLen + 1);
00141 if (!bitBuffer) {
00142 return 0;
00143 }
00144 bitIdx = 8;
00145 bitBufCount = bufLen;
00146 bitByteIdx = 0;
00147 bitBinaryFile = binaryFile;
00148 if (!getBitData()) {
00149 FILE_LOG(logERROR) << "InitGetBits : error " << errno << endl;
00150 return 0;
00151 }
00152 return bitBuffer;
00153 }
00154
00155
00156
00157 void DifReader::finishGetBits() {
00158 bitBufCount = 0;
00159 if (bitBuffer)
00160 free(bitBuffer);
00161 bitBuffer = NULL;
00162 }
00163
00164
00165 static const int masks[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
00166
00167 unsigned int DifReader::getBit() {
00168 unsigned int bit = (bitBuffer[bitByteIdx] & masks[bitIdx - 1]) >> (bitIdx - 1);
00169 bitIdx--;
00170 if (!bitIdx) {
00171 bitIdx = 8;
00172 bitByteIdx++;
00173 if (bitByteIdx == bitBufCount) {
00174
00175 bitByteIdx = 0;
00176 }
00177 }
00178 return bit;
00179 }
00180
00181
00182
00183 unsigned int DifReader::getBits(const int N) {
00184 unsigned int val = 0;
00185 int i = N;
00186 unsigned int j;
00187
00188 while (i) {
00189 j = (bitBuffer[bitByteIdx] & masks[bitIdx - 1]) >> (bitIdx - 1);
00190 bitIdx--;
00191 if (!bitIdx) {
00192 bitIdx = 8;
00193 bitByteIdx++;
00194 if (bitByteIdx == bitBufCount) {
00195
00196
00197 bitByteIdx = 0;
00198 }
00199 }
00200 val = (val << 1) | j;
00201 i--;
00202 }
00203 return val;
00204 }
00205
00206
00207
00208
00209
00210 int DifReader::seekSyncBits(unsigned int sync, const int N) {
00211 unsigned int val, val1;
00212 unsigned int maxi = 0xFFFFFFFF;
00213
00214 while (bitIdx != 8) {
00215 val = getBit();
00216
00217
00218 }
00219
00220 val = getBits(N);
00221
00222
00223
00224 while ((val & maxi) != sync) {
00225 val <<= 8;
00226 val1 = getBits(8);
00227
00228
00229 val |= val1;
00230 }
00231 return 1;
00232 }
00233
00234
00235 int DifReader::getBitBufferData(void *buffer, const int len) {
00236 if (len < bitByteIdx) {
00237 memcpy(buffer, bitBuffer, len);
00238 bitByteIdx -= len;
00239 memcpy(bitBuffer, bitBuffer + len, bitByteIdx);
00240 return len;
00241 }
00242 else {
00243 int lg = bitByteIdx;
00244 memcpy(buffer, bitBuffer, bitByteIdx);
00245 bitByteIdx = 0;
00246 return lg;
00247 }
00248 }
00249
00250
00251 void DifReader::putData(const unsigned int result, const int nBytes) {
00252 if (!displayData)
00253 return;
00254 dataBuf << hex << setw(nBytes * 2) << setfill('0') << result;
00255 if (nBytes > 1)
00256 dataBuf << " ";
00257
00258 }
00259
00260
00261 void DifReader::putData(const unsigned char *buf, const int nBytes) {
00262 if (!displayData)
00263 return;
00264 if (nBytes > 4)
00265 dataBuf << "("<< dec << nBytes << "):";
00266 dataBuf << buf;
00267 if (nBytes > 1)
00268 dataBuf << " ";
00269
00270 }
00271
00272
00273 void DifReader::putBinData(const unsigned char *buf, const int nBytes) {
00274 if (!displayData)
00275 return;
00276 if (nBytes > 4)
00277 dataBuf << "(" << dec << nBytes << "):";
00278 for (int byte = 0; byte < nBytes; byte++) {
00279 dataBuf << hex << setw(2) << setfill('0') << (int)(*buf);
00280 buf++;
00281 }
00282 if (nBytes > 1)
00283 dataBuf << " ";
00284
00285 }
00286
00287
00288 void DifReader::display(const int inc) {
00289 line++;
00290 if (inc < 0) {
00291 level += inc;
00292 if (level < 0) level = 0;
00293 }
00294 if (dataBuf.str().length() != 0) {
00295 FILE_LOG(logDEBUG) << "----display-----: " << setfill(' ') << setw(level * 2) << "" << dataBuf.str() << endl;
00296 }
00297 if (inc > 0)
00298 level += inc;
00299 dataBuf.str("");
00300 }
00301
00302
00303 const i64 DifReader::getbcIdAbsOrg(const i32 difId, const i64 bcIdAbs) {
00304 BcIdDifAbsOrgMap_t::iterator it = bcIdDifAbsOrgMap.find(difId);
00305 if (it == bcIdDifAbsOrgMap.end()) {
00306 FILE_LOG(logDEBUG) << " set bc origin (dif 0x" << hex << difId << dec << ")=" << bcIdAbs << endl;
00307 bcIdDifAbsOrgMap[difId] = bcIdAbs;
00308 return(bcIdAbs);
00309 }
00310 return(it->second);
00311 }
00312