ATBUpdateTool/update.cpp

269 lines
8.8 KiB
C++
Raw Normal View History

2023-05-22 16:06:52 +02:00
#include "update.h"
#include <QCoreApplication>
#include <QApplication>
#include <QFile>
#include <QTemporaryFile>
#include <QDebug>
#include <QTextStream>
2023-05-26 13:03:38 +02:00
#include <QRegularExpression>
2023-05-22 16:06:52 +02:00
#include "interfaces.h"
#include "DCPlugin/include/hwapi.h"
#include <QSharedMemory>
#include <QScopedPointer>
#include <QProcess>
2023-05-26 13:03:38 +02:00
#include <QDir>
2023-05-22 16:06:52 +02:00
#define COLUMN_REQUEST (0)
#define COLUMN_NAME (1)
#define COLUMN_DATE_TIME (2)
#define COLUMN_RESULT (3)
void ScopedPointerCustomDeleter::cleanup(Update *update) {
if (update->m_delete) {
delete update;
}
}
Update::Update(QString update_ctrl_file,
2023-05-26 13:03:38 +02:00
QString workingDir,
2023-05-22 16:06:52 +02:00
QObject *parent,
hwinf *hw,
char const *serialInterface,
char const *baudrate)
: QObject(parent)
, m_hw(hw != nullptr ? hw : new hwapi())
, m_serialInterface(serialInterface)
, m_baudrate(baudrate)
, m_update_ctrl_file(update_ctrl_file)
, m_update_ctrl_file_copy(update_ctrl_file + ".copy")
2023-05-26 13:03:38 +02:00
, m_workingDir(workingDir)
2023-05-22 16:06:52 +02:00
, m_init(true)
, m_delete(hw == nullptr) {
// qCritical() << "workingDir" << m_workingDir;
2023-05-26 13:03:38 +02:00
execUpdateScript();
2023-05-22 16:06:52 +02:00
if (!m_update_ctrl_file.exists()) {
qCritical() << "Update-file" << m_update_ctrl_file.fileName()
<< "does not exist";
m_init = false;
}
if (!m_update_ctrl_file_copy.exists()) {
qCritical() << "Update-file-copy" << m_update_ctrl_file_copy.fileName()
<< "does not exist";
m_init = false;
}
if (!m_update_ctrl_file.open(QIODevice::ReadWrite | QIODevice::Text)) {
qCritical() << "can not open " << m_update_ctrl_file.fileName();
m_init = false;
}
qDebug() << "Opened" << m_update_ctrl_file.fileName();
if (!m_update_ctrl_file_copy.open(QIODevice::ReadWrite | QIODevice::Text)) {
qCritical() << "can not open " << m_update_ctrl_file_copy.fileName();
m_init = false;
}
qDebug() << "Opened" << m_update_ctrl_file_copy.fileName();
}
Update::~Update() {
}
2023-05-26 13:03:38 +02:00
bool Update::execUpdateScript() {
// path of update-script 'update_psa'
QString update_psa("/opt/app/tools/atbupdate/update_psa -m --wdir ");
2023-05-26 13:03:38 +02:00
update_psa += m_workingDir;
qCritical() << "update_psa: " << update_psa;
2023-05-26 13:03:38 +02:00
//QStringList const params(QStringList() << "-c" << update_psa);
QScopedPointer<QProcess> p(new QProcess(this));
p->setProcessChannelMode(QProcess::MergedChannels);
p->start(update_psa);
if (p->waitForStarted(1000)) {
if (p->state() == QProcess::ProcessState::Running) {
if (p->waitForFinished(60000)) {
QString output = p->readAllStandardOutput().toStdString().c_str();
QStringList lst = output.split('\n');
for (int i = 0; i < lst.size(); ++i) {
qDebug() << lst[i];
}
qInfo() << "EXECUTED" << update_psa;
return ((p->exitStatus() == QProcess::NormalExit)
&& (p->exitCode() == 0));
}
}
}
return false;
}
2023-05-22 16:06:52 +02:00
bool Update::updateBinary(char const *fileToSendToDC) {
// return true; // debug
2023-05-22 16:06:52 +02:00
return m_hw->dc_updateDC(fileToSendToDC, m_baudrate, m_serialInterface);
}
bool Update::updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC) {
// return true; // debug
2023-05-22 16:06:52 +02:00
QVector<int> printTemplates{ nrOfTemplate };
QVector<QString> filesToSend{ fileToSendToDC };
return m_hw->dc_updatePrinterTemplate(hwapi::FileTypeJson::PRINTER,
printTemplates, filesToSend,
QString(m_baudrate),
QString(m_serialInterface));
}
QStringList Update::getOpenLines() {
QStringList openLines;
QTextStream in(&m_update_ctrl_file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.startsWith("DONE")) {
m_update_ctrl_file_copy.write(line.toUtf8().constData());
m_update_ctrl_file_copy.write("\n");
} else {
openLines << line;
}
}
return openLines;
}
QStringList Update::split(QString line, QChar sep) {
2023-05-22 16:06:52 +02:00
QStringList lst;
QString next;
int start = 0, end;
while ((end = line.indexOf(sep, start)) != -1) {
2023-05-22 16:06:52 +02:00
next = line.mid(start, end - start).trimmed();
lst << next;
start = end + 1;
}
next = line.mid(start, end - start).trimmed();
lst << next;
return lst;
}
bool Update::doUpdate() {
/*
The file referred to by 'update_data' has the following structure for
each line:
# ======================================================================
# REQUEST | NAME | DATE | RESULT
# ======================================================================
# where
#
# STATUS: DOWNLOAD, EXECUTE or DONE
# NAME : If starting with 'opkg' it is an opkg-command to be executed.
# Otherwise its the name of a file which has to be updated.
# DATE : 0000-00-00T00:00:00
# RESULT: SUCCESS or ERROR (possibly with description)
#
*/
// qCritical() << "Device Controller SW-version" << m_hw->dc_getSWversion();
2023-05-22 16:06:52 +02:00
if (!m_init) {
return false;
}
QStringList openLines = getOpenLines();
QList<QString>::const_iterator it;
for (it = openLines.cbegin(); it != openLines.cend(); ++it) {
bool res = false;
QString line = (*it).trimmed();
if (line.size() == 0 || line.startsWith(QChar('#'))) {
continue;
}
QStringList lst = split(line.trimmed());
if (lst.size() != 4) {
qCritical() << "Parsing error for" << m_update_ctrl_file.fileName();
return false;
}
QString const &request = lst[COLUMN_REQUEST];
QString const &name = lst[COLUMN_NAME];
// QString const &datetime = lst[COLUMN_DATE_TIME];
QString const &result = lst[COLUMN_RESULT];
if ((!request.contains("DOWNLOAD") && !request.contains("EXECUTE")) ||
!result.contains("N/A")) {
qCritical() << "Parsing error for" << m_update_ctrl_file.fileName();
return false;
}
if (name.contains("dc2c") && name.endsWith(".bin")) {
if ((res = updateBinary(name.toStdString().c_str())) == true) {
qInfo() << "Downloaded" << name;
}
} else
if (name.contains("DC2C_print") && name.endsWith(".json")) {
int i = name.indexOf("DC2C_print");
int templateIdx = name.mid(i).midRef(10, 2).toInt();
if ((res = updatePrinterConf(templateIdx, name.toStdString().c_str())) == true) {
qInfo() << "Downloaded" << name;
}
} else
if (name.contains("opkg")) {
int i = name.indexOf("opkg ");
QString rest = name.mid(i+5).trimmed();
QScopedPointer<QProcess> p(new QProcess(this));
p->setProcessChannelMode(QProcess::MergedChannels);
p->start("opkg", QStringList() << rest);
if (p->waitForStarted(1000)) {
if (p->state() == QProcess::ProcessState::Running) {
if (p->waitForFinished(10000)) {
QString output = p->readAllStandardOutput();
QStringList outputLst = split(output, QChar('\n'));
qDebug() << outputLst;
2023-05-22 16:06:52 +02:00
res = true;
qInfo() << "EXECUTED" << name;
}
}
}
} else {
// TODO
}
char buf[80];
int const bytesWritten =
snprintf(buf, sizeof(buf)-1, "DONE, %*.*s, %*.*s, %*.*s\n",
35, 35, name.toStdString().c_str(),
20, 20, QDateTime::currentDateTime().toString(Qt::ISODate).toStdString().c_str(),
10, 10, (res == true) ? "SUCCESS" : "ERROR");
if (bytesWritten < 80) {
buf[bytesWritten] = '\0';
}
m_update_ctrl_file_copy.write(buf);
} // for (it = openLines.cbegin(); it != openLines.end(); ++it) {
return finishUpdate(openLines.size() > 0);
}
bool Update::finishUpdate(bool swapCtrlFiles) {
if (swapCtrlFiles) {
m_update_ctrl_file.close();
m_update_ctrl_file_copy.close();
QString const &fn = m_update_ctrl_file.fileName();
QString const &fn_tmp = m_update_ctrl_file.fileName() + ".tmp";
QString const &fn_copy = m_update_ctrl_file_copy.fileName();
QFile tmp(fn_tmp);
if (tmp.exists()) {
tmp.remove();
}
if (m_update_ctrl_file.rename(fn_tmp)) {
if (m_update_ctrl_file_copy.rename(fn)) {
return m_update_ctrl_file.rename(fn_copy);
}
}
return false;
}
return true;
}