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 | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 5 | import contextlib |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 6 | import datetime |
| 7 | import fcntl |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 8 | import fnmatch |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 9 | import logging |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 10 | import os |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 11 | import random |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 12 | import re |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 13 | import shutil |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 14 | import SimpleXMLRPCServer |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 15 | import subprocess |
| 16 | import tempfile |
Todd Broch | 7a91c25 | 2012-02-03 12:37:45 -0800 | [diff] [blame] | 17 | import time |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 18 | import urllib |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 19 | import usb |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 20 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 21 | import drv as servo_drv |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 22 | import bbadc |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 23 | import bbi2c |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 24 | import bbgpio |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 25 | import bbuart |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 26 | import ec3po_interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 27 | import ftdigpio |
| 28 | import ftdii2c |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 29 | import ftdi_common |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 30 | import ftdiuart |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 31 | import i2cbus |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 32 | import keyboard_handlers |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 33 | import servo_interfaces |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 34 | import servo_postinit |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 35 | import stm32gpio |
| 36 | import stm32i2c |
| 37 | import stm32uart |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 38 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 39 | HwDriverError = servo_drv.hw_driver.HwDriverError |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 40 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 41 | MAX_I2C_CLOCK_HZ = 100000 |
| 42 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 43 | # It takes about 16-17 seconds for the entire probe usb device method, |
| 44 | # let's wait double plus some buffer. |
| 45 | _MAX_USB_LOCK_WAIT = 40 |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 46 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 47 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 48 | class ServodError(Exception): |
| 49 | """Exception class for servod.""" |
| 50 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 51 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 52 | class Servod(object): |
| 53 | """Main class for Servo debug/controller Daemon.""" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 54 | _USB_DETECTION_DELAY = 10 |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 55 | _USB_POWEROFF_DELAY = 2 |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 56 | _HTTP_PREFIX = 'http://' |
| 57 | _USB_J3 = 'usb_mux_sel1' |
| 58 | _USB_J3_TO_SERVO = 'servo_sees_usbkey' |
| 59 | _USB_J3_TO_DUT = 'dut_sees_usbkey' |
| 60 | _USB_J3_PWR = 'prtctl4_pwren' |
| 61 | _USB_J3_PWR_ON = 'on' |
| 62 | _USB_J3_PWR_OFF = 'off' |
| 63 | _USB_LOCK_FILE = '/var/lib/servod/lock_file' |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 64 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 65 | # 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] | 66 | MAIN_SERIAL = 'main' |
| 67 | SERVO_MICRO_SERIAL = 'servo_micro' |
| 68 | CCD_SERIAL = 'ccd' |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 69 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 70 | def init_servo_interfaces(self, vendor, product, serialname, interfaces): |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 71 | """Init the servo interfaces with the given interfaces. |
| 72 | |
| 73 | We don't use the self._{vendor,product,serialname} attributes because we |
| 74 | want to allow other callers to initialize other interfaces that may not |
| 75 | be associated with the initialized attributes (e.g. a servo v4 servod object |
| 76 | that wants to also initialize a servo micro interface). |
| 77 | |
| 78 | Args: |
| 79 | vendor: USB vendor id of FTDI device. |
| 80 | product: USB product id of FTDI device. |
| 81 | serialname: String of device serialname/number as defined in FTDI |
| 82 | eeprom. |
| 83 | interfaces: List of strings of interface types the server will |
| 84 | instantiate. |
| 85 | |
| 86 | Raises: |
| 87 | ServodError if unable to locate init method for particular interface. |
| 88 | """ |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 89 | # If it is a new device add it to the list |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 90 | device = (vendor, product, serialname) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 91 | if device not in self._devices: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 92 | self._devices.append(device) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 93 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 94 | # Extend the interface list if we need to. |
| 95 | interfaces_len = len(interfaces) |
| 96 | interface_list_len = len(self._interface_list) |
| 97 | if interfaces_len > interface_list_len: |
| 98 | self._interface_list += [None] * (interfaces_len - interface_list_len) |
| 99 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 100 | for i, interface in enumerate(interfaces): |
| 101 | is_ftdi_interface = False |
| 102 | if type(interface) is dict: |
| 103 | name = interface['name'] |
| 104 | # Store interface index for those that care about it. |
| 105 | interface['index'] = i |
| 106 | elif type(interface) is str and interface != 'dummy': |
| 107 | name = interface |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 108 | # It's a FTDI related interface. #0 is reserved for no use. |
| 109 | interface = ((i - 1) % ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE) + 1 |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 110 | is_ftdi_interface = True |
| 111 | elif type(interface) is str and interface == 'dummy': |
| 112 | # 'dummy' reserves the interface for future use. Typically the |
| 113 | # interface will be managed by external third-party tools like |
| 114 | # openOCD for JTAG or flashrom for SPI. In the case of servo V4, |
| 115 | # it serves as a placeholder for servo micro interfaces. |
| 116 | continue |
| 117 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 118 | raise ServodError('Illegal interface type %s' % type(interface)) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 119 | |
| 120 | # servos with multiple FTDI are guaranteed to have contiguous USB PIDs |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 121 | product_increment = 0 |
| 122 | if is_ftdi_interface: |
| 123 | product_increment = (i - 1) / ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE |
| 124 | if product_increment: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 125 | self._logger.info('Use the next FTDI part @ pid = 0x%04x', |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 126 | product + product_increment) |
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 | self._logger.info('Initializing interface %d to %s', i, name) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 129 | try: |
| 130 | func = getattr(self, '_init_%s' % name) |
| 131 | except AttributeError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 132 | raise ServodError('Unable to locate init for interface %s' % name) |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 133 | result = func(vendor, product + product_increment, serialname, interface) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 134 | |
| 135 | if isinstance(result, tuple): |
| 136 | result_len = len(result) |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 137 | self._interface_list[i:(i + result_len)] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 138 | else: |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 139 | self._interface_list[i] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 140 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 141 | def __init__(self, config, vendor, product, serialname=None, interfaces=None, |
| 142 | board='', version=None, usbkm232=None): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 143 | """Servod constructor. |
| 144 | |
| 145 | Args: |
| 146 | config: instance of SystemConfig containing all controls for |
| 147 | particular Servod invocation |
| 148 | vendor: usb vendor id of FTDI device |
| 149 | product: usb product id of FTDI device |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 150 | serialname: string of device serialname/number as defined in FTDI eeprom. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 151 | interfaces: list of strings of interface types the server will instantiate |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 152 | version: String. Servo board version. Examples: servo_v1, servo_v2, |
| 153 | servo_v2_r0, servo_v3 |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 154 | usbkm232: String. Optional. Path to USB-KM232 device which allow for |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 155 | sending keyboard commands to DUTs that do not have built in |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 156 | keyboards. Used in FAFT tests. Use None for on board AVR MCU. |
| 157 | e.g. '/dev/ttyUSB0' or None. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 158 | |
| 159 | Raises: |
| 160 | ServodError: if unable to locate init method for particular interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 161 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 162 | self._logger = logging.getLogger('Servod') |
| 163 | self._logger.debug('') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 164 | self._vendor = vendor |
| 165 | self._product = product |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 166 | self._devices = [] |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 167 | self._serialnames = {self.MAIN_SERIAL: serialname} |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 168 | self._syscfg = config |
Kevin Cheng | 9071ed9 | 2016-06-21 14:37:54 -0700 | [diff] [blame] | 169 | # Hold the last image path so we can reduce downloads to the usb device. |
| 170 | self._image_path = None |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 171 | # list of objects (Fi2c, Fgpio) to physical interfaces (gpio, i2c) that ftdi |
| 172 | # interfaces are mapped to |
| 173 | self._interface_list = [] |
| 174 | # Dict of Dict to map control name, function name to to tuple (params, drv) |
| 175 | # Ex) _drv_dict[name]['get'] = (params, drv) |
| 176 | self._drv_dict = {} |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 177 | self._board = board |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 178 | self._base_board = '' |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 179 | self._version = version |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 180 | self._usbkm232 = usbkm232 |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 181 | # Seed the random generator with the serial to differentiate from other |
| 182 | # servod processes. |
| 183 | random.seed(serialname if serialname else time.time()) |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 184 | if not interfaces: |
Todd Broch | b21d804 | 2014-05-15 12:54:54 -0700 | [diff] [blame] | 185 | try: |
| 186 | interfaces = servo_interfaces.INTERFACE_BOARDS[board][vendor][product] |
| 187 | except KeyError: |
| 188 | interfaces = servo_interfaces.INTERFACE_DEFAULTS[vendor][product] |
Dino Li | c89d8c8 | 2018-01-11 09:56:47 +0800 | [diff] [blame] | 189 | self._interfaces = interfaces |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 190 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 191 | self.init_servo_interfaces(vendor, product, serialname, interfaces) |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 192 | servo_postinit.post_init(self) |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 193 | |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 194 | def reinitialize(self): |
| 195 | """Reinitialize all interfaces that support reinitialization""" |
| 196 | |
| 197 | # If all of the devices that were originally connected are not connected |
| 198 | # now, wait up to max_tries * sleep_time for the devices to reconnect |
| 199 | max_tries = 10 |
| 200 | sleep_time = 0.5 |
| 201 | for i in range(max_tries): |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 202 | for vid, pid, serialname in self._devices: |
| 203 | for current in usb.core.find(idVendor=vid, idProduct=pid, |
| 204 | find_all=True): |
| 205 | current_serial = usb.util.get_string(current, 256, |
| 206 | current.iSerialNumber) |
| 207 | if not serialname or serialname == current_serial: |
| 208 | # This device still available |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 209 | break |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 210 | else: |
| 211 | self._logger.warn('Servo USB device not found: %x:%x serial:%s', vid, |
| 212 | pid, serialname) |
| 213 | break |
| 214 | else: |
| 215 | # All the devices found. Reinitialize the interfaces... |
| 216 | break |
| 217 | time.sleep(sleep_time) |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 218 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 219 | raise ServodError('Servo USB device not found during reinitialize') |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 220 | |
| 221 | for i, interface in enumerate(self._interface_list): |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 222 | if hasattr(interface, 'reinitialize'): |
| 223 | interface.reinitialize() |
| 224 | else: |
| 225 | self._logger.debug('interface %d has no reset functionality', i) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 226 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 227 | def get_servo_interfaces(self, position, size): |
| 228 | """Get the list of servo interfaces. |
| 229 | |
| 230 | Args: |
| 231 | position: The index the first interface to get. |
| 232 | size: The number of the interfaces. |
| 233 | """ |
| 234 | return self._interface_list[position:(position + size)] |
| 235 | |
| 236 | def set_servo_interfaces(self, position, interfaces): |
| 237 | """Set the list of servo interfaces. |
| 238 | |
| 239 | Args: |
| 240 | position: The index the first interface to set. |
| 241 | interfaces: The list of interfaces to set. |
| 242 | """ |
| 243 | size = len(interfaces) |
| 244 | self._interface_list[position:(position + size)] = interfaces |
| 245 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 246 | def _init_keyboard_handler(self, servo, board=''): |
| 247 | """Initialize the correct keyboard handler for board. |
| 248 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 249 | Args: |
| 250 | servo: servo object. |
| 251 | board: string, board name. |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 252 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 253 | Returns: |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 254 | keyboard handler object, or None if no keyboard supported. |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 255 | """ |
| 256 | if board == 'parrot': |
| 257 | return keyboard_handlers.ParrotHandler(servo) |
| 258 | elif board == 'stout': |
| 259 | return keyboard_handlers.StoutHandler(servo) |
PeggyChuang | 4f07d87 | 2015-08-07 12:11:38 +0800 | [diff] [blame] | 260 | elif board in ('buddy', 'cranky', 'guado', 'jecht', 'mccloud', 'monroe', |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 261 | 'ninja', 'nyan_kitty', 'panther', 'rikku', 'stumpy', 'sumo', |
| 262 | 'tidus', 'tricky', 'veyron_fievel', 'veyron_mickey', |
Shelley Chen | 94cd235 | 2017-07-26 11:36:45 -0700 | [diff] [blame] | 263 | 'veyron_rialto', 'veyron_tiger', 'zako'): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 264 | if self._usbkm232 is None: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 265 | logging.info('No device path specified for usbkm232 handler. Use ' |
| 266 | 'the servo atmega chip to handle.') |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 267 | |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 268 | # Use servo onboard keyboard emulator. |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 269 | if not self._syscfg.is_control('atmega_rst'): |
| 270 | logging.warn('No atmega in servo board. So no keyboard support.') |
| 271 | return None |
| 272 | |
Nick Sanders | 7842378 | 2015-11-09 14:28:19 -0800 | [diff] [blame] | 273 | self.set('atmega_rst', 'on') |
Nick Sanders | bc83628 | 2015-12-08 21:19:23 -0800 | [diff] [blame] | 274 | self.set('at_hwb', 'off') |
Nick Sanders | 7842378 | 2015-11-09 14:28:19 -0800 | [diff] [blame] | 275 | self.set('atmega_rst', 'off') |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 276 | self._usbkm232 = self.get('atmega_pty') |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 277 | |
Kevin Cheng | 810fc78 | 2016-11-01 12:36:46 -0700 | [diff] [blame] | 278 | # 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] | 279 | if 'servo_v4' not in self._version: |
Kevin Cheng | 810fc78 | 2016-11-01 12:36:46 -0700 | [diff] [blame] | 280 | self.set('atmega_baudrate', '9600') |
| 281 | self.set('atmega_bits', 'eight') |
| 282 | self.set('atmega_parity', 'none') |
| 283 | self.set('atmega_sbits', 'one') |
| 284 | self.set('usb_mux_sel4', 'on') |
| 285 | self.set('usb_mux_oe4', 'on') |
| 286 | # Allow atmega bootup time. |
| 287 | time.sleep(1.0) |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 288 | |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 289 | self._logger.info('USBKM232: %s', self._usbkm232) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 290 | return keyboard_handlers.USBkm232Handler(servo, self._usbkm232) |
| 291 | else: |
Tom Wai-Hong Tam | d64164c | 2015-04-29 07:59:45 +0800 | [diff] [blame] | 292 | # The following boards don't use Chrome EC. |
| 293 | if board in ('alex', 'butterfly', 'lumpy', 'zgb'): |
| 294 | return keyboard_handlers.MatrixKeyboardHandler(servo) |
| 295 | return keyboard_handlers.ChromeECHandler(servo) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 296 | |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 297 | def __del__(self): |
| 298 | """Servod deconstructor.""" |
| 299 | for interface in self._interface_list: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 300 | del (interface) |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 301 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 302 | def _init_ftdi_dummy(self, vendor, product, serialname, interface): |
Kevin Cheng | 042f493 | 2016-07-19 10:46:00 -0700 | [diff] [blame] | 303 | """Dummy interface for ftdi devices. |
| 304 | |
| 305 | This is a dummy function specifically for ftdi devices to not initialize |
| 306 | anything but to help pad the interface list. |
| 307 | |
| 308 | Returns: |
| 309 | None. |
| 310 | """ |
| 311 | return None |
| 312 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 313 | def _init_ftdi_gpio(self, vendor, product, serialname, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 314 | """Initialize gpio driver interface and open for use. |
| 315 | |
| 316 | Args: |
| 317 | interface: interface number of FTDI device to use. |
| 318 | |
| 319 | Returns: |
| 320 | Instance object of interface. |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 321 | |
| 322 | Raises: |
| 323 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 324 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 325 | fobj = ftdigpio.Fgpio(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 326 | try: |
| 327 | fobj.open() |
| 328 | except ftdigpio.FgpioError as e: |
| 329 | raise ServodError('Opening gpio interface. %s ( %d )' % (e.msg, e.value)) |
| 330 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 331 | return fobj |
| 332 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 333 | def _init_stm32_uart(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 334 | """Initialize stm32 uart interface and open for use |
| 335 | |
| 336 | Note, the uart runs in a separate thread. Users wishing to |
| 337 | interact with it will query control for the pty's pathname and connect |
| 338 | with their favorite console program. For example: |
| 339 | cu -l /dev/pts/22 |
| 340 | |
| 341 | Args: |
| 342 | interface: dict of interface parameters. |
| 343 | |
| 344 | Returns: |
| 345 | Instance object of interface |
| 346 | |
| 347 | Raises: |
| 348 | ServodError: Raised on init failure. |
| 349 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 350 | self._logger.info('Suart: interface: %s' % interface) |
| 351 | sobj = stm32uart.Suart(vendor, product, interface['interface'], serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 352 | |
| 353 | try: |
| 354 | sobj.run() |
| 355 | except stm32uart.SuartError as e: |
| 356 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 357 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 358 | self._logger.info('%s' % sobj.get_pty()) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 359 | return sobj |
| 360 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 361 | def _init_stm32_gpio(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 362 | """Initialize stm32 gpio interface. |
| 363 | Args: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 364 | interface: dict of interface parameters. |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 365 | |
| 366 | Returns: |
| 367 | Instance object of interface |
| 368 | |
| 369 | Raises: |
| 370 | SgpioError: Raised on init failure. |
| 371 | """ |
Kevin Cheng | 71a046f | 2016-06-13 16:37:58 -0700 | [diff] [blame] | 372 | interface_number = interface |
| 373 | # Interface could be a dict. |
| 374 | if type(interface) is dict: |
| 375 | interface_number = interface['interface'] |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 376 | self._logger.info('Sgpio: interface: %s' % interface_number) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 377 | return stm32gpio.Sgpio(vendor, product, interface_number, serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 378 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 379 | def _init_stm32_i2c(self, vendor, product, serialname, interface): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 380 | """Initialize stm32 USB to I2C bridge interface and open for use |
| 381 | |
| 382 | Args: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 383 | interface: dict of interface parameters. |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 384 | |
| 385 | Returns: |
| 386 | Instance object of interface. |
| 387 | |
| 388 | Raises: |
| 389 | Si2cError: Raised on init failure. |
| 390 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 391 | self._logger.info('Si2cBus: interface: %s' % interface) |
Nick Sanders | a364971 | 2016-03-01 16:53:52 -0800 | [diff] [blame] | 392 | port = interface.get('port', 0) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 393 | return stm32i2c.Si2cBus(vendor, product, interface['interface'], port=port, |
| 394 | serialname=serialname) |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 395 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 396 | def _init_bb_adc(self, vendor, product, serialname, interface): |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 397 | """Initalize beaglebone ADC interface.""" |
| 398 | return bbadc.BBadc() |
| 399 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 400 | def _init_bb_gpio(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 401 | """Initalize beaglebone gpio interface.""" |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 402 | return bbgpio.BBgpio() |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 403 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 404 | def _init_ftdi_i2c(self, vendor, product, serialname, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 405 | """Initialize i2c interface and open for use. |
| 406 | |
| 407 | Args: |
| 408 | interface: interface number of FTDI device to use |
| 409 | |
| 410 | Returns: |
| 411 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 412 | |
| 413 | Raises: |
| 414 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 415 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 416 | fobj = ftdii2c.Fi2c(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 417 | try: |
| 418 | fobj.open() |
| 419 | except ftdii2c.Fi2cError as e: |
| 420 | raise ServodError('Opening i2c interface. %s ( %d )' % (e.msg, e.value)) |
| 421 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 422 | # Set the frequency of operation of the i2c bus. |
| 423 | # TODO(tbroch) make configureable |
| 424 | fobj.setclock(MAX_I2C_CLOCK_HZ) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 425 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 426 | return fobj |
| 427 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 428 | # TODO (sbasi) crbug.com/187489 - Implement bb_i2c. |
| 429 | def _init_bb_i2c(self, interface): |
| 430 | """Initalize beaglebone i2c interface.""" |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 431 | return bbi2c.BBi2c(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 432 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 433 | def _init_dev_i2c(self, vendor, product, serialname, interface): |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 434 | """Initalize Linux i2c-dev interface.""" |
| 435 | return i2cbus.I2CBus('/dev/i2c-%d' % interface['bus_num']) |
| 436 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 437 | def _init_ftdi_uart(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 438 | """Initialize ftdi uart inteface and open for use |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 439 | |
| 440 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 441 | interact with it will query control for the pty's pathname and connect |
| 442 | with there favorite console program. For example: |
| 443 | cu -l /dev/pts/22 |
| 444 | |
| 445 | Args: |
| 446 | interface: interface number of FTDI device to use |
| 447 | |
| 448 | Returns: |
| 449 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 450 | |
| 451 | Raises: |
| 452 | ServodError: If init fails |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 453 | """ |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 454 | fobj = ftdiuart.Fuart(vendor, product, interface, serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 455 | try: |
| 456 | fobj.run() |
| 457 | except ftdiuart.FuartError as e: |
| 458 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 459 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 460 | self._logger.info('%s' % fobj.get_pty()) |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 461 | return fobj |
| 462 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 463 | # TODO (sbasi) crbug.com/187492 - Implement bbuart. |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 464 | def _init_bb_uart(self, vendor, product, serialname, interface): |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 465 | """Initalize beaglebone uart interface.""" |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 466 | logging.debug('UART INTERFACE: %s', interface) |
| 467 | return bbuart.BBuart(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 468 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 469 | def _init_ftdi_gpiouart(self, vendor, product, serialname, interface): |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 470 | """Initialize special gpio + uart interface and open for use |
| 471 | |
| 472 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 473 | interact with it will query control for the pty's pathname and connect |
| 474 | with there favorite console program. For example: |
| 475 | cu -l /dev/pts/22 |
| 476 | |
| 477 | Args: |
| 478 | interface: interface number of FTDI device to use |
| 479 | |
| 480 | Returns: |
| 481 | Instance objects of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 482 | |
| 483 | Raises: |
| 484 | ServodError: If init fails |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 485 | """ |
Kevin Cheng | ce7dafd | 2016-08-02 11:11:38 -0700 | [diff] [blame] | 486 | fgpio = self._init_ftdi_gpio(vendor, product, serialname, interface) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 487 | fuart = ftdiuart.Fuart(vendor, product, interface, serialname, fgpio._fc) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 488 | try: |
| 489 | fuart.run() |
| 490 | except ftdiuart.FuartError as e: |
| 491 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 492 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 493 | self._logger.info('uart pty: %s' % fuart.get_pty()) |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 494 | return fgpio, fuart |
| 495 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 496 | def _init_ec3po_uart(self, vendor, product, serialname, interface): |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 497 | """Initialize EC-3PO console interpreter interface. |
| 498 | |
| 499 | Args: |
| 500 | interface: A dictionary representing the interface. |
| 501 | |
| 502 | Returns: |
| 503 | An EC3PO object representing the EC-3PO interface or None if there's no |
| 504 | interface for the USB PD UART. |
| 505 | """ |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 506 | raw_uart_name = interface['raw_pty'] |
Nick Sanders | 116ed9e | 2018-03-09 19:05:16 -0800 | [diff] [blame] | 507 | raw_uart_source = interface['source'] |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 508 | if self._syscfg.is_control(raw_uart_name): |
Nick Sanders | 97bc446 | 2016-01-04 15:37:31 -0800 | [diff] [blame] | 509 | raw_ec_uart = self.get(raw_uart_name) |
Nick Sanders | 116ed9e | 2018-03-09 19:05:16 -0800 | [diff] [blame] | 510 | return ec3po_interface.EC3PO(raw_ec_uart, raw_uart_source) |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 511 | else: |
Wai-Hong Tam | 6c0fa59 | 2017-04-21 12:41:33 -0700 | [diff] [blame] | 512 | # The overlay doesn't have the raw PTY defined, therefore we can skip |
| 513 | # initializing this interface since no control relies on it. |
| 514 | self._logger.debug( |
| 515 | 'Skip initializing EC3PO for %s, no control specified.', |
| 516 | raw_uart_name) |
| 517 | return None |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 518 | |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 519 | def _camel_case(self, string): |
| 520 | output = '' |
| 521 | for s in string.split('_'): |
| 522 | if output: |
| 523 | output += s.capitalize() |
| 524 | else: |
| 525 | output = s |
| 526 | return output |
| 527 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 528 | def clear_cached_drv(self): |
| 529 | """Clear the cached drivers. |
| 530 | |
| 531 | The drivers are cached in the Dict _drv_dict when a control is got or set. |
| 532 | When the servo interfaces are relocated, the cached values may become wrong. |
| 533 | Should call this method to clear the cached values. |
| 534 | """ |
| 535 | self._drv_dict = {} |
| 536 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 537 | def _get_param_drv(self, control_name, is_get=True): |
| 538 | """Get access to driver for a given control. |
| 539 | |
| 540 | Note, some controls have different parameter dictionaries for 'getting' the |
| 541 | control's value versus 'setting' it. Boolean is_get distinguishes which is |
| 542 | being requested. |
| 543 | |
| 544 | Args: |
| 545 | control_name: string name of control |
| 546 | is_get: boolean to determine |
| 547 | |
| 548 | Returns: |
| 549 | tuple (param, drv) where: |
| 550 | param: param dictionary for control |
| 551 | drv: instance object of driver for particular control |
| 552 | |
| 553 | Raises: |
| 554 | ServodError: Error occurred while examining params dict |
| 555 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 556 | self._logger.debug('') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 557 | # if already setup just return tuple from driver dict |
| 558 | if control_name in self._drv_dict: |
| 559 | if is_get and ('get' in self._drv_dict[control_name]): |
| 560 | return self._drv_dict[control_name]['get'] |
| 561 | if not is_get and ('set' in self._drv_dict[control_name]): |
| 562 | return self._drv_dict[control_name]['set'] |
| 563 | |
| 564 | params = self._syscfg.lookup_control_params(control_name, is_get) |
| 565 | if 'drv' not in params: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 566 | self._logger.error('Unable to determine driver for %s' % control_name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 567 | raise ServodError("'drv' key not found in params dict") |
| 568 | if 'interface' not in params: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 569 | self._logger.error('Unable to determine interface for %s' % control_name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 570 | raise ServodError("'interface' key not found in params dict") |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 571 | |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 572 | # Find the candidate servos. Using servo_v4 with a servo_micro connected as |
| 573 | # an example, the following shows the priority for selecting the interface. |
| 574 | # |
| 575 | # 1. The full name. (e.g. - 'servo_v4_with_servo_micro_interface') |
| 576 | # 2. servo_micro_interface |
| 577 | # 3. servo_v4_interface |
| 578 | # 4. Fallback to the default, interface. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 579 | candidates = [self._version] |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 580 | candidates.extend(reversed(self._version.split('_with_'))) |
| 581 | |
| 582 | interface_id = 'unknown' |
| 583 | for c in candidates: |
| 584 | interface_name = '%s_interface' % c |
| 585 | if interface_name in params: |
| 586 | interface_id = params[interface_name] |
| 587 | self._logger.debug('Using %s parameter.' % interface_name) |
| 588 | break |
| 589 | |
| 590 | # Use the default interface value if we couldn't find a more specific |
| 591 | # interface. |
| 592 | if interface_id == 'unknown': |
| 593 | interface_id = params['interface'] |
| 594 | self._logger.debug('Using default interface parameter.') |
| 595 | |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 596 | if interface_id == 'servo': |
| 597 | interface = self |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 598 | else: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 599 | index = int(interface_id) |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 600 | interface = self._interface_list[index] |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 601 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 602 | drv_name = params['drv'] |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 603 | drv_module = getattr(servo_drv, drv_name) |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 604 | drv_class = getattr(drv_module, self._camel_case(drv_name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 605 | drv = drv_class(interface, params) |
| 606 | if control_name not in self._drv_dict: |
| 607 | self._drv_dict[control_name] = {} |
| 608 | if is_get: |
| 609 | self._drv_dict[control_name]['get'] = (params, drv) |
| 610 | else: |
| 611 | self._drv_dict[control_name]['set'] = (params, drv) |
| 612 | return (params, drv) |
| 613 | |
| 614 | def doc_all(self): |
| 615 | """Return all documenation for controls. |
| 616 | |
| 617 | Returns: |
| 618 | string of <doc> text in config file (xml) and the params dictionary for |
| 619 | all controls. |
| 620 | |
| 621 | For example: |
| 622 | warm_reset :: Reset the device warmly |
| 623 | ------------------------> {'interface': '1', 'map': 'onoff_i', ... } |
| 624 | """ |
| 625 | return self._syscfg.display_config() |
| 626 | |
| 627 | def doc(self, name): |
| 628 | """Retreive doc string in system config file for given control name. |
| 629 | |
| 630 | Args: |
| 631 | name: name string of control to get doc string |
| 632 | |
| 633 | Returns: |
| 634 | doc string of name |
| 635 | |
| 636 | Raises: |
| 637 | NameError: if fails to locate control |
| 638 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 639 | self._logger.debug('name(%s)' % (name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 640 | if self._syscfg.is_control(name): |
| 641 | return self._syscfg.get_control_docstring(name) |
| 642 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 643 | raise NameError('No control %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 644 | |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 645 | def _switch_usbkey(self, mux_direction): |
| 646 | """Connect USB flash stick to either servo or DUT. |
| 647 | |
| 648 | This function switches 'usb_mux_sel1' to provide electrical |
| 649 | connection between the USB port J3 and either servo or DUT side. |
| 650 | |
| 651 | Switching the usb mux is accompanied by powercycling |
| 652 | of the USB stick, because it sometimes gets wedged if the mux |
| 653 | is switched while the stick power is on. |
| 654 | |
| 655 | Args: |
| 656 | mux_direction: "servo_sees_usbkey" or "dut_sees_usbkey". |
| 657 | """ |
| 658 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_OFF) |
| 659 | time.sleep(self._USB_POWEROFF_DELAY) |
| 660 | self.set(self._USB_J3, mux_direction) |
| 661 | time.sleep(self._USB_POWEROFF_DELAY) |
| 662 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_ON) |
| 663 | if mux_direction == self._USB_J3_TO_SERVO: |
| 664 | time.sleep(self._USB_DETECTION_DELAY) |
| 665 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 666 | def _get_usb_port_set(self): |
| 667 | """Gets a set of USB disks currently connected to the system |
| 668 | |
| 669 | Returns: |
| 670 | A set of USB disk paths. |
| 671 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 672 | usb_set = fnmatch.filter(os.listdir('/dev/'), 'sd[a-z]') |
| 673 | return set(['/dev/' + dev for dev in usb_set]) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 674 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 675 | @contextlib.contextmanager |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 676 | def _block_other_servod(self, timeout): |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 677 | """Block other servod processes by locking a file. |
| 678 | |
| 679 | To enable multiple servods processes to safely probe_host_usb_dev, we use |
| 680 | a given lock file to signal other servod processes that we're probing |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 681 | for a usb device. This will be a context manager that will return |
| 682 | if the block was successful or not. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 683 | |
| 684 | If the lock file exists, we open it and try to lock it. |
| 685 | - If another servod processes has locked it already, we'll sleep a random |
| 686 | amount of time and try again, we'll keep doing that until |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 687 | timeout amount of time has passed. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 688 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 689 | - If we're able to lock the file, we'll yield that the block was successful |
| 690 | and upon return, unlock the file and exit out. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 691 | |
| 692 | This blocking behavior is only enabled if the lock file exists, if it |
| 693 | doesn't, then we pretend the block was successful. |
| 694 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 695 | Args: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 696 | timeout: Max waiting time for the block to succeed; 0 to wait forever. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 697 | """ |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 698 | if not os.path.exists(self._USB_LOCK_FILE): |
| 699 | # No lock file so we'll pretend the block was a success. |
| 700 | yield True |
| 701 | else: |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 702 | start_time = datetime.datetime.now() |
| 703 | while True: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 704 | with open(self._USB_LOCK_FILE, 'w') as lock_file: |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 705 | try: |
| 706 | fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 707 | yield True |
| 708 | fcntl.flock(lock_file, fcntl.LOCK_UN) |
| 709 | break |
| 710 | except IOError: |
| 711 | current_time = datetime.datetime.now() |
| 712 | current_wait_time = (current_time - start_time).total_seconds() |
| 713 | if timeout and current_wait_time > timeout: |
| 714 | yield False |
| 715 | break |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 716 | # Sleep random amount. |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 717 | sleep_time = random.random() |
| 718 | logging.debug('sleep %.04fs and try obtaining a lock...', sleep_time) |
| 719 | time.sleep(sleep_time) |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 720 | |
Kevin Cheng | 8fcf06c | 2016-10-12 08:02:44 -0700 | [diff] [blame] | 721 | def safe_switch_usbkey_power(self, power_state, timeout=0): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 722 | """Toggle the usb power safely. |
| 723 | |
| 724 | We'll make sure we're the only servod process toggling the usbkey power. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 725 | |
| 726 | Args: |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 727 | power_state: The setting to set for the usbkey power. |
Kevin Cheng | 8fcf06c | 2016-10-12 08:02:44 -0700 | [diff] [blame] | 728 | timeout: Timeout to wait for blocking other servod processes, default is |
| 729 | no timeout. |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 730 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 731 | Returns: |
| 732 | An empty string to appease the xmlrpc gods. |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 733 | |
| 734 | Raises: |
| 735 | ServodError if failed to obtain a lock. |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 736 | """ |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 737 | with self._block_other_servod(timeout=timeout) as block_success: |
| 738 | if not block_success: |
| 739 | raise ServodError('Timed out obtaining a lock to block other servod') |
| 740 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 741 | if power_state != self.get(self._USB_J3_PWR): |
| 742 | self.set(self._USB_J3_PWR, power_state) |
| 743 | return '' |
| 744 | |
Kevin Cheng | 8fcf06c | 2016-10-12 08:02:44 -0700 | [diff] [blame] | 745 | def safe_switch_usbkey(self, mux_direction, timeout=0): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 746 | """Toggle the usb direction safely. |
| 747 | |
| 748 | We'll make sure we're the only servod process toggling the usbkey direction. |
| 749 | |
| 750 | Args: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 751 | mux_direction: "servo_sees_usbkey" or "dut_sees_usbkey". |
Kevin Cheng | 8fcf06c | 2016-10-12 08:02:44 -0700 | [diff] [blame] | 752 | timeout: Timeout to wait for blocking other servod processes, default is |
| 753 | no timeout. |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 754 | |
| 755 | Returns: |
| 756 | An empty string to appease the xmlrpc gods. |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 757 | |
| 758 | Raises: |
| 759 | ServodError if failed to obtain a lock. |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 760 | """ |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 761 | with self._block_other_servod(timeout=timeout) as block_success: |
| 762 | if not block_success: |
| 763 | raise ServodError('Timed out obtaining a lock to block other servod') |
| 764 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 765 | self._switch_usbkey(mux_direction) |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 766 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 767 | return '' |
| 768 | |
| 769 | def probe_host_usb_dev(self, timeout=_MAX_USB_LOCK_WAIT): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 770 | """Probe the USB disk device plugged in the servo from the host side. |
| 771 | |
| 772 | Method can fail by: |
| 773 | 1) Having multiple servos connected and returning incorrect /dev/sdX of |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 774 | another servo unless _USB_LOCK_FILE exists on the servo host. If that |
| 775 | file exists, then it is safe to probe for usb devices among multiple |
| 776 | servod instances. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 777 | 2) Finding multiple /dev/sdX and returning None. |
| 778 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 779 | Args: |
| 780 | timeout: Timeout to wait for blocking other servod processes. |
| 781 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 782 | Returns: |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 783 | USB disk path if one and only one USB disk path is found, otherwise an |
| 784 | empty string. |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 785 | |
| 786 | Raises: |
| 787 | ServodError if failed to obtain a lock. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 788 | """ |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 789 | with self._block_other_servod(timeout=timeout) as block_success: |
| 790 | if not block_success: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 791 | raise ServodError('Timed out obtaining a lock to block other servod') |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 792 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 793 | original_value = self.get(self._USB_J3) |
| 794 | original_usb_power = self.get(self._USB_J3_PWR) |
| 795 | # Make the host unable to see the USB disk. |
| 796 | if (original_usb_power == self._USB_J3_PWR_ON and |
| 797 | original_value != self._USB_J3_TO_DUT): |
| 798 | self._switch_usbkey(self._USB_J3_TO_DUT) |
| 799 | no_usb_set = self._get_usb_port_set() |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 800 | logging.debug('Device set when USB disk unplugged: %r', no_usb_set) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 801 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 802 | # Make the host able to see the USB disk. |
| 803 | self._switch_usbkey(self._USB_J3_TO_SERVO) |
| 804 | has_usb_set = self._get_usb_port_set() |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 805 | logging.debug('Device set when USB disk plugged: %r', has_usb_set) |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 806 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 807 | # Back to its original value. |
| 808 | if original_value != self._USB_J3_TO_SERVO: |
| 809 | self._switch_usbkey(original_value) |
| 810 | if original_usb_power != self._USB_J3_PWR_ON: |
| 811 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_OFF) |
| 812 | time.sleep(self._USB_POWEROFF_DELAY) |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 813 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 814 | # Subtract the two sets to find the usb device. |
| 815 | diff_set = has_usb_set - no_usb_set |
| 816 | if len(diff_set) == 1: |
| 817 | return diff_set.pop() |
| 818 | else: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 819 | logging.warn("Can't find the USB device. Diff: %r", diff_set) |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 820 | return '' |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 821 | |
Kevin Cheng | 8583133 | 2016-10-13 13:14:44 -0700 | [diff] [blame] | 822 | def download_image_to_usb(self, image_path, probe_timeout=_MAX_USB_LOCK_WAIT): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 823 | """Download image and save to the USB device found by probe_host_usb_dev. |
| 824 | If the image_path is a URL, it will download this url to the USB path; |
| 825 | otherwise it will simply copy the image_path's contents to the USB path. |
| 826 | |
| 827 | Args: |
| 828 | image_path: path or url to the recovery image. |
Kevin Cheng | 8583133 | 2016-10-13 13:14:44 -0700 | [diff] [blame] | 829 | probe_timeout: timeout for the probe to take. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 830 | |
| 831 | Returns: |
| 832 | True|False: True if process completed successfully, False if error |
| 833 | occurred. |
| 834 | Can't return None because XMLRPC doesn't allow it. PTAL at tbroch's |
| 835 | comment at the end of set(). |
| 836 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 837 | self._logger.debug('image_path(%s)' % image_path) |
| 838 | self._logger.debug('Detecting USB stick device...') |
Kevin Cheng | 8583133 | 2016-10-13 13:14:44 -0700 | [diff] [blame] | 839 | usb_dev = self.probe_host_usb_dev(timeout=probe_timeout) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 840 | if not usb_dev: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 841 | self._logger.error('No usb device connected to servo') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 842 | return False |
| 843 | |
Kevin Cheng | 9071ed9 | 2016-06-21 14:37:54 -0700 | [diff] [blame] | 844 | # Let's check if we downloaded this last time and if so assume the image is |
| 845 | # still on the usb device and return True. |
| 846 | if self._image_path == image_path: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 847 | self._logger.debug('Image already on USB device, skipping transfer') |
Kevin Cheng | 9071ed9 | 2016-06-21 14:37:54 -0700 | [diff] [blame] | 848 | return True |
| 849 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 850 | try: |
| 851 | if image_path.startswith(self._HTTP_PREFIX): |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 852 | self._logger.debug('Image path is a URL, downloading image') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 853 | urllib.urlretrieve(image_path, usb_dev) |
| 854 | else: |
| 855 | shutil.copyfile(image_path, usb_dev) |
| 856 | except IOError as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 857 | self._logger.error('Failed to transfer image to USB device: %s ( %s ) ', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 858 | e.strerror, e.errno) |
| 859 | return False |
| 860 | except urllib.ContentTooShortError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 861 | self._logger.error('Failed to download URL: %s to USB device: %s', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 862 | image_path, usb_dev) |
| 863 | return False |
| 864 | except BaseException as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 865 | self._logger.error('Unexpected exception downloading %s to %s: %s', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 866 | image_path, usb_dev, str(e)) |
| 867 | return False |
J. Richard Barnette | e4125af | 2013-02-26 18:31:56 -0800 | [diff] [blame] | 868 | finally: |
| 869 | # We just plastered the partition table for a block device. |
| 870 | # Pass or fail, we mustn't go without telling the kernel about |
| 871 | # the change, or it will punish us with sporadic, hard-to-debug |
| 872 | # failures. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 873 | subprocess.call(['sync']) |
| 874 | subprocess.call(['blockdev', '--rereadpt', usb_dev]) |
Kevin Cheng | 9071ed9 | 2016-06-21 14:37:54 -0700 | [diff] [blame] | 875 | self._image_path = image_path |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 876 | return True |
| 877 | |
| 878 | def make_image_noninteractive(self): |
| 879 | """Makes the recovery image noninteractive. |
| 880 | |
| 881 | A noninteractive image will reboot automatically after installation |
| 882 | instead of waiting for the USB device to be removed to initiate a system |
| 883 | reboot. |
| 884 | |
| 885 | Mounts partition 1 of the image stored on usb_dev and creates a file |
| 886 | called "non_interactive" so that the image will become noninteractive. |
| 887 | |
| 888 | Returns: |
| 889 | True|False: True if process completed successfully, False if error |
| 890 | occurred. |
| 891 | """ |
| 892 | result = True |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 893 | usb_dev = self.probe_host_usb_dev() |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 894 | if not usb_dev: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 895 | self._logger.error('No usb device connected to servo') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 896 | return False |
| 897 | # Create TempDirectory |
| 898 | tmpdir = tempfile.mkdtemp() |
| 899 | if tmpdir: |
| 900 | # Mount drive to tmpdir. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 901 | partition_1 = '%s1' % usb_dev |
| 902 | rc = subprocess.call(['mount', partition_1, tmpdir]) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 903 | if rc == 0: |
| 904 | # Create file 'non_interactive' |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 905 | non_interactive_file = os.path.join(tmpdir, 'non_interactive') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 906 | try: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 907 | open(non_interactive_file, 'w').close() |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 908 | except IOError as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 909 | self._logger.error('Failed to create file %s : %s ( %d )', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 910 | non_interactive_file, e.strerror, e.errno) |
| 911 | result = False |
| 912 | except BaseException as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 913 | self._logger.error('Unexpected Exception creating file %s : %s', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 914 | non_interactive_file, str(e)) |
| 915 | result = False |
| 916 | # Unmount drive regardless if file creation worked or not. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 917 | rc = subprocess.call(['umount', partition_1]) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 918 | if rc != 0: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 919 | self._logger.error('Failed to unmount USB Device') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 920 | result = False |
| 921 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 922 | self._logger.error('Failed to mount USB Device') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 923 | result = False |
| 924 | |
| 925 | # Delete tmpdir. May throw exception if 'umount' failed. |
| 926 | try: |
| 927 | os.rmdir(tmpdir) |
| 928 | except OSError as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 929 | self._logger.error('Failed to remove temp directory %s : %s', tmpdir, |
| 930 | str(e)) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 931 | return False |
| 932 | except BaseException as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 933 | self._logger.error('Unexpected Exception removing tempdir %s : %s', |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 934 | tmpdir, str(e)) |
| 935 | return False |
| 936 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 937 | self._logger.error('Failed to create temp directory.') |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 938 | return False |
| 939 | return result |
| 940 | |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 941 | def set_get_all(self, cmds): |
| 942 | """Set &| get one or more control values. |
| 943 | |
| 944 | Args: |
| 945 | cmds: list of control[:value] to get or set. |
| 946 | |
| 947 | Returns: |
| 948 | rv: list of responses from calling get or set methods. |
| 949 | """ |
| 950 | rv = [] |
| 951 | for cmd in cmds: |
| 952 | if ':' in cmd: |
| 953 | (control, value) = cmd.split(':') |
| 954 | rv.append(self.set(control, value)) |
| 955 | else: |
| 956 | rv.append(self.get(cmd)) |
| 957 | return rv |
| 958 | |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 959 | def get_serial_number(self, name): |
| 960 | """Returns the desired serial number from the serialnames dict. |
| 961 | |
| 962 | Args: |
| 963 | name: A string which is the key into the _serialnames dictionary. |
| 964 | |
| 965 | Returns: |
| 966 | A string containing the serial number or "unknown". |
| 967 | """ |
| 968 | if not name: |
| 969 | name = 'main' |
| 970 | |
| 971 | try: |
| 972 | return self._serialnames[name] |
| 973 | except KeyError: |
| 974 | self._logger.debug("'%s_serialname' not found!", name) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 975 | return 'unknown' |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 976 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 977 | def get(self, name): |
| 978 | """Get control value. |
| 979 | |
| 980 | Args: |
| 981 | name: name string of control |
| 982 | |
| 983 | Returns: |
| 984 | Response from calling drv get method. Value is reformatted based on |
| 985 | control's dictionary parameters |
| 986 | |
| 987 | Raises: |
| 988 | HwDriverError: Error occurred while using drv |
| 989 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 990 | self._logger.debug('name(%s)' % (name)) |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 991 | # This route is to retrieve serialnames on servo v4, which |
| 992 | # connects to multiple servo-micros or CCD, like the controls, |
| 993 | # 'ccd_serialname', 'servo_micro_for_soraka_serialname', etc. |
| 994 | # TODO(aaboagye): Refactor it. |
| 995 | if 'serialname' in name: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 996 | return self.get_serial_number(name.split('serialname')[0].strip('_')) |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 997 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 998 | (param, drv) = self._get_param_drv(name) |
| 999 | try: |
| 1000 | val = drv.get() |
| 1001 | rd_val = self._syscfg.reformat_val(param, val) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1002 | self._logger.debug('%s = %s' % (name, rd_val)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1003 | return rd_val |
Todd Broch | fbc499d | 2011-06-16 16:09:58 -0700 | [diff] [blame] | 1004 | except AttributeError, error: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1005 | self._logger.error('Getting %s: %s' % (name, error)) |
Todd Broch | fbc499d | 2011-06-16 16:09:58 -0700 | [diff] [blame] | 1006 | raise |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 1007 | except HwDriverError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1008 | self._logger.error('Getting %s' % (name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1009 | raise |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 1010 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1011 | def get_all(self, verbose): |
| 1012 | """Get all controls values. |
| 1013 | |
| 1014 | Args: |
| 1015 | verbose: Boolean on whether to return doc info as well |
| 1016 | |
| 1017 | Returns: |
| 1018 | string creating from trying to get all values of all controls. In case of |
| 1019 | error attempting access to control, response is 'ERR'. |
| 1020 | """ |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 1021 | rsp = [] |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1022 | for name in self._syscfg.syscfg_dict['control']: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1023 | self._logger.debug('name = %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1024 | try: |
| 1025 | value = self.get(name) |
| 1026 | except Exception: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1027 | value = 'ERR' |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1028 | pass |
| 1029 | if verbose: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1030 | rsp.append('GET %s = %s :: %s' % (name, value, self.doc(name))) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1031 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1032 | rsp.append('%s:%s' % (name, value)) |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 1033 | return '\n'.join(sorted(rsp)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1034 | |
| 1035 | def set(self, name, wr_val_str): |
| 1036 | """Set control. |
| 1037 | |
| 1038 | Args: |
| 1039 | name: name string of control |
| 1040 | wr_val_str: value string to write. Can be integer, float or a |
| 1041 | alpha-numerical that is mapped to a integer or float. |
| 1042 | |
| 1043 | Raises: |
| 1044 | HwDriverError: Error occurred while using driver |
| 1045 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1046 | self._logger.debug('name(%s) wr_val(%s)' % (name, wr_val_str)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1047 | (params, drv) = self._get_param_drv(name, False) |
| 1048 | wr_val = self._syscfg.resolve_val(params, wr_val_str) |
| 1049 | try: |
| 1050 | drv.set(wr_val) |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 1051 | except HwDriverError: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1052 | self._logger.error('Setting %s -> %s' % (name, wr_val_str)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1053 | raise |
| 1054 | # TODO(tbroch) Figure out why despite allow_none=True for both xmlrpc server |
| 1055 | # & client I still have to return something to appease the |
| 1056 | # marshall/unmarshall |
| 1057 | return True |
| 1058 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 1059 | def hwinit(self, verbose=False): |
| 1060 | """Initialize all controls. |
| 1061 | |
| 1062 | These values are part of the system config XML files of the form |
| 1063 | init=<value>. This command should be used by clients wishing to return the |
| 1064 | servo and DUT its connected to a known good/safe state. |
| 1065 | |
Vadim Bendebury | bb51dd4 | 2013-01-31 13:47:46 -0800 | [diff] [blame] | 1066 | Note that initialization errors are ignored (as in some cases they could |
| 1067 | be caused by DUT firmware deficiencies). This might need to be fine tuned |
| 1068 | later. |
| 1069 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 1070 | Args: |
| 1071 | verbose: boolean, if True prints info about control initialized. |
| 1072 | Otherwise prints nothing. |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 1073 | |
| 1074 | Returns: |
| 1075 | This function is called across RPC and as such is expected to return |
| 1076 | something unless transferring 'none' across is allowed. Hence adding a |
| 1077 | dummy return value to make things simpler. |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 1078 | """ |
Todd Broch | d9acf0a | 2012-12-05 13:43:06 -0800 | [diff] [blame] | 1079 | for control_name, value in self._syscfg.hwinit: |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 1080 | try: |
John Carey | 6fe2bbf | 2015-08-31 16:13:03 -0700 | [diff] [blame] | 1081 | # Workaround for bug chrome-os-partner:42349. Without this check, the |
| 1082 | # gpio will briefly pulse low if we set it from high to high. |
| 1083 | if self.get(control_name) != value: |
Aseda Aboagye | a849d46 | 2016-05-04 17:08:16 -0700 | [diff] [blame] | 1084 | self.set(control_name, value) |
| 1085 | if verbose: |
| 1086 | self._logger.info('Initialized %s to %s', control_name, value) |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 1087 | except Exception as e: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1088 | self._logger.error('Problem initializing %s -> %s :: %s', control_name, |
| 1089 | value, str(e)) |
Nick Sanders | bc83628 | 2015-12-08 21:19:23 -0800 | [diff] [blame] | 1090 | |
| 1091 | # Init keyboard after all the intefaces are up. |
| 1092 | self._keyboard = self._init_keyboard_handler(self, self._board) |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 1093 | return True |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 1094 | |
Dino Li | c89d8c8 | 2018-01-11 09:56:47 +0800 | [diff] [blame] | 1095 | def ftdii2c(self, args): |
| 1096 | """Calling a method of Fi2c.""" |
| 1097 | index = self._interfaces.index('ftdi_i2c') |
| 1098 | if index: |
| 1099 | obj = self._interface_list[index] |
| 1100 | func = getattr(obj, '%s' % args[0]) |
| 1101 | func() |
| 1102 | return True |
| 1103 | |
| 1104 | return False |
| 1105 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1106 | def echo(self, echo): |
| 1107 | """Dummy echo function for testing/examples. |
| 1108 | |
| 1109 | Args: |
| 1110 | echo: string to echo back to client |
| 1111 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1112 | self._logger.debug('echo(%s)' % (echo)) |
| 1113 | return 'ECH0ING: %s' % (echo) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1114 | |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 1115 | def get_board(self): |
| 1116 | """Return the board specified at startup, if any.""" |
| 1117 | return self._board |
| 1118 | |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 1119 | def get_base_board(self): |
| 1120 | """Returns the board name of the base if present. |
| 1121 | |
| 1122 | Returns: |
| 1123 | A string of the board name, or '' if not present. |
| 1124 | """ |
| 1125 | # The value is set in servo_postinit. |
| 1126 | return self._base_board |
| 1127 | |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 1128 | def get_version(self): |
| 1129 | """Get servo board version.""" |
| 1130 | return self._version |
| 1131 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 1132 | def power_long_press(self): |
| 1133 | """Simulate a long power button press.""" |
| 1134 | # After a long power press, the EC may ignore the next power |
| 1135 | # button press (at least on Alex). To guarantee that this |
| 1136 | # won't happen, we need to allow the EC one second to |
| 1137 | # collect itself. |
| 1138 | self._keyboard.power_long_press() |
| 1139 | return True |
| 1140 | |
| 1141 | def power_normal_press(self): |
| 1142 | """Simulate a normal power button press.""" |
| 1143 | self._keyboard.power_normal_press() |
| 1144 | return True |
| 1145 | |
| 1146 | def power_short_press(self): |
| 1147 | """Simulate a short power button press.""" |
| 1148 | self._keyboard.power_short_press() |
| 1149 | return True |
| 1150 | |
| 1151 | def power_key(self, secs=''): |
| 1152 | """Simulate a power button press. |
| 1153 | |
| 1154 | Args: |
| 1155 | secs: Time in seconds to simulate the keypress. |
| 1156 | """ |
| 1157 | self._keyboard.power_key(secs) |
| 1158 | return True |
| 1159 | |
| 1160 | def ctrl_d(self, press_secs=''): |
| 1161 | """Simulate Ctrl-d simultaneous button presses.""" |
| 1162 | self._keyboard.ctrl_d(press_secs) |
| 1163 | return True |
| 1164 | |
Victor Dodon | e539cea | 2016-03-29 18:50:17 -0700 | [diff] [blame] | 1165 | def ctrl_u(self, press_secs=''): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 1166 | """Simulate Ctrl-u simultaneous button presses.""" |
Victor Dodon | e539cea | 2016-03-29 18:50:17 -0700 | [diff] [blame] | 1167 | self._keyboard.ctrl_u(press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 1168 | return True |
| 1169 | |
| 1170 | def ctrl_enter(self, press_secs=''): |
| 1171 | """Simulate Ctrl-enter simultaneous button presses.""" |
| 1172 | self._keyboard.ctrl_enter(press_secs) |
| 1173 | return True |
| 1174 | |
| 1175 | def d_key(self, press_secs=''): |
| 1176 | """Simulate Enter key button press.""" |
| 1177 | self._keyboard.d_key(press_secs) |
| 1178 | return True |
| 1179 | |
| 1180 | def ctrl_key(self, press_secs=''): |
| 1181 | """Simulate Enter key button press.""" |
| 1182 | self._keyboard.ctrl_key(press_secs) |
| 1183 | return True |
| 1184 | |
| 1185 | def enter_key(self, press_secs=''): |
| 1186 | """Simulate Enter key button press.""" |
| 1187 | self._keyboard.enter_key(press_secs) |
| 1188 | return True |
| 1189 | |
| 1190 | def refresh_key(self, press_secs=''): |
| 1191 | """Simulate Refresh key (F3) button press.""" |
| 1192 | self._keyboard.refresh_key(press_secs) |
| 1193 | return True |
| 1194 | |
| 1195 | def ctrl_refresh_key(self, press_secs=''): |
| 1196 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 1197 | |
| 1198 | This key combination is an alternative of Space key. |
| 1199 | """ |
| 1200 | self._keyboard.ctrl_refresh_key(press_secs) |
| 1201 | return True |
| 1202 | |
| 1203 | def imaginary_key(self, press_secs=''): |
| 1204 | """Simulate imaginary key button press. |
| 1205 | |
| 1206 | Maps to a key that doesn't physically exist. |
| 1207 | """ |
| 1208 | self._keyboard.imaginary_key(press_secs) |
| 1209 | return True |
| 1210 | |
Vincent Palatin | 3acbbe5 | 2016-07-19 17:40:12 +0200 | [diff] [blame] | 1211 | def sysrq_x(self, press_secs=''): |
| 1212 | """Simulate Alt VolumeUp X simultaneous press. |
| 1213 | |
| 1214 | This key combination is the kernel system request (sysrq) x. |
| 1215 | """ |
| 1216 | self._keyboard.sysrq_x(press_secs) |
| 1217 | return True |
| 1218 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 1219 | def get_servo_serials(self): |
| 1220 | """Return all the serials associated with this process.""" |
| 1221 | return self._serialnames |
| 1222 | |
| 1223 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1224 | def test(): |
| 1225 | """Integration testing. |
| 1226 | |
| 1227 | TODO(tbroch) Enhance integration test and add unittest (see mox) |
| 1228 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1229 | logging.basicConfig( |
| 1230 | level=logging.DEBUG, |
| 1231 | format='%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1232 | # configure server & listen |
| 1233 | servod_obj = Servod(1) |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 1234 | # 5 == number of interfaces on a FT4232H device |
| 1235 | for i in xrange(1, 5): |
| 1236 | if i == 2: |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1237 | # its an i2c interface ... see __init__ for details and TODO to make |
| 1238 | # this configureable |
| 1239 | servod_obj._interface_list[i].wr_rd(0x21, [0], 1) |
| 1240 | else: |
| 1241 | # its a gpio interface |
| 1242 | servod_obj._interface_list[i].wr_rd(0) |
| 1243 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1244 | server = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 9999), |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1245 | allow_none=True) |
| 1246 | server.register_introspection_functions() |
| 1247 | server.register_multicall_functions() |
| 1248 | server.register_instance(servod_obj) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1249 | logging.info('Listening on localhost port 9999') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1250 | server.serve_forever() |
| 1251 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1252 | |
| 1253 | if __name__ == '__main__': |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1254 | test() |
| 1255 | |
| 1256 | # simple client transaction would look like |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 1257 | """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) |
| 1258 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 1259 | """ |