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