mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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