diff --git a/tests/elm_car_simulator.py b/tests/elm_car_simulator.py index 58ae7d1c..5449bd36 100755 --- a/tests/elm_car_simulator.py +++ b/tests/elm_car_simulator.py @@ -5,6 +5,7 @@ import sys import os import struct import binascii +import time sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) from panda import Panda @@ -15,36 +16,35 @@ if __name__ == "__main__": p.set_can_speed_kbps(0, 500) p.set_safety_mode(Panda.SAFETY_ALLOUTPUT) + multipart_data = None + while True: can_recv = p.can_recv() - if can_recv: - a, b, c, d = can_recv[0] - #print(hex(a),b,repr(c),d) for address, ts, data, src in can_recv: if src is 0 and len(data) >= 2: #Check functional address of 11 bit and 29 bit CAN dat_trim = data[1:1+data[0]] if address == 0x18db33f1: continue - print("MSG", binascii.hexlify(dat_trim), "Addr:", hex(address), "Mode:", hex(dat_trim[0])[2:].zfill(2), - "PID:", hex(dat_trim[1])[2:].zfill(2), binascii.hexlify(dat_trim[2:]) if dat_trim[2:] else '') - if address == 0x7DF:# or address == 0x18db33f1: + print("MSG", binascii.hexlify(dat_trim), "Addr:", hex(address), + "Mode:", hex(dat_trim[0])[2:].zfill(2), + "PID:", hex(dat_trim[1])[2:].zfill(2), "Len:", data[0], + binascii.hexlify(dat_trim[2:]) if dat_trim[2:] else '') + + if address == 0x7DF or address == 0x7E0:# or address == 0x18db33f1: outmsg = None - """ - MSG b'0133' Addr: 0x7df Mode: 01 PID: 33 - MSG b'010b' Addr: 0x7df Mode: 01 PID: 0b - MSG b'0110' Addr: 0x7df Mode: 01 PID: 10 - MSG b'010c' Addr: 0x7df Mode: 01 PID: 0c - Reply b'0441001a' - MSG b'010d' Addr: 0x7df Mode: 01 PID: 0d - Reply b'034100' - MSG b'0104' Addr: 0x7df Mode: 01 PID: 04 - MSG b'0133' Addr: 0x7df Mode: 01 PID: 33 - MSG b'010c' Addr: 0x7df Mode: 01 PID: 0c - Reply b'0441001a' - MSG b'0111' Addr: 0x7df Mode: 01 PID: 11 - Reply b'034100' - """ - if data[1] == 0x01: # Mode: Show current data + if data[:3] == b'\x30\x00\x00' and len(multipart_data): + print("Request for more data"); + msgnum = 1 + while(multipart_data): + datalen = min(7, len(multipart_data)) + msgpiece = struct.pack("B", 0x20 | msgnum) + multipart_data[:datalen] + print(" Reply", binascii.hexlify(msgpiece)) + p.can_send(outaddr, msgpiece + b'\x00'*(8-len(msgpiece)), 0) + multipart_data = multipart_data[7:] + msgnum = (msgnum+1)%0x10 + time.sleep(0.01) + + elif data[1] == 0x01: # Mode: Show current data if data[2] == 0x00: #List supported things outmsg = b"\xff\xff\xff\xfe"#b"\xBE\x1F\xB8\x10" #Bitfield, random features elif data[2] == 0x01: # Monitor Status since DTC cleared @@ -65,12 +65,26 @@ if __name__ == "__main__": outmsg = b"\x90" elif data[2] == 0x33: # Absolute Barometric Pressure outmsg = b"\x90" + elif data[1] == 0x09: # Mode: Request vehicle information + if data[2] == 0x02: # Show VIN + outmsg = b"1D4GP00R55B123456" + if data[2] == 0xFF: # test very long multi message + outmsg = b"\xAA"*(0xFFF-3) + if outmsg: - #print("Got req msg") - outmsg = struct.pack("BBB", len(outmsg)+2, 0x40|data[1], data[2]) + outmsg - outmsg += b'\x00'*(8-len(outmsg)) - outaddr = 0x7E8 if address == 0x7DF else 0 - #b"\x06\x41\x00\xBE\x1F\xB8\x10\x00" - #Should display on elm '41 00 BE 1F B8 10' - print(" Reply", binascii.hexlify(outmsg))#[:outmsg[0]]+1)) - p.can_send(outaddr, outmsg, 0) + outaddr = 0x7E8 if address == 0x7DF or address == 0x7E0 else 0 + + if len(outmsg) <= 5: + outmsg = struct.pack("BBB", len(outmsg)+2, 0x40|data[1], data[2]) + outmsg + outmsg += b'\x00'*(8-len(outmsg)) + print(" Reply", binascii.hexlify(outmsg)) + p.can_send(outaddr, outmsg, 0) + else: + first_msg_len = min(3, len(outmsg)%7) + payload_len = len(outmsg)+3 + msgpiece = struct.pack("BBBBB", 0x10 | ((payload_len>>8)&0xF), + payload_len&0xFF, + 0x40|data[1], data[2], 1) + outmsg[:first_msg_len] + print(" Reply", binascii.hexlify(msgpiece)) + p.can_send(outaddr, msgpiece + b'\x00'*(8-len(msgpiece)), 0) + multipart_data = outmsg[first_msg_len:]