1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
/**
* @licence app begin@
* Copyright (C) 2011-2012 BMW AG
*
* This file is part of COVESA Project Dlt Viewer.
*
* Contributions are licensed to the COVESA Alliance under one or more
* Contribution License Agreements.
*
* \copyright
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> 2011-2012
*
* \file qdlt.cpp
* For further information see http://www.covesa.global/.
* @licence end@
*/
#include <QFile>
#include <QtDebug>
#include "qdlt.h"
extern "C"
{
#include "dlt_common.h"
}
QDltFile::QDltFile()
{
filterFlag = false;
sortByTimeFlag = false;
sortByTimestampFlag = false;
}
QDltFile::~QDltFile()
{
clear();
}
void QDltFile::clear()
{
for(int num=0;num<files.size();num++)
{
if(files[num]->infile.isOpen()) {
files[num]->infile.close();
}
delete(files[num]);
}
files.clear();
}
int QDltFile::getNumberOfFiles() const
{
return files.size();
}
void QDltFile::setDltIndex(QVector<qint64> &_indexAll, int num)
{
if(num<0 || num>=files.size())
{
return;
}
files[num]->indexAll = _indexAll;
}
int QDltFile::size() const
{
int size=0;
for(int num=0;num<files.size();num++)
{
if (nullptr!=files[num])
size += files[num]->indexAll.size();
}
return size;
}
qint64 QDltFile::fileSize() const
{
qint64 size=0;
for(int num=0;num<files.size();num++)
{
if (nullptr!=files[num])
size += files[num]->infile.size();
}
return size;
}
int QDltFile::sizeFilter() const
{
if(filterFlag)
return indexFilter.size();
else
return size();
}
bool QDltFile::open(QString _filename, bool append)
{
qDebug() << "Open file" << _filename << "started" << __FILE__ << __LINE__;
/* check if file is already opened */
if(!append)
clear();
/* create new file item */
QDltFileItem *item = new QDltFileItem();
files.append(item);
/* set new filename */
item->infile.setFileName(_filename);
/* open the log file read only */
if(item->infile.open(QIODevice::ReadOnly)==false)
{
/* open file failed */
qWarning() << "open of file" << _filename << "failed";
return false;
}
return true;
}
void QDltFile::clearIndex()
{
for(int num=0;num<files.size();num++)
{
files[num]->indexAll.clear();
}
}
bool QDltFile::createIndex()
{
bool ret = false;
clearIndex();
/* Example to use QtConcurrent to call updateIndex in separate Thread*/
//QFuture<bool> f = QtConcurrent::run(this, &QDltFile::updateIndex);
//f.waitForFinished();
ret = updateIndex();
//qDebug() << "Create index finished - " << size() << "messages found";
return ret;
}
bool QDltFile::updateIndex()
{
QByteArray buf;
qint64 pos = 0;
quint16 last_message_length = 0;
mutexQDlt.lock();
for(int numFile=0;numFile<files.size();numFile++)
{
/* check if file is already opened */
if(false == files[numFile]->infile.isOpen())
{
qDebug() << "updateMsg: Infile is not open" << files[numFile]->infile.fileName() << __FILE__ << "line" << __LINE__;
mutexQDlt.unlock();
return false;
}
/* start at last found position */
if(files[numFile]->indexAll.size())
{
/* move behind last found position */
const QVector<qint64>* const_indexAll = &(files[numFile]->indexAll);
pos = (*const_indexAll)[files[numFile]->indexAll.size()-1];
// read the first 18 bytes of last message to look for actual size
files[numFile]->infile.seek(pos + 18);
buf = files[numFile]->infile.read(2);
// Read low and high bytes of message length
last_message_length = (unsigned char)buf.at(0);
last_message_length = (last_message_length<<8 | ((unsigned char)buf.at(1))) +16;
// move just behind the next expected message
pos += (last_message_length - 1);
files[numFile]->infile.seek(pos);
}
else {
/* the file was empty the last call */
files[numFile]->infile.seek(0);
}
/* Align kbytes, 1MB read at a time */
static const int READ_BUF_SZ = 1024 * 1024;
/* walk through the whole file and find all DLT0x01 markers */
/* store the found positions in the indexAll */
char lastFound = 0;
qint64 current_message_pos = 0;
qint64 next_message_pos = 0;
int counter_header = 0;
quint16 message_length = 0;
qint64 file_size = files[numFile]->infile.size();
qint64 errors_in_file = 0;
while(true)
{
/* read buffer from file */
buf = files[numFile]->infile.read(READ_BUF_SZ);
if(buf.isEmpty())
break; // EOF
/* Use primitive buffer for faster access */
int cbuf_sz = buf.size();
const char *cbuf = buf.constData();
/* find marker in buffer */
for(int num=0;num<cbuf_sz;num++) {
// search length of DLT message
if(counter_header>0)
{
counter_header++;
if (counter_header==16)
{
// Read low byte of message length
message_length = (unsigned char)cbuf[num];
}
else if (counter_header==17)
{
// Read high byte of message length
counter_header = 0;
message_length = (message_length<<8 | ((unsigned char)cbuf[num])) +16;
next_message_pos = current_message_pos + message_length;
if(next_message_pos==file_size)
{
// last message found in file
files[numFile]->indexAll.append(current_message_pos);
break;
}
// speed up move directly to next message, if inside current buffer
if((message_length > 20))
{
if((num+message_length-20<cbuf_sz))
{
num+=message_length-20;
}
}
}
}
else if(cbuf[num] == 'D')
{
lastFound = 'D';
}
else if(lastFound == 'D' && cbuf[num] == 'L')
{
lastFound = 'L';
}
else if(lastFound == 'L' && cbuf[num] == 'T')
{
lastFound = 'T';
}
else if(lastFound == 'T' && cbuf[num] == 0x01)
{
if(next_message_pos == 0)
{
// first message detected or first message after error
current_message_pos = pos+num-3;
counter_header = 1;
if(current_message_pos!=0)
{
// first messages not at beginning or error occured before
errors_in_file++;
}
// speed up move directly to message length, if inside current buffer
if(num+14<cbuf_sz)
{
num+=14;
counter_header+=14;
}
}
else if( next_message_pos == (pos+num-3) )
{
// Add message only when it is in the correct position in relationship to the last message
files[numFile]->indexAll.append(current_message_pos);
current_message_pos = pos+num-3;
counter_header = 1;
// speed up move directly to message length, if inside current buffer
if(num+14<cbuf_sz)
{
num+=14;
counter_header+=14;
}
}
else if(next_message_pos > (pos+num-3))
{
// Header detected before end of message
}
else
{
// Header detected after end of message
// start search for new message back after last header found
files[numFile]->infile.seek(current_message_pos+4);
pos = current_message_pos+4;
buf = files[numFile]->infile.read(READ_BUF_SZ);
cbuf_sz = buf.size();
cbuf = buf.constData();
num=0;
next_message_pos = 0;
}
lastFound = 0;
}
else
{
lastFound = 0;
}
}
pos += cbuf_sz;
}
}
mutexQDlt.unlock();
/* success */
return true;
}
bool QDltFile::createIndexFilter()
{
/* clear old index */
indexFilter.clear();
return updateIndexFilter();
}
bool QDltFile::updateIndexFilter()
{
QDltMsg msg;
QByteArray buf;
int index;
/* update index filter by starting from last found index in list */
/* get lattest found index in filter list */
if(indexFilter.size()>0) {
index = indexFilter[indexFilter.size()-1] + 1;
}
else {
index = 0;
}
for(int num=index;num<size();num++) {
buf = getMsg(num);
if(!buf.isEmpty()) {
msg.setMsg(buf);
if(checkFilter(msg)) {
indexFilter.append(num);
}
}
}
return true;
}
bool QDltFile::checkFilter(QDltMsg &msg)
{
if(!filterFlag)
{
return true;
}
return filterList.checkFilter(msg);
}
QDltFilterList QDltFile::getFilterList() const
{
return filterList;
}
void QDltFile::setFilterList(QDltFilterList &_filterList)
{
filterList = _filterList;
}
void QDltFile::clearFilterIndex()
{
/* clear old index */
indexFilter.clear();
}
void QDltFile::addFilterIndex (int index)
{
indexFilter.append(index);
}
#ifdef USECOLOR
QColor QDltFile::checkMarker(QDltMsg &msg)
{
if(!filterFlag)
{
return QColor();
}
return filterList.checkMarker(msg);
}
#else
QString QDltFile::checkMarker(QDltMsg &msg)
{
if(!filterFlag)
{
return QString(DEFAULT_COLOR);
}
return filterList.checkMarker(msg);
}
#endif
QString QDltFile::getFileName(int num)
{
if(num<0 || num>=files.size())
return QString();
return files[num]->infile.fileName();
}
int QDltFile::getFileMsgNumber(int num) const
{
if(num<0 || num>=files.size())
return -1;
return files[num]->indexAll.size();
}
void QDltFile::close()
{
/* close file */
clear();
}
QByteArray QDltFile::getMsg(int index) const
{
QByteArray buf;
int num = 0;
/* check if index is in range */
if( index < 0 )
{
qDebug() << "getMsg: Index is out of range" << __FILE__ << "line" << __LINE__;
/* return empty data buffer */
return QByteArray();
}
for( num=0; num < files.size(); num++ )
{
if(index < files[num]->indexAll.size())
break;
else
index -= files[num]->indexAll.size();
}
if(num >= files.size())
{
qDebug() << "getMsg: Index is out of range in" << __FILE__ << "line" << __LINE__;
/* return empty data buffer */
return QByteArray();
}
/* check if file is already opened */
if(false == files[num]->infile.isOpen())
{
/* return empty buffer */
qDebug() << "getMsg: Infile is not open" << files[num]->infile.fileName() << __FILE__ << "line" << __LINE__;
/* return empty data buffer */
return QByteArray();
}
mutexQDlt.lock();
QDltFileItem* file = files[num];
const QDltFileItem* const_file = file;
qint64 positionForIndex = const_file->indexAll[index];
/* move to file position selected by index */
if ( false == file->infile.seek(positionForIndex) )
{
qDebug() << "Seek error on " << positionForIndex << file->infile.fileName() << __FILE__ << __LINE__;
mutexQDlt.unlock();
buf.clear();
return buf;
}
/* read DLT message from file */
if(index == (file->indexAll.size()-1))
{
/* last message in file */
long int cal_index = file->infile.size() - positionForIndex;
if ( cal_index < 0 )
qDebug() << "Negativ index " << cal_index << index << "in" << file->infile.fileName() << __LINE__ << "of" << __FILE__;
else
buf = file->infile.read(file->infile.size() - positionForIndex);
}
else
{
/* any other file position */
long int cal_index = const_file->indexAll[index+1] - positionForIndex;
if ( cal_index < 0 )
qDebug() << "Negativ index " << cal_index << index << "in" << __LINE__ << "of" << __FILE__;
else
buf = file->infile.read(cal_index);
}
mutexQDlt.unlock();
/* return DLT message buffer */
return buf;
}
bool QDltFile::getMsg(int index,QDltMsg &msg) const
{
QByteArray data = getMsg(index);
if(data.isEmpty())
return false;
return msg.setMsg(data);
}
QByteArray QDltFile::getMsgFilter(int index) const
{
if(filterFlag)
{
/* check if index is in range */
if(index<0 || index>=indexFilter.size())
{
qDebug() << "getMsg: Index is out of range" << __FILE__ << "line" << __LINE__;
/* return empty data buffer */
return QByteArray();
}
return getMsg(indexFilter[index]);
}
else
{
/* check if index is in range */
if(index<0 || index>=size())
{
qDebug() << "getMsg: Index" << index << "is out of range" << size() << __FILE__ << "line" << __LINE__;
/* return empty data buffer */
return QByteArray();
}
return getMsg(index);
}
}
int QDltFile::getMsgFilterPos(int index) const
{
if(filterFlag)
{
/* check if index is in range */
if(index<0 || index>=indexFilter.size())
{
//qDebug() << "getMsg: Index is out of range" << __FILE__ << "line" << __LINE__;
qDebug() << "getMsg: Index" << index << "is out of range" << indexFilter.size() << __FILE__ << "line" << __LINE__;
/* return invalid */
return -1;
}
return indexFilter[index];
}
else {
/* check if index is in range */
if(index<0 || index>=size())
{
qDebug() << "getMsg: Index is out of range" << __FILE__ << "line" << __LINE__;
/* return invalid */
return -1;
}
return index;
}
}
void QDltFile::clearFilter()
{
filterList.clearFilter();
}
void QDltFile::addFilter(QDltFilter *_filter)
{
filterList.addFilter(_filter);
}
void QDltFile::updateSortedFilter()
{
filterList.updateSortedFilter();
}
bool QDltFile::isFilter() const
{
return filterFlag;
}
void QDltFile::enableFilter(bool state)
{
filterFlag = state;
}
void QDltFile::enableSortByTime(bool state)
{
sortByTimeFlag = state;
}
void QDltFile::enableSortByTimestamp(bool state)
{
sortByTimestampFlag = state;
}
QVector<qint64> QDltFile::getIndexFilter() const
{
return indexFilter;
}
void QDltFile::setIndexFilter(QVector<qint64> _indexFilter)
{
indexFilter = _indexFilter;
}
|