summaryrefslogtreecommitdiffstats
path: root/adb/console.cpp
blob: 452ee4103019bb2f89af419c6eb1ffb587d30092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "sysdeps.h"
#include "adb.h"
#include "adb_client.h"
#include <stdio.h>

static int  connect_to_console(void)
{
    int  fd, port;

    port = adb_get_emulator_console_port();
    if (port < 0) {
        if (port == -2)
            fprintf(stderr, "error: more than one emulator detected. use -s option\n");
        else
            fprintf(stderr, "error: no emulator detected\n");
        return -1;
    }
    fd = socket_loopback_client( port, SOCK_STREAM );
    if (fd < 0) {
        fprintf(stderr, "error: could not connect to TCP port %d\n", port);
        return -1;
    }
    return  fd;
}


int  adb_send_emulator_command(int  argc, const char**  argv)
{
    int   fd, nn;

    fd = connect_to_console();
    if (fd < 0)
        return 1;

#define  QUIT  "quit\n"

    for (nn = 1; nn < argc; nn++) {
        adb_write( fd, argv[nn], strlen(argv[nn]) );
        adb_write( fd, (nn == argc-1) ? "\n" : " ", 1 );
    }
    adb_write( fd, QUIT, sizeof(QUIT)-1 );
    adb_close(fd);

    return 0;
}