forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canframemodel.cpp
625 lines (549 loc) · 16.3 KB
/
canframemodel.cpp
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
#include "canframemodel.h"
#include <QFile>
#include <QApplication>
#include <QPalette>
#include <QDateTime>
#include "utility.h"
CANFrameModel::~CANFrameModel()
{
frames.clear();
filteredFrames.clear();
filters.clear();
}
int CANFrameModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
if (filteredFrames.data())
{
int rows = filteredFrames.count();
return rows;
}
//just in case somehow data is invalid which I have seen before.
//But, this should not happen so issue a debugging message too
qDebug() << "Invalid data for filteredFrames. Returning 0.";
return 0;
}
int CANFrameModel::totalFrameCount()
{
int count;
count = frames.count();
return count;
}
int CANFrameModel::columnCount(const QModelIndex &index) const
{
Q_UNUSED(index);
return 7;
}
CANFrameModel::CANFrameModel(QObject *parent)
: QAbstractTableModel(parent)
{
if (QSysInfo::WordSize > 32)
{
qDebug() << "64 bit OS detected. Requesting a large preallocation";
preallocSize = 10000000;
}
else //if compiling for 32 bit you can't ask for gigabytes of preallocation so tone it down.
{
qDebug() << "32 bit OS detected. Requesting a much restricted prealloc";
preallocSize = 2000000;
}
frames.reserve(preallocSize);
filteredFrames.reserve(preallocSize); //the goal is to prevent a reallocation from ever happening
dbcHandler = DBCHandler::getReference();
interpretFrames = false;
overwriteDups = false;
useHexMode = true;
timeSeconds = false;
timeOffset = 0;
needFilterRefresh = false;
lastUpdateNumFrames = 0;
timeFormat = "MMM-dd HH:mm:ss.zzz";
}
void CANFrameModel::setHexMode(bool mode)
{
if (useHexMode != mode)
{
this->beginResetModel();
useHexMode = mode;
Utility::decimalMode = !useHexMode;
this->endResetModel();
}
}
void CANFrameModel::setSecondsMode(bool mode)
{
if (Utility::secondsMode != mode)
{
this->beginResetModel();
Utility::secondsMode = mode;
this->endResetModel();
}
}
void CANFrameModel::setSysTimeMode(bool mode)
{
if (Utility::sysTimeMode != mode)
{
this->beginResetModel();
Utility::sysTimeMode = mode;
this->endResetModel();
}
}
void CANFrameModel::setInterpetMode(bool mode)
{
//if the state of interpretFrames changes then we need to reset the model
//so that QT will refresh the view properly
if (interpretFrames != mode)
{
this->beginResetModel();
interpretFrames = mode;
this->endResetModel();
}
}
void CANFrameModel::setTimeFormat(QString format)
{
Utility::timeFormat = format;
beginResetModel(); //reset model to show new time format
endResetModel();
}
void CANFrameModel::normalizeTiming()
{
mutex.lock();
if (frames.count() == 0) return;
timeOffset = frames[0].timestamp;
for (int j = 0; j < frames.count(); j++)
{
if (frames[j].timestamp < timeOffset) timeOffset = frames[j].timestamp;
}
for (int i = 0; i < frames.count(); i++)
{
frames[i].timestamp -= timeOffset;
}
this->beginResetModel();
for (int i = 0; i < filteredFrames.count(); i++)
{
filteredFrames[i].timestamp -= timeOffset;
}
this->endResetModel();
mutex.unlock();
}
void CANFrameModel::setOverwriteMode(bool mode)
{
beginResetModel();
overwriteDups = mode;
endResetModel();
}
void CANFrameModel::setFilterState(unsigned int ID, bool state)
{
if (!filters.contains(ID)) return;
filters[ID] = state;
sendRefresh();
}
void CANFrameModel::setAllFilters(bool state)
{
QMap<int, bool>::iterator it;
for (it = filters.begin(); it != filters.end(); ++it)
{
it.value() = state;
}
sendRefresh();
}
void CANFrameModel::recalcOverwrite()
{
if (!overwriteDups) return; //no need to do a thing if mode is disabled
qDebug() << "recalcOverwrite called in model";
int lastUnique = 0;
bool found;
mutex.lock();
beginResetModel();
for (int i = 1; i < frames.count(); i++)
{
found = false;
for (int j = 0; j <= lastUnique; j++)
{
if (frames[i].ID == frames[j].ID)
{
frames.replace(j, frames[i]);
found = true;
break;
}
}
if (!found)
{
lastUnique++;
frames.replace(lastUnique, frames[i]);
}
}
while (frames.count() > lastUnique) frames.removeLast();
filteredFrames.clear();
filteredFrames.reserve(preallocSize);
for (int i = 0; i < frames.count(); i++)
{
if (filters[frames[i].ID])
{
filteredFrames.append(frames[i]);
}
}
endResetModel();
mutex.unlock();
}
enum class Column {
TimeStamp = 0, ///< The timestamp when the frame was transmitted or received
FrameId = 1, ///< The frames CAN identifier (Standard: 11 or Extended: 29 bit)
Extended = 2, ///< True if the frames CAN identifier is 29 bit
Direction = 3, ///< Whether the frame was transmitted or received
Bus = 4, ///< The bus where the frame was transmitted or received
Length = 5, ///< The frames payload data length
Data = 6 ///< The frames payload data
};
QVariant CANFrameModel::data(const QModelIndex &index, int role) const
{
int dLen;
QString tempString;
CANFrame thisFrame;
if (!index.isValid())
return QVariant();
if (index.row() >= (filteredFrames.count()))
return QVariant();
thisFrame = filteredFrames.at(index.row());
if (role == Qt::BackgroundColorRole)
{
if (dbcHandler != NULL && interpretFrames)
{
DBC_MESSAGE *msg = dbcHandler->findMessage(thisFrame);
if (msg != NULL)
{
return msg->bgColor;
}
}
//return QApplication::palette().color(QPalette::Button);
return QColor(Qt::white);
}
if (role == Qt::TextColorRole)
{
if (dbcHandler != NULL && interpretFrames)
{
DBC_MESSAGE *msg = dbcHandler->findMessage(thisFrame);
if (msg != NULL)
{
return msg->fgColor;
}
}
return QApplication::palette().color(QPalette::WindowText);
}
if (role == Qt::DisplayRole) {
switch (Column(index.column()))
{
case Column::TimeStamp:
return Utility::formatTimestamp(thisFrame.timestamp);
case Column::FrameId:
return Utility::formatCANID(thisFrame.ID, thisFrame.extended);
case Column::Extended:
return QString::number(thisFrame.extended);
case Column::Direction:
if (thisFrame.isReceived) return QString(tr("Rx"));
return QString(tr("Tx"));
case Column::Bus:
return QString::number(thisFrame.bus);
case Column::Length:
return QString::number(thisFrame.len);
case Column::Data:
dLen = thisFrame.len;
if (dLen < 0) dLen = 0;
if (dLen > 8) dLen = 8;
for (int i = 0; i < dLen; i++)
{
tempString.append(Utility::formatNumber(thisFrame.data[i]));
tempString.append(" ");
}
//now, if we're supposed to interpret the data and the DBC handler is loaded then use it
if (dbcHandler != NULL && interpretFrames)
{
DBC_MESSAGE *msg = dbcHandler->findMessage(thisFrame);
if (msg != NULL)
{
tempString.append("\n");
tempString.append(msg->name + "\n" + msg->comment + "\n");
for (int j = 0; j < msg->sigHandler->getCount(); j++)
{
QString sigString;
if (msg->sigHandler->findSignalByIdx(j)->processAsText(thisFrame, sigString))
{
tempString.append(sigString);
tempString.append("\n");
}
}
}
}
return tempString;
}
}
return QVariant();
}
QVariant CANFrameModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
{
switch (Column(section))
{
case Column::TimeStamp:
return QString(tr("Timestamp"));
case Column::FrameId:
return QString(tr("ID"));
case Column::Extended:
return QString(tr("Ext"));
case Column::Direction:
return QString(tr("Dir"));
case Column::Bus:
return QString(tr("Bus"));
case Column::Length:
return QString(tr("Len"));
case Column::Data:
return QString(tr("Data"));
}
}
else
return QString::number(section + 1);
return QVariant();
}
void CANFrameModel::addFrame(const CANFrame& frame, bool autoRefresh = false)
{
/*TODO: remove mutex */
mutex.lock();
CANFrame tempFrame;
tempFrame = frame;
tempFrame.timestamp -= timeOffset;
lastUpdateNumFrames++;
//if this ID isn't found in the filters list then add it and show it by default
if (!filters.contains(tempFrame.ID))
{
filters.insert(tempFrame.ID, true);
needFilterRefresh = true;
}
if (!overwriteDups)
{
frames.append(tempFrame);
if (filters[tempFrame.ID])
{
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count() + 1, filteredFrames.count() + 1);
filteredFrames.append(tempFrame);
if (autoRefresh) endInsertRows();
}
}
else //yes, overwrite dups
{
bool found = false;
for (int i = 0; i < frames.count(); i++)
{
if (frames[i].ID == tempFrame.ID)
{
frames.replace(i, tempFrame);
found = true;
break;
}
}
if (!found)
{
frames.append(tempFrame);
if (filters[tempFrame.ID])
{
if (autoRefresh) beginInsertRows(QModelIndex(), filteredFrames.count() + 1, filteredFrames.count() + 1);
filteredFrames.append(tempFrame);
if (autoRefresh) endInsertRows();
}
}
else
{
for (int j = 0; j < filteredFrames.count(); j++)
{
if (filteredFrames[j].ID == tempFrame.ID)
{
if (autoRefresh) beginResetModel();
filteredFrames.replace(j, tempFrame);
if (autoRefresh) endResetModel();
}
}
}
}
mutex.unlock();
}
void CANFrameModel::addFrames(const CANConnection*, const QVector<CANFrame>& pFrames)
{
foreach(const CANFrame& frame, pFrames)
{
addFrame(frame);
}
if (overwriteDups) //if in overwrite mode we'll update every time frames come in
{
beginResetModel();
endResetModel();
}
}
void CANFrameModel::sendRefresh()
{
qDebug() << "Sending mass refresh";
QVector<CANFrame> tempContainer;
int count = frames.count();
for (int i = 0; i < count; i++)
{
if (filters[frames[i].ID])
{
tempContainer.append(frames[i]);
}
}
mutex.lock();
beginResetModel();
filteredFrames.clear();
filteredFrames.reserve(preallocSize);
filteredFrames.append(tempContainer);
lastUpdateNumFrames = 0;
endResetModel();
mutex.unlock();
}
void CANFrameModel::sendRefresh(int pos)
{
beginInsertRows(QModelIndex(), pos, pos);
endInsertRows();
}
//issue a refresh for the last num entries in the model.
//used by the serial worker to do batch updates so it doesn't
//have to send thousands of messages per second
int CANFrameModel::sendBulkRefresh()
{
//int num = filteredFrames.count() - lastUpdateNumFrames;
if (lastUpdateNumFrames <= 0) return 0;
if (lastUpdateNumFrames == 0 && !overwriteDups) return 0;
if (filteredFrames.count() == 0) return 0;
qDebug() << "Bulk refresh of " << lastUpdateNumFrames;
beginResetModel();
endResetModel();
int num = lastUpdateNumFrames;
lastUpdateNumFrames = 0;
return num;
}
void CANFrameModel::clearFrames()
{
mutex.lock();
this->beginResetModel();
frames.clear();
filteredFrames.clear();
filters.clear();
frames.reserve(preallocSize);
filteredFrames.reserve(preallocSize);
this->endResetModel();
lastUpdateNumFrames = 0;
mutex.unlock();
emit updatedFiltersList();
}
/*
* Since the getListReference function returns readonly
* you can't insert frames with it. Instead this function
* allows for a mass import of frames into the model
*/
void CANFrameModel::insertFrames(const QVector<CANFrame> &newFrames)
{
//not resetting the model here because the serial worker automatically does a bulk refresh every 1/4 second
//and that refresh will cause the view to update. If you do both it usually ends up thinking you have
//double the number of frames.
//beginResetModel();
mutex.lock();
int insertedFiltered = 0;
for (int i = 0; i < newFrames.count(); i++)
{
frames.append(newFrames[i]);
if (!filters.contains(newFrames[i].ID))
{
filters.insert(newFrames[i].ID, true);
needFilterRefresh = true;
}
if (filters[newFrames[i].ID])
{
insertedFiltered++;
filteredFrames.append(newFrames[i]);
}
}
lastUpdateNumFrames = newFrames.count();
mutex.unlock();
//endResetModel();
//beginInsertRows(QModelIndex(), filteredFrames.count() + 1, filteredFrames.count() + insertedFiltered);
//endInsertRows();
if (needFilterRefresh) emit updatedFiltersList();
}
int CANFrameModel::getIndexFromTimeID(unsigned int ID, double timestamp)
{
int bestIndex = -1;
uint64_t intTimeStamp = timestamp * 1000000l;
for (int i = 0; i < frames.count(); i++)
{
if ((frames[i].ID == ID))
{
if (frames[i].timestamp <= intTimeStamp) bestIndex = i;
else break; //drop out of loop as soon as we pass the proper timestamp
}
}
return bestIndex;
}
void CANFrameModel::loadFilterFile(QString filename)
{
QFile *inFile = new QFile(filename);
QByteArray line;
int ID;
if (!inFile->open(QIODevice::ReadOnly | QIODevice::Text))
return;
filters.clear();
while (!inFile->atEnd()) {
line = inFile->readLine().simplified();
if (line.length() > 2)
{
QList<QByteArray> tokens = line.split(',');
ID = tokens[0].toInt(NULL, 16);
if (tokens[1].toUpper() == "T") filters.insert(ID, true);
else filters.insert(ID, false);
}
}
inFile->close();
sendRefresh();
emit updatedFiltersList();
}
void CANFrameModel::saveFilterFile(QString filename)
{
QFile *outFile = new QFile(filename);
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
return;
QMap<int, bool>::const_iterator it;
for (it = filters.begin(); it != filters.end(); ++it)
{
outFile->write(QString::number(it.key(), 16).toUtf8());
outFile->putChar(',');
if (it.value()) outFile->putChar('T');
else outFile->putChar('F');
outFile->write("\n");
}
outFile->close();
}
bool CANFrameModel::needsFilterRefresh()
{
bool temp = needFilterRefresh;
needFilterRefresh = false;
return temp;
}
/*
*This used to not be const correct but it is now. So, there's little harm in
* allowing external code to peek at our frames. There's just no touching.
* This ability to get a direct read-only reference speeds up a variety of
* external code that needs to access frames directly and doesn't care about
* this model's normal output mechanism.
*/
const QVector<CANFrame>* CANFrameModel::getListReference() const
{
return &frames;
}
const QVector<CANFrame>* CANFrameModel::getFilteredListReference() const
{
return &filteredFrames;
}
const QMap<int, bool>* CANFrameModel::getFiltersReference() const
{
return &filters;
}