From 862f0775f8e9418b5ec3757b921b6020594c1b73 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Wed, 12 Aug 2020 12:30:15 +0800 Subject: [PATCH] Shorten the waitTime for execCMDWithOutput() --- common/util.cpp | 22 +++++++++++++++++++--- common/util.h | 24 +++++++++++++++++++++++- module/mifare.cpp | 7 ++++--- ui/mainwindow.cpp | 15 ++++++++++++--- ui/mainwindow.h | 3 +++ 5 files changed, 61 insertions(+), 10 deletions(-) diff --git a/common/util.cpp b/common/util.cpp index 707dfe7..bb4f3ab 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -27,22 +27,38 @@ void Util::execCMD(QString cmd) emit write(cmd + "\r\n"); } -QString Util::execCMDWithOutput(QString cmd, unsigned long waitTime) +QString Util::execCMDWithOutput(QString cmd, ReturnTrigger trigger) { + bool isResultFound = false; + QRegularExpression re; + re.setPatternOptions(QRegularExpression::DotMatchesEverythingOption); + if(!isRunning) return ""; QTime currTime = QTime::currentTime(); - QTime targetTime = QTime::currentTime().addMSecs(waitTime); + QTime targetTime = QTime::currentTime().addMSecs(trigger.waitTime); isRequiringOutput = true; requiredOutput->clear(); execCMD(cmd); while(QTime::currentTime() < targetTime) { QApplication::processEvents(); + for(QString otpt : trigger.expectedOutputs) + { + re.setPattern(otpt); + isResultFound = re.match(*requiredOutput).hasMatch(); + if(requiredOutput->contains(otpt)) + break; + } + if(isResultFound) + { + delay(200); + break; + } if(timeStamp > currTime) { currTime = timeStamp; - targetTime = timeStamp.addMSecs(waitTime); + targetTime = timeStamp.addMSecs(trigger.waitTime); } } isRequiringOutput = false; diff --git a/common/util.h b/common/util.h index b2ca006..bbf935a 100644 --- a/common/util.h +++ b/common/util.h @@ -9,6 +9,7 @@ #include #include #include +#include class Util : public QObject { @@ -20,12 +21,33 @@ public: CLIENTTYPE_ICEMAN, }; + struct ReturnTrigger + { + unsigned long waitTime; + QStringList expectedOutputs; + ReturnTrigger(unsigned long time) + { + waitTime = time; + expectedOutputs = QStringList(); + } + ReturnTrigger(QStringList outputs) + { + waitTime = 10000; + expectedOutputs = outputs; + } + ReturnTrigger(unsigned long time, QStringList outputs) + { + waitTime = time; + expectedOutputs = outputs; + } + }; + Q_ENUM(Util::ClientType) explicit Util(QObject *parent = nullptr); void execCMD(QString cmd); - QString execCMDWithOutput(QString cmd, unsigned long waitTime = 2000); + QString execCMDWithOutput(QString cmd, ReturnTrigger trigger = 10000); void delay(unsigned int msec); ClientType getClientType(); public slots: diff --git a/module/mifare.cpp b/module/mifare.cpp index a82816b..92d377e 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -119,7 +119,7 @@ void Mifare::chk() "hf mf chk *" + QString::number(cardType.type) + " ?", - 1000 + cardType.type * 1000); + Util::ReturnTrigger(1000 + cardType.sector_size * 200, {"No valid", "\\|---\\|----------------\\|----------------\\|"})); qDebug() << result; int offset = 0; @@ -183,7 +183,8 @@ void Mifare::nested() result = util->execCMDWithOutput( "hf mf nested " + QString::number(cardType.type) - + " *", 10000); + + " *", + Util::ReturnTrigger(10000, {"Can't found", "\\|000\\|"})); } else if(util->getClientType() == Util::CLIENTTYPE_ICEMAN) { @@ -624,7 +625,7 @@ void Mifare::writeSelected(TargetType targetType) continue; else if(choice == QMessageBox::YesToAll) yes2All = true; - else if(QMessageBox::NoToAll) + else if(choice == QMessageBox::NoToAll) { no2All = true; continue; diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 8fbbd8a..aac66f5 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -125,6 +125,10 @@ void MainWindow::refreshCMD(const QString& cmd) ui->Raw_CMDHistoryWidget->addItem(cmd); } +void MainWindow::on_stopButton_clicked() +{ + +} // ********************************************************* // ******************** raw command ******************** @@ -798,12 +802,15 @@ void MainWindow::uiInit() connectStatusBar = new QLabel(this); programStatusBar = new QLabel(this); PM3VersionBar = new QLabel(this); + stopButton = new QPushButton(this); setStatusBar(connectStatusBar, tr("Not Connected")); setStatusBar(programStatusBar, tr("Idle")); setStatusBar(PM3VersionBar, ""); + stopButton->setText(tr("Stop")); ui->statusbar->addPermanentWidget(PM3VersionBar, 1); ui->statusbar->addPermanentWidget(connectStatusBar, 1); ui->statusbar->addPermanentWidget(programStatusBar, 1); + ui->statusbar->addPermanentWidget(stopButton); ui->MF_dataWidget->setColumnCount(3); ui->MF_dataWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Sec"))); @@ -812,7 +819,7 @@ void MainWindow::uiInit() ui->MF_dataWidget->verticalHeader()->setVisible(false); ui->MF_dataWidget->setColumnWidth(0, 55); ui->MF_dataWidget->setColumnWidth(1, 55); - ui->MF_dataWidget->setColumnWidth(2, 430); + ui->MF_dataWidget->setColumnWidth(2, 450); ui->MF_keyWidget->setColumnCount(3); ui->MF_keyWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Sec"))); @@ -820,8 +827,8 @@ void MainWindow::uiInit() ui->MF_keyWidget->setHorizontalHeaderItem(2, new QTableWidgetItem(tr("KeyB"))); ui->MF_keyWidget->verticalHeader()->setVisible(false); ui->MF_keyWidget->setColumnWidth(0, 35); - ui->MF_keyWidget->setColumnWidth(1, 115); - ui->MF_keyWidget->setColumnWidth(2, 115); + ui->MF_keyWidget->setColumnWidth(1, 125); + ui->MF_keyWidget->setColumnWidth(2, 125); MF_widgetReset(); typeBtnGroup = new QButtonGroup(this); @@ -887,6 +894,8 @@ void MainWindow::signalInit() connect(ui->MF_UIDGroupBox, &QGroupBox::clicked, this, &MainWindow::on_GroupBox_clicked); connect(ui->MF_simGroupBox, &QGroupBox::clicked, this, &MainWindow::on_GroupBox_clicked); connect(ui->MF_sniffGroupBox, &QGroupBox::clicked, this, &MainWindow::on_GroupBox_clicked); + + connect(stopButton, &QPushButton::clicked, this, &MainWindow::on_stopButton_clicked); } void MainWindow::setStatusBar(QLabel * target, const QString & text) diff --git a/ui/mainwindow.h b/ui/mainwindow.h index 873f6d4..55c8c6f 100644 --- a/ui/mainwindow.h +++ b/ui/mainwindow.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "common/pm3process.h" #include "module/mifare.h" @@ -148,12 +149,14 @@ private slots: void on_MF_selectTrailerBox_stateChanged(int arg1); + void on_stopButton_clicked(); private: Ui::MainWindow* ui; QButtonGroup* typeBtnGroup; QLabel* connectStatusBar; QLabel* programStatusBar; QLabel* PM3VersionBar; + QPushButton* stopButton; QAction* myInfo; QAction* checkUpdate; QSettings* settings;