panda/tests/automated/helpers.py

92 lines
2.4 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
2018-02-13 11:13:30 -07:00
import requests
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()
2017-08-29 12:34:47 -06:00
dongle_id, pw = p.get_serial()
assert(dongle_id.isalnum())
_connect_wifi(dongle_id, pw)
def _connect_wifi(dongle_id, pw):
ssid = str("panda-" + dongle_id)
2017-07-30 09:49:53 -06:00
2017-08-29 11:46:08 -06:00
print("WIFI: connecting to %s" % ssid)
2018-02-13 11:13:30 -07:00
while 1:
if sys.platform == "darwin":
os.system("networksetup -setairportnetwork en0 %s %s" % (ssid, pw))
else:
cnt = 0
MAX_TRIES = 10
while cnt < MAX_TRIES:
print "WIFI: scanning %d" % cnt
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)
# MAX_TRIES tries, ~10 seconds max
cnt += 1
assert cnt < MAX_TRIES
if "-pair" in wifi_scan[0]:
os.system("nmcli d wifi connect %s-pair" % (ssid))
2018-02-14 16:27:10 -07:00
# fetch webpage
2018-02-13 11:13:30 -07:00
print "connecting to insecure network to secure"
2018-02-14 16:27:10 -07:00
r = requests.get("http://192.168.0.10/")
assert r.status_code==200
print "securing"
2018-02-13 11:13:30 -07:00
try:
r = requests.get("http://192.168.0.10/secure", timeout=0.01)
except requests.exceptions.Timeout:
pass
else:
os.system("nmcli d wifi connect %s password %s" % (ssid, pw))
2017-08-29 12:01:24 -06:00
break
# 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