1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
8 from devil
.utils
import cmd_helper
10 logger
= logging
.getLogger(__name__
)
12 _COULDNT_OPEN_ERROR_RE
= re
.compile(r
'Couldn\'t
open device
.*')
13 _INDENTATION_RE = re.compile(r'^
( *)')
14 _LSUSB_BUS_DEVICE_RE = re.compile(r'^
Bus (\d{3}
) Device (\d{3}
): (.*)')
15 _LSUSB_ENTRY_RE = re.compile(r'^
*([^
]+) +([^
]+) *([^
].*)?$
')
16 _LSUSB_GROUP_RE = re.compile(r'^
*([^
]+.*):$
')
19 def _lsusbv_on_device(bus_id, dev_id):
20 """Calls lsusb -v on device."""
21 _, raw_output = cmd_helper.GetCmdStatusAndOutputWithTimeout(
22 ['lsusb
', '-v
', '-s
', '%s:%s' % (bus_id, dev_id)], timeout=10)
24 device = {'bus
': bus_id, 'device
': dev_id}
25 depth_stack = [device]
27 # This builds a nested dict -- a tree, basically -- that corresponds
28 # to the lsusb output. It looks first for a line containing
30 # "Bus <bus number> Device <device number>: ..."
32 # and uses that to create the root node. It then parses all remaining
33 # lines as a tree, with the indentation level determining the
34 # depth of the new node.
36 # This expects two kinds of lines:
37 # - "groups", which take the form
39 # and typically have children, and
40 # - "entries", which take the form
41 # "<entry name> <entry value> <possible entry description>"
42 # and typically do not have children (but can).
44 # This maintains a stack containing all current ancestor nodes in
45 # order to add new nodes to the proper place in the tree.
46 # The stack is added to when a new node is parsed. Nodes are removed
47 # from the stack when they are either at the same indentation level as
48 # or a deeper indentation level than the current line.
50 # e.g. the following lsusb output:
52 # Bus 123 Device 456: School bus
54 # bDeviceClass 5 Actual School Bus
55 # Configuration Descriptor:
58 # would produce the following dict:
63 # 'desc
': 'School bus
',
64 # 'Device Descriptor
': {
67 # '_desc
': 'Actual School Bus
',
69 # 'Configuration Descriptor
': {
77 for line in raw_output.splitlines():
81 # Filter out error mesage about opening device.
82 if _COULDNT_OPEN_ERROR_RE.match(line):
84 # Find start of device information.
85 m = _LSUSB_BUS_DEVICE_RE.match(line)
87 if m.group(1) != bus_id:
89 'Expected bus_id value
: %r, seen
%r', bus_id, m.group(1))
90 if m.group(2) != dev_id:
92 'Expected dev_id value
: %r, seen
%r', dev_id, m.group(2))
93 device['desc
'] = m.group(3)
96 # Skip any lines that aren't indented
, as they
're not part of the
98 indent_match = _INDENTATION_RE.match(line)
102 # Determine the indentation depth.
103 depth = 1 + len(indent_match.group(1)) / 2
104 if depth > len(depth_stack):
106 'lsusb parsing error
: unexpected indentation
: "%s"', line)
109 # Pop everything off the depth stack that isn't a parent of
111 while depth
< len(depth_stack
):
114 cur
= depth_stack
[-1]
116 m
= _LSUSB_GROUP_RE
.match(line
)
119 cur
[m
.group(1)] = new_group
120 depth_stack
.append(new_group
)
123 m
= _LSUSB_ENTRY_RE
.match(line
)
126 '_value': m
.group(2),
129 cur
[m
.group(1)] = new_entry
130 depth_stack
.append(new_entry
)
133 logger
.error('lsusb parsing error: unrecognized line: "%s"', line
)
138 """Call lsusb and return the parsed output."""
139 _
, lsusb_list_output
= cmd_helper
.GetCmdStatusAndOutputWithTimeout(
140 ['lsusb'], timeout
=10)
142 for line
in lsusb_list_output
.splitlines():
143 m
= _LSUSB_BUS_DEVICE_RE
.match(line
)
148 devices
.append(_lsusbv_on_device(bus_num
, dev_num
))
149 except cmd_helper
.TimeoutError
:
150 # Will be blacklisted if it is in expected device file, but times out.
151 logger
.info('lsusb -v %s:%s timed out.', bus_num
, dev_num
)
155 return cmd_helper
.GetCmdOutput(['lsusb'])
157 def get_lsusb_serial(device
):
159 return device
['Device Descriptor']['iSerial']['_desc']
163 def _is_android_device(device
):
165 # Hubs are not android devices.
166 if device
['Device Descriptor']['bDeviceClass']['_value'] == '9':
170 return get_lsusb_serial(device
) is not None
172 def get_android_devices():
173 android_devices
= (d
for d
in lsusb() if _is_android_device(d
))
174 return [get_lsusb_serial(d
) for d
in android_devices
]