forked from kolyandex/Lexia_J2534
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTranslator.cpp
337 lines (317 loc) · 8.59 KB
/
CTranslator.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "CTranslator.h"
#include "j2534.h"
#include <cstdlib>
#include <ctime>
#include "dllmain.h"
unsigned char AppVersion[] = { 0x00, 0xFA, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char EepromRead[] = { 0x00, 0x09, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,0x00, 0xFF };
static unsigned char protocolMessage[] = {
0x00, 0x16, 0xAA, 0x04,
0x7C, 0x15, 0x00, 0x00,
0x76, 0x00, 0x00, 0x00,
0x54, 0x00, 0x64, 0x01, 0x54, 0x01, 0x0A, 0x00,
0x54, 0x02, 0x0D, 0x00, 0x54, 0x03, 0x64, 0x01,
0x54, 0x04, 0x0A, 0x00, 0x54, 0x05, 0x32, 0x00,
0x54, 0x09, 0x00, 0x00, 0x54, 0x0A, 0xC4, 0x09,
0x54, 0x0C, 0xF4, 0x01, 0x50, 0x00, 0x08, 0x00,
0x50, 0x01, 0x84, 0x07,
0x50, 0x02, 0x85, 0x07,
0x50, 0x03, 0x00, 0x00,
0x43, 0x55,
0x00, 0x00, 0x01, 0x01, 0x56, 0x00, 0xFF, 0x00,
0x52, 0x00, 0xFF, 0x00, 0x43, 0x55, 0x00, 0x00,
0x02, 0x02, 0x56, 0x00, 0xFF, 0x00, 0x52, 0x00,
0xFF, 0x00, 0x43, 0x50,
0xD0, 0x07, //periodic time
0x03, 0x00, 0x44, 0x3E, 0x44, 0x02, // periodoc message
0x43, 0x55, 0x00, 0x00,
0x04, 0x00, 0x56, 0x00, 0xFF, 0x00, 0x52, 0x00,
0xFF, 0x00, 0x43, 0x55, 0x00, 0x00, 0xFF, 0x00,
0x56, 0x00, 0xFF, 0x00, 0x52, 0x00, 0xFF, 0x00 };
unsigned char someMsg[] = { 0x00, 0x09, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,0x16, 0x06 };
unsigned char someVerReq[] = { 0x00, 0x09, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,0x00, 0x1C };
unsigned char CommStatus[] = { 0x00, 0xFE, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char someMsg4[] = { 0x00, 0x0B, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04 };
unsigned char someMsg5[] = { 0x00, 0x0B, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03 };
unsigned char JumpToApp[] = { 0x00, 0x12, 0x00, 0x00, 0x7C, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char connectAsCan[] = { 0x00, 0x05, 0xAA, 0x02, 0x7C, 0x15, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x46, 0x46, 0x58, 0x58, 0x58, 0x58, 0x43, 0x30, 0x32, 0x2B, 0x32, 0x58, 0x58, 0x43, 0x35, 0x58, 0x58, 0x58, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 };
unsigned char sendMessage[] = { 0xFF, 0x01, 0x00, 0x04, 0x74, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x1A, 0x87 };
unsigned char sendMessage2[] = { 0xFF, 0x01, 0x00, 0x04, 0x74, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x92 };
unsigned char sendMessage3[] = { 0xFF, 0x01, 0x00, 0x04, 0x74, 0x40, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10, 0x85 };
static void Replace(char* start, char* end, char old_val, char new_val)
{
if (start > end)
return;
while (start != end)
{
if (*start == old_val)
{
*start = new_val;
}
start++;
}
}
CTranslator::CTranslator()
{
Lexia = new CLexiaExchanger();
device = NOT_INITIALIZED;
channel = NOT_INITIALIZED;
filter = NOT_INITIALIZED;
sendLen = 0;
memset(tempBuf, 0x00, sizeof(tempBuf));
memset(sendBuf, 0x00, sizeof(sendBuf));
}
long CTranslator::OpenDevice(unsigned long* id)
{
if (device != NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
LEXIA_STATUS status = Lexia->Connect();
if (status == LEXIA_NOERROR)
{
std::srand(static_cast<unsigned int>(std::time(0)));
device = std::rand();
*id = device;
LexiaLog("Device opened with ID: %d", device);
return STATUS_NOERROR;
}
else
{
LexiaLog("Open device: %s", LexiaErrors[status]);
return ERR_FAILED;
}
}
long CTranslator::CloseDevice(unsigned long id)
{
if (device == NOT_INITIALIZED)
{
return ERR_FAILED;
}
if (id != device)
{
return ERR_INVALID_DEVICE_ID;
}
if (Lexia->Disconnect() == LEXIA_NOERROR)
{
device = NOT_INITIALIZED;
return STATUS_NOERROR;
}
return ERR_FAILED;
}
long CTranslator::GetFmwVer(unsigned long id, char* fmw_str)
{
if (id != device)
{
return ERR_FAILED;
}
size_t received_len = 0;
if (Lexia->SendReceive(AppVersion, sizeof(AppVersion), tempBuf, &received_len) == LEXIA_NOERROR)
{
Replace(&tempBuf[20], &tempBuf[59], 0x00, ' ');
LexiaLog("Firmware: %s", &tempBuf[20]);
strcpy(fmw_str, &tempBuf[20]);
return STATUS_NOERROR;
}
return ERR_FAILED;
}
long CTranslator::ConnectAsCan500(unsigned long id, unsigned long* ch)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (id != device)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (channel != NOT_INITIALIZED)
{
return ERR_FAILED;
}
size_t received_len = 0;
Lexia->SendReceive(JumpToApp, sizeof(JumpToApp), tempBuf, &received_len);
Lexia->SendReceive(CommStatus, sizeof(CommStatus), tempBuf, &received_len);
//Lexia->SendReceive(EepromRead, sizeof(EepromRead), tempBuf, &received_len);//!
clock_t curr_tick = clock();
// Waiting for connection
while (clock() - curr_tick < 5000)
{
if (Lexia->SendReceive(connectAsCan, sizeof(connectAsCan), tempBuf, &received_len) == LEXIA_NOERROR)
{
if (tempBuf[14] == 0x29) continue;
else break;
}
else
return ERR_FAILED;
}
if (clock() - curr_tick < 5000)
{
channel = std::rand();
*ch = channel;
return STATUS_NOERROR;
}
return ERR_FAILED;
}
long CTranslator::Disconnect(unsigned long ch)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
channel = NOT_INITIALIZED;
return STATUS_NOERROR;
}
long CTranslator::SendMsg(unsigned long ch, unsigned char* buf, size_t len, unsigned short addr)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
if (filter == NOT_INITIALIZED)
{
return ERR_NO_FLOW_CONTROL;
}
if (len == 0)
{
return ERR_BUFFER_EMPTY;
}
if (addr != *(unsigned short*)& protocolMessage[54])
{
LexiaLog("ECU addr not match!");
return ERR_NOT_SUPPORTED;
}
if (sendLen != 0)
{
return ERR_BUFFER_FULL;
}
memcpy(sendBuf, buf, len);
sendLen = len;
return STATUS_NOERROR;
}
long CTranslator::ReadMsg(unsigned long ch, unsigned char* buf, size_t* len, unsigned short* addr)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
if (filter == NOT_INITIALIZED)
{
return ERR_NO_FLOW_CONTROL;
}
if (sendLen == 0)
{
return ERR_BUFFER_EMPTY;
}
size_t received_len = 0;
Lexia->SendReceive(protocolMessage, sizeof(protocolMessage), tempBuf, &received_len);
unsigned long total_len = 12 + sendLen;
unsigned char* tmpSendBuf = new unsigned char[total_len];
memset(tmpSendBuf, 0x00, total_len);
tmpSendBuf[0] = 0xFF;
tmpSendBuf[1] = 0x01;
unsigned long timeout = 8250;
*((unsigned long*)tmpSendBuf + 1) = timeout;
*((unsigned long*)tmpSendBuf + 2) = sendLen;
memcpy(&tmpSendBuf[12], sendBuf, sendLen);
sendLen = 0;
//001200000000000000000012 00 000CAA00000000
//FF010200000000000000FF01 07 0001AA0000000001FF0200055092
received_len = 0;
Lexia->SendReceive(tmpSendBuf, total_len, tempBuf, &received_len);
if (received_len >= 20)
{
if (tempBuf[14] != 0x01)
{
return ERR_TIMEOUT;
}
if (tempBuf[12] >= 3)
{
if (tempBuf[22] > 0)
{
memcpy(buf, &tempBuf[25], tempBuf[22]);
*len = tempBuf[22];
*addr = *(unsigned short*)& protocolMessage[58];
return STATUS_NOERROR;
}
}
}
return ERR_FAILED;
}
long CTranslator::SetFilter(unsigned long ch, unsigned short ecu_addr, unsigned short ans_addr, unsigned long* id)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
if (filter != NOT_INITIALIZED)
{
return ERR_EXCEEDED_LIMIT;
}
filter = rand();
*id = filter;
*(unsigned short*)& protocolMessage[54] = ecu_addr;
*(unsigned short*)& protocolMessage[58] = ans_addr;
LexiaLog("ECU: %03X", *(unsigned short*)& protocolMessage[54]);
LexiaLog("ANS: %03X", *(unsigned short*)& protocolMessage[58]);
return STATUS_NOERROR;
}
long CTranslator::StopFilter(unsigned long ch, unsigned long id)
{
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
if (id == filter)
{
filter = NOT_INITIALIZED;
return STATUS_NOERROR;
}
return ERR_INVALID_FILTER_ID;
}
long CTranslator::ClearFilters(unsigned long ch)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
filter = NOT_INITIALIZED;
return STATUS_NOERROR;
}
long CTranslator::ClearBuffers(unsigned long ch)
{
if (device == NOT_INITIALIZED)
{
return ERR_DEVICE_NOT_CONNECTED;
}
if (ch != channel || channel == NOT_INITIALIZED)
{
return ERR_INVALID_CHANNEL_ID;
}
sendLen = 0;
return STATUS_NOERROR;
}
CTranslator::~CTranslator()
{
delete (Lexia);
}