executeUpdateScript: set timeout to 200 secs. Open serial port only once for the whole loop handling the lines in update_log.csv. Sending of device controller in Debug-setting worked. Execution of opkg-commnds worked. Sending of JSON files needs further testing: sys_readyForSending failes sometimes after 5 seconds.
This commit is contained in:
		
							
								
								
									
										174
									
								
								update.cpp
									
									
									
									
									
								
							
							
						
						
									
										174
									
								
								update.cpp
									
									
									
									
									
								
							@@ -130,7 +130,8 @@ bool Update::execUpdateScript() {
 | 
			
		||||
    p->start(update_psa);
 | 
			
		||||
    if (p->waitForStarted(1000)) {
 | 
			
		||||
        if (p->state() == QProcess::ProcessState::Running) {
 | 
			
		||||
            if (p->waitForFinished(60000)) {
 | 
			
		||||
            int const timeout = 200000; // sometimes signal strength of modem is quite low
 | 
			
		||||
            if (p->waitForFinished(timeout)) {
 | 
			
		||||
                QString output = p->readAllStandardOutput().toStdString().c_str();
 | 
			
		||||
                QStringList lst = output.split('\n');
 | 
			
		||||
                for (int i = 0; i < lst.size(); ++i) {
 | 
			
		||||
@@ -139,6 +140,9 @@ bool Update::execUpdateScript() {
 | 
			
		||||
                qInfo() << "EXECUTED" << update_psa;
 | 
			
		||||
                return ((p->exitStatus() == QProcess::NormalExit)
 | 
			
		||||
                     && (p->exitCode() == 0));
 | 
			
		||||
            } else {
 | 
			
		||||
                qCritical() << "update-script TIMEDOUT after"
 | 
			
		||||
                            << timeout/1000 << "seconds";
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -363,127 +367,110 @@ bool Update::downloadBinaryToDC(QString const &bFile) const {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updateBinary(char const *fileToSendToDC) {
 | 
			
		||||
    return true;
 | 
			
		||||
    // TODO
 | 
			
		||||
    qInfo() << "updating device controller binary" << 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();
 | 
			
		||||
        qInfo() << "updating binary (dc)" << linkTarget << "...";
 | 
			
		||||
        if ((r = updateDC(linkTarget, m_baudrate, m_serialInterface)) == true) {
 | 
			
		||||
            qInfo() << "updating binary (dc)" << linkTarget << "... done";
 | 
			
		||||
        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 (dc)" << linkTarget << "... FAILED";
 | 
			
		||||
            qCritical() << "updating binary (size=" << linkTarget << fi.size() << ")... FAILED";
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        qCritical() << "symlink" << fileToSendToDC << "does not exist";
 | 
			
		||||
        qCritical() << "symlink" << fileToSendToDC
 | 
			
		||||
                    << "does not exist -> NO UPDATE OF DC FIRMWARE";
 | 
			
		||||
    }
 | 
			
		||||
    return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updateDC(QString bFile, QString br, QString serial) const {
 | 
			
		||||
    if (!baudrateMap.contains(br)) { // sanity check
 | 
			
		||||
        qCritical() << "passed wrong baudrate" << br;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    m_hw->dc_autoRequest(false);
 | 
			
		||||
    qDebug() << "updating dc: " << bFile << br << serial << "...";
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
    if (!openSerial(baudrateMap.value(br), br, serial)) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
bool Update::updateDC(QString bFile) const {
 | 
			
		||||
    qDebug() << "updating dc...";
 | 
			
		||||
    qDebug() << "updating dc: file to send" << bFile;
 | 
			
		||||
    if (!resetDeviceController()) {
 | 
			
		||||
        closeSerial();
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (!startBootloader()) {
 | 
			
		||||
        closeSerial();
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (!downloadBinaryToDC(bFile)) {
 | 
			
		||||
        stopBootloader();
 | 
			
		||||
        closeSerial();
 | 
			
		||||
        qCritical() << "updating dc: " << bFile << br << serial << "...FAILED";
 | 
			
		||||
        qCritical() << "updating dc: " << bFile << "...FAILED";
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qInfo() << "updating dc: " << bFile << br << serial << "...OK";
 | 
			
		||||
 | 
			
		||||
    qInfo() << "updating dc: " << bFile << "...OK";
 | 
			
		||||
    stopBootloader();
 | 
			
		||||
    QThread::sleep(3);
 | 
			
		||||
 | 
			
		||||
    closeSerial();
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updatePrinterTemplate(enum FileTypeJson type,
 | 
			
		||||
                                   int templateIdx,
 | 
			
		||||
                                   QString fname,
 | 
			
		||||
                                   QString br,
 | 
			
		||||
                                   QString serial) const {
 | 
			
		||||
                                   QString fname) const { // name of the json-file
 | 
			
		||||
    // sanity checks
 | 
			
		||||
    if (!baudrateMap.contains(br)) {
 | 
			
		||||
        qCritical() << "passed wrong baudrate" << br;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (type != FileTypeJson::PRINTER) {
 | 
			
		||||
        qCritical() << "wrong file type" << (uint8_t)type;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    qDebug() << "updating: " << fname << br << serial << "...";
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
    qInfo() << "updating printer template:" << fname << "...";
 | 
			
		||||
    qInfo() << "   printer-template-index:" << templateIdx;
 | 
			
		||||
 | 
			
		||||
    if (!serial.isNull()) {
 | 
			
		||||
        if (!openSerial(baudrateMap.value(br), br, serial)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    int nTry = 50;
 | 
			
		||||
    int nTry = 10;
 | 
			
		||||
    while (!m_hw->sys_ready4sending()) { // wait max. 5 seconds
 | 
			
		||||
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
 | 
			
		||||
        QThread::sleep(1);
 | 
			
		||||
        if (--nTry <= 0) {
 | 
			
		||||
            qCritical() << "sys not ready for sending";
 | 
			
		||||
            if (!serial.isNull()) {
 | 
			
		||||
                closeSerial();
 | 
			
		||||
            }
 | 
			
		||||
            qCritical() << "SYS NOT READY FOR SENDING AFTER 10 SECONDS";
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    bool ret = true;
 | 
			
		||||
    bool ret = false;
 | 
			
		||||
    QFile file(fname);
 | 
			
		||||
    if (file.exists() && file.open(QIODevice::ReadOnly)) {
 | 
			
		||||
        QByteArray ba = file.readAll();
 | 
			
		||||
        if (ba.size() <= 800) { // max. size is 800 bytes
 | 
			
		||||
            if (m_hw->sys_sendJsonFileToDc((uint8_t)(type),
 | 
			
		||||
                                           templateIdx,
 | 
			
		||||
                                           (uint8_t *)ba.data())) {
 | 
			
		||||
                std::this_thread::sleep_for(std::chrono::seconds(1));
 | 
			
		||||
                qInfo() << "sent file" << fname << "to dc";
 | 
			
		||||
    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;
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                qCritical() << "SIZE OF" << fname
 | 
			
		||||
                            << "TOO BIG (" << fi.size() << "BYTES)";
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            qCritical() << "CANNOT OPEN" << fname << "FOR READING";
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        qCritical() << fname << "!!! does not exist!!!";
 | 
			
		||||
        ret = false;
 | 
			
		||||
    }
 | 
			
		||||
    if (!serial.isNull()) {
 | 
			
		||||
        closeSerial();
 | 
			
		||||
        qCritical() << fname << "DOES NOT EXIST";
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updatePrinterConf(int templateIdx, QString fileToSendToDC) {
 | 
			
		||||
    return true;
 | 
			
		||||
    // TODO
 | 
			
		||||
    qCritical() << "updating printer template: " << fileToSendToDC;
 | 
			
		||||
    return updatePrinterTemplate(FileTypeJson::PRINTER,
 | 
			
		||||
                                 templateIdx,
 | 
			
		||||
                                 fileToSendToDC,
 | 
			
		||||
                                 QString(m_baudrate),
 | 
			
		||||
                                 QString(m_serialInterface));
 | 
			
		||||
                                 fileToSendToDC);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updateConf(QString fileToSendToDC) {
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Update::updateCashConf(QString fileToSendToDC) {
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QStringList Update::getLinesToWorkOn() {
 | 
			
		||||
@@ -540,12 +527,20 @@ bool Update::doUpdate() {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!openSerial(baudrateMap.value(m_baudrate), m_baudrate, m_serialInterface)) {
 | 
			
		||||
        qCritical() << "CANNOT OPEN" << m_serialInterface << "(BAUDRATE="
 | 
			
		||||
                    << m_baudrate << ")";
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    m_hw->dc_autoRequest(false);
 | 
			
		||||
    QThread::sleep(3); // wait to be sure that there are no more commands sent
 | 
			
		||||
                       // to dc-hardware
 | 
			
		||||
    QStringList linesToWorkOn = getLinesToWorkOn();
 | 
			
		||||
    if (linesToWorkOn.size() == 0) {
 | 
			
		||||
        qCritical() << "No lines to handle in" << m_update_ctrl_file.fileName();
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qDebug() << "open lines...";
 | 
			
		||||
    for (int i=0; i< linesToWorkOn.size(); ++i) {
 | 
			
		||||
        qDebug() << "line" << i << ":" << linesToWorkOn.at(i).trimmed();
 | 
			
		||||
@@ -560,8 +555,9 @@ bool Update::doUpdate() {
 | 
			
		||||
        }
 | 
			
		||||
        QStringList lst = split(line.trimmed());
 | 
			
		||||
        if (lst.size() != 4) {
 | 
			
		||||
            qCritical() << "Parsing error for" << m_update_ctrl_file.fileName();
 | 
			
		||||
            return false;
 | 
			
		||||
            qCritical() << "PARSING ERROR FOR LINE"
 | 
			
		||||
                        << line << "IN" << m_update_ctrl_file.fileName();
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        QString const &request = lst[COLUMN_REQUEST];
 | 
			
		||||
        QString const &name = lst[COLUMN_NAME];
 | 
			
		||||
@@ -569,24 +565,31 @@ bool Update::doUpdate() {
 | 
			
		||||
        // QString const &result = lst[COLUMN_RESULT];
 | 
			
		||||
        qDebug() << "request=" << request << ", name=" << name;
 | 
			
		||||
        if (request.trimmed() == "DOWNLOAD") {
 | 
			
		||||
            if (name.contains("dc2c") && name.endsWith(".bin")) {
 | 
			
		||||
            if (name.contains("dc2c", Qt::CaseInsensitive) &&
 | 
			
		||||
                name.endsWith(".bin", Qt::CaseInsensitive)) {
 | 
			
		||||
                qInfo() << "downloading" << name.trimmed() << "to DC";
 | 
			
		||||
                if ((res = updateBinary(name.toStdString().c_str())) == true) {
 | 
			
		||||
                    qInfo() << "downloaded binary" << 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();
 | 
			
		||||
            } else if (name.contains("DC2C_print", Qt::CaseInsensitive)
 | 
			
		||||
                    && name.endsWith(".json", Qt::CaseInsensitive)) {
 | 
			
		||||
                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";
 | 
			
		||||
                    qCritical() << "WRONG TEMPLATE INDEX" << templateIdx;
 | 
			
		||||
                    res = false;
 | 
			
		||||
                } else {
 | 
			
		||||
                    if ((res = updatePrinterConf(templateIdx, name)) == true) {
 | 
			
		||||
                        qInfo() << "downloaded printer template" << name;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            } else if (name.contains("DC2C_cash", Qt::CaseInsensitive)
 | 
			
		||||
                    && name.endsWith(".json", Qt::CaseInsensitive)) {
 | 
			
		||||
            } else if (name.contains("DC2C_conf", Qt::CaseInsensitive)
 | 
			
		||||
                    && name.endsWith(".json", Qt::CaseInsensitive)) {
 | 
			
		||||
            } else {
 | 
			
		||||
                // TODO
 | 
			
		||||
                qCritical() << "UNKNOWN JSON FILE NAME" << name;
 | 
			
		||||
                res = false;
 | 
			
		||||
            }
 | 
			
		||||
        } else if (request == "EXECUTE" && name.contains("opkg")) {
 | 
			
		||||
            qInfo() << "starting" << name.trimmed();
 | 
			
		||||
@@ -622,14 +625,15 @@ bool Update::doUpdate() {
 | 
			
		||||
        }
 | 
			
		||||
        char buf[128];
 | 
			
		||||
        memset(buf, 0x00, sizeof(buf));
 | 
			
		||||
        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");
 | 
			
		||||
        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");
 | 
			
		||||
        m_update_ctrl_file_copy.write(buf);
 | 
			
		||||
    } // for (it = openLines.cbegin(); it != openLines.end(); ++it) {
 | 
			
		||||
 | 
			
		||||
    closeSerial();
 | 
			
		||||
    m_hw->dc_autoRequest(true);
 | 
			
		||||
    return finishUpdate(linesToWorkOn.size() > 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user