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