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