/*! * * 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 tst_acknow.cpp * \author (last) Behrouz NematiPour * \date (last) 10-Aug-2021 * \author (original) Behrouz NematiPour * \date (original) 21-Jan-2020 * */ #include "tst_acknow.h" // Project #include "CanInterface.h" #include "FrameInterface.h" #include "MessageAcknowModel.h" #define DEBUG_ACKBACK_HD_TO_UI using namespace Can; tst_acknow::tst_acknow(QObject *parent) { Q_UNUSED(parent) } /*! * \brief tst_acknow::init * \details this method should only initialize the can interfaces and is not for test * bus needs to pass the initialization. */ void tst_acknow::init() { _emited = false; _Logger.init(); _CanInterface.init(); _CanInterface.enableConsoleOut(true); QString mTr = tr("Connected"); QCOMPARE(_CanInterface.status().left(mTr.length()), mTr); _CanInterface.onFrameReceive (); _FrameInterface .init(); _MessageDispatcher .init(); _MessageDispatcher .enableConsoleOut(true); connect(&_MessageDispatcher , SIGNAL(didFrameTransmit(Can_Id , const QByteArray &)), this , SLOT( onFrameTransmit(Can_Id , const QByteArray &))); connect(&_MessageDispatcher , SIGNAL(didActionReceive(GuiActionType , const QVariantList &)), this , SLOT( onActionReceive(GuiActionType , const QVariantList &))); connect(&_MessageAcknowModel, SIGNAL(didFramesTransmit(Can_Id, Sequence, const FrameList &)), this , SLOT( onFramesTransmit(Can_Id, Sequence, const FrameList &))); connect(&_MessageAcknowModel, SIGNAL(didFailedTransmit(Sequence)), this , SLOT( onFailedTransmit(Sequence))); } void tst_acknow::tst_AcknowModel_Init() { QVERIFY( _MessageAcknowModel.init() ); } /*! * \brief tst_acknow::tst_AcknowModel_Receive_Normal * \details UI receives a message which does not require Acknow */ void tst_acknow::tst_AcknowModel_Receive_Normal() { _action = Gui::GuiActionType::ID_PowerOff; _data = {0}; QCanBusFrame mFrame; QString mPayload; mFrame.setFrameId(Can_Id::eChlid_HD_UI); mPayload = "A5.01.00.01.00.01.00.38"; mFrame.setPayload(QByteArray::fromHex(mPayload.remove(QLatin1Char('.')).toLatin1())); emit _CanInterface.didFrameReceive(mFrame); QVERIFY(_emited); } /*! * \brief tst_acknow::tst_AcknowModel_Receive_Acknow * \details UI receives a message which requires Acknow * UI should send the Ack Back */ void tst_acknow::tst_AcknowModel_Receive_Acknow() { _expected = "A5.01.00.FF.FF.00.19.00"; _action = Gui::GuiActionType::ID_PowerOff; _data = {0}; QCanBusFrame mFrame; QString mPayload; mFrame.setFrameId(Can_Id::eChlid_HD_UI); mPayload = "A5.FF.FF.01.00.01.00.96"; mFrame.setPayload(QByteArray::fromHex(mPayload.remove(QLatin1Char('.')).toLatin1())); emit _CanInterface.didFrameReceive(mFrame); QVERIFY(_emited); } /*! * \brief tst_acknow::tst_AcknowModel_Transmit_Normal * \details Transmit a normal message */ void tst_acknow::tst_AcknowModel_Transmit_Normal() { _expected = "A5.02.00.07.00.00.74.00"; _MessageDispatcher.onActionTransmit(Gui::GuiActionType::ID_KeepAlive, {}); QVERIFY(_emited); } /*! * \brief tst_acknow::tst_AcknowModel_Transmit_Acknow * \details Transmit a message which needs a */ void tst_acknow::tst_AcknowModel_Transmit_Acknow() { QCOMPARE( _MessageAcknowModel._acknowList.count(), 0 ); _MessageDispatcher._needsAcknow.append(Gui::GuiActionType::ID_KeepAlive); _expected = "A5.FD.FF.07.00.00.30.00"; Sequence sequence = 3; _MessageDispatcher.actionTransmit(Gui::GuiActionType::ID_KeepAlive, {}, sequence); QCOMPARE( _MessageAcknowModel._acknowList.count(), 1 ); QVERIFY(_emited); _MessageDispatcher._needsAcknow.removeOne(Gui::GuiActionType::ID_KeepAlive); } /*! * \brief tst_acknow::tst_AcknowModel_Receive_Normal * \details UI receives a message which does not require Acknow */ void tst_acknow::tst_AcknowModel_onAcknowReceive_Correct() { QCOMPARE( _MessageAcknowModel._acknowList.count(), 1 ); _action = Gui::GuiActionType::ID_KeepAlive; _data = {0}; QCanBusFrame mFrame; QString mPayload; mFrame.setFrameId(Can_Id::eChlid_HD_UI); mPayload = "A5.03.00.FF.FF.01.00.91"; mFrame.setPayload(QByteArray::fromHex(mPayload.remove(QLatin1Char('.')).toLatin1())); qDebug() << "#" << mFrame.toString(); emit _CanInterface.didFrameReceive(mFrame); QCOMPARE( _MessageAcknowModel._acknowList.count(), 0 ); } /*! * \brief tst_acknow::tst_AcknowModel_onAcknowReceive_Incorrect * \details Tries to remove a seq from acknow model which is not expected * (there was no message send with this seq which required acknow) * and the seq should not exist before and after removal * this is a test for qt hash test used for acknow model * \param vSequence */ void tst_acknow::tst_AcknowModel_onAcknowReceive_Incorrect() { QVERIFY( ! _MessageAcknowModel._acknowList.contains(9999)); _MessageAcknowModel.onAcknowReceive(9999); QVERIFY( ! _MessageAcknowModel._acknowList.contains(9999)); } void tst_acknow::cleanupTestCase() { _CanInterface.quitDevice(); QString mTr = tr("Disconnected"); QCOMPARE(_CanInterface.status().left(mTr.length()), mTr); } void tst_acknow::onFrameTransmit(Can_Id vCan_Id, const QByteArray &vPayload) { Q_UNUSED(vCan_Id) _emited = true; QCOMPARE(Format::toHexString(vPayload), _expected); } void tst_acknow::onActionReceive(Gui::GuiActionType vAction, const QVariantList &vData) { _emited = true; QVERIFY(vAction == _action); for (int i = 0; i < vData.length(); i++) { if (! strcmp(vData[i].typeName(), "float")) { float f1 = vData[i].toFloat(); float f2 = _data[i].toFloat(); QVERIFY(Types::floatCompare(f1, f2)); } else { QVERIFY(vData[i] == _data[i]); } } } void tst_acknow::onFramesTransmit(Can_Id vCan_Id, Sequence vSequence, const FrameList &vFrameList) { qDebug() << quint16(vCan_Id) << vSequence << vFrameList; } void tst_acknow::onFailedTransmit(Sequence) { qDebug() << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "; }