From a07893ddab6d1782bf847432ca2ff4c0a71667c6 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Sat, 9 Sep 2023 14:59:05 +0200 Subject: [PATCH] doUpdate() and updateDC(): communicate with main window to enter bootloader (i.e. to prepare device for download of device controller firmware), but do not perform an actual download at the moment. --- update.cpp | 256 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 175 insertions(+), 81 deletions(-) diff --git a/update.cpp b/update.cpp index 54d74dc..342b21a 100644 --- a/update.cpp +++ b/update.cpp @@ -491,28 +491,88 @@ bool Update::updateBinary(char const *fileToSendToDC) { } bool Update::updateDC(QString bFile) const { - qDebug() << "updating dc..."; - qDebug() << "updating dc: file to send" << bFile; - if (!resetDeviceController()) { - return false; + qDebug() << "IN UPDATEDC: UPDATING DC: FILE TO SEND" << bFile; + + m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::NONE); + + QApplication::postEvent( // step 1: reset device controller + m_worker->mainWindow(), + new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::DC_REBOOT, 1)); + QThread::sleep(1); + + for (int i=1; i <= MainWindow::BL_START_COUNT; ++i) { + QApplication::postEvent( // step 2: start bootloader (5x) + m_worker->mainWindow(), + new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_START, i)); + QThread::sleep(1); } - if (!startBootloader()) { - // even when start seems to fail, stopping the boot loader does not harm - stopBootloader(); + + int const cntLimit = 100; // wait until its for sure that bl_startBL() + int cnt = 0; // has been excuted + while (++cnt < cntLimit && + m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_START) { + // wait until all bl_startBL() are done + QThread::msleep(200); + } + + if (cnt == cntLimit) { + // start events not received ??? + Utils::printCriticalErrorMsg("BL_START EVENT NOT RECEIVED AFTER 20 SECS"); return false; } - if (!downloadBinaryToDC(bFile)) { - stopBootloader(); - qCritical() << "updating dc: " << bFile << "...FAILED"; + m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::BL_CHECK); + + for (int i=1; i <= MainWindow::BL_IS_UP_COUNT; ++i) { + QApplication::postEvent(m_worker->mainWindow(), new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_CHECK, i)); + QThread::sleep(1); + QApplication::postEvent(m_worker->mainWindow(), new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_IS_UP, i)); + if (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_UP) { + break; + } + QThread::sleep(1); + } + + cnt = 0; + while (++cnt < cntLimit && + m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_UP) { + // wait until all bl_startBL() are done + QThread::msleep(200); + } + + if (cnt == cntLimit) { + // really not up + Utils::printCriticalErrorMsg("BL_IS_UP EVENT NOT RECEIVED AFTER 20 SECS"); + stopBootloader(); // try to stop bootloader whichhas been already started return false; } - qInfo() << "updating dc: " << bFile << "...OK"; - stopBootloader(); - //resetDeviceController(); + if (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_UP) { + // bootloader MUST be running to download device-controller +#if 0 + if (!downloadBinaryToDC(bFile)) { + Utils::printCriticalErrorMsg( + QString("UPDATING DC: ") + bFile + " ...DOWNLOAD FAILED"); + } +#endif - QThread::sleep(3); + } else { + Utils::printCriticalErrorMsg( + QString("UPDATING DC: ") + bFile + " BOOT LOADER NOT RUNNING -> NO DOWNLOAD (" + + QThread::currentThread()->objectName() + ")"); + return false; + } + + // do this unconditionally, even if bootloader is not running at all -> + // the controller possibly tells us nonsense. + if (!stopBootloader()) { + Utils::printCriticalErrorMsg( + QString("UPDATING DC: ") + bFile + " BOOT LOADER STILL RUNNING (" + + QThread::currentThread()->objectName() + ")"); + return false; + } + + Utils::printInfoMsg(QString("UPDATING DC: ") + bFile + " ...OK"); return true; } @@ -634,25 +694,73 @@ void Update::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) { disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardError())); } +QStringList Update::getDcSoftAndHardWareVersion() { + m_hw->dc_autoRequest(true); + QThread::sleep(1); // make sure the timer-slots are active + + for (int i=0; i < 3; ++i) { // send explicit reuests to get + // current SW/HW-versions + m_hw->request_DC2_SWversion(); + m_hw->request_DC2_HWversion(); + QThread::sleep(1); + } + + QString const &hwVersion = m_hw->dc_getHWversion().toLower().trimmed(); + QString const &swVersion = m_hw->dc_getSWversion().toLower().trimmed(); + + m_hw->dc_autoRequest(false); + QThread::sleep(1); // make sure the timer-slots are inactive + + if (!hwVersion.isEmpty() && !swVersion.isEmpty()) { + return QStringList() << hwVersion << swVersion; + } + + return QStringList() << "DC HW-version not available" + << "DC SW-version not available"; +} + bool Update::doUpdate(int &displayIndex, QStringList const &filesToWorkOn) { - // - // ACHTUNG !!! - // - return true; - bool serialOpened = false; - bool serialOpen = false; - if (!serialOpen) { - if (!isSerialOpen()) { // open serial only if not already open - if ((serialOpened = openSerial(baudrateMap.value(m_baudrate), m_baudrate, m_serialInterface)) == false) { - qCritical() << "CANNOT OPEN" << m_serialInterface << "(BAUDRATE=" - << m_baudrate << ")"; - return false; - } + QString const &parentName = Utils::getParentName(); + + if (parentName == "ATBQT" || parentName == "systemd") { + // the tool was not called during 'service' ot during an automatic + // update procedure. and it was called explicitly with libCAmaster.so + if (m_pluginName.contains("master", Qt::CaseInsensitive)) { + Utils::printCriticalErrorMsg(parentName + + " IS MASTER, BUT ATB-UPDATE-TOOL CALLED WITH libCAmaster.so"); + return false; } - serialOpen = true; - qInfo() << "SERIAL OPEN" << m_serialInterface << "(BAUDRATE=" << m_baudrate << ")"; + } else + if (Utils::isATBQTRunning()) { // manual testing + if (m_pluginName.contains("master", Qt::CaseInsensitive)) { + Utils::printCriticalErrorMsg( + "ATBQT IS MASTER, BUT ATB-UPDATE-TOOL CALLED WITH libCAmaster.so"); + return false; + } + } else { + if (m_pluginName.contains("slave", Qt::CaseInsensitive)) { + Utils::printCriticalErrorMsg( + "ATB-UPDATE-TOOL CALLED WITH libCAslave.so ALTHOUGH MASTER"); + return false; + } + + if ((serialOpened = openSerial(baudrateMap.value(m_baudrate), + m_baudrate, + m_serialInterface)) == false) { + Utils::printCriticalErrorMsg( + QString("CANNOT OPEN ") + + m_serialInterface + + "( BAUDRATE=" + m_baudrate + ")"); + return false; + } + + m_hw->dc_autoRequest(false); + + Utils::printInfoMsg( + QString("SERIAL OPEN ") + m_serialInterface + + " (BAUDRATE=" + m_baudrate + ")"); } bool res = false; @@ -662,67 +770,49 @@ bool Update::doUpdate(int &displayIndex, QStringList const &filesToWorkOn) { QString fToWorkOn = (*it).trimmed(); fToWorkOn = QDir::cleanPath(m_customerRepository + QDir::separator() + fToWorkOn); - static const QRegularExpression version("^.*dc2c[.][0-9][0-9][.][0-9][0-9][.]bin.*$"); + static const QRegularExpression version("^.*dc2c[.][0-9]{1,2}[.][0-9]{1,2}[.]bin.*$"); if (fToWorkOn.contains(version)) { - - qInfo() << QString(80, '*'); - qInfo() << "DO-UPDATE FILE-TO-WORK-ON" << fToWorkOn; - qInfo() << QString(80, '*'); - - for (int i=0; i < 3; ++i) { // send explicit reuests to get - // current SW/HW-versions - m_hw->request_DC2_SWversion(); - m_hw->request_DC2_HWversion(); - QThread::sleep(1); - } - - QString const hwVersion = m_hw->dc_getHWversion().toLower(); - QString const fwVersion = m_hw->dc_getSWversion().toLower(); - - qInfo() << "current dc-hardware-version" << hwVersion; - qInfo() << "current dc-firmware-version" << fwVersion; + Utils::printInfoMsg("DO-UPDATE FILE-TO-WORK-ON " + fToWorkOn); QFile fn(fToWorkOn); QFileInfo finfo(fn); if (!fn.exists()) { // check for broken link - qCritical() << QString(80, '*'); - qCritical() << "FILE-TO-WORK-ON" << fn << "DOES NOT EXIST"; - qCritical() << QString(80, '*'); + Utils::printCriticalErrorMsg("DO-UPDATE FILE-TO-WORK-ON " + + fToWorkOn + " DOES NOT EXIST"); res = false; } else { - if (false) { - //if (fwVersion.startsWith(linkTarget.completeBaseName())) { - // qCritical() << "current dc-firmware-version" << fwVersion - // << "already installed"; - // res = false; - } else { - res = true; + bool updateBinaryRes = true; - qInfo() << "DOWNLOADING" << finfo.completeBaseName() << "TO DC"; + qInfo() << "DOWNLOADING" << finfo.completeBaseName() << "TO DC"; #if UPDATE_DC == 1 - m_hw->dc_autoRequest(false);// default: turn auto-request setting off - QThread::sleep(1); // wait to be sure that there are no more - // commands sent to dc-hardware - qInfo() << "SET AUTO-REQUEST=FALSE"; + m_hw->dc_autoRequest(false);// default: turn auto-request setting off + QThread::sleep(1); // wait to be sure that there are no more + // commands sent to dc-hardware + qInfo() << "SET AUTO-REQUEST=FALSE"; - if ((res = updateBinary(fToWorkOn.toStdString().c_str())) == true) { - qCritical() << "downloaded binary" << fToWorkOn; - ++displayIndex; - emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")") - + QString(" Update ") + QFileInfo(fToWorkOn).fileName(), - Worker::UPDATE_STEP_DONE); - } - - m_hw->dc_autoRequest(true); // turn auto-request setting on - qInfo() << "SET AUTO-REQUEST=TRUE"; - qInfo() << "WAIT 10 SECS TO RECEIVE RESPONSES..."; - - QThread::sleep(10); // wait to be sure that responses - // have been received - qInfo() << "updated dc-hardware-version" << m_hw->dc_getHWversion(); - qInfo() << "updated dc-firmware-version" << m_hw->dc_getSWversion(); -#endif + if ((updateBinaryRes = updateBinary(fToWorkOn.toStdString().c_str())) == true) { + qCritical() << "downloaded binary" << fToWorkOn; + ++displayIndex; + emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")") + + QString(" Update ") + QFileInfo(fToWorkOn).fileName(), + Worker::UPDATE_STEP_DONE); } + + m_hw->dc_autoRequest(true); // turn auto-request setting on + qInfo() << "SET AUTO-REQUEST=TRUE"; + + QStringList const &versions = Update::getDcSoftAndHardWareVersion(); + if (versions.size() >= 2) { + if (updateBinaryRes == true) { + qInfo() << "dc-hardware-version (UPDATED)" << versions[0]; + qInfo() << "dc-firmware-version (UPDATED)" << versions[1]; + } else { + qInfo() << "dc-hardware-version (NOT UPDATED)" << versions[0]; + qInfo() << "dc-firmware-version (NOT UPDATED)" << versions[1]; + } + } +#endif + res = updateBinaryRes; } } else if (fToWorkOn.contains("DC2C_print", Qt::CaseInsensitive) && fToWorkOn.endsWith(".json", Qt::CaseInsensitive)) { @@ -791,8 +881,12 @@ bool Update::doUpdate(int &displayIndex, QStringList const &filesToWorkOn) { } } // for (it = openLines.cbegin(); it != openLines.end(); ++it) { - m_hw->dc_autoRequest(true); // ALWAYS turn autoRequest ON - qDebug() << "SET AUTO-REQUEST=TRUE"; + //m_hw->dc_autoRequest(true); // ALWAYS turn autoRequest ON + //qDebug() << "SET AUTO-REQUEST=TRUE"; + + if (serialOpened) { + m_hw->dc_closeSerial(); + } return res; }