diff --git a/update.cpp b/update.cpp index da4758f..0cc7b0f 100644 --- a/update.cpp +++ b/update.cpp @@ -129,200 +129,6 @@ Update::Update(Worker *worker, Update::~Update() { } -Update::DownloadResult Update::sendStatus(int ret) const { - switch (ret) { // return values of dc are: - case 0: // 0: no answer by now - return DownloadResult::NOP; // 1: error - case 10: // 10: success - return DownloadResult::OK; - default:; - } - return DownloadResult::ERROR; -} - -Update::DownloadResult Update::sendNextAddress(int bNum) const { - // sends address only if blockNumber is one of 0, 1024, 2048, 3072, 4096 - int noAnswerCount = 0; - int errorCount = 0; - if ( bNum==0 || bNum==1024 || bNum==2048 || bNum==3072 || bNum==4096 ) { - // qDebug() << "addr-block" << bNum << "..."; - while (noAnswerCount <= 250) { - m_hw->bl_sendAddress(bNum); - QThread::msleep(100); - DownloadResult const res = sendStatus(m_hw->bl_wasSendingAddOK()); - if (res != DownloadResult::NOP) { - if (res == DownloadResult::ERROR) { - if (++errorCount >= 10) { - qCritical() << "addr-block" << bNum << "...FAILED"; - return res; - } - } else { // res == DownloadResult::OK - // qInfo() << "addr-block" << bNum << "...OK"; - return res; - } - } else { - noAnswerCount += 1; // no answer by now - } - } - // wait max. about 3 seconds - return DownloadResult::TIMEOUT; - } - // blockNumber is not one of 0, 1024, 2048, 3072, 4096 -> do nothing - return DownloadResult::NOP; -} - -Update::DownloadResult Update::sendNextDataBlock(QByteArray const &binary, - int bNum) const { - uint8_t local[66]; - int const bAddr = bNum * 64; - int noAnswerCount = 0; - int errorCount = 0; - - memcpy(local, binary.constData() + bAddr, 64); - local[64] = local[65] = 0x00; - - // QByteArray b((const char *)(&local[0]), 64); - // qCritical() << "SNDB" << bNum << b.size() << b.toHex(); - - while (noAnswerCount <= 250) { - m_hw->bl_sendDataBlock(64, local); - QThread::msleep(10); - DownloadResult const res = sendStatus(m_hw->bl_wasSendingDataOK()); - if (res != DownloadResult::NOP) { - if (res == DownloadResult::ERROR) { - if (++errorCount >= 10) { - qCritical() << "data for block" << bNum << "...FAILED"; - return res; - } - } else { - // qInfo() << "data for block" << bNum << "OK"; - return res; - } - } else { - noAnswerCount += 1; // no answer by now - } - } - // wait max. about 3 seconds - return DownloadResult::TIMEOUT; -} - -Update::DownloadResult Update::dc_downloadBinary(QByteArray const &b) const { - int const nBlocks = (((b.size())%64)==0) ? (b.size()/64) : (b.size()/64)+1; - - // fill lst block of data to be sent with 0xFF - QByteArray ba = b.leftJustified(nBlocks*64, (char)(0xFF)); - - qInfo() << "total number of bytes to send to dc" << ba.size(); - qInfo() << "total number of blocks to send to dc" << nBlocks; - - int bNum = 0; - DownloadResult res = DownloadResult::OK; - fprintf(stderr, "\n64-byte block %04d ", bNum); - while (res != DownloadResult::ERROR && bNum < nBlocks) { - if ((res = sendNextAddress(bNum)) != DownloadResult::ERROR) { - if ((res = sendNextDataBlock(ba, bNum)) != DownloadResult::ERROR) { - bNum += 1; - fprintf(stderr, "."); - if ((bNum % 80) == 0) { - fprintf(stderr, "\n64-byte block %04d ", bNum); - } - } - } - } - fprintf(stderr, "\nlast 64-byte block %04d\n", bNum); - - int const rest = ba.size() % 64; - int const offset = ba.size() - rest; - char const *startAddress = ba.constData() + offset; - - if (rest > 0) { - // SHOULD NEVER HAPPEN !!! - uint8_t local[66]; - memset(local, 0xFF, sizeof(local)); - memcpy(local, startAddress, rest); - qCritical() << "ERROR SEND REMAINING" << rest << "BYTES"; - m_hw->bl_sendDataBlock(64, local); - } - - m_hw->bl_sendLastBlock(); - qInfo() << "last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK()); - return res; -} - -bool Update::startBootloader() const { // deprecated - return false; -#if 0 - int nStartTry = 5; - while (--nStartTry >= 0) { - m_hw->bl_startBL(); - QThread::msleep(500); - int nCheckTry = 10; - while (--nCheckTry >= 0) { - m_hw->bl_checkBL(); - QThread::msleep(500); - if (m_hw->bl_isUp()) { - qInfo() << "starting bootloader...OK"; - return true; - } else { - qCritical() << "bootloader not up (" - << nStartTry << "," << nCheckTry << ")" << QThread::currentThread(); - } - } - } - qCritical() << "starting bootloader...FAILED" << QThread::currentThread(); - return false; -#endif -} - -bool Update::stopBootloader() const { - // stop bootloader: this MUST work -> otherwise the PSA has to be restarted - // manually - emit m_worker->showErrorMessage("dc update", "stopping bootloader..."); - - int nTryFinalize = 1; // could do this in an endless loop - - do { - // in principle, any value except BL_STOP will do, as we want to detect - // change to BL_STOP - m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::BL_CHECK); - - QApplication::postEvent( - m_worker->mainWindow(), - new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_STOP, nTryFinalize)); - - QThread::sleep(1); - - int const cntLimit = 20; - int cnt = 0; - while (++cnt < cntLimit && - m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_STOP) { - // wait until bl_stopBL() has been sent - QThread::msleep(500); - } - - QApplication::postEvent( - m_worker->mainWindow(), - new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_CHECK, nTryFinalize)); - QThread::sleep(1); - - QApplication::postEvent( - m_worker->mainWindow(), - new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_IS_UP, nTryFinalize)); - QThread::sleep(1); - - cnt = 0; - while (++cnt < cntLimit && - m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_DOWN) { - // wait until done - QThread::msleep(200); - } - - } while (++nTryFinalize <= MainWindow::BL_STOP_COUNT && - m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_DOWN); - - return (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_DOWN); -} - // br is a index into a table, used for historical reasons. bool Update::openSerial(int br, QString baudrate, QString comPort) const { qDebug() << "opening serial" << br << baudrate << comPort << "..."; @@ -354,51 +160,6 @@ bool Update::isSerialOpen() const { return m_hw->dc_isPortOpen(); } -bool Update::resetDeviceController() const { // deprecated - return false; -#if 0 - qDebug() << "resetting device controller..."; - m_hw->bl_rebootDC(); - // wait maximally 3 seconds, before starting bootloader - qInfo() << "resetting device controller...OK"; - return true; -#endif -} - -QByteArray Update::loadBinaryDCFile(QString filename) const { - qDebug() << "loading dc binary" << filename << "..."; - - QFile file(filename); // closed in destructor call - if (!file.exists()) { - qCritical() << file.fileName() << "does not exist"; - return QByteArray(); - } - if (!file.open(QIODevice::ReadOnly)) { - qCritical() << "cannot open file" << file.fileName(); - return QByteArray(); - } - qInfo() << "loading dc binary" << filename << "...OK"; - return file.readAll(); -} - -bool Update::downloadBinaryToDC(QString const &bFile) const { - qDebug() << "sending" << bFile << "to dc..."; - QByteArray const dcBinary = loadBinaryDCFile(bFile); - if (dcBinary.size() > 0) { - if (dc_downloadBinary(dcBinary) != DownloadResult::OK) { - qCritical() << "sending" << bFile << "to dc...FAILED"; - return false; - } else { - qInfo() << "sending" << bFile << "to dc...OK"; - } - } else { - qCritical() << "sending" << bFile << "to dc...FAILED"; - qCritical() << "loading binary" << bFile << "FAILED"; - return false; - } - return true; -} - /* ///////////////////////////////////////////////////////////////////////////////