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