console.c revision 4f6e8d7a00cbeda1e70cc15be9c4af1018bdad53
1#include "sysdeps.h"
2#include "adb.h"
3#include "adb_client.h"
4#include <stdio.h>
5
6static int  connect_to_console(void)
7{
8    int  fd, port;
9
10    port = adb_get_emulator_console_port();
11    if (port < 0) {
12        if (port == -2)
13            fprintf(stderr, "error: more than one emulator detected. use -s option\n");
14        else
15            fprintf(stderr, "error: no emulator detected\n");
16        return -1;
17    }
18    fd = socket_loopback_client( port, SOCK_STREAM );
19    if (fd < 0) {
20        fprintf(stderr, "error: could not connect to TCP port %d\n", port);
21        return -1;
22    }
23    return  fd;
24}
25
26
27int  adb_send_emulator_command(int  argc, char**  argv)
28{
29    int   fd, nn;
30
31    fd = connect_to_console();
32    if (fd < 0)
33        return 1;
34
35#define  QUIT  "quit\n"
36
37    for (nn = 1; nn < argc; nn++) {
38        adb_write( fd, argv[nn], strlen(argv[nn]) );
39        adb_write( fd, (nn == argc-1) ? "\n" : " ", 1 );
40    }
41    adb_write( fd, QUIT, sizeof(QUIT)-1 );
42    adb_close(fd);
43
44    return 0;
45}
46