/*! * * 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 #include // #include // Project #include "logger.h" #include "format.h" using namespace Can; #define DEBUG_RECEIVE_SIGNAL(vID, vMODEL) // Debug() << #vID << #vMODEL; // This define helps to prevent having multiple overloaded functions for each model #define EMIT_RECEIVE_SIGNAL(vID, vMODEL) { \ vMODEL mModel; \ ok = prepareData(vMessage, vID, mModel, vData); \ if ( ! ok ) return false; \ emit didActionReceive(mModel.data()); \ logReceivedMessage(mModel); \ DEBUG_RECEIVE_SIGNAL(vID, vMODEL) \ } /*! * \brief MessageInterpreter::MessageInterpreter * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ MessageInterpreter::MessageInterpreter(QObject *parent) : QObject(parent) { } /*! * \brief MessageInterpreter::isType * \details Checks if this is the message intended to be * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on correct type */ bool MessageInterpreter::isType(const Message &vMessage, Gui::GuiActionType vType) const { if ( vMessage.actionId != vType ) { return false; } return true; } /*! * \brief MessageInterpreter::isPayloadLenValid * \details Checks if the Data length has been defined for this type od messsage * if not logs Undefind Data Length error * if defined checks if the correct length of data is provided for this type of message. * if not logs Incorrect Data Length error * otherwise returns true * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on correct data length for the type vType */ bool MessageInterpreter::isPayloadLenValid(const Message &vMessage, Gui::GuiActionType vType) const { QString mActionIdHexString = Format::toHexString(vMessage.actionId); if ( ! payloadLen.contains(vType) ) { LOG_DEBUG(QString("Undefined data length for received Message with ID '%1'").arg(mActionIdHexString)); return false; } if ( vMessage.data.length() < payloadLen[vType] ) { LOG_DEBUG(QString("Incorrect data length for received Message with ID '%1'").arg(mActionIdHexString)); return false; } return true; } void MessageInterpreter::logInvalidLength(const Gui::GuiActionType &vActionId) { QString mActionIdHexString = Format::toHexString(vActionId); LOG_DEBUG(QString("Incorrect data length for trsansmit Message with ID '%1'") .arg(mActionIdHexString)); } /*! * \brief MessageInterpreter::validateMessage * \details Validate the messgae by checking its type and data * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on valid massage */ bool MessageInterpreter::isValidMessage(const Message &vMessage, Gui::GuiActionType vType) const { if ( ! isType (vMessage, vType) ) return false; if ( ! isPayloadLenValid(vMessage, vType) ) return false; return true; } /*! * \brief MessageInterpreter::printUnhandled * \details Prints out the formatted string of the vMessage of type Message * In case the Message ID of received CANBUS message * is known to the interpreter but has not been handled/implemented. * \param vMessage - The message contains Unhandled Message ID */ void MessageInterpreter::printUnhandled(const Message &vMessage) const { if ( gDisableUnhandledReport ) return; QString mActionIdHexString = Format::toHexString(vMessage.actionId, false, eLenMessageIDDigits); QString logMessage = tr("Unhandled Message ID (HD)") + '\n' + QString("%1 # %2 %3") .arg(int(vMessage.can_id), 3, 16, QChar('0')) .arg(mActionIdHexString) .arg(QString(vMessage.data.toHex('.'))); LOG_DEBUG(logMessage); } /*! * \brief MessageInterpreter::prepareData * \details Checks and Prepaires the model with the Message Data * \param vMessage - The Denali message * \param vID - The Message ID to be checked against * \param vModel - The appropriate model for the Message Data * \param vData - A QVariant list of the Message Data which will be used for debugging if needed. * \return true on successful check and prepare. */ bool MessageInterpreter::prepareData(const Message &vMessage, Gui::GuiActionType vID, Model::MAbstract &vModel, QVariantList &vData) { bool ok = false; if ( ! isValidMessage(vMessage, vID) ) return ok; ok = vModel.fromByteArray(vMessage.data); vModel.toVariantList(vData); return ok; } /*! * \brief MessageInterpreter::logReceived * \details Regarding the type of message logs the message recived. * \param vModel - the MAbstract model type */ void MessageInterpreter::logReceivedMessage(const Model::MAbstract &vModel) { switch (vModel.typeText()) { case Model::MAbstract::Type_Enum::eDatum: LOG_DATUM(vModel.toString()); break; case Model::MAbstract::Type_Enum::eEvent: LOG_EVENT(vModel.toString()); break; } } /*! * \brief MessageInterpreter::interpretMessage * \details This method will be called * to interpret messages from UI regarding vActionId. * \param vActionId - The ActionID of type GuiActionType * to be interpreted to hex representation of Message ID. * \param vData - The data which has to be sent over the CANBUS. * \param vPayload - The Payload of the frame of Type QByteArray of hex values * Which has been interpreted from vData of Type QVariantList * \return true if the vActionId is valid. * This return value will be used later for error handling. */ bool MessageInterpreter::interpretMessage(const Gui::GuiActionType &vActionId, const QVariantList &vData, QByteArray &vPayload) { bool ok = true; QString mSenderID = "UI,"; vPayload.clear(); int count = vData.length(); switch (vActionId) { // notice we are in transmit mode case Gui::GuiActionType::ID_Acknow: break; // No data, Just registered case Gui::GuiActionType::ID_KeepAlive: if ( count ) { // this message has a variable length vPayload = Format::fromVariant(vData[0]); } LOG_EVENT(mSenderID + QString("CheckIn")); break; case Gui::GuiActionType::ID_RawData: if ( count ) { // this message has a variable length vPayload = Format::fromVariant(vData[0]); } LOG_EVENT(mSenderID + QString("RawData")); break; case Gui::GuiActionType::ID_PowerOff: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload += Format::fromVariant(vData); LOG_EVENT(AdjustPowerOffRequestData::toString(vData)); break; case Gui::GuiActionType::ID_AdjustBloodDialysateReq: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload = Format::fromVariant(vData); LOG_EVENT(AdjustBloodDialysateRequestData::toString(vData)); break; case Gui::GuiActionType::ID_AdjustDurationReq: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload = Format::fromVariant(vData); LOG_EVENT(AdjustDurationRequestData::toString(vData)); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationStateReq: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload = Format::fromVariant(vData); LOG_EVENT(AdjustUltrafiltrationStateRequestData::toString(vData)); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationEditReq: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload = Format::fromVariant(vData); LOG_EVENT(AdjustUltrafiltrationEditRequestData::toString(vData)); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmReq: if ( ! count ) { logInvalidLength(vActionId); return false; } vPayload = Format::fromVariant(vData); LOG_EVENT(AdjustUltrafiltrationConfirmRequestData::toString(vData)); break; default: QString mActionIdHexString = Format::toHexString(vActionId); LOG_DEBUG(mSenderID + tr("Unknown transmit Message with ID '%1'").arg(mActionIdHexString)); ok = false; break; } return ok; } /*! * \brief MessageInterpreter::interpretMessage * \details This method will call appropriate message interpreter * for received messages from HD or DG regarding the Can_Id. * \param vCan_Id - The Channel Id of the CANBUS frame. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBUS Message Payload * regarding each Message Id definition. * \return true if the message channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage(const Message &vMessage, QVariantList &vData) { bool ok = false; switch (vMessage.can_id) { case eChlid_HD_UI : case eChlid_HD_Alarm: case eChlid_HD_Sync : ok = interpretMessage_HD(vMessage, vData); break; // coco-begin-validated: Is a placeholder and There is no definition/implementation of DG communication with UI. case eChlid_DG_UI : case eChlid_DG_Alarm: case eChlid_DG_Sync : ok = interpretMessage_DG(vMessage, vData); break; default: break; // coco-end } return ok; } /*! * \brief MessageInterpreter::interpretMessage_HD * \details This method will be called * for received messages from HD to interpret the vMessage of type Message * to vData of type QVariantList which UI understands regarding the Can_Id. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBUS Message Payload * in vMessage of type Message regarding each Message Id definition. * \return true if the message CANBUS channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage_HD(const Message &vMessage, QVariantList &vData) { bool ok = false; vData.clear(); switch (vMessage.actionId) { // notice we are in receive mode // ----- Debug case Gui::GuiActionType::ID_CANBusFaultCount : ok = canbusFaultCountData (vMessage, vData); break; // ----- Datum case Gui::GuiActionType::ID_TreatmentTime : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_TreatmentTime , Model::MTreatmentTime ); break; case Gui::GuiActionType::ID_BloodFlow : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_BloodFlow , Model::MBloodFlow ); break; case Gui::GuiActionType::ID_DialysateInletFlow : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DialysateInletFlow , Model::MDialysateFlow ); break; case Gui::GuiActionType::ID_DialysateOutletFlow : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DialysateOutletFlow , Model::MOutletFlow ); break; case Gui::GuiActionType::ID_TreatmentRanges : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_TreatmentRanges , Model::MTreatmentRanges ); break; case Gui::GuiActionType::ID_PressureOcclusion : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_PressureOcclusion , Model::MPressureOcclusion ); break; case Gui::GuiActionType::ID_TreatmentStates : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_TreatmentStates , Model::MTreatmentStates ); break; // ----- Events case Gui::GuiActionType::ID_HDOperationModeData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_HDOperationModeData , Model::MHDOperationMode ); break; case Gui::GuiActionType::ID_HDDebugText : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_HDDebugText , Model::MHDDebugText ); break; case Gui::GuiActionType::ID_Acknow : ok = true; break; // Needs more investigation for EMIT_RECEIVE_SIGNAL consistency case Gui::GuiActionType::ID_PowerOff : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_PowerOff , Model::MPowerOff ); break; case Gui::GuiActionType::ID_ShuttingDown : ok = true; LOG_EVENT("HD,ShuttingDown"); break; // Needs more investigation for EMIT_RECEIVE_SIGNAL consistency case Gui::GuiActionType::ID_AlarmStatus : ok = alarmStatus (vMessage, vData); break; case Gui::GuiActionType::ID_AlarmTriggered : ok = alarmTriggered (vMessage, vData); break; case Gui::GuiActionType::ID_AlarmCleared : ok = alarmCleared (vMessage, vData); break; // Adjustment Response Messages case Gui::GuiActionType::ID_AdjustDurationRsp : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_AdjustDurationRsp , Model::MAdjustDurationResponse ); break; case Gui::GuiActionType::ID_AdjustBloodDialysateRsp : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_AdjustBloodDialysateRsp , Model::MAdjustBloodDialysateResponse ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationStateReq : ok = adjustUltrafiltrationState (vMessage, vData); break; // Needs more investigation for EMIT_RECEIVE_SIGNAL consistency case Gui::GuiActionType::ID_AdjustUltrafiltrationEditRsp : ok = adjustUltrafiltrationEdit (vMessage, vData); break; // Needs more investigation for EMIT_RECEIVE_SIGNAL consistency case Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmRsp : ok = adjustUltrafiltrationConfirm (vMessage, vData); break; // Needs more investigation for EMIT_RECEIVE_SIGNAL consistency // unhandles messages: these will only be logged as received message // there has nothing been defined for these messages. default : printUnhandled (vMessage ); break; } return ok; } /*! * \brief MessageInterpreter::interpretMessage_DG * \details This method will be called * for received messages from DG to interpret the vMessage of type Message * to vData of type QVariantList which UI understands regarding the Can_Id. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBUS Message Payload * in vMessage of type Message regarding each Message Id definition. * \return true if the message CANBUS channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage_DG(const Message &vMessage, QVariantList &vData) { bool ok = false; vData.clear(); switch (vMessage.actionId) { // notice we are in receive mode case Gui::GuiActionType::ID_DGCheckIn: ok = true; LOG_EVENT(QString("DG,CheckIn," + QVariant(vData).toStringList().join(','))); break; case Gui::GuiActionType::ID_DGROPumpData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGROPumpData , Model::MDGROPump ); break; case Gui::GuiActionType::ID_DGPressuresData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGPressuresData , Model::MDGPressures ); break; case Gui::GuiActionType::ID_DGDrainPumpData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGDrainPumpData , Model::MDGDrainPump ); break; case Gui::GuiActionType::ID_DGOperationModeData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGOperationModeData , Model::MDGOperationMode ); break; case Gui::GuiActionType::ID_DGReservoirData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGReservoirData , Model::MDGReservoir ); break; case Gui::GuiActionType::ID_DGValvesStatesData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGValvesStatesData , Model::MDGValvesStates ); break; case Gui::GuiActionType::ID_DGHeatersData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGHeatersData , Model::MDGHeaters ); break; case Gui::GuiActionType::ID_DGLoadCellReadingsData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGLoadCellReadingsData , Model::MDGLoadCellReadings ); break; case Gui::GuiActionType::ID_DGTemperaturesData : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGTemperaturesData , Model::MDGTemperatures ); break; case Gui::GuiActionType::ID_DGDebugText : EMIT_RECEIVE_SIGNAL(Gui::GuiActionType::ID_DGDebugText , Model::MDGDebugText ); break; // unhandles messages: these will only be logged as received message // there has nothing been defined for these messages. default: printUnhandled (vMessage); break; } return ok; } // ---------- ---------- Message handlers ---------- ---------- // // ---------- ---------- ---------- ---------- ---------- Debug ---------- ---------- ---------- ---------- ---------- // /*! * \brief MessageInterpreter::canbusFaultCountData * \details This method interprets Fault Count message data * in vMessage of type Message. This message is only used for debugging purposes. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Fault Count data * \return true if the data can be extracted as defined for Fault Count Message ID */ bool MessageInterpreter::canbusFaultCountData(const Message &vMessage, QVariantList &vData) { // TODO : review other methods bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_CANBusFaultCount) ) return ok; QVariantList mData; int index = 0; Types::U32 mCanBUSFaultCount; ok = GetValue(vMessage.data, index, mCanBUSFaultCount); // coco begin validated : developer safety if for any reason length of CanBUSFaultCount set to 0 if (ok) { // coco end vData += mCanBUSFaultCount.value; } return ok; } // ---------- ---------- ---------- ---------- ---------- Events ---------- ---------- ---------- ---------- ---------- // // ---------- ---------- ---------- ---------- ---------- - HD ---------- ---------- ---------- ---------- ---------- // /*! * \brief MessageInterpreter::alarmStatus * \details This method interprets Alarm Status message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Alarm Status data * \return true if the data can be extracted as defined for Alarm Status Message ID */ bool MessageInterpreter::alarmStatus(const Message &vMessage, QVariantList &vData) { // TODO : review other methods bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AlarmStatus) ) return ok; Model::MAlarmStatus mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; // --- an example of unit test --- // // Types::Flags flag; // int i = 0; // QByteArray ba; // ba += 0x83; ba += 0xf8; ba += 0x28; ba += 0xa1; // Types::getBits(ba, i, flag, 32); // qDebug() << '@' << flag << flag.toString() << ba; } /*! * \brief MessageInterpreter::alarmTriggered * \details This method interprets Alarm Triggered message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Alarm Triggered data * \return true if the data can be extracted as defined for Alarm Triggered Message ID */ bool MessageInterpreter::alarmTriggered(const Message &vMessage, QVariantList &vData) { // TODO : review other methods bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AlarmTriggered) ) return ok; Model::MAlarmTriggered mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } /*! * \brief MessageInterpreter::alarmCleared * \details This method interprets Alarm Cleared message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Alarm Cleared data * \return true if the data can be extracted as defined for Alarm Cleared Message ID */ bool MessageInterpreter::alarmCleared(const Message &vMessage, QVariantList &vData) { // TODO : review other methods bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AlarmCleared) ) return ok; Model::MAlarmCleared mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } // ---------- ---------- ---------- ---------- ---------- Adjustments ---------- ---------- ---------- ---------- ---------- // /*! * \brief MessageInterpreter::ultrafiltrationState * \details This method interprets Treatment Ultrafiltration State Adjustment Response message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Treatment Ultrafiltration State Adjustment Response data * \return true if the data can be extracted as defined for Treatment Ultrafiltration State Adjustment Response Message ID */ bool MessageInterpreter::adjustUltrafiltrationState(const Message &vMessage, QVariantList &vData) { bool ok = false; // TODO : In HD the Ultrafiltration State change shall have the rejection reason like other responses. if ( ! isType (vMessage, Gui::GuiActionType::ID_AdjustUltrafiltrationStateReq) ) return ok; if ( ! isPayloadLenValid(vMessage, Gui::GuiActionType::ID_AcknowGeneric ) ) return ok; // This is an exception It has to be changed. Model::MAdjustUltrafiltrationStateResponse mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } /*! * \brief MessageInterpreter::adjustUltrafiltrationEditData * \details This method interprets Treatment Ultrafiltration Volume Adjustment Response message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Treatment Ultrafiltration Volume Adjustment Response data * \return true if the data can be extracted as defined for Treatment Ultrafiltration Volume Adjustment Response Message ID */ bool MessageInterpreter::adjustUltrafiltrationEdit(const Message &vMessage, QVariantList &vData) { bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AdjustUltrafiltrationEditRsp) ) return ok; Model::MAdjustUltrafiltrationEditResponse mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } /*! * \brief MessageInterpreter::adjustUltrafiltrationConfirmData * \details This method interprets Treatment Ultrafiltration Volume Adjustment Confirm Response message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Treatment Ultrafiltration Volume Adjustment Confirm Response data * \return true if the data can be extracted as defined for Treatment Ultrafiltration Volume Adjustment Confirm Response Message ID */ bool MessageInterpreter::adjustUltrafiltrationConfirm(const Message &vMessage, QVariantList &vData) { bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmRsp) ) return ok; Model::MAdjustUltrafiltrationConfirmResponse mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; }