From 80a8db540f68e5962ee214d68b4d71066d42d4b2 Mon Sep 17 00:00:00 2001 From: wh201906 <62299611+wh201906@users.noreply.github.com> Date: Sat, 18 Apr 2020 02:01:42 +0800 Subject: [PATCH] Optimize the read logic and complete PM3 connection state --- mainwindow.cpp | 69 +++++++++++++++++++++++++++------- mainwindow.h | 3 ++ mainwindow.ui | 61 +++++++++++++++++++++++++++--- mf_attack_hardnesteddialog.cpp | 10 ++++- mf_attack_hardnesteddialog.ui | 60 ++++++++++++++++++++++++++--- pm3process.cpp | 39 ++++++++++++++++++- pm3process.h | 8 ++++ 7 files changed, 221 insertions(+), 29 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index ae482a2..720de88 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,6 +9,7 @@ MainWindow::MainWindow(QWidget *parent) pm3 = new PM3Process; mifare = new Mifare; connect(pm3, &PM3Process::readyRead, this, &MainWindow::refresh); + connect(pm3, &PM3Process::PM3disconnected, this, &MainWindow::onPM3disconnected); connect(ui->Raw_CMDEdit, &QLineEdit::editingFinished, this, &MainWindow::sendMSG); uiInit(); } @@ -38,21 +39,31 @@ void MainWindow::on_PM3_connectButton_clicked() else { pm3->setRequiringOutput(true); - qDebug() << pm3->start(ui->PM3_pathEdit->text(), port); - while(pm3->waitForReadyRead()) - ; - QString result = pm3->getRequiredOutput(); - pm3->setRequiringOutput(false); - result = result.mid(result.indexOf("os: ")); - result = result.left(result.indexOf("\r\n")); - result = result.mid(3, result.lastIndexOf(" ") - 3); - setStatusBar(PM3VersionBar, result); + if(pm3->start(ui->PM3_pathEdit->text(), port)) + { + while(pm3->waitForReadyRead()) + ; + QString result = pm3->getRequiredOutput(); + pm3->setRequiringOutput(false); + result = result.mid(result.indexOf("os: ")); + result = result.left(result.indexOf("\r\n")); + result = result.mid(3, result.lastIndexOf(" ") - 3); + setStatusBar(PM3VersionBar, result); + setStatusBar(connectStatusBar,"Connected"); + } } } void MainWindow::on_PM3_disconnectButton_clicked() { pm3->kill(); + pm3->setSerialListener("",false); + onPM3disconnected(); +} + +void MainWindow::onPM3disconnected() +{ + setStatusBar(connectStatusBar,"Not Connected"); } // ********************************************************* @@ -108,6 +119,11 @@ void MainWindow::on_Raw_CMDHistoryWidget_itemDoubleClicked(QListWidgetItem *item // ******************** mifare ******************** +void MainWindow::on_MF_Attack_infoButton_clicked() +{ + execCMD("hf 14a info", true); +} + void MainWindow::on_MF_Attack_chkButton_clicked() { QString result = execCMDWithOutput("hf mf chk *1 ?"); @@ -252,6 +268,7 @@ void MainWindow::on_MF_RW_readAllButton_clicked() } else { + result=ui->MF_dataWidget->item(4 * i + 3, 2)->text(); result = result.replace(30, 17, "?? ?? ?? ?? ?? ??"); ui->MF_dataWidget->setItem(4 * i + 3, 2, new QTableWidgetItem(result)); } @@ -264,7 +281,7 @@ void MainWindow::on_MF_RW_readAllButton_clicked() void MainWindow::on_MF_RW_readBlockButton_clicked() { QString result = execCMDWithOutput("hf mf rdbl " - + ui->MF_RW_blockBox->text() + + ui->MF_RW_blockBox->currentText() + " " + ui->MF_RW_keyTypeBox->currentText() + " " @@ -272,7 +289,7 @@ void MainWindow::on_MF_RW_readBlockButton_clicked() if(result.indexOf("isOk:01") != -1) { result = result.mid(result.indexOf("isOk:01") + 13, 47).toUpper(); - if((ui->MF_RW_blockBox->text().toInt() + 1) % 4 == 0) + if((ui->MF_RW_blockBox->currentText().toInt() + 1) % 4 == 0) { if(ui->MF_RW_keyTypeBox->currentText() == "A") { @@ -280,20 +297,37 @@ void MainWindow::on_MF_RW_readBlockButton_clicked() { result = result.replace(i * 3, 2, ui->MF_RW_keyEdit->text().mid(i * 2, 2)); } + ui->MF_RW_dataEdit->setText(result); + QString tmpKey=result.right(18).replace(" ",""); + result = execCMDWithOutput("hf mf rdbl " + + ui->MF_RW_keyTypeBox->currentText() + + " B " + + tmpKey); + if(result.indexOf("isOk:01") == -1) + { + result= ui->MF_RW_dataEdit->text(); + result = result.replace(30, 17, "?? ?? ?? ?? ?? ??"); + ui->MF_RW_dataEdit->setText(result); + } } else { + for(int i = 0; i < 6; i++) + { + result = result.replace(30 + i * 3, 2, ui->MF_RW_keyEdit->text().mid(i * 2, 2)); + } result = result.replace(0, 18, "?? ?? ?? ?? ?? ?? "); + ui->MF_RW_dataEdit->setText(result); } } - ui->MF_RW_dataEdit->setText(result); + } } void MainWindow::on_MF_RW_writeBlockButton_clicked() { QString result = execCMDWithOutput("hf mf wrbl " - + ui->MF_RW_blockBox->text() + + ui->MF_RW_blockBox->currentText() + " " + ui->MF_RW_keyTypeBox->currentText() + " " @@ -367,6 +401,12 @@ void MainWindow::uiInit() ui->MF_keyWidget->setColumnWidth(1, 200); ui->MF_keyWidget->setColumnWidth(2, 200); + for(int i=0;i<64;i++) + { + ui->MF_RW_blockBox->addItem(QString::number(i)); + ui->MF_UID_blockBox->addItem(QString::number(i)); + } + on_Raw_moreFuncCheckBox_stateChanged(0); on_PM3_refreshPortButton_clicked(); } @@ -374,7 +414,7 @@ void MainWindow::uiInit() void MainWindow::setStatusBar(QLabel* target, const QString & text) { if(target == PM3VersionBar) - target->setText("Version:" + text); + target->setText("HW Version:" + text); else if(target == connectStatusBar) target->setText("Connecton State:" + text); else if(target == programStatusBar) @@ -411,3 +451,4 @@ bool MainWindow::MF_isKeyValid(const QString key) return true; } // *********************************************** + diff --git a/mainwindow.h b/mainwindow.h index d9eeae8..0b4a42f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -28,6 +28,7 @@ public slots: void refresh(); void setStatusBar(QLabel* target,const QString & text); void execCMD(QString cmd, bool gotoRawTab); + void onPM3disconnected(); private slots: void on_PM3_connectButton_clicked(); @@ -63,6 +64,8 @@ private slots: void on_MF_RW_writeBlockButton_clicked(); + void on_MF_Attack_infoButton_clicked(); + private: Ui::MainWindow *ui; PM3Process* pm3; diff --git a/mainwindow.ui b/mainwindow.ui index 1ddfbab..67d8ded 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -150,6 +150,19 @@ + + + + + 0 + 30 + + + + Card Info + + + @@ -401,9 +414,21 @@ - - - 63 + + + + 40 + 0 + + + + + 40 + 16777215 + + + + true @@ -430,6 +455,18 @@ + + + 35 + 0 + + + + + 35 + 16777215 + + A @@ -611,9 +648,21 @@ - - - 63 + + + + 40 + 0 + + + + + 40 + 16777215 + + + + true diff --git a/mf_attack_hardnesteddialog.cpp b/mf_attack_hardnesteddialog.cpp index 801ee78..1365bd2 100644 --- a/mf_attack_hardnesteddialog.cpp +++ b/mf_attack_hardnesteddialog.cpp @@ -6,6 +6,12 @@ MF_Attack_hardnestedDialog::MF_Attack_hardnestedDialog(QWidget *parent) : ui(new Ui::MF_Attack_hardnestedDialog) { ui->setupUi(this); + for(int i=0;i<64;i++) + { + ui->knownKeySectorBox->addItem(QString::number(i)); + ui->targetKeySectorBox->addItem(QString::number(i)); + } + } MF_Attack_hardnestedDialog::~MF_Attack_hardnestedDialog() @@ -16,13 +22,13 @@ MF_Attack_hardnestedDialog::~MF_Attack_hardnestedDialog() void MF_Attack_hardnestedDialog::on_buttonBox_accepted() { emit sendCMD("hf mf hardnested " - +ui->knownKeySectorBox->text() + +ui->knownKeySectorBox->currentText() +" " +ui->knownKeyTypeBox->currentText() +" " +ui->knownKeyBox->text() +" " - +ui->targetKeySectorBox->text() + +ui->targetKeySectorBox->currentText() +" " +ui->targetKeyTypeBox->currentText()); } diff --git a/mf_attack_hardnesteddialog.ui b/mf_attack_hardnesteddialog.ui index b9693e2..647b872 100644 --- a/mf_attack_hardnesteddialog.ui +++ b/mf_attack_hardnesteddialog.ui @@ -31,14 +31,38 @@ - - - 63 + + + + 40 + 0 + + + + + 40 + 16777215 + + + + true + + + 35 + 0 + + + + + 35 + 16777215 + + A @@ -77,14 +101,38 @@ - - - 63 + + + + 40 + 0 + + + + + 40 + 16777215 + + + + true + + + 35 + 0 + + + + + 35 + 16777215 + + A diff --git a/pm3process.cpp b/pm3process.cpp index 0a498cf..f06cbac 100644 --- a/pm3process.cpp +++ b/pm3process.cpp @@ -5,6 +5,10 @@ PM3Process::PM3Process(QObject* parent): QProcess(parent) setProcessChannelMode(PM3Process::MergedChannels); isRequiringOutput=false; requiredOutput=new QString(); + serialListener=new QTimer(this); + serialListener->setInterval(1000); + serialListener->setTimerType(Qt::VeryCoarseTimer); + connect(serialListener,&QTimer::timeout,this,&PM3Process::onTimeout); } QStringList PM3Process::findPort() @@ -13,7 +17,9 @@ QStringList PM3Process::findPort() QStringList retList; foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { + qDebug()<start(); + } + else + { + serialListener->stop(); + delete portInfo; + } +} + +void PM3Process::onTimeout() +{ + qDebug()<isBusy(); + if(!portInfo->isBusy()) + { + emit PM3disconnected(); + setSerialListener("",false); + } +} diff --git a/pm3process.h b/pm3process.h index 6184548..aa9f4ee 100644 --- a/pm3process.h +++ b/pm3process.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -18,9 +19,16 @@ public: void setRequiringOutput(bool st); QString getRequiredOutput(); bool waitForReadyRead(int msecs = 2000); + void setSerialListener(const QString &name, bool state); +private slots: + void onTimeout(); private: bool isRequiringOutput; QString* requiredOutput; + QTimer* serialListener; + QSerialPortInfo* portInfo; +signals: + void PM3disconnected(); }; #endif // PM3PROCESS_H