blob: c7751120e360d78e21462e20259cc7a3734578a1 [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
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +080011import os
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +080012import readline
13import rlcompleter
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +080014import subprocess
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +080015import xmlrpclib
16
17
18def ShowMessages(proxy):
19 """Shows the messages for usage.
20
21 Args:
22 proxy: The xmlrpclib.ServerProxy to chameleond.
23 """
24 logging.info('In interactive shell, p is the proxy to chameleond server')
25 supported_ports = proxy.GetSupportedPorts()
26 linein_port = None
27 hdmi_port = None
28 port_messages = []
29 for port in supported_ports:
30 port_type = proxy.GetConnectorType(port)
31 if port_type == 'LineIn':
32 linein_port = port
33 if port_type == 'HDMI':
34 hdmi_port = port
35 port_messages.append('Port %d is %s.' % (port, port_type))
36
37 logging.info('''
38 %s
39 E.g.
40 p.StartCapturingAudio(%d) to capture from LineIn.
41 p.StopCapturingAudio(%d) to stop capturing from LineIn.
42 p.Plug(%d) to plug HDMI.
43 p.UnPlug(%d) to unplug HDMI.''',
44 '\n '.join(port_messages), linein_port, linein_port,
45 hdmi_port, hdmi_port)
46
47
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +080048def StartInteractiveShell(p, options):
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +080049 """Starts an interactive shell.
50
51 Args:
52 p: The xmlrpclib.ServerProxy to chameleond.
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +080053 options: The namespace from argparse.
54
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +080055 """
56 vars = globals()
57 vars.update(locals())
58 readline.set_completer(rlcompleter.Completer(vars).complete)
59 readline.parse_and_bind("tab: complete")
60 shell = code.InteractiveConsole(vars)
61 shell.interact()
62
63
64def ParseArgs():
65 """Parses the arguments.
66
67 Returns: the namespace containing parsed arguments.
68
69 """
70 parser = argparse.ArgumentParser(
71 description='Connect to Chameleond and use interactive shell.',
72 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
73 parser.add_argument('--chameleon_host', type=str, dest='host', required=True,
74 help='host address of Chameleond')
75 parser.add_argument('--port', type=int, dest='port', default=9992,
76 help='port number of Chameleond')
77 return parser.parse_args()
78
79
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +080080def GetAndConvertRecordedFile(remote_path):
81 """Gets recorded file and converts it into a wav file.
82
83 A helper function to get recorded file from Chameleon host and do
84 file format conversion from 32 bit, 48000 rate, 8 channel raw file
85 to 2 channel wav file.
86
87 E.g.
88 >>> p.StartCapturingAudio(6)
89 >>> s = p.StopCapturingAudio(6)
90 >>> GetAndConvertRecordedFile(s[0])
91
92 The recorded raw file and converted wav file will be in current
93 directory.
94
95 Args:
96 remote_path: The file to copy from Chameleon host.
97
98 """
99 basename = os.path.basename(remote_path)
100 # options is already in the namespace.
101 subprocess.check_call(
102 ['scp', 'root@%s:%s' % (options.host, remote_path), basename])
103 subprocess.check_call(
104 ['sox', '-b', '32', '-r', '48000', '-c', '8', '-e', 'signed',
105 basename, '-c', '2', basename + '.wav'])
106
107
108def ConnectCrosToLineIn():
109 """Connects a audio bus path from Cros headphone to Chameleon LineIn."""
110 p.AudioBoardConnect(1, 'Cros device headphone')
111 p.AudioBoardConnect(1, 'Chameleon FPGA line-in')
112
113
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +0800114def Main():
115 """The Main program."""
116 logging.basicConfig(
117 format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG)
118
119 options = ParseArgs()
120
121 address = 'http://%s:%s' % (options.host, options.port)
122 proxy = xmlrpclib.ServerProxy(address)
123 logging.info('Connected to %s with MAC address %s',
124 address, proxy.GetMacAddress())
125 ShowMessages(proxy)
Cheng-Yi Chiangab747d82016-11-22 15:39:12 +0800126 StartInteractiveShell(proxy, options)
Cheng-Yi Chiang3f9cb972015-11-23 17:43:59 +0800127
128
129if __name__ == '__main__':
130 Main()