Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(esp-idf-monitor): Add --retry-open flag (IDFGH-13270) #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions esp_idf_monitor/base/argument_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,10 @@ def get_parser(): # type: () -> argparse.ArgumentParser
default=False,
action='store_true')

parser.add_argument(
'--retry-open',
help='Indefinitely retry opening the port device for the first time',
default=False,
action='store_true')

return parser
2 changes: 1 addition & 1 deletion esp_idf_monitor/base/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
LAST_LINE_THREAD_INTERVAL = 0.1

MINIMAL_EN_LOW_DELAY = 0.005
RECONNECT_DELAY = 0.5 # timeout between reconnect tries
RECONNECT_DELAY = 0.1 # timeout between reconnect tries
CHECK_ALIVE_FLAG_TIMEOUT = 0.25 # timeout for checking alive flags (currently used by serial reader)

# closing wait timeout for serial port
Expand Down
58 changes: 38 additions & 20 deletions esp_idf_monitor/base/serial_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import queue # noqa: F401
import subprocess # noqa: F401
import sys
import termios
import time

import serial
Expand All @@ -25,14 +26,15 @@ class SerialReader(Reader):
event queue, until stopped.
"""

def __init__(self, serial_instance, event_queue, reset, target):
# type: (serial.Serial, queue.Queue, bool, str) -> None
def __init__(self, serial_instance, event_queue, reset, retry_open, target):
# type: (serial.Serial, queue.Queue, bool, bool, str) -> None
super(SerialReader, self).__init__()
self.baud = serial_instance.baudrate
self.serial = serial_instance
self.event_queue = event_queue
self.gdb_exit = False
self.reset = reset
self.retry_open = retry_open
self.reset_strategy = Reset(serial_instance, target)
if not hasattr(self.serial, 'cancel_read'):
# enable timeout for checking alive flag,
Expand All @@ -46,40 +48,56 @@ def run(self):
try:
# We can come to this thread at startup or from external application line GDB.
# If we come from GDB we would like to continue to run without reset.
self.open_serial(reset=not self.gdb_exit and self.reset)
self.reset = not self.gdb_exit and self.reset
self.open_serial(reset=self.reset)
# Successfully connected, so any further reconnections should occur without a reset.
self.reset = False
except serial.SerialException as e:
print(e)
# if connection to port fails suggest other available ports
port_list = '\n'.join(
[
p.device
for p in list_ports.comports()
if not p.device.endswith(FILTERED_PORTS)
]
)
yellow_print(f'Connection to {self.serial.portstr} failed. Available ports:\n{port_list}')
return
if not self.retry_open:
# if connection to port fails suggest other available ports
port_list = '\n'.join(
[
p.device
for p in list_ports.comports()
if not p.device.endswith(FILTERED_PORTS)
]
)
yellow_print(f'Connection to {self.serial.portstr} failed. Available ports:\n{port_list}')
return
self.gdb_exit = False
try:
while self.alive:
try:
data = self.serial.read(self.serial.in_waiting or 1)
except (serial.SerialException, IOError) as e:
if self.serial.is_open:
# in_waiting assumes the port is already open
data = self.serial.read(self.serial.in_waiting or 1)
else:
raise serial.PortNotOpenError
except (serial.SerialException, IOError, OSError, termios.error) as e:
data = b''
# self.serial.open() was successful before, therefore, this is an issue related to
# the disappearance of the device
red_print(e.strerror)
red_print(str(e))
yellow_print('Waiting for the device to reconnect', newline='')
self.close_serial()
last_dot_time = 0.0
waited_time = 0.0
while self.alive: # so that exiting monitor works while waiting
try:
time.sleep(RECONNECT_DELAY)
waited_time += RECONNECT_DELAY
# reset on reconnect can be unexpected for wakeup from deepsleep using JTAG
self.open_serial(reset=False)
self.open_serial(reset=self.reset)
self.reset = False
break # device connected
except serial.SerialException:
yellow_print('.', newline='')
sys.stderr.flush()
except (serial.SerialException, IOError, OSError, termios.error):
if waited_time - last_dot_time > 0.5:
# print a dot every half second
last_dot_time = waited_time
yellow_print('.', newline='')
sys.stderr.flush()

yellow_print('') # go to new line
if data:
self.event_queue.put((TAG_SERIAL, data), False)
Expand Down
4 changes: 3 additions & 1 deletion esp_idf_monitor/idf_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(
make='make', # type: str
encrypted=False, # type: bool
reset=DEFAULT_TARGET_RESET, # type: bool
retry_open=False, # type: bool
toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX, # type: str
eol='CRLF', # type: str
decode_coredumps=COREDUMP_DECODE_INFO, # type: str
Expand Down Expand Up @@ -128,7 +129,7 @@ def __init__(
# testing hook: when running tests, input from console is ignored
socket_test_mode = os.environ.get('ESP_IDF_MONITOR_TEST') == '1'
self.serial = serial_instance
self.serial_reader = SerialReader(self.serial, self.event_queue, reset, target) # type: Reader
self.serial_reader = SerialReader(self.serial, self.event_queue, reset, retry_open, target) # type: Reader

self.gdb_helper = GDBHelper(toolchain_prefix, websocket_client, self.elf_file, self.serial.port,
self.serial.baudrate) if self.elf_exists else None
Expand Down Expand Up @@ -414,6 +415,7 @@ def main() -> None:
args.make,
args.encrypted,
not args.no_reset,
args.retry_open,
args.toolchain_prefix,
args.eol,
args.decode_coredumps,
Expand Down
Loading