From 9308db14c9e24067a81562236ce9ea6a5518f339 Mon Sep 17 00:00:00 2001 From: whd <7058128+superwhd@users.noreply.github.com> Date: Fri, 26 Aug 2022 14:19:28 +0800 Subject: [PATCH] [otci] support ADB connection for OTCI (#8088) --- tools/otci/otci/__init__.py | 5 +++- tools/otci/otci/command_handlers.py | 43 +++++++++++++++++++++++++++++ tools/otci/otci/otci.py | 7 ++++- tools/otci/setup.py | 2 +- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/tools/otci/otci/__init__.py b/tools/otci/otci/__init__.py index 3e97e8959..1952b0abe 100644 --- a/tools/otci/otci/__init__.py +++ b/tools/otci/otci/__init__.py @@ -35,7 +35,9 @@ from .otci import \ connect_cli_serial, \ connect_ncp_sim, \ connect_cmd_handler, \ - connect_otbr_ssh + connect_otbr_ssh, \ + connect_otbr_adb + from .types import Rloc16, ChildId, NetifIdentifier _connectors = [ @@ -43,6 +45,7 @@ _connectors = [ 'connect_cli_serial', 'connect_ncp_sim', 'connect_otbr_ssh', + 'connect_otbr_adb', 'connect_cmd_handler', ] diff --git a/tools/otci/otci/command_handlers.py b/tools/otci/otci/command_handlers.py index 0055b1a46..3955cd5a8 100644 --- a/tools/otci/otci/command_handlers.py +++ b/tools/otci/otci/command_handlers.py @@ -282,3 +282,46 @@ class OtbrSshCommandRunner(OTCommandHandler): def set_line_read_callback(self, callback: Optional[Callable[[str], Any]]): self.__line_read_callback = callback + + +class OtbrAdbCommandRunner(OTCommandHandler): + + def __init__(self, host, port): + from adb_shell.adb_device import AdbDeviceTcp + + self.__host = host + self.__port = port + self.__adb = AdbDeviceTcp(host, port, default_transport_timeout_s=9.0) + + self.__line_read_callback = None + self.__adb.connect(rsa_keys=None, auth_timeout_s=0.1) + + def __repr__(self): + return f'{self.__host}:{self.__port}' + + def execute_command(self, cmd: str, timeout: float) -> List[str]: + sh_cmd = f'ot-ctl {cmd}' + + output = self.shell(sh_cmd, timeout=timeout) + + if self.__line_read_callback is not None: + for line in output: + self.__line_read_callback(line) + + if cmd in ('reset', 'factoryreset'): + self.wait(3) + + return output + + def shell(self, cmd: str, timeout: float) -> List[str]: + return self.__adb.shell(cmd, timeout_s=timeout).splitlines() + + def close(self): + self.__adb.close() + + def wait(self, duration: float) -> List[str]: + time.sleep(duration) + return [] + + def set_line_read_callback(self, callback: Optional[Callable[[str], Any]]): + self.__line_read_callback = callback diff --git a/tools/otci/otci/otci.py b/tools/otci/otci/otci.py index a3bcefe53..3c70dfcb6 100644 --- a/tools/otci/otci/otci.py +++ b/tools/otci/otci/otci.py @@ -33,7 +33,7 @@ from collections import Counter from typing import Callable, List, Collection, Union, Tuple, Optional, Dict, Pattern, Any from . import connectors -from .command_handlers import OTCommandHandler, OtCliCommandRunner, OtbrSshCommandRunner +from .command_handlers import OTCommandHandler, OtCliCommandRunner, OtbrSshCommandRunner, OtbrAdbCommandRunner from .connectors import Simulator from .errors import UnexpectedCommandOutput, ExpectLineTimeoutError, CommandError, InvalidArgumentsError from .types import ChildId, Rloc16, Ip6Addr, ThreadState, PartitionId, DeviceMode, RouterId, SecurityPolicy, Ip6Prefix, \ @@ -2487,5 +2487,10 @@ def connect_otbr_ssh(host: str, port: int = 22, username='pi', password='raspber return OTCI(cmd_handler) +def connect_otbr_adb(host: str, port: int = 5555): + cmd_handler = OtbrAdbCommandRunner(host, port) + return OTCI(cmd_handler) + + def connect_cmd_handler(cmd_handler: OTCommandHandler) -> OTCI: return OTCI(cmd_handler) diff --git a/tools/otci/setup.py b/tools/otci/setup.py index f3d68685a..6284ace65 100644 --- a/tools/otci/setup.py +++ b/tools/otci/setup.py @@ -46,5 +46,5 @@ setuptools.setup( "Operating System :: OS Independent", ], python_requires='>=3.6', - install_requires=['pySerial', 'paramiko', 'pyspinel'], + install_requires=['pySerial', 'paramiko', 'pyspinel', 'adb-shell'], )