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.""" |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 5 | import logging |
| 6 | import SimpleXMLRPCServer |
| 7 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 8 | import drv as servo_drv |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 9 | import interface as _interface |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 10 | import servo_dev |
Simran Basi | e750a34 | 2013-03-12 13:45:26 -0700 | [diff] [blame] | 11 | import servo_interfaces |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 12 | import servo_logging |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 13 | import servo_postinit |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 14 | |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 15 | HwDriverError = servo_drv.hw_driver.HwDriverError |
Aseda Aboagye | a492221 | 2015-11-20 15:19:08 -0800 | [diff] [blame] | 16 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 17 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 18 | class ServodError(Exception): |
| 19 | """Exception class for servod.""" |
| 20 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 21 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 22 | class Servod(object): |
| 23 | """Main class for Servo debug/controller Daemon.""" |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 24 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 25 | # This is the key to get the main serial used in the _serialnames dict. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 26 | MAIN_SERIAL = 'main' |
| 27 | SERVO_MICRO_SERIAL = 'servo_micro' |
| 28 | CCD_SERIAL = 'ccd' |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 29 | |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 30 | # Timeout to wait for interfaces to become available again if reinitialization |
Mary Ruthven | b7cc554 | 2019-07-15 15:20:10 -0700 | [diff] [blame] | 31 | # is taking place. In seconds. This is supposed to recover from brief resets. |
| 32 | # If the interface disappears for more than 5 seconds, then someone probably |
| 33 | # intentionally disconnected the device. Servod shouldn't be responsible for |
| 34 | # waiting for the device during an intentional disconnect. |
| 35 | INTERFACE_AVAILABILITY_TIMEOUT = 5 |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 36 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 37 | def init_servo_interfaces(self, vendor, product, serialname, interfaces): |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 38 | """Init the servo interfaces with the given interfaces. |
| 39 | |
| 40 | We don't use the self._{vendor,product,serialname} attributes because we |
| 41 | want to allow other callers to initialize other interfaces that may not |
| 42 | be associated with the initialized attributes (e.g. a servo v4 servod object |
| 43 | that wants to also initialize a servo micro interface). |
| 44 | |
| 45 | Args: |
| 46 | vendor: USB vendor id of FTDI device. |
| 47 | product: USB product id of FTDI device. |
| 48 | serialname: String of device serialname/number as defined in FTDI |
| 49 | eeprom. |
| 50 | interfaces: List of strings of interface types the server will |
| 51 | instantiate. |
| 52 | |
| 53 | Raises: |
| 54 | ServodError if unable to locate init method for particular interface. |
| 55 | """ |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 56 | # If it is a new device add it to the list |
Wai-Hong Tam | 1f9e9a7 | 2017-05-02 14:14:46 -0700 | [diff] [blame] | 57 | device = (vendor, product, serialname) |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 58 | self.add_device(device) |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 59 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 60 | # Extend the interface list if we need to. |
| 61 | interfaces_len = len(interfaces) |
| 62 | interface_list_len = len(self._interface_list) |
| 63 | if interfaces_len > interface_list_len: |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 64 | # Fill with dummies. |
| 65 | self._interface_list += [_interface.dummy.Dummy()] * (interfaces_len - |
| 66 | interface_list_len) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 67 | |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 68 | for i, interface_data in enumerate(interfaces): |
| 69 | if type(interface_data) is dict: |
| 70 | name = interface_data['name'] |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 71 | # Store interface index for those that care about it. |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 72 | interface_data['index'] = i |
| 73 | elif type(interface_data) is str: |
| 74 | if interface_data in ['dummy', 'ftdi_dummy']: |
Ruben Rodriguez Buchillon | 78c2349 | 2019-06-18 13:55:21 -0700 | [diff] [blame] | 75 | # 'dummy' reserves the interface for future use. Typically the |
| 76 | # interface will be managed by external third-party tools like |
| 77 | # openOCD for JTAG or flashrom for SPI. In the case of servo V4, |
| 78 | # it serves as a placeholder for servo micro interfaces. |
| 79 | continue |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 80 | name = interface_data |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 81 | else: |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 82 | raise ServodError('Illegal interface data type %s' |
| 83 | % type(interface_data)) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 84 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 85 | self._logger.info('Initializing interface %d to %s', i, name) |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 86 | result = _interface.Build(name=name, index=i, vid=vendor, pid=product, |
| 87 | sid=serialname, interface_data=interface_data, |
| 88 | servod=self) |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 89 | if isinstance(result, tuple): |
| 90 | result_len = len(result) |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 91 | self._interface_list[i:(i + result_len)] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 92 | else: |
Wai-Hong Tam | d8a94d6 | 2017-04-28 10:11:51 -0700 | [diff] [blame] | 93 | self._interface_list[i] = result |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 94 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 95 | def __init__(self, config, vendor, product, serialname=None, interfaces=None, |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 96 | board='', model='', version=None, usbkm232=None): |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 97 | """Servod constructor. |
| 98 | |
| 99 | Args: |
| 100 | config: instance of SystemConfig containing all controls for |
| 101 | particular Servod invocation |
| 102 | vendor: usb vendor id of FTDI device |
| 103 | product: usb product id of FTDI device |
Todd Broch | ad03444 | 2011-05-25 15:05:29 -0700 | [diff] [blame] | 104 | serialname: string of device serialname/number as defined in FTDI eeprom. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 105 | interfaces: list of strings of interface types the server will instantiate |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 106 | board: board name. e.g. octopus, coral, or scarlet. |
| 107 | model: model name of a given board. e.g. fleex, ampton, or apel. |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 108 | version: String. Servo board version. Examples: servo_v1, servo_v2, |
| 109 | servo_v2_r0, servo_v3 |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 110 | usbkm232: String. Optional. Path to USB-KM232 device which allow for |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 111 | sending keyboard commands to DUTs that do not have built in |
Wai-Hong Tam | 7b9f299 | 2017-10-24 16:07:14 -0700 | [diff] [blame] | 112 | keyboards. Used in FAFT tests. Use None for on board AVR MCU. |
| 113 | e.g. '/dev/ttyUSB0' or None. |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 114 | |
| 115 | Raises: |
| 116 | ServodError: if unable to locate init method for particular interface |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 117 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 118 | self._logger = logging.getLogger('Servod') |
| 119 | self._logger.debug('') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 120 | self._vendor = vendor |
| 121 | self._product = product |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 122 | self._devices = {} |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 123 | self._serialnames = {self.MAIN_SERIAL: serialname} |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 124 | self._syscfg = config |
| 125 | # list of objects (Fi2c, Fgpio) to physical interfaces (gpio, i2c) that ftdi |
| 126 | # interfaces are mapped to |
| 127 | self._interface_list = [] |
| 128 | # Dict of Dict to map control name, function name to to tuple (params, drv) |
| 129 | # Ex) _drv_dict[name]['get'] = (params, drv) |
| 130 | self._drv_dict = {} |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 131 | self._base_board = '' |
Namyoon Woo | 341a833 | 2019-03-07 12:01:31 -0800 | [diff] [blame] | 132 | self._board = board |
| 133 | if model: |
| 134 | self._board += '_' + model |
| 135 | self._model = model |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 136 | self._version = version |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 137 | self._usbkm232 = usbkm232 |
Ruben Rodriguez Buchillon | 386a010 | 2018-08-16 09:11:20 +0800 | [diff] [blame] | 138 | self._keyboard = None |
| 139 | self._usb_keyboard = None |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 140 | if not interfaces: |
Todd Broch | b21d804 | 2014-05-15 12:54:54 -0700 | [diff] [blame] | 141 | try: |
| 142 | interfaces = servo_interfaces.INTERFACE_BOARDS[board][vendor][product] |
| 143 | except KeyError: |
| 144 | interfaces = servo_interfaces.INTERFACE_DEFAULTS[vendor][product] |
Dino Li | c89d8c8 | 2018-01-11 09:56:47 +0800 | [diff] [blame] | 145 | self._interfaces = interfaces |
Todd Broch | dbb0998 | 2011-10-02 07:14:26 -0700 | [diff] [blame] | 146 | |
Kevin Cheng | dc3befd | 2016-07-15 12:34:00 -0700 | [diff] [blame] | 147 | self.init_servo_interfaces(vendor, product, serialname, interfaces) |
Kevin Cheng | 16304d1 | 2016-07-08 11:56:55 -0700 | [diff] [blame] | 148 | servo_postinit.post_init(self) |
Danny Chan | 662b602 | 2015-11-04 17:34:53 -0800 | [diff] [blame] | 149 | |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 150 | def reinitialize(self): |
| 151 | """Reinitialize all interfaces that support reinitialization""" |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 152 | for i, interface in enumerate(self._interface_list): |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 153 | interface.reinitialize() |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 154 | # Indicate interfaces are safe to use again. |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 155 | for device in self._devices.values(): |
| 156 | device.connect() |
Mary Ruthven | 1338964 | 2017-02-14 12:15:34 -0800 | [diff] [blame] | 157 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 158 | def get_servo_interfaces(self, position, size): |
| 159 | """Get the list of servo interfaces. |
| 160 | |
| 161 | Args: |
| 162 | position: The index the first interface to get. |
| 163 | size: The number of the interfaces. |
| 164 | """ |
| 165 | return self._interface_list[position:(position + size)] |
| 166 | |
| 167 | def set_servo_interfaces(self, position, interfaces): |
| 168 | """Set the list of servo interfaces. |
| 169 | |
| 170 | Args: |
| 171 | position: The index the first interface to set. |
| 172 | interfaces: The list of interfaces to set. |
| 173 | """ |
| 174 | size = len(interfaces) |
| 175 | self._interface_list[position:(position + size)] = interfaces |
| 176 | |
Ruben Rodriguez Buchillon | a16374b | 2018-06-20 16:45:00 -0700 | [diff] [blame] | 177 | def close(self): |
| 178 | """Servod turn down logic.""" |
| 179 | for i, interface in enumerate(self._interface_list): |
| 180 | self._logger.info('Turning down interface %d' % i) |
Ruben Rodriguez Buchillon | 1092ebf | 2020-02-28 15:12:19 -0800 | [diff] [blame] | 181 | interface.close() |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 182 | |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 183 | def get_devices(self): |
| 184 | return self._devices.values() |
| 185 | |
| 186 | def add_device(self, device): |
| 187 | if device not in self._devices: |
| 188 | vid, pid, serial = device |
Mary Ruthven | 6b14d3f | 2019-07-15 12:52:53 -0700 | [diff] [blame] | 189 | servod_device = servo_dev.ServoDevice(vid, pid, serial) |
Mary Ruthven | cb86185 | 2019-07-15 16:30:48 -0700 | [diff] [blame] | 190 | self._devices[device] = servod_device |
| 191 | |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 192 | def _camel_case(self, string): |
| 193 | output = '' |
| 194 | for s in string.split('_'): |
| 195 | if output: |
| 196 | output += s.capitalize() |
| 197 | else: |
| 198 | output = s |
| 199 | return output |
| 200 | |
Wai-Hong Tam | 4544c30 | 2017-05-24 19:44:53 -0700 | [diff] [blame] | 201 | def clear_cached_drv(self): |
| 202 | """Clear the cached drivers. |
| 203 | |
| 204 | The drivers are cached in the Dict _drv_dict when a control is got or set. |
| 205 | When the servo interfaces are relocated, the cached values may become wrong. |
| 206 | Should call this method to clear the cached values. |
| 207 | """ |
| 208 | self._drv_dict = {} |
| 209 | |
Ruben Rodriguez Buchillon | 2c6589f | 2018-10-20 15:30:26 +0800 | [diff] [blame] | 210 | def _get_servo_specific_param(self, params, param_key, control_name): |
| 211 | """Get |param_key| from params by looking for servo specific params first. |
| 212 | |
| 213 | Find the candidate servos. Using servo_v4 with a servo_micro connected as |
| 214 | example, the following shows the priority for selecting the interface. |
| 215 | |
| 216 | 1. The full name. (e.g. - 'servo_v4_with_servo_micro_interface') |
| 217 | 2. servo_micro_interface |
| 218 | 3. servo_v4_interface |
| 219 | 4. Fallback to the default, interface. |
| 220 | |
| 221 | Args: |
| 222 | params: params dictionary for a control |
| 223 | param_key: identifier in the params dictionary to look for |
| 224 | control_name: control name the params correspond to |
| 225 | |
| 226 | Returns: |
| 227 | The best suited param value for param_key given the servo type or |
| 228 | None if even the default is not defined. |
| 229 | """ |
| 230 | candidates = [self._version] |
Ruben Rodriguez Buchillon | d18c6eb | 2019-07-10 14:40:22 -0700 | [diff] [blame] | 231 | if '_with_' in self._version: |
| 232 | v4, raw_dut_device = self._version.split('_with_') |
| 233 | dut_devices = raw_dut_device.split('_and_') |
| 234 | # NOTE(coconutruben): all of this nonsense is going away with the new |
| 235 | # servod and is to bridge the time until then. Please forgive the below |
| 236 | # until then. |
| 237 | if '.' in control_name: |
| 238 | # In the current implementation, the only case where a '.' (a prefix) |
| 239 | # is in the control name is when there is a dual instance with micro and |
| 240 | # ccd on a v4. |
| 241 | dut_device = dut_devices[1] |
| 242 | else: |
| 243 | # In the normal control name, we need to make sure the version used |
| 244 | # does not include the potential _and_ portion from a dual instance. |
| 245 | dut_device = dut_devices[0] |
| 246 | candidates.extend([dut_device, v4]) |
Ruben Rodriguez Buchillon | 2c6589f | 2018-10-20 15:30:26 +0800 | [diff] [blame] | 247 | candidates = ['%s_%s' % (c, param_key) for c in candidates] |
| 248 | candidates.append(param_key) |
| 249 | for c in candidates: |
| 250 | if c in params: |
| 251 | self._logger.debug('Using %s parameter.', c) |
| 252 | return params[c] |
| 253 | self._logger.error('Unable to determine %s for %s', param_key, control_name) |
| 254 | self._logger.error('params: %r', params) |
| 255 | return None |
| 256 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 257 | def _get_param_drv(self, control_name, is_get=True): |
| 258 | """Get access to driver for a given control. |
| 259 | |
| 260 | Note, some controls have different parameter dictionaries for 'getting' the |
| 261 | control's value versus 'setting' it. Boolean is_get distinguishes which is |
| 262 | being requested. |
| 263 | |
| 264 | Args: |
| 265 | control_name: string name of control |
| 266 | is_get: boolean to determine |
| 267 | |
| 268 | Returns: |
Fei Shao | 8aec57b | 2019-12-11 17:08:40 +0800 | [diff] [blame] | 269 | tuple (params, drv, device_info) where: |
| 270 | params: param dictionary for control |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 271 | drv: instance object of driver for particular control |
Fei Shao | 8aec57b | 2019-12-11 17:08:40 +0800 | [diff] [blame] | 272 | device_info: servo device information |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 273 | |
| 274 | Raises: |
| 275 | ServodError: Error occurred while examining params dict |
| 276 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 277 | self._logger.debug('') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 278 | # if already setup just return tuple from driver dict |
| 279 | if control_name in self._drv_dict: |
| 280 | if is_get and ('get' in self._drv_dict[control_name]): |
| 281 | return self._drv_dict[control_name]['get'] |
| 282 | if not is_get and ('set' in self._drv_dict[control_name]): |
| 283 | return self._drv_dict[control_name]['set'] |
| 284 | |
| 285 | params = self._syscfg.lookup_control_params(control_name, is_get) |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 286 | |
Ruben Rodriguez Buchillon | 2c6589f | 2018-10-20 15:30:26 +0800 | [diff] [blame] | 287 | # Get the most suitable drv given the servo instance. |
| 288 | drv_name = self._get_servo_specific_param(params, 'drv', control_name) |
| 289 | if drv_name == 'na': |
| 290 | # 'na' drv can be used to selectively turn controls into noops for |
| 291 | # a given servo hardware. Ensure that there is an interface. |
| 292 | params.setdefault('interface', 'servo') |
| 293 | self._logger.debug('Setting interface to default to %r for %r unless ' |
| 294 | ' defined in params, as drv is %r.', 'servo', |
| 295 | control_name, 'na') |
| 296 | # Setting input_type to str allows all inputs through enabling a true noop |
| 297 | params.update({'input_type': 'str'}) |
| 298 | interface_id = self._get_servo_specific_param(params, 'interface', |
| 299 | control_name) |
| 300 | if None in [drv_name, interface_id]: |
| 301 | raise ServodError('No drv/interface for control %r found' % control_name) |
Aseda Aboagye | 1d8477b | 2017-05-10 17:24:31 -0700 | [diff] [blame] | 302 | |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 303 | if interface_id == 'servo': |
| 304 | interface = self |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 305 | else: |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 306 | index = int(interface_id) |
J. Richard Barnette | 275d9fd | 2014-02-11 14:38:54 -0800 | [diff] [blame] | 307 | interface = self._interface_list[index] |
Simran Basi | 668be0e | 2013-08-07 11:54:50 -0700 | [diff] [blame] | 308 | |
Mary Ruthven | 6b14d3f | 2019-07-15 12:52:53 -0700 | [diff] [blame] | 309 | device_info = None |
| 310 | if hasattr(interface, 'get_device_info'): |
| 311 | device_info = interface.get_device_info() |
Wai-Hong Tam | 4c09eff | 2017-02-17 11:46:19 -0800 | [diff] [blame] | 312 | drv_module = getattr(servo_drv, drv_name) |
Tom Wai-Hong Tam | 28f0a5f | 2012-08-21 12:49:57 +0800 | [diff] [blame] | 313 | drv_class = getattr(drv_module, self._camel_case(drv_name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 314 | drv = drv_class(interface, params) |
| 315 | if control_name not in self._drv_dict: |
| 316 | self._drv_dict[control_name] = {} |
| 317 | if is_get: |
Mary Ruthven | 6b14d3f | 2019-07-15 12:52:53 -0700 | [diff] [blame] | 318 | self._drv_dict[control_name]['get'] = (params, drv, device_info) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 319 | else: |
Mary Ruthven | 6b14d3f | 2019-07-15 12:52:53 -0700 | [diff] [blame] | 320 | self._drv_dict[control_name]['set'] = (params, drv, device_info) |
| 321 | return (params, drv, device_info) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 322 | |
| 323 | def doc_all(self): |
| 324 | """Return all documenation for controls. |
| 325 | |
| 326 | Returns: |
| 327 | string of <doc> text in config file (xml) and the params dictionary for |
| 328 | all controls. |
| 329 | |
| 330 | For example: |
| 331 | warm_reset :: Reset the device warmly |
| 332 | ------------------------> {'interface': '1', 'map': 'onoff_i', ... } |
| 333 | """ |
| 334 | return self._syscfg.display_config() |
| 335 | |
| 336 | def doc(self, name): |
| 337 | """Retreive doc string in system config file for given control name. |
| 338 | |
| 339 | Args: |
| 340 | name: name string of control to get doc string |
| 341 | |
| 342 | Returns: |
| 343 | doc string of name |
| 344 | |
| 345 | Raises: |
| 346 | NameError: if fails to locate control |
| 347 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 348 | self._logger.debug('name(%s)' % (name)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 349 | if self._syscfg.is_control(name): |
| 350 | return self._syscfg.get_control_docstring(name) |
| 351 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 352 | raise NameError('No control %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 353 | |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 354 | def safe_switch_usbkey_power(self, power_state, _=None): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 355 | """Toggle the usb power safely. |
| 356 | |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 357 | Args: |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 358 | power_state: The setting to set for the usbkey power. |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 359 | _: to conform to current API |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 360 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 361 | Returns: |
| 362 | An empty string to appease the xmlrpc gods. |
| 363 | """ |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 364 | self.set('image_usbkey_pwr', power_state) |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 365 | return '' |
| 366 | |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 367 | def safe_switch_usbkey(self, mux_direction, _=0): |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 368 | """Toggle the usb direction safely. |
| 369 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 370 | Args: |
Wai-Hong Tam | f93f9a2 | 2018-02-06 14:24:46 -0800 | [diff] [blame] | 371 | mux_direction: "servo_sees_usbkey" or "dut_sees_usbkey". |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 372 | _: to conform to current API |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 373 | |
| 374 | Returns: |
| 375 | An empty string to appease the xmlrpc gods. |
| 376 | """ |
Ruben Rodriguez Buchillon | 247d88f | 2018-08-03 18:07:01 +0800 | [diff] [blame] | 377 | self.set('image_usbkey_direction', mux_direction) |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 378 | return '' |
| 379 | |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 380 | def probe_host_usb_dev(self, _=0): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 381 | """Probe the USB disk device plugged in the servo from the host side. |
| 382 | |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 383 | Args: |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 384 | _: to conform to current API |
Kevin Cheng | 5595b34 | 2016-09-29 15:51:01 -0700 | [diff] [blame] | 385 | |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 386 | Returns: |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 387 | USB disk path if one and only one USB disk path is found, otherwise an |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 388 | """ |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 389 | return self.get('image_usbkey_dev') |
Kevin Cheng | c49494e | 2016-07-25 12:13:38 -0700 | [diff] [blame] | 390 | |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 391 | def download_image_to_usb(self, image_path, _=0): |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 392 | """Download image and save to the USB device found by probe_host_usb_dev. |
| 393 | If the image_path is a URL, it will download this url to the USB path; |
| 394 | otherwise it will simply copy the image_path's contents to the USB path. |
| 395 | |
| 396 | Args: |
| 397 | image_path: path or url to the recovery image. |
Ruben Rodriguez Buchillon | 68958f8 | 2018-08-03 18:24:28 +0800 | [diff] [blame] | 398 | _: to conform to current API |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 399 | |
| 400 | Returns: |
| 401 | True|False: True if process completed successfully, False if error |
Ruben Rodriguez Buchillon | 4e00f0e | 2018-08-28 15:21:47 +0800 | [diff] [blame] | 402 | occurred. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 403 | """ |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 404 | try: |
Ruben Rodriguez Buchillon | 4e00f0e | 2018-08-28 15:21:47 +0800 | [diff] [blame] | 405 | self.set('download_image_to_usb_dev', image_path) |
| 406 | return True |
| 407 | except Exception: |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 408 | return False |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 409 | |
| 410 | def make_image_noninteractive(self): |
| 411 | """Makes the recovery image noninteractive. |
| 412 | |
| 413 | A noninteractive image will reboot automatically after installation |
| 414 | instead of waiting for the USB device to be removed to initiate a system |
| 415 | reboot. |
| 416 | |
| 417 | Mounts partition 1 of the image stored on usb_dev and creates a file |
| 418 | called "non_interactive" so that the image will become noninteractive. |
| 419 | |
| 420 | Returns: |
| 421 | True|False: True if process completed successfully, False if error |
Ruben Rodriguez Buchillon | 1a3a7ec | 2018-08-03 18:44:55 +0800 | [diff] [blame] | 422 | occurred. |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 423 | """ |
Ruben Rodriguez Buchillon | 1a3a7ec | 2018-08-03 18:44:55 +0800 | [diff] [blame] | 424 | try: |
| 425 | usb_dev = self.get('image_usbkey_dev') |
| 426 | usb_dev_partition = '%s1' % usb_dev |
| 427 | self.set('make_usb_dev_image_noninteractive', usb_dev_partition) |
| 428 | return True |
| 429 | except Exception: |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 430 | return False |
Simran Basi | a9f4103 | 2012-05-11 14:21:58 -0700 | [diff] [blame] | 431 | |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 432 | def set_get_all(self, cmds): |
| 433 | """Set &| get one or more control values. |
| 434 | |
| 435 | Args: |
| 436 | cmds: list of control[:value] to get or set. |
| 437 | |
| 438 | Returns: |
| 439 | rv: list of responses from calling get or set methods. |
| 440 | """ |
| 441 | rv = [] |
| 442 | for cmd in cmds: |
| 443 | if ':' in cmd: |
Wai-Hong Tam | 269f180 | 2019-05-16 12:37:17 -0700 | [diff] [blame] | 444 | (control, value) = cmd.split(':', 1) |
Todd Broch | 352b4b2 | 2013-03-22 09:48:40 -0700 | [diff] [blame] | 445 | rv.append(self.set(control, value)) |
| 446 | else: |
| 447 | rv.append(self.get(cmd)) |
| 448 | return rv |
| 449 | |
Mary Ruthven | 493df51 | 2019-07-12 13:10:18 -0700 | [diff] [blame] | 450 | def add_serial_number(self, name, serial_number): |
| 451 | """Adds the serial number to the _serialnames dictionary. |
| 452 | |
| 453 | Args: |
| 454 | name: A string which is the key into the _serialnames dictionary. |
| 455 | serial_number: A string which is the key into the _serialnames dictionary. |
| 456 | """ |
| 457 | self._serialnames[name] = serial_number |
| 458 | self._logger.debug('Added %s %s to serialnames %r', name, serial_number, |
| 459 | self._serialnames) |
| 460 | |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 461 | def get_serial_number(self, name): |
| 462 | """Returns the desired serial number from the serialnames dict. |
| 463 | |
| 464 | Args: |
| 465 | name: A string which is the key into the _serialnames dictionary. |
| 466 | |
| 467 | Returns: |
| 468 | A string containing the serial number or "unknown". |
| 469 | """ |
Mary Ruthven | f17fb17 | 2019-08-15 17:17:35 -0700 | [diff] [blame] | 470 | # Remove the prefix from the serialname control. Serialnames are |
| 471 | # universal. It doesn't matter what the prefix is. |
| 472 | # The prefix is separated from the main control with '.' |
| 473 | name = name.split('.', 1)[-1] |
| 474 | |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 475 | if not name: |
| 476 | name = 'main' |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 477 | try: |
| 478 | return self._serialnames[name] |
| 479 | except KeyError: |
| 480 | self._logger.debug("'%s_serialname' not found!", name) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 481 | return 'unknown' |
Aseda Aboagye | 6921f60 | 2017-08-01 14:45:38 -0700 | [diff] [blame] | 482 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 483 | def get(self, name): |
| 484 | """Get control value. |
| 485 | |
| 486 | Args: |
| 487 | name: name string of control |
| 488 | |
| 489 | Returns: |
| 490 | Response from calling drv get method. Value is reformatted based on |
| 491 | control's dictionary parameters |
| 492 | |
| 493 | Raises: |
| 494 | HwDriverError: Error occurred while using drv |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 495 | ServodError: if interfaces are not available within timeout period |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 496 | """ |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 497 | if 'serialname' in name: |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 498 | # This route is to retrieve serialnames on servo v4, which |
| 499 | # connects to multiple servo-micros or CCD, like the controls, |
| 500 | # 'ccd_serialname', 'servo_micro_for_soraka_serialname', etc. |
| 501 | # TODO(aaboagye): Refactor it. |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 502 | return self.get_serial_number(name.split('serialname')[0].strip('_')) |
Wai-Hong Tam | bafeca7 | 2017-10-05 14:22:12 -0700 | [diff] [blame] | 503 | |
Dana Goyette | 17b0d25 | 2020-03-09 10:32:50 -0700 | [diff] [blame] | 504 | with servo_logging.WrapGetCall( |
| 505 | name, known_exceptions=(AttributeError, HwDriverError)) as wrapper: |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 506 | (params, drv, device) = self._get_param_drv(name) |
| 507 | if device in self._devices: |
| 508 | self._devices[device].wait(self.INTERFACE_AVAILABILITY_TIMEOUT) |
| 509 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 510 | val = drv.get() |
Fei Shao | 8aec57b | 2019-12-11 17:08:40 +0800 | [diff] [blame] | 511 | rd_val = self._syscfg.reformat_val(params, val) |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 512 | wrapper.got_result(rd_val) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 513 | return rd_val |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 514 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 515 | def get_all(self, verbose): |
| 516 | """Get all controls values. |
| 517 | |
| 518 | Args: |
| 519 | verbose: Boolean on whether to return doc info as well |
| 520 | |
| 521 | Returns: |
| 522 | string creating from trying to get all values of all controls. In case of |
| 523 | error attempting access to control, response is 'ERR'. |
| 524 | """ |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 525 | rsp = [] |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 526 | for name in self._syscfg.syscfg_dict['control']: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 527 | self._logger.debug('name = %s' % name) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 528 | try: |
| 529 | value = self.get(name) |
| 530 | except Exception: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 531 | value = 'ERR' |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 532 | pass |
| 533 | if verbose: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 534 | rsp.append('GET %s = %s :: %s' % (name, value, self.doc(name))) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 535 | else: |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 536 | rsp.append('%s:%s' % (name, value)) |
Vadim Bendebury | b07944c | 2013-01-16 10:47:10 -0800 | [diff] [blame] | 537 | return '\n'.join(sorted(rsp)) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 538 | |
| 539 | def set(self, name, wr_val_str): |
| 540 | """Set control. |
| 541 | |
| 542 | Args: |
| 543 | name: name string of control |
| 544 | wr_val_str: value string to write. Can be integer, float or a |
| 545 | alpha-numerical that is mapped to a integer or float. |
| 546 | |
| 547 | Raises: |
| 548 | HwDriverError: Error occurred while using driver |
Ruben Rodriguez Buchillon | a5d8b92 | 2018-09-03 16:51:03 +0800 | [diff] [blame] | 549 | ServodError: if interfaces are not available within timeout period |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 550 | """ |
Dana Goyette | 17b0d25 | 2020-03-09 10:32:50 -0700 | [diff] [blame] | 551 | with servo_logging.WrapSetCall( |
| 552 | name, wr_val_str, known_exceptions=(AttributeError, HwDriverError)): |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 553 | (params, drv, device) = self._get_param_drv(name, False) |
| 554 | if device in self._devices: |
| 555 | self._devices[device].wait(self.INTERFACE_AVAILABILITY_TIMEOUT) |
| 556 | wr_val = self._syscfg.resolve_val(params, wr_val_str) |
| 557 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 558 | drv.set(wr_val) |
Dana Goyette | 91cae86 | 2020-02-21 13:43:13 -0800 | [diff] [blame] | 559 | |
Ruben Rodriguez Buchillon | b5fe0f1 | 2018-05-09 10:19:56 +0800 | [diff] [blame] | 560 | # TODO(crbug.com/841097) Figure out why despite allow_none=True for both |
| 561 | # xmlrpc server & client I still have to return something to appease the |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 562 | # marshall/unmarshall |
| 563 | return True |
| 564 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 565 | def hwinit(self, verbose=False): |
| 566 | """Initialize all controls. |
| 567 | |
| 568 | These values are part of the system config XML files of the form |
| 569 | init=<value>. This command should be used by clients wishing to return the |
| 570 | servo and DUT its connected to a known good/safe state. |
| 571 | |
Vadim Bendebury | bb51dd4 | 2013-01-31 13:47:46 -0800 | [diff] [blame] | 572 | Note that initialization errors are ignored (as in some cases they could |
| 573 | be caused by DUT firmware deficiencies). This might need to be fine tuned |
| 574 | later. |
| 575 | |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 576 | Args: |
| 577 | verbose: boolean, if True prints info about control initialized. |
| 578 | Otherwise prints nothing. |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 579 | |
| 580 | Returns: |
| 581 | This function is called across RPC and as such is expected to return |
| 582 | something unless transferring 'none' across is allowed. Hence adding a |
| 583 | dummy return value to make things simpler. |
Todd Broch | d606167 | 2012-05-11 15:52:47 -0700 | [diff] [blame] | 584 | """ |
Todd Broch | d9acf0a | 2012-12-05 13:43:06 -0800 | [diff] [blame] | 585 | for control_name, value in self._syscfg.hwinit: |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 586 | try: |
John Carey | 6fe2bbf | 2015-08-31 16:13:03 -0700 | [diff] [blame] | 587 | # Workaround for bug chrome-os-partner:42349. Without this check, the |
| 588 | # gpio will briefly pulse low if we set it from high to high. |
| 589 | if self.get(control_name) != value: |
Aseda Aboagye | a849d46 | 2016-05-04 17:08:16 -0700 | [diff] [blame] | 590 | self.set(control_name, value) |
| 591 | if verbose: |
| 592 | self._logger.info('Initialized %s to %s', control_name, value) |
Ruben Rodriguez Buchillon | 70eabcc | 2019-06-20 10:23:40 -0700 | [diff] [blame] | 593 | except Exception as e: |
| 594 | self._logger.error( |
Matthew Blecker | a5d979c | 2018-10-16 20:59:19 -0700 | [diff] [blame] | 595 | 'Problem initializing %s -> %s', control_name, value) |
Ruben Rodriguez Buchillon | 70eabcc | 2019-06-20 10:23:40 -0700 | [diff] [blame] | 596 | self._logger.error(str(e)) |
| 597 | self._logger.error('Please consider verifying the logs and if the ' |
| 598 | 'error is not just a setup issue, consider filing ' |
| 599 | 'a bug. Also checkout go/servo-ki.') |
Nick Sanders | bc83628 | 2015-12-08 21:19:23 -0800 | [diff] [blame] | 600 | |
Namyoon Woo | 6ff3661 | 2019-10-16 16:41:12 -0700 | [diff] [blame] | 601 | # If there is the control of 'active_v4_device', set active_v4_device to |
| 602 | # the default device as initialization. |
Namyoon Woo | ad2a2bb | 2019-11-04 16:26:15 -0800 | [diff] [blame] | 603 | try: |
Mary Ruthven | f547c08 | 2020-05-07 11:32:28 -0700 | [diff] [blame] | 604 | if self._syscfg.is_control('active_v4_device'): |
| 605 | self.set('active_v4_device', 'default') |
Todd Broch | a10cba1 | 2019-11-25 16:00:35 -0800 | [diff] [blame] | 606 | except servo_drv.active_v4_device.activeV4DeviceError as e: |
Mary Ruthven | f547c08 | 2020-05-07 11:32:28 -0700 | [diff] [blame] | 607 | self._logger.debug('Could not set active device: %s', str(e)) |
Namyoon Woo | 6ff3661 | 2019-10-16 16:41:12 -0700 | [diff] [blame] | 608 | |
Vadim Bendebury | 5934e4b | 2013-02-06 13:57:54 -0800 | [diff] [blame] | 609 | return True |
Todd Broch | 3ec8df0 | 2012-11-20 10:53:03 -0800 | [diff] [blame] | 610 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 611 | def echo(self, echo): |
| 612 | """Dummy echo function for testing/examples. |
| 613 | |
| 614 | Args: |
| 615 | echo: string to echo back to client |
| 616 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 617 | self._logger.debug('echo(%s)' % (echo)) |
| 618 | return 'ECH0ING: %s' % (echo) |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 619 | |
J. Richard Barnette | e282055 | 2013-03-14 16:13:46 -0700 | [diff] [blame] | 620 | def get_board(self): |
| 621 | """Return the board specified at startup, if any.""" |
| 622 | return self._board |
| 623 | |
Wai-Hong Tam | 416cf61 | 2017-09-19 11:39:21 -0700 | [diff] [blame] | 624 | def get_base_board(self): |
| 625 | """Returns the board name of the base if present. |
| 626 | |
| 627 | Returns: |
| 628 | A string of the board name, or '' if not present. |
| 629 | """ |
| 630 | # The value is set in servo_postinit. |
| 631 | return self._base_board |
| 632 | |
Simran Basi | a23c139 | 2013-08-06 14:59:10 -0700 | [diff] [blame] | 633 | def get_version(self): |
| 634 | """Get servo board version.""" |
| 635 | return self._version |
| 636 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 637 | def power_long_press(self): |
| 638 | """Simulate a long power button press.""" |
| 639 | # After a long power press, the EC may ignore the next power |
| 640 | # button press (at least on Alex). To guarantee that this |
| 641 | # won't happen, we need to allow the EC one second to |
| 642 | # collect itself. |
Ruben Rodriguez Buchillon | 0f46794 | 2018-07-27 18:02:32 +0800 | [diff] [blame] | 643 | return self.set('power_key', 'long_press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 644 | |
| 645 | def power_normal_press(self): |
| 646 | """Simulate a normal power button press.""" |
Ruben Rodriguez Buchillon | 0f46794 | 2018-07-27 18:02:32 +0800 | [diff] [blame] | 647 | return self.set('power_key', 'press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 648 | |
| 649 | def power_short_press(self): |
| 650 | """Simulate a short power button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 651 | return self.set('power_key', 'short_press') |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 652 | |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 653 | def power_key(self, press_secs=''): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 654 | """Simulate a power button press. |
| 655 | |
| 656 | Args: |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 657 | press_secs: Time in seconds to simulate the keypress. |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 658 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 659 | return self.set('power_key', 'press' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 660 | |
| 661 | def ctrl_d(self, press_secs=''): |
| 662 | """Simulate Ctrl-d simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 663 | return self.set('ctrl_d', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 664 | |
Victor Dodon | e539cea | 2016-03-29 18:50:17 -0700 | [diff] [blame] | 665 | def ctrl_u(self, press_secs=''): |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 666 | """Simulate Ctrl-u simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 667 | return self.set('ctrl_u', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 668 | |
| 669 | def ctrl_enter(self, press_secs=''): |
| 670 | """Simulate Ctrl-enter simultaneous button presses.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 671 | return self.set('ctrl_enter', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 672 | |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 673 | def ctrl_key(self, press_secs=''): |
| 674 | """Simulate Enter key button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 675 | return self.set('ctrl_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 676 | |
| 677 | def enter_key(self, press_secs=''): |
| 678 | """Simulate Enter key button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 679 | return self.set('enter_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 680 | |
| 681 | def refresh_key(self, press_secs=''): |
| 682 | """Simulate Refresh key (F3) button press.""" |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 683 | return self.set('refresh_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 684 | |
| 685 | def ctrl_refresh_key(self, press_secs=''): |
| 686 | """Simulate Ctrl and Refresh (F3) simultaneous press. |
| 687 | |
| 688 | This key combination is an alternative of Space key. |
| 689 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 690 | return self.set('ctrl_refresh_key', ('tab' if press_secs is '' else |
| 691 | press_secs)) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 692 | |
| 693 | def imaginary_key(self, press_secs=''): |
| 694 | """Simulate imaginary key button press. |
| 695 | |
| 696 | Maps to a key that doesn't physically exist. |
| 697 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 698 | return self.set('imaginary_key', 'tab' if press_secs is '' else press_secs) |
Yusuf Mohsinally | 29e30d2 | 2014-01-14 15:29:17 -0800 | [diff] [blame] | 699 | |
Vincent Palatin | 3acbbe5 | 2016-07-19 17:40:12 +0200 | [diff] [blame] | 700 | def sysrq_x(self, press_secs=''): |
| 701 | """Simulate Alt VolumeUp X simultaneous press. |
| 702 | |
| 703 | This key combination is the kernel system request (sysrq) x. |
| 704 | """ |
Lenine Ajagappane | 400d7d2 | 2018-09-05 02:29:21 +0530 | [diff] [blame] | 705 | return self.set('sysrq_x', 'tab' if press_secs is '' else press_secs) |
Vincent Palatin | 3acbbe5 | 2016-07-19 17:40:12 +0200 | [diff] [blame] | 706 | |
Kevin Cheng | 4b4f002 | 2016-09-09 02:37:07 -0700 | [diff] [blame] | 707 | def get_servo_serials(self): |
| 708 | """Return all the serials associated with this process.""" |
| 709 | return self._serialnames |
| 710 | |
| 711 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 712 | def test(): |
| 713 | """Integration testing. |
| 714 | |
| 715 | TODO(tbroch) Enhance integration test and add unittest (see mox) |
| 716 | """ |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 717 | logging.basicConfig( |
| 718 | level=logging.DEBUG, |
| 719 | format='%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 720 | # configure server & listen |
| 721 | servod_obj = Servod(1) |
Wai-Hong Tam | 564c170 | 2017-04-24 09:23:38 -0700 | [diff] [blame] | 722 | # 5 == number of interfaces on a FT4232H device |
| 723 | for i in xrange(1, 5): |
| 724 | if i == 2: |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 725 | # its an i2c interface ... see __init__ for details and TODO to make |
| 726 | # this configureable |
| 727 | servod_obj._interface_list[i].wr_rd(0x21, [0], 1) |
| 728 | else: |
| 729 | # its a gpio interface |
| 730 | servod_obj._interface_list[i].wr_rd(0) |
| 731 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 732 | server = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 9999), |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 733 | allow_none=True) |
| 734 | server.register_introspection_functions() |
| 735 | server.register_multicall_functions() |
| 736 | server.register_instance(servod_obj) |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 737 | logging.info('Listening on localhost port 9999') |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 738 | server.serve_forever() |
| 739 | |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 740 | |
| 741 | if __name__ == '__main__': |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 742 | test() |
| 743 | |
| 744 | # simple client transaction would look like |
Puthikorn Voravootivat | 01ead15 | 2018-03-23 15:38:40 -0700 | [diff] [blame] | 745 | """remote_uri = 'http://localhost:9999' client = xmlrpclib.ServerProxy(remote_uri, verbose=False) send_str = "Hello_there" print "Sent " + send_str + ", Recv " + client.echo(send_str) |
| 746 | |
Todd Broch | e505b8d | 2011-03-21 18:19:54 -0700 | [diff] [blame] | 747 | """ |