blob: 07079609c953e74cfeab19612d6e0031a4fa443d [file] [log] [blame]
Spencer Low142ec752015-05-06 16:13:42 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include "sysdeps.h"
Spencer Low142ec752015-05-06 16:13:42 -070018
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <stdio.h>
20
Spencer Low142ec752015-05-06 16:13:42 -070021#include "base/file.h"
22#include "base/logging.h"
23#include "base/strings.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024
Spencer Low142ec752015-05-06 16:13:42 -070025#include "adb.h"
26#include "adb_client.h"
27
28// Return the console port of the currently connected emulator (if any) or -1 if
29// there is no emulator, and -2 if there is more than one.
30static int adb_get_emulator_console_port(const char* serial) {
31 if (serial) {
32 // The user specified a serial number; is it an emulator?
33 int port;
34 return (sscanf(serial, "emulator-%d", &port) == 1) ? port : -1;
35 }
36
37 // No specific device was given, so get the list of connected devices and
38 // search for emulators. If there's one, we'll take it. If there are more
39 // than one, that's an error.
40 std::string devices;
41 std::string error;
42 if (!adb_query("host:devices", &devices, &error)) {
43 fprintf(stderr, "error: no emulator connected: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044 return -1;
45 }
Spencer Low142ec752015-05-06 16:13:42 -070046
47 int port;
48 size_t emulator_count = 0;
49 for (const auto& device : android::base::Split(devices, "\n")) {
50 if (sscanf(device.c_str(), "emulator-%d", &port) == 1) {
51 if (++emulator_count > 1) {
52 fprintf(
53 stderr, "error: more than one emulator detected; use -s\n");
54 return -1;
55 }
56 }
57 }
58
59 if (emulator_count == 0) {
60 fprintf(stderr, "error: no emulator detected\n");
61 return -1;
62 }
63
64 return port;
65}
66
67static int connect_to_console(const char* serial) {
68 int port = adb_get_emulator_console_port(serial);
69 if (port == -1) {
70 return -1;
71 }
72
73 int fd = socket_loopback_client(port, SOCK_STREAM);
74 if (fd == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 fprintf(stderr, "error: could not connect to TCP port %d\n", port);
76 return -1;
77 }
Spencer Low142ec752015-05-06 16:13:42 -070078 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079}
80
Spencer Low142ec752015-05-06 16:13:42 -070081int adb_send_emulator_command(int argc, const char** argv, const char* serial) {
82 int fd = connect_to_console(serial);
83 if (fd == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 }
Spencer Low142ec752015-05-06 16:13:42 -070086
87 for (int i = 1; i < argc; i++) {
88 adb_write(fd, argv[i], strlen(argv[i]));
89 adb_write(fd, i == argc - 1 ? "\n" : " ", 1);
90 }
91
92 const char disconnect_command[] = "quit\n";
93 if (adb_write(fd, disconnect_command, sizeof(disconnect_command) - 1) == -1) {
94 LOG(FATAL) << "Could not finalize emulator command";
95 }
96
97 // Drain output that the emulator console has sent us to prevent a problem
98 // on Windows where if adb closes the socket without reading all the data,
99 // the emulator's next call to recv() will have an ECONNABORTED error,
100 // preventing the emulator from reading the command that adb has sent.
101 // https://code.google.com/p/android/issues/detail?id=21021
102 int result;
103 do {
104 char buf[BUFSIZ];
105 result = adb_read(fd, buf, sizeof(buf));
106 // Keep reading until zero bytes (EOF) or an error. If 'adb emu kill'
107 // is executed, the emulator calls exit() which causes adb to get
108 // ECONNRESET. Any other emu command is followed by the quit command
109 // that we sent above, and that causes the emulator to close the socket
110 // which should cause zero bytes (EOF) to be returned.
111 } while (result > 0);
112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 adb_close(fd);
114
115 return 0;
116}