Skip to content

Commit

Permalink
Revert "common: Add support for hid_gpio active hub"
Browse files Browse the repository at this point in the history
This reverts commit f2a795f.
  • Loading branch information
mringwal committed Oct 21, 2024
1 parent 5d5ed6e commit 2f3f901
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 267 deletions.
6 changes: 0 additions & 6 deletions autopts/bot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def __init__(self, args, **kwargs):
self.stress_test = args.get('stress_test', False)
self.ykush = args.get('ykush', None)
self.ykush_replug_delay = args.get('ykush_replug_delay', 3)
self.active_hub_server = args.get('active_hub_server', None)
self.recovery = args.get('recovery', False)
self.superguard = float(args.get('superguard', 0))
self.cron_optim = args.get('cron_optim', False)
Expand All @@ -122,11 +121,6 @@ def __init__(self, args, **kwargs):
self.max_server_restart_time = args.get('max_server_restart_time', MAX_SERVER_RESTART_TIME)
self.use_backup = args.get('use_backup', False)

if self.ykush or self.active_hub_server:
self.usb_replug_available = True
else:
self.usb_replug_available = False

if self.server_args is not None:
from autoptsserver import SvrArgumentParser
_server_args = SvrArgumentParser(
Expand Down
56 changes: 22 additions & 34 deletions autopts/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from autopts.pybtp import btp, defs
from autopts.pybtp.types import BTPError, SynchError, MissingWIDError
from autopts.utils import InterruptableThread, ResultWithFlag, CounterWithFlag, set_global_end, \
raise_on_global_end, RunEnd, get_global_end, have_admin_rights, ykush_replug_usb, active_hub_server_replug_usb
raise_on_global_end, RunEnd, get_global_end, have_admin_rights, ykush_replug_usb
from cliparser import CliParser

log = logging.debug
Expand Down Expand Up @@ -1501,8 +1501,23 @@ def run_recovery(args, ptses):
iut = autoprojects.iutctl.get_iut()
iut.stop()

if args.usb_replug_available:
replug_usb(args)
if args.ykush:
if sys.platform == 'win32':
device_id = tty_to_com(args.tty_file)
elif args.tty_alias:
device_id = args.tty_alias
else:
device_id = args.tty_file

ykush_replug_usb(args.ykush, device_id=device_id, delay=args.ykush_replug_delay)

if args.tty_alias:
while not os.path.islink(args.tty_alias) and not os.path.exists(os.path.realpath(args.tty_alias)):
raise_on_global_end()
log(f'Waiting for TTY {args.tty_alias} to appear...\n')
time.sleep(1)

args.tty_file = os.path.realpath(args.tty_alias)

for pts in ptses:
req_sent = False
Expand All @@ -1520,23 +1535,19 @@ def run_recovery(args, ptses):
req_sent = True
err = pts.callback.get_result('recover_pts', timeout=args.max_server_restart_time)
if err == True:
log('PTS recovered')
break

if last_restart_time < pts.get_last_recovery_time():
log('PTS recovered')
break

except BaseException as e:
log(e)

log('Server is still resetting. Wait a little more.')
time.sleep(1)
except Exception:
log('Server is still resetting. Wait a little more.')
time.sleep(1)

stack_inst = stack.get_stack()
stack_inst.cleanup()

if args.usb_replug_available:
if args.ykush:
# mynewt project has not been refactored yet to reduce the number of
# IUT board resets.
if stack_inst.core:
Expand All @@ -1545,29 +1556,6 @@ def run_recovery(args, ptses):
log('Recovery finished')


def replug_usb(args):
if args.ykush:
if sys.platform == 'win32':
device_id = tty_to_com(args.tty_file)
elif args.tty_alias:
device_id = args.tty_alias
else:
device_id = args.tty_file

ykush_replug_usb(args.ykush, device_id=device_id, delay=args.ykush_replug_delay)

if args.tty_alias:
while not os.path.islink(args.tty_alias) and not os.path.exists(os.path.realpath(args.tty_alias)):
raise_on_global_end()
log(f'Waiting for TTY {args.tty_alias} to appear...\n')
time.sleep(1)

args.tty_file = os.path.realpath(args.tty_alias)

elif args.active_hub_server:
active_hub_server_replug_usb(args.active_hub_server)


def setup_project_name(project):
global autoprojects
autoprojects = project # importlib.import_module('ptsprojects.' + project)
Expand Down
86 changes: 15 additions & 71 deletions autopts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import sys
import threading
import traceback
import xmlrpc.client
import hid
import psutil
from time import sleep

import psutil

PTS_WORKSPACE_FILE_EXT = ".pqw6"

Expand Down Expand Up @@ -210,23 +208,18 @@ def interrupt(self):
pass


def ykush_set_usb_power(ykush_port, on=True, ykush_srn=None):
def usb_power(ykush_port, on=True, ykush_srn=None):
if not pykush_installed:
print('pykush not installed')
return

yk = None
try:
ykush_port = int(ykush_port)
yk = pykush.YKUSH(serial=ykush_srn)
state = pykush.YKUSH_PORT_STATE_UP if on else pykush.YKUSH_PORT_STATE_DOWN
yk.set_port_state(ykush_port, state)
if yk.get_port_state(ykush_port) != state:
ykush_name = ykush_srn if ykush_srn else ''
raise Exception(f'YKUSH {ykush_name} failed to change state {state} of port {ykush_port}')
finally:
if yk:
del yk
ykush_port = int(ykush_port)
yk = pykush.YKUSH(serial=ykush_srn)
state = pykush.YKUSH_PORT_STATE_UP if on else pykush.YKUSH_PORT_STATE_DOWN
yk.set_port_state(ykush_port, state)
if yk.get_port_state(ykush_port) != state:
ykush_name = ykush_srn if ykush_srn else ''
raise Exception(f'YKUSH {ykush_name} failed to change state {state} of port {ykush_port}')


def get_own_workspaces():
Expand Down Expand Up @@ -350,9 +343,9 @@ def ykush_replug_usb(ykush_config, device_id=None, delay=0, end_flag=None):
return

if device_id is None:
ykush_set_usb_power(ykush_port, False, ykush_srn)
usb_power(ykush_port, False, ykush_srn)
sleep(delay)
ykush_set_usb_power(ykush_port, True, ykush_srn)
usb_power(ykush_port, True, ykush_srn)
sleep(delay)
return

Expand All @@ -364,15 +357,15 @@ def ykush_replug_usb(ykush_config, device_id=None, delay=0, end_flag=None):

if i == 0:
logging.debug(f'Power down device ({device_id}) under ykush_port:{ykush_port}')
ykush_set_usb_power(ykush_port, False, ykush_srn)
usb_power(ykush_port, False, ykush_srn)
i = 20
else:
i -= 1
sleep(0.1)

sleep(delay)
logging.debug(f'Power up device ({device_id}) under ykush_port:{ykush_port}')
ykush_set_usb_power(ykush_port, True, ykush_srn)
usb_power(ykush_port, True, ykush_srn)

i = 0
while not device_exists(device_id):
Expand All @@ -383,65 +376,16 @@ def ykush_replug_usb(ykush_config, device_id=None, delay=0, end_flag=None):
if i == 20:
# Sometimes JLink falls into a bad state and cannot
# be enumerated correctly at first time
ykush_set_usb_power(ykush_port, False, ykush_srn)
usb_power(ykush_port, False, ykush_srn)
sleep(delay)
ykush_set_usb_power(ykush_port, True, ykush_srn)
usb_power(ykush_port, True, ykush_srn)
i = 0
else:
i += 1

sleep(0.1)


def hid_gpio_hub_set_usb_power(vid, pid, port, on):
path = None
cmd = b"\x05xxxxxxxx"
index = int(port)

if 1 <= index <= len(cmd) - 1:
cmd_list = list(cmd)
cmd_list[index] = ord('0' if on else '1')
cmd = bytes(cmd_list)

for device in hid.enumerate(vid, pid):
print(device)
path = device['path']

device = hid.device()
device.open_path(path)
device.send_feature_report(cmd)
device.close()

# Read the flashed versions of hid_gpio and apache-mynewt-core
# print(device.get_indexed_string(32))
# print(device.get_indexed_string(33))
# Read the states of hub ports
# print(device.get_feature_report(5, 9))


def active_hub_server_replug_usb(config):
with xmlrpc.client.ServerProxy(uri=f"http://{config['ip']}:{config['tcp_port']}/",
allow_none=True, transport=None,
encoding=None, verbose=False,
use_datetime=False, use_builtin_types=False,
headers=(), context=None) as proxy:
logging.debug(f'Power down USB port: {config["usb_port"]}')
proxy.set_usb_power(config['usb_port'], False)
sleep(config['replug_delay'])
logging.debug(f'Power up USB port: {config["usb_port"]}')
proxy.set_usb_power(config['usb_port'], True)


def active_hub_server_set_usb_power(config, on):
with xmlrpc.client.ServerProxy(uri=f"http://{config['ip']}:{config['tcp_port']}/",
allow_none=True, transport=None,
encoding=None, verbose=False,
use_datetime=False, use_builtin_types=False,
headers=(), context=None) as proxy:

proxy.set_usb_power(config['usb_port'], on)


def print_thread_stack_trace():
logging.debug("Printing stack trace for each thread:")
for thread_id, thread_obj in threading._active.items():
Expand Down
63 changes: 17 additions & 46 deletions autoptsserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@

from autopts import ptscontrol
from autopts.config import SERVER_PORT
from autopts.utils import CounterWithFlag, get_global_end, exit_if_admin, ykush_replug_usb, ykush_set_usb_power, \
print_thread_stack_trace, active_hub_server_replug_usb, active_hub_server_set_usb_power
from autopts.utils import CounterWithFlag, get_global_end, exit_if_admin, ykush_replug_usb, usb_power, \
print_thread_stack_trace
from autopts.winutils import kill_all_processes

logging = root_logging.getLogger('server')
Expand Down Expand Up @@ -154,21 +154,19 @@ def unregister_client_callback(self):

def _replug_dongle(self):
log(f"{self._replug_dongle.__name__}")
ykush_port = self.args.ykush
if not ykush_port:
return

if self.args.ykush:
ykush_port = self.args.ykush
device = self._device
log(f'Replugging device ({device}) under ykush:{ykush_port} ...')
if device:
ykush_replug_usb(self.args.ykush, device_id=device, delay=0, end_flag=self._end)
else:
# Cases where ykush was down or the dongle was
# not enumerated for any other reason.
ykush_replug_usb(self.args.ykush, device_id=None, delay=3, end_flag=self._end)
log(f'Done replugging device ({device}) under ykush:{ykush_port}')

elif self.args.active_hub_server:
active_hub_server_replug_usb(self.args.active_hub_server)
device = self._device
log(f'Replugging device ({device}) under ykush:{ykush_port} ...')
if device:
ykush_replug_usb(self.args.ykush, device_id=device, delay=0, end_flag=self._end)
else:
# Cases where ykush was down or the dongle was
# not enumerated for any other reason.
ykush_replug_usb(self.args.ykush, device_id=None, delay=3, end_flag=self._end)
log(f'Done replugging device ({device}) under ykush:{ykush_port}')

def _dispatch(self, method_name, param_tuple):
"""Dispatcher that is used by xmlrpc server"""
Expand Down Expand Up @@ -312,10 +310,6 @@ def __init__(self, description):
help="Specify ykush hub downstream port number, so "
"during recovery steps PTS dongle could be replugged.")

self.add_argument("--active-hub-server", nargs="+", default=[],
help="Specify active hub server e.g. IP:TCP_PORT:USB_PORT, so "
"during recovery steps PTS dongle could be replugged.")

self.add_argument("--dongle", nargs="+", default=None,
help='Select the dongle port.'
'COMx in case of LE only dongle. '
Expand Down Expand Up @@ -353,25 +347,8 @@ def check_args(arg):
ykush_confs.append(config)

arg.ykush = ykush_confs
arg.active_hub = True

elif arg.active_hub_server:
active_hub_server_configs = []
for active_hub_server_conf in arg.active_hub_server:
config = {}
ip, tcp_port, usb_port = active_hub_server_conf.split(':')

config['ip'] = ip
config['tcp_port'] = tcp_port
config['usb_port'] = usb_port
config['replug_delay'] = 5
active_hub_server_configs.append(config)

arg.active_hub_server = active_hub_server_configs
arg.active_hub = True

def parse_args(self, args=None, namespace=None):
namespace = argparse.Namespace(active_hub=None)
arg = super().parse_args(args, namespace)
self.check_args(arg)
return arg
Expand Down Expand Up @@ -604,14 +581,9 @@ def shutdown_pts_bpv(self):

init_logging(_args)

if _args.active_hub:
if _args.ykush:
for ykush_config in _args.ykush:
ykush_set_usb_power(ykush_config['ports'], False, ykush_config['ykush_srn'])

elif _args.active_hub_server:
for active_hub_server_config in _args.active_hub_server:
active_hub_server_set_usb_power(active_hub_server_config, False)
if _args.ykush:
for ykush_config in _args.ykush:
usb_power(ykush_config['ports'], False, ykush_config['ykush_srn'])

autoptsservers = []
server_count = len(_args.srv_port)
Expand All @@ -621,7 +593,6 @@ def shutdown_pts_bpv(self):
args_copy = copy.deepcopy(_args)
args_copy.srv_port = _args.srv_port[i]
args_copy.ykush = _args.ykush[i] if _args.ykush else None
args_copy.active_hub_server = _args.active_hub_server[i] if _args.active_hub_server else None
args_copy.dongle = _args.dongle[i] if _args.dongle else None
srv = Server(finish_count, args_copy)
autoptsservers.append(srv)
Expand Down
Loading

0 comments on commit 2f3f901

Please sign in to comment.