From 986a170a39e83502eb76b8deb56e5cb113a2f532 Mon Sep 17 00:00:00 2001 From: Daniel DeGrasse Date: Fri, 10 Jan 2025 16:24:11 -0500 Subject: [PATCH] scripts: west_commands: runners: fix support for JLink RTT JLink refuses new RTT telnet connections for a few moments after a socket closes. This causes an issue when using `nc` as the telnet viewer, since JLink would deny the connection. To resolve this, keep the "ping" socket we use to determine if the RTT viewer is active connected, and use that socket for RTT communication. Signed-off-by: Daniel DeGrasse --- scripts/west_commands/runners/core.py | 17 +++++++++++------ scripts/west_commands/runners/jlink.py | 4 +--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/scripts/west_commands/runners/core.py b/scripts/west_commands/runners/core.py index cf01d2abf45..20e4b72a8c3 100644 --- a/scripts/west_commands/runners/core.py +++ b/scripts/west_commands/runners/core.py @@ -920,22 +920,27 @@ class ZephyrBinaryRunner(abc.ABC): # RuntimeError avoids a stack trace saved in run_common. raise RuntimeError(err) - def run_telnet_client(self, host: str, port: int) -> None: + def run_telnet_client(self, host: str, port: int, active_sock=None) -> None: ''' Run a telnet client for user interaction. ''' - # If a `nc` command is available, run it, as it will provide the best support for - # CONFIG_SHELL_VT100_COMMANDS etc. - if shutil.which('nc') is not None: + # If the caller passed in an active socket, use that + if active_sock is not None: + sock = active_sock + elif shutil.which('nc') is not None: + # If a `nc` command is available, run it, as it will provide the + # best support for CONFIG_SHELL_VT100_COMMANDS etc. client_cmd = ['nc', host, str(port)] # Note: netcat (nc) does not handle sigint, so cannot use run_client() self.check_call(client_cmd) return + else: + # Start a new socket connection + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((host, port)) # Otherwise, use a pure python implementation. This will work well for logging, # but input is line based only. - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) sel = selectors.DefaultSelector() sel.register(sys.stdin, selectors.EVENT_READ) sel.register(sock, selectors.EVENT_READ) diff --git a/scripts/west_commands/runners/jlink.py b/scripts/west_commands/runners/jlink.py index f2c3bc21d90..00438ba6e5d 100644 --- a/scripts/west_commands/runners/jlink.py +++ b/scripts/west_commands/runners/jlink.py @@ -330,9 +330,7 @@ class JLinkBinaryRunner(ZephyrBinaryRunner): break except ConnectionRefusedError: time.sleep(0.1) - sock.shutdown(socket.SHUT_RDWR) - time.sleep(0.1) - self.run_telnet_client('localhost', self.rtt_port) + self.run_telnet_client('localhost', self.rtt_port, sock) except Exception as e: self.logger.error(e) finally: