Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 1 | /* |
| 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include "sysdeps.h" |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame^] | 21 | #include <base/file.h> |
| 22 | #include <base/logging.h> |
| 23 | #include <base/strings.h> |
| 24 | #include <cutils/sockets.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 26 | #include "adb.h" |
| 27 | #include "adb_client.h" |
| 28 | |
| 29 | // Return the console port of the currently connected emulator (if any) or -1 if |
| 30 | // there is no emulator, and -2 if there is more than one. |
| 31 | static int adb_get_emulator_console_port(const char* serial) { |
| 32 | if (serial) { |
| 33 | // The user specified a serial number; is it an emulator? |
| 34 | int port; |
| 35 | return (sscanf(serial, "emulator-%d", &port) == 1) ? port : -1; |
| 36 | } |
| 37 | |
| 38 | // No specific device was given, so get the list of connected devices and |
| 39 | // search for emulators. If there's one, we'll take it. If there are more |
| 40 | // than one, that's an error. |
| 41 | std::string devices; |
| 42 | std::string error; |
| 43 | if (!adb_query("host:devices", &devices, &error)) { |
| 44 | fprintf(stderr, "error: no emulator connected: %s\n", error.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | return -1; |
| 46 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 47 | |
| 48 | int port; |
| 49 | size_t emulator_count = 0; |
| 50 | for (const auto& device : android::base::Split(devices, "\n")) { |
| 51 | if (sscanf(device.c_str(), "emulator-%d", &port) == 1) { |
| 52 | if (++emulator_count > 1) { |
| 53 | fprintf( |
| 54 | stderr, "error: more than one emulator detected; use -s\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (emulator_count == 0) { |
| 61 | fprintf(stderr, "error: no emulator detected\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | return port; |
| 66 | } |
| 67 | |
| 68 | static int connect_to_console(const char* serial) { |
| 69 | int port = adb_get_emulator_console_port(serial); |
| 70 | if (port == -1) { |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | int fd = socket_loopback_client(port, SOCK_STREAM); |
| 75 | if (fd == -1) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | fprintf(stderr, "error: could not connect to TCP port %d\n", port); |
| 77 | return -1; |
| 78 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 79 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 82 | int adb_send_emulator_command(int argc, const char** argv, const char* serial) { |
| 83 | int fd = connect_to_console(serial); |
| 84 | if (fd == -1) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | return 1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | } |
Spencer Low | 7dc759f | 2015-05-06 16:13:42 -0700 | [diff] [blame] | 87 | |
| 88 | for (int i = 1; i < argc; i++) { |
| 89 | adb_write(fd, argv[i], strlen(argv[i])); |
| 90 | adb_write(fd, i == argc - 1 ? "\n" : " ", 1); |
| 91 | } |
| 92 | |
| 93 | const char disconnect_command[] = "quit\n"; |
| 94 | if (adb_write(fd, disconnect_command, sizeof(disconnect_command) - 1) == -1) { |
| 95 | LOG(FATAL) << "Could not finalize emulator command"; |
| 96 | } |
| 97 | |
| 98 | // Drain output that the emulator console has sent us to prevent a problem |
| 99 | // on Windows where if adb closes the socket without reading all the data, |
| 100 | // the emulator's next call to recv() will have an ECONNABORTED error, |
| 101 | // preventing the emulator from reading the command that adb has sent. |
| 102 | // https://code.google.com/p/android/issues/detail?id=21021 |
| 103 | int result; |
| 104 | do { |
| 105 | char buf[BUFSIZ]; |
| 106 | result = adb_read(fd, buf, sizeof(buf)); |
| 107 | // Keep reading until zero bytes (EOF) or an error. If 'adb emu kill' |
| 108 | // is executed, the emulator calls exit() which causes adb to get |
| 109 | // ECONNRESET. Any other emu command is followed by the quit command |
| 110 | // that we sent above, and that causes the emulator to close the socket |
| 111 | // which should cause zero bytes (EOF) to be returned. |
| 112 | } while (result > 0); |
| 113 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | adb_close(fd); |
| 115 | |
| 116 | return 0; |
| 117 | } |