@@ -18,6 +18,7 @@ use std::thread;
1818use std:: time:: Duration ;
1919
2020const REMOTE_ADDR_ENV : & str = "TEST_DEVICE_ADDR" ;
21+ const DEFAULT_ADDR : & str = "127.0.0.1:12345" ;
2122
2223macro_rules! t {
2324 ( $e: expr) => {
@@ -30,8 +31,12 @@ macro_rules! t {
3031
3132fn main ( ) {
3233 let mut args = env:: args ( ) . skip ( 1 ) ;
34+ let next = args. next ( ) ;
35+ if next. is_none ( ) {
36+ return help ( ) ;
37+ }
3338
34- match & args . next ( ) . unwrap ( ) [ ..] {
39+ match & next. unwrap ( ) [ ..] {
3540 "spawn-emulator" => spawn_emulator (
3641 & args. next ( ) . unwrap ( ) ,
3742 Path :: new ( & args. next ( ) . unwrap ( ) ) ,
@@ -40,12 +45,16 @@ fn main() {
4045 ) ,
4146 "push" => push ( Path :: new ( & args. next ( ) . unwrap ( ) ) ) ,
4247 "run" => run ( args. next ( ) . unwrap ( ) , args. collect ( ) ) ,
43- cmd => panic ! ( "unknown command: {}" , cmd) ,
48+ "help" | "-h" | "--help" => help ( ) ,
49+ cmd => {
50+ println ! ( "unknown command: {}" , cmd) ;
51+ help ( ) ;
52+ }
4453 }
4554}
4655
4756fn spawn_emulator ( target : & str , server : & Path , tmpdir : & Path , rootfs : Option < PathBuf > ) {
48- let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( "127.0.0.1:12345" . to_string ( ) ) ;
57+ let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( DEFAULT_ADDR . to_string ( ) ) ;
4958
5059 if env:: var ( REMOTE_ADDR_ENV ) . is_ok ( ) {
5160 println ! ( "Connecting to remote device {} ..." , device_address) ;
@@ -172,7 +181,7 @@ fn start_qemu_emulator(target: &str, rootfs: &Path, server: &Path, tmpdir: &Path
172181}
173182
174183fn push ( path : & Path ) {
175- let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( "127.0.0.1:12345" . to_string ( ) ) ;
184+ let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( DEFAULT_ADDR . to_string ( ) ) ;
176185 let client = t ! ( TcpStream :: connect( device_address) ) ;
177186 let mut client = BufWriter :: new ( client) ;
178187 t ! ( client. write_all( b"push" ) ) ;
@@ -189,7 +198,7 @@ fn push(path: &Path) {
189198}
190199
191200fn run ( files : String , args : Vec < String > ) {
192- let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( "127.0.0.1:12345" . to_string ( ) ) ;
201+ let device_address = env:: var ( REMOTE_ADDR_ENV ) . unwrap_or ( DEFAULT_ADDR . to_string ( ) ) ;
193202 let client = t ! ( TcpStream :: connect( device_address) ) ;
194203 let mut client = BufWriter :: new ( client) ;
195204 t ! ( client. write_all( b"run " ) ) ;
@@ -284,3 +293,40 @@ fn send(path: &Path, dst: &mut dyn Write) {
284293 t ! ( dst. write_all( & [ ( amt >> 24 ) as u8 , ( amt >> 16 ) as u8 , ( amt >> 8 ) as u8 , ( amt >> 0 ) as u8 , ] ) ) ;
285294 t ! ( io:: copy( & mut file, dst) ) ;
286295}
296+
297+ fn help ( ) {
298+ println ! (
299+ "
300+ Usage: {0} <command> [<args>]
301+
302+ Sub-commands:
303+ spawn-emulator <target> <server> <tmpdir> [rootfs] See below
304+ push <path> Copy <path> to emulator
305+ run <files> [args...] Run program on emulator
306+ help Display help message
307+
308+ Spawning an emulator:
309+
310+ For Android <target>s, adb will push the <server>, set up TCP forwarding and run
311+ the <server>. Otherwise qemu emulates the target using a rootfs image created in
312+ <tmpdir> and generated from <rootfs> plus the <server> executable.
313+ If {1} is set in the environment, this step is skipped.
314+
315+ Pushing a path to a running emulator:
316+
317+ A running emulator or adb device is connected to at the IP address and port in
318+ the {1} environment variable or {2} if this isn't
319+ specified. The file at <path> is sent to this target.
320+
321+ Executing commands on a running emulator:
322+
323+ First the target emulator/adb session is connected to as for pushing files. Next
324+ the colon separated list of <files> is pushed to the target. Finally, the first
325+ file in <files> is executed in the emulator, preserving the current environment.
326+ That command's status code is returned.
327+ " ,
328+ env:: args( ) . next( ) . unwrap( ) ,
329+ REMOTE_ADDR_ENV ,
330+ DEFAULT_ADDR
331+ ) ;
332+ }
0 commit comments