-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPackStructure.h
289 lines (276 loc) · 10.1 KB
/
PackStructure.h
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef PACKSTRUCTURE_H
#define PACKSTRUCTURE_H
/**
* Developed By Muhammad Almuhmmah
* Year : 2021
* Freeware File
*
Smart Pack Structure For QC Diag And LG LAF Commands
- High flexibility in use
- Easy and expandable without the usual limits
- It can easily be expanded to support other protocols
- It automatically generates the value CRC X25
- Flexibility in arguments is limitless
C++ Qt
**/
#include <QtCore>
// see -> https://stackoverflow.com/questions/81870/is-it-possible-to-print-a-variables-type-in-standard-c
#include <type_traits>
#include <typeinfo>
#ifndef _MSC_VER
# include <cxxabi.h>
#endif
#include <memory>
#include <string>
#include <cstdlib>
#include <iostream>
#define QD(x) qDebug() <<x;
#define getMemoryDataHex(x) QByteArray((const char*)&x,sizeof(x)).toHex()
#define \
Int2Hex(xnum)({\
QString str = QString::number((qlonglong)xnum,16);\
str;\
})
#define \
GetUint64(stringNumber)({\
std::string s = stringNumber;\
uint64_t newUint = std::stoull(s, nullptr, 16);\
newUint;\
})
#define \
GetUint32(stringNumber)({\
std::string s = stringNumber;\
uint32_t newUint = std::stoul(s, nullptr, 16);\
newUint;\
})
#define Buffer2intger(buffer,start,end) buffer.mid(start,end).toHex().toInt(NULL,16)
template <class T>
std::string
type_name()
{
typedef typename std::remove_reference<T>::type TR;
std::unique_ptr<char, void(*)(void*)> own
(
#ifndef _MSC_VER
abi::__cxa_demangle(typeid(TR).name(), nullptr,
nullptr, nullptr),
#else
nullptr,
#endif
std::free
);
std::string r = own != nullptr ? own.get() : typeid(TR).name();
if (std::is_const<TR>::value)
r += " const";
if (std::is_volatile<TR>::value)
r += " volatile";
if (std::is_lvalue_reference<T>::value)
r += "&";
else if (std::is_rvalue_reference<T>::value)
r += "&&";
return r;
}
typedef struct __arg_item__{
__arg_item__(const QVariant &variant,const QString &name){
Variant=variant;
Name=name;
}
QVariant Variant;
QString Name;
}ArgItem;
template<typename ... Args>
static QByteArray PackStructureLaf(const Args&... args) // At least one argument
{
QD(endl<<endl<<"------------------ PackLafStructure 2 ------------------");
QList<ArgItem> lv;
int sink[] = { 0, ((void)(lv.append(ArgItem(args,QString::fromStdString(type_name<decltype(args)>())))), 0)... };(void) sink;
bool bls=1;
QVariant vArg;
QByteArray output;
int type=0;
for(int i=0;i<lv.count();i++)
{
ArgItem item=lv.at(i);
vArg=item.Variant;
// qDebug() <<item.Name<<vArg<<output;
type=(int)(QMetaType::Type)vArg.type();
if(type==QMetaType::Type::UInt or type==QMetaType::Type::Double or type==QVariant::Int or type==QVariant::LongLong or type==QVariant::ULongLong){
if(item.Name=="unsigned char const&"){
unsigned char const carVl=vArg.toChar().toLatin1();
output.append(getMemoryDataHex(carVl));
}else if(item.Name=="short const&" or item.Name=="unsigned short const&"){
short shortVl=vArg.toInt();
output.append(getMemoryDataHex(shortVl));
}else if(type==QMetaType::Type::Double){
double NumberVl=vArg.toDouble();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::Int){
int NumberVl=vArg.toInt();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::UInt){
uint NumberVl=vArg.toUInt();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::LongLong){
qlonglong NumberVl=vArg.toLongLong();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::ULongLong){
qulonglong NumberVl=vArg.toULongLong();
output.append(getMemoryDataHex(NumberVl));
}
}else if(type==QVariant::String){
output.append(vArg.toString());
}else if(type==QVariant::Char)
output.append(vArg.toChar());
else if(type==QVariant::ByteArray)
output.append(vArg.toByteArray());
}
// calculate crc x25
if(output.length()>=32){
QByteArray ddd=QByteArray::fromHex(output);
quint16 crc=qChecksum(ddd.data(),ddd.length());
output.clear();
output=ddd.mid(0,24);
output.append((char*)&crc,sizeof(crc));
output.append(QByteArray().fill(0x0,2));
output.append(ddd.mid(28));
output=output.toHex();
}
if(output.length()>=300){
bls=0;
}
if(bls)
qDebug() <<"output[HEX]"<<output.toUpper();
output=QByteArray::fromHex(output);
if(bls)
qDebug() <<"output"<<output;
return output;
}
template<typename ... Args>
static QByteArray PackStructureQC(const Args&... args) // At least one argument
{
QD(endl<<endl<<"------------------ PackStructureQC ------------------");
QList<ArgItem> lv;
int sink[] = { 0, ((void)(lv.append(ArgItem(args,QString::fromStdString(type_name<decltype(args)>())))), 0)... };(void) sink;
bool bls=1;
QVariant vArg;
QByteArray output;
int type=0;
for(int i=0;i<lv.count();i++)
{
ArgItem item=lv.at(i);
vArg=item.Variant;
type=(int)(QMetaType::Type)vArg.type();
if(type==QMetaType::Type::UInt or type==QMetaType::Type::Double or type==QVariant::Int or type==QVariant::LongLong or type==QVariant::ULongLong){
if(item.Name=="unsigned char const&"){
unsigned char const carVl=vArg.toChar().toLatin1();
output.append(getMemoryDataHex(carVl));
}else if(item.Name=="short const&" or item.Name=="unsigned short const&"){
short shortVl=vArg.toInt();
output.append(getMemoryDataHex(shortVl));
}else if(type==QMetaType::Type::Double){
double NumberVl=vArg.toDouble();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::Int){
int NumberVl=vArg.toInt();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::UInt){
uint NumberVl=vArg.toUInt();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::LongLong){
qlonglong NumberVl=vArg.toLongLong();
output.append(getMemoryDataHex(NumberVl));
}else if(type==QMetaType::Type::ULongLong){
qulonglong NumberVl=vArg.toULongLong();
output.append(getMemoryDataHex(NumberVl));
}
}else if(type==QVariant::String){
output.append(vArg.toString());
}else if(type==QVariant::Char)
output.append(vArg.toChar());
else if(type==QVariant::ByteArray)
output.append(vArg.toByteArray());
}
// calculate crc x25
if(output.length()>=2){
QByteArray ddd=QByteArray::fromHex(output);
quint16 crc=qChecksum(ddd.data(),ddd.length());
output.clear();
output=ddd;
output.append((char*)&crc,sizeof(crc));
output.append(0x7E);
output=output.toHex();
}
qDebug() <<"output[HEX] QC "<<output.toUpper();
output=QByteArray::fromHex(output);
return output;
}
template<typename ... Args>
static QByteArray PackStructure(const Args&... args) // At least one argument
{
QList<QVariant> lv;
int sink[] = { 0, ((void)(lv.append(args)), 0)... };(void) sink;
QVariant vArg;
QByteArray output;
int type=0;
for(int i=0;i<lv.count();i++)
{
vArg=lv.at(i);
qDebug() <<vArg;
type=(int)(QMetaType::Type)vArg.type();
if(type==QMetaType::Type::UInt or type==QMetaType::Type::Double or type==QVariant::Int or type==QVariant::LongLong or type==QVariant::ULongLong){
if(type==QMetaType::Type::Double)
output.append(Int2Hex(vArg.toDouble()));
else if(type==QMetaType::Type::Int)
output.append(Int2Hex(vArg.toInt()));
else if(type==QMetaType::Type::UInt)
output.append(Int2Hex(vArg.toUInt()));
else if(type==QMetaType::Type::LongLong)
output.append(Int2Hex(vArg.toLongLong()));
else if(type==QMetaType::Type::ULongLong)
output.append(Int2Hex(vArg.toULongLong()));
}else if(type==QVariant::String)
output.append(vArg.toString());
else if(type==QVariant::Char)
output.append(vArg.toChar());
else if(type==QVariant::ByteArray)
output.append(vArg.toByteArray());
}
qDebug() <<"output[HEX]"<<output.toUpper();
output=QByteArray::fromHex(output);
qDebug() <<"output"<<output;
return output;
}
#define QC_FILL(len) QByteArray().fill('0',len*2)
#define QC_STANDARD_FILL(len) QByteArray().fill('0',len)
inline void Filter(QByteArray &data)
{
if(data.isEmpty())
return;
for(int i=0;i<data.count();i++)
{
if(data.at(i)==0x00)
data.remove(i,1);
}
for(int i=data.count()-1;i>=0;i--)
{
if(data.at(i)==0x00)
data.remove(i,1);
}
}
inline QByteArray Filter2(QByteArray data)
{
if(data.isEmpty())
return data;
for(int i=0;i<data.count();i++)
{
if(data.at(i)==0x00)
data.remove(i,1);
}
for(int i=data.count()-1;i>=0;i--)
{
if(data.at(i)==0x00)
data.remove(i,1);
}
return data;
}
#endif // PACKSTRUCTURE_H