panda/tests/automated/helpers.py

73 lines
1.8 KiB
Python
Raw Normal View History

2017-07-30 09:49:53 -06:00
import os
2017-08-29 11:46:08 -06:00
import sys
import time
2017-08-12 19:42:24 -06:00
import random
2017-08-29 12:01:24 -06:00
import subprocess
2017-08-29 11:46:08 -06:00
from panda import Panda
from nose.tools import timed, assert_equal, assert_less, assert_greater
2017-07-30 09:26:48 -06:00
def connect_wo_esp():
# connect to the panda
p = Panda()
# power down the ESP
p.set_esp_power(False)
# clear old junk
while len(p.can_recv()) > 0:
pass
2017-08-24 18:52:54 -06:00
return p
2017-07-30 09:49:53 -06:00
def connect_wifi():
p = Panda()
ssid, pw = p.get_serial()
ssid = ssid.strip("\x00")
assert(ssid.isalnum())
assert(pw.isalnum())
2017-08-29 12:01:24 -06:00
ssid = str("panda-" + ssid)
2017-07-30 09:49:53 -06:00
2017-08-29 11:46:08 -06:00
print("WIFI: connecting to %s" % ssid)
if sys.platform == "darwin":
os.system("networksetup -setairportnetwork en0 %s %s" % (ssid, pw))
else:
2017-08-29 12:01:24 -06:00
cnt = 0
while 1:
os.system("nmcli device wifi rescan")
wifi_scan = filter(lambda x: ssid in x, subprocess.check_output(["nmcli","dev", "wifi", "list"]).split("\n"))
if len(wifi_scan) != 0:
break
time.sleep(0.1)
# 100 tries, ~10 seconds max
cnt += 1
assert cnt < 100
2017-08-29 11:46:08 -06:00
os.system("nmcli d wifi connect %s password %s" % (ssid, pw))
2017-08-29 12:01:24 -06:00
# TODO: confirm that it's connected to the right panda
2017-07-30 09:49:53 -06:00
2017-08-12 19:42:24 -06:00
def time_many_sends(p, bus, precv=None, msg_count=100, msg_id=None):
if precv == None:
precv = p
2017-08-12 19:42:24 -06:00
if msg_id == None:
msg_id = random.randint(0x100, 0x200)
2017-07-30 09:26:48 -06:00
st = time.time()
2017-08-12 19:42:24 -06:00
p.can_send_many([(msg_id, 0, "\xaa"*8, bus)]*msg_count)
2017-07-30 09:26:48 -06:00
r = []
while len(r) < (msg_count*2) and (time.time() - st) < 3:
r.extend(precv.can_recv())
2017-07-30 09:26:48 -06:00
2017-08-12 19:42:24 -06:00
sent_echo = filter(lambda x: x[3] == 0x80 | bus and x[0] == msg_id, r)
loopback_resp = filter(lambda x: x[3] == bus and x[0] == msg_id, r)
2017-07-30 09:26:48 -06:00
assert_equal(len(sent_echo), msg_count)
assert_equal(len(loopback_resp), msg_count)
2017-07-30 09:26:48 -06:00
et = (time.time()-st)*1000.0
comp_kbps = (1+11+1+1+1+4+8*8+15+1+1+1+7)*msg_count / et
2017-07-30 09:26:48 -06:00
return comp_kbps
2017-07-30 09:49:53 -06:00