From 6a49cd6276062861a7a2ba5d95f3f243b30ff786 Mon Sep 17 00:00:00 2001 From: deXol Date: Thu, 17 Aug 2023 21:00:52 +0200 Subject: [PATCH] [BLE] Add option to continue recondition --- src/MPDeviceBleImpl.cpp | 21 ++-- src/MPDeviceBleImpl.h | 7 +- src/MainWindow.cpp | 31 ++++-- src/MainWindow.h | 4 +- src/MainWindow.ui | 205 +++++++++++++++++++++++++--------------- src/WSClient.cpp | 10 +- src/WSClient.h | 4 +- src/WSServerCon.cpp | 7 +- src/WSServerCon.h | 2 +- 9 files changed, 186 insertions(+), 105 deletions(-) diff --git a/src/MPDeviceBleImpl.cpp b/src/MPDeviceBleImpl.cpp index 4eb1ca39..e4612272 100644 --- a/src/MPDeviceBleImpl.cpp +++ b/src/MPDeviceBleImpl.cpp @@ -1229,10 +1229,10 @@ void MPDeviceBleImpl::getBattery() } } -void MPDeviceBleImpl::nihmReconditioning() +void MPDeviceBleImpl::nihmReconditioning(bool enableRestart) { auto *jobs = new AsyncJobs("NiMH Reconditioning", mpDev); - m_nimhResponse = ""; + m_nimhResultSec = 0; jobs->append(new MPCommandJob(mpDev, MPCmd::NIMH_RECONDITION, [this](const QByteArray &data, bool &) { @@ -1246,7 +1246,7 @@ void MPDeviceBleImpl::nihmReconditioning() const auto dischargeTimeUpper = bleProt->toIntFromLittleEndian(static_cast(payload[2]), static_cast(payload[3])); quint32 dischargeTime = dischargeTimeLower; dischargeTime |= static_cast((dischargeTimeUpper<<16)); - m_nimhResponse = QString::number(dischargeTime/1000.0); + m_nimhResultSec = dischargeTime/1000.0; if (std::numeric_limits::max() == dischargeTime) { qCritical() << "NiMH Recondition failed"; @@ -1255,19 +1255,28 @@ void MPDeviceBleImpl::nihmReconditioning() return true; })); - connect(jobs, &AsyncJobs::finished, [this](const QByteArray &data) + connect(jobs, &AsyncJobs::finished, [this, enableRestart](const QByteArray &data) { Q_UNUSED(data) qDebug() << "NiMH Reconditioning finished"; + bool restarted = false; + if (enableRestart && m_nimhResultSec < RECONDITION_RESTART_UNDER_SECS) + { + restarted = true; + } createAndAddCustomJob("NiMH Reconditioning finished job", - [this](){emit nimhReconditionFinished(true, m_nimhResponse);}); + [this, restarted, result = m_nimhResultSec](){emit nimhReconditionFinished(true, QString::number(result), restarted);}); + if (restarted) + { + nihmReconditioning(true); + } }); connect(jobs, &AsyncJobs::failed, [this](AsyncJob *) { qDebug() << "NiMH Reconditioning failed"; createAndAddCustomJob("NiMH Reconditioning failed job", - [this](){emit nimhReconditionFinished(false, m_nimhResponse);}); + [this](){emit nimhReconditionFinished(false, QString::number(m_nimhResultSec));}); }); mpDev->enqueueAndRunJob(jobs); diff --git a/src/MPDeviceBleImpl.h b/src/MPDeviceBleImpl.h index 13b658af..58104215 100644 --- a/src/MPDeviceBleImpl.h +++ b/src/MPDeviceBleImpl.h @@ -123,7 +123,7 @@ class MPDeviceBleImpl: public QObject void readBatteryPercent(const QByteArray& statusData); void getBattery(); - void nihmReconditioning(); + void nihmReconditioning(bool enableRestart); void getSecurityChallenge(const QString& key, const MessageHandlerCb &cb); void processDebugMsg(const QByteArray& data, bool& isDebugMsg); @@ -194,7 +194,7 @@ class MPDeviceBleImpl: public QObject void batteryPercentChanged(int batteryPct); void userCategoriesFetched(QJsonObject categories); void notesFetched(); - void nimhReconditionFinished(bool success, QString response); + void nimhReconditionFinished(bool success, QString response, bool restarted = false); void changeBleName(const QString& name); private slots: @@ -251,7 +251,7 @@ public slots: std::atomic_bool m_fetchingNotes = {false}; bool m_enforceLayout = false; - QString m_nimhResponse = ""; + quint32 m_nimhResultSec = 0; int m_miniFilePartCounter = 0; @@ -291,6 +291,7 @@ public slots: static constexpr int WAKEUP_DEVICE_BUNDLE_VERSION = 10; static constexpr int POINTED_TO_ADDR_SIZE = 2; static constexpr int PLATFORM_SERIAL_NUM_FIRST_BYTE = 16; + static constexpr int RECONDITION_RESTART_UNDER_SECS = 2500; const QByteArray DEFAULT_BUNDLE_PASSWORD = "\x63\x44\x31\x91\x3a\xfd\x23\xff\xb3\xac\x93\x69\x22\x5b\xf3\xc0"; const QString DEFAULT_BLE_NAME = "Mooltipass Mini BLE"; }; diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 79ab22a6..be45e507 100755 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1993,6 +1993,17 @@ bool MainWindow::validateSerialString(const QString &serialStr, uint &serialNum) return true; } +void MainWindow::displayReconditionWaitScreen(double lastElapsedTime) +{ + QString lastElapsedText = lastElapsedTime > 0 ? QString("

Recondition restarted. Last elapsed time: %1 seconds

").arg(lastElapsedTime) : ""; + ui->labelWait->show(); + ui->labelWait->setText(tr("

NiMH Recondition is in progress.

%1

Please wait.

").arg(lastElapsedText)); + ui->stackedWidget->setCurrentWidget(ui->pageWaiting); + ui->progressBarWait->hide(); + ui->labelProgressMessage->hide(); + updateTabButtons(); +} + void MainWindow::on_toolButton_clearBackupFilePath_released() { ui->lineEdit_dbBackupFilePath->clear(); @@ -2310,13 +2321,8 @@ void MainWindow::on_pushButtonNiMHRecondition_clicked() if (btn == QMessageBox::Ok) { - wsClient->sendNiMHReconditioning(); - ui->labelWait->show(); - ui->labelWait->setText(tr("

NiMH Recondition is in progress.

Please wait.

")); - ui->stackedWidget->setCurrentWidget(ui->pageWaiting); - ui->progressBarWait->hide(); - ui->labelProgressMessage->hide(); - updateTabButtons(); + wsClient->sendNiMHReconditioning(ui->checkBoxContinueRecondition->isChecked()); + displayReconditionWaitScreen(); } } @@ -2337,15 +2343,22 @@ void MainWindow::on_pushButtonSecurityValidate_clicked() wsClient->sendSecurityChallenge(challengeString); } -void MainWindow::onReconditionFinished(bool success, double dischargeTime) +void MainWindow::onReconditionFinished(bool success, double dischargeTime, bool restarted) { ui->stackedWidget->setCurrentWidget(ui->pageAdvanced); updatePage(); updateTabButtons(); if (success) { - QMessageBox::information(this, tr("NiMH Recondition Finished"), + if (!restarted) + { + QMessageBox::information(this, tr("NiMH Recondition Finished"), tr("Recondition finished in %1 seconds").arg(dischargeTime)); + } + else + { + displayReconditionWaitScreen(dischargeTime); + } } else { diff --git a/src/MainWindow.h b/src/MainWindow.h index 7608df0a..e515b6a0 100755 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -188,7 +188,7 @@ private slots: void on_pushButtonSecurityValidate_clicked(); - void onReconditionFinished(bool success, double dischargeTime); + void onReconditionFinished(bool success, double dischargeTime, bool restarted); void on_checkBoxEnforceBTLayout_stateChanged(int arg1); @@ -263,6 +263,8 @@ private slots: bool validateSerialString(const QString& serialStr, uint& serialNum); + void displayReconditionWaitScreen(double lastElapsedTime = 0); + Ui::MainWindow *ui = nullptr; QtAwesome* awesome; diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 1689f83e..7d0b7743 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -4880,6 +4880,19 @@ Hint: keep your mouse positioned over an option to get more details. 80 + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -4955,19 +4968,6 @@ Hint: keep your mouse positioned over an option to get more details. - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -5006,6 +5006,71 @@ Hint: keep your mouse positioned over an option to get more details. + + + + Reset card + + + Qt::AlignCenter + + + + + + QLayout::SetNoConstraint + + + 30 + + + 0 + + + 30 + + + 0 + + + 60 + + + 30 + + + + + + + Erase Inserted Card + + + true + + + + + + + ? + + + + + + + + + Reset Card + + + + + + + + @@ -5108,71 +5173,6 @@ Hint: keep your mouse positioned over an option to get more details. - - - - Reset card - - - Qt::AlignCenter - - - - - - QLayout::SetNoConstraint - - - 30 - - - 0 - - - 30 - - - 0 - - - 60 - - - 30 - - - - - - - Erase Inserted Card - - - true - - - - - - - ? - - - - - - - - - Reset Card - - - - - - - - @@ -5199,6 +5199,55 @@ Hint: keep your mouse positioned over an option to get more details. + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 165 + 20 + + + + + + + + Qt::LeftToRight + + + + + + Continues until nominal capacity reached + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 150 + 20 + + + + + + diff --git a/src/WSClient.cpp b/src/WSClient.cpp index 940e7cb2..64a75417 100644 --- a/src/WSClient.cpp +++ b/src/WSClient.cpp @@ -569,6 +569,7 @@ void WSClient::onTextMessageReceived(const QString &message) { QJsonObject o = rootobj["data"].toObject(); bool success = o["success"].toBool(); + bool restarted = o["restarted"].toBool(); bool ok = false; double dischargeTime = o["discharge_time"].toString().toDouble(&ok); if (!ok) @@ -576,7 +577,7 @@ void WSClient::onTextMessageReceived(const QString &message) qCritical() << "Cannot convert discarge time"; success = false; } - emit reconditionFinished(success, dischargeTime); + emit reconditionFinished(success, dischargeTime, restarted); } else if (rootobj["msg"] == "delete_fido_nodes") { @@ -913,9 +914,12 @@ void WSClient::sendCurrentCategory(int category) {"data", o}}); } -void WSClient::sendNiMHReconditioning() +void WSClient::sendNiMHReconditioning(bool enableRestart) { - sendJsonData({{ "msg", "nimh_reconditioning" }}); + QJsonObject o; + o["enable_restart"] = enableRestart; + sendJsonData({{ "msg", "nimh_reconditioning" }, + {"data", o}}); } void WSClient::sendSecurityChallenge(QString str) diff --git a/src/WSClient.h b/src/WSClient.h index 63aa20f6..11b68d8a 100644 --- a/src/WSClient.h +++ b/src/WSClient.h @@ -122,7 +122,7 @@ class WSClient: public QObject void sendBatteryRequest(); void sendBleNameRequest(); void sendCurrentCategory(int category); - void sendNiMHReconditioning(); + void sendNiMHReconditioning(bool enableRestart); void sendSecurityChallenge(QString str); void sendSetBleName(QString name); void sendSetSerialNumber(uint serialNum); @@ -177,7 +177,7 @@ class WSClient: public QObject void updateChargingStatus(int charging); void challengeResultReceived(QString result); void challengeResultFailed(); - void reconditionFinished(bool success, double dischargeTime); + void reconditionFinished(bool success, double dischargeTime, bool restarted); void deleteFidoNodesFailed(); void showExportPrompt(); void displayMiniImportWarning(); diff --git a/src/WSServerCon.cpp b/src/WSServerCon.cpp index 10a195bc..97d7cb5c 100644 --- a/src/WSServerCon.cpp +++ b/src/WSServerCon.cpp @@ -952,12 +952,13 @@ void WSServerCon::sendChargingStatus(bool charging) sendJsonMessage(oroot); } -void WSServerCon::sendNimhReconditionFinished(bool success, QString resposne) +void WSServerCon::sendNimhReconditionFinished(bool success, QString resposne, bool restarted) { QJsonObject oroot = { {"msg", "nimh_reconditioning"} }; QJsonObject data; data.insert("success", success); data.insert("discharge_time", resposne); + data.insert("restarted", restarted); oroot["data"] = data; sendJsonMessage(oroot); } @@ -1397,7 +1398,9 @@ void WSServerCon::processMessageBLE(QJsonObject root, const MPDeviceProgressCb & } else if (root["msg"] == "nimh_reconditioning") { - bleImpl->nihmReconditioning(); + QJsonObject o = root["data"].toObject(); + auto enableRestart = o.value("enable_restart").toBool(); + bleImpl->nihmReconditioning(enableRestart); } else if (root["msg"] == "request_security_challenge") { diff --git a/src/WSServerCon.h b/src/WSServerCon.h index dce1b9a4..1a551eb1 100644 --- a/src/WSServerCon.h +++ b/src/WSServerCon.h @@ -72,7 +72,7 @@ private slots: void sendBatteryPercent(int batteryPct); void sendChargingStatus(bool charging); - void sendNimhReconditionFinished(bool success, QString resposne); + void sendNimhReconditionFinished(bool success, QString resposne, bool restarted); void sendMiniImportWarning(); void sendBleName(const QString& name);