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