From 35d40e3b3c84472157f9542feb5a800af89d58da Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 26 May 2023 13:03:38 +0200 Subject: [PATCH] Add handling of update-script --- update.cpp | 34 ++++++++++++++++++++++++++++++++ update.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 update.h diff --git a/update.cpp b/update.cpp index fe025c0..e0e5bf2 100644 --- a/update.cpp +++ b/update.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "interfaces.h" #include "DCPlugin/include/hwapi.h" @@ -13,6 +14,7 @@ #include #include #include +#include #define COLUMN_REQUEST (0) #define COLUMN_NAME (1) @@ -26,6 +28,7 @@ void ScopedPointerCustomDeleter::cleanup(Update *update) { } Update::Update(QString update_ctrl_file, + QString workingDir, QObject *parent, hwinf *hw, char const *serialInterface, @@ -36,9 +39,12 @@ Update::Update(QString update_ctrl_file, , m_baudrate(baudrate) , m_update_ctrl_file(update_ctrl_file) , m_update_ctrl_file_copy(update_ctrl_file + ".copy") + , m_workingDir(workingDir) , m_init(true) , m_delete(hw == nullptr) { + execUpdateScript(); + if (!m_update_ctrl_file.exists()) { qCritical() << "Update-file" << m_update_ctrl_file.fileName() << "does not exist"; @@ -64,6 +70,34 @@ Update::Update(QString update_ctrl_file, Update::~Update() { } +bool Update::execUpdateScript() { + // path of update-script 'update_psa' + QString update_psa("/opt/app/tools/atbupdate/update_psa --wdir "); + update_psa += m_workingDir; + + //QStringList const params(QStringList() << "-c" << update_psa); + + QScopedPointer 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; +} + bool Update::updateBinary(char const *fileToSendToDC) { return m_hw->dc_updateDC(fileToSendToDC, m_baudrate, m_serialInterface); } diff --git a/update.h b/update.h new file mode 100644 index 0000000..83ee577 --- /dev/null +++ b/update.h @@ -0,0 +1,57 @@ +#ifndef UPDATE_H_INCLUDED +#define UPDATE_H_INCLUDED + +#include +#include +#include + +#include "interfaces.h" +#include "DCPlugin/include/hwapi.h" + +#ifdef PTU5 +#define SERIAL_PORT "ttymxc2" +#else +#define SERIAL_PORT "ttyUSB0" +#endif + +class Update; +struct ScopedPointerCustomDeleter { + static void cleanup(Update *pointer); +}; + +// TODO: check hardware compatibility +// TODO: opkg commandos + +class Update : public QObject { + Q_OBJECT + + QScopedPointer m_hw; + char const *m_serialInterface; + char const *m_baudrate; + QFile m_update_ctrl_file; + QFile m_update_ctrl_file_copy; + QString m_workingDir; + + bool m_init; + + bool updateBinary(char const *fileToSendToDC); + bool updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC); + bool finishUpdate(bool finish); + QStringList getOpenLines(); + QStringList split(QString line); + static constexpr QChar SEPARATOR = QChar(','); + + bool execUpdateScript(); +public: + explicit Update(QString update_ctrl_file, + QString workingDir = ".", + QObject *parent = nullptr, + hwinf *hw = nullptr, + char const *serialInterface = SERIAL_PORT, + char const *baudrate = "115200"); + virtual ~Update() override; + bool doUpdate(); + + bool const m_delete; +}; +#endif // UPDATE_H_INCLUDED