blob: ea2bf7747542212da740e4518a928dc2b250d286 [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"
20#include "transport.h"
21
Dan Albertb302d122015-02-24 15:51:19 -080022#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080026#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027
Pirama Arumuga Nainarbd4d4e12016-09-06 13:34:42 -070028#include <condition_variable>
Josh Gaoe7daf572016-09-21 12:37:10 -070029#include <mutex>
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
Elliott Hughes8b249d22016-09-23 15:40:03 -070036#include <android-base/properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080037#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
Josh Gao89513a52016-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
Josh Gaoe7daf572016-09-21 12:37:10 -070051static std::mutex& local_transports_lock = *new std::mutex();
Josh Gao89513a52016-05-06 02:37:24 -070052
Stefan Hilzinger1ec03422010-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 Project9ca14dc2009-03-03 19:32:55 -080056 */
The Android Open Source Project9ca14dc2009-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 Albert66a91b02015-02-24 21:26:58 -080062 if(!ReadFdExactly(t->sfd, &p->msg, sizeof(amessage))){
Yabin Cui815ad882015-09-02 17:44:28 -070063 D("remote local: read terminated (message)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064 return -1;
65 }
66
Tamas Berghammera1c60c02015-07-13 19:12:28 +010067 if(check_header(p, t)) {
Yabin Cui815ad882015-09-02 17:44:28 -070068 D("bad header: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069 return -1;
70 }
71
Dan Albert66a91b02015-02-24 21:26:58 -080072 if(!ReadFdExactly(t->sfd, p->data, p->msg.data_length)){
Yabin Cui815ad882015-09-02 17:44:28 -070073 D("remote local: terminated (data)");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074 return -1;
75 }
76
77 if(check_data(p)) {
Yabin Cui815ad882015-09-02 17:44:28 -070078 D("bad data: terminated (data)");
The Android Open Source Project9ca14dc2009-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 Albert66a91b02015-02-24 21:26:58 -080089 if(!WriteFdExactly(t->sfd, &p->msg, sizeof(amessage) + length)) {
Yabin Cui815ad882015-09-02 17:44:28 -070090 D("remote local: write terminated");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091 return -1;
92 }
93
94 return 0;
95}
96
Yabin Cuif401ead2016-04-29 16:53:52 -070097bool local_connect(int port) {
Elliott Hughes43df1092015-07-23 17:12:58 -070098 std::string dummy;
Yabin Cuif401ead2016-04-29 16:53:52 -070099 return local_connect_arbitrary_ports(port-1, port, &dummy) == 0;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100100}
101
Elliott Hughes43df1092015-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 Project9ca14dc2009-03-03 19:32:55 -0800104
105#if ADB_HOST
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700106 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
107 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700108 return -1;
109 }
110
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111 const char *host = getenv("ADBHOST");
112 if (host) {
Elliott Hughes43df1092015-07-23 17:12:58 -0700113 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800114 }
115#endif
116 if (fd < 0) {
Spencer Low753d4852015-07-30 23:07:55 -0700117 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800118 }
119
120 if (fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700121 D("client: connected on remote on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800122 close_on_exec(fd);
123 disable_tcp_nagle(fd);
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700124 std::string serial = getEmulatorSerialString(console_port);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700125 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
126 return 0;
127 }
128 adb_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129 }
130 return -1;
131}
132
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700133#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700134
135static void PollAllLocalPortsForEmulator() {
136 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
137 int count = ADB_LOCAL_TRANSPORT_MAX;
138
139 // Try to connect to any number of running emulator instances.
140 for ( ; count > 0; count--, port += 2 ) {
141 local_connect(port);
142 }
143}
144
145// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
146constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
147constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000;
148
149struct RetryPort {
150 int port;
151 uint32_t retry_count;
152};
153
154// Retry emulators just kicked.
155static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
156std::mutex &retry_ports_lock = *new std::mutex;
157std::condition_variable &retry_ports_cond = *new std::condition_variable;
158
Josh Gao382ea6e2016-02-12 14:31:15 -0800159static void client_socket_thread(void* x) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700160 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700161 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700162 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700163 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700164 std::vector<RetryPort> ports;
165 // Collect retry ports.
166 {
167 std::unique_lock<std::mutex> lock(retry_ports_lock);
168 while (retry_ports.empty()) {
169 retry_ports_cond.wait(lock);
170 }
171 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700172 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700173 // Sleep here instead of the end of loop, because if we immediately try to reconnect
174 // the emulator just kicked, the adbd on the emulator may not have time to remove the
175 // just kicked transport.
176 adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS);
177
178 // Try connecting retry ports.
179 std::vector<RetryPort> next_ports;
180 for (auto& port : ports) {
181 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
182 << port.retry_count;
183 if (local_connect(port.port)) {
184 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
185 continue;
186 }
187 if (--port.retry_count > 0) {
188 next_ports.push_back(port);
189 } else {
190 VLOG(TRANSPORT) << "stop retrying port " << port.port;
191 }
192 }
193
194 // Copy back left retry ports.
195 {
196 std::unique_lock<std::mutex> lock(retry_ports_lock);
197 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
198 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200}
201
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700202#else // ADB_HOST
203
Josh Gao382ea6e2016-02-12 14:31:15 -0800204static void server_socket_thread(void* arg) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205 int serverfd, fd;
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800206 int port = (int) (uintptr_t) arg;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800207
Siva Velusamy2669cf92015-08-28 16:37:29 -0700208 adb_thread_setname("server socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700209 D("transport: server_socket_thread() starting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210 serverfd = -1;
211 for(;;) {
212 if(serverfd == -1) {
Spencer Low753d4852015-07-30 23:07:55 -0700213 std::string error;
214 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 if(serverfd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700216 D("server: cannot bind socket yet: %s", error.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800217 adb_sleep_ms(1000);
218 continue;
219 }
220 close_on_exec(serverfd);
221 }
222
Yabin Cui815ad882015-09-02 17:44:28 -0700223 D("server: trying to get new connection from %d", port);
Josh Gao782d8d42016-08-23 15:41:56 -0700224 fd = adb_socket_accept(serverfd, nullptr, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800225 if(fd >= 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700226 D("server: new connection on fd %d", fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800227 close_on_exec(fd);
228 disable_tcp_nagle(fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700229 if (register_socket_transport(fd, "host", port, 1) != 0) {
230 adb_close(fd);
231 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232 }
233 }
Yabin Cui815ad882015-09-02 17:44:28 -0700234 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235}
236
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800237/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800238/*
239 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200240 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800241 */
242#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200243#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800244#undef write
245#define open adb_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200246#define read adb_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800247#define write adb_write
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200248#include <system/qemu_pipe.h>
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800249#undef open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200250#undef read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800251#undef write
252#define open ___xxx_open
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200253#define read ___xxx_read
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800254#define write ___xxx_write
255
256/* A worker thread that monitors host connections, and registers a transport for
257 * every new host connection. This thread replaces server_socket_thread on
258 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
259 * pipe to communicate with adbd daemon inside the guest. This is done in order
260 * to provide more robust communication channel between ADB host and guest. The
261 * main issue with server_socket_thread approach is that it runs on top of TCP,
262 * and thus is sensitive to network disruptions. For instance, the
263 * ConnectionManager may decide to reset all network connections, in which case
264 * the connection between ADB host and guest will be lost. To make ADB traffic
265 * independent from the network, we use here 'adb' QEMUD service to transfer data
266 * between the host, and the guest. See external/qemu/android/adb-*.* that
267 * implements the emulator's side of the protocol. Another advantage of using
268 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
269 * anymore on network being set up.
270 * The guest side of the protocol contains the following phases:
271 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
272 * is opened, and it becomes clear whether or not emulator supports that
273 * protocol.
274 * - Wait for the ADB host to create connection with the guest. This is done by
275 * sending an 'accept' request to the adb QEMUD service, and waiting on
276 * response.
277 * - When new ADB host connection is accepted, the connection with adb QEMUD
278 * service is registered as the transport, and a 'start' request is sent to the
279 * adb QEMUD service, indicating that the guest is ready to receive messages.
280 * Note that the guest will ignore messages sent down from the emulator before
281 * the transport registration is completed. That's why we need to send the
282 * 'start' request after the transport is registered.
283 */
Josh Gao382ea6e2016-02-12 14:31:15 -0800284static void qemu_socket_thread(void* arg) {
285 /* 'accept' request to the adb QEMUD service. */
286 static const char _accept_req[] = "accept";
287 /* 'start' request to the adb QEMUD service. */
288 static const char _start_req[] = "start";
289 /* 'ok' reply from the adb QEMUD service. */
290 static const char _ok_resp[] = "ok";
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800291
Elliott Hughes9c0d9402014-01-16 10:53:11 -0800292 const int port = (int) (uintptr_t) arg;
bohuf4ae8fa2016-02-29 13:13:19 -0800293 int fd;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800294 char tmp[256];
295 char con_name[32];
296
Siva Velusamy2669cf92015-08-28 16:37:29 -0700297 adb_thread_setname("qemu socket");
Yabin Cui815ad882015-09-02 17:44:28 -0700298 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800299
300 /* adb QEMUD service connection request. */
David 'Digit' Turnerb82031f2016-06-16 17:05:52 +0200301 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800302
303 /* Connect to the adb QEMUD service. */
304 fd = qemu_pipe_open(con_name);
305 if (fd < 0) {
306 /* This could be an older version of the emulator, that doesn't
307 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui815ad882015-09-02 17:44:28 -0700308 D("adb service is not available. Falling back to TCP socket.");
Elliott Hughesf2517142015-05-05 13:41:21 -0700309 adb_thread_create(server_socket_thread, arg);
Josh Gao382ea6e2016-02-12 14:31:15 -0800310 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800311 }
312
313 for(;;) {
314 /*
315 * Wait till the host creates a new connection.
316 */
317
318 /* Send the 'accept' request. */
bohuf4ae8fa2016-02-29 13:13:19 -0800319 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800320 /* Wait for the response. In the response we expect 'ok' on success,
321 * or 'ko' on failure. */
bohuf4ae8fa2016-02-29 13:13:19 -0800322 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700323 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800324 adb_close(fd);
325 } else {
326 /* Host is connected. Register the transport, and start the
327 * exchange. */
Prathmesh Prabhuefa49792016-03-09 13:51:30 -0800328 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cuic7221782016-05-17 15:14:25 -0700329 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
330 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohuf4ae8fa2016-02-29 13:13:19 -0800331 adb_close(fd);
332 }
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800333 }
334
335 /* Prepare for accepting of the next ADB host connection. */
336 fd = qemu_pipe_open(con_name);
337 if (fd < 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700338 D("adb service become unavailable.");
Josh Gao382ea6e2016-02-12 14:31:15 -0800339 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800340 }
341 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700342 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gao382ea6e2016-02-12 14:31:15 -0800343 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800344 }
345 }
Yabin Cui815ad882015-09-02 17:44:28 -0700346 D("transport: qemu_socket_thread() exiting");
Josh Gao382ea6e2016-02-12 14:31:15 -0800347 return;
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800348}
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800349#endif // !ADB_HOST
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800350
Mike Lockwood26b88e32009-08-24 15:58:40 -0700351void local_init(int port)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800352{
Josh Gao382ea6e2016-02-12 14:31:15 -0800353 adb_thread_func_t func;
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700354 const char* debug_name = "";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800355
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800356#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700357 func = client_socket_thread;
358 debug_name = "client";
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800359#else
Elliott Hughes8b249d22016-09-23 15:40:03 -0700360 // For the adbd daemon in the system image we need to distinguish
361 // between the device, and the emulator.
362 if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {
363 // Running inside the emulator: use QEMUD pipe as the transport.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700364 func = qemu_socket_thread;
365 } else {
Elliott Hughes8b249d22016-09-23 15:40:03 -0700366 // Running inside the device: use TCP socket as the transport.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700367 func = server_socket_thread;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700369 debug_name = "server";
370#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371
Yabin Cui815ad882015-09-02 17:44:28 -0700372 D("transport: local %s init", debug_name);
Elliott Hughesf2517142015-05-05 13:41:21 -0700373 if (!adb_thread_create(func, (void *) (uintptr_t) port)) {
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700374 fatal_errno("cannot create local socket %s thread", debug_name);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375 }
376}
377
378static void remote_kick(atransport *t)
379{
380 int fd = t->sfd;
381 t->sfd = -1;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400382 adb_shutdown(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383 adb_close(fd);
384
385#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700386 int nn;
Josh Gaoe7daf572016-09-21 12:37:10 -0700387 std::lock_guard<std::mutex> lock(local_transports_lock);
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700388 for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
389 if (local_transports[nn] == t) {
390 local_transports[nn] = NULL;
391 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800393 }
394#endif
395}
396
397static void remote_close(atransport *t)
398{
Spencer Low6f38e232015-06-09 12:32:17 -0700399 int fd = t->sfd;
400 if (fd != -1) {
401 t->sfd = -1;
402 adb_close(fd);
403 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700404#if ADB_HOST
405 int local_port;
406 if (t->GetLocalPortForEmulator(&local_port)) {
407 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port;
408 std::unique_lock<std::mutex> lock(retry_ports_lock);
409 RetryPort port;
410 port.port = local_port;
411 port.retry_count = LOCAL_PORT_RETRY_COUNT;
412 retry_ports.push_back(port);
413 retry_ports_cond.notify_one();
414 }
415#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416}
417
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100418
419#if ADB_HOST
420/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700421static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100422{
423 int i;
424 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700425 int local_port;
426 if (local_transports[i] && local_transports[i]->GetLocalPortForEmulator(&local_port)) {
427 if (local_port == adb_port) {
428 return local_transports[i];
429 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100430 }
431 }
432 return NULL;
433}
434
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700435std::string getEmulatorSerialString(int console_port)
436{
437 return android::base::StringPrintf("emulator-%d", console_port);
438}
439
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100440atransport* find_emulator_transport_by_adb_port(int adb_port)
441{
Josh Gaoe7daf572016-09-21 12:37:10 -0700442 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100443 atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100444 return result;
445}
446
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700447atransport* find_emulator_transport_by_console_port(int console_port)
448{
449 return find_transport(getEmulatorSerialString(console_port).c_str());
450}
451
452
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100453/* Only call this function if you already hold local_transports_lock. */
454int get_available_local_transport_index_locked()
455{
456 int i;
457 for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
458 if (local_transports[i] == NULL) {
459 return i;
460 }
461 }
462 return -1;
463}
464
465int get_available_local_transport_index()
466{
Josh Gaoe7daf572016-09-21 12:37:10 -0700467 std::lock_guard<std::mutex> lock(local_transports_lock);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100468 int result = get_available_local_transport_index_locked();
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100469 return result;
470}
471#endif
472
473int init_socket_transport(atransport *t, int s, int adb_port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800474{
475 int fail = 0;
476
Yabin Cuiefc525a2016-04-18 11:22:34 -0700477 t->SetKickFunction(remote_kick);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800478 t->close = remote_close;
479 t->read_from_remote = remote_read;
480 t->write_to_remote = remote_write;
481 t->sfd = s;
482 t->sync_token = 1;
Dan Albert9a50f4c2015-05-18 16:43:57 -0700483 t->connection_state = kCsOffline;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800484 t->type = kTransportLocal;
485
486#if ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700487 if (local) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700488 std::lock_guard<std::mutex> lock(local_transports_lock);
489 t->SetLocalPortForEmulator(adb_port);
490 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
491 int index = get_available_local_transport_index_locked();
492 if (existing_transport != NULL) {
493 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
494 fail = -1;
495 } else if (index < 0) {
496 // Too many emulators.
497 D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
498 fail = -1;
499 } else {
500 local_transports[index] = t;
501 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800502 }
503#endif
504 return fail;
505}