-
Notifications
You must be signed in to change notification settings - Fork 70
/
ipuds.js
453 lines (433 loc) · 17.9 KB
/
ipuds.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* eslint-disable no-constant-condition */
/* eslint-disable no-empty */
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
const dgram = require('dgram')
const net = require('net')
const { ipcMain } = require('electron')
const sprintf = require('sprintf-js').sprintf
const PORT = 13400
const VER = 0x02
const UDS = require('./uds.js')
class IPUDS extends UDS {
constructor(win) {
super(win)
this.lastData = Buffer.alloc(0)
this.header = Buffer.from([VER, VER ^ 0xff, 0, 0, 0, 0, 0, 0])
this.timeout = 2000
this.sDelay = 100
this.cMap = {}
this.waitDoipAck = false;
this.typeList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 0x4001, 0x4002, 0x4003, 0x4004, 0x8001, 0x8002, 0x8003]
this.clientTypeList = [0, 4, 6, 7, 0x4002, 0x4004, 0x8001, 0x8002, 0x8003]
this.udpFd = dgram.createSocket('udp4')
this.udpFd.on('message', (msg, rinfo) => {
var rets = this.parseData(msg)
for (var i in rets) {
if (rets[i].err > 0) {
if (rets[i].type === 4) {
this.emit('doipDeviceFound', [rets[i].data, rinfo])
}
}
}
})
ipcMain.on('doipTcpDisconnect', (event, arg) => {
var key = arg.SA.toString() + arg.TA.toString()
clearTimeout(this.cMap[key].timer)
this.cMap[key].fd.destroy()
delete this.cMap[key]
})
ipcMain.on('doipTcpDisconnectWithKey', (event, arg) => {
var key = arg
clearTimeout(this.cMap[key].timer)
this.cMap[key].fd.destroy()
delete this.cMap[key]
})
ipcMain.on('doipTcpConnect', (event, arg) => {
var target = arg[0]
var active = arg[1]
var key = active.sa + target.logicalAddr
if (key in this.cMap) {
this.emit('doipTcpStatus', {
err: -1,
key: key,
msg: 'This Connection Existed'
}
)
} else {
var item = {}
this.cMap[key] = item
item.fd = net.createConnection(PORT, target.ip, () => {
item.active = false
var msg = this.writeRouteActive(parseInt(active.sa), parseInt(active.activeType), active.option)
item.receive = true
item.fd.write(msg, () => {
item.timer = setTimeout(() => {
item.fd.destroy()
delete this.cMap[key]
this.emit('doipTcpStatus', {
key: key,
err: -1,
msg: 'Active Response Timeout'
}
)
}, active.timeout);
})
})
item.fd.on('error', (msg) => {
clearTimeout(item.timer)
this.emit('doipTcpStatus', {
key: key,
err: -2,
msg: 'Connecting Error'
}
)
if (item.receive) {
this.emit('udsError', {
msg: "This socket error",
index: this.tableIndex
})
}
delete this.cMap[key]
})
item.fd.on('end', (msg) => {
clearTimeout(item.timer)
this.emit('doipTcpStatus', {
key: key,
err: -2,
msg: 'Server Close'
}
)
if (item.receive) {
this.emit('udsError', {
msg: "This socket error",
index: this.tableIndex
})
}
delete this.cMap[key]
})
item.fd.on('data', (msg) => {
var rets = this.parseData(msg)
for (var i in rets) {
var ret = rets[i]
if (ret.err > 0) {
if ((item.receive) || (ret.type === 7)) {
item.receive = false
if (ret.type === 6) {
if (item.active === false) {
clearTimeout(item.timer)
item.active = true
this.emit('doipTcpStatus', {
key: key,
err: 0,
msg: 'Active Success',
data: ret.data
}
)
} else {
this.verbose("The router has actived")
}
} else if (ret.type === 7) {
item.fd.write(this.writeAliveRes(parseInt(active.sa)))
} else if (ret.type === 0x8001) {
if (this.waitDoipAck) {
this.error('Lose doip ack');
}
clearTimeout(item.timer)
this.verbose(sprintf("uds response:%s.", ret.data.payload.join(',')))
try {
if ((ret.data.payload[0] == 0x7F) && (ret.data.payload[2] == 0X78)) {
item.receive = true;
this.delay()
} else {
if (this.checkFunc(this.writeData, ret.data.payload)) {
if (item.timer.hasRef()) {
item.receive = true;
this.verbose(":User insert a new delay")
} else {
this.step()
}
} else {
this.emit('udsError', {
msg: "User defined function return false",
index: this.tableIndex
})
}
}
} catch (error) {
this.emit('udsError', {
msg: sprintf("User defined function syntax error,%s", error.message),
index: this.tableIndex
})
}
} else if (ret.type === 0x8002) {
clearTimeout(item.timer)
this.waitDoipAck = false;
this.verbose(sprintf("ack:0x%X,msg:%s.\r\n", ret.data.code, ret.data.payload.join(',')))
if (item.suppress) {
setTimeout(() => {
this.step()
}, this.sDelay)
} else {
item.receive = true
item.timer = setTimeout(() => {
this.emit('udsError', {
msg: 'No uds response',
index: this.tableIndex
})
}, this.timeout)
}
} else if (ret.type === 0x8003) {
this.waitDoipAck = false;
clearTimeout(item.timer)
this.emit('udsError', {
msg: sprintf("nack:0x%X,msg:0x%s", ret.data.code, ret.data.payload),
index: this.tableIndex
})
}
}
} else if (ret.err < 0) {
clearTimeout(item.timer)
this.emit('udsError', {
msg: sprintf("%s", ret.msg),
index: this.tableIndex
})
}
}
})
}
})
ipcMain.on('doipudsExcute', (event, arg) => {
this.sDelay = arg.sDelay
this.timeout = arg.timeout
this.UDSstart(arg.udsTable)
this.addr = arg.addr
this.step()
})
ipcMain.on('doipDeviceFind', (event, arg) => {
var msg
if (arg.type === 'NULL') {
msg = this.writeReqNULL()
} else if (arg.type === 'EID') {
msg = this.writeReqEID(Buffer.from(arg.eid, 'hex'))
} else {
msg = this.writeReqVIN(Buffer.from(arg.vin, 'ascii'))
}
this.udpFd.send(msg, PORT, arg.multicast)
})
}
delay(timeout) {
var t = typeof timeout !== 'undefined' ? timeout : this.timeout
this.cMap[this.key].timer = setTimeout(() => {
this.emit('udsError', {
msg: 'No Response',
index: this.tableIndex
})
}, t)
}
step() {
try {
var item = this.getNextService()
} catch (e) {
this.emit('udsError', {
msg: "User defined function syntax error," + e.message,
index: this.tableIndex
})
return -1
}
if (item === null) {
this.emit('udsEnd', "Excute successful")
return 0
}
this.checkFunc = item.checkFunc
this.writeData = item.payload
var key = this.addr.key
var msg = this.writeDiaMsg(this.addr.SA, this.addr.TA, item.data)
this.key = key
if (key in this.cMap) {
this.cMap[key].suppress = item.suppress
this.cMap[key].receive = true
if (this.cMap[key].fd.write(msg, () => {
this.waitDoipAck = true
this.cMap[key].timer = setTimeout(() => {
/*no response*/
this.emit('udsError', {
msg: 'No ack response',
index: this.tableIndex
})
}, this.timeout);
}) == false) {
this.emit('udsError', {
msg: 'TCP write error',
index: this.tableIndex
})
}
} else {
this.emit('udsError', {
msg: 'This connection lost',
index: this.tableIndex
})
}
return 1
}
parseData(msg) {
var data = Buffer.concat([this.lastData, msg])
this.lastData = Buffer.alloc(0)
this.verbose(msg.toString('hex'))
var rets = []
var payload
var header
while (1) {
let ret = {}
ret.err = 0
ret.msg = 'empty'
rets.push(ret)
if (data.length >= 8) {
header = Buffer.from(data.slice(0, 8))
/*header handler*/
if ((header.readUInt8(0) !== VER) || header.readUInt8(1) !== (VER ^ 0xff)) {
ret.err = -1
ret.msg = "Incorrect pattern format"
return rets
}
let type = header.readUInt16BE(2)
if (this.typeList.indexOf(type) === -1) {
ret.err = -1
ret.msg = "Unknown payload type"
return rets
}
let len = header.readUInt32BE(4)
if (this.clientTypeList.indexOf(type) === -1) {
ret.err = -2
ret.msg = "Discard message"
return rets
}
if (data.length - 8 >= len) {
/*a ok frame*/
payload = Buffer.from(data.slice(8, 8 + len))
ret.type = type
ret.data = {}
if (type === 4 && payload.length >= 32) {
ret.data.vin = payload.slice(0, 17).toString('ascii')
ret.data.logicalAddr = payload.readUInt16BE(17)
ret.data.eid = payload.slice(19, 25).toString('hex')
ret.data.gid = payload.slice(25, 31).toString('hex')
ret.data.fAction = payload.readUInt8(31)
if (payload.length == 33) {
ret.data.syncStatus = payload.readUInt8(32)
}
} else if (type === 6) {
ret.data.testerAddr = payload.readUInt16BE(0)
ret.data.entityAddr = payload.readUInt16BE(2)
ret.data.code = payload.readUInt8(4)
if (payload.length == 13) {
ret.data.option = payload.readUInt32BE(9)
}
} else if (type === 0x8001) {
ret.data.sa = payload.readUInt16BE(0)
ret.data.ta = payload.readUInt16BE(2)
ret.data.payload = [...payload.slice(4)]
} else if (type === 0x8002) {
ret.data.sa = payload.readUInt16BE(0)
ret.data.ta = payload.readUInt16BE(2)
ret.data.code = payload.readUInt8(4)
ret.data.payload = [...payload.slice(5)]
} else if (type === 0x8003) {
// ret.err=-1
// ret.msg='Negative diagnostic message'
ret.data.sa = payload.readUInt16BE(0)
ret.data.ta = payload.readUInt16BE(2)
ret.data.code = payload.readUInt8(4)
ret.data.payload = [...payload.slice(5)]
}
else if (type === 7) {
//todo active request
} else if (type === 0x4004) {
ret.data.powerMode = payload.readUInt8(0)
} else if (type === 0x4002) {
ret.data.nt = payload.readUInt8(0)
ret.data.mcts = payload.readUInt8(1)
ret.data.ncts = payload.readUInt8(2)
if (payload.length === 7) {
ret.data.mds = payload.readUInt32BE(3)
}
}
this.verbose(JSON.stringify(ret))
ret.err = len + 8
data = Buffer.from(data.slice(8 + len))
} else {
/* stroe and handle later*/
this.lastData = Buffer.from(data)
break
}
} else {
this.lastData = Buffer.from(data)
break
}
}
return rets
}
changeLen(len) {
this.header.writeUInt32BE(len, 4)
}
changeType(type) {
this.header.writeUInt16BE(type, 2)
}
writeReqNULL() {
this.changeLen(0)
this.changeType(1)
return this.header
}
writeReqEID(eid) {
this.changeLen(6)
this.changeType(2)
return Buffer.concat([this.header, Buffer.from(eid, 'binary')], this.header.length + 6)
}
writeReqVIN(vin) {
this.changeLen(17)
this.changeType(3)
return Buffer.concat([this.header, Buffer.from(vin, 'ascii')], this.header.length + 17)
}
writeRouteActive(sa, activeType, option) {
var len = 0
if (option !== '') {
len = 4
}
this.changeLen(7 + len)
this.changeType(5)
var b = Buffer.alloc(7 + len, 0)
if (len > 0) {
Buffer.from(option, 'hex').copy(b, 7)
}
b.writeUInt16BE(sa, 0)
b.writeUInt8(activeType, 2)
return Buffer.concat([this.header, b], this.header.length + b.length)
}
writeAliveRes(sa) {
this.changeLen(2)
this.changeType(8)
var b = Buffer.alloc(2, 0)
b.writeUInt16BE(sa, 0)
return Buffer.concat([this.header, b], this.header.length + 2)
}
writeReqDiaPowerMode(writer) {
this.changeLen(0)
this.changeType(0x4003)
return writer(this.header)
}
writeReqDoipStatus(writer) {
this.changeLen(0)
this.changeType(0x4001)
return writer(this.header)
}
writeDiaMsg(sa, ta, ud) {
var b = Buffer.alloc(4 + ud.length, 0)
Buffer.from(ud).copy(b, 4)
b.writeUInt16BE(sa, 0)
b.writeUInt16BE(ta, 2)
this.changeLen(b.length)
this.changeType(0x8001)
return Buffer.concat([this.header, b], this.header.length + b.length)
}
}
module.exports = IPUDS