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.""" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 5 | import fnmatch |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 6 | import imp |
| 7 | import logging |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 8 | import os |
| 9 | import shutil |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 10 | import SimpleXMLRPCServer |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 11 | import subprocess |
| 12 | import tempfile |
Todd Broch | 7a91c25 | 2012-02-03 12:37:45 -0800 | [diff] [blame] | 13 | import time |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 14 | import urllib |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 15 | |
| 16 | # TODO(tbroch) deprecate use of relative imports |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 17 | from drv.hw_driver import HwDriverError |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 18 | import bbadc |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 19 | import bbi2c |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 20 | import bbgpio |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 21 | import bbuart |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 22 | import ftdigpio |
| 23 | import ftdii2c |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 24 | import ftdi_common |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 25 | import ftdiuart |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 26 | import i2cbus |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 27 | import keyboard_handlers |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 28 | import servo_interfaces |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 29 | |
| 30 | MAX_I2C_CLOCK_HZ = 100000 |
| 31 | |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 32 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 33 | class ServodError(Exception): |
| 34 | """Exception class for servod.""" |
| 35 | |
| 36 | class Servod(object): |
| 37 | """Main class for Servo debug/controller Daemon.""" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 38 | _USB_DETECTION_DELAY = 10 |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 39 | _USB_POWEROFF_DELAY = 2 |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 40 | _HTTP_PREFIX = "http://" |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 41 | _USB_J3 = "usb_mux_sel1" |
| 42 | _USB_J3_TO_SERVO = "servo_sees_usbkey" |
| 43 | _USB_J3_TO_DUT = "dut_sees_usbkey" |
| 44 | _USB_J3_PWR = "prtctl4_pwren" |
| 45 | _USB_J3_PWR_ON = "on" |
| 46 | _USB_J3_PWR_OFF = "off" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 47 | |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 48 | def __init__(self, config, vendor, product, serialname=None, |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 49 | interfaces=None, board="", version=None, usbkm232=None): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 50 | """Servod constructor. |
| 51 | |
| 52 | Args: |
| 53 | config: instance of SystemConfig containing all controls for |
| 54 | particular Servod invocation |
| 55 | vendor: usb vendor id of FTDI device |
| 56 | product: usb product id of FTDI device |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 57 | serialname: string of device serialname/number as defined in FTDI eeprom. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 58 | interfaces: list of strings of interface types the server will instantiate |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 59 | version: String. Servo board version. Examples: servo_v1, servo_v2, |
| 60 | servo_v2_r0, servo_v3 |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 61 | usbkm232: String. Optional. Path to USB-KM232 device which allow for |
| 62 | sending keyboard commands to DUTs that do not have built in |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 63 | keyboards. Used in FAFT tests. Use 'atmega' for on board AVR MCU. |
| 64 | e.g. '/dev/ttyUSB0' or 'atmega' |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 65 | |
| 66 | Raises: |
| 67 | ServodError: if unable to locate init method for particular interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 68 | """ |
| 69 | self._logger = logging.getLogger("Servod") |
| 70 | self._logger.debug("") |
| 71 | self._vendor = vendor |
| 72 | self._product = product |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 73 | self._serialname = serialname |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 74 | self._syscfg = config |
| 75 | # list of objects (Fi2c, Fgpio) to physical interfaces (gpio, i2c) that ftdi |
| 76 | # interfaces are mapped to |
| 77 | self._interface_list = [] |
| 78 | # Dict of Dict to map control name, function name to to tuple (params, drv) |
| 79 | # Ex) _drv_dict[name]['get'] = (params, drv) |
| 80 | self._drv_dict = {} |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 81 | self._board = board |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 82 | self._version = version |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 83 | self._usbkm232 = usbkm232 |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 84 | # Note, interface i is (i - 1) in list |
| 85 | if not interfaces: |
Todd Broch | b21d804 | 2014-05-15 12:54:54 -0700 | [diff] [blame] | 86 | try: |
| 87 | interfaces = servo_interfaces.INTERFACE_BOARDS[board][vendor][product] |
| 88 | except KeyError: |
| 89 | interfaces = servo_interfaces.INTERFACE_DEFAULTS[vendor][product] |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 90 | |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 91 | for i, interface in enumerate(interfaces): |
| 92 | is_ftdi_interface = False |
| 93 | if type(interface) is dict: |
| 94 | name = interface['name'] |
| 95 | elif type(interface) is str: |
| 96 | # Its FTDI related interface |
| 97 | name = interface |
| 98 | interface = (i % ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE) + 1 |
| 99 | is_ftdi_interface = True |
| 100 | else: |
| 101 | raise ServodError("Illegal interface type %s" % type(interface)) |
| 102 | |
Todd Broch | 8a77a99 | 2012-01-27 09:46:08 -0800 | [diff] [blame] | 103 | # servos with multiple FTDI are guaranteed to have contiguous USB PIDs |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 104 | if is_ftdi_interface and i and \ |
| 105 | ((i % ftdi_common.MAX_FTDI_INTERFACES_PER_DEVICE) == 0): |
Todd Broch | 8a77a99 | 2012-01-27 09:46:08 -0800 | [diff] [blame] | 106 | self._product += 1 |
| 107 | self._logger.info("Changing to next FTDI part @ pid = 0x%04x", |
| 108 | self._product) |
| 109 | |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 110 | self._logger.info("Initializing interface %d to %s", i + 1, name) |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 111 | try: |
| 112 | func = getattr(self, '_init_%s' % name) |
| 113 | except AttributeError: |
| 114 | raise ServodError("Unable to locate init for interface %s" % name) |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 115 | result = func(interface) |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 116 | if isinstance(result, tuple): |
| 117 | self._interface_list.extend(result) |
| 118 | else: |
| 119 | self._interface_list.append(result) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 120 | |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 121 | # Init keyboard after all the intefaces are up. |
| 122 | self._keyboard = self._init_keyboard_handler(self, self._board) |
| 123 | |
| 124 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 125 | def _init_keyboard_handler(self, servo, board=''): |
| 126 | """Initialize the correct keyboard handler for board. |
| 127 | |
| 128 | @param servo: servo object. |
| 129 | @param board: string, board name. |
| 130 | |
| 131 | """ |
| 132 | if board == 'parrot': |
| 133 | return keyboard_handlers.ParrotHandler(servo) |
| 134 | elif board == 'stout': |
| 135 | return keyboard_handlers.StoutHandler(servo) |
PeggyChuang | 4f07d87 | 2015-08-07 12:11:38 +0800 | [diff] [blame] | 136 | elif board in ('buddy', 'cranky', 'guado', 'jecht', 'mccloud', 'monroe', |
Tom Wai-Hong Tam | d64164c | 2015-04-29 07:59:45 +0800 | [diff] [blame] | 137 | 'ninja', 'nyan_kitty', 'panther', 'rikku', 'stumpy', |
Jason Simmons | e5cccd1 | 2015-08-25 16:05:25 -0700 | [diff] [blame] | 138 | 'sumo', 'tidus', 'tricky', 'veyron_mickey', 'veyron_rialto', |
| 139 | 'zako'): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 140 | if self._usbkm232 is None: |
Yusuf Mohsinally | 6684469 | 2014-05-22 10:37:52 -0700 | [diff] [blame] | 141 | logging.warn("No device path specified for usbkm232 handler. Returning " |
Tom Wai-Hong Tam | d64164c | 2015-04-29 07:59:45 +0800 | [diff] [blame] | 142 | "the MatrixKeyboardHandler, which is likely the wrong " |
| 143 | "keyboard handler for the board type specified.") |
| 144 | return keyboard_handlers.MatrixKeyboardHandler(servo) |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 145 | if self._usbkm232 == 'atmega': |
| 146 | # Use servo onboard keyboard emulator. |
| 147 | self._usbkm232 = self.get('atmega_pty') |
| 148 | self.set('atmega_baudrate', '9600') |
| 149 | self.set('atmega_bits', 'eight') |
| 150 | self.set('atmega_parity', 'none') |
| 151 | self.set('atmega_sbits', 'one') |
| 152 | self.set('usb_mux_sel4', 'on') |
| 153 | self.set('atmega_rst', 'off') |
| 154 | self._logger.info('USBKM232: %s', self._usbkm232) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 155 | return keyboard_handlers.USBkm232Handler(servo, self._usbkm232) |
| 156 | else: |
Tom Wai-Hong Tam | d64164c | 2015-04-29 07:59:45 +0800 | [diff] [blame] | 157 | # The following boards don't use Chrome EC. |
| 158 | if board in ('alex', 'butterfly', 'lumpy', 'zgb'): |
| 159 | return keyboard_handlers.MatrixKeyboardHandler(servo) |
| 160 | return keyboard_handlers.ChromeECHandler(servo) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 161 | |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 162 | def __del__(self): |
| 163 | """Servod deconstructor.""" |
| 164 | for interface in self._interface_list: |
| 165 | del(interface) |
| 166 | |
Todd Broch | b304849 | 2012-01-15 21:52:41 -0800 | [diff] [blame] | 167 | def _init_dummy(self, interface): |
| 168 | """Initialize dummy interface. |
| 169 | |
| 170 | Dummy interface is just a mechanism to reserve that interface for non servod |
| 171 | interaction. Typically the interface will be managed by external |
| 172 | third-party tools like openOCD or urjtag for JTAG or flashrom for SPI |
| 173 | interfaces. |
| 174 | |
| 175 | TODO(tbroch): Investigate merits of incorporating these third-party |
| 176 | interfaces into servod or creating a communication channel between them |
| 177 | |
| 178 | Returns: None |
| 179 | """ |
| 180 | return None |
| 181 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 182 | def _init_ftdi_gpio(self, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 183 | """Initialize gpio driver interface and open for use. |
| 184 | |
| 185 | Args: |
| 186 | interface: interface number of FTDI device to use. |
| 187 | |
| 188 | Returns: |
| 189 | Instance object of interface. |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 190 | |
| 191 | Raises: |
| 192 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 193 | """ |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 194 | fobj = ftdigpio.Fgpio(self._vendor, self._product, interface, |
| 195 | self._serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 196 | try: |
| 197 | fobj.open() |
| 198 | except ftdigpio.FgpioError as e: |
| 199 | raise ServodError('Opening gpio interface. %s ( %d )' % (e.msg, e.value)) |
| 200 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 201 | return fobj |
| 202 | |
Aaron.Chuang | 88eff33 | 2014-07-31 08:32:00 +0800 | [diff] [blame] | 203 | def _init_bb_adc(self, interface): |
| 204 | """Initalize beaglebone ADC interface.""" |
| 205 | return bbadc.BBadc() |
| 206 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 207 | def _init_bb_gpio(self, interface): |
| 208 | """Initalize beaglebone gpio interface.""" |
Simran Basi | 5492bde | 2013-05-16 17:08:47 -0700 | [diff] [blame] | 209 | return bbgpio.BBgpio() |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 210 | |
| 211 | def _init_ftdi_i2c(self, interface): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 212 | """Initialize i2c interface and open for use. |
| 213 | |
| 214 | Args: |
| 215 | interface: interface number of FTDI device to use |
| 216 | |
| 217 | Returns: |
| 218 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 219 | |
| 220 | Raises: |
| 221 | ServodError: If init fails |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 222 | """ |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 223 | fobj = ftdii2c.Fi2c(self._vendor, self._product, interface, |
| 224 | self._serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 225 | try: |
| 226 | fobj.open() |
| 227 | except ftdii2c.Fi2cError as e: |
| 228 | raise ServodError('Opening i2c interface. %s ( %d )' % (e.msg, e.value)) |
| 229 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 230 | # Set the frequency of operation of the i2c bus. |
| 231 | # TODO(tbroch) make configureable |
| 232 | fobj.setclock(MAX_I2C_CLOCK_HZ) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 233 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 234 | return fobj |
| 235 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 236 | # TODO (sbasi) crbug.com/187489 - Implement bb_i2c. |
| 237 | def _init_bb_i2c(self, interface): |
| 238 | """Initalize beaglebone i2c interface.""" |
Simran Basi | a9ad25e | 2013-04-23 11:57:00 -0700 | [diff] [blame] | 239 | return bbi2c.BBi2c(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 240 | |
Rong Chang | c6c8c02 | 2014-08-11 14:07:11 +0800 | [diff] [blame] | 241 | def _init_dev_i2c(self, interface): |
| 242 | """Initalize Linux i2c-dev interface.""" |
| 243 | return i2cbus.I2CBus('/dev/i2c-%d' % interface['bus_num']) |
| 244 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 245 | def _init_ftdi_uart(self, interface): |
| 246 | """Initialize ftdi uart inteface and open for use |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 247 | |
| 248 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 249 | interact with it will query control for the pty's pathname and connect |
| 250 | with there favorite console program. For example: |
| 251 | cu -l /dev/pts/22 |
| 252 | |
| 253 | Args: |
| 254 | interface: interface number of FTDI device to use |
| 255 | |
| 256 | Returns: |
| 257 | Instance object of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 258 | |
| 259 | Raises: |
| 260 | ServodError: If init fails |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 261 | """ |
Jeremy Thorpe | 9e11006 | 2012-10-25 10:40:00 -0700 | [diff] [blame] | 262 | fobj = ftdiuart.Fuart(self._vendor, self._product, interface, |
| 263 | self._serialname) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 264 | try: |
| 265 | fobj.run() |
| 266 | except ftdiuart.FuartError as e: |
| 267 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 268 | |
Todd Broch | 47c43f4 | 2011-05-26 15:11:31 -0700 | [diff] [blame] | 269 | self._logger.info("%s" % fobj.get_pty()) |
| 270 | return fobj |
| 271 | |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 272 | # TODO (sbasi) crbug.com/187492 - Implement bbuart. |
| 273 | def _init_bb_uart(self, interface): |
| 274 | """Initalize beaglebone uart interface.""" |
Simran Basi | 949309b | 2013-05-31 15:12:15 -0700 | [diff] [blame] | 275 | logging.debug('UART INTERFACE: %s', interface) |
| 276 | return bbuart.BBuart(interface) |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 277 | |
| 278 | def _init_ftdi_gpiouart(self, interface): |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 279 | """Initialize special gpio + uart interface and open for use |
| 280 | |
| 281 | Note, the uart runs in a separate thread (pthreads). Users wishing to |
| 282 | interact with it will query control for the pty's pathname and connect |
| 283 | with there favorite console program. For example: |
| 284 | cu -l /dev/pts/22 |
| 285 | |
| 286 | Args: |
| 287 | interface: interface number of FTDI device to use |
| 288 | |
| 289 | Returns: |
| 290 | Instance objects of interface |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 291 | |
| 292 | Raises: |
| 293 | ServodError: If init fails |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 294 | """ |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 295 | fgpio = self._init_ftdi_gpio(interface) |
Jeremy Thorpe | 9e11006 | 2012-10-25 10:40:00 -0700 | [diff] [blame] | 296 | fuart = ftdiuart.Fuart(self._vendor, self._product, interface, |
| 297 | self._serialname, fgpio._fc) |
Todd Broch | 6de9dc6 | 2012-04-09 15:23:53 -0700 | [diff] [blame] | 298 | try: |
| 299 | fuart.run() |
| 300 | except ftdiuart.FuartError as e: |
| 301 | raise ServodError('Running uart interface. %s ( %d )' % (e.msg, e.value)) |
| 302 | |
Todd Broch | 888da78 | 2011-10-07 14:29:09 -0700 | [diff] [blame] | 303 | self._logger.info("uart pty: %s" % fuart.get_pty()) |
| 304 | return fgpio, fuart |
| 305 | |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 306 | def _camel_case(self, string): |
| 307 | output = '' |
| 308 | for s in string.split('_'): |
| 309 | if output: |
| 310 | output += s.capitalize() |
| 311 | else: |
| 312 | output = s |
| 313 | return output |
| 314 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 315 | def _get_param_drv(self, control_name, is_get=True): |
| 316 | """Get access to driver for a given control. |
| 317 | |
| 318 | Note, some controls have different parameter dictionaries for 'getting' the |
| 319 | control's value versus 'setting' it. Boolean is_get distinguishes which is |
| 320 | being requested. |
| 321 | |
| 322 | Args: |
| 323 | control_name: string name of control |
| 324 | is_get: boolean to determine |
| 325 | |
| 326 | Returns: |
| 327 | tuple (param, drv) where: |
| 328 | param: param dictionary for control |
| 329 | drv: instance object of driver for particular control |
| 330 | |
| 331 | Raises: |
| 332 | ServodError: Error occurred while examining params dict |
| 333 | """ |
| 334 | self._logger.debug("") |
| 335 | # if already setup just return tuple from driver dict |
| 336 | if control_name in self._drv_dict: |
| 337 | if is_get and ('get' in self._drv_dict[control_name]): |
| 338 | return self._drv_dict[control_name]['get'] |
| 339 | if not is_get and ('set' in self._drv_dict[control_name]): |
| 340 | return self._drv_dict[control_name]['set'] |
| 341 | |
| 342 | params = self._syscfg.lookup_control_params(control_name, is_get) |
| 343 | if 'drv' not in params: |
| 344 | self._logger.error("Unable to determine driver for %s" % control_name) |
| 345 | raise ServodError("'drv' key not found in params dict") |
| 346 | if 'interface' not in params: |
| 347 | self._logger.error("Unable to determine interface for %s" % |
| 348 | control_name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 349 | raise ServodError("'interface' key not found in params dict") |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 350 | |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 351 | interface_id = params.get( |
| 352 | '%s_interface' % self._version, params['interface']) |
| 353 | if interface_id == 'servo': |
| 354 | interface = self |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 355 | else: |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 356 | index = int(interface_id) - 1 |
| 357 | interface = self._interface_list[index] |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 358 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 359 | servo_pkg = imp.load_module('servo', *imp.find_module('servo')) |
| 360 | drv_pkg = imp.load_module('drv', |
| 361 | *imp.find_module('drv', servo_pkg.__path__)) |
| 362 | drv_name = params['drv'] |
| 363 | drv_module = getattr(drv_pkg, drv_name) |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 364 | drv_class = getattr(drv_module, self._camel_case(drv_name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 365 | drv = drv_class(interface, params) |
| 366 | if control_name not in self._drv_dict: |
| 367 | self._drv_dict[control_name] = {} |
| 368 | if is_get: |
| 369 | self._drv_dict[control_name]['get'] = (params, drv) |
| 370 | else: |
| 371 | self._drv_dict[control_name]['set'] = (params, drv) |
| 372 | return (params, drv) |
| 373 | |
| 374 | def doc_all(self): |
| 375 | """Return all documenation for controls. |
| 376 | |
| 377 | Returns: |
| 378 | string of <doc> text in config file (xml) and the params dictionary for |
| 379 | all controls. |
| 380 | |
| 381 | For example: |
| 382 | warm_reset :: Reset the device warmly |
| 383 | ------------------------> {'interface': '1', 'map': 'onoff_i', ... } |
| 384 | """ |
| 385 | return self._syscfg.display_config() |
| 386 | |
| 387 | def doc(self, name): |
| 388 | """Retreive doc string in system config file for given control name. |
| 389 | |
| 390 | Args: |
| 391 | name: name string of control to get doc string |
| 392 | |
| 393 | Returns: |
| 394 | doc string of name |
| 395 | |
| 396 | Raises: |
| 397 | NameError: if fails to locate control |
| 398 | """ |
| 399 | self._logger.debug("name(%s)" % (name)) |
| 400 | if self._syscfg.is_control(name): |
| 401 | return self._syscfg.get_control_docstring(name) |
| 402 | else: |
| 403 | raise NameError("No control %s" %name) |
| 404 | |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 405 | def _switch_usbkey(self, mux_direction): |
| 406 | """Connect USB flash stick to either servo or DUT. |
| 407 | |
| 408 | This function switches 'usb_mux_sel1' to provide electrical |
| 409 | connection between the USB port J3 and either servo or DUT side. |
| 410 | |
| 411 | Switching the usb mux is accompanied by powercycling |
| 412 | of the USB stick, because it sometimes gets wedged if the mux |
| 413 | is switched while the stick power is on. |
| 414 | |
| 415 | Args: |
| 416 | mux_direction: "servo_sees_usbkey" or "dut_sees_usbkey". |
| 417 | """ |
| 418 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_OFF) |
| 419 | time.sleep(self._USB_POWEROFF_DELAY) |
| 420 | self.set(self._USB_J3, mux_direction) |
| 421 | time.sleep(self._USB_POWEROFF_DELAY) |
| 422 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_ON) |
| 423 | if mux_direction == self._USB_J3_TO_SERVO: |
| 424 | time.sleep(self._USB_DETECTION_DELAY) |
| 425 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 426 | def _get_usb_port_set(self): |
| 427 | """Gets a set of USB disks currently connected to the system |
| 428 | |
| 429 | Returns: |
| 430 | A set of USB disk paths. |
| 431 | """ |
| 432 | usb_set = fnmatch.filter(os.listdir("/dev/"), "sd[a-z]") |
| 433 | return set(["/dev/" + dev for dev in usb_set]) |
| 434 | |
| 435 | def _probe_host_usb_dev(self): |
| 436 | """Probe the USB disk device plugged in the servo from the host side. |
| 437 | |
| 438 | Method can fail by: |
| 439 | 1) Having multiple servos connected and returning incorrect /dev/sdX of |
| 440 | another servo. |
| 441 | 2) Finding multiple /dev/sdX and returning None. |
| 442 | |
| 443 | Returns: |
| 444 | USB disk path if one and only one USB disk path is found, otherwise None. |
| 445 | """ |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 446 | original_value = self.get(self._USB_J3) |
| 447 | original_usb_power = self.get(self._USB_J3_PWR) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 448 | # Make the host unable to see the USB disk. |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 449 | if (original_usb_power == self._USB_J3_PWR_ON and |
| 450 | original_value != self._USB_J3_TO_DUT): |
| 451 | self._switch_usbkey(self._USB_J3_TO_DUT) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 452 | no_usb_set = self._get_usb_port_set() |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 453 | |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 454 | # Make the host able to see the USB disk. |
| 455 | self._switch_usbkey(self._USB_J3_TO_SERVO) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 456 | has_usb_set = self._get_usb_port_set() |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 457 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 458 | # Back to its original value. |
Fang Deng | 9037771 | 2013-06-03 15:51:48 -0700 | [diff] [blame] | 459 | if original_value != self._USB_J3_TO_SERVO: |
| 460 | self._switch_usbkey(original_value) |
| 461 | if original_usb_power != self._USB_J3_PWR_ON: |
| 462 | self.set(self._USB_J3_PWR, self._USB_J3_PWR_OFF) |
| 463 | time.sleep(self._USB_POWEROFF_DELAY) |
| 464 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 465 | # Subtract the two sets to find the usb device. |
| 466 | diff_set = has_usb_set - no_usb_set |
| 467 | if len(diff_set) == 1: |
| 468 | return diff_set.pop() |
| 469 | else: |
| 470 | return None |
| 471 | |
| 472 | def download_image_to_usb(self, image_path): |
| 473 | """Download image and save to the USB device found by probe_host_usb_dev. |
| 474 | If the image_path is a URL, it will download this url to the USB path; |
| 475 | otherwise it will simply copy the image_path's contents to the USB path. |
| 476 | |
| 477 | Args: |
| 478 | image_path: path or url to the recovery image. |
| 479 | |
| 480 | Returns: |
| 481 | True|False: True if process completed successfully, False if error |
| 482 | occurred. |
| 483 | Can't return None because XMLRPC doesn't allow it. PTAL at tbroch's |
| 484 | comment at the end of set(). |
| 485 | """ |
| 486 | self._logger.debug("image_path(%s)" % image_path) |
| 487 | self._logger.debug("Detecting USB stick device...") |
| 488 | usb_dev = self._probe_host_usb_dev() |
| 489 | if not usb_dev: |
| 490 | self._logger.error("No usb device connected to servo") |
| 491 | return False |
| 492 | |
| 493 | try: |
| 494 | if image_path.startswith(self._HTTP_PREFIX): |
| 495 | self._logger.debug("Image path is a URL, downloading image") |
| 496 | urllib.urlretrieve(image_path, usb_dev) |
| 497 | else: |
| 498 | shutil.copyfile(image_path, usb_dev) |
| 499 | except IOError as e: |
| 500 | self._logger.error("Failed to transfer image to USB device: %s ( %d ) ", |
| 501 | e.strerror, e.errno) |
| 502 | return False |
| 503 | except urllib.ContentTooShortError: |
| 504 | self._logger.error("Failed to download URL: %s to USB device: %s", |
| 505 | image_path, usb_dev) |
| 506 | return False |
| 507 | except BaseException as e: |
| 508 | self._logger.error("Unexpected exception downloading %s to %s: %s", |
| 509 | image_path, usb_dev, str(e)) |
| 510 | return False |
J. Richard Barnette | e4125af | 2013-02-26 18:31:56 -0800 | [diff] [blame] | 511 | finally: |
| 512 | # We just plastered the partition table for a block device. |
| 513 | # Pass or fail, we mustn't go without telling the kernel about |
| 514 | # the change, or it will punish us with sporadic, hard-to-debug |
| 515 | # failures. |
| 516 | subprocess.call(["sync"]) |
| 517 | subprocess.call(["blockdev", "--rereadpt", usb_dev]) |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 518 | return True |
| 519 | |
| 520 | def make_image_noninteractive(self): |
| 521 | """Makes the recovery image noninteractive. |
| 522 | |
| 523 | A noninteractive image will reboot automatically after installation |
| 524 | instead of waiting for the USB device to be removed to initiate a system |
| 525 | reboot. |
| 526 | |
| 527 | Mounts partition 1 of the image stored on usb_dev and creates a file |
| 528 | called "non_interactive" so that the image will become noninteractive. |
| 529 | |
| 530 | Returns: |
| 531 | True|False: True if process completed successfully, False if error |
| 532 | occurred. |
| 533 | """ |
| 534 | result = True |
| 535 | usb_dev = self._probe_host_usb_dev() |
| 536 | if not usb_dev: |
| 537 | self._logger.error("No usb device connected to servo") |
| 538 | return False |
| 539 | # Create TempDirectory |
| 540 | tmpdir = tempfile.mkdtemp() |
| 541 | if tmpdir: |
| 542 | # Mount drive to tmpdir. |
| 543 | partition_1 = "%s1" % usb_dev |
| 544 | rc = subprocess.call(["mount", partition_1, tmpdir]) |
| 545 | if rc == 0: |
| 546 | # Create file 'non_interactive' |
| 547 | non_interactive_file = os.path.join(tmpdir, "non_interactive") |
| 548 | try: |
| 549 | open(non_interactive_file, "w").close() |
| 550 | except IOError as e: |
| 551 | self._logger.error("Failed to create file %s : %s ( %d )", |
| 552 | non_interactive_file, e.strerror, e.errno) |
| 553 | result = False |
| 554 | except BaseException as e: |
| 555 | self._logger.error("Unexpected Exception creating file %s : %s", |
| 556 | non_interactive_file, str(e)) |
| 557 | result = False |
| 558 | # Unmount drive regardless if file creation worked or not. |
| 559 | rc = subprocess.call(["umount", partition_1]) |
| 560 | if rc != 0: |
| 561 | self._logger.error("Failed to unmount USB Device") |
| 562 | result = False |
| 563 | else: |
| 564 | self._logger.error("Failed to mount USB Device") |
| 565 | result = False |
| 566 | |
| 567 | # Delete tmpdir. May throw exception if 'umount' failed. |
| 568 | try: |
| 569 | os.rmdir(tmpdir) |
| 570 | except OSError as e: |
| 571 | self._logger.error("Failed to remove temp directory %s : %s", |
| 572 | tmpdir, str(e)) |
| 573 | return False |
| 574 | except BaseException as e: |
| 575 | self._logger.error("Unexpected Exception removing tempdir %s : %s", |
| 576 | tmpdir, str(e)) |
| 577 | return False |
| 578 | else: |
| 579 | self._logger.error("Failed to create temp directory.") |
| 580 | return False |
| 581 | return result |
| 582 | |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 583 | def set_get_all(self, cmds): |
| 584 | """Set &| get one or more control values. |
| 585 | |
| 586 | Args: |
| 587 | cmds: list of control[:value] to get or set. |
| 588 | |
| 589 | Returns: |
| 590 | rv: list of responses from calling get or set methods. |
| 591 | """ |
| 592 | rv = [] |
| 593 | for cmd in cmds: |
| 594 | if ':' in cmd: |
| 595 | (control, value) = cmd.split(':') |
| 596 | rv.append(self.set(control, value)) |
| 597 | else: |
| 598 | rv.append(self.get(cmd)) |
| 599 | return rv |
| 600 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 601 | def get(self, name): |
| 602 | """Get control value. |
| 603 | |
| 604 | Args: |
| 605 | name: name string of control |
| 606 | |
| 607 | Returns: |
| 608 | Response from calling drv get method. Value is reformatted based on |
| 609 | control's dictionary parameters |
| 610 | |
| 611 | Raises: |
| 612 | HwDriverError: Error occurred while using drv |
| 613 | """ |
| 614 | self._logger.debug("name(%s)" % (name)) |
Vadim Bendebury | c3a83cf | 2015-03-24 13:07:00 -0700 | [diff] [blame] | 615 | if name == 'serialname': |
| 616 | if self._serialname: |
| 617 | return self._serialname |
| 618 | return 'unknown' |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 619 | (param, drv) = self._get_param_drv(name) |
| 620 | try: |
| 621 | val = drv.get() |
| 622 | rd_val = self._syscfg.reformat_val(param, val) |
Todd Broch | b042e7a | 2011-12-14 17:41:36 -0800 | [diff] [blame] | 623 | self._logger.debug("%s = %s" % (name, rd_val)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 624 | return rd_val |
Todd Broch | fbc499d | 2011-06-16 16:09:58 -0700 | [diff] [blame] | 625 | except AttributeError, error: |
| 626 | self._logger.error("Getting %s: %s" % (name, error)) |
| 627 | raise |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 628 | except HwDriverError: |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 629 | self._logger.error("Getting %s" % (name)) |
| 630 | raise |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 631 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 632 | def get_all(self, verbose): |
| 633 | """Get all controls values. |
| 634 | |
| 635 | Args: |
| 636 | verbose: Boolean on whether to return doc info as well |
| 637 | |
| 638 | Returns: |
| 639 | string creating from trying to get all values of all controls. In case of |
| 640 | error attempting access to control, response is 'ERR'. |
| 641 | """ |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 642 | rsp = [] |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 643 | for name in self._syscfg.syscfg_dict['control']: |
| 644 | self._logger.debug("name = %s" %name) |
| 645 | try: |
| 646 | value = self.get(name) |
| 647 | except Exception: |
| 648 | value = "ERR" |
| 649 | pass |
| 650 | if verbose: |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 651 | rsp.append("GET %s = %s :: %s" % (name, value, self.doc(name))) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 652 | else: |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 653 | rsp.append("%s:%s" % (name, value)) |
| 654 | return '\n'.join(sorted(rsp)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 655 | |
| 656 | def set(self, name, wr_val_str): |
| 657 | """Set control. |
| 658 | |
| 659 | Args: |
| 660 | name: name string of control |
| 661 | wr_val_str: value string to write. Can be integer, float or a |
| 662 | alpha-numerical that is mapped to a integer or float. |
| 663 | |
| 664 | Raises: |
| 665 | HwDriverError: Error occurred while using driver |
| 666 | """ |
| 667 | self._logger.debug("name(%s) wr_val(%s)" % (name, wr_val_str)) |
| 668 | (params, drv) = self._get_param_drv(name, False) |
| 669 | wr_val = self._syscfg.resolve_val(params, wr_val_str) |
| 670 | try: |
| 671 | drv.set(wr_val) |
Vic Yang | be6cf26 | 2012-09-10 10:40:56 +0800 | [diff] [blame] | 672 | except HwDriverError: |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 673 | self._logger.error("Setting %s -> %s" % (name, wr_val_str)) |
| 674 | raise |
| 675 | # TODO(tbroch) Figure out why despite allow_none=True for both xmlrpc server |
| 676 | # & client I still have to return something to appease the |
| 677 | # marshall/unmarshall |
| 678 | return True |
| 679 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 680 | def hwinit(self, verbose=False): |
| 681 | """Initialize all controls. |
| 682 | |
| 683 | These values are part of the system config XML files of the form |
| 684 | init=<value>. This command should be used by clients wishing to return the |
| 685 | servo and DUT its connected to a known good/safe state. |
| 686 | |
Vadim Bendebury | bb51dd4 | 2013-01-31 13:47:46 -0800 | [diff] [blame] | 687 | Note that initialization errors are ignored (as in some cases they could |
| 688 | be caused by DUT firmware deficiencies). This might need to be fine tuned |
| 689 | later. |
| 690 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 691 | Args: |
| 692 | verbose: boolean, if True prints info about control initialized. |
| 693 | Otherwise prints nothing. |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 694 | |
| 695 | Returns: |
| 696 | This function is called across RPC and as such is expected to return |
| 697 | something unless transferring 'none' across is allowed. Hence adding a |
| 698 | dummy return value to make things simpler. |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 699 | """ |
Todd Broch | d9acf0a | 2012-12-05 13:43:06 -0800 | [diff] [blame] | 700 | for control_name, value in self._syscfg.hwinit: |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 701 | try: |
John Carey | 6fe2bbf | 2015-08-31 16:13:03 -0700 | [diff] [blame] | 702 | # Workaround for bug chrome-os-partner:42349. Without this check, the |
| 703 | # gpio will briefly pulse low if we set it from high to high. |
| 704 | if self.get(control_name) != value: |
| 705 | self.set(control_name, value) |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 706 | except Exception as e: |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 707 | self._logger.error("Problem initializing %s -> %s :: %s", |
| 708 | control_name, value, str(e)) |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 709 | if verbose: |
| 710 | self._logger.info('Initialized %s to %s', control_name, value) |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 711 | return True |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 712 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 713 | def echo(self, echo): |
| 714 | """Dummy echo function for testing/examples. |
| 715 | |
| 716 | Args: |
| 717 | echo: string to echo back to client |
| 718 | """ |
| 719 | self._logger.debug("echo(%s)" % (echo)) |
| 720 | return "ECH0ING: %s" % (echo) |
| 721 | |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 722 | def get_board(self): |
| 723 | """Return the board specified at startup, if any.""" |
| 724 | return self._board |
| 725 | |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 726 | def get_version(self): |
| 727 | """Get servo board version.""" |
| 728 | return self._version |
| 729 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 730 | def power_long_press(self): |
| 731 | """Simulate a long power button press.""" |
| 732 | # After a long power press, the EC may ignore the next power |
| 733 | # button press (at least on Alex). To guarantee that this |
| 734 | # won't happen, we need to allow the EC one second to |
| 735 | # collect itself. |
| 736 | self._keyboard.power_long_press() |
| 737 | return True |
| 738 | |
| 739 | def power_normal_press(self): |
| 740 | """Simulate a normal power button press.""" |
| 741 | self._keyboard.power_normal_press() |
| 742 | return True |
| 743 | |
| 744 | def power_short_press(self): |
| 745 | """Simulate a short power button press.""" |
| 746 | self._keyboard.power_short_press() |
| 747 | return True |
| 748 | |
| 749 | def power_key(self, secs=''): |
| 750 | """Simulate a power button press. |
| 751 | |
| 752 | Args: |
| 753 | secs: Time in seconds to simulate the keypress. |
| 754 | """ |
| 755 | self._keyboard.power_key(secs) |
| 756 | return True |
| 757 | |
| 758 | def ctrl_d(self, press_secs=''): |
| 759 | """Simulate Ctrl-d simultaneous button presses.""" |
| 760 | self._keyboard.ctrl_d(press_secs) |
| 761 | return True |
| 762 | |
| 763 | def ctrl_u(self): |
| 764 | """Simulate Ctrl-u simultaneous button presses.""" |
| 765 | self._keyboard.ctrl_u() |
| 766 | return True |
| 767 | |
| 768 | def ctrl_enter(self, press_secs=''): |
| 769 | """Simulate Ctrl-enter simultaneous button presses.""" |
| 770 | self._keyboard.ctrl_enter(press_secs) |
| 771 | return True |
| 772 | |
| 773 | def d_key(self, press_secs=''): |
| 774 | """Simulate Enter key button press.""" |
| 775 | self._keyboard.d_key(press_secs) |
| 776 | return True |
| 777 | |
| 778 | def ctrl_key(self, press_secs=''): |
| 779 | """Simulate Enter key button press.""" |
| 780 | self._keyboard.ctrl_key(press_secs) |
| 781 | return True |
| 782 | |
| 783 | def enter_key(self, press_secs=''): |
| 784 | """Simulate Enter key button press.""" |
| 785 | self._keyboard.enter_key(press_secs) |
| 786 | return True |
| 787 | |
| 788 | def refresh_key(self, press_secs=''): |
| 789 | """Simulate Refresh key (F3) button press.""" |
| 790 | self._keyboard.refresh_key(press_secs) |
| 791 | return True |
| 792 | |
| 793 | def ctrl_refresh_key(self, press_secs=''): |
| 794 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 795 | |
| 796 | This key combination is an alternative of Space key. |
| 797 | """ |
| 798 | self._keyboard.ctrl_refresh_key(press_secs) |
| 799 | return True |
| 800 | |
| 801 | def imaginary_key(self, press_secs=''): |
| 802 | """Simulate imaginary key button press. |
| 803 | |
| 804 | Maps to a key that doesn't physically exist. |
| 805 | """ |
| 806 | self._keyboard.imaginary_key(press_secs) |
| 807 | return True |
| 808 | |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 809 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 810 | def test(): |
| 811 | """Integration testing. |
| 812 | |
| 813 | TODO(tbroch) Enhance integration test and add unittest (see mox) |
| 814 | """ |
| 815 | logging.basicConfig(level=logging.DEBUG, |
| 816 | format="%(asctime)s - %(name)s - " + |
| 817 | "%(levelname)s - %(message)s") |
| 818 | # configure server & listen |
| 819 | servod_obj = Servod(1) |
| 820 | # 4 == number of interfaces on a FT4232H device |
| 821 | for i in xrange(4): |
| 822 | if i == 1: |
| 823 | # its an i2c interface ... see __init__ for details and TODO to make |
| 824 | # this configureable |
| 825 | servod_obj._interface_list[i].wr_rd(0x21, [0], 1) |
| 826 | else: |
| 827 | # its a gpio interface |
| 828 | servod_obj._interface_list[i].wr_rd(0) |
| 829 | |
| 830 | server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 9999), |
| 831 | allow_none=True) |
| 832 | server.register_introspection_functions() |
| 833 | server.register_multicall_functions() |
| 834 | server.register_instance(servod_obj) |
| 835 | logging.info("Listening on localhost port 9999") |
| 836 | server.serve_forever() |
| 837 | |
| 838 | if __name__ == "__main__": |
| 839 | test() |
| 840 | |
| 841 | # simple client transaction would look like |
| 842 | """ |
| 843 | remote_uri = 'http://localhost:9999' |
| 844 | client = xmlrpclib.ServerProxy(remote_uri, verbose=False) |
| 845 | send_str = "Hello_there" |
| 846 | print "Sent " + send_str + ", Recv " + client.echo(send_str) |
| 847 | """ |