forked from pkaselj/CAN_PacketSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can_parser.cpp
128 lines (100 loc) · 3.33 KB
/
can_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "can_parser.h"
#include <QDebug>
#include <QDateTime>
CAN_Parser::CAN_Parser(QObject *parent) : QObject(parent)
{
}
void CAN_Parser::AddEntry(unsigned int ID, CAN_DecodeParameters decodeParameters)
{
m_decodeTable[ID] = decodeParameters;
emit CAN_DecodeParametersChanged(ID);
}
void CAN_Parser::DecodeData(RawData rawPacket)
{
TableEntry decodedData;
if(m_decodeTable.contains(rawPacket.m_ID) == false)
{
qDebug() << QString("Received invalid data with ID: %1").arg(rawPacket.m_ID);
decodedData = DecodeData_NonExistent(rawPacket);
}
else
{
decodedData = DecodeData_Existing(rawPacket);
}
decodedData.m_rawHex = QByteArray::fromRawData((const char*)rawPacket.m_value, sizeof(RawData::m_value));
emit DataSuccessfullyDecoded(decodedData);
}
TableEntry CAN_Parser::DecodeData_Existing(RawData rawData)
{
CAN_DecodeParameters params = m_decodeTable.value(rawData.m_ID);
TableEntry decodedData;
decodedData.m_unit = params.m_unit;
decodedData.m_timestamp = QDateTime::currentDateTime().toString("hh:mm:ss,zzz");
decodedData.m_ID = rawData.m_ID;
uint64_t decodedValue = DecodeValue(rawData);
qDebug() << "DECODED VALUE #1 " << decodedValue;
decodedData.m_value = decodedValue * params.m_multiplier + params.m_offset;
decodedData.m_endianness = params.m_bigEndian ? "BIG ENDIAN" : "LITTLE ENDIAN";
return decodedData;
}
TableEntry CAN_Parser::DecodeData_NonExistent(RawData rawData)
{
TableEntry decodedData;
decodedData.m_unit = "RAW [DEC]";
decodedData.m_timestamp = QDateTime::currentDateTime().toString("hh:mm:ss,zzz");
decodedData.m_ID = rawData.m_ID;
decodedData.m_value = DecodeValue(rawData);
decodedData.m_endianness = "LITTLLE ENDIAN";
return decodedData;
}
uint64_t CAN_Parser::DecodeValue(RawData& rawData)
{
CAN_DecodeParameters decodeParameters;
decodeParameters.m_valueMask = CAN_DecodeParameters::enuValueMask::ACCEPT_ALL;
if(m_decodeTable.contains(rawData.m_ID) == true)
{
decodeParameters = m_decodeTable.value(rawData.m_ID);
}
uint64_t value = 0;
QByteArray temp = QByteArray::fromRawData((const char*)rawData.m_value, 8);
print("RAW VALUE", temp);
qDebug() << "Value mask: " << decodeParameters.m_valueMask;
if(decodeParameters.m_bigEndian)
{
qDebug() << "BIG ENDIAN";
for(int i = 0; i < 8; ++i)
{
if(decodeParameters.m_valueMask & (1 << i))
{
qDebug() << "i = " << i << " value = " << (int)rawData.m_value[i];
value = value << 8;
value += (uint8_t)rawData.m_value[i];
}
}
}
else
{
qDebug() << "LITTLE ENDIAN";
for(int i = 7; i >=0; --i)
{
if(decodeParameters.m_valueMask & (1 << i))
{
qDebug() << "i = " << i << " value = " << (int)rawData.m_value[i];
value = value << 8;
value += (uint8_t)rawData.m_value[i];
}
}
}
qDebug() << "VALUE: " << value;
return value;
}
void CAN_Parser::RemoveEntry(unsigned int ID)
{
if(!m_decodeTable.contains(ID))
{
qDebug() << "Decode table doesn't contain ID: " << ID;
return;
}
m_decodeTable.remove(ID);
emit CAN_DecodeParametersChanged(ID);
}