/*! * * Copyright (c) 2020-2024 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 * \author (last) Dara Navaei * \date (last) 06-May-2024 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #include "MessageInterpreter.h" // Qt #include // #include // Project #include "Logger.h" #include "format.h" #include "Settings.h" #include "DryDemoStates.h" using namespace Can; #define DISABLE_CHECKIN_LOG 0 #define DEBUG_SIGNAL(vID, vMODEL) // qDebug() << vID << vMODEL; // a macro to simplify the transmit message // would be better later to be replaced by a template method // like the notify method of received messages #define INTERPRET_TRANSMIT_MESSAGE() \ /*if ( ! length ) { logInvalidLength(vActionId); return false; }*/ \ vCanId = eChlid_HD_Sync; /*vMODEL::canid();*/ \ vPayload = Format::fromVariant(vData); \ /*LOG_APPED_MSG(vActionId, vMODEL::toString(vData));*/ \ /*DEBUG_SIGNAL(0, typeid(vMODEL).name())*/ // another version of the INTERPRET_TRANSMIT_MESSAGE for empty messages // same later improvements apply to this MACRO as well. #define INTERPRET_TRSMT_MT_MESSAGE(vMODEL) \ if ( length ) { logInvalidLength(vActionId); return false; } \ vCanId = vMODEL::canid(); \ vPayload = Format::fromVariant(vData); \ LOG_APPED_MSG(vActionId, vMODEL::toString(vData)); \ DEBUG_SIGNAL(0, typeid(vMODEL).name()) /*! * \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::notify * \details Checks and prepares the model with the Message Data * Regarding the type of message logs the message received. * Notifies observers by emitting the didActionReceive( < Data > ) signal * \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. */ template bool MessageInterpreter::notify(const Message &vMessage, QVariantList &vData) { bool ok = false; TModel tModel; if ( ! isValidMessage(vMessage) ) return ok; ok = tModel.fromByteArray(vMessage.data); tModel.toVariantList(vData); // disabled coco begin validated : Tested manually. This code will never go false // because the isValidMessage is catching errors. // only is checking here for developer safety if logic has changed. if ( ! ok ) return false; // disabled coco end emit didActionReceive(tModel.data()); //LOG_APPED_MSG("0","0"); DEBUG_SIGNAL(0, typeid(TModel).name()) return ok; } /*! * \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) const { Q_UNUSED(vMessage); /* if ( vMessage.actionId != vType ) { LOG_DEBUG(QString("Incorrect expected ID '%1', got '%2'") .arg(vType) .arg(vMessage.actionId) ); return false; } */ return true; } /*! * \brief MessageInterpreter::isPayloadLenValid * \details Checks if the Data length has been defined for this type of message * if not logs Undefined 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) const { Q_UNUSED(vMessage); /* int len = vMessage.data.length(); 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 ( len < payloadLen[vType] ) { LOG_DEBUG(QString("Incorrect data length (%2 of %3) for received Message with ID '%1'") .arg(mActionIdHexString) .arg(len) .arg(payloadLen[vType]) ); return false; } */ return true; } /*! * \brief MessageInterpreter::logInvalidLength * \details Logs invalid data length for the message type vActionId * \param vActionId - Message Type */ void MessageInterpreter::logInvalidLength() { quint8 vActionId = 0; QString mActionIdHexString = Format::toHexString(vActionId); LOG_DEBUG(QString("Incorrect data length for transmit message with ID '%1'") .arg(mActionIdHexString)); } /*! * \brief MessageInterpreter::validateMessage * \details Validate the message 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) const { if ( ! isType (vMessage) ) return false; if ( ! isPayloadLenValid(vMessage) ) 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 ( logUnhandledMessage(vMessage)) return; // the message is defined as unhandled and can still be handled and logged, return. if ( gDisableUnhandledReport ) return; // if the unhandled message error has been disabled, 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('.'))).toUpper(); LOG_DEBUG(logMessage); } /*! * \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 QVariantList &vData, QByteArray &vPayload, Can_Id &vCanId) { bool ok = true; if (vCanId == Can::Can_Id::eChlid_NONE ) vCanId = Can::Can_Id::eChlid_UI_HD ; vPayload.clear(); int length = vData.length(); Q_UNUSED(length); INTERPRET_TRANSMIT_MESSAGE(); qDebug() << "Len" << length << vData[0] << vPayload; return ok; } /*! * \brief MessageInterpreter::identifySource * \details Identifies the source of the message regarding the channel id * \return the source of the message in enum * \sa can::Can_Source */ Can_Source MessageInterpreter::identifySource(Can_Id vCanId, QString *vText) { switch (vCanId) { case eChlid_HD_DG : // 0x008, ///< HD => DG case eChlid_HD_UI : // 0x020, ///< HD => UI case eChlid_HD_Alarm: // 0x001, ///< HD alarm broadcast case eChlid_HD_Sync : // 0x040, ///< HD sync broadcast if (vText) *vText = "HD"; return Can_Source::eCan_HD; case eChlid_DG_HD : // 0x010, ///< DG => HD case eChlid_DG_UI : // 0x070, ///< DG => UI case eChlid_DG_Alarm: // 0x002, ///< DG alarm broadcast case eChlid_DG_Sync : // 0x080, ///< DG sync broadcast if (vText) *vText = "DG"; return Can_Source::eCan_DG; case eDialin_HD : // 0x400, ///< dialin => HD case eHD_Dialin : // 0x401, ///< HD => dialin case eDialin_DG : // 0x402, ///< dialin => DG case eDG_Dialin : // 0x403, ///< DG => dialin case eDialin_UI : // 0x404, ///< dialin => UI case eUI_Dialin : // 0x405, ///< UI => dialin if (vText) *vText = "DI"; return Can_Source::eCan_DI; default: if (vText) *vText = "XX"; return Can_Source::eCan_Unknown; } } /*! * \brief MessageInterpreter::identifyDestination * \details Identifies the destination of the message regarding the channel id for the acknowledges * \return the destination of the message in enum * \sa can::Can_Source * \sa can::Can_Id */ Can_Id MessageInterpreter::identifyDestination(Can_Id vCanId, QString *vText) { switch (vCanId) { case eChlid_HD_UI : // 0x020, ///< HD => UI case eChlid_HD_Alarm: // 0x001, ///< HD alarm broadcast case eChlid_HD_Sync : // 0x040, ///< HD sync broadcast if (vText) *vText = "HD"; return Can_Id::eChlid_UI_HD; case eChlid_DG_UI : // 0x070, ///< DG => UI case eChlid_DG_Alarm: // 0x002, ///< DG alarm broadcast case eChlid_DG_Sync : // 0x080, ///< DG sync broadcast if (vText) *vText = "DG"; return Can_Id::eChlid_UI_DG; case eDialin_HD : // 0x400, ///< dialin => HD case eHD_Dialin : // 0x401, ///< HD => dialin case eDialin_DG : // 0x402, ///< dialin => DG case eDG_Dialin : // 0x403, ///< DG => dialin case eDialin_UI : // 0x404, ///< dialin => UI case eUI_Dialin : // 0x405, ///< UI => dialin if (vText) *vText = "DI"; return Can_Id::eChlid_UI_Sync; default: if (vText) *vText = "XX"; return Can_Id::eChlid_UI_Sync; } } /*! * \brief MessageInterpreter::interpretMessage * \details This method will call appropriate message interpreter * for received messages from HD or DG regarding the Can_Id. * This function checks to identify the source of the message. * if it is from HD or DG then checks the standard handled messages with strongly typed models. * we still have a chance in each HD/DG interpreter to log the unhandled messages. * if the source is none of the HD or DG it doesn't go to the individual interpreters, * and the message will identified as unhandled immediately. * \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; if ( ! gLogUnhandledOnly ) { switch (identifySource(vMessage.can_id)) { case Can_Source::eCan_HD: ok = interpretMessage_HD(vMessage, vData); break; case Can_Source::eCan_DG: ok = interpretMessage_DG(vMessage, vData); break; default : printUnhandled (vMessage ); qDebug() << "2" <<__FUNCTION__; break; // ok is false, the individual interpreters for ?HD/DG should not be called, and it should be done here. } } else { Q_UNUSED (vData ); printUnhandled (vMessage ); qDebug() << "1" <<__FUNCTION__; } 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(); Q_UNUSED(vMessage); // TODO print unhandled? 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(); Q_UNUSED(vMessage); // TODO print unhandled? 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) ) return ok; QVariantList mData; int index = 0; Types::U32 mCanBUSFaultCount; ok = GetValue(vMessage.data, index, mCanBUSFaultCount); // disabled coco begin validated : developer safety if for any reason length of CanBUSFaultCount set to 0 if (ok) { // disabled coco end vData += mCanBUSFaultCount.value; } return ok; } /*! * \brief MessageInterpreter::updateUnhandledMessages * \return this method is converting the general settings messages/unhandled (Storage::Settings_Category_MessagesUnhandled) group of settings to the message interpreter specific map structure. * it is done for performance to keep the lookup table shorter and faster. * This method is called by the chain of events when Application controller is done reading the settings and emits it didSettingsDone, * which Message dispatcher is listening to will call this function in its signal handler slot. */ void MessageInterpreter::updateUnhandledMessages() { QString category = Storage::Settings_Category_MessagesUnhandled; QStringList groups = _Settings.groups(category); // DEBUG: qDebug() << groups; for(const auto &group: qAsConst(groups)) { bool ok; quint16 id = QString(group).toUInt(&ok,16); if (!ok) continue; _messageList[ id ] = _Settings.keys(category, group); // DEBUG: qDebug() << _Settings.keys(group); } } /*! * \brief MessageInterpreter::logUnhandledMessage * \details Search in the list of the unhandled messages and tries to extract the data and log it. * \param vMessage - the received message. * \return true if the message data can be extracted and logged. */ bool MessageInterpreter::logUnhandledMessage(const Message &vMessage) const { bool ok = false; quint16 id = vMessage.actionId; QString logString = QString("%1,").arg(id,4,16); qDebug() << "ID" << logString; if (_messageList.contains(id)) { ok = true; QStringList items = _messageList[id]; int index = 0; for ( int i = 0; i < items.count(); i++ ) { QString item = items[i]; if (i == 0) { QString ID; switch (identifySource(vMessage.can_id, &ID)) { case Can_Source::eCan_HD: logString += ID + ",~" + item; break; case Can_Source::eCan_DG: logString += ID + ",~" + item; break; case Can_Source::eCan_DI: logString += ID + ",~" + item; break; default : logString += ID + ",~" + item; break; } } else { if ( item == "F32" ) { Types::F32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S32" ) { Types::S32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U32" ) { Types::U32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S16" ) { Types::S16 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U16" ) { Types::U16 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S08" ) { Types::S08 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U08" ) { Types::U08 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "BOOL" ) { Types::U32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } } } LOG_APPED(logString); qDebug() << "Message" << logString; emit onUnhandledMsgReady(logString); } else { if ( gDisableUnhandledReport ) { // if the unhandled message error has been disabled, return. LOG_DEBUG(QString("Undefined unhandled message [%1]").arg(id, 0, 16)); } } return ok; }