2023-01-13 12:41:57 -08:00
|
|
|
-- J2819 TP2.0 vehicle diagnostics protocol
|
|
|
|
-- very limited implementation
|
|
|
|
-- this controls onCanRx rate as well!
|
2023-01-31 21:15:02 -08:00
|
|
|
tickRate = 100
|
|
|
|
|
|
|
|
setTickRate(tickRate)
|
2023-01-13 12:41:57 -08:00
|
|
|
|
|
|
|
timeout = 3000
|
|
|
|
|
2024-02-10 18:41:19 -08:00
|
|
|
withErrorsCounter = 0
|
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
cuType = 0x02 -- TCU
|
|
|
|
|
|
|
|
hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
|
|
|
|
|
|
|
|
function toHexString(num)
|
|
|
|
if num == 0 then
|
|
|
|
return '0'
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = ""
|
|
|
|
while num > 0 do
|
|
|
|
local n = num % 16
|
|
|
|
result = hexstr[n + 1] ..result
|
|
|
|
num = math.floor(num / 16)
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function arrayToString(arr)
|
|
|
|
local str = ""
|
|
|
|
local index = 1
|
|
|
|
while arr[index] ~= nil do
|
|
|
|
str = str.." "..toHexString(arr[index])
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
VWTP_OUT = 0x200
|
|
|
|
|
|
|
|
VWTP_IN = 0x200 + cuType
|
|
|
|
VWTP_TESTER = 0x300
|
|
|
|
|
|
|
|
local cuId = 0
|
|
|
|
|
|
|
|
function onCanHello(bus, id, dlc, data)
|
|
|
|
-- here we handle 201 packets
|
2023-03-05 10:53:59 -08:00
|
|
|
code = data[2]
|
2023-03-05 14:31:25 -08:00
|
|
|
print('Got 0x201 packet code ' ..toHexString(code) ..': TP Response ' ..arrayToString(data))
|
2023-03-05 10:53:59 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if code ~= 0xD0 then
|
|
|
|
print('BAD RESPONSE, trying longer sleep')
|
|
|
|
byeByeStateCounter = 13 * tickRate
|
|
|
|
return
|
|
|
|
end
|
2023-03-05 11:19:02 -08:00
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
cuId = data[6] * 256 + data[5]
|
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
out = { 0xA0, 0x0F, 0x8A, 0xFF, 0x32, 0xFF }
|
|
|
|
print("Responding to 0x201 packet from " ..cuId ..": Saying hello " ..arrayToString(out))
|
2023-01-13 12:41:57 -08:00
|
|
|
txCan(1, cuId, 0, out)
|
|
|
|
end
|
|
|
|
|
|
|
|
local sendCounter = 0
|
|
|
|
local packetCounter = 1
|
|
|
|
local payLoadIndex = 0
|
|
|
|
|
|
|
|
local groupIndex = 1
|
|
|
|
|
|
|
|
withErrorCodes = 0
|
|
|
|
|
2023-01-31 21:15:02 -08:00
|
|
|
byeByeStateCounter = 2
|
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
function nextReq()
|
2023-03-05 14:31:25 -08:00
|
|
|
local result = 0x10 + sendCounter
|
|
|
|
sendCounter = sendCounter + 1
|
2023-01-13 12:41:57 -08:00
|
|
|
if sendCounter == 16 then
|
|
|
|
sendCounter = 0
|
|
|
|
end
|
2023-03-05 14:31:25 -08:00
|
|
|
return result
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|
2023-01-31 21:15:02 -08:00
|
|
|
function startTp()
|
2023-03-05 14:31:25 -08:00
|
|
|
sendCounter = 0
|
|
|
|
packetCounter = 1
|
|
|
|
payLoadIndex = 0
|
|
|
|
print("Sending 0x200: Starting TP with TCU")
|
2023-01-31 21:15:02 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
txCan(1, VWTP_OUT, 0, { cuType, 0xC0, 0x00, 0x10, 0x00, 0x03, 0x01 })
|
2023-01-31 21:15:02 -08:00
|
|
|
end
|
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
function requestErrorCodes()
|
2023-03-05 14:31:25 -08:00
|
|
|
out = { nextReq(), 0x00, 0x04, 0x18, 0x02, 0xFF, 0x00 }
|
|
|
|
txCan(1, cuId, 0, out)
|
|
|
|
print("Requesting error codes " ..arrayToString(out))
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|
2023-01-13 12:54:00 -08:00
|
|
|
function requestEraseCodes()
|
2023-03-05 14:31:25 -08:00
|
|
|
out = { nextReq(), 00, 0x03, 0x14, 0xFF, 0x00 }
|
|
|
|
txCan(1, cuId, 0, out)
|
|
|
|
print("******************* Requesting to Erase Code(s) *****************************")
|
2023-01-13 12:54:00 -08:00
|
|
|
end
|
|
|
|
|
2023-02-03 16:07:20 -08:00
|
|
|
function scheduleTpRestart()
|
2023-03-05 14:31:25 -08:00
|
|
|
byeByeStateCounter = 3 * tickRate
|
2023-02-03 16:07:20 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
keepAliveCounter = 0
|
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
function onCanTester(bus, id, dlc, data)
|
|
|
|
-- here we handle 300 packets
|
2023-03-05 14:31:25 -08:00
|
|
|
scheduleTpRestart()
|
2023-01-13 12:41:57 -08:00
|
|
|
|
|
|
|
if data[1] == 0xA3 then
|
2023-03-05 14:31:25 -08:00
|
|
|
print ("0x300 A3 Keep-alive withErrorCodes=" ..withErrorCodes)
|
2023-01-13 12:41:57 -08:00
|
|
|
txCan(1, cuId, 0, { 0xA1, 0x0F, 0x8A, 0xFF, 0x4A, 0xFF })
|
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if withErrorCodes > 0 then
|
2024-02-10 18:41:19 -08:00
|
|
|
withErrorsCounter = withErrorsCounter + 1
|
2023-03-05 14:31:25 -08:00
|
|
|
requestEraseCodes()
|
|
|
|
else
|
|
|
|
keepAliveCounter = keepAliveCounter + 1
|
|
|
|
if keepAliveCounter > 4 then
|
|
|
|
requestErrorCodes()
|
2023-02-03 16:07:20 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
keepAliveCounter = 0
|
|
|
|
end
|
|
|
|
end
|
2023-01-13 12:41:57 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if data[1] == 0xA1 then
|
2023-03-05 14:31:25 -08:00
|
|
|
print ("Happy 300 startup packet " ..arrayToString(data))
|
2023-01-13 12:41:57 -08:00
|
|
|
txCan(1, cuId, 0, { nextReq(), 0x00, 0x02, 0x10, 0x89 })
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if data[1] == 0xA8 then
|
2023-03-05 10:53:59 -08:00
|
|
|
print ("0x300 They said Bye-Bye")
|
2023-01-13 12:41:57 -08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-03-05 10:53:59 -08:00
|
|
|
print('0x300 Got from tester ' ..arrayToString(data))
|
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
if data[1] == 0x10 and dlc == 5 then
|
|
|
|
ackPacket = 0xB0 + packetCounter
|
|
|
|
print ("Sending ACK Bx " ..ackPacket)
|
|
|
|
txCan(1, cuId, 0, { ackPacket })
|
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if data[3] == 2 and data[4] == 0x50 then
|
|
|
|
print('Got Hello2 Response ' ..arrayToString(data))
|
2023-01-13 12:41:57 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
requestErrorCodes()
|
|
|
|
end
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
top4 = math.floor(data[1] / 16)
|
|
|
|
|
|
|
|
if top4 == 0xB then
|
|
|
|
print("Got 0xBx ACK")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if top4 == 2 or top4 == 1 then
|
|
|
|
print ("Looks like payload index " ..payLoadIndex ..": " ..arrayToString(data))
|
|
|
|
|
|
|
|
if payLoadIndex == 0 and data[4] == 0x58 then
|
2023-03-05 14:31:25 -08:00
|
|
|
len = data[3]
|
|
|
|
print("Looks like CODES_REQ response of length " ..len)
|
2023-01-13 12:41:57 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if len ~= 2 then
|
|
|
|
print("HAVE CODES " ..len)
|
|
|
|
withErrorCodes = 1
|
|
|
|
else
|
|
|
|
withErrorCodes = 0
|
|
|
|
end
|
2023-01-13 12:41:57 -08:00
|
|
|
|
2024-02-10 16:11:22 -08:00
|
|
|
-- warning: random custom packet ID!
|
2024-02-10 18:41:19 -08:00
|
|
|
txCan(1, 0x699, 0, { withErrorCodes })
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
payLoadIndex = payLoadIndex + 1
|
|
|
|
|
|
|
|
packetCounter = packetCounter + 1
|
|
|
|
if packetCounter > 15 then
|
|
|
|
packetCounter = 0
|
|
|
|
end
|
|
|
|
|
2023-01-13 12:50:03 -08:00
|
|
|
ackPacket = 0xB0 + packetCounter
|
|
|
|
print ("Sending payload ACK " ..ackPacket)
|
2023-03-05 14:31:25 -08:00
|
|
|
txCan(1, cuId, 0, { ackPacket })
|
2023-01-13 12:50:03 -08:00
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
if top4 == 1 then
|
|
|
|
payLoadIndex = 0
|
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if data[2] == 0 and data[3] == 2 and data[4] == 0x58 then
|
2024-02-10 18:41:19 -08:00
|
|
|
print("****************** NO CODES had " .. withErrorsCounter)
|
2023-03-05 14:31:25 -08:00
|
|
|
end
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
print('Got unexpected ' ..arrayToString(data))
|
|
|
|
end
|
|
|
|
|
|
|
|
canRxAdd(VWTP_IN, onCanHello)
|
|
|
|
canRxAdd(VWTP_TESTER, onCanTester)
|
|
|
|
|
2023-01-31 21:15:02 -08:00
|
|
|
startTp()
|
2023-01-13 12:41:57 -08:00
|
|
|
|
|
|
|
function onTick()
|
2023-01-31 21:15:02 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if byeByeStateCounter > 0 then
|
|
|
|
byeByeStateCounter = byeByeStateCounter - 1
|
|
|
|
end
|
2023-01-31 21:15:02 -08:00
|
|
|
|
2023-03-05 14:31:25 -08:00
|
|
|
if byeByeStateCounter == 1 then
|
|
|
|
scheduleTpRestart()
|
|
|
|
startTp()
|
|
|
|
end
|
2023-01-31 21:15:02 -08:00
|
|
|
|
2023-01-13 12:41:57 -08:00
|
|
|
end
|
|
|
|
|