From 763b20136edf4b7d035275a0c44f84a85efd7abf Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Thu, 28 Jan 2016 19:54:52 -0500 Subject: [PATCH] Fixed up the rest of the fuzzing window. It should be pretty complete and functional now. --- fuzzingwindow.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++--- fuzzingwindow.h | 5 +- 2 files changed, 126 insertions(+), 9 deletions(-) diff --git a/fuzzingwindow.cpp b/fuzzingwindow.cpp index 27d5ec0..e378859 100644 --- a/fuzzingwindow.cpp +++ b/fuzzingwindow.cpp @@ -2,6 +2,7 @@ #include "ui_fuzzingwindow.h" #include "utility.h" #include +#include "mainwindow.h" FuzzingWindow::FuzzingWindow(const QVector *frames, QWidget *parent) : QDialog(parent), @@ -22,13 +23,19 @@ FuzzingWindow::FuzzingWindow(const QVector *frames, QWidget *parent) : connect(ui->spinBytes, SIGNAL(valueChanged(int)), this, SLOT(changedNumDataBytes(int))); connect(ui->bitfield, SIGNAL(gridClicked(int,int)), this, SLOT(bitfieldClicked(int,int))); + connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int))); + refreshIDList(); currentlyFuzzing = false; for (int j = 0; j < 64; j++) bitGrid[j] = 1; + numBits = 64; + bitAccum = 0; redrawGrid(); + fuzzTimer->setInterval(ui->spinTiming->value()); + ui->cbBuses->addItem(tr("0")); ui->cbBuses->addItem(tr("1")); ui->cbBuses->addItem(tr("Both")); @@ -39,6 +46,41 @@ FuzzingWindow::~FuzzingWindow() delete ui; } +void FuzzingWindow::updatedFrames(int numFrames) +{ + CANFrame thisFrame; + int id; + if (numFrames == -1) //all frames deleted. Kill the display + { + ui->listID->clear(); + foundIDs.clear(); + refreshIDList(); + } + else if (numFrames == -2) //all new set of frames. Reset + { + ui->listID->clear(); + foundIDs.clear(); + refreshIDList(); + } + else //just got some new frames. See if they are relevant. + { + for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++) + { + id = modelFrames->at(i).ID; + if (!foundIDs.contains(id)) + { + foundIDs.append(id); + selectedIDs.append(id); + QListWidgetItem *thisItem = new QListWidgetItem(); + thisItem->setText(Utility::formatNumber(id)); + thisItem->setFlags(thisItem->flags() | Qt::ItemIsUserCheckable); + thisItem->setCheckState(Qt::Checked); + ui->listID->addItem(thisItem); + } + } + } +} + void FuzzingWindow::changePlaybackSpeed(int newSpeed) { fuzzTimer->setInterval(newSpeed); @@ -116,15 +158,18 @@ void FuzzingWindow::setAllFilters() void FuzzingWindow::calcNextID() { if (seqIDScan) - { - currentID++; + { if (rangeIDSelect) { + currentID++; if (currentID > endID) currentID = startID; } else //IDs by filter. So, select the first filter { - if (currentID > selectedIDs.length()) currentID = 0; + currentIdx++; + if (currentIdx >= selectedIDs.length()) currentIdx = 0; + currentID = selectedIDs[currentIdx]; + qDebug() << "idx id: " << currentID; } } else //random IDs @@ -137,13 +182,16 @@ void FuzzingWindow::calcNextID() } else //IDs by filter so pick a random selected ID from the filter list { - currentID = qrand() % selectedIDs.length(); + currentIdx = qrand() % selectedIDs.length(); + currentID = selectedIDs[currentIdx]; } } } void FuzzingWindow::calcNextBitPattern() { + uint64_t accum; + switch (bitSequenceType) { case BitSequenceType::Random: @@ -163,8 +211,53 @@ void FuzzingWindow::calcNextBitPattern() } break; case BitSequenceType::Sequential: + bitAccum++; + bitAccum &= ((1 << numBits) - 1); + accum = bitAccum; + for (int byt = 0; byt < ui->spinBytes->value(); byt++) + { + currentBytes[byt] = 0; + for (int bit = 0; bit < 8; bit++) + { + thisBit = bitGrid[byt * 8 + bit]; + if (thisBit == 1) + { + if (accum & 1) currentBytes[byt] |= (1 << bit); + accum >>= 1; + } + if (thisBit == 2) currentBytes[byt] |= (1 << bit); + } + } break; case BitSequenceType::Sweeping: + qDebug() << "Start " << bitAccum; + accum = bitAccum; + + int offset; + for (int i = 1; i < 64; i++) + { + offset = (i + bitAccum) % 64; + if (bitGrid[offset] == 1) + { + bitAccum = offset; + qDebug() << "End " << bitAccum; + break; + } + } + + for (int byt = 0; byt < ui->spinBytes->value(); byt++) + { + currentBytes[byt] = 0; + for (int bit = 0; bit < 8; bit++) + { + thisBit = bitGrid[byt * 8 + bit]; + if (thisBit == 1 && (byt * 8 + bit) == bitAccum) + { + currentBytes[byt] |= (1 << bit); + } + if (thisBit == 2) currentBytes[byt] |= (1 << bit); + } + } break; } } @@ -188,8 +281,23 @@ void FuzzingWindow::toggleFuzzing() seqIDScan = ui->rbSequentialID->isChecked(); rangeIDSelect = ui->rbRangeIDSel->isChecked(); if (ui->rbSequentialBits->isChecked()) bitSequenceType = BitSequenceType::Sequential; - if (ui->rbRandomBits->isChecked()) bitSequenceType = BitSequenceType::Random; - if (ui->rbSweep->isChecked()) bitSequenceType = BitSequenceType::Sweeping; + if (ui->rbRandomBits->isChecked()) + { + bitSequenceType = BitSequenceType::Random; + bitAccum = 0; + } + if (ui->rbSweep->isChecked()) + { + bitSequenceType = BitSequenceType::Sweeping; + for (int i = 0; i < 64; i++) + { + if (bitGrid[i] == 1) + { + bitAccum = i; + break; + } + } + } numSentFrames = 0; @@ -201,7 +309,8 @@ void FuzzingWindow::toggleFuzzing() } else //IDs by filter. So, select the first filter { - currentID = 0; + currentIdx = 0; + currentID = selectedIDs[currentIdx]; } } else //random IDs @@ -214,7 +323,8 @@ void FuzzingWindow::toggleFuzzing() } else //IDs by filter so pick a random selected ID from the filter list { - currentID = qrand() % selectedIDs.length(); + currentIdx = qrand() % selectedIDs.length(); + currentID = selectedIDs[currentIdx]; } } @@ -227,6 +337,7 @@ void FuzzingWindow::toggleFuzzing() void FuzzingWindow::refreshIDList() { ui->listID->clear(); + foundIDs.clear(); int id; for (int i = 0; i < modelFrames->count(); i++) @@ -298,6 +409,8 @@ void FuzzingWindow::redrawGrid() usedBytes[j] = 0; } + numBits = 0; + for (int i = 0; i < 64; i++) { int byt = i / 8; @@ -308,6 +421,7 @@ void FuzzingWindow::redrawGrid() break; case 1: //Green, fuzz this bit dataBytes[byt] |= (1 << bit); + numBits++; break; case 2: //black, bit always set dataBytes[byt] |= (1 << bit); diff --git a/fuzzingwindow.h b/fuzzingwindow.h index 0a43009..4521602 100644 --- a/fuzzingwindow.h +++ b/fuzzingwindow.h @@ -41,6 +41,7 @@ private slots: void idListChanged(QListWidgetItem *item); void bitfieldClicked(int, int); void changedNumDataBytes(int newVal); + void updatedFrames(int numFrames); private: Ui::FuzzingWindow *ui; @@ -49,12 +50,14 @@ private: QList foundIDs; QList selectedIDs; QList sendingBuffer; - int startID, endID, currentID; + int startID, endID, currentID, currentIdx; bool seqIDScan, rangeIDSelect; int bitSequenceType; bool currentlyFuzzing; uint8_t currentBytes[8]; uint8_t bitGrid[64]; + uint8_t numBits; + uint64_t bitAccum; int numSentFrames; void refreshIDList();