Skip to content

Commit

Permalink
Added debugging lines to BLF Handler, fixed a glitch in loading (well…
Browse files Browse the repository at this point in the history
…, kludged it)
  • Loading branch information
collin80 committed May 20, 2018
1 parent 0bc20d7 commit c3d3283
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
35 changes: 31 additions & 4 deletions blfhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ BLFHandler::BLFHandler()

}


/*
Written while peeking at source code here:
https://python-can.readthedocs.io/en/latest/_modules/can/io/blf.html
Expand All @@ -36,28 +35,44 @@ bool BLFHandler::loadBLF(QString filename, QVector<CANFrame>* frames)
inFile->read((char *)&header, sizeof(header));
if (qFromLittleEndian(header.sig) == 0x47474F4C)
{
qDebug() << "Proper BLF file header token";
}
else return false;

while (!inFile->atEnd())
{
qDebug() << "Position within file: " << inFile->pos();
inFile->read((char *)&objHeader, sizeof(objHeader));
if (qFromLittleEndian(objHeader.sig) == 0x4A424F4C)
{
int readSize = objHeader.objSize - sizeof(BLF_OBJ_HEADER);
qDebug() << "Proper object header token. Read Size: " << readSize;
fileData = inFile->read(readSize);
junk = inFile->read(readSize % 4); //file is padded so sizes must always end up on even multiple of 4
//qDebug() << "Fudge bytes in readSize: " << (readSize % 4);
if (objHeader.objType == BLF_CONTAINER)
{
qDebug() << "Object is a container. Uncompressing it";
fileData.prepend(objHeader.uncompSize & 0xFF);
fileData.prepend((objHeader.uncompSize >> 8) & 0xFF);
fileData.prepend((objHeader.uncompSize >> 16) & 0xFF);
fileData.prepend((objHeader.uncompSize >> 24) & 0xFF);
uncompressedData += qUncompress(fileData);
qDebug() << "Uncompressed size: " << uncompressedData.count();
pos = 0;
while (pos + sizeof(BLF_OBJ_HEADER) < uncompressedData.count())
bool foundHeader = false;
//first skip forward to find a header signature - usually not necessary
while ( (int)(pos + sizeof(BLF_OBJ_HEADER)) < uncompressedData.count())
{
memcpy(&obj.header, uncompressedData.mid(pos, sizeof(BLF_OBJ_HEADER)).constData(), sizeof(BLF_OBJ_HEADER));
int32_t *headerSig = (int32_t *)(uncompressedData.constData() + pos);
if (*headerSig == 0x4A424F4C) break;
pos += 4;
}
//then process all the objects
while ( (int)(pos + sizeof(BLF_OBJ_HEADER)) < uncompressedData.count())
{
memcpy(&obj.header, (uncompressedData.constData() + pos), sizeof(BLF_OBJ_HEADER));
qDebug() << "Pos: " << pos << " Type: " << obj.header.objType << "Obj Size: " << obj.header.objSize;
if (qFromLittleEndian(objHeader.sig) == 0x4A424F4C)
{
fileData = uncompressedData.mid(pos + sizeof(BLF_OBJ_HEADER), obj.header.objSize - sizeof(BLF_OBJ_HEADER));
Expand All @@ -70,14 +85,26 @@ bool BLFHandler::loadBLF(QString filename, QVector<CANFrame>* frames)
frame.ID = canObject.id & 0x1FFFFFFFull;
frame.isReceived = true;
frame.len = canObject.dlc;
frame.timestamp = obj.header.uncompSize / 1000000.0; //uncompsize field also used for timestamp oddly enough
frame.timestamp = obj.header.uncompSize / 100000.0; //uncompsize field also used for timestamp oddly enough
for (int i = 0; i < 8; i++) frame.data[i] = canObject.data[i];
frames->append(frame);
}
else
{
//qDebug() << "Not a can frame! ObjType: " << obj.header.objType;
if (obj.header.objType > 0xFFFF)
return false;
}
pos += obj.header.objSize + (obj.header.objSize % 4);
}
else
{
qDebug() << "Unexpected object header signature, aborting";
return false;
}
}
uncompressedData.remove(0, pos);
qDebug() << "After removing used data uncompressedData is now this big: " << uncompressedData.count();
}
}
else return false;
Expand Down
18 changes: 9 additions & 9 deletions blfhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ struct BLF_FILE_HEADER

struct BLF_OBJ_HEADER
{
uint32_t sig;
uint16_t headerSize;
uint16_t headerVersion;
uint32_t objSize;
uint32_t objType;
uint32_t flags;
uint16_t nothing;
uint16_t objVer;
uint64_t uncompSize;
uint32_t sig; //0 offset from start
uint16_t headerSize; //4
uint16_t headerVersion; //6
uint32_t objSize; //8
uint32_t objType; //12
uint32_t flags; //16
uint16_t nothing; //20
uint16_t objVer; //22
uint64_t uncompSize; //24
}; //32 bytes

struct BLF_OBJECT
Expand Down

0 comments on commit c3d3283

Please sign in to comment.