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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_ADB |
| 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 | |
| 34 | #include <base/stringprintf.h> |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 35 | #include <base/strings.h> |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 36 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 37 | #include "adb_io.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
| 39 | static transport_type __adb_transport = kTransportAny; |
| 40 | static const char* __adb_serial = NULL; |
| 41 | |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 42 | static int __adb_server_port = DEFAULT_ADB_PORT; |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 43 | static const char* __adb_server_name = NULL; |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 44 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 45 | static std::string perror_str(const char* msg) { |
| 46 | return android::base::StringPrintf("%s: %s", msg, strerror(errno)); |
| 47 | } |
| 48 | |
| 49 | static bool ReadProtocolString(int fd, std::string* s, std::string* error) { |
| 50 | char buf[5]; |
| 51 | |
| 52 | if (!ReadFdExactly(fd, buf, 4)) { |
| 53 | *error = perror_str("protocol fault (couldn't read status length)"); |
| 54 | return false; |
| 55 | } |
| 56 | buf[4] = 0; |
| 57 | |
| 58 | unsigned long len = strtoul(buf, 0, 16); |
| 59 | s->resize(len + 1, '\0'); // Ensure NUL-termination. |
| 60 | if (!ReadFdExactly(fd, &(*s)[0], len)) { |
| 61 | *error = perror_str("protocol fault (couldn't read status message)"); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | void adb_set_transport(transport_type type, const char* serial) |
| 69 | { |
| 70 | __adb_transport = type; |
| 71 | __adb_serial = serial; |
| 72 | } |
| 73 | |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 74 | void adb_set_tcp_specifics(int server_port) |
| 75 | { |
| 76 | __adb_server_port = server_port; |
| 77 | } |
| 78 | |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 79 | void adb_set_tcp_name(const char* hostname) |
| 80 | { |
| 81 | __adb_server_name = hostname; |
| 82 | } |
| 83 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 84 | int adb_get_emulator_console_port() { |
| 85 | if (__adb_serial) { |
| 86 | // The user specified a serial number; is it an emulator? |
| 87 | int port; |
| 88 | return (sscanf(__adb_serial, "emulator-%d", &port) == 1) ? port : -1; |
| 89 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 91 | // No specific device was given, so get the list of connected |
| 92 | // devices and search for emulators. If there's one, we'll |
| 93 | // take it. If there are more than one, that's an error. |
| 94 | std::string devices; |
| 95 | std::string error; |
| 96 | if (!adb_query("host:devices", &devices, &error)) { |
| 97 | printf("no emulator connected: %s\n", error.c_str()); |
| 98 | return -1; |
| 99 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 101 | int port; |
| 102 | size_t emulator_count = 0; |
| 103 | for (auto& device : android::base::Split(devices, "\n")) { |
| 104 | if (sscanf(device.c_str(), "emulator-%d", &port) == 1) { |
| 105 | if (++emulator_count > 1) { |
| 106 | return -2; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 110 | if (emulator_count == 0) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | return port; |
| 112 | } |
| 113 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 114 | static int switch_socket_transport(int fd, std::string* error) { |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 115 | std::string service; |
| 116 | if (__adb_serial) { |
| 117 | service += "host:transport:"; |
| 118 | service += __adb_serial; |
| 119 | } else { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 120 | const char* transport_type = "???"; |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 121 | switch (__adb_transport) { |
| 122 | case kTransportUsb: |
| 123 | transport_type = "transport-usb"; |
| 124 | break; |
| 125 | case kTransportLocal: |
| 126 | transport_type = "transport-local"; |
| 127 | break; |
| 128 | case kTransportAny: |
| 129 | transport_type = "transport-any"; |
| 130 | break; |
| 131 | case kTransportHost: |
| 132 | // no switch necessary |
| 133 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | } |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 135 | service += "host:"; |
| 136 | service += transport_type; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 139 | char tmp[5]; |
| 140 | snprintf(tmp, sizeof(tmp), "%04zx", service.size()); |
| 141 | if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 142 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | adb_close(fd); |
| 144 | return -1; |
| 145 | } |
| 146 | D("Switch transport in progress\n"); |
| 147 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 148 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | adb_close(fd); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 150 | D("Switch transport failed: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | D("Switch transport success\n"); |
| 154 | return 0; |
| 155 | } |
| 156 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 157 | bool adb_status(int fd, std::string* error) { |
| 158 | char buf[5]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 160 | if (!ReadFdExactly(fd, buf, 4)) { |
| 161 | *error = perror_str("protocol fault (couldn't read status)"); |
| 162 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 165 | if (!memcmp(buf, "OKAY", 4)) { |
| 166 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 169 | if (memcmp(buf, "FAIL", 4)) { |
| 170 | *error = android::base::StringPrintf("protocol fault (status %02x %02x %02x %02x?!)", |
| 171 | buf[0], buf[1], buf[2], buf[3]); |
| 172 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 175 | ReadProtocolString(fd, error, error); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 176 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 179 | int _adb_connect(const std::string& service, std::string* error) { |
| 180 | D("_adb_connect: %s\n", service.c_str()); |
| 181 | if (service.empty() || service.size() > 1024) { |
| 182 | *error = android::base::StringPrintf("bad service name length (%d)", |
| 183 | static_cast<int>(service.size())); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | return -1; |
| 185 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 187 | int fd; |
| 188 | if (__adb_server_name) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 189 | fd = socket_network_client(__adb_server_name, __adb_server_port, SOCK_STREAM); |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 190 | } else { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 191 | fd = socket_loopback_client(__adb_server_port, SOCK_STREAM); |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 192 | } |
| 193 | if (fd < 0) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 194 | *error = perror_str("cannot connect to daemon"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | return -2; |
| 196 | } |
| 197 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 198 | 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] | 199 | return -1; |
| 200 | } |
| 201 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 202 | char tmp[5]; |
| 203 | snprintf(tmp, sizeof(tmp), "%04zx", service.size()); |
| 204 | if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, &service[0], service.size())) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 205 | *error = perror_str("write failure during connection"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 206 | adb_close(fd); |
| 207 | return -1; |
| 208 | } |
| 209 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 210 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | adb_close(fd); |
| 212 | return -1; |
| 213 | } |
| 214 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 215 | D("_adb_connect: return fd %d\n", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | return fd; |
| 217 | } |
| 218 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 219 | 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] | 220 | // first query the adb server's version |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 221 | int fd = _adb_connect("host:version", error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 222 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 223 | D("adb_connect: service %s\n", service.c_str()); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 224 | if (fd == -2 && __adb_server_name) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 225 | fprintf(stderr,"** Cannot start server on remote host\n"); |
| 226 | return fd; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 227 | } else if (fd == -2) { |
Stefan Hilzinger | d0eacb8 | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 228 | fprintf(stdout,"* daemon not running. starting it now on port %d *\n", |
| 229 | __adb_server_port); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | start_server: |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 231 | if (launch_server(__adb_server_port)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 232 | fprintf(stderr,"* failed to start daemon *\n"); |
| 233 | return -1; |
| 234 | } else { |
| 235 | fprintf(stdout,"* daemon started successfully *\n"); |
| 236 | } |
| 237 | /* give the server some time to start properly and detect devices */ |
Xavier Ducrohet | a481d09 | 2009-05-21 17:47:43 -0700 | [diff] [blame] | 238 | adb_sleep_ms(3000); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | // fall through to _adb_connect |
| 240 | } else { |
| 241 | // if server was running, check its version to make sure it is not out of date |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 | int version = ADB_SERVER_VERSION - 1; |
| 243 | |
| 244 | // if we have a file descriptor, then parse version result |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 245 | if (fd >= 0) { |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 246 | std::string version_string; |
| 247 | if (!ReadProtocolString(fd, &version_string, error)) { |
| 248 | goto error; |
| 249 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | adb_close(fd); |
| 252 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 253 | if (sscanf(&version_string[0], "%04x", &version) != 1) { |
| 254 | goto error; |
| 255 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | } else { |
| 257 | // if fd is -1, then check for "unknown host service", |
| 258 | // which would indicate a version of adb that does not support the version command |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 259 | if (*error == "unknown host service") { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 260 | return fd; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 261 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 264 | if (version != ADB_SERVER_VERSION) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | printf("adb server is out of date. killing...\n"); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 266 | fd = _adb_connect("host:kill", error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | adb_close(fd); |
| 268 | |
| 269 | /* XXX can we better detect its death? */ |
| 270 | adb_sleep_ms(2000); |
| 271 | goto start_server; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // if the command is start-server, we are done. |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 276 | if (service == "host:start-server") { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | return 0; |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 278 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 279 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 280 | fd = _adb_connect(service, error); |
| 281 | if (fd == -1) { |
| 282 | D("_adb_connect error: %s", error->c_str()); |
Brian Carlstrom | 93c91fa | 2013-10-18 13:58:48 -0700 | [diff] [blame] | 283 | } else if(fd == -2) { |
Matt Gumbel | d7b3308 | 2012-11-14 10:16:17 -0800 | [diff] [blame] | 284 | fprintf(stderr,"** daemon still not running\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 285 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 286 | D("adb_connect: return fd %d\n", fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | |
| 288 | return fd; |
| 289 | error: |
| 290 | adb_close(fd); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 295 | int adb_command(const std::string& service, std::string* error) { |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 296 | int fd = adb_connect(service, error); |
| 297 | if (fd < 0) { |
| 298 | fprintf(stderr, "error: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | return -1; |
| 300 | } |
| 301 | |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 302 | if (!adb_status(fd, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | adb_close(fd); |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 310 | bool adb_query(const std::string& service, std::string* result, std::string* error) { |
| 311 | D("adb_query: %s\n", service.c_str()); |
Elliott Hughes | 078f0fc | 2015-04-29 08:35:59 -0700 | [diff] [blame] | 312 | int fd = adb_connect(service, error); |
| 313 | if (fd < 0) { |
| 314 | fprintf(stderr,"error: %s\n", error->c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 318 | result->clear(); |
| 319 | if (!ReadProtocolString(fd, result, error)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 320 | adb_close(fd); |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 321 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | } |
Elliott Hughes | 6452a89 | 2015-04-29 12:28:13 -0700 | [diff] [blame] | 323 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | } |