blob: 2a4857fd65f4fa8626dcfefe011e3526281803fc [file] [log] [blame]
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +08001#!/usr/bin/python
2# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""A simple utility to connect to Chameleond in an interactive shell."""
7
8import argparse
9import code
10import logging
11import readline
12import rlcompleter
13import xmlrpclib
14
15
16def ShowMessages(proxy):
17 """Shows the messages for usage.
18
19 Args:
20 proxy: The xmlrpclib.ServerProxy to chameleond.
21 """
22 logging.info('In interactive shell, p is the proxy to chameleond server')
23 supported_ports = proxy.GetSupportedPorts()
24 linein_port = None
25 hdmi_port = None
26 port_messages = []
27 for port in supported_ports:
28 port_type = proxy.GetConnectorType(port)
29 if port_type == 'LineIn':
30 linein_port = port
31 if port_type == 'HDMI':
32 hdmi_port = port
33 port_messages.append('Port %d is %s.' % (port, port_type))
34
35 logging.info('''
36 %s
37 E.g.
38 p.StartCapturingAudio(%d) to capture from LineIn.
39 p.StopCapturingAudio(%d) to stop capturing from LineIn.
40 p.Plug(%d) to plug HDMI.
41 p.UnPlug(%d) to unplug HDMI.''',
42 '\n '.join(port_messages), linein_port, linein_port,
43 hdmi_port, hdmi_port)
44
45
46def StartInteractiveShell(p):
47 """Starts an interactive shell.
48
49 Args:
50 p: The xmlrpclib.ServerProxy to chameleond.
51 """
52 vars = globals()
53 vars.update(locals())
54 readline.set_completer(rlcompleter.Completer(vars).complete)
55 readline.parse_and_bind("tab: complete")
56 shell = code.InteractiveConsole(vars)
57 shell.interact()
58
59
60def ParseArgs():
61 """Parses the arguments.
62
63 Returns: the namespace containing parsed arguments.
64
65 """
66 parser = argparse.ArgumentParser(
67 description='Connect to Chameleond and use interactive shell.',
68 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
69 parser.add_argument('--chameleon_host', type=str, dest='host', required=True,
70 help='host address of Chameleond')
71 parser.add_argument('--port', type=int, dest='port', default=9992,
72 help='port number of Chameleond')
73 return parser.parse_args()
74
75
76def Main():
77 """The Main program."""
78 logging.basicConfig(
79 format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG)
80
81 options = ParseArgs()
82
83 address = 'http://%s:%s' % (options.host, options.port)
84 proxy = xmlrpclib.ServerProxy(address)
85 logging.info('Connected to %s with MAC address %s',
86 address, proxy.GetMacAddress())
87 ShowMessages(proxy)
88 StartInteractiveShell(proxy)
89
90
91if __name__ == '__main__':
92 Main()