forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangestatewindow.cpp
480 lines (403 loc) · 15.9 KB
/
rangestatewindow.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
#include "rangestatewindow.h"
#include "ui_rangestatewindow.h"
#include "mainwindow.h"
#include "utility.h"
#include "helpwindow.h"
#include "filterutility.h"
RangeStateWindow::RangeStateWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::RangeStateWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
modelFrames = frames;
ui->graphSignal->xAxis->setRange(0, 8);
ui->graphSignal->yAxis->setRange(-10, 265); //run range a bit outside possible number so they aren't plotted in a hard to see place
ui->graphSignal->xAxis->setVisible(false);
ui->graphSignal->yAxis->setVisible(false);
//ui->graphSignal->axisRect()->setupFullAxesBox();
ui->cbSignalMode->addItem(tr("Big Endian"));
ui->cbSignalMode->addItem(tr("Little Endian"));
ui->cbSignalMode->addItem(tr("Try Both"));
ui->cbSignedMode->addItem(tr("Signed Value"));
ui->cbSignedMode->addItem(tr("Unsigned Value"));
ui->cbSignedMode->addItem(tr("Try Both"));
//lambda expressions used here because these are tiny functions that don't have any reason to be full named functions
//unfortunately the fact that valueChanged is overloaded makes the syntax here HORRIBLE. That sucks.
connect(ui->spinMaxSigSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
[=](int newVal)
{
if (newVal < ui->spinMinSigSize->value()) ui->spinMinSigSize->setValue(newVal);
});
connect(ui->spinMinSigSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
[=](int newVal)
{
if (newVal > ui->spinMaxSigSize->value()) ui->spinMaxSigSize->setValue(newVal);
});
connect(ui->btnAllFilter, &QAbstractButton::clicked,
[=]()
{
for (int i = 0; i < ui->listFilter->count(); i++)
{
QListWidgetItem *item = ui->listFilter->item(i);
item->setCheckState(Qt::Checked);
idFilters[Utility::ParseStringToNum(item->text())] = true;
}
});
connect(ui->btnNoneFilter, &QAbstractButton::clicked,
[=]()
{
for (int i = 0; i < ui->listFilter->count(); i++)
{
QListWidgetItem *item = ui->listFilter->item(i);
item->setCheckState(Qt::Unchecked);
idFilters[Utility::ParseStringToNum(item->text())] = false;
}
});
connect(ui->listFilter, &QListWidget::itemChanged,
[=](QListWidgetItem *item)
{
bool isChecked = false;
int id = FilterUtility::getIdAsInt(item);
if (item->checkState() == Qt::Checked) isChecked = true;
idFilters[id] = isChecked;
});
connect(ui->btnRecalc, &QAbstractButton::clicked, this, &RangeStateWindow::recalcButton);
connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int)));
connect(ui->listCandidates, &QListWidget::currentRowChanged, this, &RangeStateWindow::clickedSignalList);
}
RangeStateWindow::~RangeStateWindow()
{
delete ui;
}
void RangeStateWindow::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
readSettings();
refreshFilterList();
installEventFilter(this);
}
void RangeStateWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
removeEventFilter(this);
writeSettings();
}
bool RangeStateWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key())
{
case Qt::Key_F1:
HelpWindow::getRef()->showHelp("rangestate.md");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
void RangeStateWindow::readSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
resize(settings.value("RangeStateView/WindowSize", QSize(765, 615)).toSize());
move(Utility::constrainedWindowPos(settings.value("RangeStateView/WindowPos", QPoint(50, 50)).toPoint()));
}
}
void RangeStateWindow::writeSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
settings.setValue("RangeStateView/WindowSize", size());
settings.setValue("RangeStateView/WindowPos", pos());
}
}
void RangeStateWindow::updatedFrames(int numFrames)
{
CANFrame thisFrame;
if (numFrames == -1) //all frames deleted. We don't need to do a thing on this window but erase everything in the filters section
{
ui->listFilter->clear();
idFilters.clear();
}
else if (numFrames == -2) //all new set of frames. Reset
{
refreshFilterList();
}
else //just got some new frames. See if we need to update the filters list. Otherwise nothing to do - no recalc happens until the button is pressed
{
if (numFrames > modelFrames->count()) return;
for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++)
{
thisFrame = modelFrames->at(i);
if (!idFilters.contains(thisFrame.frameId()))
{
idFilters.insert(thisFrame.frameId(), true);
FilterUtility::createCheckableFilterItem(thisFrame.frameId(), true, ui->listFilter);
}
}
}
}
void RangeStateWindow::refreshFilterList()
{
int id;
idFilters.clear();
ui->listFilter->clear();
for (int i = 0; i < modelFrames->length(); i++)
{
id = modelFrames->at(i).frameId();
if (!idFilters.contains(id))
{
idFilters.insert(id, true);
FilterUtility::createCheckableFilterItem(id, true, ui->listFilter);
}
}
ui->listFilter->sortItems();
}
void RangeStateWindow::recalcButton()
{
QMap<int, bool>::iterator iter;
uint32_t id;
ui->listCandidates->clear();
foundSignals.clear();
ui->graphSignal->clearGraphs();
QProgressDialog progress(qApp->activeWindow());
progress.setWindowModality(Qt::WindowModal);
progress.setLabelText("Calculating");
progress.setCancelButton(0);
progress.setRange(0,0);
progress.setMinimumDuration(0);
progress.show();
for (iter = idFilters.begin(); iter != idFilters.end(); ++iter)
{
if (iter.value() == true)
{
qDebug() << "Processing for ID: " << iter.key();
//so, we're supposed to process this frame ID. We'll need to create a frame cache for it
frameCache.clear();
frameCache.reserve(modelFrames->count()); //block allocate more than enough space
id = iter.key();
for (int j = 0; j < modelFrames->count(); j++)
{
if (modelFrames->at(j).frameId() == id) frameCache.append(modelFrames->at(j));
}
//now we've got a list with all the same ID. Time to send it off for processing
signalsFactory();
}
}
progress.cancel();
qDebug() << "Found " << foundSignals.count() << " signals total.";
}
/*
* Uses the settings exposed to the user to generate a set of candidate signals that should be checked.
* The user could specify signal sizes, granularity, endian type and we generate all the permutations from there
* Should process from max to min and stop when a valid signal is found (at least as an option) to declutter a bit.
* Mostly what we're interested in is the largest signal that matches
*/
void RangeStateWindow::signalsFactory()
{
int minSig = ui->spinMinSigSize->value();
int maxSig = ui->spinMaxSigSize->value();
int granularity = ui->spinGranularity->value();
int sigType = ui->cbSignalMode->currentIndex() + 1;
int signedType = ui->cbSignedMode->currentIndex() + 1;
int maxBits = frameCache.at(0).payload().length() * 8;
int sens = ui->slideSensitivity->value();
for (int sigSize = maxSig; sigSize >= minSig; sigSize -= granularity)
{
qApp->processEvents();
for (int startBit = 0; startBit < maxBits; startBit += granularity)
{
if (sigType & 1)
{
if (signedType & 1) processSignal(startBit, sigSize, sens, true, true);
if (signedType & 2) processSignal(startBit, sigSize, sens, true, false);
}
if (sigType & 2)
{
if (signedType & 1) processSignal(startBit, sigSize, sens, false, true);
if (signedType & 2) processSignal(startBit, sigSize, sens, false, false);
}
//have to try both types even with 8 bit and smaller signals
//because they could cross byte boundaries. Could check whether they
//do and not try both types if it is impossible.
}
}
}
/*
* Given the signal we generate the relevant data and figure out whether this signal seems to be a smooth range signal
*/
bool RangeStateWindow::processSignal(int startBit, int bitLength, int sensitivity, bool bigEndian, bool isSigned)
{
qDebug() << "";
qDebug() << "S:" << startBit << " B:" << bitLength << " Sens:" << sensitivity << " Big E:" << bigEndian << " Signed: " << isSigned;
QVector<int> scaledVals;
QVector<int> diff1;
QVector<int> diff2;
int64_t valu;
int64_t highestValue = -1000000000000LL;
int64_t lowestValue = 1000000000000LL;
double lerpPoint = ((double)sensitivity - 10.0) / 240.0;
int numFrames = frameCache.count();
scaledVals.reserve(frameCache.count());
diff1.reserve(frameCache.count() - 1);
diff2.reserve(frameCache.count() - 2);
int i;
for (i = 0; i < numFrames; i++)
{
valu = Utility::processIntegerSignal(frameCache.at(i).payload(), startBit, bitLength, !bigEndian, isSigned);
if (valu < lowestValue) lowestValue = valu;
if (valu > highestValue) highestValue = valu;
}
qDebug() << "Min: " << lowestValue << " Max: " << highestValue;
if (lowestValue == highestValue) return false; //a signal that never changes is worthless and not a range signal
int64_t range = highestValue - lowestValue;
int64_t maxRange = isSigned?(1<<(bitLength - 1)):(1 << bitLength);
//at highest sensitivity require signal to at least range 20% of max range
//at lowest sensitivity require signal to at least range 1% of max range
int64_t requiredRange = Utility::Lerp(maxRange * 0.01, maxRange * 0.2, lerpPoint);
if (range < requiredRange)
return false; //doesn't range enough.
for (i = 0; i < numFrames; i++)
scaledVals.append((int)((Utility::processIntegerSignal(frameCache.at(i).payload(), startBit, bitLength, !bigEndian, isSigned) - lowestValue)));
for (i = 1; i < numFrames; i++)
{
valu = scaledVals[i-1] - scaledVals[i];
diff1.append(valu);
}
for (i = 1; i < diff1.count(); i++)
{
valu = diff1[i-1] - diff1[i];
diff2.append(valu);
}
//now the differences are all stored so let's go through and see if first order diffs seem to suggest a ramping sort of signal or not.
//for a first test lets let through any signal where the first order diff doesn't seem too large
bool isGood = true;
int comparisonValue = Utility::Lerp((double)range * 0.55, 0, lerpPoint);
qDebug() << " 1st Delta comparisonvalue: " << comparisonValue;
int overValues = 0;
for (i = 0; i < diff1.count(); i++)
{
if (abs(diff1[i]) > comparisonValue) overValues++;
}
int maxOvers = Utility::Lerp(numFrames / 30.0, 2, lerpPoint);
qDebug() << "1st order overages: " << overValues << " Max Overs: " << maxOvers;
if (overValues > maxOvers) isGood = false;
if (isGood)
{
//now look at the second order differentials. This is acceleration. There shouldn't be hard acceleration in values for a ranging signal
comparisonValue = Utility::Lerp((double)range * 0.20, 1, lerpPoint);
maxOvers = Utility::Lerp(8, 2, lerpPoint); //really clamp down on second order over limits
qDebug() << "2nd Delta comparisonvalue: " << comparisonValue;
overValues = 0;
for (i = 0; i < diff2.count(); i++)
{
if (abs(diff2[i]) > comparisonValue) overValues++;
}
if (overValues > maxOvers) isGood = false;
qDebug() << "2nd order overages: " << overValues << " Max Overs: " << maxOvers;
if (overValues > maxOvers) isGood = false;
}
qDebug() << "Is this signal good: " << isGood << " Num overs: " << overValues;
if (isGood)
{
//createGraph(scaledVals);
QString temp;
temp = "ID: " + QString::number(frameCache.at(0).frameId(), 16) + " startBit: " + QString::number(startBit) + " len: " + QString::number(bitLength);
int64_t foundSig;
foundSig = frameCache.at(0).frameId();
foundSig += (int64_t)startBit << 32;
foundSig += (int64_t)bitLength << 40;
if (isSigned)
{
temp += " Signed";
foundSig += (int64_t)1 << 48;
}
else
{
temp += " Unsigned";
}
if (bigEndian)
{
temp += " BigEndian";
foundSig += (int64_t)1 << 49;
}
else
{
temp += " LittleEndian";
}
ui->listCandidates->addItem(temp);
foundSignals.append(foundSig);
}
return isGood;
}
//graphs the vector such that the X axis is just the index into the vector and Y is perfectly graphed within the window
void RangeStateWindow::createGraph(QVector<int> values)
{
int tempVal;
float minval=1000000, maxval = -100000;
int numEntries = values.count();
ui->graphSignal->clearGraphs();
QVector<double> x(numEntries), y(numEntries);
for (int j = 0; j < numEntries; j++)
{
tempVal = values[j];
x[j] = j;
y[j] = tempVal;
if (y[j] < minval) minval = y[j];
if (y[j] > maxval) maxval = y[j];
}
qDebug() << "YMin: " << minval << " YMax: " << maxval;
double ymin, ymax;
ymin = minval;
if (ymin < 0) ymin *= 1.2;
else ymin *= 0.8;
ymax = maxval;
if (ymax < 0) ymax *= 0.8;
else ymax *= 1.2;
if (fabs(ymin) < 0.01) ymin -= (ymax / 60.0);
if (fabs(ymax) < 0.01) ymax -= (ymin / 60.0);
qDebug() << "YFm: " << ymin << " YFM: " << ymax;
ui->graphSignal->addGraph();
ui->graphSignal->graph()->setName("Graph");
ui->graphSignal->graph()->setData(x,y);
ui->graphSignal->graph()->setLineStyle(QCPGraph::lsLine); //connect points with lines
QPen graphPen;
graphPen.setColor(Qt::black);
graphPen.setWidth(1);
ui->graphSignal->graph()->setPen(graphPen);
ui->graphSignal->xAxis->setRange(0, numEntries);
ui->graphSignal->yAxis->setRange(ymin, ymax);
ui->graphSignal->replot();
}
void RangeStateWindow::clickedSignalList(int idx)
{
if (idx == -1) return; //just in case...
uint32_t id, startBit, bitLength;
bool isSigned = false, isBigEndian = false;
int64_t valu;
valu = foundSignals.at(idx);
id = valu & 0xFFFFFFFFULL;
startBit = (valu >> 32) & 0xFF;
bitLength = (valu >> 40) & 0xFF;
if (valu & (1LL << 48)) isSigned = true;
if (valu & (1LL << 49)) isBigEndian = true;
qDebug() << "I:" << id << " sb:" << startBit << " len:" << bitLength << " signed:" << isSigned << " big:" << isBigEndian;
frameCache.clear();
frameCache.reserve(modelFrames->count()); //block allocate more than enough space
for (int j = 0; j < modelFrames->count(); j++)
{
if (modelFrames->at(j).frameId() == id) frameCache.append(modelFrames->at(j));
}
int numFrames = frameCache.count();
QVector<int> values;
values.reserve(numFrames);
for (int i = 0; i < numFrames; i++) values.append((int)((Utility::processIntegerSignal(frameCache.at(i).payload(), startBit, bitLength, !isBigEndian, isSigned))));
createGraph(values);
}