User | Revision | Line number | New contents of line |
switches |
0:0e018d759a2a
|
1
|
"""
|
switches |
0:0e018d759a2a
|
2
|
mbed SDK
|
switches |
0:0e018d759a2a
|
3
|
Copyright (c) 2011-2013 ARM Limited
|
switches |
0:0e018d759a2a
|
4
|
|
switches |
0:0e018d759a2a
|
5
|
Licensed under the Apache License, Version 2.0 (the "License");
|
switches |
0:0e018d759a2a
|
6
|
you may not use this file except in compliance with the License.
|
switches |
0:0e018d759a2a
|
7
|
You may obtain a copy of the License at
|
switches |
0:0e018d759a2a
|
8
|
|
switches |
0:0e018d759a2a
|
9
|
http://www.apache.org/licenses/LICENSE-2.0
|
switches |
0:0e018d759a2a
|
10
|
|
switches |
0:0e018d759a2a
|
11
|
Unless required by applicable law or agreed to in writing, software
|
switches |
0:0e018d759a2a
|
12
|
distributed under the License is distributed on an "AS IS" BASIS,
|
switches |
0:0e018d759a2a
|
13
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
switches |
0:0e018d759a2a
|
14
|
See the License for the specific language governing permissions and
|
switches |
0:0e018d759a2a
|
15
|
limitations under the License.
|
switches |
0:0e018d759a2a
|
16
|
"""
|
switches |
0:0e018d759a2a
|
17
|
|
switches |
0:0e018d759a2a
|
18
|
# Check if 'serial' module is installed
|
switches |
0:0e018d759a2a
|
19
|
try:
|
switches |
0:0e018d759a2a
|
20
|
from serial import Serial
|
switches |
0:0e018d759a2a
|
21
|
except ImportError, e:
|
switches |
0:0e018d759a2a
|
22
|
print "Error: Can't import 'serial' module: %s"% e
|
switches |
0:0e018d759a2a
|
23
|
exit(-1)
|
switches |
0:0e018d759a2a
|
24
|
|
switches |
0:0e018d759a2a
|
25
|
import os
|
switches |
0:0e018d759a2a
|
26
|
import re
|
switches |
0:0e018d759a2a
|
27
|
import types
|
switches |
0:0e018d759a2a
|
28
|
from sys import stdout
|
switches |
0:0e018d759a2a
|
29
|
from time import sleep, time
|
switches |
0:0e018d759a2a
|
30
|
from optparse import OptionParser
|
switches |
0:0e018d759a2a
|
31
|
|
switches |
0:0e018d759a2a
|
32
|
import host_tests_plugins
|
switches |
0:0e018d759a2a
|
33
|
|
switches |
0:0e018d759a2a
|
34
|
# This is a little tricky. We need to add upper directory to path so
|
switches |
0:0e018d759a2a
|
35
|
# we can find packages we want from the same level as other files do
|
switches |
0:0e018d759a2a
|
36
|
import sys
|
switches |
0:0e018d759a2a
|
37
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
|
switches |
0:0e018d759a2a
|
38
|
from tools.test_api import get_autodetected_MUTS_list
|
switches |
0:0e018d759a2a
|
39
|
from tools.test_api import get_module_avail
|
switches |
0:0e018d759a2a
|
40
|
|
switches |
0:0e018d759a2a
|
41
|
|
switches |
0:0e018d759a2a
|
42
|
class Mbed:
|
switches |
0:0e018d759a2a
|
43
|
""" Base class for a host driven test
|
switches |
0:0e018d759a2a
|
44
|
"""
|
switches |
0:0e018d759a2a
|
45
|
def __init__(self):
|
switches |
0:0e018d759a2a
|
46
|
parser = OptionParser()
|
switches |
0:0e018d759a2a
|
47
|
|
switches |
0:0e018d759a2a
|
48
|
parser.add_option("-m", "--micro",
|
switches |
0:0e018d759a2a
|
49
|
dest="micro",
|
switches |
0:0e018d759a2a
|
50
|
help="The target microcontroller",
|
switches |
0:0e018d759a2a
|
51
|
metavar="MICRO")
|
switches |
0:0e018d759a2a
|
52
|
|
switches |
0:0e018d759a2a
|
53
|
parser.add_option("-p", "--port",
|
switches |
0:0e018d759a2a
|
54
|
dest="port",
|
switches |
0:0e018d759a2a
|
55
|
help="The serial port of the target mbed",
|
switches |
0:0e018d759a2a
|
56
|
metavar="PORT")
|
switches |
0:0e018d759a2a
|
57
|
|
switches |
0:0e018d759a2a
|
58
|
parser.add_option("-d", "--disk",
|
switches |
0:0e018d759a2a
|
59
|
dest="disk",
|
switches |
0:0e018d759a2a
|
60
|
help="The target disk path",
|
switches |
0:0e018d759a2a
|
61
|
metavar="DISK_PATH")
|
switches |
0:0e018d759a2a
|
62
|
|
switches |
0:0e018d759a2a
|
63
|
parser.add_option("-f", "--image-path",
|
switches |
0:0e018d759a2a
|
64
|
dest="image_path",
|
switches |
0:0e018d759a2a
|
65
|
help="Path with target's image",
|
switches |
0:0e018d759a2a
|
66
|
metavar="IMAGE_PATH")
|
switches |
0:0e018d759a2a
|
67
|
|
switches |
0:0e018d759a2a
|
68
|
parser.add_option("-c", "--copy",
|
switches |
0:0e018d759a2a
|
69
|
dest="copy_method",
|
switches |
0:0e018d759a2a
|
70
|
help="Copy method selector",
|
switches |
0:0e018d759a2a
|
71
|
metavar="COPY_METHOD")
|
switches |
0:0e018d759a2a
|
72
|
|
switches |
0:0e018d759a2a
|
73
|
parser.add_option("-C", "--program_cycle_s",
|
switches |
0:0e018d759a2a
|
74
|
dest="program_cycle_s",
|
switches |
0:0e018d759a2a
|
75
|
help="Program cycle sleep. Define how many seconds you want wait after copying bianry onto target",
|
switches |
0:0e018d759a2a
|
76
|
type="float",
|
switches |
0:0e018d759a2a
|
77
|
metavar="COPY_METHOD")
|
switches |
0:0e018d759a2a
|
78
|
|
switches |
0:0e018d759a2a
|
79
|
parser.add_option("-t", "--timeout",
|
switches |
0:0e018d759a2a
|
80
|
dest="timeout",
|
switches |
0:0e018d759a2a
|
81
|
help="Timeout",
|
switches |
0:0e018d759a2a
|
82
|
metavar="TIMEOUT")
|
switches |
0:0e018d759a2a
|
83
|
|
switches |
0:0e018d759a2a
|
84
|
parser.add_option("-r", "--reset",
|
switches |
0:0e018d759a2a
|
85
|
dest="forced_reset_type",
|
switches |
0:0e018d759a2a
|
86
|
help="Forces different type of reset")
|
switches |
0:0e018d759a2a
|
87
|
|
switches |
0:0e018d759a2a
|
88
|
parser.add_option("-R", "--reset-timeout",
|
switches |
0:0e018d759a2a
|
89
|
dest="forced_reset_timeout",
|
switches |
0:0e018d759a2a
|
90
|
metavar="NUMBER",
|
switches |
0:0e018d759a2a
|
91
|
type="int",
|
switches |
0:0e018d759a2a
|
92
|
help="When forcing a reset using option -r you can set up after reset timeout in seconds")
|
switches |
0:0e018d759a2a
|
93
|
|
switches |
0:0e018d759a2a
|
94
|
parser.add_option('', '--auto',
|
switches |
0:0e018d759a2a
|
95
|
dest='auto_detect',
|
switches |
0:0e018d759a2a
|
96
|
metavar=False,
|
switches |
0:0e018d759a2a
|
97
|
action="store_true",
|
switches |
0:0e018d759a2a
|
98
|
help='Use mbed-ls module to detect all connected mbed devices')
|
switches |
0:0e018d759a2a
|
99
|
|
switches |
0:0e018d759a2a
|
100
|
(self.options, _) = parser.parse_args()
|
switches |
0:0e018d759a2a
|
101
|
|
switches |
0:0e018d759a2a
|
102
|
self.DEFAULT_RESET_TOUT = 0
|
switches |
0:0e018d759a2a
|
103
|
self.DEFAULT_TOUT = 10
|
switches |
0:0e018d759a2a
|
104
|
|
switches |
0:0e018d759a2a
|
105
|
if self.options.port is None:
|
switches |
0:0e018d759a2a
|
106
|
raise Exception("The serial port of the target mbed have to be provided as command line arguments")
|
switches |
0:0e018d759a2a
|
107
|
|
switches |
0:0e018d759a2a
|
108
|
# Options related to copy / reset mbed device
|
switches |
0:0e018d759a2a
|
109
|
self.port = self.options.port
|
switches |
0:0e018d759a2a
|
110
|
self.disk = self.options.disk
|
switches |
0:0e018d759a2a
|
111
|
self.image_path = self.options.image_path.strip('"')
|
switches |
0:0e018d759a2a
|
112
|
self.copy_method = self.options.copy_method
|
switches |
0:0e018d759a2a
|
113
|
self.program_cycle_s = float(self.options.program_cycle_s)
|
switches |
0:0e018d759a2a
|
114
|
|
switches |
0:0e018d759a2a
|
115
|
self.serial = None
|
switches |
0:0e018d759a2a
|
116
|
self.serial_baud = 9600
|
switches |
0:0e018d759a2a
|
117
|
self.serial_timeout = 1
|
switches |
0:0e018d759a2a
|
118
|
|
switches |
0:0e018d759a2a
|
119
|
self.timeout = self.DEFAULT_TOUT if self.options.timeout is None else self.options.timeout
|
switches |
0:0e018d759a2a
|
120
|
print 'MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk)
|
switches |
0:0e018d759a2a
|
121
|
|
switches |
0:0e018d759a2a
|
122
|
def init_serial_params(self, serial_baud=9600, serial_timeout=1):
|
switches |
0:0e018d759a2a
|
123
|
""" Initialize port parameters.
|
switches |
0:0e018d759a2a
|
124
|
This parameters will be used by self.init_serial() function to open serial port
|
switches |
0:0e018d759a2a
|
125
|
"""
|
switches |
0:0e018d759a2a
|
126
|
self.serial_baud = serial_baud
|
switches |
0:0e018d759a2a
|
127
|
self.serial_timeout = serial_timeout
|
switches |
0:0e018d759a2a
|
128
|
|
switches |
0:0e018d759a2a
|
129
|
def init_serial(self, serial_baud=None, serial_timeout=None):
|
switches |
0:0e018d759a2a
|
130
|
""" Initialize serial port.
|
switches |
0:0e018d759a2a
|
131
|
Function will return error is port can't be opened or initialized
|
switches |
0:0e018d759a2a
|
132
|
"""
|
switches |
0:0e018d759a2a
|
133
|
# Overload serial port configuration from default to parameters' values if they are specified
|
switches |
0:0e018d759a2a
|
134
|
serial_baud = serial_baud if serial_baud is not None else self.serial_baud
|
switches |
0:0e018d759a2a
|
135
|
serial_timeout = serial_timeout if serial_timeout is not None else self.serial_timeout
|
switches |
0:0e018d759a2a
|
136
|
|
switches |
0:0e018d759a2a
|
137
|
if get_module_avail('mbed_lstools') and self.options.auto_detect:
|
switches |
0:0e018d759a2a
|
138
|
# Ensure serial port is up-to-date (try to find it 60 times)
|
switches |
0:0e018d759a2a
|
139
|
found = False
|
switches |
0:0e018d759a2a
|
140
|
|
switches |
0:0e018d759a2a
|
141
|
for i in range(0, 60):
|
switches |
0:0e018d759a2a
|
142
|
print('Looking for %s with MBEDLS' % self.options.micro)
|
switches |
0:0e018d759a2a
|
143
|
muts_list = get_autodetected_MUTS_list(platform_name_filter=[self.options.micro])
|
switches |
0:0e018d759a2a
|
144
|
|
switches |
0:0e018d759a2a
|
145
|
if 1 in muts_list:
|
switches |
0:0e018d759a2a
|
146
|
mut = muts_list[1]
|
switches |
0:0e018d759a2a
|
147
|
self.port = mut['port']
|
switches |
0:0e018d759a2a
|
148
|
found = True
|
switches |
0:0e018d759a2a
|
149
|
break
|
switches |
0:0e018d759a2a
|
150
|
else:
|
switches |
0:0e018d759a2a
|
151
|
sleep(3)
|
switches |
0:0e018d759a2a
|
152
|
|
switches |
0:0e018d759a2a
|
153
|
if not found:
|
switches |
0:0e018d759a2a
|
154
|
return False
|
switches |
0:0e018d759a2a
|
155
|
|
switches |
0:0e018d759a2a
|
156
|
# Clear serial port
|
switches |
0:0e018d759a2a
|
157
|
if self.serial:
|
switches |
0:0e018d759a2a
|
158
|
self.serial.close()
|
switches |
0:0e018d759a2a
|
159
|
self.serial = None
|
switches |
0:0e018d759a2a
|
160
|
|
switches |
0:0e018d759a2a
|
161
|
# We will pool for serial to be re-mounted if it was unmounted after device reset
|
switches |
0:0e018d759a2a
|
162
|
result = self.pool_for_serial_init(serial_baud, serial_timeout) # Blocking
|
switches |
0:0e018d759a2a
|
163
|
|
switches |
0:0e018d759a2a
|
164
|
# Port can be opened
|
switches |
0:0e018d759a2a
|
165
|
if result:
|
switches |
0:0e018d759a2a
|
166
|
self.flush()
|
switches |
0:0e018d759a2a
|
167
|
return result
|
switches |
0:0e018d759a2a
|
168
|
|
switches |
0:0e018d759a2a
|
169
|
def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, init_delay=0.5, loop_delay=0.25):
|
switches |
0:0e018d759a2a
|
170
|
""" Functions pools for serial port readiness
|
switches |
0:0e018d759a2a
|
171
|
"""
|
switches |
0:0e018d759a2a
|
172
|
result = True
|
switches |
0:0e018d759a2a
|
173
|
last_error = None
|
switches |
0:0e018d759a2a
|
174
|
# This loop is used to check for serial port availability due to
|
switches |
0:0e018d759a2a
|
175
|
# some delays and remounting when devices are being flashed with new software.
|
switches |
0:0e018d759a2a
|
176
|
for i in range(pooling_loops):
|
switches |
0:0e018d759a2a
|
177
|
sleep(loop_delay if i else init_delay)
|
switches |
0:0e018d759a2a
|
178
|
try:
|
switches |
0:0e018d759a2a
|
179
|
self.serial = Serial(self.port, baudrate=serial_baud, timeout=serial_timeout)
|
switches |
0:0e018d759a2a
|
180
|
except Exception as e:
|
switches |
0:0e018d759a2a
|
181
|
result = False
|
switches |
0:0e018d759a2a
|
182
|
last_error = "MBED: %s"% str(e)
|
switches |
0:0e018d759a2a
|
183
|
stdout.write('.')
|
switches |
0:0e018d759a2a
|
184
|
stdout.flush()
|
switches |
0:0e018d759a2a
|
185
|
else:
|
switches |
0:0e018d759a2a
|
186
|
print "...port ready!"
|
switches |
0:0e018d759a2a
|
187
|
result = True
|
switches |
0:0e018d759a2a
|
188
|
break
|
switches |
0:0e018d759a2a
|
189
|
if not result and last_error:
|
switches |
0:0e018d759a2a
|
190
|
print last_error
|
switches |
0:0e018d759a2a
|
191
|
return result
|
switches |
0:0e018d759a2a
|
192
|
|
switches |
0:0e018d759a2a
|
193
|
def set_serial_timeout(self, timeout):
|
switches |
0:0e018d759a2a
|
194
|
""" Wraps self.mbed.serial object timeout property
|
switches |
0:0e018d759a2a
|
195
|
"""
|
switches |
0:0e018d759a2a
|
196
|
result = None
|
switches |
0:0e018d759a2a
|
197
|
if self.serial:
|
switches |
0:0e018d759a2a
|
198
|
self.serial.timeout = timeout
|
switches |
0:0e018d759a2a
|
199
|
result = True
|
switches |
0:0e018d759a2a
|
200
|
return result
|
switches |
0:0e018d759a2a
|
201
|
|
switches |
0:0e018d759a2a
|
202
|
def serial_read(self, count=1):
|
switches |
0:0e018d759a2a
|
203
|
""" Wraps self.mbed.serial object read method
|
switches |
0:0e018d759a2a
|
204
|
"""
|
switches |
0:0e018d759a2a
|
205
|
result = None
|
switches |
0:0e018d759a2a
|
206
|
if self.serial:
|
switches |
0:0e018d759a2a
|
207
|
try:
|
switches |
0:0e018d759a2a
|
208
|
result = self.serial.read(count)
|
switches |
0:0e018d759a2a
|
209
|
except:
|
switches |
0:0e018d759a2a
|
210
|
result = None
|
switches |
0:0e018d759a2a
|
211
|
return result
|
switches |
0:0e018d759a2a
|
212
|
|
switches |
0:0e018d759a2a
|
213
|
def serial_readline(self, timeout=5):
|
switches |
0:0e018d759a2a
|
214
|
""" Wraps self.mbed.serial object read method to read one line from serial port
|
switches |
0:0e018d759a2a
|
215
|
"""
|
switches |
0:0e018d759a2a
|
216
|
result = ''
|
switches |
0:0e018d759a2a
|
217
|
start = time()
|
switches |
0:0e018d759a2a
|
218
|
while (time() - start) < timeout:
|
switches |
0:0e018d759a2a
|
219
|
if self.serial:
|
switches |
0:0e018d759a2a
|
220
|
try:
|
switches |
0:0e018d759a2a
|
221
|
c = self.serial.read(1)
|
switches |
0:0e018d759a2a
|
222
|
result += c
|
switches |
0:0e018d759a2a
|
223
|
except Exception as e:
|
switches |
0:0e018d759a2a
|
224
|
print "MBED: %s"% str(e)
|
switches |
0:0e018d759a2a
|
225
|
result = None
|
switches |
0:0e018d759a2a
|
226
|
break
|
switches |
0:0e018d759a2a
|
227
|
if c == '\n':
|
switches |
0:0e018d759a2a
|
228
|
break
|
switches |
0:0e018d759a2a
|
229
|
return result
|
switches |
0:0e018d759a2a
|
230
|
|
switches |
0:0e018d759a2a
|
231
|
def serial_write(self, write_buffer):
|
switches |
0:0e018d759a2a
|
232
|
""" Wraps self.mbed.serial object write method
|
switches |
0:0e018d759a2a
|
233
|
"""
|
switches |
0:0e018d759a2a
|
234
|
result = None
|
switches |
0:0e018d759a2a
|
235
|
if self.serial:
|
switches |
0:0e018d759a2a
|
236
|
try:
|
switches |
0:0e018d759a2a
|
237
|
result = self.serial.write(write_buffer)
|
switches |
0:0e018d759a2a
|
238
|
except:
|
switches |
0:0e018d759a2a
|
239
|
result = None
|
switches |
0:0e018d759a2a
|
240
|
return result
|
switches |
0:0e018d759a2a
|
241
|
|
switches |
0:0e018d759a2a
|
242
|
def reset_timeout(self, timeout):
|
switches |
0:0e018d759a2a
|
243
|
""" Timeout executed just after reset command is issued
|
switches |
0:0e018d759a2a
|
244
|
"""
|
switches |
0:0e018d759a2a
|
245
|
for n in range(0, timeout):
|
switches |
0:0e018d759a2a
|
246
|
sleep(1)
|
switches |
0:0e018d759a2a
|
247
|
|
switches |
0:0e018d759a2a
|
248
|
def reset(self):
|
switches |
0:0e018d759a2a
|
249
|
""" Calls proper reset plugin to do the job.
|
switches |
0:0e018d759a2a
|
250
|
Please refer to host_test_plugins functionality
|
switches |
0:0e018d759a2a
|
251
|
"""
|
switches |
0:0e018d759a2a
|
252
|
# Flush serials to get only input after reset
|
switches |
0:0e018d759a2a
|
253
|
self.flush()
|
switches |
0:0e018d759a2a
|
254
|
if self.options.forced_reset_type:
|
switches |
0:0e018d759a2a
|
255
|
result = host_tests_plugins.call_plugin('ResetMethod', self.options.forced_reset_type, disk=self.disk)
|
switches |
0:0e018d759a2a
|
256
|
else:
|
switches |
0:0e018d759a2a
|
257
|
result = host_tests_plugins.call_plugin('ResetMethod', 'default', serial=self.serial)
|
switches |
0:0e018d759a2a
|
258
|
# Give time to wait for the image loading
|
switches |
0:0e018d759a2a
|
259
|
reset_tout_s = self.options.forced_reset_timeout if self.options.forced_reset_timeout is not None else self.DEFAULT_RESET_TOUT
|
switches |
0:0e018d759a2a
|
260
|
self.reset_timeout(reset_tout_s)
|
switches |
0:0e018d759a2a
|
261
|
return result
|
switches |
0:0e018d759a2a
|
262
|
|
switches |
0:0e018d759a2a
|
263
|
def copy_image(self, image_path=None, disk=None, copy_method=None):
|
switches |
0:0e018d759a2a
|
264
|
""" Closure for copy_image_raw() method.
|
switches |
0:0e018d759a2a
|
265
|
Method which is actually copying image to mbed
|
switches |
0:0e018d759a2a
|
266
|
"""
|
switches |
0:0e018d759a2a
|
267
|
# Set closure environment
|
switches |
0:0e018d759a2a
|
268
|
image_path = image_path if image_path is not None else self.image_path
|
switches |
0:0e018d759a2a
|
269
|
disk = disk if disk is not None else self.disk
|
switches |
0:0e018d759a2a
|
270
|
copy_method = copy_method if copy_method is not None else self.copy_method
|
switches |
0:0e018d759a2a
|
271
|
# Call proper copy method
|
switches |
0:0e018d759a2a
|
272
|
result = self.copy_image_raw(image_path, disk, copy_method)
|
switches |
0:0e018d759a2a
|
273
|
return result
|
switches |
0:0e018d759a2a
|
274
|
|
switches |
0:0e018d759a2a
|
275
|
def copy_image_raw(self, image_path=None, disk=None, copy_method=None):
|
switches |
0:0e018d759a2a
|
276
|
""" Copy file depending on method you want to use. Handles exception
|
switches |
0:0e018d759a2a
|
277
|
and return code from shell copy commands.
|
switches |
0:0e018d759a2a
|
278
|
"""
|
switches |
0:0e018d759a2a
|
279
|
# image_path - Where is binary with target's firmware
|
switches |
0:0e018d759a2a
|
280
|
if copy_method is not None:
|
switches |
0:0e018d759a2a
|
281
|
# We override 'default' method with 'shell' method
|
switches |
0:0e018d759a2a
|
282
|
if copy_method == 'default':
|
switches |
0:0e018d759a2a
|
283
|
copy_method = 'shell'
|
switches |
0:0e018d759a2a
|
284
|
else:
|
switches |
0:0e018d759a2a
|
285
|
copy_method = 'shell'
|
switches |
0:0e018d759a2a
|
286
|
|
switches |
0:0e018d759a2a
|
287
|
result = host_tests_plugins.call_plugin('CopyMethod', copy_method, image_path=image_path, destination_disk=disk, program_cycle_s=self.program_cycle_s, target_mcu=self.options.micro)
|
switches |
0:0e018d759a2a
|
288
|
return result;
|
switches |
0:0e018d759a2a
|
289
|
|
switches |
0:0e018d759a2a
|
290
|
def flush(self):
|
switches |
0:0e018d759a2a
|
291
|
""" Flush serial ports
|
switches |
0:0e018d759a2a
|
292
|
"""
|
switches |
0:0e018d759a2a
|
293
|
result = False
|
switches |
0:0e018d759a2a
|
294
|
if self.serial:
|
switches |
0:0e018d759a2a
|
295
|
self.serial.flushInput()
|
switches |
0:0e018d759a2a
|
296
|
self.serial.flushOutput()
|
switches |
0:0e018d759a2a
|
297
|
result = True
|
switches |
0:0e018d759a2a
|
298
|
return result
|
switches |
0:0e018d759a2a
|
299
|
|
switches |
0:0e018d759a2a
|
300
|
|
switches |
0:0e018d759a2a
|
301
|
class HostTestResults:
|
switches |
0:0e018d759a2a
|
302
|
""" Test results set by host tests
|
switches |
0:0e018d759a2a
|
303
|
"""
|
switches |
0:0e018d759a2a
|
304
|
def __init__(self):
|
switches |
0:0e018d759a2a
|
305
|
self.RESULT_SUCCESS = 'success'
|
switches |
0:0e018d759a2a
|
306
|
self.RESULT_FAILURE = 'failure'
|
switches |
0:0e018d759a2a
|
307
|
self.RESULT_ERROR = 'error'
|
switches |
0:0e018d759a2a
|
308
|
self.RESULT_IO_SERIAL = 'ioerr_serial'
|
switches |
0:0e018d759a2a
|
309
|
self.RESULT_NO_IMAGE = 'no_image'
|
switches |
0:0e018d759a2a
|
310
|
self.RESULT_IOERR_COPY = "ioerr_copy"
|
switches |
0:0e018d759a2a
|
311
|
self.RESULT_PASSIVE = "passive"
|
switches |
0:0e018d759a2a
|
312
|
self.RESULT_NOT_DETECTED = "not_detected"
|
switches |
0:0e018d759a2a
|
313
|
self.RESULT_MBED_ASSERT = "mbed_assert"
|
switches |
0:0e018d759a2a
|
314
|
|
switches |
0:0e018d759a2a
|
315
|
|
switches |
0:0e018d759a2a
|
316
|
import tools.host_tests as host_tests
|
switches |
0:0e018d759a2a
|
317
|
|
switches |
0:0e018d759a2a
|
318
|
|
switches |
0:0e018d759a2a
|
319
|
class Test(HostTestResults):
|
switches |
0:0e018d759a2a
|
320
|
""" Base class for host test's test runner
|
switches |
0:0e018d759a2a
|
321
|
"""
|
switches |
0:0e018d759a2a
|
322
|
# Select default host_test supervision (replaced after autodetection)
|
switches |
0:0e018d759a2a
|
323
|
test_supervisor = host_tests.get_host_test("default")
|
switches |
0:0e018d759a2a
|
324
|
|
switches |
0:0e018d759a2a
|
325
|
def __init__(self):
|
switches |
0:0e018d759a2a
|
326
|
self.mbed = Mbed()
|
switches |
0:0e018d759a2a
|
327
|
|
switches |
0:0e018d759a2a
|
328
|
def detect_test_config(self, verbose=False):
|
switches |
0:0e018d759a2a
|
329
|
""" Detects test case configuration
|
switches |
0:0e018d759a2a
|
330
|
"""
|
switches |
0:0e018d759a2a
|
331
|
result = {}
|
switches |
0:0e018d759a2a
|
332
|
while True:
|
switches |
0:0e018d759a2a
|
333
|
line = self.mbed.serial_readline()
|
switches |
0:0e018d759a2a
|
334
|
if "{start}" in line:
|
switches |
0:0e018d759a2a
|
335
|
self.notify("HOST: Start test...")
|
switches |
0:0e018d759a2a
|
336
|
break
|
switches |
0:0e018d759a2a
|
337
|
else:
|
switches |
0:0e018d759a2a
|
338
|
# Detect if this is property from TEST_ENV print
|
switches |
0:0e018d759a2a
|
339
|
m = re.search('{([\w_]+);([\w\d\+ ]+)}}', line[:-1])
|
switches |
0:0e018d759a2a
|
340
|
if m and len(m.groups()) == 2:
|
switches |
0:0e018d759a2a
|
341
|
# This is most likely auto-detection property
|
switches |
0:0e018d759a2a
|
342
|
result[m.group(1)] = m.group(2)
|
switches |
0:0e018d759a2a
|
343
|
if verbose:
|
switches |
0:0e018d759a2a
|
344
|
self.notify("HOST: Property '%s' = '%s'"% (m.group(1), m.group(2)))
|
switches |
0:0e018d759a2a
|
345
|
else:
|
switches |
0:0e018d759a2a
|
346
|
# We can check if this is TArget Id in mbed specific format
|
switches |
0:0e018d759a2a
|
347
|
m2 = re.search('^([\$]+)([a-fA-F0-9]+)', line[:-1])
|
switches |
0:0e018d759a2a
|
348
|
if m2 and len(m2.groups()) == 2:
|
switches |
0:0e018d759a2a
|
349
|
if verbose:
|
switches |
0:0e018d759a2a
|
350
|
target_id = m2.group(1) + m2.group(2)
|
switches |
0:0e018d759a2a
|
351
|
self.notify("HOST: TargetID '%s'"% target_id)
|
switches |
0:0e018d759a2a
|
352
|
self.notify(line[len(target_id):-1])
|
switches |
0:0e018d759a2a
|
353
|
else:
|
switches |
0:0e018d759a2a
|
354
|
self.notify("HOST: Unknown property: %s"% line.strip())
|
switches |
0:0e018d759a2a
|
355
|
return result
|
switches |
0:0e018d759a2a
|
356
|
|
switches |
0:0e018d759a2a
|
357
|
def run(self):
|
switches |
0:0e018d759a2a
|
358
|
""" Test runner for host test. This function will start executing
|
switches |
0:0e018d759a2a
|
359
|
test and forward test result via serial port to test suite
|
switches |
0:0e018d759a2a
|
360
|
"""
|
switches |
0:0e018d759a2a
|
361
|
# Copy image to device
|
switches |
0:0e018d759a2a
|
362
|
self.notify("HOST: Copy image onto target...")
|
switches |
0:0e018d759a2a
|
363
|
result = self.mbed.copy_image()
|
switches |
0:0e018d759a2a
|
364
|
if not result:
|
switches |
0:0e018d759a2a
|
365
|
self.print_result(self.RESULT_IOERR_COPY)
|
switches |
0:0e018d759a2a
|
366
|
|
switches |
0:0e018d759a2a
|
367
|
# Initialize and open target's serial port (console)
|
switches |
0:0e018d759a2a
|
368
|
self.notify("HOST: Initialize serial port...")
|
switches |
0:0e018d759a2a
|
369
|
result = self.mbed.init_serial()
|
switches |
0:0e018d759a2a
|
370
|
if not result:
|
switches |
0:0e018d759a2a
|
371
|
self.print_result(self.RESULT_IO_SERIAL)
|
switches |
0:0e018d759a2a
|
372
|
|
switches |
0:0e018d759a2a
|
373
|
# Reset device
|
switches |
0:0e018d759a2a
|
374
|
self.notify("HOST: Reset target...")
|
switches |
0:0e018d759a2a
|
375
|
result = self.mbed.reset()
|
switches |
0:0e018d759a2a
|
376
|
if not result:
|
switches |
0:0e018d759a2a
|
377
|
self.print_result(self.RESULT_IO_SERIAL)
|
switches |
0:0e018d759a2a
|
378
|
|
switches |
0:0e018d759a2a
|
379
|
# Run test
|
switches |
0:0e018d759a2a
|
380
|
try:
|
switches |
0:0e018d759a2a
|
381
|
CONFIG = self.detect_test_config(verbose=True) # print CONFIG
|
switches |
0:0e018d759a2a
|
382
|
|
switches |
0:0e018d759a2a
|
383
|
if "host_test_name" in CONFIG:
|
switches |
0:0e018d759a2a
|
384
|
if host_tests.is_host_test(CONFIG["host_test_name"]):
|
switches |
0:0e018d759a2a
|
385
|
self.test_supervisor = host_tests.get_host_test(CONFIG["host_test_name"])
|
switches |
0:0e018d759a2a
|
386
|
result = self.test_supervisor.test(self) #result = self.test()
|
switches |
0:0e018d759a2a
|
387
|
|
switches |
0:0e018d759a2a
|
388
|
if result is not None:
|
switches |
0:0e018d759a2a
|
389
|
self.print_result(result)
|
switches |
0:0e018d759a2a
|
390
|
else:
|
switches |
0:0e018d759a2a
|
391
|
self.notify("HOST: Passive mode...")
|
switches |
0:0e018d759a2a
|
392
|
except Exception, e:
|
switches |
0:0e018d759a2a
|
393
|
print str(e)
|
switches |
0:0e018d759a2a
|
394
|
self.print_result(self.RESULT_ERROR)
|
switches |
0:0e018d759a2a
|
395
|
|
switches |
0:0e018d759a2a
|
396
|
def setup(self):
|
switches |
0:0e018d759a2a
|
397
|
""" Setup and check if configuration for test is
|
switches |
0:0e018d759a2a
|
398
|
correct. E.g. if serial port can be opened.
|
switches |
0:0e018d759a2a
|
399
|
"""
|
switches |
0:0e018d759a2a
|
400
|
result = True
|
switches |
0:0e018d759a2a
|
401
|
if not self.mbed.serial:
|
switches |
0:0e018d759a2a
|
402
|
result = False
|
switches |
0:0e018d759a2a
|
403
|
self.print_result(self.RESULT_IO_SERIAL)
|
switches |
0:0e018d759a2a
|
404
|
return result
|
switches |
0:0e018d759a2a
|
405
|
|
switches |
0:0e018d759a2a
|
406
|
def notify(self, message):
|
switches |
0:0e018d759a2a
|
407
|
""" On screen notification function
|
switches |
0:0e018d759a2a
|
408
|
"""
|
switches |
0:0e018d759a2a
|
409
|
print message
|
switches |
0:0e018d759a2a
|
410
|
stdout.flush()
|
switches |
0:0e018d759a2a
|
411
|
|
switches |
0:0e018d759a2a
|
412
|
def print_result(self, result):
|
switches |
0:0e018d759a2a
|
413
|
""" Test result unified printing function
|
switches |
0:0e018d759a2a
|
414
|
"""
|
switches |
0:0e018d759a2a
|
415
|
self.notify("\r\n{{%s}}\r\n{{end}}" % result)
|
switches |
0:0e018d759a2a
|
416
|
|
switches |
0:0e018d759a2a
|
417
|
|
switches |
0:0e018d759a2a
|
418
|
class DefaultTestSelector(Test):
|
switches |
0:0e018d759a2a
|
419
|
""" Test class with serial port initialization
|
switches |
0:0e018d759a2a
|
420
|
"""
|
switches |
0:0e018d759a2a
|
421
|
def __init__(self):
|
switches |
0:0e018d759a2a
|
422
|
HostTestResults.__init__(self)
|
switches |
0:0e018d759a2a
|
423
|
Test.__init__(self)
|
switches |
0:0e018d759a2a
|
424
|
|
switches |
0:0e018d759a2a
|
425
|
if __name__ == '__main__':
|
switches |
0:0e018d759a2a
|
426
|
DefaultTestSelector().run()
|