From 12a0837c503e3941641640a8ad550811be6f5a82 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Sun, 5 Sep 2021 21:35:03 +0800 Subject: [PATCH] Fix a bug Trying to fix #22 Open client with QProcess::Text for proper newline character(s) On Raspbian, the isBusy() function will always return false, even the serial port is actually connected. --- common/pm3process.cpp | 24 +++++++++++++++--------- common/util.cpp | 2 +- module/lf.cpp | 4 ++-- module/mifare.cpp | 6 +++--- ui/mainwindow.cpp | 8 ++++---- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/common/pm3process.cpp b/common/pm3process.cpp index 5417e0f..c4df2af 100644 --- a/common/pm3process.cpp +++ b/common/pm3process.cpp @@ -26,7 +26,7 @@ void PM3Process::connectPM3(const QString& path, const QStringList args) currArgs = args; // using "-f" option to make the client output flushed after every print. - start(path, args, QProcess::Unbuffered | QProcess::ReadWrite); + start(path, args, QProcess::Unbuffered | QProcess::ReadWrite | QProcess::Text); if(waitForStarted(10000)) { waitForReadyRead(10000); @@ -36,11 +36,13 @@ void PM3Process::connectPM3(const QString& path, const QStringList args) { clientType = Util::CLIENTTYPE_ICEMAN; setRequiringOutput(true); - write("hw version\r\n"); - for(int i = 0; i < 10; i++) + write("hw version\n"); + for(int i = 0; i < 50; i++) { waitForReadyRead(200); result += *requiredOutput; + if(result.indexOf("os: ") != -1) + break; } setRequiringOutput(false); } @@ -52,8 +54,8 @@ void PM3Process::connectPM3(const QString& path, const QStringList args) { emit changeClientType(clientType); result = result.mid(result.indexOf("os: ")); - result = result.left(result.indexOf("\r\n")); - result = result.mid(3, result.lastIndexOf(" ") - 3); + result = result.left(result.indexOf("\n")); + result = result.mid(4, result.indexOf(" ", 4) - 4); emit PM3StatedChanged(true, result); } else @@ -105,11 +107,15 @@ void PM3Process::setSerialListener(bool state) void PM3Process::onTimeout() //when the proxmark3 client is unexpectedly terminated or the PM3 hardware is removed, the isBusy() will return false(only tested on Windows); { +// isBusy() is a deprecated function. +// It will always return false on Raspbian. +// SerialListener need to be removed. +// // qDebug()<isBusy(); - if(!portInfo->isBusy()) - { - killPM3(); - } +// if(!portInfo->isBusy()) +// { +// killPM3(); +// } } void PM3Process::testThread() diff --git a/common/util.cpp b/common/util.cpp index 573c5f9..4fca7cb 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -31,7 +31,7 @@ void Util::execCMD(const QString& cmd) { qDebug() << "executing: " << cmd; if(isRunning) - emit write(cmd + "\r\n"); + emit write(cmd + "\n"); } QString Util::execCMDWithOutput(const QString& cmd, ReturnTrigger trigger) diff --git a/module/lf.cpp b/module/lf.cpp index 075bf54..7878f27 100644 --- a/module/lf.cpp +++ b/module/lf.cpp @@ -70,10 +70,10 @@ void LF::getConfig() result = util->execCMDWithOutput("hw status", 400); // not all output from "hw status will be processed". result = result.right(result.length() - result.indexOf("LF Sampling config")); offset = result.indexOf("samples to skip"); - offset = result.indexOf("\r\n", offset); + offset = result.indexOf("\n", offset); result = result.mid(0, offset + 2); qDebug() << "LF CONFIG GET\n" << result; - resultList = result.split("\r\n"); + resultList = result.split("\n"); for(int i = 0; i < resultList.length(); i++) { for(int j = 0; j < symbolList.length(); j++) diff --git a/module/mifare.cpp b/module/mifare.cpp index 91a0169..42bd333 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -794,7 +794,7 @@ void Mifare::setParameterC() QMessageBox::information(parent, tr("Info"), tr("Failed to read card.")); else { - result.replace("\r\n", ""); + result.replace("\n", ""); result.replace(QRegularExpression("\\[.\\]"), ""); result.replace("UID", ""); result.replace("ATQA", ""); @@ -1071,7 +1071,7 @@ bool Mifare::data_loadDataFile(const QString& filename) else { QString tmp = buff.left(cardType.block_size * 34); - QStringList tmpList = tmp.split("\r\n"); + QStringList tmpList = tmp.split("\n"); for(int i = 0; i < cardType.block_size; i++) { dataList->replace(i, tmpList[i].toUpper()); @@ -1174,7 +1174,7 @@ bool Mifare::data_saveDataFile(const QString& filename, bool isBin) for(int i = 0; i < cardType.block_size; i++) { buff += dataList->at(i); - buff += "\r\n"; + buff += "\n"; } } bool ret = file.write(buff) != -1; diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index b2a928a..13f5fae 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -126,11 +126,11 @@ void MainWindow::on_PM3_connectButton_clicked() // use the shell session to keep the environment then read it #ifdef Q_OS_WIN // cmd /c "">>nul && set - envSetProcess.start("cmd"); - envSetProcess.write(QString("\"" + envScriptPath.absoluteFilePath() + "\">>nul\r\n").toLatin1()); + envSetProcess.start("cmd", {}, QProcess::Unbuffered | QProcess::ReadWrite | QProcess::Text); + envSetProcess.write(QString("\"" + envScriptPath.absoluteFilePath() + "\">>nul\n").toLatin1()); envSetProcess.waitForReadyRead(10000); envSetProcess.readAll(); - envSetProcess.write("set\r\n"); + envSetProcess.write("set\n"); #else // need implementation(or test if space works) // sh -c '. "">>/dev/null && env' @@ -138,7 +138,7 @@ void MainWindow::on_PM3_connectButton_clicked() #endif envSetProcess.waitForReadyRead(10000); QString envSetResult = QString(envSetProcess.readAll()); - clientEnv = envSetResult.split(QRegExp("[\r\n]{1,2}"), QString::SkipEmptyParts); + clientEnv = envSetResult.split("\n", QString::SkipEmptyParts); if(clientEnv.size() > 2) // the first element is "set" and the last element is the current path { clientEnv.removeFirst();