From 0668ab65be32338661e72890aa3d9c2adc797a38 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Thu, 22 Jun 2023 15:36:36 +0200 Subject: [PATCH] Added helper-function downloadJson() to implement downloadinf print/conf/cash/device.json. Added some compile time flags used for testing. --- update.cpp | 178 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 123 insertions(+), 55 deletions(-) diff --git a/update.cpp b/update.cpp index 9672daa..96a9879 100644 --- a/update.cpp +++ b/update.cpp @@ -19,10 +19,17 @@ #include #include -#define COLUMN_REQUEST (0) -#define COLUMN_NAME (1) -#define COLUMN_DATE_TIME (2) -#define COLUMN_RESULT (3) +#define COLUMN_REQUEST (0) +#define COLUMN_NAME (1) +#define COLUMN_DATE_TIME (2) +#define COLUMN_RESULT (3) + +#define UPDATE_OPKG (0) +#define UPDATE_DC (1) +#define UPDATE_PRINTER_TEMPLATES (0) +#define UPDATE_CASH_TEMPLATE (0) +#define UPDATE_CONF_TEMPLATE (0) +#define UPDATE_DEVICE_TEMPLATE (0) static const QMap baudrateMap = { {"1200" , 0}, {"9600" , 1}, {"19200" , 2}, {"38400" , 3}, @@ -282,6 +289,8 @@ bool Update::startBootloader() const { qInfo() << "starting bootloader...OK"; std::this_thread::sleep_for(std::chrono::milliseconds(500)); return true; + } else { + qCritical() << "bootloader not up (" << nTry << ")"; } } qCritical() << "starting bootloader...FAILED"; @@ -324,7 +333,7 @@ bool Update::resetDeviceController() const { // std::this_thread::sleep_for(std::chrono::milliseconds(1000)); m_hw->bl_rebootDC(); // wait maximally 3 seconds, before starting bootloader - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + QThread::msleep(1500); qInfo() << "resetting device controller...OK"; return true; //} @@ -371,17 +380,11 @@ bool Update::updateBinary(char const *fileToSendToDC) { QFile fn(fileToSendToDC); bool r; if ((r = fn.exists()) == true) { - QString fwVersion = m_hw->dc_getSWversion(); - QString const hwVersion = m_hw->dc_getHWversion(); QString const linkTarget = fn.symLinkTarget(); QFileInfo fi(linkTarget); qInfo() << " updating binary (size=" << linkTarget << fi.size() << ")"; - qInfo() << " dc-hardware-version" << hwVersion; - qInfo() << "previous dc-firmware-version" << fwVersion; if ((r = updateDC(linkTarget)) == true) { - fwVersion = m_hw->dc_getSWversion(); qInfo() << " updating binary (size=" << linkTarget << fi.size() << ") done"; - qInfo() << "current dc-firmware-version" << fwVersion; } else { qCritical() << "updating binary (size=" << linkTarget << fi.size() << ")... FAILED"; } @@ -412,65 +415,88 @@ bool Update::updateDC(QString bFile) const { return true; } -bool Update::updatePrinterTemplate(enum FileTypeJson type, - int templateIdx, - QString fname) const { // name of the json-file - // sanity checks - if (type != FileTypeJson::PRINTER) { - qCritical() << "wrong file type" << (uint8_t)type; - return false; +QString Update::jsonType(enum FileTypeJson type) { + switch (type) { + case FileTypeJson::CASH: return "CASH"; + case FileTypeJson::CONFIG: return "CONFIG"; + case FileTypeJson::PRINTER: return "PRINTER"; + case FileTypeJson::SERIAL: return "SERIAL"; + case FileTypeJson::DEVICE: return "DEVICE"; + case FileTypeJson::TIME: return "TIME"; } + return "N/A"; +} - qInfo() << "updating printer template:" << fname << "..."; - qInfo() << " printer-template-index:" << templateIdx; +bool Update::downloadJson(enum FileTypeJson type, + int templateIdx, + QString jsFileToSendToDC) const { - int nTry = 10; - while (!m_hw->sys_ready4sending()) { // wait max. 5 seconds - QThread::sleep(1); + qDebug() << "updating json-file:" << jsFileToSendToDC << "..."; + qDebug() << " template-index:" << templateIdx; + qDebug() << " json-type:" << jsonType(type); + + m_hw->dc_autoRequest(true); // downloading Json needs the AutoEmission flag + qDebug() << "SET AUTO-REQUEST=TRUE"; + QThread::sleep(1); // make sure the auto-request flag is acknowledged + + bool ready = false; + int nTry = 25; + while ((ready = m_hw->sys_ready4sending()) == false) { + QThread::msleep(200); if (--nTry <= 0) { - qCritical() << "SYS NOT READY FOR SENDING AFTER 10 SECONDS"; - return false; + qCritical() << "SYS NOT READY FOR SENDING AFTER 5 SECONDS"; + break; } } + bool ret = false; - QFile file(fname); - QFileInfo fi(fname); // max. size of template file is 800 bytes - if (file.exists()) { - if (file.open(QIODevice::ReadOnly)) { - if (fi.size() <= 800) { - QByteArray ba = file.readAll(); - if (m_hw->sys_sendJsonFileToDc((uint8_t)(type), - templateIdx, - (uint8_t *)ba.data())) { - QThread::sleep(1); - qInfo() << "sent file" << fname << "to dc"; - ret = true; + if (ready) { + QFile file(jsFileToSendToDC); + QFileInfo fi(jsFileToSendToDC); // max. size of template file is 800 bytes + if (file.exists()) { + if (file.open(QIODevice::ReadOnly)) { + if (fi.size() <= 800) { + QByteArray ba = file.readAll(); + if (m_hw->sys_sendJsonFileToDc((uint8_t)(type), + templateIdx, + (uint8_t *)ba.data())) { + QThread::sleep(1); + qDebug() << "SENT" << jsFileToSendToDC; + ret = true; + } + } else { + qCritical() << "SIZE OF" << jsFileToSendToDC + << "TOO BIG (" << fi.size() << "BYTES)"; } } else { - qCritical() << "SIZE OF" << fname - << "TOO BIG (" << fi.size() << "BYTES)"; + qCritical() << "CANNOT OPEN" << jsFileToSendToDC << "FOR READING"; } } else { - qCritical() << "CANNOT OPEN" << fname << "FOR READING"; + qCritical() << jsFileToSendToDC << "DOES NOT EXIST"; } - } else { - qCritical() << fname << "DOES NOT EXIST"; } + + m_hw->dc_autoRequest(false); + qDebug() << "SET AUTO-REQUEST=FALSE"; + QThread::sleep(1); // make sure the auto-request flag is acknowledged + return ret; } -bool Update::updatePrinterConf(int templateIdx, QString fileToSendToDC) { - return updatePrinterTemplate(FileTypeJson::PRINTER, - templateIdx, - fileToSendToDC); +bool Update::updatePrinterTemplate(int templateIdx, QString jsFile) const { + return downloadJson(FileTypeJson::PRINTER, templateIdx, jsFile); } -bool Update::updateConf(QString fileToSendToDC) { - return false; +bool Update::updateConfig(QString jsFile) { + return downloadJson(FileTypeJson::CONFIG, 0, jsFile); } -bool Update::updateCashConf(QString fileToSendToDC) { - return false; +bool Update::updateCashConf(QString jsFile) { + return downloadJson(FileTypeJson::CASH, 0, jsFile); +} + +bool Update::updateDeviceConf(QString jsFile) { + return downloadJson(FileTypeJson::DEVICE, 0, jsFile); } QStringList Update::getLinesToWorkOn() { @@ -533,9 +559,17 @@ bool Update::doUpdate() { return false; } - m_hw->dc_autoRequest(false); - QThread::sleep(3); // wait to be sure that there are no more commands sent - // to dc-hardware + QString fwVersion = m_hw->dc_getSWversion(); + QString const hwVersion = m_hw->dc_getHWversion(); + + qInfo() << "current dc-hardware-version" << hwVersion; + qInfo() << "current dc-firmware-version" << fwVersion; + + m_hw->dc_autoRequest(false);// default: turn auto-request setting off + QThread::sleep(3); // wait to be sure that there are no more + // commands sent to dc-hardware + qDebug() << "SET AUTO-REQUEST=FALSE"; + QStringList linesToWorkOn = getLinesToWorkOn(); if (linesToWorkOn.size() == 0) { qCritical() << "No lines to handle in" << m_update_ctrl_file.fileName(); @@ -568,31 +602,59 @@ bool Update::doUpdate() { if (name.contains("dc2c", Qt::CaseInsensitive) && name.endsWith(".bin", Qt::CaseInsensitive)) { qInfo() << "downloading" << name.trimmed() << "to DC"; + res = true; +#if UPDATE_DC == 1 if ((res = updateBinary(name.toStdString().c_str())) == true) { qInfo() << "downloaded binary" << name; } +#endif } else if (name.contains("DC2C_print", Qt::CaseInsensitive) && name.endsWith(".json", Qt::CaseInsensitive)) { + res = true; +#if UPDATE_PRINTER_TEMPLATES == 1 int i = name.indexOf("DC2C_print", Qt::CaseInsensitive); int const templateIdx = name.mid(i).midRef(10, 2).toInt(); if ((templateIdx < 1) || (templateIdx > 32)) { qCritical() << "WRONG TEMPLATE INDEX" << templateIdx; res = false; } else { - if ((res = updatePrinterConf(templateIdx, name)) == true) { - qInfo() << "downloaded printer template" << name; + if ((res = updatePrinterTemplate(templateIdx, name))) { + qInfo() << "downloaded printer template"<< name; } } +#endif } else if (name.contains("DC2C_cash", Qt::CaseInsensitive) && name.endsWith(".json", Qt::CaseInsensitive)) { + res = true; +#if UPDATE_CASH_TEMPLATE + if ((res = updateCashConf(name))) { + qInfo() << "downloaded cash template"<< name; + } +#endif } else if (name.contains("DC2C_conf", Qt::CaseInsensitive) && name.endsWith(".json", Qt::CaseInsensitive)) { + res = true; +#if UPDATE_CONF_TEMPLATE + if ((res= updateConfig(name))) { + qInfo() << "downloaded config template"<< name; + } +#endif + } else if (name.contains("DC2C_device", Qt::CaseInsensitive) + && name.endsWith(".json", Qt::CaseInsensitive)) { + res = true; +#if UPDATE_DEVICE_TEMPLATE + if ((res = updateDeviceConf(name))) { + qInfo() << "downloaded device template"<< name; + } +#endif } else { qCritical() << "UNKNOWN JSON FILE NAME" << name; res = false; } } else if (request == "EXECUTE" && name.contains("opkg")) { qInfo() << "starting" << name.trimmed(); + res = true; +#if UPDATE_OPKG == 1 QScopedPointer p(new QProcess(this)); p->setProcessChannelMode(QProcess::MergedChannels); p->start(name.trimmed()); @@ -620,6 +682,7 @@ bool Update::doUpdate() { } else { qCritical() << "PROCESS" << name << "TIMEOUT AT START"; } +#endif } else { // TODO } @@ -634,6 +697,11 @@ bool Update::doUpdate() { closeSerial(); m_hw->dc_autoRequest(true); + qDebug() << "SET AUTO-REQUEST=TRUE"; + + qInfo() << "current dc-hardware-version" << m_hw->dc_getHWversion(); + qInfo() << "current dc-firmware-version" << m_hw->dc_getSWversion(); + return finishUpdate(linesToWorkOn.size() > 0); }