Skip to content

Commit

Permalink
[BLE] Moved received data processing to MPDeviceBleImpl, added commen…
Browse files Browse the repository at this point in the history
…ts and constants
  • Loading branch information
deXol committed Nov 16, 2019
1 parent 119da63 commit 8e947b0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
51 changes: 2 additions & 49 deletions src/MPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
64 changes: 64 additions & 0 deletions src/MPDeviceBleImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
12 changes: 12 additions & 0 deletions src/MPDeviceBleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class MPDeviceBleImpl: public QObject
void setCurrentFlipBit(QByteArray &msg);
void flipMessageBit(QVector<QByteArray> &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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";
};

Expand Down

0 comments on commit 8e947b0

Please sign in to comment.