blob: d620a376638eace3c21797ca0dd0e0465f716973 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
Yabin Cuif401ead2016-04-29 16:53:52 -070020#include "sysdeps/condition_variable.h"
21#include "sysdeps/mutex.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070022#include "transport.h"
23
Dan Albertb302d122015-02-24 15:51:19 -080024#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
Yabin Cuif401ead2016-04-29 16:53:52 -070030#include <vector>
31
Elliott Hughesf55ead92015-12-04 22:00:26 -080032#include <android-base/stringprintf.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070033#include <cutils/sockets.h>
Elliott Hughesfb596842015-05-01 17:04:38 -070034
Dan Albertb302d122015-02-24 15:51:19 -080035#if !ADB_HOST
36#include "cutils/properties.h"
37#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070038
39#include "adb.h"
40#include "adb_io.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070041#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043#if ADB_HOST
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010044/* we keep a list of opened transports. The atransport struct knows to which
45 * local transport it is connected. The list is used to detect when we're
46 * trying to connect twice to a given local transport.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047 */
David 'Digit' Turnerb0f0a462014-03-13 11:21:58 +010048#define ADB_LOCAL_TRANSPORT_MAX 64
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049
50ADB_MUTEX_DEFINE( local_transports_lock );
51
52static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
53#endif /* ADB_HOST */
54
55static int remote_read(apacket *p, atransport *t)
56{
Dan Albert66a91b02015-02-24 21:26:58 -080057 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui815ad882015-09-02 17:44:28 -070058 D("remote local: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059 return -1;
60 }
61
Tamas Berghammera1c60c02015-07-13 19:12:28 +010062 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -070063 D("bad header: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064 return -1;
65 }
66
Dan Albert66a91b02015-02-24 21:26:58 -080067 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui815ad882015-09-02 17:44:28 -070068 D("remote local: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069 return -1;
70 }
71
72 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -070073 D("bad data: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074 return -1;
75 }
76
77 return 0;
78}
79
80static int remote_write(apacket *p, atransport *t)
81{
82 int length = p->msg.data_length;
83
Dan Albert66a91b02015-02-24 21:26:58 -080084 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui815ad882015-09-02 17:44:28 -070085 D("remote local: write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080086 return -1;
87 }
88
89 return 0;
90}
91
Yabin Cuif401ead2016-04-29 16:53:52 -070092bool local_connect(int port) {
Elliott Hughes43df1092015-07-23 17:12:58 -070093 std::string dummy;
Yabin Cuif401ead2016-04-29 16:53:52 -070094 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010095}
96
Elliott Hughes43df1092015-07-23 17:12:58 -070097int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
98 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099
100#if ADB_HOST
Yabin Cuid802bcf2015-07-31 14:10:54 -0700101 if (find_emulator_transport_by_adb_port(adb_port) != nullptr) {
102 return -1;
103 }
104
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105 const char *host = getenv("ADBHOST");
106 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700107 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 }
109#endif
110 if (fd < 0) {
Spencer Low753d4852015-07-30 23:07:55 -0700111 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 }
113
114 if (fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700115 D("client: connected on remote on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116 close_on_exec(fd);
117 disable_tcp_nagle(fd);
Elliott Hughesfb596842015-05-01 17:04:38 -0700118 std::string serial = android::base::StringPrintf("emulator-%d", console_port);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700119 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
120 return 0;
121 }
122 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123 }
124 return -1;
125}
126
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700127#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700128
129static void PollAllLocalPortsForEmulator() {
130 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
131 int count = ADB_LOCAL_TRANSPORT_MAX;
132
133 // Try to connect to any number of running emulator instances.
134 for ( ; count > 0; count--, port += 2 ) {
135 local_connect(port);
136 }
137}
138
139// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
140constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
141constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000;
142
143struct RetryPort {
144 int port;
145 uint32_t retry_count;
146};
147
148// Retry emulators just kicked.
149static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
150std::mutex &retry_ports_lock = *new std::mutex;
151std::condition_variable &retry_ports_cond = *new std::condition_variable;
152
Josh Gao7d405252016-02-12 14:31:15 -0800153static void client_socket_thread(void* x) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700154 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700155 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700156 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700157 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700158 std::vector<RetryPort> ports;
159 // Collect retry ports.
160 {
161 std::unique_lock<std::mutex> lock(retry_ports_lock);
162 while (retry_ports.empty()) {
163 retry_ports_cond.wait(lock);
164 }
165 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700166 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700167 // Sleep here instead of the end of loop, because if we immediately try to reconnect
168 // the emulator just kicked, the adbd on the emulator may not have time to remove the
169 // just kicked transport.
170 adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS);
171
172 // Try connecting retry ports.
173 std::vector<RetryPort> next_ports;
174 for (auto& port : ports) {
175 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
176 << port.retry_count;
177 if (local_connect(port.port)) {
178 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
179 continue;
180 }
181 if (--port.retry_count > 0) {
182 next_ports.push_back(port);
183 } else {
184 VLOG(TRANSPORT) << "stop retrying port " << port.port;
185 }
186 }
187
188 // Copy back left retry ports.
189 {
190 std::unique_lock<std::mutex> lock(retry_ports_lock);
191 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
192 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194}
195
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700196#else // ADB_HOST
197
Josh Gao7d405252016-02-12 14:31:15 -0800198static void server_socket_thread(void* arg) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 int serverfd, fd;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800200 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201
Siva Velusamy2669cf92015-08-28 16:37:29 -0700202 adb_thread_setname("server socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700203 D("transport: server_socket_thread() starting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204 serverfd = -1;
205 for(;;) {
206 if(serverfd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700207 std::string error;
208 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800209 if(serverfd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700210 D("server: cannot bind socket yet: %s", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800211 adb_sleep_ms(1000);
212 continue;
213 }
214 close_on_exec(serverfd);
215 }
216
Yabin Cui815ad882015-09-02 17:44:28 -0700217 D("server: trying to get new connection from %d", port);
Josh Gao782d8d42016-08-23 15:41:56 -0700218 fd = adb_socket_accept(serverfd, nullptr, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219 if(fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700220 D("server: new connection on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221 close_on_exec(fd);
222 disable_tcp_nagle(fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700223 if (register_socket_transport(fd, "host", port, 1) != 0) {
224 adb_close(fd);
225 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 }
227 }
Yabin Cui815ad882015-09-02 17:44:28 -0700228 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229}
230
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800231/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800232/*
233 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200234 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800235 */
236#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200237#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800238#undef write
239#define open adb_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200240#define read adb_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800241#define write adb_write
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200242#include <system/qemu_pipe.h>
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800243#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200244#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800245#undef write
246#define open ___xxx_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200247#define read ___xxx_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800248#define write ___xxx_write
249
250/* A worker thread that monitors host connections, and registers a transport for
251 * every new host connection. This thread replaces server_socket_thread on
252 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
253 * pipe to communicate with adbd daemon inside the guest. This is done in order
254 * to provide more robust communication channel between ADB host and guest. The
255 * main issue with server_socket_thread approach is that it runs on top of TCP,
256 * and thus is sensitive to network disruptions. For instance, the
257 * ConnectionManager may decide to reset all network connections, in which case
258 * the connection between ADB host and guest will be lost. To make ADB traffic
259 * independent from the network, we use here 'adb' QEMUD service to transfer data
260 * between the host, and the guest. See external/qemu/android/adb-*.* that
261 * implements the emulator's side of the protocol. Another advantage of using
262 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
263 * anymore on network being set up.
264 * The guest side of the protocol contains the following phases:
265 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
266 * is opened, and it becomes clear whether or not emulator supports that
267 * protocol.
268 * - Wait for the ADB host to create connection with the guest. This is done by
269 * sending an 'accept' request to the adb QEMUD service, and waiting on
270 * response.
271 * - When new ADB host connection is accepted, the connection with adb QEMUD
272 * service is registered as the transport, and a 'start' request is sent to the
273 * adb QEMUD service, indicating that the guest is ready to receive messages.
274 * Note that the guest will ignore messages sent down from the emulator before
275 * the transport registration is completed. That's why we need to send the
276 * 'start' request after the transport is registered.
277 */
Josh Gao7d405252016-02-12 14:31:15 -0800278static void qemu_socket_thread(void* arg) {
279 /* 'accept' request to the adb QEMUD service. */
280 static const char _accept_req[] = "accept";
281 /* 'start' request to the adb QEMUD service. */
282 static const char _start_req[] = "start";
283 /* 'ok' reply from the adb QEMUD service. */
284 static const char _ok_resp[] = "ok";
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800285
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800286 const int port = (int) (uintptr_t) arg;
bohu82a28342016-02-29 13:13:19 -0800287 int fd;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800288 char tmp[256];
289 char con_name[32];
290
Siva Velusamy2669cf92015-08-28 16:37:29 -0700291 adb_thread_setname("qemu socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700292 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800293
294 /* adb QEMUD service connection request. */
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200295 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800296
297 /* Connect to the adb QEMUD service. */
298 fd = qemu_pipe_open(con_name);
299 if (fd < 0) {
300 /* This could be an older version of the emulator, that doesn't
301 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui815ad882015-09-02 17:44:28 -0700302 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughesf2517142015-05-05 13:41:21 -0700303 adb_thread_create(server_socket_thread, arg);
Josh Gao7d405252016-02-12 14:31:15 -0800304 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800305 }
306
307 for(;;) {
308 /*
309 * Wait till the host creates a new connection.
310 */
311
312 /* Send the 'accept' request. */
bohu82a28342016-02-29 13:13:19 -0800313 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800314 /* Wait for the response. In the response we expect 'ok' on success,
315 * or 'ko' on failure. */
bohu82a28342016-02-29 13:13:19 -0800316 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700317 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800318 adb_close(fd);
319 } else {
320 /* Host is connected. Register the transport, and start the
321 * exchange. */
Prathmesh Prabhu07778a02016-03-09 13:51:30 -0800322 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700323 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
324 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohu82a28342016-02-29 13:13:19 -0800325 adb_close(fd);
326 }
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800327 }
328
329 /* Prepare for accepting of the next ADB host connection. */
330 fd = qemu_pipe_open(con_name);
331 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700332 D("adb service become unavailable.");
Josh Gao7d405252016-02-12 14:31:15 -0800333 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800334 }
335 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700336 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gao7d405252016-02-12 14:31:15 -0800337 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800338 }
339 }
Yabin Cui815ad882015-09-02 17:44:28 -0700340 D("transport: qemu_socket_thread() exiting");
Josh Gao7d405252016-02-12 14:31:15 -0800341 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800342}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800343#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800344
Mike Lockwood26b88e32009-08-24 15:58:40 -0700345void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346{
Josh Gao7d405252016-02-12 14:31:15 -0800347 adb_thread_func_t func;
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700348 const char* debug_name = "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800349
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800350#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700351 func = client_socket_thread;
352 debug_name = "client";
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800353#else
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700354 /* For the adbd daemon in the system image we need to distinguish
355 * between the device, and the emulator. */
356 char is_qemu[PROPERTY_VALUE_MAX];
357 property_get("ro.kernel.qemu", is_qemu, "");
358 if (!strcmp(is_qemu, "1")) {
359 /* Running inside the emulator: use QEMUD pipe as the transport. */
360 func = qemu_socket_thread;
361 } else {
362 /* Running inside the device: use TCP socket as the transport. */
363 func = server_socket_thread;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800364 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700365 debug_name = "server";
366#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800367
Yabin Cui815ad882015-09-02 17:44:28 -0700368 D("transport: local %s init", debug_name);
Elliott Hughesf2517142015-05-05 13:41:21 -0700369 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700370 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371 }
372}
373
374static void remote_kick(atransport *t)
375{
376 int fd = t->sfd;
377 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400378 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 adb_close(fd);
380
381#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700382 int nn;
383 adb_mutex_lock( &local_transports_lock );
384 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
385 if (local_transports[nn] == t) {
386 local_transports[nn] = NULL;
387 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700390 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800391#endif
392}
393
394static void remote_close(atransport *t)
395{
Spencer Low6f38e232015-06-09 12:32:17 -0700396 int fd = t->sfd;
397 if (fd != -1) {
398 t->sfd = -1;
399 adb_close(fd);
400 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700401#if ADB_HOST
402 int local_port;
403 if (t->GetLocalPortForEmulator(&local_port)) {
404 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
405 std::unique_lock<std::mutex> lock(retry_ports_lock);
406 RetryPort port;
407 port.port = local_port;
408 port.retry_count = LOCAL_PORT_RETRY_COUNT;
409 retry_ports.push_back(port);
410 retry_ports_cond.notify_one();
411 }
412#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413}
414
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100415
416#if ADB_HOST
417/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700418static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100419{
420 int i;
421 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700422 int local_port;
423 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
424 if (local_port == adb_port) {
425 return local_transports[i];
426 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100427 }
428 }
429 return NULL;
430}
431
432atransport* find_emulator_transport_by_adb_port(int adb_port)
433{
434 adb_mutex_lock( &local_transports_lock );
435 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
436 adb_mutex_unlock( &local_transports_lock );
437 return result;
438}
439
440/* Only call this function if you already hold local_transports_lock. */
441int get_available_local_transport_index_locked()
442{
443 int i;
444 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
445 if (local_transports[i] == NULL) {
446 return i;
447 }
448 }
449 return -1;
450}
451
452int get_available_local_transport_index()
453{
454 adb_mutex_lock( &local_transports_lock );
455 int result = get_available_local_transport_index_locked();
456 adb_mutex_unlock( &local_transports_lock );
457 return result;
458}
459#endif
460
461int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800462{
463 int fail = 0;
464
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700465 t->SetKickFunction(remote_kick);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800466 t->close = remote_close;
467 t->read_from_remote = remote_read;
468 t->write_to_remote = remote_write;
469 t->sfd = s;
470 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700471 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800472 t->type = kTransportLocal;
473
474#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700475 if (local) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476 adb_mutex_lock( &local_transports_lock );
477 {
Yabin Cuif401ead2016-04-29 16:53:52 -0700478 t->SetLocalPortForEmulator(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100479 atransport* existing_transport =
480 find_emulator_transport_by_adb_port_locked(adb_port);
481 int index = get_available_local_transport_index_locked();
482 if (existing_transport != NULL) {
Yabin Cui815ad882015-09-02 17:44:28 -0700483 D("local transport for port %d already registered (%p)?",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100484 adb_port, existing_transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800485 fail = -1;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100486 } else if (index < 0) {
487 // Too many emulators.
Yabin Cui815ad882015-09-02 17:44:28 -0700488 D("cannot register more emulators. Maximum is %d",
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100489 ADB_LOCAL_TRANSPORT_MAX);
490 fail = -1;
491 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800492 local_transports[index] = t;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100493 }
494 }
495 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496 }
497#endif
498 return fail;
499}