Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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