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