blob: a27dd4750853eed1d0ae7c435e84186cecaa577a [file] [log] [blame]
Dan Albert76649012015-02-24 15:51:19 -08001/*
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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG ADB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20#include "adb_client.h"
21
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <errno.h>
23#include <limits.h>
24#include <stdarg.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
Dan Albert76649012015-02-24 15:51:19 -080029#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Elliott Hughes9309ecb2015-04-27 14:20:17 -070031#include <string>
Elliott Hughes078f0fc2015-04-29 08:35:59 -070032#include <vector>
33
Elliott Hughes4f713192015-12-04 22:00:26 -080034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070036#include <cutils/sockets.h>
Elliott Hughes9309ecb2015-04-27 14:20:17 -070037
Dan Albertcc731cc2015-02-24 21:26:58 -080038#include "adb_io.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070039#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Elliott Hughes3bd73c12015-05-05 13:10:43 -070041static TransportType __adb_transport = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042static const char* __adb_serial = NULL;
43
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010044static int __adb_server_port = DEFAULT_ADB_PORT;
Matt Gumbeld7b33082012-11-14 10:16:17 -080045static const char* __adb_server_name = NULL;
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +010046
Elliott Hughes3bd73c12015-05-05 13:10:43 -070047void adb_set_transport(TransportType type, const char* serial)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048{
49 __adb_transport = type;
50 __adb_serial = serial;
51}
52
Josh Gao06c73ae2016-03-04 15:16:18 -080053void 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 Hilzingerd0eacb82010-04-19 12:21:12 +010062void adb_set_tcp_specifics(int server_port)
63{
64 __adb_server_port = server_port;
65}
66
Matt Gumbeld7b33082012-11-14 10:16:17 -080067void adb_set_tcp_name(const char* hostname)
68{
69 __adb_server_name = hostname;
70}
71
Elliott Hughes078f0fc2015-04-29 08:35:59 -070072static int switch_socket_transport(int fd, std::string* error) {
Elliott Hughes9309ecb2015-04-27 14:20:17 -070073 std::string service;
74 if (__adb_serial) {
75 service += "host:transport:";
76 service += __adb_serial;
77 } else {
Dan Albertbac34742015-02-25 17:51:28 -080078 const char* transport_type = "???";
Elliott Hughes9309ecb2015-04-27 14:20:17 -070079 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 Projectdd7bc332009-03-03 19:32:55 -080092 }
Elliott Hughes9309ecb2015-04-27 14:20:17 -070093 service += "host:";
94 service += transport_type;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096
Elliott Hughese67f1f82015-04-30 17:32:03 -070097 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -070098 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 adb_close(fd);
100 return -1;
101 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700102 D("Switch transport in progress");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700104 if (!adb_status(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 adb_close(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700106 D("Switch transport failed: %s", error->c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 return -1;
108 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700109 D("Switch transport success");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 return 0;
111}
112
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700113bool adb_status(int fd, std::string* error) {
114 char buf[5];
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700115 if (!ReadFdExactly(fd, buf, 4)) {
116 *error = perror_str("protocol fault (couldn't read status)");
117 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 }
119
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700120 if (!memcmp(buf, "OKAY", 4)) {
121 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 }
123
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700124 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 Projectdd7bc332009-03-03 19:32:55 -0800128 }
129
Elliott Hughes6452a892015-04-29 12:28:13 -0700130 ReadProtocolString(fd, error, error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700131 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132}
133
Elliott Hughes6452a892015-04-29 12:28:13 -0700134int _adb_connect(const std::string& service, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700135 D("_adb_connect: %s", service.c_str());
Josh Gao7e6683c2016-01-15 14:35:54 -0800136 if (service.empty() || service.size() > MAX_PAYLOAD_V1) {
Spencer Low6001c872015-05-13 00:02:55 -0700137 *error = android::base::StringPrintf("bad service name length (%zd)",
138 service.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 return -1;
140 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
Elliott Hughes6452a892015-04-29 12:28:13 -0700142 int fd;
Spencer Low5200c662015-07-30 23:07:55 -0700143 std::string reason;
Elliott Hughes6452a892015-04-29 12:28:13 -0700144 if (__adb_server_name) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700145 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 Hughes6452a892015-04-29 12:28:13 -0700152 } else {
Spencer Low5200c662015-07-30 23:07:55 -0700153 fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason);
Elliott Hughes381cfa92015-07-23 17:12:58 -0700154 if (fd == -1) {
Spencer Low5200c662015-07-30 23:07:55 -0700155 *error = android::base::StringPrintf("cannot connect to daemon: %s",
156 reason.c_str());
Elliott Hughes381cfa92015-07-23 17:12:58 -0700157 return -2;
158 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
160
Yabin Cui03468c82016-04-05 13:50:44 -0700161 if ((memcmp(&service[0],"host",4) != 0 || service == "host:reconnect") &&
162 switch_socket_transport(fd, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 return -1;
164 }
165
Elliott Hughesaa245492015-08-03 10:38:08 -0700166 if (!SendProtocolString(fd, service)) {
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700167 *error = perror_str("write failure during connection");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 adb_close(fd);
169 return -1;
170 }
171
Yabin Cui03468c82016-04-05 13:50:44 -0700172 if (service != "reconnect") {
173 if (!adb_status(fd, error)) {
174 adb_close(fd);
175 return -1;
176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700179 D("_adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return fd;
181}
182
Elliott Hughes6452a892015-04-29 12:28:13 -0700183int adb_connect(const std::string& service, std::string* error) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 // first query the adb server's version
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700185 int fd = _adb_connect("host:version", error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700187 D("adb_connect: service %s", service.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700188 if (fd == -2 && __adb_server_name) {
Matt Gumbeld7b33082012-11-14 10:16:17 -0800189 fprintf(stderr,"** Cannot start server on remote host\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700190 // error is the original network connection error
Matt Gumbeld7b33082012-11-14 10:16:17 -0800191 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700192 } else if (fd == -2) {
Stefan Hilzingerd0eacb82010-04-19 12:21:12 +0100193 fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
194 __adb_server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 start_server:
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700196 if (launch_server(__adb_server_port)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 fprintf(stderr,"* failed to start daemon *\n");
Spencer Lowf18fc082015-08-11 17:05:02 -0700198 // launch_server() has already printed detailed error info, so just
199 // return a generic error string about the overall adb_connect()
200 // that the caller requested.
201 *error = "cannot connect to daemon";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return -1;
203 } else {
204 fprintf(stdout,"* daemon started successfully *\n");
205 }
206 /* give the server some time to start properly and detect devices */
Xavier Ducroheta481d092009-05-21 17:47:43 -0700207 adb_sleep_ms(3000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 // fall through to _adb_connect
209 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700210 // If a server is already running, check its version matches.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 int version = ADB_SERVER_VERSION - 1;
212
Elliott Hughes5b73a102015-10-02 19:49:10 -0700213 // If we have a file descriptor, then parse version result.
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700214 if (fd >= 0) {
Elliott Hughes6452a892015-04-29 12:28:13 -0700215 std::string version_string;
216 if (!ReadProtocolString(fd, &version_string, error)) {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700217 adb_close(fd);
218 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220
Spencer Low351ecd12015-10-14 17:32:44 -0700221 ReadOrderlyShutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 adb_close(fd);
223
Elliott Hughes6452a892015-04-29 12:28:13 -0700224 if (sscanf(&version_string[0], "%04x", &version) != 1) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700225 *error = android::base::StringPrintf("cannot parse version string: %s",
226 version_string.c_str());
Spencer Lowf18fc082015-08-11 17:05:02 -0700227 return -1;
Elliott Hughes6452a892015-04-29 12:28:13 -0700228 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 } else {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700230 // If fd is -1 check for "unknown host service" which would
231 // indicate a version of adb that does not support the
Spencer Low71635bb2015-08-05 19:26:50 -0700232 // version command, in which case we should fall-through to kill it.
233 if (*error != "unknown host service") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 return fd;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700235 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
237
Elliott Hughes6452a892015-04-29 12:28:13 -0700238 if (version != ADB_SERVER_VERSION) {
Elliott Hughes5b73a102015-10-02 19:49:10 -0700239 printf("adb server version (%d) doesn't match this client (%d); killing...\n",
240 version, ADB_SERVER_VERSION);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700241 fd = _adb_connect("host:kill", error);
Spencer Lowf18fc082015-08-11 17:05:02 -0700242 if (fd >= 0) {
Spencer Low351ecd12015-10-14 17:32:44 -0700243 ReadOrderlyShutdown(fd);
Spencer Lowf18fc082015-08-11 17:05:02 -0700244 adb_close(fd);
245 } else {
246 // If we couldn't connect to the server or had some other error,
247 // report it, but still try to start the server.
248 fprintf(stderr, "error: %s\n", error->c_str());
249 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
251 /* XXX can we better detect its death? */
252 adb_sleep_ms(2000);
253 goto start_server;
254 }
255 }
256
257 // if the command is start-server, we are done.
Elliott Hughes6452a892015-04-29 12:28:13 -0700258 if (service == "host:start-server") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 return 0;
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700260 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700262 fd = _adb_connect(service, error);
263 if (fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("_adb_connect error: %s", error->c_str());
Brian Carlstrom93c91fa2013-10-18 13:58:48 -0700265 } else if(fd == -2) {
Matt Gumbeld7b33082012-11-14 10:16:17 -0800266 fprintf(stderr,"** daemon still not running\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700268 D("adb_connect: return fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
270 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271}
272
273
Elliott Hughes424af022015-05-29 17:55:19 -0700274bool adb_command(const std::string& service) {
275 std::string error;
276 int fd = adb_connect(service, &error);
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700277 if (fd < 0) {
Elliott Hughes424af022015-05-29 17:55:19 -0700278 fprintf(stderr, "error: %s\n", error.c_str());
279 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 }
281
Elliott Hughes424af022015-05-29 17:55:19 -0700282 if (!adb_status(fd, &error)) {
283 fprintf(stderr, "error: %s\n", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700285 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 }
287
Spencer Low351ecd12015-10-14 17:32:44 -0700288 ReadOrderlyShutdown(fd);
289 adb_close(fd);
Elliott Hughes424af022015-05-29 17:55:19 -0700290 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291}
292
Elliott Hughes6452a892015-04-29 12:28:13 -0700293bool adb_query(const std::string& service, std::string* result, std::string* error) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700294 D("adb_query: %s", service.c_str());
Elliott Hughes078f0fc2015-04-29 08:35:59 -0700295 int fd = adb_connect(service, error);
296 if (fd < 0) {
Elliott Hughes3d5f60d2015-07-18 12:21:30 -0700297 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
299
Elliott Hughes6452a892015-04-29 12:28:13 -0700300 result->clear();
301 if (!ReadProtocolString(fd, result, error)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700303 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
Spencer Low351ecd12015-10-14 17:32:44 -0700305
306 ReadOrderlyShutdown(fd);
307 adb_close(fd);
Elliott Hughes6452a892015-04-29 12:28:13 -0700308 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309}
Josh Gaof3f6a1d2016-01-31 19:12:26 -0800310
311std::string format_host_command(const char* command, TransportType type, const char* serial) {
312 if (serial) {
313 return android::base::StringPrintf("host-serial:%s:%s", serial, command);
314 }
315
316 const char* prefix = "host";
317 if (type == kTransportUsb) {
318 prefix = "host-usb";
319 } else if (type == kTransportLocal) {
320 prefix = "host-local";
321 }
322 return android::base::StringPrintf("%s:%s", prefix, command);
323}
324
325bool adb_get_feature_set(FeatureSet* feature_set, std::string* error) {
326 std::string result;
327 if (adb_query(format_host_command("features", __adb_transport, __adb_serial), &result, error)) {
328 *feature_set = StringToFeatureSet(result);
329 return true;
330 }
331 feature_set->clear();
332 return false;
333}