Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Servo Server.""" |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 5 | import datetime |
| 6 | import fcntl |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 7 | import fnmatch |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 8 | import logging |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 9 | import os |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 10 | import re |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 11 | import SimpleXMLRPCServer |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 12 | import threading |
Todd Broch | 7a91c25 | 2012-02-03 12:37:45 -0800 | [diff] [blame] | 13 | import time |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 14 | import usb |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 15 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 16 | import drv as servo_drv |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 17 | import bbadc |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 18 | import bbi2c |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 19 | import bbgpio |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 20 | import bbuart |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 21 | import ec3po_interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 22 | import ftdigpio |
| 23 | import ftdii2c |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 24 | import ftdi_common |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 25 | import ftdiuart |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 26 | import i2cbus |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 27 | import keyboard_handlers |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 28 | import servo_interfaces |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 29 | import servo_postinit |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 30 | import stm32gpio |
| 31 | import stm32i2c |
| 32 | import stm32uart |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 33 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 34 | HwDriverError = servo_drv.hw_driver.HwDriverError |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 35 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 36 | MAX_I2C_CLOCK_HZ = 100000 |
| 37 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 38 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 39 | class ServodError(Exception): |
| 40 | """Exception class for servod.""" |
| 41 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 42 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 43 | class Servod(object): |
| 44 | """Main class for Servo debug/controller Daemon.""" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 45 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 46 | # This is the key to get the main serial used in the _serialnames dict. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 47 | MAIN_SERIAL = 'main' |
| 48 | SERVO_MICRO_SERIAL = 'servo_micro' |
| 49 | CCD_SERIAL = 'ccd' |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 50 | |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 51 | # Timeout to wait for interfaces to become available again if reinitialization |
| 52 | # is taking place. In seconds. |
| 53 | INTERFACE_AVAILABILITY_TIMEOUT = 60 |
| 54 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 55 | def init_servo_interfaces(self, vendor, product, serialname, interfaces): |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 56 | """Init the servo interfaces with the given interfaces. |
| 57 | |
| 58 | We don't use the self._{vendor,product,serialname} attributes because we |
| 59 | want to allow other callers to initialize other interfaces that may not |
| 60 | be associated with the initialized attributes (e.g. a servo v4 servod object |
| 61 | that wants to also initialize a servo micro interface). |
| 62 | |
| 63 | Args: |
| 64 | vendor: USB vendor id of FTDI device. |
| 65 | product: USB product id of FTDI device. |
| 66 | serialname: String of device serialname/number as defined in FTDI |
| 67 | eeprom. |
| 68 | interfaces: List of strings of interface types the server will |
| 69 | instantiate. |
| 70 | |
| 71 | Raises: |
| 72 | ServodError if unable to locate init method for particular interface. |
| 73 | """ |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 74 | # If it is a new device add it to the list |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 75 | device = (vendor, product, serialname) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 76 | if device not in self._devices: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 77 | self._devices.append(device) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 78 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 79 | # Extend the interface list if we need to. |
| 80 | interfaces_len = len(interfaces) |
| 81 | interface_list_len = len(self._interface_list) |
| 82 | if interfaces_len > interface_list_len: |
| 83 | self._interface_list += [None] * (interfaces_len - interface_list_len) |
| 84 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 85 | for i, interface in enumerate(interfaces): |
| 86 | is_ftdi_interface = False |
| 87 | if type(interface) is dict: |
| 88 | name = interface['name'] |
| 89 | # Store interface index for those that care about it. |
| 90 | interface['index'] = i |
| 91 | elif type(interface) is str and interface != 'dummy': |
| 92 | name = interface |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 93 | # It's a FTDI related interface. #0 is reserved for no use. |
| 94 | interface = ((i - 1) % ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE) + 1 |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 95 | is_ftdi_interface = True |
| 96 | elif type(interface) is str and interface == 'dummy': |
| 97 | # 'dummy' reserves the interface for future use. Typically the |
| 98 | # interface will be managed by external third-party tools like |
| 99 | # openOCD for JTAG or flashrom for SPI. In the case of servo V4, |
| 100 | # it serves as a placeholder for servo micro interfaces. |
| 101 | continue |
| 102 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 103 | raise ServodError('Illegal interface type %s' % type(interface)) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 104 | |
| 105 | # servos with multiple FTDI are guaranteed to have contiguous USB PIDs |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 106 | product_increment = 0 |
| 107 | if is_ftdi_interface: |
| 108 | product_increment = (i - 1) / ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE |
| 109 | if product_increment: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 110 | self._logger.info('Use the next FTDI part @ pid = 0x%04x', |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 111 | product + product_increment) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 112 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 113 | self._logger.info('Initializing interface %d to %s', i, name) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 114 | try: |
| 115 | func = getattr(self, '_init_%s' % name) |
| 116 | except AttributeError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 117 | raise ServodError('Unable to locate init for interface %s' % name) |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 118 | result = func(vendor, product + product_increment, serialname, interface) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 119 | |
| 120 | if isinstance(result, tuple): |
| 121 | result_len = len(result) |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 122 | self._interface_list[i:(i + result_len)] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 123 | else: |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 124 | self._interface_list[i] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 125 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 126 | def __init__(self, config, vendor, product, serialname=None, interfaces=None, |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 127 | board='', model='', version=None, usbkm232=None): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 128 | """Servod constructor. |
| 129 | |
| 130 | Args: |
| 131 | config: instance of SystemConfig containing all controls for |
| 132 | particular Servod invocation |
| 133 | vendor: usb vendor id of FTDI device |
| 134 | product: usb product id of FTDI device |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 135 | serialname: string of device serialname/number as defined in FTDI eeprom. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 136 | interfaces: list of strings of interface types the server will instantiate |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 137 | board: board name. e.g. octopus, coral, or scarlet. |
| 138 | model: model name of a given board. e.g. fleex, ampton, or apel. |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 139 | version: String. Servo board version. Examples: servo_v1, servo_v2, |
| 140 | servo_v2_r0, servo_v3 |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 141 | usbkm232: String. Optional. Path to USB-KM232 device which allow for |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 142 | sending keyboard commands to DUTs that do not have built in |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 143 | keyboards. Used in FAFT tests. Use None for on board AVR MCU. |
| 144 | e.g. '/dev/ttyUSB0' or None. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 145 | |
| 146 | Raises: |
| 147 | ServodError: if unable to locate init method for particular interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 148 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 149 | self._logger = logging.getLogger('Servod') |
| 150 | self._logger.debug('') |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 151 | self._ifaces_available = threading.Event() |
| 152 | # Initially interfaces should be available. |
| 153 | self._ifaces_available.set() |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 154 | self._vendor = vendor |
| 155 | self._product = product |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 156 | self._devices = [] |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 157 | self._serialnames = {self.MAIN_SERIAL: serialname} |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 158 | self._syscfg = config |
| 159 | # list of objects (Fi2c, Fgpio) to physical interfaces (gpio, i2c) that ftdi |
| 160 | # interfaces are mapped to |
| 161 | self._interface_list = [] |
| 162 | # Dict of Dict to map control name, function name to to tuple (params, drv) |
| 163 | # Ex) _drv_dict[name]['get'] = (params, drv) |
| 164 | self._drv_dict = {} |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 165 | self._base_board = '' |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 166 | self._board = board |
| 167 | if model: |
| 168 | self._board += '_' + model |
| 169 | self._model = model |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 170 | self._version = version |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 171 | self._usbkm232 = usbkm232 |
Ruben Rodriguez Buchillon | 386a010 | 2018-08-16 09:11:20 +0800 | [diff] [blame] | 172 | self._keyboard = None |
| 173 | self._usb_keyboard = None |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 174 | if not interfaces: |
Todd Broch | b21d804 | 2014-05-15 12:54:54 -0700 | [diff] [blame] | 175 | try: |
| 176 | interfaces = servo_interfaces.INTERFACE_BOARDS[board][vendor][product] |
| 177 | except KeyError: |
| 178 | interfaces = servo_interfaces.INTERFACE_DEFAULTS[vendor][product] |
Dino Li | c89d8c8 | 2018-01-11 09:56:47 +0800 | [diff] [blame] | 179 | self._interfaces = interfaces |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 180 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 181 | self.init_servo_interfaces(vendor, product, serialname, interfaces) |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 182 | servo_postinit.post_init(self) |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 183 | |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 184 | def reinitialize(self): |
| 185 | """Reinitialize all interfaces that support reinitialization""" |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 186 | for i, interface in enumerate(self._interface_list): |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 187 | if hasattr(interface, 'reinitialize'): |
| 188 | interface.reinitialize() |
| 189 | else: |
| 190 | self._logger.debug('interface %d has no reset functionality', i) |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 191 | # Indicate interfaces are safe to use again. |
| 192 | self._ifaces_available.set() |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 193 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 194 | def get_servo_interfaces(self, position, size): |
| 195 | """Get the list of servo interfaces. |
| 196 | |
| 197 | Args: |
| 198 | position: The index the first interface to get. |
| 199 | size: The number of the interfaces. |
| 200 | """ |
| 201 | return self._interface_list[position:(position + size)] |
| 202 | |
| 203 | def set_servo_interfaces(self, position, interfaces): |
| 204 | """Set the list of servo interfaces. |
| 205 | |
| 206 | Args: |
| 207 | position: The index the first interface to set. |
| 208 | interfaces: The list of interfaces to set. |
| 209 | """ |
| 210 | size = len(interfaces) |
| 211 | self._interface_list[position:(position + size)] = interfaces |
| 212 | |
Ruben Rodriguez Buchillon | a16374b | 2018-06-20 16:45:00 -0700 | [diff] [blame] | 213 | def close(self): |
| 214 | """Servod turn down logic.""" |
| 215 | for i, interface in enumerate(self._interface_list): |
| 216 | self._logger.info('Turning down interface %d' % i) |
| 217 | if hasattr(interface, 'close'): |
| 218 | interface.close() |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 219 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 220 | def _init_ftdi_dummy(self, vendor, product, serialname, interface): |
Kevin Cheng | 042f493 | 2016-07-19 10:46:00 -0700 | [diff] [blame] | 221 | """Dummy interface for ftdi devices. |
| 222 | |
| 223 | This is a dummy function specifically for ftdi devices to not initialize |
| 224 | anything but to help pad the interface list. |
| 225 | |
| 226 | Returns: |
| 227 | None. |
| 228 | """ |
| 229 | return None |
| 230 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 231 | def _init_ftdi_gpio(self, vendor, product, serialname, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 232 | """Initialize gpio driver interface and open for use. |
| 233 | |
| 234 | Args: |
| 235 | interface: interface number of FTDI device to use. |
| 236 | |
| 237 | Returns: |
| 238 | Instance object of interface. |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 239 | |
| 240 | Raises: |
| 241 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 242 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 243 | fobj = ftdigpio.Fgpio(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 244 | try: |
| 245 | fobj.open() |
| 246 | except ftdigpio.FgpioError as e: |
| 247 | raise ServodError('Opening gpio interface. %s ( %d )' % (e.msg, e.value)) |
| 248 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 249 | return fobj |
| 250 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 251 | def _init_stm32_uart(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 252 | """Initialize stm32 uart interface and open for use |
| 253 | |
| 254 | Note, the uart runs in a separate thread. Users wishing to |
| 255 | interact with it will query control for the pty's pathname and connect |
| 256 | with their favorite console program. For example: |
| 257 | cu -l /dev/pts/22 |
| 258 | |
| 259 | Args: |
| 260 | interface: dict of interface parameters. |
| 261 | |
| 262 | Returns: |
| 263 | Instance object of interface |
| 264 | |
| 265 | Raises: |
| 266 | ServodError: Raised on init failure. |
| 267 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 268 | self._logger.info('Suart: interface: %s' % interface) |
| 269 | sobj = stm32uart.Suart(vendor, product, interface['interface'], serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 270 | |
| 271 | try: |
| 272 | sobj.run() |
| 273 | except stm32uart.SuartError as e: |
| 274 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 275 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 276 | self._logger.info('%s' % sobj.get_pty()) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 277 | return sobj |
| 278 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 279 | def _init_stm32_gpio(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 280 | """Initialize stm32 gpio interface. |
| 281 | Args: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 282 | interface: dict of interface parameters. |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 283 | |
| 284 | Returns: |
| 285 | Instance object of interface |
| 286 | |
| 287 | Raises: |
| 288 | SgpioError: Raised on init failure. |
| 289 | """ |
Kevin Cheng | 71a046f | 2016-06-13 16:37:58 -0700 | [diff] [blame] | 290 | interface_number = interface |
| 291 | # Interface could be a dict. |
| 292 | if type(interface) is dict: |
| 293 | interface_number = interface['interface'] |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 294 | self._logger.info('Sgpio: interface: %s' % interface_number) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 295 | return stm32gpio.Sgpio(vendor, product, interface_number, serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 296 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 297 | def _init_stm32_i2c(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 298 | """Initialize stm32 USB to I2C bridge interface and open for use |
| 299 | |
| 300 | Args: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 301 | interface: dict of interface parameters. |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 302 | |
| 303 | Returns: |
| 304 | Instance object of interface. |
| 305 | |
| 306 | Raises: |
| 307 | Si2cError: Raised on init failure. |
| 308 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 309 | self._logger.info('Si2cBus: interface: %s' % interface) |
Nick Sanders | a364971 | 2016-03-01 16:53:52 -0800 | [diff] [blame] | 310 | port = interface.get('port', 0) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 311 | return stm32i2c.Si2cBus(vendor, product, interface['interface'], port=port, |
| 312 | serialname=serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 313 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 314 | def _init_bb_adc(self, vendor, product, serialname, interface): |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 315 | """Initalize beaglebone ADC interface.""" |
| 316 | return bbadc.BBadc() |
| 317 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 318 | def _init_bb_gpio(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 319 | """Initalize beaglebone gpio interface.""" |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 320 | return bbgpio.BBgpio() |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 321 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 322 | def _init_ftdi_i2c(self, vendor, product, serialname, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 323 | """Initialize i2c interface and open for use. |
| 324 | |
| 325 | Args: |
| 326 | interface: interface number of FTDI device to use |
| 327 | |
| 328 | Returns: |
| 329 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 330 | |
| 331 | Raises: |
| 332 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 333 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 334 | fobj = ftdii2c.Fi2c(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 335 | try: |
| 336 | fobj.open() |
| 337 | except ftdii2c.Fi2cError as e: |
| 338 | raise ServodError('Opening i2c interface. %s ( %d )' % (e.msg, e.value)) |
| 339 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 340 | # Set the frequency of operation of the i2c bus. |
| 341 | # TODO(tbroch) make configureable |
| 342 | fobj.setclock(MAX_I2C_CLOCK_HZ) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 343 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 344 | return fobj |
| 345 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 346 | # TODO (sbasi) crbug.com/187489 - Implement bb_i2c. |
| 347 | def _init_bb_i2c(self, interface): |
| 348 | """Initalize beaglebone i2c interface.""" |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 349 | return bbi2c.BBi2c(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 350 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 351 | def _init_dev_i2c(self, vendor, product, serialname, interface): |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 352 | """Initalize Linux i2c-dev interface.""" |
| 353 | return i2cbus.I2CBus('/dev/i2c-%d' % interface['bus_num']) |
| 354 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 355 | def _init_ftdi_uart(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 356 | """Initialize ftdi uart inteface and open for use |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 357 | |
| 358 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 359 | interact with it will query control for the pty's pathname and connect |
| 360 | with there favorite console program. For example: |
| 361 | cu -l /dev/pts/22 |
| 362 | |
| 363 | Args: |
| 364 | interface: interface number of FTDI device to use |
| 365 | |
| 366 | Returns: |
| 367 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 368 | |
| 369 | Raises: |
| 370 | ServodError: If init fails |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 371 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 372 | fobj = ftdiuart.Fuart(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 373 | try: |
| 374 | fobj.run() |
| 375 | except ftdiuart.FuartError as e: |
| 376 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 377 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 378 | self._logger.info('%s' % fobj.get_pty()) |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 379 | return fobj |
| 380 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 381 | # TODO (sbasi) crbug.com/187492 - Implement bbuart. |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 382 | def _init_bb_uart(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 383 | """Initalize beaglebone uart interface.""" |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 384 | logging.debug('UART INTERFACE: %s', interface) |
| 385 | return bbuart.BBuart(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 386 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 387 | def _init_ftdi_gpiouart(self, vendor, product, serialname, interface): |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 388 | """Initialize special gpio + uart interface and open for use |
| 389 | |
| 390 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 391 | interact with it will query control for the pty's pathname and connect |
| 392 | with there favorite console program. For example: |
| 393 | cu -l /dev/pts/22 |
| 394 | |
| 395 | Args: |
| 396 | interface: interface number of FTDI device to use |
| 397 | |
| 398 | Returns: |
| 399 | Instance objects of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 400 | |
| 401 | Raises: |
| 402 | ServodError: If init fails |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 403 | """ |
Kevin Cheng | ce7dafd | 2016-08-02 11:11:38 -0700 | [diff] [blame] | 404 | fgpio = self._init_ftdi_gpio(vendor, product, serialname, interface) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 405 | fuart = ftdiuart.Fuart(vendor, product, interface, serialname, fgpio._fc) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 406 | try: |
| 407 | fuart.run() |
| 408 | except ftdiuart.FuartError as e: |
| 409 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 410 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 411 | self._logger.info('uart pty: %s' % fuart.get_pty()) |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 412 | return fgpio, fuart |
| 413 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 414 | def _init_ec3po_uart(self, vendor, product, serialname, interface): |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 415 | """Initialize EC-3PO console interpreter interface. |
| 416 | |
| 417 | Args: |
| 418 | interface: A dictionary representing the interface. |
| 419 | |
| 420 | Returns: |
| 421 | An EC3PO object representing the EC-3PO interface or None if there's no |
| 422 | interface for the USB PD UART. |
| 423 | """ |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 424 | raw_uart_name = interface['raw_pty'] |
Nick Sanders | 116ed9e | 2018-03-09 19:05:16 -0800 | [diff] [blame] | 425 | raw_uart_source = interface['source'] |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 426 | if self._syscfg.is_control(raw_uart_name): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 427 | raw_ec_uart = self.get(raw_uart_name) |
Nick Sanders | 116ed9e | 2018-03-09 19:05:16 -0800 | [diff] [blame] | 428 | return ec3po_interface.EC3PO(raw_ec_uart, raw_uart_source) |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 429 | else: |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 430 | # The overlay doesn't have the raw PTY defined, therefore we can skip |
| 431 | # initializing this interface since no control relies on it. |
| 432 | self._logger.debug( |
| 433 | 'Skip initializing EC3PO for %s, no control specified.', |
| 434 | raw_uart_name) |
| 435 | return None |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 436 | |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 437 | def _camel_case(self, string): |
| 438 | output = '' |
| 439 | for s in string.split('_'): |
| 440 | if output: |
| 441 | output += s.capitalize() |
| 442 | else: |
| 443 | output = s |
| 444 | return output |
| 445 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 446 | def clear_cached_drv(self): |
| 447 | """Clear the cached drivers. |
| 448 | |
| 449 | The drivers are cached in the Dict _drv_dict when a control is got or set. |
| 450 | When the servo interfaces are relocated, the cached values may become wrong. |
| 451 | Should call this method to clear the cached values. |
| 452 | """ |
| 453 | self._drv_dict = {} |
| 454 | |
Ruben Rodriguez Buchillon | 2c6589f | 2018-10-20 15:30:26 +0800 | [diff] [blame] | 455 | def _get_servo_specific_param(self, params, param_key, control_name): |
| 456 | """Get |param_key| from params by looking for servo specific params first. |
| 457 | |
| 458 | Find the candidate servos. Using servo_v4 with a servo_micro connected as |
| 459 | example, the following shows the priority for selecting the interface. |
| 460 | |
| 461 | 1. The full name. (e.g. - 'servo_v4_with_servo_micro_interface') |
| 462 | 2. servo_micro_interface |
| 463 | 3. servo_v4_interface |
| 464 | 4. Fallback to the default, interface. |
| 465 | |
| 466 | Args: |
| 467 | params: params dictionary for a control |
| 468 | param_key: identifier in the params dictionary to look for |
| 469 | control_name: control name the params correspond to |
| 470 | |
| 471 | Returns: |
| 472 | The best suited param value for param_key given the servo type or |
| 473 | None if even the default is not defined. |
| 474 | """ |
| 475 | candidates = [self._version] |
| 476 | candidates.extend(reversed(self._version.split('_with_'))) |
| 477 | candidates = ['%s_%s' % (c, param_key) for c in candidates] |
| 478 | candidates.append(param_key) |
| 479 | for c in candidates: |
| 480 | if c in params: |
| 481 | self._logger.debug('Using %s parameter.', c) |
| 482 | return params[c] |
| 483 | self._logger.error('Unable to determine %s for %s', param_key, control_name) |
| 484 | self._logger.error('params: %r', params) |
| 485 | return None |
| 486 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 487 | def _get_param_drv(self, control_name, is_get=True): |
| 488 | """Get access to driver for a given control. |
| 489 | |
| 490 | Note, some controls have different parameter dictionaries for 'getting' the |
| 491 | control's value versus 'setting' it. Boolean is_get distinguishes which is |
| 492 | being requested. |
| 493 | |
| 494 | Args: |
| 495 | control_name: string name of control |
| 496 | is_get: boolean to determine |
| 497 | |
| 498 | Returns: |
| 499 | tuple (param, drv) where: |
| 500 | param: param dictionary for control |
| 501 | drv: instance object of driver for particular control |
| 502 | |
| 503 | Raises: |
| 504 | ServodError: Error occurred while examining params dict |
| 505 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 506 | self._logger.debug('') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 507 | # if already setup just return tuple from driver dict |
| 508 | if control_name in self._drv_dict: |
| 509 | if is_get and ('get' in self._drv_dict[control_name]): |
| 510 | return self._drv_dict[control_name]['get'] |
| 511 | if not is_get and ('set' in self._drv_dict[control_name]): |
| 512 | return self._drv_dict[control_name]['set'] |
| 513 | |
| 514 | params = self._syscfg.lookup_control_params(control_name, is_get) |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 515 | |
Ruben Rodriguez Buchillon | 2c6589f | 2018-10-20 15:30:26 +0800 | [diff] [blame] | 516 | # Get the most suitable drv given the servo instance. |
| 517 | drv_name = self._get_servo_specific_param(params, 'drv', control_name) |
| 518 | if drv_name == 'na': |
| 519 | # 'na' drv can be used to selectively turn controls into noops for |
| 520 | # a given servo hardware. Ensure that there is an interface. |
| 521 | params.setdefault('interface', 'servo') |
| 522 | self._logger.debug('Setting interface to default to %r for %r unless ' |
| 523 | ' defined in params, as drv is %r.', 'servo', |
| 524 | control_name, 'na') |
| 525 | # Setting input_type to str allows all inputs through enabling a true noop |
| 526 | params.update({'input_type': 'str'}) |
| 527 | interface_id = self._get_servo_specific_param(params, 'interface', |
| 528 | control_name) |
| 529 | if None in [drv_name, interface_id]: |
| 530 | raise ServodError('No drv/interface for control %r found' % control_name) |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 531 | |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 532 | if interface_id == 'servo': |
| 533 | interface = self |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 534 | else: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 535 | index = int(interface_id) |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 536 | interface = self._interface_list[index] |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 537 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 538 | drv_module = getattr(servo_drv, drv_name) |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 539 | drv_class = getattr(drv_module, self._camel_case(drv_name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 540 | drv = drv_class(interface, params) |
| 541 | if control_name not in self._drv_dict: |
| 542 | self._drv_dict[control_name] = {} |
| 543 | if is_get: |
| 544 | self._drv_dict[control_name]['get'] = (params, drv) |
| 545 | else: |
| 546 | self._drv_dict[control_name]['set'] = (params, drv) |
| 547 | return (params, drv) |
| 548 | |
| 549 | def doc_all(self): |
| 550 | """Return all documenation for controls. |
| 551 | |
| 552 | Returns: |
| 553 | string of <doc> text in config file (xml) and the params dictionary for |
| 554 | all controls. |
| 555 | |
| 556 | For example: |
| 557 | warm_reset :: Reset the device warmly |
| 558 | ------------------------> {'interface': '1', 'map': 'onoff_i', ... } |
| 559 | """ |
| 560 | return self._syscfg.display_config() |
| 561 | |
| 562 | def doc(self, name): |
| 563 | """Retreive doc string in system config file for given control name. |
| 564 | |
| 565 | Args: |
| 566 | name: name string of control to get doc string |
| 567 | |
| 568 | Returns: |
| 569 | doc string of name |
| 570 | |
| 571 | Raises: |
| 572 | NameError: if fails to locate control |
| 573 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 574 | self._logger.debug('name(%s)' % (name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 575 | if self._syscfg.is_control(name): |
| 576 | return self._syscfg.get_control_docstring(name) |
| 577 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 578 | raise NameError('No control %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 579 | |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 580 | def safe_switch_usbkey_power(self, power_state, _=None): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 581 | """Toggle the usb power safely. |
| 582 | |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 583 | Args: |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 584 | power_state: The setting to set for the usbkey power. |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 585 | _: to conform to current API |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 586 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 587 | Returns: |
| 588 | An empty string to appease the xmlrpc gods. |
| 589 | """ |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 590 | self.set('image_usbkey_pwr', power_state) |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 591 | return '' |
| 592 | |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 593 | def safe_switch_usbkey(self, mux_direction, _=0): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 594 | """Toggle the usb direction safely. |
| 595 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 596 | Args: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 597 | mux_direction: "servo_sees_usbkey" or "dut_sees_usbkey". |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 598 | _: to conform to current API |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 599 | |
| 600 | Returns: |
| 601 | An empty string to appease the xmlrpc gods. |
| 602 | """ |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 603 | self.set('image_usbkey_direction', mux_direction) |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 604 | return '' |
| 605 | |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 606 | def probe_host_usb_dev(self, _=0): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 607 | """Probe the USB disk device plugged in the servo from the host side. |
| 608 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 609 | Args: |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 610 | _: to conform to current API |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 611 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 612 | Returns: |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 613 | USB disk path if one and only one USB disk path is found, otherwise an |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 614 | """ |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 615 | return self.get('image_usbkey_dev') |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 616 | |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 617 | def download_image_to_usb(self, image_path, _=0): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 618 | """Download image and save to the USB device found by probe_host_usb_dev. |
| 619 | If the image_path is a URL, it will download this url to the USB path; |
| 620 | otherwise it will simply copy the image_path's contents to the USB path. |
| 621 | |
| 622 | Args: |
| 623 | image_path: path or url to the recovery image. |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 624 | _: to conform to current API |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 625 | |
| 626 | Returns: |
| 627 | True|False: True if process completed successfully, False if error |
Ruben Rodriguez Buchillon | 4e00f0e | 2018-08-28 15:21:47 +0800 | [diff] [blame] | 628 | occurred. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 629 | """ |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 630 | try: |
Ruben Rodriguez Buchillon | 4e00f0e | 2018-08-28 15:21:47 +0800 | [diff] [blame] | 631 | self.set('download_image_to_usb_dev', image_path) |
| 632 | return True |
| 633 | except Exception: |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 634 | return False |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 635 | |
| 636 | def make_image_noninteractive(self): |
| 637 | """Makes the recovery image noninteractive. |
| 638 | |
| 639 | A noninteractive image will reboot automatically after installation |
| 640 | instead of waiting for the USB device to be removed to initiate a system |
| 641 | reboot. |
| 642 | |
| 643 | Mounts partition 1 of the image stored on usb_dev and creates a file |
| 644 | called "non_interactive" so that the image will become noninteractive. |
| 645 | |
| 646 | Returns: |
| 647 | True|False: True if process completed successfully, False if error |
Ruben Rodriguez Buchillon | 1a3a7ec | 2018-08-03 18:44:55 +0800 | [diff] [blame] | 648 | occurred. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 649 | """ |
Ruben Rodriguez Buchillon | 1a3a7ec | 2018-08-03 18:44:55 +0800 | [diff] [blame] | 650 | try: |
| 651 | usb_dev = self.get('image_usbkey_dev') |
| 652 | usb_dev_partition = '%s1' % usb_dev |
| 653 | self.set('make_usb_dev_image_noninteractive', usb_dev_partition) |
| 654 | return True |
| 655 | except Exception: |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 656 | return False |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 657 | |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 658 | def set_get_all(self, cmds): |
| 659 | """Set &| get one or more control values. |
| 660 | |
| 661 | Args: |
| 662 | cmds: list of control[:value] to get or set. |
| 663 | |
| 664 | Returns: |
| 665 | rv: list of responses from calling get or set methods. |
| 666 | """ |
| 667 | rv = [] |
| 668 | for cmd in cmds: |
| 669 | if ':' in cmd: |
Wai-Hong Tam | 269f180 | 2019-05-16 12:37:17 -0700 | [diff] [blame] | 670 | (control, value) = cmd.split(':', 1) |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 671 | rv.append(self.set(control, value)) |
| 672 | else: |
| 673 | rv.append(self.get(cmd)) |
| 674 | return rv |
| 675 | |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 676 | def get_serial_number(self, name): |
| 677 | """Returns the desired serial number from the serialnames dict. |
| 678 | |
| 679 | Args: |
| 680 | name: A string which is the key into the _serialnames dictionary. |
| 681 | |
| 682 | Returns: |
| 683 | A string containing the serial number or "unknown". |
| 684 | """ |
| 685 | if not name: |
| 686 | name = 'main' |
| 687 | |
| 688 | try: |
| 689 | return self._serialnames[name] |
| 690 | except KeyError: |
| 691 | self._logger.debug("'%s_serialname' not found!", name) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 692 | return 'unknown' |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 693 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 694 | def get(self, name): |
| 695 | """Get control value. |
| 696 | |
| 697 | Args: |
| 698 | name: name string of control |
| 699 | |
| 700 | Returns: |
| 701 | Response from calling drv get method. Value is reformatted based on |
| 702 | control's dictionary parameters |
| 703 | |
| 704 | Raises: |
| 705 | HwDriverError: Error occurred while using drv |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 706 | ServodError: if interfaces are not available within timeout period |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 707 | """ |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 708 | if not self._ifaces_available.wait(self.INTERFACE_AVAILABILITY_TIMEOUT): |
| 709 | raise ServodError('Timed out waiting for interfaces to become available.') |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 710 | self._logger.debug('name(%s)' % (name)) |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 711 | # This route is to retrieve serialnames on servo v4, which |
| 712 | # connects to multiple servo-micros or CCD, like the controls, |
| 713 | # 'ccd_serialname', 'servo_micro_for_soraka_serialname', etc. |
| 714 | # TODO(aaboagye): Refactor it. |
| 715 | if 'serialname' in name: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 716 | return self.get_serial_number(name.split('serialname')[0].strip('_')) |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 717 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 718 | (param, drv) = self._get_param_drv(name) |
| 719 | try: |
| 720 | val = drv.get() |
| 721 | rd_val = self._syscfg.reformat_val(param, val) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 722 | self._logger.debug('%s = %s' % (name, rd_val)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 723 | return rd_val |
Todd Broch | fbc499d | 2011-06-16 16:09:58 -0700 | [diff] [blame] | 724 | except AttributeError, error: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 725 | self._logger.error('Getting %s: %s' % (name, error)) |
Todd Broch | fbc499d | 2011-06-16 16:09:58 -0700 | [diff] [blame] | 726 | raise |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 727 | except HwDriverError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 728 | self._logger.error('Getting %s' % (name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 729 | raise |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 730 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 731 | def get_all(self, verbose): |
| 732 | """Get all controls values. |
| 733 | |
| 734 | Args: |
| 735 | verbose: Boolean on whether to return doc info as well |
| 736 | |
| 737 | Returns: |
| 738 | string creating from trying to get all values of all controls. In case of |
| 739 | error attempting access to control, response is 'ERR'. |
| 740 | """ |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 741 | rsp = [] |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 742 | for name in self._syscfg.syscfg_dict['control']: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 743 | self._logger.debug('name = %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 744 | try: |
| 745 | value = self.get(name) |
| 746 | except Exception: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 747 | value = 'ERR' |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 748 | pass |
| 749 | if verbose: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 750 | rsp.append('GET %s = %s :: %s' % (name, value, self.doc(name))) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 751 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 752 | rsp.append('%s:%s' % (name, value)) |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 753 | return '\n'.join(sorted(rsp)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 754 | |
| 755 | def set(self, name, wr_val_str): |
| 756 | """Set control. |
| 757 | |
| 758 | Args: |
| 759 | name: name string of control |
| 760 | wr_val_str: value string to write. Can be integer, float or a |
| 761 | alpha-numerical that is mapped to a integer or float. |
| 762 | |
| 763 | Raises: |
| 764 | HwDriverError: Error occurred while using driver |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 765 | ServodError: if interfaces are not available within timeout period |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 766 | """ |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 767 | if not self._ifaces_available.wait(self.INTERFACE_AVAILABILITY_TIMEOUT): |
| 768 | raise ServodError('Timed out waiting for interfaces to become available.') |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 769 | self._logger.debug('name(%s) wr_val(%s)' % (name, wr_val_str)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 770 | (params, drv) = self._get_param_drv(name, False) |
| 771 | wr_val = self._syscfg.resolve_val(params, wr_val_str) |
| 772 | try: |
| 773 | drv.set(wr_val) |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 774 | except HwDriverError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 775 | self._logger.error('Setting %s -> %s' % (name, wr_val_str)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 776 | raise |
Ruben Rodriguez Buchillon | b5fe0f1 | 2018-05-09 10:19:56 +0800 | [diff] [blame] | 777 | # TODO(crbug.com/841097) Figure out why despite allow_none=True for both |
| 778 | # xmlrpc server & client I still have to return something to appease the |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 779 | # marshall/unmarshall |
| 780 | return True |
| 781 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 782 | def hwinit(self, verbose=False): |
| 783 | """Initialize all controls. |
| 784 | |
| 785 | These values are part of the system config XML files of the form |
| 786 | init=<value>. This command should be used by clients wishing to return the |
| 787 | servo and DUT its connected to a known good/safe state. |
| 788 | |
Vadim Bendebury | bb51dd4 | 2013-01-31 13:47:46 -0800 | [diff] [blame] | 789 | Note that initialization errors are ignored (as in some cases they could |
| 790 | be caused by DUT firmware deficiencies). This might need to be fine tuned |
| 791 | later. |
| 792 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 793 | Args: |
| 794 | verbose: boolean, if True prints info about control initialized. |
| 795 | Otherwise prints nothing. |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 796 | |
| 797 | Returns: |
| 798 | This function is called across RPC and as such is expected to return |
| 799 | something unless transferring 'none' across is allowed. Hence adding a |
| 800 | dummy return value to make things simpler. |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 801 | """ |
Todd Broch | d9acf0a | 2012-12-05 13:43:06 -0800 | [diff] [blame] | 802 | for control_name, value in self._syscfg.hwinit: |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 803 | try: |
John Carey | 6fe2bbf | 2015-08-31 16:13:03 -0700 | [diff] [blame] | 804 | # Workaround for bug chrome-os-partner:42349. Without this check, the |
| 805 | # gpio will briefly pulse low if we set it from high to high. |
| 806 | if self.get(control_name) != value: |
Aseda Aboagye | a849d46 | 2016-05-04 17:08:16 -0700 | [diff] [blame] | 807 | self.set(control_name, value) |
| 808 | if verbose: |
| 809 | self._logger.info('Initialized %s to %s', control_name, value) |
Matthew Blecker | a5d979c | 2018-10-16 20:59:19 -0700 | [diff] [blame] | 810 | except Exception: |
| 811 | self._logger.exception( |
| 812 | 'Problem initializing %s -> %s', control_name, value) |
Nick Sanders | bc83628 | 2015-12-08 21:19:23 -0800 | [diff] [blame] | 813 | |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 814 | return True |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 815 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 816 | def echo(self, echo): |
| 817 | """Dummy echo function for testing/examples. |
| 818 | |
| 819 | Args: |
| 820 | echo: string to echo back to client |
| 821 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 822 | self._logger.debug('echo(%s)' % (echo)) |
| 823 | return 'ECH0ING: %s' % (echo) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 824 | |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 825 | def get_board(self): |
| 826 | """Return the board specified at startup, if any.""" |
| 827 | return self._board |
| 828 | |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 829 | def get_base_board(self): |
| 830 | """Returns the board name of the base if present. |
| 831 | |
| 832 | Returns: |
| 833 | A string of the board name, or '' if not present. |
| 834 | """ |
| 835 | # The value is set in servo_postinit. |
| 836 | return self._base_board |
| 837 | |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 838 | def get_version(self): |
| 839 | """Get servo board version.""" |
| 840 | return self._version |
| 841 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 842 | def power_long_press(self): |
| 843 | """Simulate a long power button press.""" |
| 844 | # After a long power press, the EC may ignore the next power |
| 845 | # button press (at least on Alex). To guarantee that this |
| 846 | # won't happen, we need to allow the EC one second to |
| 847 | # collect itself. |
Ruben Rodriguez Buchillon | 0f46794 | 2018-07-27 18:02:32 +0800 | [diff] [blame] | 848 | return self.set('power_key', 'long_press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 849 | |
| 850 | def power_normal_press(self): |
| 851 | """Simulate a normal power button press.""" |
Ruben Rodriguez Buchillon | 0f46794 | 2018-07-27 18:02:32 +0800 | [diff] [blame] | 852 | return self.set('power_key', 'press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 853 | |
| 854 | def power_short_press(self): |
| 855 | """Simulate a short power button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 856 | return self.set('power_key', 'short_press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 857 | |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 858 | def power_key(self, press_secs=''): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 859 | """Simulate a power button press. |
| 860 | |
| 861 | Args: |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 862 | press_secs: Time in seconds to simulate the keypress. |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 863 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 864 | return self.set('power_key', 'press' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 865 | |
| 866 | def ctrl_d(self, press_secs=''): |
| 867 | """Simulate Ctrl-d simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 868 | return self.set('ctrl_d', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 869 | |
Victor Dodon | e539cea | 2016-03-29 18:50:17 -0700 | [diff] [blame] | 870 | def ctrl_u(self, press_secs=''): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 871 | """Simulate Ctrl-u simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 872 | return self.set('ctrl_u', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 873 | |
| 874 | def ctrl_enter(self, press_secs=''): |
| 875 | """Simulate Ctrl-enter simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 876 | return self.set('ctrl_enter', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 877 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 878 | def ctrl_key(self, press_secs=''): |
| 879 | """Simulate Enter key button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 880 | return self.set('ctrl_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 881 | |
| 882 | def enter_key(self, press_secs=''): |
| 883 | """Simulate Enter key button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 884 | return self.set('enter_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 885 | |
| 886 | def refresh_key(self, press_secs=''): |
| 887 | """Simulate Refresh key (F3) button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 888 | return self.set('refresh_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 889 | |
| 890 | def ctrl_refresh_key(self, press_secs=''): |
| 891 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 892 | |
| 893 | This key combination is an alternative of Space key. |
| 894 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 895 | return self.set('ctrl_refresh_key', ('tab' if press_secs is '' else |
| 896 | press_secs)) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 897 | |
| 898 | def imaginary_key(self, press_secs=''): |
| 899 | """Simulate imaginary key button press. |
| 900 | |
| 901 | Maps to a key that doesn't physically exist. |
| 902 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 903 | return self.set('imaginary_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 904 | |
Vincent Palatin | 3acbbe5 | 2016-07-19 17:40:12 +0200 | [diff] [blame] | 905 | def sysrq_x(self, press_secs=''): |
| 906 | """Simulate Alt VolumeUp X simultaneous press. |
| 907 | |
| 908 | This key combination is the kernel system request (sysrq) x. |
| 909 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 910 | return self.set('sysrq_x', 'tab' if press_secs is '' else press_secs) |
Vincent Palatin | 3acbbe5 | 2016-07-19 17:40:12 +0200 | [diff] [blame] | 911 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 912 | def get_servo_serials(self): |
| 913 | """Return all the serials associated with this process.""" |
| 914 | return self._serialnames |
| 915 | |
| 916 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 917 | def test(): |
| 918 | """Integration testing. |
| 919 | |
| 920 | TODO(tbroch) Enhance integration test and add unittest (see mox) |
| 921 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 922 | logging.basicConfig( |
| 923 | level=logging.DEBUG, |
| 924 | format='%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 925 | # configure server & listen |
| 926 | servod_obj = Servod(1) |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 927 | # 5 == number of interfaces on a FT4232H device |
| 928 | for i in xrange(1, 5): |
| 929 | if i == 2: |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 930 | # its an i2c interface ... see __init__ for details and TODO to make |
| 931 | # this configureable |
| 932 | servod_obj._interface_list[i].wr_rd(0x21, [0], 1) |
| 933 | else: |
| 934 | # its a gpio interface |
| 935 | servod_obj._interface_list[i].wr_rd(0) |
| 936 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 937 | server = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 9999), |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 938 | allow_none=True) |
| 939 | server.register_introspection_functions() |
| 940 | server.register_multicall_functions() |
| 941 | server.register_instance(servod_obj) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 942 | logging.info('Listening on localhost port 9999') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 943 | server.serve_forever() |
| 944 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 945 | |
| 946 | if __name__ == '__main__': |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 947 | test() |
| 948 | |
| 949 | # simple client transaction would look like |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 950 | """remote_uri = 'http://localhost:9999' client = xmlrpclib.ServerProxy(remote_uri, verbose=False) send_str = "Hello_there" print "Sent " + send_str + ", Recv " + client.echo(send_str) |
| 951 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 952 | """ |