diff --git a/config.h b/config.h index de7d50d..1fe461f 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG #define CONFIG -#define VERSION 149 +#define VERSION 150 //try to keep this in sync. //SavvyCAN will complain if you connect a GVRET board with a revision diff --git a/fuzzingwindow.cpp b/fuzzingwindow.cpp index f038269..77bbe06 100644 --- a/fuzzingwindow.cpp +++ b/fuzzingwindow.cpp @@ -1,17 +1,203 @@ #include "fuzzingwindow.h" #include "ui_fuzzingwindow.h" +#include "utility.h" +#include -FuzzingWindow::FuzzingWindow(const QVector *frames, SerialWorker *worker, QWidget *parent) : +FuzzingWindow::FuzzingWindow(const QVector *frames, QWidget *parent) : QDialog(parent), ui(new Ui::FuzzingWindow) { ui->setupUi(this); modelFrames = frames; - serialWorker = worker; + + fuzzTimer = new QTimer(); + + connect(ui->btnStartStop, &QPushButton::clicked, this, &FuzzingWindow::toggleFuzzing); + connect(ui->btnAllFilters, &QPushButton::clicked, this, &FuzzingWindow::setAllFilters); + connect(ui->btnNoFilters, &QPushButton::clicked, this, &FuzzingWindow::clearAllFilters); + connect(fuzzTimer, &QTimer::timeout, this, &FuzzingWindow::timerTriggered); + connect(ui->spinTiming, SIGNAL(valueChanged(int)), this, SLOT(changePlaybackSpeed(int))); + connect(ui->listID, &QListWidget::itemChanged, this, &FuzzingWindow::idListChanged); + + refreshIDList(); + + currentlyFuzzing = false; } FuzzingWindow::~FuzzingWindow() { delete ui; } + +void FuzzingWindow::changePlaybackSpeed(int newSpeed) +{ + fuzzTimer->setInterval(newSpeed); +} + +void FuzzingWindow::timerTriggered() +{ + CANFrame thisFrame; + sendingBuffer.clear(); + for (int count = 0; count < ui->spinBurst->value(); count++) + { + thisFrame.ID = currentID; + for (int i = 0; i < 8; i++) thisFrame.data[i] = currentBytes[i]; + if (currentID > 0x7FF) thisFrame.extended = true; + else thisFrame.extended = false; + thisFrame.bus = 0; //hard coded for now. TODO: do not hard code + thisFrame.len = 8; + sendingBuffer.append(thisFrame); + calcNextID(); + calcNextBitPattern(); + numSentFrames++; + } + emit sendFrameBatch(&sendingBuffer); + ui->lblNumFrames->setText("# of sent frames: " + QString::number(numSentFrames)); +} + +void FuzzingWindow::clearAllFilters() +{ + for (int i = 0; i < ui->listID->count(); i++) + { + ui->listID->item(i)->setCheckState(Qt::Unchecked); + } +} + +void FuzzingWindow::setAllFilters() +{ + for (int i = 0; i < ui->listID->count(); i++) + { + ui->listID->item(i)->setCheckState(Qt::Checked); + } +} + +void FuzzingWindow::calcNextID() +{ + if (seqIDScan) + { + currentID++; + if (rangeIDSelect) + { + if (currentID > endID) currentID = startID; + } + else //IDs by filter. So, select the first filter + { + if (currentID > selectedIDs.length()) currentID = 0; + } + } + else //random IDs + { + if (rangeIDSelect) + { + int range = endID - startID; + currentID = startID + qrand() % range; + } + else //IDs by filter so pick a random selected ID from the filter list + { + currentID = qrand() % selectedIDs.length(); + } + } +} + +void FuzzingWindow::calcNextBitPattern() +{ + //simple, quick random data for now + for (int i = 0; i < 8; i++) + { + currentBytes[i] = qrand() % 256; + } +} + +void FuzzingWindow::toggleFuzzing() +{ + if (currentlyFuzzing) //stop it then + { + ui->btnStartStop->setText("Start Fuzzing"); + currentlyFuzzing = false; + fuzzTimer->stop(); + } + else //start it then + { + ui->btnStartStop->setText("Stop Fuzzing"); + currentlyFuzzing = true; + + startID = Utility::ParseStringToNum(ui->txtStartID->text()); + endID = Utility::ParseStringToNum(ui->txtEndID->text()); + + seqIDScan = ui->rbSequentialID->isChecked(); + rangeIDSelect = ui->rbRangeIDSel->isChecked(); + seqBitSelect = ui->rbSequentialBits->isChecked(); + + numSentFrames = 0; + + if (seqIDScan) + { + if (rangeIDSelect) + { + currentID = startID; + } + else //IDs by filter. So, select the first filter + { + currentID = 0; + } + } + else //random IDs + { + if (rangeIDSelect) + { + int range = endID - startID; + currentID = startID + qrand() % range; + } + else //IDs by filter so pick a random selected ID from the filter list + { + currentID = qrand() % selectedIDs.length(); + } + } + + calcNextBitPattern(); + + fuzzTimer->start(); + } +} + +void FuzzingWindow::refreshIDList() +{ + ui->listID->clear(); + + int id; + for (int i = 0; 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); + } + } + //default is to sort in ascending order + ui->listID->sortItems(); +} + +void FuzzingWindow::idListChanged(QListWidgetItem *item) +{ + int id = Utility::ParseStringToNum(item->text()); + if (item->checkState() == Qt::Checked) + { + if (!selectedIDs.contains(id)) + { + qDebug() << "adding " << id << " to list of selected IDs"; + selectedIDs.append(id); + } + } + else + { + qDebug() << "removing " << id << " from the list of selected ids"; + selectedIDs.removeOne(id); + } +} diff --git a/fuzzingwindow.h b/fuzzingwindow.h index 2e13882..fe88dab 100644 --- a/fuzzingwindow.h +++ b/fuzzingwindow.h @@ -2,8 +2,9 @@ #define FUZZINGWINDOW_H #include +#include +#include #include "can_structs.h" -#include "serialworker.h" namespace Ui { class FuzzingWindow; @@ -14,14 +15,38 @@ class FuzzingWindow : public QDialog Q_OBJECT public: - explicit FuzzingWindow(const QVector *frames, SerialWorker *worker, QWidget *parent = 0); + explicit FuzzingWindow(const QVector *frames, QWidget *parent = 0); ~FuzzingWindow(); +signals: + void sendCANFrame(const CANFrame *, int); + void sendFrameBatch(const QList *); + +private slots: + void changePlaybackSpeed(int newSpeed); + void timerTriggered(); + void clearAllFilters(); + void setAllFilters(); + void toggleFuzzing(); + void idListChanged(QListWidgetItem *item); + private: Ui::FuzzingWindow *ui; const QVector *modelFrames; QTimer *fuzzTimer; - SerialWorker *serialWorker; + QList foundIDs; + QList selectedIDs; + QList sendingBuffer; + int startID, endID, currentID; + bool seqIDScan, rangeIDSelect, seqBitSelect; + bool currentlyFuzzing; + uint8_t currentBytes[8]; + uint8_t bitGrid[64]; + int numSentFrames; + + void refreshIDList(); + void calcNextID(); + void calcNextBitPattern(); }; #endif // FUZZINGWINDOW_H diff --git a/fuzzingwindow.ui b/fuzzingwindow.ui index 943e5a1..43bf9ab 100644 --- a/fuzzingwindow.ui +++ b/fuzzingwindow.ui @@ -13,7 +13,7 @@ Fuzzing Window - + @@ -21,12 +21,19 @@ - Interframe Timing + Interframe Timing (ms) - + + + 10000 + + + 10 + + @@ -40,7 +47,17 @@ - + + + 1 + + + 10 + + + 1 + + @@ -53,19 +70,25 @@ ID Scanning + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + - + Seqential - + Random + + true + @@ -78,14 +101,17 @@ - + Range of IDs + + true + - + Filter List @@ -101,17 +127,20 @@ - + Sequential - + Random + + true + @@ -137,7 +166,7 @@ - + @@ -151,7 +180,7 @@ - + @@ -161,19 +190,19 @@ - + - + All - + None @@ -195,6 +224,27 @@ + + + + Start Fuzzing + + + + + + + + 11 + 75 + true + + + + # of sent frames: + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index fe94de8..078ae66 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -923,7 +923,9 @@ void MainWindow::showFuzzingWindow() { if (!fuzzingWindow) { - fuzzingWindow = new FuzzingWindow(model->getListReference(), worker); + fuzzingWindow = new FuzzingWindow(model->getListReference()); + connect(fuzzingWindow, SIGNAL(sendCANFrame(const CANFrame*,int)), worker, SLOT(sendFrame(const CANFrame*,int))); + connect(fuzzingWindow, SIGNAL(sendFrameBatch(const QList*)), worker, SLOT(sendFrameBatch(const QList*))); } fuzzingWindow->show(); } diff --git a/serialworker.cpp b/serialworker.cpp index d6d695d..d6f212b 100644 --- a/serialworker.cpp +++ b/serialworker.cpp @@ -193,8 +193,10 @@ void SerialWorker::sendFrame(const CANFrame *frame, int bus = 0) //Don't get carried away here. The GVRET firmware only has finite //buffers and besides, the other end will get buried in traffic. void SerialWorker::sendFrameBatch(const QList *frames) -{ +{ + sendBulkMutex.lock(); for (int i = 0; i < frames->length(); i++) sendFrame(&frames->at(i), frames->at(i).bus); + sendBulkMutex.unlock(); } void SerialWorker::updateBaudRates(int Speed1, int Speed2) diff --git a/serialworker.h b/serialworker.h index 0741fab..2b71266 100644 --- a/serialworker.h +++ b/serialworker.h @@ -76,6 +76,7 @@ private: CANFrameModel *canModel; QTimer *ticker; QTime *elapsedTime; + QMutex sendBulkMutex; int framesPerSec; int gotFrames; int targetID;