| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [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 |  | 
| Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB | 
| Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include "sysdeps.h" | 
|  | 20 | #include "adb_client.h" | 
|  | 21 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <errno.h> | 
|  | 23 | #include <limits.h> | 
|  | 24 | #include <stdarg.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 25 | #include <stdio.h> | 
|  | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include <sys/types.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 31 | #include <string> | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 32 | #include <vector> | 
|  | 33 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> | 
|  | 35 | #include <android-base/strings.h> | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 36 | #include <cutils/sockets.h> | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 37 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 38 | #include "adb_io.h" | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 39 | #include "adb_utils.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 |  | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 41 | static TransportType __adb_transport = kTransportAny; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | static const char* __adb_serial = NULL; | 
|  | 43 |  | 
| Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 44 | static int __adb_server_port = DEFAULT_ADB_PORT; | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 45 | static const char* __adb_server_name = NULL; | 
| Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 46 |  | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 47 | void adb_set_transport(TransportType type, const char* serial) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | { | 
|  | 49 | __adb_transport = type; | 
|  | 50 | __adb_serial = serial; | 
|  | 51 | } | 
|  | 52 |  | 
| Josh Gao | d262122 | 2016-03-04 15:16:18 -0800 | [diff] [blame] | 53 | void adb_get_transport(TransportType* type, const char** serial) { | 
|  | 54 | if (type) { | 
|  | 55 | *type = __adb_transport; | 
|  | 56 | } | 
|  | 57 | if (serial) { | 
|  | 58 | *serial = __adb_serial; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
| Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 62 | void adb_set_tcp_specifics(int server_port) | 
|  | 63 | { | 
|  | 64 | __adb_server_port = server_port; | 
|  | 65 | } | 
|  | 66 |  | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 67 | void adb_set_tcp_name(const char* hostname) | 
|  | 68 | { | 
|  | 69 | __adb_server_name = hostname; | 
|  | 70 | } | 
|  | 71 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 72 | static int switch_socket_transport(int fd, std::string* error) { | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 73 | std::string service; | 
|  | 74 | if (__adb_serial) { | 
|  | 75 | service += "host:transport:"; | 
|  | 76 | service += __adb_serial; | 
|  | 77 | } else { | 
| Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 78 | const char* transport_type = "???"; | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 79 | switch (__adb_transport) { | 
|  | 80 | case kTransportUsb: | 
|  | 81 | transport_type = "transport-usb"; | 
|  | 82 | break; | 
|  | 83 | case kTransportLocal: | 
|  | 84 | transport_type = "transport-local"; | 
|  | 85 | break; | 
|  | 86 | case kTransportAny: | 
|  | 87 | transport_type = "transport-any"; | 
|  | 88 | break; | 
|  | 89 | case kTransportHost: | 
|  | 90 | // no switch necessary | 
|  | 91 | return 0; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | } | 
| Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 93 | service += "host:"; | 
|  | 94 | service += transport_type; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 97 | if (!SendProtocolString(fd, service)) { | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 98 | *error = perror_str("write failure during connection"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | adb_close(fd); | 
|  | 100 | return -1; | 
|  | 101 | } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 102 | D("Switch transport in progress"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 104 | if (!adb_status(fd, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | adb_close(fd); | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 106 | D("Switch transport failed: %s", error->c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | return -1; | 
|  | 108 | } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 109 | D("Switch transport success"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 113 | bool adb_status(int fd, std::string* error) { | 
|  | 114 | char buf[5]; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 115 | if (!ReadFdExactly(fd, buf, 4)) { | 
|  | 116 | *error = perror_str("protocol fault (couldn't read status)"); | 
|  | 117 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 120 | if (!memcmp(buf, "OKAY", 4)) { | 
|  | 121 | return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 124 | if (memcmp(buf, "FAIL", 4)) { | 
|  | 125 | *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)", | 
|  | 126 | buf[0], buf[1], buf[2], buf[3]); | 
|  | 127 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 130 | ReadProtocolString(fd, error, error); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 131 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 134 | int _adb_connect(const std::string& service, std::string* error) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 135 | D("_adb_connect: %s", service.c_str()); | 
| Josh Gao | 7e6683c | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 136 | if (service.empty() || service.size() > MAX_PAYLOAD_V1) { | 
| Spencer Low | 6001c87 | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 137 | *error = android::base::StringPrintf("bad service name length (%zd)", | 
|  | 138 | service.size()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | return -1; | 
|  | 140 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 142 | int fd; | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 143 | std::string reason; | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 144 | if (__adb_server_name) { | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 145 | fd = network_connect(__adb_server_name, __adb_server_port, SOCK_STREAM, 0, &reason); | 
|  | 146 | if (fd == -1) { | 
|  | 147 | *error = android::base::StringPrintf("can't connect to %s:%d: %s", | 
|  | 148 | __adb_server_name, __adb_server_port, | 
|  | 149 | reason.c_str()); | 
|  | 150 | return -2; | 
|  | 151 | } | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 152 | } else { | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 153 | fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason); | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 154 | if (fd == -1) { | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 155 | *error = android::base::StringPrintf("cannot connect to daemon: %s", | 
|  | 156 | reason.c_str()); | 
| Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 157 | return -2; | 
|  | 158 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 161 | if (memcmp(&service[0],"host",4) != 0 && switch_socket_transport(fd, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | return -1; | 
|  | 163 | } | 
|  | 164 |  | 
| Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 165 | if (!SendProtocolString(fd, service)) { | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 166 | *error = perror_str("write failure during connection"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 | adb_close(fd); | 
|  | 168 | return -1; | 
|  | 169 | } | 
|  | 170 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 171 | if (!adb_status(fd, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | adb_close(fd); | 
|  | 173 | return -1; | 
|  | 174 | } | 
|  | 175 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 176 | D("_adb_connect: return fd %d", fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | return fd; | 
|  | 178 | } | 
|  | 179 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 180 | int adb_connect(const std::string& service, std::string* error) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | // first query the adb server's version | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 182 | int fd = _adb_connect("host:version", error); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 |  | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 184 | D("adb_connect: service %s", service.c_str()); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 185 | if (fd == -2 && __adb_server_name) { | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 186 | fprintf(stderr,"** Cannot start server on remote host\n"); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 187 | // error is the original network connection error | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 188 | return fd; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 189 | } else if (fd == -2) { | 
| Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 190 | fprintf(stdout,"* daemon not running. starting it now on port %d *\n", | 
|  | 191 | __adb_server_port); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | start_server: | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 193 | if (launch_server(__adb_server_port)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | fprintf(stderr,"* failed to start daemon *\n"); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 195 | // launch_server() has already printed detailed error info, so just | 
|  | 196 | // return a generic error string about the overall adb_connect() | 
|  | 197 | // that the caller requested. | 
|  | 198 | *error = "cannot connect to daemon"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | return -1; | 
|  | 200 | } else { | 
|  | 201 | fprintf(stdout,"* daemon started successfully *\n"); | 
|  | 202 | } | 
|  | 203 | /* give the server some time to start properly and detect devices */ | 
| Xavier Ducrohet | a481d09 | 2009-05-21 17:47:43 -0700 | [diff] [blame] | 204 | adb_sleep_ms(3000); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | // fall through to _adb_connect | 
|  | 206 | } else { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 207 | // If a server is already running, check its version matches. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | int version = ADB_SERVER_VERSION - 1; | 
|  | 209 |  | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 210 | // If we have a file descriptor, then parse version result. | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 211 | if (fd >= 0) { | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 212 | std::string version_string; | 
|  | 213 | if (!ReadProtocolString(fd, &version_string, error)) { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 214 | adb_close(fd); | 
|  | 215 | return -1; | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 216 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 |  | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 218 | ReadOrderlyShutdown(fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 219 | adb_close(fd); | 
|  | 220 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 221 | if (sscanf(&version_string[0], "%04x", &version) != 1) { | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 222 | *error = android::base::StringPrintf("cannot parse version string: %s", | 
|  | 223 | version_string.c_str()); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 224 | return -1; | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 225 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | } else { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 227 | // If fd is -1 check for "unknown host service" which would | 
|  | 228 | // indicate a version of adb that does not support the | 
| Spencer Low | 71635bb | 2015-08-05 19:26:50 -0700 | [diff] [blame] | 229 | // version command, in which case we should fall-through to kill it. | 
|  | 230 | if (*error != "unknown host service") { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | return fd; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 232 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 235 | if (version != ADB_SERVER_VERSION) { | 
| Elliott Hughes | 5b73a10 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 236 | printf("adb server version (%d) doesn't match this client (%d); killing...\n", | 
|  | 237 | version, ADB_SERVER_VERSION); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 238 | fd = _adb_connect("host:kill", error); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 239 | if (fd >= 0) { | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 240 | ReadOrderlyShutdown(fd); | 
| Spencer Low | f18fc08 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 241 | adb_close(fd); | 
|  | 242 | } else { | 
|  | 243 | // If we couldn't connect to the server or had some other error, | 
|  | 244 | // report it, but still try to start the server. | 
|  | 245 | fprintf(stderr, "error: %s\n", error->c_str()); | 
|  | 246 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 |  | 
|  | 248 | /* XXX can we better detect its death? */ | 
|  | 249 | adb_sleep_ms(2000); | 
|  | 250 | goto start_server; | 
|  | 251 | } | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | // if the command is start-server, we are done. | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 255 | if (service == "host:start-server") { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | return 0; | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 257 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 |  | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 259 | fd = _adb_connect(service, error); | 
|  | 260 | if (fd == -1) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 261 | D("_adb_connect error: %s", error->c_str()); | 
| Brian Carlstrom | 93c91fa | 2013-10-18 13:58:48 -0700 | [diff] [blame] | 262 | } else if(fd == -2) { | 
| Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 263 | fprintf(stderr,"** daemon still not running\n"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 264 | } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 265 | D("adb_connect: return fd %d", fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 |  | 
|  | 267 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
|  | 270 |  | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 271 | bool adb_command(const std::string& service) { | 
|  | 272 | std::string error; | 
|  | 273 | int fd = adb_connect(service, &error); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 274 | if (fd < 0) { | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 275 | fprintf(stderr, "error: %s\n", error.c_str()); | 
|  | 276 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 279 | if (!adb_status(fd, &error)) { | 
|  | 280 | fprintf(stderr, "error: %s\n", error.c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 281 | adb_close(fd); | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 282 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 285 | ReadOrderlyShutdown(fd); | 
|  | 286 | adb_close(fd); | 
| Elliott Hughes | 424af02 | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 287 | return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | } | 
|  | 289 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 290 | bool adb_query(const std::string& service, std::string* result, std::string* error) { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 291 | D("adb_query: %s", service.c_str()); | 
| Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 292 | int fd = adb_connect(service, error); | 
|  | 293 | if (fd < 0) { | 
| Elliott Hughes | 3d5f60d | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 294 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 297 | result->clear(); | 
|  | 298 | if (!ReadProtocolString(fd, result, error)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | adb_close(fd); | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 300 | return false; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | } | 
| Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 302 |  | 
|  | 303 | ReadOrderlyShutdown(fd); | 
|  | 304 | adb_close(fd); | 
| Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 305 | return true; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 306 | } | 
| Josh Gao | f3f6a1d | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 307 |  | 
|  | 308 | std::string format_host_command(const char* command, TransportType type, const char* serial) { | 
|  | 309 | if (serial) { | 
|  | 310 | return android::base::StringPrintf("host-serial:%s:%s", serial, command); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | const char* prefix = "host"; | 
|  | 314 | if (type == kTransportUsb) { | 
|  | 315 | prefix = "host-usb"; | 
|  | 316 | } else if (type == kTransportLocal) { | 
|  | 317 | prefix = "host-local"; | 
|  | 318 | } | 
|  | 319 | return android::base::StringPrintf("%s:%s", prefix, command); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) { | 
|  | 323 | std::string result; | 
|  | 324 | if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) { | 
|  | 325 | *feature_set = StringToFeatureSet(result); | 
|  | 326 | return true; | 
|  | 327 | } | 
|  | 328 | feature_set->clear(); | 
|  | 329 | return false; | 
|  | 330 | } |