Skip to content

Commit

Permalink
close nrfjprog after searching for vcom0
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Jul 3, 2024
1 parent 9748699 commit 7daece9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tockloader/bootloader_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# access to Windows machines, and support in testing fixes/updates is helpful.
if platform.system() != "Windows":
import fcntl

_IS_WINDOWS = False
else:
_IS_WINDOWS = True
Expand Down Expand Up @@ -236,6 +237,10 @@ def _determine_port(self, any=False):
logging.info(
'Discovered "{}" as nRF52840dk VCOM0.'.format(vcom0_path)
)

# Must close this to end the underlying pynrfjprog process.
# Otherwise on my machine it sits at 100% CPU.
api.close()
except:
# Any error with nrfjprog we just don't use this
# optimization.
Expand Down Expand Up @@ -364,18 +369,19 @@ def open_link_to_board(self, listen=False):
# running, and if it's a listen, pause that listen (unless we are also
# doing a listen), otherwise bail out.


# Windows has only partial unix socket support, so Python rejects them.
# Work around this by listening on a reasonably-unlikely-to-collide
# localhost port derived from the serial port name.
if _IS_WINDOWS:
self.comm_port = self._get_serial_port_hashed_to_ip_port()
self.client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.client_sock.connect(('localhost', self.comm_port))
self.client_sock.connect(("localhost", self.comm_port))
logging.debug("Connected to existing `tockloader listen`")
except ConnectionRefusedError:
logging.debug(f"No other listen instances running (tried localhost::{self.comm_port})")
logging.debug(
f"No other listen instances running (tried localhost::{self.comm_port})"
)
self.client_sock = None
else:
self.comm_path = "/tmp/tockloader." + self._get_serial_port_hash()
Expand All @@ -393,7 +399,6 @@ def open_link_to_board(self, listen=False):
else:
self.client_sock = None


# Check if another tockloader instance exists based on whether we were
# able to create a socket to it.
if self.client_sock:
Expand Down Expand Up @@ -469,7 +474,7 @@ def restart_listener(path):

if _IS_WINDOWS:
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_sock.bind(('localhost', self.comm_port))
self.server_sock.bind(("localhost", self.comm_port))
logging.debug(f"listening on localhost::{self.comm_port}")
else:
# Create the socket we will listen on.
Expand Down

0 comments on commit 7daece9

Please sign in to comment.