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