You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					193 lines
				
				6.0 KiB
			
		
		
			
		
	
	
					193 lines
				
				6.0 KiB
			| 
											8 years ago
										 | import os
 | ||
|  | import sys
 | ||
|  | import time
 | ||
|  | import random
 | ||
|  | import subprocess
 | ||
| 
											8 years ago
										 | import requests
 | ||
| 
											7 years ago
										 | from functools import wraps
 | ||
| 
											8 years ago
										 | from panda import Panda
 | ||
|  | from nose.tools import timed, assert_equal, assert_less, assert_greater
 | ||
| 
											7 years ago
										 | from parameterized import parameterized, param
 | ||
|  | 
 | ||
|  | test_white_and_grey = parameterized([param(panda_color="White"),
 | ||
|  |                                      param(panda_color="Grey")])
 | ||
|  | test_white = parameterized([param(panda_color="White")])
 | ||
|  | test_grey = parameterized([param(panda_color="Grey")])
 | ||
|  | test_two_panda = parameterized([param(panda_color=["Grey", "White"]),
 | ||
|  |                                 param(panda_color=["White", "Grey"])])
 | ||
|  | 
 | ||
|  | _serials = {}
 | ||
|  | def get_panda_serial(is_grey=None):
 | ||
|  |   global _serials
 | ||
|  |   if is_grey not in _serials:
 | ||
|  |     for serial in Panda.list():
 | ||
|  |       p = Panda(serial=serial)
 | ||
|  |       if is_grey is None or p.is_grey() == is_grey:
 | ||
|  |         _serials[is_grey] = serial
 | ||
|  |         return serial
 | ||
|  |     raise IOError("Panda not found. is_grey: {}".format(is_grey))
 | ||
|  |   else:
 | ||
|  |     return _serials[is_grey]
 | ||
|  | 
 | ||
|  | def connect_wo_esp(serial=None):
 | ||
| 
											8 years ago
										 |   # connect to the panda
 | ||
| 
											7 years ago
										 |   p = Panda(serial=serial)
 | ||
| 
											8 years ago
										 | 
 | ||
|  |   # power down the ESP
 | ||
|  |   p.set_esp_power(False)
 | ||
|  | 
 | ||
|  |   # clear old junk
 | ||
|  |   while len(p.can_recv()) > 0:
 | ||
|  |     pass
 | ||
|  | 
 | ||
|  |   return p
 | ||
|  | 
 | ||
| 
											7 years ago
										 | def connect_wifi(serial=None):
 | ||
|  |   p = Panda(serial=serial)
 | ||
|  |   p.set_esp_power(True)
 | ||
| 
											8 years ago
										 |   dongle_id, pw = p.get_serial()
 | ||
|  |   assert(dongle_id.isalnum())
 | ||
|  |   _connect_wifi(dongle_id, pw)
 | ||
|  | 
 | ||
| 
											7 years ago
										 | FNULL = open(os.devnull, 'w')
 | ||
| 
											8 years ago
										 | def _connect_wifi(dongle_id, pw, insecure_okay=False):
 | ||
| 
											8 years ago
										 |   ssid = str("panda-" + dongle_id)
 | ||
|  | 
 | ||
| 
											7 years ago
										 |   r = subprocess.call(["ping", "-W", "4", "-c", "1", "192.168.0.10"], stdout=FNULL, stderr=subprocess.STDOUT)
 | ||
|  |   if not r:
 | ||
|  |     #Can already ping, try connecting on wifi
 | ||
|  |     try:
 | ||
|  |       p = Panda("WIFI")
 | ||
|  |       p.get_serial()
 | ||
|  |       print("Already connected")
 | ||
|  |       return
 | ||
|  |     except:
 | ||
|  |       pass
 | ||
|  | 
 | ||
| 
											8 years ago
										 |   print("WIFI: connecting to %s" % ssid)
 | ||
|  | 
 | ||
| 
											8 years ago
										 |   while 1:
 | ||
|  |     if sys.platform == "darwin":
 | ||
|  |       os.system("networksetup -setairportnetwork en0 %s %s" % (ssid, pw))
 | ||
|  |     else:
 | ||
| 
											8 years ago
										 |       wlan_interface = subprocess.check_output(["sh", "-c", "iw dev | awk '/Interface/ {print $2}'"]).strip()
 | ||
| 
											8 years ago
										 |       cnt = 0
 | ||
|  |       MAX_TRIES = 10
 | ||
|  |       while cnt < MAX_TRIES:
 | ||
| 
											7 years ago
										 |         print("WIFI: scanning %d" % cnt)
 | ||
|  |         os.system("iwlist %s scanning > /dev/null" % wlan_interface)
 | ||
| 
											8 years ago
										 |         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))
 | ||
| 
											7 years ago
										 |         connect_cnt = 0
 | ||
|  |         MAX_TRIES = 20
 | ||
|  |         while connect_cnt < MAX_TRIES:
 | ||
|  |           connect_cnt += 1
 | ||
|  |           r = subprocess.call(["ping", "-W", "4", "-c", "1", "192.168.0.10"], stdout=FNULL, stderr=subprocess.STDOUT)
 | ||
|  |           if r:
 | ||
|  |             print("Waiting for panda to ping...")
 | ||
|  |             time.sleep(0.1)
 | ||
|  |           else:
 | ||
|  |             break
 | ||
| 
											8 years ago
										 |         if insecure_okay:
 | ||
|  |           break
 | ||
|  |         # fetch webpage
 | ||
| 
											7 years ago
										 |         print("connecting to insecure network to secure")
 | ||
|  |         try:
 | ||
|  |           r = requests.get("http://192.168.0.10/")
 | ||
|  |         except requests.ConnectionError:
 | ||
|  |           r = requests.get("http://192.168.0.10/")
 | ||
| 
											8 years ago
										 |         assert r.status_code==200
 | ||
|  | 
 | ||
| 
											7 years ago
										 |         print("securing")
 | ||
| 
											8 years ago
										 |         try:
 | ||
|  |           r = requests.get("http://192.168.0.10/secure", timeout=0.01)
 | ||
|  |         except requests.exceptions.Timeout:
 | ||
| 
											7 years ago
										 |           print("timeout http request to secure")
 | ||
| 
											8 years ago
										 |           pass
 | ||
|  |       else:
 | ||
| 
											7 years ago
										 |         ret = os.system("nmcli d wifi connect %s password %s" % (ssid, pw))
 | ||
|  |         if os.WEXITSTATUS(ret) == 0:
 | ||
|  |           #check ping too
 | ||
|  |           ping_ok = False
 | ||
|  |           connect_cnt = 0
 | ||
|  |           MAX_TRIES = 10
 | ||
|  |           while connect_cnt < MAX_TRIES:
 | ||
|  |             connect_cnt += 1
 | ||
|  |             r = subprocess.call(["ping", "-W", "4", "-c", "1", "192.168.0.10"], stdout=FNULL, stderr=subprocess.STDOUT)
 | ||
|  |             if r:
 | ||
|  |               print("Waiting for panda to ping...")
 | ||
|  |               time.sleep(0.1)
 | ||
|  |             else:
 | ||
|  |               ping_ok = True
 | ||
|  |               break
 | ||
|  |           if ping_ok:
 | ||
|  |             break
 | ||
|  | 
 | ||
| 
											8 years ago
										 |   # TODO: confirm that it's connected to the right panda
 | ||
|  | 
 | ||
| 
											7 years ago
										 | def time_many_sends(p, bus, precv=None, msg_count=100, msg_id=None, two_pandas=False):
 | ||
| 
											8 years ago
										 |   if precv == None:
 | ||
|  |     precv = p
 | ||
|  |   if msg_id == None:
 | ||
|  |     msg_id = random.randint(0x100, 0x200)
 | ||
| 
											7 years ago
										 |   if p == precv and two_pandas:
 | ||
|  |     raise ValueError("Cannot have two pandas that are the same panda")
 | ||
| 
											8 years ago
										 | 
 | ||
|  |   st = time.time()
 | ||
|  |   p.can_send_many([(msg_id, 0, "\xaa"*8, bus)]*msg_count)
 | ||
|  |   r = []
 | ||
| 
											7 years ago
										 |   r_echo = []
 | ||
|  |   r_len_expected = msg_count if two_pandas else msg_count*2
 | ||
|  |   r_echo_len_exected = msg_count if two_pandas else 0
 | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |   while len(r) < r_len_expected and (time.time() - st) < 5:
 | ||
| 
											8 years ago
										 |     r.extend(precv.can_recv())
 | ||
| 
											7 years ago
										 |   et = time.time()
 | ||
|  |   if two_pandas:
 | ||
|  |     while len(r_echo) < r_echo_len_exected and (time.time() - st) < 10:
 | ||
|  |       r_echo.extend(p.can_recv())
 | ||
| 
											8 years ago
										 | 
 | ||
|  |   sent_echo = filter(lambda x: x[3] == 0x80 | bus and x[0] == msg_id, r)
 | ||
| 
											7 years ago
										 |   sent_echo.extend(filter(lambda x: x[3] == 0x80 | bus and x[0] == msg_id, r_echo))
 | ||
|  |   resp = filter(lambda x: x[3] == bus and x[0] == msg_id, r)
 | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |   leftovers = filter(lambda x: (x[3] != 0x80 | bus and x[3] != bus) or x[0] != msg_id, r)
 | ||
|  |   assert_equal(len(leftovers), 0)
 | ||
|  | 
 | ||
|  |   assert_equal(len(resp), msg_count)
 | ||
| 
											8 years ago
										 |   assert_equal(len(sent_echo), msg_count)
 | ||
|  | 
 | ||
| 
											7 years ago
										 |   et = (et-st)*1000.0
 | ||
| 
											8 years ago
										 |   comp_kbps = (1+11+1+1+1+4+8*8+15+1+1+1+7)*msg_count / et
 | ||
|  | 
 | ||
|  |   return comp_kbps
 | ||
|  | 
 | ||
| 
											7 years ago
										 | 
 | ||
|  | def panda_color_to_serial(fn):
 | ||
|  |   @wraps(fn)
 | ||
|  |   def wrapper(panda_color=None, **kwargs):
 | ||
|  |     pandas_is_grey = []
 | ||
|  |     if panda_color is not None:
 | ||
|  |       if not isinstance(panda_color, list):
 | ||
|  |         panda_color = [panda_color]
 | ||
|  |       panda_color = [s.lower() for s in panda_color]
 | ||
|  |     for p in panda_color:
 | ||
|  |       if p is None:
 | ||
|  |         pandas_is_grey.append(None)
 | ||
|  |       elif p in ["grey", "gray"]:
 | ||
|  |         pandas_is_grey.append(True)
 | ||
|  |       elif p in ["white"]:
 | ||
|  |         pandas_is_grey.append(False)
 | ||
|  |       else:
 | ||
|  |         raise ValueError("Invalid Panda Color {}".format(p))
 | ||
|  |     return fn(*[get_panda_serial(is_grey) for is_grey in pandas_is_grey], **kwargs)
 | ||
|  |   return wrapper
 |