diff --git a/update.cpp b/update.cpp index c42979c..ea6f086 100644 --- a/update.cpp +++ b/update.cpp @@ -17,12 +17,18 @@ #include #include #include +#include #define COLUMN_REQUEST (0) #define COLUMN_NAME (1) #define COLUMN_DATE_TIME (2) #define COLUMN_RESULT (3) +static const QMap baudrateMap = { + {"1200" , 0}, {"9600" , 1}, {"19200" , 2}, {"38400" , 3}, + {"57600" , 4}, {"115200" , 5} +}; + hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) { hwinf *hw = nullptr; if (plugInDir.exists()) { @@ -78,12 +84,6 @@ Update::Update(hwinf *hw, , m_workingDir(workingDir) , m_init(true) { - // qCritical() << "workingDir" << m_workingDir; - - // m_hw->dc_autoRequest(false); - - return; - execUpdateScript(); if (!m_update_ctrl_file.exists()) { @@ -106,8 +106,6 @@ Update::Update(hwinf *hw, m_init = false; } qDebug() << "Opened" << m_update_ctrl_file_copy.fileName(); - - //QApplication::processEvents(); } Update::~Update() { @@ -373,26 +371,116 @@ bool Update::updateBinary(char const *fileToSendToDC) { bool r; if ((r = fn.exists()) == true) { QString const linkTarget = fn.symLinkTarget(); - qCritical() << "updating binary (dc): link target" << linkTarget; - // debug - //r = m_hw->dc_updateDC(linkTarget, m_baudrate, m_serialInterface); - qCritical() << "updating binary (dc): " - << linkTarget << ((r == true) ? "OK" : "ERROR"); + qInfo() << "updating binary (dc)" << linkTarget << "..."; + if ((r = updateDC(linkTarget, m_baudrate, m_serialInterface)) == true) { + qInfo() << "updating binary (dc)" << linkTarget << "... done"; + } else { + qCritical() << "updating binary (dc)" << linkTarget << "... FAILED"; + } } else { qCritical() << "symlink" << fileToSendToDC << "does not exist"; } return r; } -bool Update::updatePrinterConf(int nrOfTemplate, char const *fileToSendToDC) { +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; + } + if (!resetDeviceController()) { + closeSerial(); + return false; + } + if (!startBootloader()) { + closeSerial(); + return false; + } + if (!downloadBinaryToDC(bFile)) { + stopBootloader(); + closeSerial(); + qCritical() << "updating dc: " << bFile << br << serial << "...FAILED"; + return false; + } + + qInfo() << "updating dc: " << bFile << br << serial << "...OK"; + + stopBootloader(); + QThread::sleep(3); + + closeSerial(); + return true; +} + +bool Update::updatePrinterTemplate(enum FileTypeJson type, + int templateIdx, + QString fname, + QString br, + QString serial) const { + // 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 << "..."; + if (!serial.isNull()) { + if (!openSerial(baudrateMap.value(br), br, serial)) { + return false; + } + } + int nTry = 50; + while (!m_hw->sys_ready4sending()) { // wait max. 5 seconds + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (--nTry <= 0) { + qCritical() << "sys not ready for sending"; + if (!serial.isNull()) { + closeSerial(); + } + return false; + } + } + bool ret = true; + 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"; + } + } + } else { + qCritical() << fname << "!!! does not exist!!!"; + ret = false; + } + if (!serial.isNull()) { + closeSerial(); + } + return ret; +} + +bool Update::updatePrinterConf(int templateIdx, QString fileToSendToDC) { qCritical() << "updating printer template: " << fileToSendToDC; - return true; // debug - QVector printTemplates{ nrOfTemplate }; - QVector filesToSend{ fileToSendToDC }; - //return m_hw->dc_updatePrinterTemplate(hwapi::FileTypeJson::PRINTER, - // printTemplates, filesToSend, - // QString(m_baudrate), - // QString(m_serialInterface)); + return updatePrinterTemplate(FileTypeJson::PRINTER, + templateIdx, + fileToSendToDC, + QString(m_baudrate), + QString(m_serialInterface)); } QStringList Update::getOpenLines() { @@ -451,6 +539,15 @@ bool Update::doUpdate() { } QStringList openLines = getOpenLines(); + if (openLines.size() == 0) { + qCritical() << "No lines to handle in" << m_update_ctrl_file.fileName(); + return false; + } + + qDebug() << "open lines..."; + for (int i=0; i< openLines.size(); ++i) { + qDebug() << "line" << i << ":" << openLines.at(i).trimmed(); + } QList::const_iterator it; for (it = openLines.cbegin(); it != openLines.cend(); ++it) { @@ -474,6 +571,7 @@ bool Update::doUpdate() { return false; } if (name.contains("dc2c") && name.endsWith(".bin")) { + qInfo() << "downloading" << name.trimmed() << "to DC"; if ((res = updateBinary(name.toStdString().c_str())) == true) { qInfo() << "downloaded binary" << name; } @@ -481,22 +579,28 @@ bool Update::doUpdate() { if (name.contains("DC2C_print") && name.endsWith(".json")) { int i = name.indexOf("DC2C_print"); int templateIdx = name.mid(i).midRef(10, 2).toInt(); - if ((res = updatePrinterConf(templateIdx, name.toStdString().c_str())) == true) { - qInfo() << "downloaded printer template" << name; + if ((templateIdx < 1) || (templateIdx > 32)) { + qCritical() << "wrong template index"; + res = false; + } else { + if ((res = updatePrinterConf(templateIdx, name)) == true) { + qInfo() << "downloaded printer template" << name; + } } } else if (name.contains("opkg")) { - int i = name.indexOf("opkg "); - QString rest = name.mid(i+5).trimmed(); + qInfo() << "starting" << name.trimmed(); QScopedPointer p(new QProcess(this)); p->setProcessChannelMode(QProcess::MergedChannels); - p->start("opkg", QStringList() << rest); + p->start(name.trimmed()); if (p->waitForStarted(1000)) { if (p->state() == QProcess::ProcessState::Running) { - if (p->waitForFinished(10000)) { + if (p->waitForFinished(100000)) { QString output = p->readAllStandardOutput(); QStringList outputLst = split(output, QChar('\n')); - qDebug() << outputLst; + for (int line=0; line < outputLst.size(); ++line) { + qDebug() << outputLst[line]; + } res = true; qInfo() << "EXECUTED" << name; }