3 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Command line tool for forwarding ports from a device to the host.
9 Allows an Android device to connect to services running on the host machine,
10 i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder|
19 from pylib
import constants
20 from pylib
import forwarder
21 from pylib
.device
import adb_wrapper
22 from pylib
.device
import device_filter
23 from pylib
.device
import device_utils
24 from pylib
.utils
import run_tests_helper
28 parser
= optparse
.OptionParser(usage
='Usage: %prog [options] device_port '
29 'host_port [device_port_2 host_port_2] ...',
31 parser
.add_option('-v',
36 help='Verbose level (multiple times for more)')
37 parser
.add_option('--device',
38 help='Serial number of device we should use.')
39 parser
.add_option('--debug', action
='store_const', const
='Debug',
40 dest
='build_type', default
='Release',
41 help='Use Debug build of host tools instead of Release.')
43 options
, args
= parser
.parse_args(argv
)
44 run_tests_helper
.SetLogLevel(options
.verbose_count
)
46 if len(args
) < 2 or not len(args
) % 2:
47 parser
.error('Need even number of port pairs')
51 port_pairs
= map(int, args
[1:])
52 port_pairs
= zip(port_pairs
[::2], port_pairs
[1::2])
54 parser
.error('Bad port number')
57 devices
= adb_wrapper
.AdbWrapper
.Devices(
58 filters
=device_filter
.DefaultFilters())
61 if options
.device
not in [d
.GetDeviceSerial() for d
in devices
]:
62 raise Exception('Error: %s not in attached devices %s' % (options
.device
,
64 devices
= [options
.device
]
67 raise Exception('Error: no connected devices')
68 logging
.info('No device specified. Defaulting to %s', devices
[0])
70 device
= device_utils
.DeviceUtils(devices
[0])
71 constants
.SetBuildType(options
.build_type
)
73 forwarder
.Forwarder
.Map(port_pairs
, device
)
76 except KeyboardInterrupt:
79 forwarder
.Forwarder
.UnmapAllDevicePorts(device
)
81 if __name__
== '__main__':