Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 1 | #!/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 | |
| 8 | import argparse |
| 9 | import code |
| 10 | import logging |
Cheng-Yi Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 11 | import os |
Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 12 | import readline |
| 13 | import rlcompleter |
Cheng-Yi Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 14 | import subprocess |
Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 15 | import xmlrpclib |
| 16 | |
| 17 | |
| 18 | def 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 Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 48 | def StartInteractiveShell(p, options): |
Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 49 | """Starts an interactive shell. |
| 50 | |
| 51 | Args: |
| 52 | p: The xmlrpclib.ServerProxy to chameleond. |
Cheng-Yi Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 53 | options: The namespace from argparse. |
| 54 | |
Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 55 | """ |
| 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 | |
| 64 | def 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 Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 80 | def 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 | |
| 108 | def 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 Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 114 | def 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 Chiang | ab747d8 | 2016-11-22 15:39:12 +0800 | [diff] [blame^] | 126 | StartInteractiveShell(proxy, options) |
Cheng-Yi Chiang | 3f9cb97 | 2015-11-23 17:43:59 +0800 | [diff] [blame] | 127 | |
| 128 | |
| 129 | if __name__ == '__main__': |
| 130 | Main() |