Skip to content

Commit a8de31c

Browse files
committed
Added timeouts (delays) on serial port and mounting point accesses in host_test and copy plugins to prevent serial/disk io errors when device remounts USB iface of interface chip
1 parent 0cf5f89 commit a8de31c

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

workspace_tools/host_tests/host_test.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,43 @@ def init_serial(self, serial_baud=None, serial_timeout=None):
126126
serial_baud = serial_baud if serial_baud is not None else self.serial_baud
127127
serial_timeout = serial_timeout if serial_timeout is not None else self.serial_timeout
128128

129+
# Clear serial port
129130
if self.serial:
130131
self.serial.close()
131132
self.serial = None
132133

133-
result = True
134-
try:
135-
self.serial = Serial(self.port, baudrate=serial_baud, timeout=serial_timeout)
136-
except Exception as e:
137-
print "MBED: %s"% str(e)
138-
result = False
134+
# We will pool for serial to be re-mounted if it was unmounted after device reset
135+
result = self.pool_for_serial_init(serial_baud, serial_timeout) # Blocking
136+
139137
# Port can be opened
140138
if result:
141139
self.flush()
142140
return result
143141

142+
def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, init_delay=0.5, loop_delay=0.25):
143+
""" Functions pools for serial port readiness
144+
"""
145+
result = True
146+
last_error = None
147+
# This loop is used to check for serial port availability due to
148+
# some delays and remounting when devices are being flashed with new software.
149+
for i in range(pooling_loops):
150+
sleep(loop_delay if i else init_delay)
151+
try:
152+
self.serial = Serial(self.port, baudrate=serial_baud, timeout=serial_timeout)
153+
except Exception as e:
154+
result = False
155+
last_error = "MBED: %s"% str(e)
156+
stdout.write('.')
157+
stdout.flush()
158+
else:
159+
print "...port ready!"
160+
result = True
161+
break
162+
if not result and last_error:
163+
print last_error
164+
return result
165+
144166
def set_serial_timeout(self, timeout):
145167
""" Wraps self.mbed.serial object timeout property
146168
"""

workspace_tools/host_tests/host_tests_plugins/host_test_plugins.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
limitations under the License.
1616
"""
1717

18+
from os import access, F_OK
19+
from sys import stdout
20+
from time import sleep
1821
from subprocess import call
1922

2023

@@ -58,6 +61,34 @@ def print_plugin_error(self, text):
5861
print "Plugin error: %s::%s: %s"% (self.name, self.type, text)
5962
return False
6063

64+
def print_plugin_info(self, text, NL=True):
65+
""" Function prints notification in console and exits always with True
66+
"""
67+
if NL:
68+
print "Plugin info: %s::%s: %s"% (self.name, self.type, text)
69+
else:
70+
print "Plugin info: %s::%s: %s"% (self.name, self.type, text),
71+
return True
72+
73+
def print_plugin_char(self, char):
74+
""" Function prints char on stdout
75+
"""
76+
stdout.write(char)
77+
stdout.flush()
78+
return True
79+
80+
def check_mount_point_ready(self, destination_disk, init_delay=0.2, loop_delay=0.25):
81+
""" Checks if destination_disk is ready and can be accessed by e.g. copy commands
82+
@init_delay - Initial delay time before first access check
83+
@loop_delay - pooling delay for access check
84+
"""
85+
if not access(destination_disk, F_OK):
86+
self.print_plugin_info("Waiting for mount point '%s' to be ready..."% destination_disk, NL=False)
87+
sleep(init_delay)
88+
while not access(destination_disk, F_OK):
89+
sleep(loop_delay)
90+
self.print_plugin_char('.')
91+
6192
def check_parameters(self, capabilitity, *args, **kwargs):
6293
""" This function should be ran each time we call execute()
6394
to check if none of the required parameters is missing.

workspace_tools/host_tests/host_tests_plugins/module_copy_mbed.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def execute(self, capabilitity, *args, **kwargs):
5959
if capabilitity == 'default':
6060
image_path = kwargs['image_path']
6161
destination_disk = kwargs['destination_disk']
62+
# Wait for mount point to be ready
63+
self.check_mount_point_ready(destination_disk) # Blocking
6264
result = self.generic_mbed_copy(image_path, destination_disk)
6365
return result
6466

workspace_tools/host_tests/host_tests_plugins/module_copy_shell.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def execute(self, capabilitity, *args, **kwargs):
4242
if self.check_parameters(capabilitity, *args, **kwargs) is True:
4343
image_path = kwargs['image_path']
4444
destination_disk = kwargs['destination_disk']
45+
# Wait for mount point to be ready
46+
self.check_mount_point_ready(destination_disk) # Blocking
4547
# Prepare correct command line parameter values
4648
image_base_name = basename(image_path)
4749
destination_path = join(destination_disk, image_base_name)

0 commit comments

Comments
 (0)