panda/tests/loopback_test.py

122 lines
2.9 KiB
Python
Raw Normal View History

2017-04-07 09:11:36 +08:00
#!/usr/bin/env python
from __future__ import print_function
2017-04-07 09:11:36 +08:00
import os
import sys
import time
import random
2017-04-07 09:11:36 +08:00
from hexdump import hexdump
from itertools import permutations
2017-04-07 09:11:36 +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():
return "test"+os.urandom(10)
def run_test():
pandas = Panda.list()
print(pandas)
2017-04-18 22:34:56 +08:00
if len(pandas) == 0:
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-05-03 00:54:57 +08:00
run_test_w_pandas(pandas)
def run_test_w_pandas(pandas):
2017-04-07 09:11:36 +08:00
h = map(lambda x: Panda(x), pandas)
print(h)
2017-04-07 09:11:36 +08:00
for hh in h:
hh.set_controls_allowed(True)
2017-05-02 14:40:49 +08:00
2017-04-07 09:11:36 +08:00
# test both directions
for ho in permutations(range(len(h)), r=2):
print("***************** TESTING", ho)
2017-04-07 09:11:36 +08:00
# **** test health packet ****
print("health", ho[0], h[ho[0]].health())
2017-04-07 09:11:36 +08:00
# **** test K/L line loopback ****
for bus in [2,3]:
# flush the output
h[ho[1]].kline_drain(bus=bus)
# send the characters
st = get_test_string()
st = "\xaa"+chr(len(st)+3)+st
h[ho[0]].kline_send(st, bus=bus, checksum=False)
# check for receive
ret = h[ho[1]].kline_drain(bus=bus)
hexdump(st)
hexdump(ret)
assert st == ret
print("K/L pass", bus, ho)
2017-04-07 09:11:36 +08:00
# **** test can line loopback ****
for bus in [0,1,4,5,6]:
print("test can", bus)
2017-04-07 09:11:36 +08:00
# flush
cans_echo = h[ho[0]].can_recv()
cans_loop = h[ho[1]].can_recv()
# set GMLAN mode
if bus == 5:
h[ho[0]].set_gmlan(True,2)
h[ho[1]].set_gmlan(True,2)
2017-04-07 09:11:36 +08:00
bus = 1 # GMLAN is multiplexed with CAN2
elif bus == 6:
# on REV B panda, this just retests CAN2 GMLAN
h[ho[0]].set_gmlan(True,3)
h[ho[1]].set_gmlan(True,3)
bus = 4 # GMLAN is also multiplexed with CAN3
2017-04-07 09:11:36 +08:00
else:
h[ho[0]].set_gmlan(False)
h[ho[1]].set_gmlan(False)
# send the characters
# pick addresses high enough to not conflict with honda code
at = random.randint(1024, 2000)
st = get_test_string()[0:8]
h[ho[0]].can_send(at, st, bus)
time.sleep(0.1)
# check for receive
cans_echo = h[ho[0]].can_recv()
cans_loop = h[ho[1]].can_recv()
print(bus, cans_echo, 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
assert cans_echo[0][3] == bus+2
if cans_loop[0][3] != bus:
print("EXPECTED %d GOT %d" % (bus, cans_loop[0][3]))
2017-04-07 09:11:36 +08:00
assert cans_loop[0][3] == bus
print("CAN pass", bus, ho)
2017-04-07 09:11:36 +08:00
if __name__ == "__main__":
if len(sys.argv) > 1:
for i in range(int(sys.argv[1])):
run_test()
else :
i = 0
while True:
print("************* testing %d" % i)
run_test()
2017-04-07 09:11:36 +08:00
i += 1