diff --git a/src/MPDevice.cpp b/src/MPDevice.cpp index 44a1ad72..faeca78d 100644 --- a/src/MPDevice.cpp +++ b/src/MPDevice.cpp @@ -401,56 +401,9 @@ void MPDevice::newDataRead(const QByteArray &data) */ if (isBLE()) { - bool isFirst = bleImpl->isFirstPacket(data); - if (isFirst) + if (!bleImpl->processReceivedData(data, dataReceived)) { - auto& cmd = commandQueue.head(); - cmd.responseSize = pMesProt->getMessageSize(data); - cmd.response.append(data); - if (commandQueue.head().responseSize > 60) - { - commandQueue.head().timerTimeout = new QTimer(this); - qCritical() << "Message with multiple packet, setting timout"; - connect(cmd.timerTimeout, &QTimer::timeout, [this]() - { - qCritical() << "Timout for multiple message expired"; - delete commandQueue.head().timerTimeout; - commandQueue.head().timerTimeout = nullptr; - bool done = true; - commandQueue.head().cb(false, QByteArray{}, done); - commandQueue.dequeue(); - sendDataDequeue(); - }); - cmd.timerTimeout->setInterval(CMD_LONG_MSG_TIMEOUT); - cmd.timerTimeout->start(); - } - } - - if (bleImpl->isLastPacket(data)) - { - if (!isFirst) - { - commandQueue.head().timerTimeout->stop(); - /** - * @brief EXTRA_INFO_SIZE - * Extra bytes of the first packet. - * In the last package only the remaining bytes - * of payload is appended. - */ - constexpr int EXTRA_INFO_SIZE = 6; - int fullResponseSize = commandQueue.head().responseSize + EXTRA_INFO_SIZE; - QByteArray responseData = commandQueue.head().response; - responseData.append(pMesProt->getFullPayload(data).left(fullResponseSize - responseData.size())); - dataReceived = responseData; - } - } - else - { - if (!isFirst) - { - commandQueue.head().response.append(pMesProt->getFullPayload(data)); - } - commandQueue.head().checkReturn = false; + //Expecting more packet return; } } diff --git a/src/MPDeviceBleImpl.cpp b/src/MPDeviceBleImpl.cpp index c2fcb052..98ff5780 100644 --- a/src/MPDeviceBleImpl.cpp +++ b/src/MPDeviceBleImpl.cpp @@ -350,6 +350,58 @@ void MPDeviceBleImpl::flipMessageBit(QByteArray &msg) flipBit(); } +bool MPDeviceBleImpl::processReceivedData(const QByteArray &data, QByteArray &dataReceived) +{ + const bool isFirst = isFirstPacket(data); + auto& cmd = mpDev->commandQueue.head(); + if (isFirst) + { + cmd.responseSize = bleProt->getMessageSize(data); + cmd.response.append(data); + /* + * When multiple packet from the device expected + * start a timer with 2 sec to detect resets and + * clean the command queue if it occurs. + */ + if (cmd.responseSize > FIRST_PACKET_PAYLOAD_SIZE) + { + cmd.timerTimeout = new QTimer(this); + connect(cmd.timerTimeout, &QTimer::timeout, this, &MPDeviceBleImpl::handleLongMessageTimeout); + cmd.timerTimeout->setInterval(LONG_MESSAGE_TIMEOUT_MS); + cmd.timerTimeout->start(); + } + } + + const bool isLast = isLastPacket(data); + if (isLast) + { + if (!isFirst) + { + cmd.timerTimeout->stop(); + /** + * @brief EXTRA_INFO_SIZE + * Extra bytes of the first packet. + * In the last package only the remaining bytes + * of payload is appended. + */ + constexpr int EXTRA_INFO_SIZE = 6; + int fullResponseSize = cmd.responseSize + EXTRA_INFO_SIZE; + QByteArray responseData = cmd.response; + responseData.append(bleProt->getFullPayload(data).left(fullResponseSize - responseData.size())); + dataReceived = responseData; + } + } + else + { + if (!isFirst) + { + cmd.response.append(bleProt->getFullPayload(data)); + } + cmd.checkReturn = false; + } + return isLast; +} + bool MPDeviceBleImpl::isAfterAuxFlash() { QSettings s; @@ -630,6 +682,18 @@ void MPDeviceBleImpl::readLanguages() mpDev->enqueueAndRunJob(jobs); } +void MPDeviceBleImpl::handleLongMessageTimeout() +{ + qWarning() << "Timout for multiple packet expired"; + auto& cmd = mpDev->commandQueue.head(); + delete cmd.timerTimeout; + cmd.timerTimeout = nullptr; + bool done = true; + cmd.cb(false, QByteArray{}, done); + mpDev->commandQueue.dequeue(); + mpDev->sendDataDequeue(); +} + QByteArray MPDeviceBleImpl::createStoreCredMessage(const BleCredential &cred) { return createCredentialMessage(cred.getAttributes()); diff --git a/src/MPDeviceBleImpl.h b/src/MPDeviceBleImpl.h index 98a46930..f0a1904e 100644 --- a/src/MPDeviceBleImpl.h +++ b/src/MPDeviceBleImpl.h @@ -66,6 +66,13 @@ class MPDeviceBleImpl: public QObject void setCurrentFlipBit(QByteArray &msg); void flipMessageBit(QVector &msg); void flipMessageBit(QByteArray &msg); + /** + * @brief processReceivedData + * @param data actual packet from the device + * @param dataReceived full message data + * @return true if data is last packet of the message + */ + bool processReceivedData(const QByteArray& data, QByteArray& dataReceived); bool isAfterAuxFlash(); @@ -96,6 +103,9 @@ class MPDeviceBleImpl: public QObject void bleDeviceLanguage(const QJsonObject& langs); void bleKeyboardLayout(const QJsonObject& layouts); +private slots: + void handleLongMessageTimeout(); + private: void checkDataFlash(const QByteArray &data, QElapsedTimer *timer, AsyncJobs *jobs, QString filePath, const MPDeviceProgressCb &cbProgress); void sendBundleToDevice(QString filePath, AsyncJobs *jobs, const MPDeviceProgressCb &cbProgress); @@ -130,6 +140,8 @@ class MPDeviceBleImpl: public QObject static constexpr int USER_CATEGORY_LENGTH = 66; static constexpr int FAV_DATA_SIZE = 4; static constexpr int FAV_NUMBER = 50; + static constexpr int LONG_MESSAGE_TIMEOUT_MS = 2000; + static constexpr int FIRST_PACKET_PAYLOAD_SIZE = 58; const QString AFTER_AUX_FLASH_SETTING = "settings/after_aux_flash"; };