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 <ddegrasse@tenstorrent.com>
This commit is contained in:
Daniel DeGrasse 2025-01-10 16:24:11 -05:00 committed by Henrik Brix Andersen
parent 7d896522f0
commit 986a170a39
2 changed files with 12 additions and 9 deletions

View file

@ -920,22 +920,27 @@ class ZephyrBinaryRunner(abc.ABC):
# RuntimeError avoids a stack trace saved in run_common. # RuntimeError avoids a stack trace saved in run_common.
raise RuntimeError(err) 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. Run a telnet client for user interaction.
''' '''
# If a `nc` command is available, run it, as it will provide the best support for # If the caller passed in an active socket, use that
# CONFIG_SHELL_VT100_COMMANDS etc. if active_sock is not None:
if shutil.which('nc') 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)] client_cmd = ['nc', host, str(port)]
# Note: netcat (nc) does not handle sigint, so cannot use run_client() # Note: netcat (nc) does not handle sigint, so cannot use run_client()
self.check_call(client_cmd) self.check_call(client_cmd)
return 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, # Otherwise, use a pure python implementation. This will work well for logging,
# but input is line based only. # but input is line based only.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sel = selectors.DefaultSelector() sel = selectors.DefaultSelector()
sel.register(sys.stdin, selectors.EVENT_READ) sel.register(sys.stdin, selectors.EVENT_READ)
sel.register(sock, selectors.EVENT_READ) sel.register(sock, selectors.EVENT_READ)

View file

@ -330,9 +330,7 @@ class JLinkBinaryRunner(ZephyrBinaryRunner):
break break
except ConnectionRefusedError: except ConnectionRefusedError:
time.sleep(0.1) time.sleep(0.1)
sock.shutdown(socket.SHUT_RDWR) self.run_telnet_client('localhost', self.rtt_port, sock)
time.sleep(0.1)
self.run_telnet_client('localhost', self.rtt_port)
except Exception as e: except Exception as e:
self.logger.error(e) self.logger.error(e)
finally: finally: