/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, * IN PART OR IN WHOLE, * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * file messageinterpreter.cpp * date 12/11/2019 * author Behrouz NematiPour * */ #include "messageinterpreter.h" // Qt // Project using namespace Can; MessageInterpreter::MessageInterpreter(QObject *parent) : QObject(parent) { } bool MessageInterpreter::interpretMessage(const Can_Id vCan_Id, const Message &vMessage, GuiActionType &vActionId, QVariantList &vData) { bool ok = false; switch (vCan_Id) { case eChlid_HD: case eChlid_HD_Alarm: case eChlid_HD_Sync: ok = interpretMessage_HD(vMessage, vActionId, vData); default: break; } return ok; } bool MessageInterpreter::interpretMessage(const GuiActionType &vActionId, const QVariantList &vData, QByteArray &vPayload) { bool ok = true; vPayload.clear(); int l = vData.length(); quint8 ix = 0; switch (vActionId) { // notice we are in transmit mode case GuiActionType::PowerOff: ix = static_cast(GuiActionIndx::PowerOff_Response); if (l >= ix + 1) { quint8 tmp = vData[ix].toUInt(); vPayload += tmp; } else { QString mActionIdHexString = GuiActions::toHexString(vActionId); qDebug() << "ERROR :" << tr("Incorrect data for Message ID (UI) '%1'").arg(mActionIdHexString); ok = false; } break; case GuiActionType::KeepAlive: // Nothing needs to be done. // KeepAlive has No data. // Mentioned in the switch/case to be registered as a valid message. break; case GuiActionType::String: vPayload = fromVariant(vData[0]); break; default: QString mActionIdHexString = GuiActions::toHexString(vActionId); qDebug() << "ERROR :" << tr("Unknown Message ID (UI) '%1'").arg(mActionIdHexString); ok = false; break; } return ok; } QByteArray MessageInterpreter::fromVariant(const QVariant &vData) { QByteArray mData; if(vData.type() == QVariant::String) { mData += vData.toByteArray(); } else { mData += vData.toUInt(); } return mData; } bool MessageInterpreter::interpretMessage_HD(const Message &vMessage, GuiActionType &vActionId, QVariantList &vData) { bool ok = true; vActionId = vMessage.actionId; vData.clear(); switch (vActionId) { // notice we are in receive mode case GuiActionType::PowerOff: { quint8 mShowHide; ok = getPowerOffData(vMessage, mShowHide); if (ok) { vData += mShowHide; } break; } case GuiActionType::BloodFlow: { types::I32 i32 ; types::F32 f32_1; types::F32 f32_2; types::F32 f32_3; types::F32 f32_4; types::F32 f32_5; ok = getBloodFlowData(vMessage, i32, f32_1, f32_2, f32_3, f32_4, f32_5); if (ok) { vData += i32 .value; vData += f32_1.value; vData += f32_2.value; vData += f32_3.value; vData += f32_4.value; vData += f32_5.value; } break; } case GuiActionType::Alarm: break; default: { QString mActionIdHexString = GuiActions::toHexString(vMessage.actionId); qDebug() << "ERROR :" << tr("Unknown Message ID (HD) '%1'").arg(mActionIdHexString); ok = false; break; } } return ok; } bool MessageInterpreter::interpretMessage_DG(const Message &vMessage, GuiActionType &vActionId, QVariantList &vData) { Q_UNUSED(vMessage ); Q_UNUSED(vActionId); Q_UNUSED(vData ); // No data have been interpreted from DG yet return false; } bool MessageInterpreter::getBloodFlowData(const Message &vMessage, types::I32 &vI32, types::F32 &vF32_1, types::F32 &vF32_2, types::F32 &vF32_3, types::F32 &vF32_4, types::F32 &vF32_5) { if ( vMessage.actionId != GuiActionType::BloodFlow ) { return false; } if ( vMessage.data.length() < payloadLen[GuiActionType::BloodFlow] ) { QString mActionIdHexString = GuiActions::toHexString(vMessage.actionId); qDebug() << "ERROR :" << tr("Incorrect data for Message ID (HD) '%1'").arg(mActionIdHexString); return false; } int i = 0; int p = 0; int j = 0; p += 4; j = 0; while (i < p) { vI32.bytes[j] = vMessage.data[i]; j++; i++; } p += 4; j = 0; while (i < p) { vF32_1.bytes[j] = vMessage.data[i]; j++; i++; } p += 4; j = 0; while (i < p) { vF32_2.bytes[j] = vMessage.data[i]; j++; i++; } p += 4; j = 0; while (i < p) { vF32_3.bytes[j] = vMessage.data[i]; j++; i++; } p += 4; j = 0; while (i < p) { vF32_4.bytes[j] = vMessage.data[i]; j++; i++; } p += 4; j = 0; while (i < p) { vF32_5.bytes[j] = vMessage.data[i]; j++; i++; } return true; } bool MessageInterpreter::getPowerOffData(const Message &vMessage, quint8 &vShowHide) { bool ok = true; int l = vMessage.data.length(); quint8 ix = static_cast(GuiActionIndx::PowerOff_ShowHide); if (l >= ix + 1) { quint8 tmp = vMessage.data[ix]; vShowHide = tmp; } else { QString mActionIdHexString = GuiActions::toHexString(vMessage.actionId); qDebug() << "ERROR :" << tr("Incorrect data for Message ID (HD) '%1'").arg(mActionIdHexString); ok = false; } return ok; }