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