Skip to content

Commit

Permalink
scripts: west_commands: runners: fix support for JLink RTT
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
danieldegrasse committed Jan 10, 2025
1 parent 8998b1a commit adeb0b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 11 additions & 6 deletions scripts/west_commands/runners/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,22 +920,27 @@ def ensure_output(self, output_type: str) -> None:
# 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)
Expand Down
4 changes: 1 addition & 3 deletions scripts/west_commands/runners/jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ def do_run(self, command, **kwargs):
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:
Expand Down

0 comments on commit adeb0b9

Please sign in to comment.