Dan Albert | eb98f4e | 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 | b4245aa | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | dfa355b | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdarg.h> |
Dan Albert | eb98f4e | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | #include <sys/stat.h> |
Dan Albert | eb98f4e | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 34f00fc | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 31 | #include <string> |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Elliott Hughes | 7a90a00 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
| 35 | #include <android-base/strings.h> |
Elliott Hughes | ead95ef | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 36 | #include <cutils/sockets.h> |
Elliott Hughes | 34f00fc | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 37 | |
Dan Albert | b6c6057 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 38 | #include "adb_io.h" |
Elliott Hughes | ead95ef | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 39 | #include "adb_utils.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | ae3f8a1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 41 | static TransportType __adb_transport = kTransportAny; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | static const char* __adb_serial = NULL; |
| 43 | |
Stefan Hilzinger | 3b1cbee | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 44 | static int __adb_server_port = DEFAULT_ADB_PORT; |
Matt Gumbel | a043e1d | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 45 | static const char* __adb_server_name = NULL; |
Stefan Hilzinger | 3b1cbee | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 46 | |
Elliott Hughes | ae3f8a1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 47 | void adb_set_transport(TransportType type, const char* serial) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | { |
| 49 | __adb_transport = type; |
| 50 | __adb_serial = serial; |
| 51 | } |
| 52 | |
Josh Gao | ef9764c | 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 | 3b1cbee | 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 | a043e1d | 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 | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 72 | static int switch_socket_transport(int fd, std::string* error) { |
Elliott Hughes | 34f00fc | 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 | 21bc1ad | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 78 | const char* transport_type = "???"; |
Elliott Hughes | 34f00fc | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 92 | } |
Elliott Hughes | 34f00fc | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 93 | service += "host:"; |
| 94 | service += transport_type; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | 080c004 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 97 | if (!SendProtocolString(fd, service)) { |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 98 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | adb_close(fd); |
| 100 | return -1; |
| 101 | } |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 102 | D("Switch transport in progress"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 104 | if (!adb_status(fd, error)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | adb_close(fd); |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 106 | D("Switch transport failed: %s", error->c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | return -1; |
| 108 | } |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 109 | D("Switch transport success"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 113 | bool adb_status(int fd, std::string* error) { |
| 114 | char buf[5]; |
Elliott Hughes | 78519d1 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 120 | if (!memcmp(buf, "OKAY", 4)) { |
| 121 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Elliott Hughes | 78519d1 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 130 | ReadProtocolString(fd, error, error); |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 131 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 134 | int _adb_connect(const std::string& service, std::string* error) { |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 135 | D("_adb_connect: %s", service.c_str()); |
Josh Gao | f732bca | 2016-01-15 14:35:54 -0800 | [diff] [blame] | 136 | if (service.empty() || service.size() > MAX_PAYLOAD_V1) { |
Spencer Low | 8d4af97 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | return -1; |
| 140 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 142 | int fd; |
Spencer Low | c4841c6 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 143 | std::string reason; |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 144 | if (__adb_server_name) { |
Elliott Hughes | ead95ef | 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 | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 152 | } else { |
Spencer Low | c4841c6 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 153 | fd = network_loopback_client(__adb_server_port, SOCK_STREAM, &reason); |
Elliott Hughes | ead95ef | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 154 | if (fd == -1) { |
Spencer Low | c4841c6 | 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 | ead95ef | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 157 | return -2; |
| 158 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Yabin Cui | 751cce7 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 161 | if ((memcmp(&service[0],"host",4) != 0 || service == "host:reconnect") && |
| 162 | switch_socket_transport(fd, error)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
Elliott Hughes | 6b5effe | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 166 | if (!SendProtocolString(fd, service)) { |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 167 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | adb_close(fd); |
| 169 | return -1; |
| 170 | } |
| 171 | |
Yabin Cui | 751cce7 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 172 | if (service != "reconnect") { |
| 173 | if (!adb_status(fd, error)) { |
| 174 | adb_close(fd); |
| 175 | return -1; |
| 176 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 179 | D("_adb_connect: return fd %d", fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | return fd; |
| 181 | } |
| 182 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 183 | int adb_connect(const std::string& service, std::string* error) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | // first query the adb server's version |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 185 | int fd = _adb_connect("host:version", error); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 187 | D("adb_connect: service %s", service.c_str()); |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 188 | if (fd == -2 && __adb_server_name) { |
Matt Gumbel | a043e1d | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 189 | fprintf(stderr,"** Cannot start server on remote host\n"); |
Spencer Low | 785b747 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 190 | // error is the original network connection error |
Matt Gumbel | a043e1d | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 191 | return fd; |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 192 | } else if (fd == -2) { |
Stefan Hilzinger | 3b1cbee | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 193 | fprintf(stdout,"* daemon not running. starting it now on port %d *\n", |
| 194 | __adb_server_port); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | start_server: |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 196 | if (launch_server(__adb_server_port)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | fprintf(stderr,"* failed to start daemon *\n"); |
Spencer Low | 785b747 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 198 | // 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | 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 Ducrohet | 30a4033 | 2009-05-21 17:47:43 -0700 | [diff] [blame] | 207 | adb_sleep_ms(3000); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | // fall through to _adb_connect |
| 209 | } else { |
Elliott Hughes | c51bf20 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 210 | // If a server is already running, check its version matches. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | int version = ADB_SERVER_VERSION - 1; |
| 212 | |
Elliott Hughes | c51bf20 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 213 | // If we have a file descriptor, then parse version result. |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 214 | if (fd >= 0) { |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 215 | std::string version_string; |
| 216 | if (!ReadProtocolString(fd, &version_string, error)) { |
Elliott Hughes | c51bf20 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 217 | adb_close(fd); |
| 218 | return -1; |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 219 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | |
Spencer Low | e9364d0 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 221 | ReadOrderlyShutdown(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | adb_close(fd); |
| 223 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 224 | if (sscanf(&version_string[0], "%04x", &version) != 1) { |
Elliott Hughes | eaa93c1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 225 | *error = android::base::StringPrintf("cannot parse version string: %s", |
| 226 | version_string.c_str()); |
Spencer Low | 785b747 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 227 | return -1; |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 228 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | } else { |
Elliott Hughes | c51bf20 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 230 | // If fd is -1 check for "unknown host service" which would |
| 231 | // indicate a version of adb that does not support the |
Spencer Low | 19e47eb | 2015-08-05 19:26:50 -0700 | [diff] [blame] | 232 | // version command, in which case we should fall-through to kill it. |
| 233 | if (*error != "unknown host service") { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | return fd; |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 235 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 238 | if (version != ADB_SERVER_VERSION) { |
Elliott Hughes | c51bf20 | 2015-10-02 19:49:10 -0700 | [diff] [blame] | 239 | printf("adb server version (%d) doesn't match this client (%d); killing...\n", |
| 240 | version, ADB_SERVER_VERSION); |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 241 | fd = _adb_connect("host:kill", error); |
Spencer Low | 785b747 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 242 | if (fd >= 0) { |
Spencer Low | e9364d0 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 243 | ReadOrderlyShutdown(fd); |
Spencer Low | 785b747 | 2015-08-11 17:05:02 -0700 | [diff] [blame] | 244 | 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | |
| 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 Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 258 | if (service == "host:start-server") { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | return 0; |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 260 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 262 | fd = _adb_connect(service, error); |
| 263 | if (fd == -1) { |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 264 | D("_adb_connect error: %s", error->c_str()); |
Brian Carlstrom | 1c6e8af | 2013-10-18 13:58:48 -0700 | [diff] [blame] | 265 | } else if(fd == -2) { |
Matt Gumbel | a043e1d | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 266 | fprintf(stderr,"** daemon still not running\n"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | } |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 268 | D("adb_connect: return fd %d", fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 269 | |
| 270 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Elliott Hughes | 59ab92c | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 274 | bool adb_command(const std::string& service) { |
| 275 | std::string error; |
| 276 | int fd = adb_connect(service, &error); |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 277 | if (fd < 0) { |
Elliott Hughes | 59ab92c | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 278 | fprintf(stderr, "error: %s\n", error.c_str()); |
| 279 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Elliott Hughes | 59ab92c | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 282 | if (!adb_status(fd, &error)) { |
| 283 | fprintf(stderr, "error: %s\n", error.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | adb_close(fd); |
Elliott Hughes | 59ab92c | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 285 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Spencer Low | e9364d0 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 288 | ReadOrderlyShutdown(fd); |
| 289 | adb_close(fd); |
Elliott Hughes | 59ab92c | 2015-05-29 17:55:19 -0700 | [diff] [blame] | 290 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 293 | bool adb_query(const std::string& service, std::string* result, std::string* error) { |
Yabin Cui | 204c476 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 294 | D("adb_query: %s", service.c_str()); |
Elliott Hughes | 78519d1 | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 295 | int fd = adb_connect(service, error); |
| 296 | if (fd < 0) { |
Elliott Hughes | a526809 | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 297 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 300 | result->clear(); |
| 301 | if (!ReadProtocolString(fd, result, error)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | adb_close(fd); |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 303 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 304 | } |
Spencer Low | e9364d0 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 305 | |
| 306 | ReadOrderlyShutdown(fd); |
| 307 | adb_close(fd); |
Elliott Hughes | 2f421aa | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 308 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | } |
Josh Gao | b9d2c3e | 2016-01-31 19:12:26 -0800 | [diff] [blame] | 310 | |
| 311 | std::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 | |
| 325 | bool 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 | } |