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