The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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_TRANSPORT |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | #include "transport.h" |
| 21 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 28 | #include <base/stringprintf.h> |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 29 | #include <cutils/sockets.h> |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 30 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 31 | #if !ADB_HOST |
| 32 | #include "cutils/properties.h" |
| 33 | #endif |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 34 | |
| 35 | #include "adb.h" |
| 36 | #include "adb_io.h" |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 37 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #if ADB_HOST |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 40 | /* we keep a list of opened transports. The atransport struct knows to which |
| 41 | * local transport it is connected. The list is used to detect when we're |
| 42 | * trying to connect twice to a given local transport. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | */ |
David 'Digit' Turner | 094fd86 | 2014-03-13 11:21:58 +0100 | [diff] [blame] | 44 | #define ADB_LOCAL_TRANSPORT_MAX 64 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
| 46 | ADB_MUTEX_DEFINE( local_transports_lock ); |
| 47 | |
| 48 | static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ]; |
| 49 | #endif /* ADB_HOST */ |
| 50 | |
| 51 | static int remote_read(apacket *p, atransport *t) |
| 52 | { |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 53 | if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | D("remote local: read terminated (message)\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 58 | if(check_header(p, t)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | D("bad header: terminated (data)\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 63 | if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | D("remote local: terminated (data)\n"); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | if(check_data(p)) { |
| 69 | D("bad data: terminated (data)\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int remote_write(apacket *p, atransport *t) |
| 77 | { |
| 78 | int length = p->msg.data_length; |
| 79 | |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 80 | if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | D("remote local: write terminated\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 88 | void local_connect(int port) { |
| 89 | std::string dummy; |
| 90 | local_connect_arbitrary_ports(port-1, port, &dummy); |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 93 | int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) { |
| 94 | int fd = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | |
| 96 | #if ADB_HOST |
| 97 | const char *host = getenv("ADBHOST"); |
| 98 | if (host) { |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 99 | fd = network_connect(host, adb_port, SOCK_STREAM, 0, error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
| 101 | #endif |
| 102 | if (fd < 0) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame^] | 103 | fd = network_loopback_client(adb_port, SOCK_STREAM, error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (fd >= 0) { |
| 107 | D("client: connected on remote on fd %d\n", fd); |
| 108 | close_on_exec(fd); |
| 109 | disable_tcp_nagle(fd); |
Elliott Hughes | ab52c18 | 2015-05-01 17:04:38 -0700 | [diff] [blame] | 110 | std::string serial = android::base::StringPrintf("emulator-%d", console_port); |
| 111 | register_socket_transport(fd, serial.c_str(), adb_port, 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | static void *client_socket_thread(void *x) |
| 119 | { |
| 120 | #if ADB_HOST |
Stefan Hilzinger | a84a42e | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 121 | int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | int count = ADB_LOCAL_TRANSPORT_MAX; |
| 123 | |
| 124 | D("transport: client_socket_thread() starting\n"); |
| 125 | |
| 126 | /* try to connect to any number of running emulator instances */ |
| 127 | /* this is only done when ADB starts up. later, each new emulator */ |
| 128 | /* will send a message to ADB to indicate that is is starting up */ |
| 129 | for ( ; count > 0; count--, port += 2 ) { |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 130 | local_connect(port); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | } |
| 132 | #endif |
| 133 | return 0; |
| 134 | } |
| 135 | |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 136 | static void *server_socket_thread(void * arg) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | { |
| 138 | int serverfd, fd; |
Kenny Root | 9a77f55 | 2012-03-28 15:45:08 -0700 | [diff] [blame] | 139 | struct sockaddr addr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | socklen_t alen; |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 141 | int port = (int) (uintptr_t) arg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 142 | |
| 143 | D("transport: server_socket_thread() starting\n"); |
| 144 | serverfd = -1; |
| 145 | for(;;) { |
| 146 | if(serverfd == -1) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame^] | 147 | std::string error; |
| 148 | serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | if(serverfd < 0) { |
Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame^] | 150 | D("server: cannot bind socket yet: %s\n", error.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | adb_sleep_ms(1000); |
| 152 | continue; |
| 153 | } |
| 154 | close_on_exec(serverfd); |
| 155 | } |
| 156 | |
| 157 | alen = sizeof(addr); |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 158 | D("server: trying to get new connection from %d\n", port); |
Kenny Root | 9a77f55 | 2012-03-28 15:45:08 -0700 | [diff] [blame] | 159 | fd = adb_socket_accept(serverfd, &addr, &alen); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | if(fd >= 0) { |
| 161 | D("server: new connection on fd %d\n", fd); |
| 162 | close_on_exec(fd); |
| 163 | disable_tcp_nagle(fd); |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 164 | register_socket_transport(fd, "host", port, 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | D("transport: server_socket_thread() exiting\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 171 | /* This is relevant only for ADB daemon running inside the emulator. */ |
| 172 | #if !ADB_HOST |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 173 | /* |
| 174 | * Redefine open and write for qemu_pipe.h that contains inlined references |
| 175 | * to those routines. We will redifine them back after qemu_pipe.h inclusion. |
| 176 | */ |
| 177 | #undef open |
| 178 | #undef write |
| 179 | #define open adb_open |
| 180 | #define write adb_write |
| 181 | #include <hardware/qemu_pipe.h> |
| 182 | #undef open |
| 183 | #undef write |
| 184 | #define open ___xxx_open |
| 185 | #define write ___xxx_write |
| 186 | |
| 187 | /* A worker thread that monitors host connections, and registers a transport for |
| 188 | * every new host connection. This thread replaces server_socket_thread on |
| 189 | * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD |
| 190 | * pipe to communicate with adbd daemon inside the guest. This is done in order |
| 191 | * to provide more robust communication channel between ADB host and guest. The |
| 192 | * main issue with server_socket_thread approach is that it runs on top of TCP, |
| 193 | * and thus is sensitive to network disruptions. For instance, the |
| 194 | * ConnectionManager may decide to reset all network connections, in which case |
| 195 | * the connection between ADB host and guest will be lost. To make ADB traffic |
| 196 | * independent from the network, we use here 'adb' QEMUD service to transfer data |
| 197 | * between the host, and the guest. See external/qemu/android/adb-*.* that |
| 198 | * implements the emulator's side of the protocol. Another advantage of using |
| 199 | * QEMUD approach is that ADB will be up much sooner, since it doesn't depend |
| 200 | * anymore on network being set up. |
| 201 | * The guest side of the protocol contains the following phases: |
| 202 | * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service |
| 203 | * is opened, and it becomes clear whether or not emulator supports that |
| 204 | * protocol. |
| 205 | * - Wait for the ADB host to create connection with the guest. This is done by |
| 206 | * sending an 'accept' request to the adb QEMUD service, and waiting on |
| 207 | * response. |
| 208 | * - When new ADB host connection is accepted, the connection with adb QEMUD |
| 209 | * service is registered as the transport, and a 'start' request is sent to the |
| 210 | * adb QEMUD service, indicating that the guest is ready to receive messages. |
| 211 | * Note that the guest will ignore messages sent down from the emulator before |
| 212 | * the transport registration is completed. That's why we need to send the |
| 213 | * 'start' request after the transport is registered. |
| 214 | */ |
| 215 | static void *qemu_socket_thread(void * arg) |
| 216 | { |
| 217 | /* 'accept' request to the adb QEMUD service. */ |
| 218 | static const char _accept_req[] = "accept"; |
| 219 | /* 'start' request to the adb QEMUD service. */ |
| 220 | static const char _start_req[] = "start"; |
| 221 | /* 'ok' reply from the adb QEMUD service. */ |
| 222 | static const char _ok_resp[] = "ok"; |
| 223 | |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 224 | const int port = (int) (uintptr_t) arg; |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 225 | int res, fd; |
| 226 | char tmp[256]; |
| 227 | char con_name[32]; |
| 228 | |
| 229 | D("transport: qemu_socket_thread() starting\n"); |
| 230 | |
| 231 | /* adb QEMUD service connection request. */ |
| 232 | snprintf(con_name, sizeof(con_name), "qemud:adb:%d", port); |
| 233 | |
| 234 | /* Connect to the adb QEMUD service. */ |
| 235 | fd = qemu_pipe_open(con_name); |
| 236 | if (fd < 0) { |
| 237 | /* This could be an older version of the emulator, that doesn't |
| 238 | * implement adb QEMUD service. Fall back to the old TCP way. */ |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 239 | D("adb service is not available. Falling back to TCP socket.\n"); |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 240 | adb_thread_create(server_socket_thread, arg); |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | for(;;) { |
| 245 | /* |
| 246 | * Wait till the host creates a new connection. |
| 247 | */ |
| 248 | |
| 249 | /* Send the 'accept' request. */ |
| 250 | res = adb_write(fd, _accept_req, strlen(_accept_req)); |
Edwin Vane | ae06933 | 2012-07-26 13:40:14 -0400 | [diff] [blame] | 251 | if ((size_t)res == strlen(_accept_req)) { |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 252 | /* Wait for the response. In the response we expect 'ok' on success, |
| 253 | * or 'ko' on failure. */ |
| 254 | res = adb_read(fd, tmp, sizeof(tmp)); |
| 255 | if (res != 2 || memcmp(tmp, _ok_resp, 2)) { |
| 256 | D("Accepting ADB host connection has failed.\n"); |
| 257 | adb_close(fd); |
| 258 | } else { |
| 259 | /* Host is connected. Register the transport, and start the |
| 260 | * exchange. */ |
| 261 | register_socket_transport(fd, "host", port, 1); |
| 262 | adb_write(fd, _start_req, strlen(_start_req)); |
| 263 | } |
| 264 | |
| 265 | /* Prepare for accepting of the next ADB host connection. */ |
| 266 | fd = qemu_pipe_open(con_name); |
| 267 | if (fd < 0) { |
| 268 | D("adb service become unavailable.\n"); |
| 269 | return 0; |
| 270 | } |
| 271 | } else { |
| 272 | D("Unable to send the '%s' request to ADB service.\n", _accept_req); |
| 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | D("transport: qemu_socket_thread() exiting\n"); |
| 277 | return 0; |
| 278 | } |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 279 | #endif // !ADB_HOST |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 280 | |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 281 | void local_init(int port) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | void* (*func)(void *); |
| 284 | |
| 285 | if(HOST) { |
| 286 | func = client_socket_thread; |
| 287 | } else { |
Vladimir Chtchetkine | c4f37ee | 2011-12-13 12:19:29 -0800 | [diff] [blame] | 288 | #if ADB_HOST |
| 289 | func = server_socket_thread; |
| 290 | #else |
Vladimir Chtchetkine | c13daef | 2011-12-09 15:49:47 -0800 | [diff] [blame] | 291 | /* For the adbd daemon in the system image we need to distinguish |
| 292 | * between the device, and the emulator. */ |
| 293 | char is_qemu[PROPERTY_VALUE_MAX]; |
| 294 | property_get("ro.kernel.qemu", is_qemu, ""); |
| 295 | if (!strcmp(is_qemu, "1")) { |
| 296 | /* Running inside the emulator: use QEMUD pipe as the transport. */ |
| 297 | func = qemu_socket_thread; |
| 298 | } else { |
| 299 | /* Running inside the device: use TCP socket as the transport. */ |
| 300 | func = server_socket_thread; |
| 301 | } |
Anatol Pomazau | fc65610 | 2012-02-13 17:37:14 -0800 | [diff] [blame] | 302 | #endif // !ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | D("transport: local %s init\n", HOST ? "client" : "server"); |
| 306 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 307 | if (!adb_thread_create(func, (void *) (uintptr_t) port)) { |
| 308 | fatal_errno("cannot create local socket %s thread", HOST ? "client" : "server"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | static void remote_kick(atransport *t) |
| 313 | { |
| 314 | int fd = t->sfd; |
| 315 | t->sfd = -1; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 316 | adb_shutdown(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | adb_close(fd); |
| 318 | |
| 319 | #if ADB_HOST |
| 320 | if(HOST) { |
| 321 | int nn; |
| 322 | adb_mutex_lock( &local_transports_lock ); |
| 323 | for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) { |
| 324 | if (local_transports[nn] == t) { |
| 325 | local_transports[nn] = NULL; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | adb_mutex_unlock( &local_transports_lock ); |
| 330 | } |
| 331 | #endif |
| 332 | } |
| 333 | |
| 334 | static void remote_close(atransport *t) |
| 335 | { |
Spencer Low | 3abd31d | 2015-06-09 12:32:17 -0700 | [diff] [blame] | 336 | int fd = t->sfd; |
| 337 | if (fd != -1) { |
| 338 | t->sfd = -1; |
| 339 | adb_close(fd); |
| 340 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 343 | |
| 344 | #if ADB_HOST |
| 345 | /* Only call this function if you already hold local_transports_lock. */ |
| 346 | atransport* find_emulator_transport_by_adb_port_locked(int adb_port) |
| 347 | { |
| 348 | int i; |
| 349 | for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) { |
| 350 | if (local_transports[i] && local_transports[i]->adb_port == adb_port) { |
| 351 | return local_transports[i]; |
| 352 | } |
| 353 | } |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | atransport* find_emulator_transport_by_adb_port(int adb_port) |
| 358 | { |
| 359 | adb_mutex_lock( &local_transports_lock ); |
| 360 | atransport* result = find_emulator_transport_by_adb_port_locked(adb_port); |
| 361 | adb_mutex_unlock( &local_transports_lock ); |
| 362 | return result; |
| 363 | } |
| 364 | |
| 365 | /* Only call this function if you already hold local_transports_lock. */ |
| 366 | int get_available_local_transport_index_locked() |
| 367 | { |
| 368 | int i; |
| 369 | for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) { |
| 370 | if (local_transports[i] == NULL) { |
| 371 | return i; |
| 372 | } |
| 373 | } |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | int get_available_local_transport_index() |
| 378 | { |
| 379 | adb_mutex_lock( &local_transports_lock ); |
| 380 | int result = get_available_local_transport_index_locked(); |
| 381 | adb_mutex_unlock( &local_transports_lock ); |
| 382 | return result; |
| 383 | } |
| 384 | #endif |
| 385 | |
| 386 | int init_socket_transport(atransport *t, int s, int adb_port, int local) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 387 | { |
| 388 | int fail = 0; |
| 389 | |
| 390 | t->kick = remote_kick; |
| 391 | t->close = remote_close; |
| 392 | t->read_from_remote = remote_read; |
| 393 | t->write_to_remote = remote_write; |
| 394 | t->sfd = s; |
| 395 | t->sync_token = 1; |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 396 | t->connection_state = kCsOffline; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | t->type = kTransportLocal; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 398 | t->adb_port = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | |
| 400 | #if ADB_HOST |
Mike Lockwood | ff19670 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 401 | if (HOST && local) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | adb_mutex_lock( &local_transports_lock ); |
| 403 | { |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 404 | t->adb_port = adb_port; |
| 405 | atransport* existing_transport = |
| 406 | find_emulator_transport_by_adb_port_locked(adb_port); |
| 407 | int index = get_available_local_transport_index_locked(); |
| 408 | if (existing_transport != NULL) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | D("local transport for port %d already registered (%p)?\n", |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 410 | adb_port, existing_transport); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | fail = -1; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 412 | } else if (index < 0) { |
| 413 | // Too many emulators. |
| 414 | D("cannot register more emulators. Maximum is %d\n", |
| 415 | ADB_LOCAL_TRANSPORT_MAX); |
| 416 | fail = -1; |
| 417 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 418 | local_transports[index] = t; |
Stefan Hilzinger | d9d1ca4 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | adb_mutex_unlock( &local_transports_lock ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | } |
| 423 | #endif |
| 424 | return fail; |
| 425 | } |