blob: cc197854bb19222b73a36209d12b08b14088fdca [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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 Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
Yabin Cuib74c6492016-04-29 16:53:52 -070020#include "sysdeps/condition_variable.h"
21#include "sysdeps/mutex.h"
Dan Albert33134262015-03-19 15:21:08 -070022#include "transport.h"
23
Dan Albert76649012015-02-24 15:51:19 -080024#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Yabin Cuib74c6492016-04-29 16:53:52 -070030#include <vector>
31
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/stringprintf.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070033#include <cutils/sockets.h>
Elliott Hughesab52c182015-05-01 17:04:38 -070034
Dan Albert76649012015-02-24 15:51:19 -080035#if !ADB_HOST
36#include "cutils/properties.h"
37#endif
Dan Albert33134262015-03-19 15:21:08 -070038
39#include "adb.h"
40#include "adb_io.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070041#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#if ADB_HOST
Josh Gao9fe74262016-05-06 02:37:24 -070044
45// Android Wear has been using port 5601 in all of its documentation/tooling,
46// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
47// Avoid stomping on their port by limiting the number of emulators that can be
48// connected.
49#define ADB_LOCAL_TRANSPORT_MAX 16
50
51ADB_MUTEX_DEFINE(local_transports_lock);
52
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +010053/* we keep a list of opened transports. The atransport struct knows to which
54 * local transport it is connected. The list is used to detect when we're
55 * trying to connect twice to a given local transport.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057static atransport* local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
58#endif /* ADB_HOST */
59
60static int remote_read(apacket *p, atransport *t)
61{
Dan Albertcc731cc2015-02-24 21:26:58 -080062 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui7a3f8d62015-09-02 17:44:28 -070063 D("remote local: read terminated (message)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064 return -1;
65 }
66
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010067 if(check_header(p, t)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070068 D("bad header: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 return -1;
70 }
71
Dan Albertcc731cc2015-02-24 21:26:58 -080072 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui7a3f8d62015-09-02 17:44:28 -070073 D("remote local: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 return -1;
75 }
76
77 if(check_data(p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070078 D("bad data: terminated (data)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079 return -1;
80 }
81
82 return 0;
83}
84
85static int remote_write(apacket *p, atransport *t)
86{
87 int length = p->msg.data_length;
88
Dan Albertcc731cc2015-02-24 21:26:58 -080089 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -070090 D("remote local: write terminated");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 return -1;
92 }
93
94 return 0;
95}
96
Yabin Cuib74c6492016-04-29 16:53:52 -070097bool local_connect(int port) {
Elliott Hughes381cfa92015-07-23 17:12:58 -070098 std::string dummy;
Yabin Cuib74c6492016-04-29 16:53:52 -070099 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100100}
101
Elliott Hughes381cfa92015-07-23 17:12:58 -0700102int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
103 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
105#if ADB_HOST
Yabin Cui0e2c1942015-07-31 14:10:54 -0700106 if (find_emulator_transport_by_adb_port(adb_port) != nullptr) {
107 return -1;
108 }
109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 const char *host = getenv("ADBHOST");
111 if (host) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700112 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 }
114#endif
115 if (fd < 0) {
Spencer Low5200c662015-07-30 23:07:55 -0700116 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 }
118
119 if (fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700120 D("client: connected on remote on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 close_on_exec(fd);
122 disable_tcp_nagle(fd);
Elliott Hughesab52c182015-05-01 17:04:38 -0700123 std::string serial = android::base::StringPrintf("emulator-%d", console_port);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700124 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
125 return 0;
126 }
127 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 }
129 return -1;
130}
131
Yabin Cui661327e2015-08-11 13:40:42 -0700132#if ADB_HOST
Yabin Cuib74c6492016-04-29 16:53:52 -0700133
134static void PollAllLocalPortsForEmulator() {
135 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
136 int count = ADB_LOCAL_TRANSPORT_MAX;
137
138 // Try to connect to any number of running emulator instances.
139 for ( ; count > 0; count--, port += 2 ) {
140 local_connect(port);
141 }
142}
143
144// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
145constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
146constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000;
147
148struct RetryPort {
149 int port;
150 uint32_t retry_count;
151};
152
153// Retry emulators just kicked.
154static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
155std::mutex &retry_ports_lock = *new std::mutex;
156std::condition_variable &retry_ports_cond = *new std::condition_variable;
157
Josh Gaod9db09c2016-02-12 14:31:15 -0800158static void client_socket_thread(void* x) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700159 adb_thread_setname("client_socket_thread");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700160 D("transport: client_socket_thread() starting");
Yabin Cuib74c6492016-04-29 16:53:52 -0700161 PollAllLocalPortsForEmulator();
Yabin Cui0e2c1942015-07-31 14:10:54 -0700162 while (true) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700163 std::vector<RetryPort> ports;
164 // Collect retry ports.
165 {
166 std::unique_lock<std::mutex> lock(retry_ports_lock);
167 while (retry_ports.empty()) {
168 retry_ports_cond.wait(lock);
169 }
170 retry_ports.swap(ports);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700171 }
Yabin Cuib74c6492016-04-29 16:53:52 -0700172 // Sleep here instead of the end of loop, because if we immediately try to reconnect
173 // the emulator just kicked, the adbd on the emulator may not have time to remove the
174 // just kicked transport.
175 adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS);
176
177 // Try connecting retry ports.
178 std::vector<RetryPort> next_ports;
179 for (auto& port : ports) {
180 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
181 << port.retry_count;
182 if (local_connect(port.port)) {
183 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
184 continue;
185 }
186 if (--port.retry_count > 0) {
187 next_ports.push_back(port);
188 } else {
189 VLOG(TRANSPORT) << "stop retrying port " << port.port;
190 }
191 }
192
193 // Copy back left retry ports.
194 {
195 std::unique_lock<std::mutex> lock(retry_ports_lock);
196 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
197 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199}
200
Yabin Cui661327e2015-08-11 13:40:42 -0700201#else // ADB_HOST
202
Josh Gaod9db09c2016-02-12 14:31:15 -0800203static void server_socket_thread(void* arg) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 int serverfd, fd;
Erik Kline7e16cc12015-12-01 17:27:59 +0900205 sockaddr_storage ss;
206 sockaddr *addrp = reinterpret_cast<sockaddr*>(&ss);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 socklen_t alen;
Elliott Hughesccecf142014-01-16 10:53:11 -0800208 int port = (int) (uintptr_t) arg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700210 adb_thread_setname("server socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("transport: server_socket_thread() starting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 serverfd = -1;
213 for(;;) {
214 if(serverfd == -1) {
Spencer Low5200c662015-07-30 23:07:55 -0700215 std::string error;
216 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if(serverfd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700218 D("server: cannot bind socket yet: %s", error.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 adb_sleep_ms(1000);
220 continue;
221 }
222 close_on_exec(serverfd);
223 }
224
Erik Kline7e16cc12015-12-01 17:27:59 +0900225 alen = sizeof(ss);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700226 D("server: trying to get new connection from %d", port);
Erik Kline7e16cc12015-12-01 17:27:59 +0900227 fd = adb_socket_accept(serverfd, addrp, &alen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 if(fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700229 D("server: new connection on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230 close_on_exec(fd);
231 disable_tcp_nagle(fd);
Yabin Cui01401822016-05-17 15:14:25 -0700232 if (register_socket_transport(fd, "host", port, 1) != 0) {
233 adb_close(fd);
234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700237 D("transport: server_socket_thread() exiting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238}
239
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800240/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800241/*
242 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200243 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800244 */
245#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200246#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800247#undef write
248#define open adb_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200249#define read adb_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800250#define write adb_write
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200251#include <system/qemu_pipe.h>
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800252#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200253#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800254#undef write
255#define open ___xxx_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200256#define read ___xxx_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800257#define write ___xxx_write
258
259/* A worker thread that monitors host connections, and registers a transport for
260 * every new host connection. This thread replaces server_socket_thread on
261 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
262 * pipe to communicate with adbd daemon inside the guest. This is done in order
263 * to provide more robust communication channel between ADB host and guest. The
264 * main issue with server_socket_thread approach is that it runs on top of TCP,
265 * and thus is sensitive to network disruptions. For instance, the
266 * ConnectionManager may decide to reset all network connections, in which case
267 * the connection between ADB host and guest will be lost. To make ADB traffic
268 * independent from the network, we use here 'adb' QEMUD service to transfer data
269 * between the host, and the guest. See external/qemu/android/adb-*.* that
270 * implements the emulator's side of the protocol. Another advantage of using
271 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
272 * anymore on network being set up.
273 * The guest side of the protocol contains the following phases:
274 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
275 * is opened, and it becomes clear whether or not emulator supports that
276 * protocol.
277 * - Wait for the ADB host to create connection with the guest. This is done by
278 * sending an 'accept' request to the adb QEMUD service, and waiting on
279 * response.
280 * - When new ADB host connection is accepted, the connection with adb QEMUD
281 * service is registered as the transport, and a 'start' request is sent to the
282 * adb QEMUD service, indicating that the guest is ready to receive messages.
283 * Note that the guest will ignore messages sent down from the emulator before
284 * the transport registration is completed. That's why we need to send the
285 * 'start' request after the transport is registered.
286 */
Josh Gaod9db09c2016-02-12 14:31:15 -0800287static void qemu_socket_thread(void* arg) {
288 /* 'accept' request to the adb QEMUD service. */
289 static const char _accept_req[] = "accept";
290 /* 'start' request to the adb QEMUD service. */
291 static const char _start_req[] = "start";
292 /* 'ok' reply from the adb QEMUD service. */
293 static const char _ok_resp[] = "ok";
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800294
Elliott Hughesccecf142014-01-16 10:53:11 -0800295 const int port = (int) (uintptr_t) arg;
bohu8ac1b042016-02-29 13:13:19 -0800296 int fd;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800297 char tmp[256];
298 char con_name[32];
299
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700300 adb_thread_setname("qemu socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700301 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800302
303 /* adb QEMUD service connection request. */
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200304 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800305
306 /* Connect to the adb QEMUD service. */
307 fd = qemu_pipe_open(con_name);
308 if (fd < 0) {
309 /* This could be an older version of the emulator, that doesn't
310 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700311 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700312 adb_thread_create(server_socket_thread, arg);
Josh Gaod9db09c2016-02-12 14:31:15 -0800313 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800314 }
315
316 for(;;) {
317 /*
318 * Wait till the host creates a new connection.
319 */
320
321 /* Send the 'accept' request. */
bohu8ac1b042016-02-29 13:13:19 -0800322 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800323 /* Wait for the response. In the response we expect 'ok' on success,
324 * or 'ko' on failure. */
bohu8ac1b042016-02-29 13:13:19 -0800325 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700326 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800327 adb_close(fd);
328 } else {
329 /* Host is connected. Register the transport, and start the
330 * exchange. */
Prathmesh Prabhu251d46e2016-03-09 13:51:30 -0800331 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cui01401822016-05-17 15:14:25 -0700332 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
333 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohu8ac1b042016-02-29 13:13:19 -0800334 adb_close(fd);
335 }
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800336 }
337
338 /* Prepare for accepting of the next ADB host connection. */
339 fd = qemu_pipe_open(con_name);
340 if (fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700341 D("adb service become unavailable.");
Josh Gaod9db09c2016-02-12 14:31:15 -0800342 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800343 }
344 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gaod9db09c2016-02-12 14:31:15 -0800346 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800347 }
348 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700349 D("transport: qemu_socket_thread() exiting");
Josh Gaod9db09c2016-02-12 14:31:15 -0800350 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800351}
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800352#endif // !ADB_HOST
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800353
Mike Lockwoodff196702009-08-24 15:58:40 -0700354void local_init(int port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355{
Josh Gaod9db09c2016-02-12 14:31:15 -0800356 adb_thread_func_t func;
Yabin Cui661327e2015-08-11 13:40:42 -0700357 const char* debug_name = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800359#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700360 func = client_socket_thread;
361 debug_name = "client";
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800362#else
Yabin Cui661327e2015-08-11 13:40:42 -0700363 /* For the adbd daemon in the system image we need to distinguish
364 * between the device, and the emulator. */
365 char is_qemu[PROPERTY_VALUE_MAX];
366 property_get("ro.kernel.qemu", is_qemu, "");
367 if (!strcmp(is_qemu, "1")) {
368 /* Running inside the emulator: use QEMUD pipe as the transport. */
369 func = qemu_socket_thread;
370 } else {
371 /* Running inside the device: use TCP socket as the transport. */
372 func = server_socket_thread;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 }
Yabin Cui661327e2015-08-11 13:40:42 -0700374 debug_name = "server";
375#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700377 D("transport: local %s init", debug_name);
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700378 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui661327e2015-08-11 13:40:42 -0700379 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381}
382
383static void remote_kick(atransport *t)
384{
385 int fd = t->sfd;
386 t->sfd = -1;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400387 adb_shutdown(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 adb_close(fd);
389
390#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700391 int nn;
392 adb_mutex_lock( &local_transports_lock );
393 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
394 if (local_transports[nn] == t) {
395 local_transports[nn] = NULL;
396 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
Yabin Cui661327e2015-08-11 13:40:42 -0700399 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400#endif
401}
402
403static void remote_close(atransport *t)
404{
Spencer Low3abd31d2015-06-09 12:32:17 -0700405 int fd = t->sfd;
406 if (fd != -1) {
407 t->sfd = -1;
408 adb_close(fd);
409 }
Yabin Cuib74c6492016-04-29 16:53:52 -0700410#if ADB_HOST
411 int local_port;
412 if (t->GetLocalPortForEmulator(&local_port)) {
413 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
414 std::unique_lock<std::mutex> lock(retry_ports_lock);
415 RetryPort port;
416 port.port = local_port;
417 port.retry_count = LOCAL_PORT_RETRY_COUNT;
418 retry_ports.push_back(port);
419 retry_ports_cond.notify_one();
420 }
421#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422}
423
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100424
425#if ADB_HOST
426/* Only call this function if you already hold local_transports_lock. */
Yabin Cuib74c6492016-04-29 16:53:52 -0700427static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100428{
429 int i;
430 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700431 int local_port;
432 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
433 if (local_port == adb_port) {
434 return local_transports[i];
435 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100436 }
437 }
438 return NULL;
439}
440
441atransport* find_emulator_transport_by_adb_port(int adb_port)
442{
443 adb_mutex_lock( &local_transports_lock );
444 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
445 adb_mutex_unlock( &local_transports_lock );
446 return result;
447}
448
449/* Only call this function if you already hold local_transports_lock. */
450int get_available_local_transport_index_locked()
451{
452 int i;
453 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
454 if (local_transports[i] == NULL) {
455 return i;
456 }
457 }
458 return -1;
459}
460
461int get_available_local_transport_index()
462{
463 adb_mutex_lock( &local_transports_lock );
464 int result = get_available_local_transport_index_locked();
465 adb_mutex_unlock( &local_transports_lock );
466 return result;
467}
468#endif
469
470int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471{
472 int fail = 0;
473
Yabin Cuia28918c2016-04-18 11:22:34 -0700474 t->SetKickFunction(remote_kick);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 t->close = remote_close;
476 t->read_from_remote = remote_read;
477 t->write_to_remote = remote_write;
478 t->sfd = s;
479 t->sync_token = 1;
Dan Albertdcd78a12015-05-18 16:43:57 -0700480 t->connection_state = kCsOffline;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 t->type = kTransportLocal;
482
483#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700484 if (local) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 adb_mutex_lock( &local_transports_lock );
486 {
Yabin Cuib74c6492016-04-29 16:53:52 -0700487 t->SetLocalPortForEmulator(adb_port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100488 atransport* existing_transport =
489 find_emulator_transport_by_adb_port_locked(adb_port);
490 int index = get_available_local_transport_index_locked();
491 if (existing_transport != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700492 D("local transport for port %d already registered (%p)?",
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100493 adb_port, existing_transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 fail = -1;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100495 } else if (index < 0) {
496 // Too many emulators.
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700497 D("cannot register more emulators. Maximum is %d",
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100498 ADB_LOCAL_TRANSPORT_MAX);
499 fail = -1;
500 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 local_transports[index] = t;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100502 }
503 }
504 adb_mutex_unlock( &local_transports_lock );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 }
506#endif
507 return fail;
508}