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