Code cleanup

This commit is contained in:
2023-01-28 09:01:02 -05:00
parent eeb1d04f9b
commit 74b6a23045
24 changed files with 1548 additions and 1442 deletions
+118
View File
@@ -0,0 +1,118 @@
#include "CANAnalyzer.h"
#include "GeneralMessagesLogDispatcher.h"
#include "CANDatabase.h"
#include "OtarcikCan.h"
CCANAnalyzer::CCANAnalyzer(QObject *parent) : QObject(parent)
{
mCANDriverIF = new CPCANInterface;
mIsCANInitialized = false;
mDevicePtr = 0;
mDeviceMessagesList = 0;
mCANPollPeriod = 0;
mCANReadTimer = new QTimer;
mCANReadTimer->setInterval(1000);
mCANReadTimer->setSingleShot(true);
connect(mCANReadTimer,SIGNAL(timeout()),this,SLOT(CANTimerExpired()));
}
CCANAnalyzer::~CCANAnalyzer()
{
mCANDriverIF->DeInit();
delete mCANDriverIF;
delete mCANReadTimer;
}
int CCANAnalyzer::Init(TPCANHandle CANDeviceChannel, TPCANBaudrate CANDeviceBaudrate, QList<CCANMessage *> *MsgList, unsigned int PollPeriod)
{
if(MsgList == 0)
{
return RET_GENERAL_ERROR;
}
if(PollPeriod < 200)
{
PollPeriod = 200;
qDebug("CANAnalyzer:: Trying to init with poll period lower tan 200ms");
}
mCANDeviceChannel = CANDeviceChannel;
mCANPollPeriod = PollPeriod;
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Démarrage d'un module CAN...");
mDeviceMessagesList = MsgList;
if(mCANDriverIF->Init(mCANDeviceChannel,CANDeviceBaudrate) != RET_OK)
{
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Impossible d'initialiser le matériel CAN",true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
mIsCANInitialized = false;
return RET_GENERAL_ERROR;
}
else
{
mCANReadTimer->start(mCANPollPeriod);
mIsCANInitialized = true;
}
return RET_OK;
}
int CCANAnalyzer::ReadCAN()
{
// TPCANMsg CANMsg;
// TPCANTimestamp CANTimeStamp;
// // We execute the "Read" function of the PCANBasic
// TPCANStatus stsResult = CAN_Read(PcanHandle, &CANMsg, &CANTimeStamp);
// if (stsResult != PCAN_ERROR_QRCVEMPTY)
// {
// // We process the received message
// ProcessMessageCan(CANMsg, CANTimeStamp);
// }
// return stsResult;
return RET_OK;
}
void CCANAnalyzer::CANTimerExpired()
{
QList<CCANMessage *> NewMessagesList = mCANDriverIF->ReadCANFullBuffer(mCANDeviceChannel);
for(int i = 0 ; i < NewMessagesList.size(); i++)
{
for(int j = 0; j < mDeviceMessagesList->size(); j++)
{
if(NewMessagesList.at(i)->mCANMsgID == mDeviceMessagesList->at(j)->mCANMsgID)
{
mDeviceMessagesList->at(j)->UpdateValue(NewMessagesList.at(i));
}
}
}
//Don't leak!!
while(!NewMessagesList.isEmpty())
{
delete NewMessagesList.takeFirst();
}
mDevicePtr->NewMessageParsed();
if(NewMessagesList.isEmpty())
{
mCANReadTimer->start();
return;
}
NewMessagesList.clear();
mCANReadTimer->start();
// mProgramPtr->UpdateCANViewerDataRequest(mLastMessagesList);
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef CANANALYZER_H
#define CANANALYZER_H
#include <QObject>
#include "defines.h"
#include <QTimer>
#include <QList>
#include "CANMessage.h"
#include "PCANInterface.h"
class CCANDevice;
class CCANAnalyzer : public QObject
{
Q_OBJECT
public:
explicit CCANAnalyzer(QObject *parent = 0);
~CCANAnalyzer();
CCANDevice *mDevicePtr;
CPCANInterface *mCANDriverIF;
QTimer *mCANReadTimer;
unsigned int mCANPollPeriod;
int Init(TPCANHandle CANDeviceChannel, TPCANBaudrate CANDeviceBaudrate, QList<CCANMessage *> *MsgList, unsigned int PollPeriod);
int ReadCAN();
QList<CCANMessage *> *mDeviceMessagesList;
private:
bool mIsCANInitialized;
TPCANHandle mCANDeviceChannel;
signals:
public slots:
void CANTimerExpired();
};
#endif // CANANALYZER_H
@@ -0,0 +1,6 @@
#include "CANDataLogger.h"
CCANDataLogger::CCANDataLogger()
{
}
@@ -0,0 +1,11 @@
#ifndef CANDATALOGGER_H
#define CANDATALOGGER_H
class CCANDataLogger
{
public:
CCANDataLogger();
};
#endif // CANDATALOGGER_H
+122
View File
@@ -0,0 +1,122 @@
#include "CANDevice.h"
#include "OtarcikCan.h"
CCANDevice::CCANDevice(QObject *parent)
{
Q_UNUSED(parent)
mMessageList.clear();
mMessagesListLoaded = false;
mCANAnalyzer.mDevicePtr = this;
mProgramPtr = 0;
// mCANDeviceDatabaseFilename.clear();
// mCANDeviceID = -1;
// mCANDeviceBaudrate = -1;
// mDeviceDescription.clear();
// mDeviceName.clear();
// mDevicePollPeriod = 0;
}
CCANDevice::CCANDevice(CCANDeviceConfig &SysConfig)
{
mMessageList.clear();
mMessagesListLoaded = false;
mCANAnalyzer.mDevicePtr = this;
mProgramPtr = 0;
mDeviceConfigInfo = SysConfig;
// mCANDeviceID = SysConfig.mCANDeviceID;
// mCANDeviceBaudrate = SysConfig.mCANDeviceBaudrate;
// mCANDeviceDatabaseFilename = SysConfig.mCANDeviceDatabaseFilename;
// mDeviceName = SysConfig.mDeviceName;
// mDeviceDescription = SysConfig.mDeviceDescription;
// mDevicePollPeriod = SysConfig.mDevicePollPeriod;
}
CCANDevice::~CCANDevice()
{
while (!mMessageList.isEmpty())
{
delete mMessageList.takeFirst();
}
}
int CCANDevice::Init()
{
if(mDeviceConfigInfo.mCANDeviceID < 0 || mDeviceConfigInfo.mCANDeviceBaudrate < 0 || mDeviceConfigInfo.mDevicePollPeriod == 0 || mDeviceConfigInfo.mCANDeviceDatabaseFilename.isEmpty() == true || mDeviceConfigInfo.mDeviceName.isEmpty() == true)
{
qDebug("CCANDevice: trying to initialize a CCANDevice with invalid parameters");
return RET_GENERAL_ERROR;
}
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Initialisation du dispositif [%1]").arg(mDeviceConfigInfo.mDeviceName));
if(mCANDatabase.Init(mDeviceConfigInfo.mCANDeviceDatabaseFilename) == RET_OK)
{
if(mCANDatabase.BuildMessageList(&mMessageList) == RET_OK)
{
mMessagesListLoaded = true;
mProgramPtr->InitCANViewer(&mMessageList);
}
else
{
mMessagesListLoaded = false;
}
}
if(mCANAnalyzer.Init(mDeviceConfigInfo.mCANDeviceID,mDeviceConfigInfo.mCANDeviceBaudrate,&mMessageList,mDeviceConfigInfo.mDevicePollPeriod) != RET_OK)
{
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Le dispositif [%1] n'a pas pu s'initialiser").arg(mDeviceConfigInfo.mDeviceName),true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_ERROR_STATUS);
return RET_GENERAL_ERROR;
}
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Dispositif [%1] initialisé avec succès!").arg(mDeviceConfigInfo.mDeviceName),true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
return RET_OK;
}
int CCANDevice::Init(QString DatabaseFileName, TPCANHandle CANDeviceID, TPCANBaudrate CANDeviceBaudRate, QString DevDescription, QString DeviceName, unsigned int DevicePollPeriod)
{
mDeviceConfigInfo.mCANDeviceID = CANDeviceID;
mDeviceConfigInfo.mCANDeviceBaudrate = CANDeviceBaudRate;
mDeviceConfigInfo.mCANDeviceDatabaseFilename = DatabaseFileName;
mDeviceConfigInfo.mDeviceDescription = DevDescription;
mDeviceConfigInfo.mDeviceName = DeviceName;
mDeviceConfigInfo.mDevicePollPeriod = DevicePollPeriod;
Init();
return RET_OK;
}
int CCANDevice::NewMessageParsed()
{
return mProgramPtr->UpdateCANViewerDataRequest(&mMessageList);
}
QDataStream &operator<<(QDataStream &out, const CCANDevice &source)
{
out << source.mDeviceConfigInfo;
return out;
}
QDataStream &operator>>(QDataStream &in, CCANDevice &dest)
{
in >> dest.mDeviceConfigInfo;
return in;
}
////CAREFUL!!! OPERATOR = DOES NOT COPY THE MESSAGES, ONLY THE DEVICE DESCRIPTION !!//
//CCANDevice& CCANDevice::operator=(const CCANDevice *source)
//{
// if(source == this)
// {
// return *this;
// }
// this->mDeviceConfigInfo = source->mDeviceConfigInfo;
// return *this;
//}
+52
View File
@@ -0,0 +1,52 @@
#ifndef CANDEVICE_H
#define CANDEVICE_H
#include "defines.h"
#include <QObject>
#include "CANAnalyzer.h"
#include "CANMessage.h"
#include "PCANInterface.h"
#include "CANDatabase.h"
#include <QList>
#include "PCANBasic.h"
#include "CANDeviceConfig.h"
class COtarcikCan;
class CCANDevice : public QObject
{
Q_OBJECT
public:
explicit CCANDevice(QObject *parent = 0);
CCANDevice(CCANDeviceConfig &SysConfig);
~CCANDevice();
int Init(QString DatabaseFileName, TPCANHandle CANDeviceID, TPCANBaudrate CANDeviceBaudRate, QString DevDescription, QString DeviceName, unsigned int DevicePollPeriod);
int Init();
public:
CCANDeviceConfig mDeviceConfigInfo;
COtarcikCan *mProgramPtr;
bool mMessagesListLoaded;
CCANAnalyzer mCANAnalyzer; //The module that handles the USB puck and decodes the data
CCANDatabase mCANDatabase; //The device's database loaded from dbc file
QList<CCANMessage*> mMessageList;
// CCANDevice &operator=(const CCANDevice *source);
int NewMessageParsed();
private:
bool mConfigSet;
signals:
public slots:
};
QDataStream &operator<<(QDataStream &out, const CCANDevice &source);
QDataStream &operator>>(QDataStream &in, CCANDevice &dest);
#endif // CANDEVICE_H
@@ -0,0 +1,54 @@
#include "CANDeviceConfig.h"
CCANDeviceConfig::CCANDeviceConfig()
{
//Assign default values
mCANDeviceID = PCAN_USBBUS1;
mCANDeviceBaudrate = PCAN_BAUD_500K;
mCANDeviceDatabaseFilename = "./";
mDeviceDescription = "Description du module";
mDeviceName = "Nom du module";
mDevicePollPeriod = 200;
}
QDataStream &operator>>(QDataStream &in, CCANDeviceConfig &dest)
{
in >> dest.mCANDeviceID
>> dest.mCANDeviceBaudrate
>> dest.mCANDeviceDatabaseFilename
>> dest.mDeviceDescription
>> dest.mDeviceName
>> dest.mDevicePollPeriod;
return in;
}
QDataStream &operator<<(QDataStream &out, const CCANDeviceConfig &source)
{
out << source.mCANDeviceID
<< source.mCANDeviceBaudrate
<< source.mCANDeviceDatabaseFilename
<< source.mDeviceDescription
<< source.mDeviceName
<< source.mDevicePollPeriod;
return out;
}
CCANDeviceConfig& CCANDeviceConfig::operator=(const CCANDeviceConfig *source)
{
if(source == this)
{
return *this;
}
this->mCANDeviceID = source->mCANDeviceID;
this->mCANDeviceBaudrate = source->mCANDeviceBaudrate;
this->mCANDeviceDatabaseFilename = source->mCANDeviceDatabaseFilename;
this->mDeviceDescription = source->mDeviceDescription;
this->mDeviceName = source->mDeviceName;
this->mDevicePollPeriod = source->mDevicePollPeriod;
return *this;
}
@@ -0,0 +1,27 @@
#ifndef CANDEVICECONFIG_H
#define CANDEVICECONFIG_H
#include "PCANBasic.h"
#include <QDataStream>
#include <QString>
class CCANDeviceConfig
{
public:
CCANDeviceConfig();
TPCANHandle mCANDeviceID;
TPCANBaudrate mCANDeviceBaudrate;
QString mCANDeviceDatabaseFilename;
QString mDeviceDescription;
QString mDeviceName;
unsigned int mDevicePollPeriod;
CCANDeviceConfig &operator=(const CCANDeviceConfig *source);
};
QDataStream &operator<<(QDataStream &out, const CCANDeviceConfig &source);
QDataStream &operator>>(QDataStream &in, CCANDeviceConfig &dest);
#endif // CANDEVICECONFIG_H
+143
View File
@@ -0,0 +1,143 @@
#include "CANMessage.h"
#include "defines.h"
CCANMessage::CCANMessage()
{
mSignalsList.clear();
}
CCANMessage::CCANMessage(const CCANMessage &Src)
{
*this = Src;
}
CCANMessage::CCANMessage(const quint32 Channel, const TPCANMsg CANMsg, const TPCANTimestamp CANTimeStamp)
{
SetDataFromDriver(Channel,CANMsg,CANTimeStamp);
}
CCANMessage::~CCANMessage()
{
while (!mSignalsList.isEmpty())
{
delete mSignalsList.takeFirst();
}
}
int CCANMessage::SetDataFromDriver(quint32 Channel, TPCANMsg CANMsg, TPCANTimestamp CANTimeStamp)
{
mCANChannel = Channel;
mCANMsgID = (unsigned long long)CANMsg.ID;
mCANMsgLength = (unsigned char)CANMsg.LEN;
mCANMsgType = CANMsg.MSGTYPE;
//mCANMsgData = QByteArray((const char*)&CANMsg.DATA);
for(unsigned int i = 0; i < mCANMsgLength; i++)
{
mCANMsgData.append(CANMsg.DATA[i]);
}
mCANMsgMicrosecs = CANTimeStamp.micros;
mCANMsgMillisecs = CANTimeStamp.millis;
mCANMsgMillisecsOverflow = CANTimeStamp.millis_overflow;
return 0;
}
int CCANMessage::AddSignal(CCANSignal *NewSignal)
{
if(NewSignal == 0)
{
return RET_GENERAL_ERROR;
}
mSignalsList.append(NewSignal);
return RET_OK;
}
int CCANMessage::UpdateValue(CCANMessage *NewDeviceMessage)
{
if(NewDeviceMessage->mCANMsgID != mCANMsgID)
{
qDebug("CCANMEssage error: Trying to update mismatching message ID");
return RET_GENERAL_ERROR;
}
if(NewDeviceMessage->mCANMsgLength != mCANMsgLength)
{
qDebug("CCANMessage error: New device message length different than database!!!");
return RET_GENERAL_ERROR;
}
mCANMsgData = QByteArray(NewDeviceMessage->mCANMsgData);
// if(NewDeviceMessage->mCANMsgID == 0x524)
// int toto = 5;
mCANRawDataMotorola = mCANRawDataIntel= 0;
for(unsigned int i = 0; i < mCANMsgLength; i++)
{
mCANRawDataIntel += ((unsigned char)(mCANMsgData[i]) & 0xFF);
mCANRawDataMotorola += ((unsigned char)(mCANMsgData[(mCANMsgLength - 1) - i]) & 0xFF);
if(i < mCANMsgLength-1)
{
mCANRawDataIntel <<= 8;
mCANRawDataMotorola <<= 8;
}
}
for(int i = 0; i < mSignalsList.size(); i++)
{
mSignalsList.at(i)->ComputeNewSignalValue(mCANRawDataIntel, mCANMsgLength);
}
return RET_OK;
}
QDataStream &operator<<(QDataStream &out, const CCANMessage &source)
{
out << source.mCANChannel
<< source.mCANMsgID
<< source.mCANMsgType
<< source.mCANMsgLength
<< source.mCANMsgData
<< source.mCANMsgMillisecs
<< source.mCANMsgMillisecsOverflow
<< source.mCANMsgMicrosecs;
return out;
}
QDataStream &operator>>(QDataStream &in, CCANMessage &dest)
{
in >> dest.mCANChannel
>> dest.mCANMsgID
>> dest.mCANMsgType
>> dest.mCANMsgLength
>> dest.mCANMsgData
>> dest.mCANMsgMillisecs
>> dest.mCANMsgMillisecsOverflow
>> dest.mCANMsgMicrosecs;
return in;
}
CCANMessage& CCANMessage::operator=(const CCANMessage *source)
{
if(source == this)
{
return *this;
}
this->mCANChannel = source->mCANChannel;
this->mCANMsgID = source->mCANMsgID;
this->mCANMsgType = source->mCANMsgType;
this->mCANMsgLength = source->mCANMsgLength;
this->mCANMsgData = source->mCANMsgData;
this->mCANMsgMillisecs = source->mCANMsgMillisecs;
this->mCANMsgMillisecsOverflow = source->mCANMsgMillisecsOverflow;
this->mCANMsgMicrosecs = source->mCANMsgMicrosecs;
return *this;
}
+48
View File
@@ -0,0 +1,48 @@
#ifndef CANMESSAGE_H
#define CANMESSAGE_H
#include <QByteArray>
#include <QDataStream>
#include "PCANBasic.h"
#include "CANSignal.h"
#include <QList>
class CCANMessage
{
public:
CCANMessage();
CCANMessage(const CCANMessage &Src);
CCANMessage(const quint32 Channel,const TPCANMsg CANMsg,const TPCANTimestamp CANTimeStamp);
~CCANMessage();
unsigned int mCANChannel;
unsigned int mCANMsgType; // Type of the message, using the PCAN interface driver encoding defined in PCANbasic.h
QByteArray mCANMsgData; // Raw data structure of the message, max size = 8 bytes
quint64 mCANRawDataMotorola, mCANRawDataIntel;
//Database generated message structure
QString mCANMsgName; //Name of message in dbc file
QString mCANMsgComment; //misc comment from database
unsigned int mCANMsgID; // 11/29-bit message identifier from database
quint32 mCANMsgFlags; //Message flags from database
unsigned int mCANMsgLength; // Data Length Code of the message (0..8) from database
QList<CCANSignal*> mSignalsList;
//Timestamp
quint64 mCANMsgMillisecs; // Base-value: milliseconds: 0.. 2^32-1
quint32 mCANMsgMillisecsOverflow; // Roll-arounds of millis
quint32 mCANMsgMicrosecs; // Microseconds: 0..999
CCANMessage &operator=(const CCANMessage *source);
int SetDataFromDriver(quint32 Channel,TPCANMsg CANMsg,TPCANTimestamp CANTimeStamp);
int AddSignal(CCANSignal* NewSignal);
int UpdateValue(CCANMessage* NewDeviceMessage);
};
QDataStream &operator<<(QDataStream &out, const CCANMessage &source);
QDataStream &operator>>(QDataStream &in, CCANMessage &dest);
#endif // CANMESSAGE_H
+41
View File
@@ -0,0 +1,41 @@
#include "CANSignal.h"
#include "defines.h"
CCANSignal::CCANSignal()
{
mEncoding = CAN_SIGNAL_ENCODING_INVALID;
}
int CCANSignal::ComputeNewSignalValue(quint64 NewValue, quint16 MessageSize)
{
if(mEncoding == CAN_SIGNAL_ENCODING_INTEL)
{
//TODO: trouver un device qui crache du intel??
mRawValue = NewValue;
}
else if(mEncoding == CAN_SIGNAL_ENCODING_MOTOROLA)
{
//mRawValue = MotorolaValue;
mRawValue = NewValue;
int StartIndex = mStartBit - (mStartBit % 8) + 7 - (mStartBit % 8);
int shift = ((MessageSize * 8) - StartIndex -1);
mRawValue >>= shift;
quint64 mask = 1;
mask <<= mSignalSize;
mask -= 1;
mRawValue &= mask;
}
else
{
//TODO: À faire!!!
return RET_GENERAL_ERROR;
}
mPhysicalValue = ((signed) mRawValue * mValueFactor) + mValueOffset;
return RET_OK;
}
+90
View File
@@ -0,0 +1,90 @@
#ifndef CANSIGNAL_H
#define CANSIGNAL_H
#include <QtGlobal>
#include <QString>
class CCANSignal
{
public:
enum eCANSignalEncoding
{
CAN_SIGNAL_ENCODING_INTEL = 0,
CAN_SIGNAL_ENCODING_MOTOROLA,
CAN_SIGNAL_ENCODING_INVALID,
CAN_SIGNAL_ENCODING_MAX
};
enum eCANSignalMultiplexing
{
CAN_SIGNAL_MULTIPLEXING_NONE,
CAN_SIGNAL_MULTIPLEXING_SWITCH,
CAN_SIGNAL_MULTIPLEXING_MULTIPLEXED,
CAN_SIGNAL_MULTIPLEXING_MAX
};
enum eCANSIgnalValueType
{
CAN_SIGNAL_TYPE_INVALID,
CAN_SIGNAL_TYPE_SIGNED_INT,
CAN_SIGNAL_TYPE_UNSIGNED_INT,
CAN_SIGNAL_TYPE_32_BIT_FLOAT,
CAN_SIGNAL_TYPE_64_BIT_DOUBLE,
CAN_SIGNAL_TYPE_MAX
};
public:
CCANSignal();
QString mSignalName;
QString mSignalComment;
unsigned int mEncoding;
unsigned int mMultiplexing;
unsigned int mStartBit;
unsigned int mSignalSize;
unsigned int mValueType;
double mValueFactor;
double mValueOffset;
int mMinValue;
int mMaxValue;
QString mSignalUnit;
int ComputeNewSignalValue(quint64 NewValue, quint16 MessageSize);
quint64 mRawValue;
double mPhysicalValue;
};
#endif // CANSIGNAL_H
/*
physical_value = raw_value * factor + offset
raw_value = (physical_value – offset) / factor
*/
/*
* Signal: name='_BA_Resp_SW_Ver_Revision', unit=''
qname='IVT-S_all-variations_12082020.IVT_Msg_Response._BA_Resp_SW_Ver_Revision'
comment=''
encoding='Motorola'
representation='Unsigned'
value min=0.00, value max=0.00
scale faktor=1.00, offset=0.00
startbit=24, length=8
*/