cleaning up source. rename doUndate() to run().

This commit is contained in:
Gerhard Hoffmann 2025-03-07 12:09:07 +01:00
parent e7d8365c62
commit 7ecd45b547
2 changed files with 60 additions and 141 deletions

View File

@ -37,48 +37,38 @@ static const QMap<QString, int> baudrateMap = {
QPluginLoader Update::pluginLoader;
hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) {
hwinf *Update::loadDCPlugin(QString const &libCA /* absolute file path */) {
hwinf *hw = nullptr;
if (plugInDir.exists()) {
QString pluginLibName(fname);
pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
QFileInfo info(pluginLibName);
if (info.exists()) {
pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
pluginLoader.setFileName(pluginLibName);
// static QPluginLoader pluginLoader(pluginLibName);
if (!pluginLoader.load()) {
qCritical() << "in directory" << plugInDir.absolutePath();
qCritical() << "cannot load plugin" << pluginLoader.fileName();
qCritical() << pluginLoader.errorString();
return nullptr;
}
QFileInfo libCAInfo(libCA);
if (libCAInfo.exists()) {
pluginLoader.setFileName(libCA);
// static QPluginLoader pluginLoader(pluginLibName);
if (!pluginLoader.load()) {
qCritical() << "cannot load plugin" << pluginLoader.fileName();
qCritical() << pluginLoader.errorString();
return nullptr;
}
qCritical() << "loadDCPlugin() plugin directory:" << plugInDir.absolutePath();
qCritical() << "loadDCPlugin() plugin file name:" << pluginLoader.fileName();
// qCritical() << "loadDCPlugin() plugin file name:" << pluginLoader.fileName();
if (!pluginLoader.isLoaded()) {
qCritical() << pluginLoader.errorString();
return nullptr;
}
QObject *plugin = pluginLoader.instance();
if (!plugin) {
qCritical() << "cannot start instance";
return nullptr;
}
if (! (hw = qobject_cast<hwinf *>(plugin))) {
qCritical() << "cannot cast plugin" << plugin << "to hwinf";
return nullptr;
}
} else {
qCritical() << pluginLibName << "does not exist";
if (!pluginLoader.isLoaded()) {
qCritical() << pluginLoader.errorString();
return nullptr;
}
QObject *plugin = pluginLoader.instance();
if (!plugin) {
qCritical() << "cannot start instance";
return nullptr;
}
if (! (hw = qobject_cast<hwinf *>(plugin))) {
qCritical() << "cannot cast plugin" << plugin << "to hwinf";
return nullptr;
}
} else {
qCritical() << "plugins directory" << plugInDir.absolutePath()
<< "does not exist";
qCritical() << libCAInfo.absoluteFilePath() << "does not exist";
return nullptr;
}
return hw;
}
@ -109,75 +99,18 @@ QString Update::dcVersion(QString const &dcBinFile) {
}
class hwapi;
Update::Update(QString customerRepository,
QString customerNrStr,
QString branchName,
QString plugInDir,
QString pluginName,
QString workingDir,
QString psaDcDir,
bool dryRun,
QObject *parent,
char const *serialInterface,
char const *baudrate)
: QObject(parent) {
#if 0
, m_hw(loadDCPlugin(QDir(plugInDir), pluginName))
, m_serialInterface(serialInterface)
, m_baudrate(baudrate)
, m_customerRepository(customerRepository)
, m_customerNrStr(customerNrStr)
, m_branchName(branchName)
, m_pluginName(pluginName)
, m_workingDir(workingDir)
, m_psaDcDir(psaDcDir)
, m_dryRun(dryRun)
, m_sys_areDCdataValid(false) {
if (!m_hw) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") m_hw == nullptr -> ca-slave plugin loaded ???";
} else {
// carun stoppen
}
#endif
m_start = QDateTime::currentDateTime();
Update::Update(QString const &dcFileName, QString const &libCA, bool debug, bool noaction)
: m_dcFileName(dcFileName)
, m_hw(loadDCPlugin(libCA))
, m_sys_areDCdataValid(false)
, m_debug(debug)
, m_noaction(noaction) {
}
Update::~Update() {
unloadDCPlugin();
}
bool Update::openSerialPort() {
// const SettingsDialog::Settings p = m_settings->settings();
m_serial->setPortName(SERIAL_PORT);
m_serial->setBaudRate(QSerialPort::Baud115200);
m_serial->setDataBits(QSerialPort::DataBits::Data8);
m_serial->setParity(QSerialPort::Parity::NoParity);
m_serial->setStopBits(QSerialPort::StopBits::OneStop);
m_serial->setFlowControl(QSerialPort::FlowControl::NoFlowControl);
if (m_serial->open(QIODevice::ReadWrite)) {
//showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
// .arg(p.name, p.stringBaudRate, p.stringDataBits,
// p.stringParity, p.stringStopBits, p.stringFlowControl));
return true;
} else {
//QMessageBox::critical(this, tr("Error"), m_serial->errorString());
//showStatusMessage(tr("Open error"));
}
return false;
}
bool Update::closeSerialPort() {
if (m_serial->isOpen()) {
m_serial->close();
return true;
}
return false;
//showStatusMessage(tr("Disconnected"));
}
Update::DownloadResult Update::sendStatus(int ret) const {
switch (ret) { // return values of dc are:
case 0: // 0: no answer by now
@ -280,8 +213,9 @@ Update::sendNextDataBlock(QByteArray const &binary, int bNum) const {
}
bool Update::startBootloader() const {
qInfo() << nextTimePoint().toUtf8().constData() << "start boot loader";
QThread::msleep(1000);
qInfo() << nextTimePoint().toUtf8().constData() << "starting bootloader ...done";
qInfo() << nextTimePoint().toUtf8().constData() << "start boot loader ...done";
return true;
#if 0
@ -304,6 +238,7 @@ bool Update::startBootloader() const {
}
bool Update::stopBootloader() const {
qInfo() << nextTimePoint().toUtf8().constData() << "stopping bootloader";
QThread::msleep(1000);
qInfo() << nextTimePoint().toUtf8().constData() << "stopping bootloader ...done";
return true;
@ -325,6 +260,7 @@ bool Update::stopBootloader() const {
}
bool Update::resetDeviceController() const {
qInfo() << nextTimePoint().toUtf8().constData() << "resetting device controller";
// TODO
// m_hw->bl_rebootDC();
@ -439,19 +375,22 @@ QByteArray Update::loadBinaryDCFile(QString const &filename) const {
// There is no problem to repeat this command until the
// bootloader is really not running anymore.
*/
bool Update::doUpdate(QString const &dcFileName) {
qInfo() << "<DC-VERSION>" << Update::dcVersion(dcFileName);
int Update::run() {
if (!m_hw) {
qCritical() << "(" << __func__ << ":" << __LINE__ << ") m_hw == nullptr -> ca-slave plugin loaded ???";
return -(int)Result::PLUGIN_LOAD_ERROR;
}
m_dcFileName = dcFileName;
m_start = QDateTime::currentDateTime();
//QString const &fToWorkOn = usbStickDetected ? QDir::cleanPath(it->trimmed())
//: QDir::cleanPath(m_customerRepository + QDir::separator() + it->trimmed());
if (m_debug) {
qInfo() << "start dc-update for" << m_dcFileName << "at" << m_start.toString(Qt::ISODate);
qInfo() << "<DC-VERSION>" << Update::dcVersion(m_dcFileName);
}
//if (!m_hw) {
// qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
// << "ERROR!!! m_hw == nullptr";
// return false;
//}
m_hw->dc_autoRequest(false);
qInfo() << "DC auto request OFF";
QByteArray ba = loadBinaryDCFile(m_dcFileName);
if (ba.size() > 0) {
@ -462,6 +401,7 @@ bool Update::doUpdate(QString const &dcFileName) {
// fill last block of data to be sent with 0xFF
ba = ba.leftJustified(m_totalBlocks*64, (char)(0xFF));
// TODO
resetDeviceController();
if (startBootloader()) {
int currentBlock = 0;
@ -499,10 +439,11 @@ bool Update::doUpdate(QString const &dcFileName) {
qCritical() << "DownloadThread::run(): last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK());
#endif
}
// TODO
stopBootloader(); // there is no harm in stopping the bootloader even
// if starting the bootloader failed
qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-SUCCESS>";
return true;
return -(int)Result::SUCCESS;
}
qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-FAILURE>";

View File

@ -17,27 +17,21 @@
#ifdef PTU5
#define SERIAL_PORT "ttymxc2"
#define BAUDRATE 115200
#else
#define SERIAL_PORT "ttyUSB0"
#define BAUDRATE 115200
#endif
class QSerialPort;
class Update : public QObject {
Q_OBJECT
QString m_dcFileName{};
hwinf *m_hw = nullptr;
char const *m_serialInterface;
char const *m_baudrate;
QString m_customerRepository;
QString m_customerNrStr;
QString m_branchName;
QString m_pluginName;
QString m_workingDir;
QString m_psaDcDir;
QString m_dcFileName;
bool m_maintenanceMode;
bool m_dryRun;
bool m_sys_areDCdataValid;
bool m_sys_areDCdataValid{};
bool m_debug;
bool m_noaction;
static QPluginLoader pluginLoader;
@ -47,34 +41,18 @@ class Update : public QObject {
return QStringLiteral("+%1s").arg(secs, 7, 'f', 2, QChar('0'));
}
bool openSerialPort();
bool closeSerialPort();
QSerialPort *m_serial;
public:
enum class DownloadResult {OK, ERROR, TIMEOUT, NOP};
enum class FileTypeJson {CONFIG=1, DEVICE=2, CASH=3, SERIAL=4, TIME=5, PRINTER=6};
enum class Result {SUCCESS=0, PLUGIN_LOAD_ERROR};
static hwinf *loadDCPlugin(QDir const &plugInDir, QString const &fn);
static hwinf *loadDCPlugin(QString const &libCA = "/usr/lib/libCAslave.so");
static bool unloadDCPlugin();
static QStringList split(QString line, QChar sep = ',');
explicit Update(QString customerRepository,
QString customerNrStr,
QString branchName,
QString plugInDir,
QString pluginName,
QString workingDir,
QString psaDcDir,
bool dryRun = false,
QObject *parent = nullptr,
char const *serialInterface = SERIAL_PORT,
char const *baudrate = "115200");
explicit Update(QString const &dcBinFile, QString const &libCA, bool debug, bool noaction);
virtual ~Update() override;
bool doUpdate(QString const &dcFileName);
int run();
static QString dcVersion(QString const &dcBinFile);
private: