save for christmas

This commit is contained in:
2024-12-20 13:01:34 +01:00
parent 2ec7b61682
commit 7bfb7f5a3b
19 changed files with 624 additions and 92 deletions

View File

@@ -96,17 +96,14 @@ bool Update::unloadDCPlugin() {
}
QString Update::dcVersion(QString const &dcBinFile) {
Command c("bash");
QStringList param;
QProcess p;
QStringList params;
param << "-c" << QString(R"(strings %1 | grep DC2c.\[0-9\] | uniq)").arg(dcBinFile);
params << "-c" << QString(R"(strings %1 | grep DC2c.\[0-9\] | uniq)").arg(dcBinFile);
if (c.execute("/tmp", param)) {
return c.getCommandResult().trimmed().split(QRegularExpression("\\s")).first();
// qInfo() << "(" << __func__ << ":" << __LINE__ << ")" << v;
}
return "";
p.start("bash", params);
p.waitForFinished();
return QString(p.readAllStandardOutput()).trimmed().split(QRegularExpression("\\s")).first();
}
class hwapi;
@@ -116,11 +113,14 @@ Update::Update(QString customerRepository,
QString plugInDir,
QString pluginName,
QString workingDir,
QString psaDcDir,
bool dryRun,
QObject *parent,
char const *serialInterface,
char const *baudrate)
: QObject(parent)
: QObject(parent) {
#if 0
, m_hw(loadDCPlugin(QDir(plugInDir), pluginName))
, m_serialInterface(serialInterface)
, m_baudrate(baudrate)
@@ -129,6 +129,7 @@ Update::Update(QString customerRepository,
, m_branchName(branchName)
, m_pluginName(pluginName)
, m_workingDir(workingDir)
, m_psaDcDir(psaDcDir)
, m_dryRun(dryRun)
, m_sys_areDCdataValid(false) {
@@ -138,6 +139,8 @@ Update::Update(QString customerRepository,
// carun stoppen
}
#endif
m_start = QDateTime::currentDateTime();
}
Update::~Update() {
@@ -163,9 +166,16 @@ Update::sendNextAddress(int bNum) const {
if ( bNum==0 || bNum==1024 || bNum==2048 || bNum==3072 || bNum==4096 ) {
// qDebug() << "addr-block" << bNum << "...";
while (noAnswerCount <= 250) {
m_hw->bl_sendAddress(bNum);
// TODO
// m_hw->bl_sendAddress(bNum);
QThread::msleep(100);
DownloadResult const res = sendStatus(m_hw->bl_wasSendingAddOK());
// TODO
// DownloadResult const res = sendStatus(m_hw->bl_wasSendingAddOK());
DownloadResult const res = DownloadResult::OK;
if (res != DownloadResult::NOP) {
if (res == DownloadResult::ERROR) {
if (++errorCount >= 10) {
@@ -173,7 +183,7 @@ Update::sendNextAddress(int bNum) const {
return res;
}
} else { // res == DownloadResult::OK
qCritical() << "addr-block" << bNum << "...OK";
qInfo() << nextTimePoint().toUtf8().constData() << "addr-block" << bNum << "...done";
return res;
}
} else {
@@ -197,13 +207,28 @@ Update::sendNextDataBlock(QByteArray const &binary, int bNum) const {
memcpy(local, binary.constData() + bAddr, 64);
local[64] = local[65] = 0x00;
QString s = nextTimePoint();
s += " sending block ";
s += QString("%1/%2 ...done <PROGRESS>").arg(bNum).arg(m_totalBlocks);
s += QString::number(ceil(((bNum * 100.0) / (double)m_totalBlocks)));
qInfo() << s.toUtf8().constData();
QThread::msleep(200);
return DownloadResult::OK;
// 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());
// TODO
// m_hw->bl_sendDataBlock(64, local);
// TODO
// DownloadResult const res = sendStatus(m_hw->bl_wasSendingDataOK());
DownloadResult const res = DownloadResult::OK;
if (res != DownloadResult::NOP) {
if (res == DownloadResult::ERROR) {
if (++errorCount >= 10) {
@@ -211,7 +236,8 @@ Update::sendNextDataBlock(QByteArray const &binary, int bNum) const {
return res;
}
} else {
qCritical() << "data for block" << bNum << "OK";
qInfo() << nextTimePoint().toUtf8().constData() << "data for block"
<< QString("%1/%2").arg(bNum).arg(m_totalBlocks) << "done";
return res;
}
} else {
@@ -223,7 +249,11 @@ Update::sendNextDataBlock(QByteArray const &binary, int bNum) const {
}
bool Update::startBootloader() const {
qDebug() << "starting bootloader...";
QThread::msleep(1000);
qInfo() << nextTimePoint().toUtf8().constData() << "starting bootloader ...done";
return true;
#if 0
int nTry = 5;
while (--nTry >= 0) {
m_hw->bl_startBL();
@@ -239,9 +269,15 @@ bool Update::startBootloader() const {
}
qCritical() << "starting bootloader...FAILED";
return false;
#endif
}
bool Update::stopBootloader() const {
QThread::msleep(1000);
qInfo() << nextTimePoint().toUtf8().constData() << "stopping bootloader ...done";
return true;
#if 0
qDebug() << "stopping bootloader...";
int nTry = 5;
while (--nTry >= 0) {
@@ -254,21 +290,24 @@ bool Update::stopBootloader() const {
}
qCritical() << "stopping bootloader...FAILED";
return false;
#endif
}
bool Update::resetDeviceController() const {
qDebug() << "resetting device controller...";
m_hw->bl_rebootDC();
// TODO
// m_hw->bl_rebootDC();
// wait maximally 3 seconds, before starting bootloader
QThread::sleep(1);
qInfo() << "resetting device controller...OK";
qInfo() << nextTimePoint().toUtf8().constData()
<< "resetting device controller ...done";
return true;
}
QByteArray Update::loadBinaryDCFile(QString const &filename) const {
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "loading dc binary" << filename << "...";
QFile file(filename); // closed in destructor call
if (!file.exists()) {
@@ -281,8 +320,9 @@ QByteArray Update::loadBinaryDCFile(QString const &filename) const {
<< "cannot open file" << file.fileName();
return QByteArray();
}
qCritical() << "(" << __func__ << ":" << __LINE__ << ")"
<< "loading dc binary" << filename << "...OK";
qInfo() << nextTimePoint().toUtf8().constData()
<< "loading dc binary to memory" << Update::dcVersion(filename) << "...done";
return file.readAll();
}
@@ -368,42 +408,46 @@ QByteArray Update::loadBinaryDCFile(QString const &filename) const {
// There is no problem to repeat this command until the
// bootloader is really not running anymore.
*/
bool Update::doUpdate() {
bool Update::doUpdate(QString const &dcFileName) {
qInfo() << "<DC-VERSION>" << Update::dcVersion(dcFileName);
m_dcFileName = dcFileName;
//QString const &fToWorkOn = usbStickDetected ? QDir::cleanPath(it->trimmed())
//: QDir::cleanPath(m_customerRepository + QDir::separator() + it->trimmed());
if (!m_hw) {
qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
<< "ERROR!!! m_hw == nullptr";
return false;
}
//if (!m_hw) {
// qCritical() << "(" << __func__ << ":" << __LINE__ << "):"
// << "ERROR!!! m_hw == nullptr";
// return false;
//}
QByteArray ba = loadBinaryDCFile(m_hw->dcDownloadFileName());
QByteArray ba = loadBinaryDCFile(m_dcFileName);
if (ba.size() > 0) {
uint16_t const totalBlocks = (((ba.size())%64)==0) ? (ba.size()/64) : (ba.size()/64)+1;
m_hw->dcDownloadSetTotalBlockNumber(totalBlocks);
m_totalBlocks = (((ba.size())%64)==0) ? (ba.size()/64) : (ba.size()/64)+1;
qInfo() << nextTimePoint().toUtf8().constData() << "blocks to send" << m_totalBlocks;
// fill last block of data to be sent with 0xFF
ba = ba.leftJustified(totalBlocks*64, (char)(0xFF));
ba = ba.leftJustified(m_totalBlocks*64, (char)(0xFF));
resetDeviceController();
if (startBootloader()) {
qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BYTES TO SEND TO DC" << ba.size();
qCritical() << "DownloadThread::run(): TOTAL NUMBER OF BLOCKS" << totalBlocks;
int currentBlock = 0;
DownloadResult res = DownloadResult::OK;
qCritical() << "64-byte block " << currentBlock;
while (res != DownloadResult::ERROR && currentBlock < totalBlocks) {
qInfo() << nextTimePoint().toUtf8().constData() << "64-byte block" << currentBlock;
while (res != DownloadResult::ERROR && currentBlock < m_totalBlocks) {
if ((res = sendNextAddress(currentBlock)) != DownloadResult::ERROR) {
if ((res = sendNextDataBlock(ba, currentBlock)) != DownloadResult::ERROR) {
m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
// TODO
// m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
currentBlock += 1;
}
} else break;
}
}
#if 0
qCritical() << "DownloadThread::run(): last 64-byte block %04d" << currentBlock;
int const rest = ba.size() % 64;
@@ -422,10 +466,15 @@ bool Update::doUpdate() {
m_hw->dcDownloadSetCurrentBlockNumber(currentBlock);
}
qCritical() << "DownloadThread::run(): last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK());
#endif
}
stopBootloader(); // there is no harm in stopping the bootloader even
// if starting the bootloader failed
qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-SUCCESS>";
return true;
}
qInfo() << nextTimePoint().toUtf8().constData() << "<DC-UPDATE-FAILURE>";
return false;
}