Proxmark3GUI/common/util.cpp

88 lines
2.1 KiB
C++
Raw Normal View History

2020-04-22 16:42:58 +08:00
#include "util.h"
2020-08-05 19:08:02 +08:00
Util::Util(QObject *parent) : QObject(parent)
2020-04-22 16:42:58 +08:00
{
2020-04-22 23:13:00 +08:00
isRequiringOutput = false;
requiredOutput = new QString();
timeStamp = QTime::currentTime();
2020-08-05 19:08:02 +08:00
this->clientType = CLIENTTYPE_OFFICIAL;
qRegisterMetaType<Util::ClientType>("Util::ClientType");
2020-04-22 21:14:33 +08:00
}
2020-04-22 16:42:58 +08:00
2020-08-13 09:30:47 +08:00
void Util::processOutput(const QString& output)
2020-04-22 21:14:33 +08:00
{
2020-04-23 17:50:20 +08:00
// qDebug() << "Util::processOutput:" << output;
2020-04-22 21:14:33 +08:00
if(isRequiringOutput)
{
requiredOutput->append(output);
2020-04-22 23:13:00 +08:00
timeStamp = QTime::currentTime();
2020-04-22 21:14:33 +08:00
}
emit refreshOutput(output);
2020-04-22 16:42:58 +08:00
}
2020-08-13 09:30:47 +08:00
void Util::execCMD(const QString& cmd)
2020-04-22 16:42:58 +08:00
{
2020-04-22 21:14:33 +08:00
qDebug() << cmd;
if(isRunning)
emit write(cmd + "\r\n");
2020-04-22 21:14:33 +08:00
}
2020-04-22 16:42:58 +08:00
2020-08-13 09:30:47 +08:00
QString Util::execCMDWithOutput(const QString& cmd, ReturnTrigger trigger)
2020-04-22 21:14:33 +08:00
{
bool isResultFound = false;
QRegularExpression re;
re.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
if(!isRunning)
return "";
2020-04-22 23:13:00 +08:00
QTime currTime = QTime::currentTime();
QTime targetTime = QTime::currentTime().addMSecs(trigger.waitTime);
2020-04-22 23:13:00 +08:00
isRequiringOutput = true;
2020-04-22 21:14:33 +08:00
requiredOutput->clear();
execCMD(cmd);
2020-08-01 21:30:55 +08:00
while(QTime::currentTime() < targetTime)
2020-04-22 21:14:33 +08:00
{
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;
}
2020-04-22 23:13:00 +08:00
if(timeStamp > currTime)
2020-04-22 21:14:33 +08:00
{
2020-04-22 23:13:00 +08:00
currTime = timeStamp;
targetTime = timeStamp.addMSecs(trigger.waitTime);
2020-04-22 21:14:33 +08:00
}
}
2020-04-22 23:13:00 +08:00
isRequiringOutput = false;
2020-04-22 21:14:33 +08:00
return *requiredOutput;
}
void Util::delay(unsigned int msec)
{
QTime timer = QTime::currentTime().addMSecs(msec);
2020-08-01 21:30:55 +08:00
while(QTime::currentTime() < timer)
2020-04-22 21:14:33 +08:00
QApplication::processEvents(QEventLoop::AllEvents, 100);
2020-04-22 16:42:58 +08:00
}
2020-08-01 21:30:55 +08:00
Util::ClientType Util::getClientType()
{
return this->clientType;
}
2020-08-05 19:08:02 +08:00
void Util::setClientType(Util::ClientType clientType)
{
this->clientType = clientType;
}
void Util::setRunningState(bool st)
{
this->isRunning = st;
}