Refactor project structure

This commit is contained in:
wh201906
2020-04-22 16:42:58 +08:00
parent a6a699d33c
commit ed1e9cb1d6
15 changed files with 55 additions and 27 deletions
+112
View File
@@ -0,0 +1,112 @@
#include "pm3process.h"
PM3Process::PM3Process(QThread* thread, QObject* parent): QProcess(parent)
{
moveToThread(thread);
setProcessChannelMode(PM3Process::MergedChannels);
isRequiringOutput=false;
requiredOutput=new QString();
serialListener=new QTimer(); // if using new QTimer(this), the debug output will show "Cannot create children for a parent that is in a different thread."
serialListener->moveToThread(this->thread());// I've tried many ways to creat a QTimer instance, but all of the instances are in the main thread(UI thread), so I have to move it manually
serialListener->setInterval(1000);
serialListener->setTimerType(Qt::VeryCoarseTimer);
connect(serialListener,&QTimer::timeout,this,&PM3Process::onTimeout);
}
void PM3Process::connectPM3(const QString path, const QString port)
{
setRequiringOutput(true);
// using "-f" option to make the client output flushed after every print.
start(path, QStringList()<<port<<"-f",QProcess::Unbuffered|QProcess::ReadWrite);
if(waitForStarted(10000))
{
while(waitForReadyRead(1000))
;
setRequiringOutput(false);
QString result = *requiredOutput;
if(result.indexOf("os: ")!=-1)// make sure the PM3 is connected
{
result = result.mid(result.indexOf("os: "));
result = result.left(result.indexOf("\r\n"));
result = result.mid(3, result.lastIndexOf(" ") - 3);
emit PM3StatedChanged(true,result);
setSerialListener(port,true);
}
else
kill();
}
}
void PM3Process::setRequiringOutput(bool st)
{
isRequiringOutput=st;
if(isRequiringOutput)
requiredOutput->clear();
}
QByteArray PM3Process::readLine(qint64 maxlen)
{
QByteArray buff;
buff=QProcess::readLine(maxlen);
if(isRequiringOutput)
requiredOutput->append(buff);
return buff;
}
QString PM3Process::getRequiredOutput()
{
return *requiredOutput;
}
bool PM3Process::waitForReadyRead(int msecs)
{
return QProcess::waitForReadyRead(msecs);
}
void PM3Process::setSerialListener(const QString& name,bool state)
{
if(state)
{
portInfo=new QSerialPortInfo(name);
serialListener->start();
qDebug()<<serialListener->thread();
}
else
{
serialListener->stop();
delete portInfo;
}
}
void PM3Process::onTimeout() //when the proxmark3 client is unexpectedly terminated or the PM3 hardware is removed, the isBusy() will return false(tested on Windows);
{
qDebug()<<portInfo->isBusy();
if(!portInfo->isBusy())
{
kill();
emit PM3StatedChanged(false);
setSerialListener("",false);
}
}
void PM3Process::testThread()
{
qDebug()<<"PM3:"<<QThread::currentThread();
}
qint64 PM3Process::write(QString data)
{
return QProcess::write(data.toLatin1());
}
void PM3Process::onReadyRead()
{
QString btay = readLine();
// while(btay != "")
// {
// qDebug() << btay;
// ui->Raw_outputEdit->insertPlainText(btay);
// btay = pm3->readLine();
// }
// ui->Raw_outputEdit->moveCursor(QTextCursor::End);
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef PM3PROCESS_H
#define PM3PROCESS_H
#include <QProcess>
#include <QThread>
#include <QString>
#include <QDebug>
#include <QTimer>
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort>
class PM3Process : public QProcess
{
Q_OBJECT
public:
explicit PM3Process(QThread* thread, QObject* parent=nullptr);
QByteArray readLine(qint64 maxlen = 0);
QString getRequiredOutput();
bool waitForReadyRead(int msecs = 2000);
void testThread();
public slots:
void setRequiringOutput(bool st);
void connectPM3(const QString path, const QString port);
void setSerialListener(const QString &name, bool state);
qint64 write(QString data);
private slots:
void onTimeout();
void onReadyRead();
private:
bool isRequiringOutput;
QString* requiredOutput;
QTimer* serialListener;
QSerialPortInfo* portInfo;
signals:
void PM3StatedChanged(bool st, QString info="");
void newOutput(QString output);
};
#endif // PM3PROCESS_H
+11
View File
@@ -0,0 +1,11 @@
#include "util.h"
Util::Util(QObject *parent) : QObject(parent)
{
}
void Util::processOutput()
{
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef UTIL_H
#define UTIL_H
#include <QObject>
class Util : public QObject
{
Q_OBJECT
public:
explicit Util(QObject *parent = nullptr);
signals:
private slots:
void processOutput();
};
#endif // UTIL_H