2017-04-07 09:11:36 +08:00
|
|
|
#!/usr/bin/env python
|
2017-06-14 09:50:47 +08:00
|
|
|
from __future__ import print_function
|
2017-04-07 09:11:36 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import random
|
2017-07-04 05:35:48 +08:00
|
|
|
import argparse
|
2017-06-14 09:50:47 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
from hexdump import hexdump
|
2017-05-03 01:20:05 +08:00
|
|
|
from itertools import permutations
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-06-14 09:50:47 +08:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
|
|
|
|
from panda import Panda
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
def get_test_string():
|
2017-06-28 10:47:53 +08:00
|
|
|
return b"test"+os.urandom(10)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-04 05:35:48 +08:00
|
|
|
def run_test(can_speeds, gmlan_speeds, sleep_duration=0):
|
2017-04-07 09:11:36 +08:00
|
|
|
pandas = Panda.list()
|
2017-06-14 09:50:47 +08:00
|
|
|
print(pandas)
|
2017-04-18 22:34:56 +08:00
|
|
|
|
|
|
|
if len(pandas) == 0:
|
2017-06-14 09:50:47 +08:00
|
|
|
print("NO PANDAS")
|
2017-04-18 22:34:56 +08:00
|
|
|
assert False
|
|
|
|
|
|
|
|
if len(pandas) == 1:
|
|
|
|
# if we only have one on USB, assume the other is on wifi
|
|
|
|
pandas.append("WIFI")
|
2017-07-04 05:35:48 +08:00
|
|
|
run_test_w_pandas(pandas, can_speeds, gmlan_speeds, sleep_duration)
|
2017-05-03 00:54:57 +08:00
|
|
|
|
2017-07-11 16:50:46 +08:00
|
|
|
GMLAN_BUS = 3 # Virtual 'CAN 4'
|
|
|
|
|
2017-07-04 05:35:48 +08:00
|
|
|
def run_test_w_pandas(pandas, can_speeds, gmlan_speeds, sleep_duration=0):
|
2017-06-28 10:47:53 +08:00
|
|
|
h = list(map(lambda x: Panda(x), pandas))
|
|
|
|
print("H", h)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-05-03 01:20:05 +08:00
|
|
|
for hh in h:
|
2017-07-11 09:14:53 +08:00
|
|
|
hh.set_controls_mode()
|
2017-05-02 14:40:49 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# test both directions
|
2017-05-03 01:20:05 +08:00
|
|
|
for ho in permutations(range(len(h)), r=2):
|
2017-06-14 09:50:47 +08:00
|
|
|
print("***************** TESTING", ho)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-06 02:55:59 +08:00
|
|
|
panda_snd, panda_rcv = h[ho[0]], h[ho[1]]
|
|
|
|
|
|
|
|
if(panda_snd._serial == "WIFI"):
|
|
|
|
print(" *** Can not send can data over wifi panda. Skipping! ***")
|
|
|
|
continue
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# **** test health packet ****
|
2017-07-06 02:55:59 +08:00
|
|
|
print("health", ho[0], panda_snd.health())
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# **** test K/L line loopback ****
|
|
|
|
for bus in [2,3]:
|
|
|
|
# flush the output
|
2017-07-06 02:55:59 +08:00
|
|
|
panda_rcv.kline_drain(bus=bus)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# send the characters
|
|
|
|
st = get_test_string()
|
2017-06-28 10:47:53 +08:00
|
|
|
st = b"\xaa"+chr(len(st)+3).encode()+st
|
2017-07-06 02:55:59 +08:00
|
|
|
panda_snd.kline_send(st, bus=bus, checksum=False)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# check for receive
|
2017-07-06 02:55:59 +08:00
|
|
|
ret = panda_rcv.kline_drain(bus=bus)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-06-29 05:50:00 +08:00
|
|
|
print("ST Data:")
|
|
|
|
hexdump(st)
|
|
|
|
print("RET Data:")
|
|
|
|
hexdump(ret)
|
2017-04-07 09:11:36 +08:00
|
|
|
assert st == ret
|
2017-06-28 10:47:53 +08:00
|
|
|
print("K/L pass", bus, ho, "\n")
|
2017-07-04 05:35:48 +08:00
|
|
|
time.sleep(sleep_duration)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# **** test can line loopback ****
|
2017-07-04 05:35:48 +08:00
|
|
|
for bus, gmlan in [(0, None), (1, False), (2, False), (1, True), (2, True)]:
|
|
|
|
print("\ntest can", bus, "gmlan" if gmlan else "")
|
2017-04-07 09:11:36 +08:00
|
|
|
# flush
|
2017-07-06 02:55:59 +08:00
|
|
|
cans_echo = panda_snd.can_recv()
|
|
|
|
cans_loop = panda_rcv.can_recv()
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# set GMLAN mode
|
2017-07-04 05:35:48 +08:00
|
|
|
if(gmlan is not None):
|
2017-07-06 02:55:59 +08:00
|
|
|
panda_snd.set_gmlan(bus, gmlan)
|
|
|
|
panda_rcv.set_gmlan(bus, gmlan)
|
|
|
|
|
2017-07-07 06:17:33 +08:00
|
|
|
if gmlan:
|
|
|
|
print("Setting GMLAN %d Speed to %d" % (bus, gmlan_speeds[bus]))
|
|
|
|
panda_snd.set_can_baud(bus, gmlan_speeds[bus])
|
|
|
|
panda_rcv.set_can_baud(bus, gmlan_speeds[bus])
|
2017-07-11 16:50:46 +08:00
|
|
|
bus = GMLAN_BUS
|
2017-07-07 06:17:33 +08:00
|
|
|
else:
|
2017-07-11 16:50:46 +08:00
|
|
|
print("bus", bus)
|
2017-07-07 06:17:33 +08:00
|
|
|
print("Setting CanBus %d Speed to %d" % (bus, can_speeds[bus]))
|
|
|
|
panda_snd.set_can_baud(bus, can_speeds[bus])
|
|
|
|
panda_rcv.set_can_baud(bus, can_speeds[bus])
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# send the characters
|
|
|
|
# pick addresses high enough to not conflict with honda code
|
|
|
|
at = random.randint(1024, 2000)
|
|
|
|
st = get_test_string()[0:8]
|
2017-07-06 02:55:59 +08:00
|
|
|
panda_snd.can_send(at, st, bus)
|
2017-04-07 09:11:36 +08:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
# check for receive
|
2017-07-06 02:55:59 +08:00
|
|
|
cans_echo = panda_snd.can_recv()
|
|
|
|
cans_loop = panda_rcv.can_recv()
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-06-28 10:47:53 +08:00
|
|
|
print("Bus", bus, "echo", cans_echo, "loop", cans_loop)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
assert len(cans_echo) == 1
|
|
|
|
assert len(cans_loop) == 1
|
|
|
|
|
|
|
|
assert cans_echo[0][0] == at
|
|
|
|
assert cans_loop[0][0] == at
|
|
|
|
|
|
|
|
assert cans_echo[0][2] == st
|
|
|
|
assert cans_loop[0][2] == st
|
|
|
|
|
2017-07-01 07:41:36 +08:00
|
|
|
assert cans_echo[0][3] == 0x80 | bus
|
2017-06-10 05:11:37 +08:00
|
|
|
if cans_loop[0][3] != bus:
|
2017-06-14 09:50:47 +08:00
|
|
|
print("EXPECTED %d GOT %d" % (bus, cans_loop[0][3]))
|
2017-04-07 09:11:36 +08:00
|
|
|
assert cans_loop[0][3] == bus
|
2017-07-04 05:35:48 +08:00
|
|
|
time.sleep(sleep_duration)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-06-14 09:50:47 +08:00
|
|
|
print("CAN pass", bus, ho)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-07-04 05:35:48 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-n", type=int, help="Number of test iterations to run")
|
|
|
|
parser.add_argument("-can1baud", type=int, help="Baud Rate of CAN1", default=500000)
|
|
|
|
parser.add_argument("-can2baud", type=int, help="Baud Rate of CAN2", default=500000)
|
|
|
|
parser.add_argument("-can3baud", type=int, help="Baud Rate of CAN3", default=500000)
|
|
|
|
parser.add_argument("-gmlan2baud", type=int, help="Baud Rate of GMLAN2", default=33333)
|
|
|
|
parser.add_argument("-gmlan3baud", type=int, help="Baud Rate of GMLAN3", default=33333)
|
|
|
|
parser.add_argument("-sleep", type=int, help="Sleep time between tests", default=0)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
can_speeds = (args.can1baud, args.can2baud, args.can3baud)
|
|
|
|
gmlan_speeds = (None, args.gmlan2baud, args.gmlan2baud)
|
|
|
|
|
|
|
|
if args.n is None:
|
2017-06-14 09:50:47 +08:00
|
|
|
while True:
|
2017-07-04 05:35:48 +08:00
|
|
|
run_test(can_speeds, gmlan_speeds, sleep_duration=args.sleep)
|
|
|
|
else:
|
|
|
|
for i in range(args.n):
|
|
|
|
run_test(can_speeds, gmlan_speeds, sleep_duration=args.sleep)
|