blob: 560a0312b5c1841df5235252dda9f45895d8b6dc [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"
20#include "transport.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Pirama Arumuga Nainar7eaef8a2016-09-06 13:34:42 -070028#include <condition_variable>
Josh Gao0cd3ae12016-09-21 12:37:10 -070029#include <mutex>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080030#include <thread>
Josh Gaob800d882018-01-28 20:32:46 -080031#include <unordered_map>
Yabin Cuib74c6492016-04-29 16:53:52 -070032#include <vector>
33
Casey Dahlin13a269e2016-06-23 14:19:37 -070034#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
Josh Gaob800d882018-01-28 20:32:46 -080036#include <android-base/thread_annotations.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070037#include <cutils/sockets.h>
Elliott Hughesab52c182015-05-01 17:04:38 -070038
Dan Albert76649012015-02-24 15:51:19 -080039#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070040#include <android-base/properties.h>
Dan Albert76649012015-02-24 15:51:19 -080041#endif
Dan Albert33134262015-03-19 15:21:08 -070042
43#include "adb.h"
44#include "adb_io.h"
Josh Gaob800d882018-01-28 20:32:46 -080045#include "adb_unique_fd.h"
Elliott Hughes381cfa92015-07-23 17:12:58 -070046#include "adb_utils.h"
Josh Gao4602adb2016-11-15 18:55:47 -080047#include "sysdeps/chrono.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#if ADB_HOST
Josh Gao9fe74262016-05-06 02:37:24 -070050
51// Android Wear has been using port 5601 in all of its documentation/tooling,
52// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
53// Avoid stomping on their port by limiting the number of emulators that can be
54// connected.
55#define ADB_LOCAL_TRANSPORT_MAX 16
56
Josh Gao0cd3ae12016-09-21 12:37:10 -070057static std::mutex& local_transports_lock = *new std::mutex();
Josh Gao9fe74262016-05-06 02:37:24 -070058
Josh Gaob800d882018-01-28 20:32:46 -080059// We keep a map from emulator port to transport.
60// TODO: weak_ptr?
61static auto& local_transports GUARDED_BY(local_transports_lock) =
62 *new std::unordered_map<int, atransport*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#endif /* ADB_HOST */
64
Yabin Cuib74c6492016-04-29 16:53:52 -070065bool local_connect(int port) {
Elliott Hughes381cfa92015-07-23 17:12:58 -070066 std::string dummy;
Josh Gaob800d882018-01-28 20:32:46 -080067 return local_connect_arbitrary_ports(port - 1, port, &dummy) == 0;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +010068}
69
Casey Dahlin13a269e2016-06-23 14:19:37 -070070void connect_device(const std::string& address, std::string* response) {
71 if (address.empty()) {
72 *response = "empty address";
73 return;
74 }
75
76 std::string serial;
77 std::string host;
78 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
79 if (!android::base::ParseNetAddress(address, &host, &port, &serial, response)) {
80 return;
81 }
82
83 std::string error;
84 int fd = network_connect(host.c_str(), port, SOCK_STREAM, 10, &error);
85 if (fd == -1) {
86 *response = android::base::StringPrintf("unable to connect to %s: %s",
87 serial.c_str(), error.c_str());
88 return;
89 }
90
91 D("client: connected %s remote on fd %d", serial.c_str(), fd);
92 close_on_exec(fd);
93 disable_tcp_nagle(fd);
94
95 // Send a TCP keepalive ping to the device every second so we can detect disconnects.
96 if (!set_tcp_keepalive(fd, 1)) {
97 D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
98 }
99
100 int ret = register_socket_transport(fd, serial.c_str(), port, 0);
101 if (ret < 0) {
102 adb_close(fd);
103 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
104 } else {
105 *response = android::base::StringPrintf("connected to %s", serial.c_str());
106 }
107}
108
109
Elliott Hughes381cfa92015-07-23 17:12:58 -0700110int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
111 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112
113#if ADB_HOST
Lingfeng Yang11979522016-10-06 12:22:55 -0700114 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
115 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cui0e2c1942015-07-31 14:10:54 -0700116 return -1;
117 }
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 const char *host = getenv("ADBHOST");
120 if (host) {
Elliott Hughes381cfa92015-07-23 17:12:58 -0700121 fd = network_connect(host, adb_port, SOCK_STREAM, 0, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 }
123#endif
124 if (fd < 0) {
Spencer Low5200c662015-07-30 23:07:55 -0700125 fd = network_loopback_client(adb_port, SOCK_STREAM, error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 }
127
128 if (fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700129 D("client: connected on remote on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 close_on_exec(fd);
131 disable_tcp_nagle(fd);
Lingfeng Yang11979522016-10-06 12:22:55 -0700132 std::string serial = getEmulatorSerialString(console_port);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700133 if (register_socket_transport(fd, serial.c_str(), adb_port, 1) == 0) {
134 return 0;
135 }
136 adb_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
138 return -1;
139}
140
Yabin Cui661327e2015-08-11 13:40:42 -0700141#if ADB_HOST
Yabin Cuib74c6492016-04-29 16:53:52 -0700142
143static void PollAllLocalPortsForEmulator() {
144 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
145 int count = ADB_LOCAL_TRANSPORT_MAX;
146
147 // Try to connect to any number of running emulator instances.
148 for ( ; count > 0; count--, port += 2 ) {
149 local_connect(port);
150 }
151}
152
153// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
154constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
Josh Gao4602adb2016-11-15 18:55:47 -0800155constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
Yabin Cuib74c6492016-04-29 16:53:52 -0700156
157struct RetryPort {
158 int port;
159 uint32_t retry_count;
160};
161
162// Retry emulators just kicked.
163static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
164std::mutex &retry_ports_lock = *new std::mutex;
165std::condition_variable &retry_ports_cond = *new std::condition_variable;
166
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700167static void client_socket_thread(int) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700168 adb_thread_setname("client_socket_thread");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700169 D("transport: client_socket_thread() starting");
Yabin Cuib74c6492016-04-29 16:53:52 -0700170 PollAllLocalPortsForEmulator();
Yabin Cui0e2c1942015-07-31 14:10:54 -0700171 while (true) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700172 std::vector<RetryPort> ports;
173 // Collect retry ports.
174 {
175 std::unique_lock<std::mutex> lock(retry_ports_lock);
176 while (retry_ports.empty()) {
177 retry_ports_cond.wait(lock);
178 }
179 retry_ports.swap(ports);
Yabin Cui0e2c1942015-07-31 14:10:54 -0700180 }
Yabin Cuib74c6492016-04-29 16:53:52 -0700181 // Sleep here instead of the end of loop, because if we immediately try to reconnect
182 // the emulator just kicked, the adbd on the emulator may not have time to remove the
183 // just kicked transport.
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800184 std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
Yabin Cuib74c6492016-04-29 16:53:52 -0700185
186 // Try connecting retry ports.
187 std::vector<RetryPort> next_ports;
188 for (auto& port : ports) {
189 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
190 << port.retry_count;
191 if (local_connect(port.port)) {
192 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
193 continue;
194 }
195 if (--port.retry_count > 0) {
196 next_ports.push_back(port);
197 } else {
198 VLOG(TRANSPORT) << "stop retrying port " << port.port;
199 }
200 }
201
202 // Copy back left retry ports.
203 {
204 std::unique_lock<std::mutex> lock(retry_ports_lock);
205 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
206 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208}
209
Yabin Cui661327e2015-08-11 13:40:42 -0700210#else // ADB_HOST
211
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700212static void server_socket_thread(int port) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 int serverfd, fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700215 adb_thread_setname("server socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700216 D("transport: server_socket_thread() starting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 serverfd = -1;
218 for(;;) {
219 if(serverfd == -1) {
Spencer Low5200c662015-07-30 23:07:55 -0700220 std::string error;
221 serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if(serverfd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700223 D("server: cannot bind socket yet: %s", error.c_str());
Josh Gao4602adb2016-11-15 18:55:47 -0800224 std::this_thread::sleep_for(1s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 continue;
226 }
227 close_on_exec(serverfd);
228 }
229
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700230 D("server: trying to get new connection from %d", port);
Josh Gao78e1eb12016-08-23 15:41:56 -0700231 fd = adb_socket_accept(serverfd, nullptr, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 if(fd >= 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700233 D("server: new connection on fd %d", fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 close_on_exec(fd);
235 disable_tcp_nagle(fd);
Tao Wu043912e2016-10-21 15:43:57 -0700236 std::string serial = android::base::StringPrintf("host-%d", fd);
237 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0) {
Yabin Cui01401822016-05-17 15:14:25 -0700238 adb_close(fd);
239 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 }
241 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700242 D("transport: server_socket_thread() exiting");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243}
244
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800245/* This is relevant only for ADB daemon running inside the emulator. */
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800246/*
247 * Redefine open and write for qemu_pipe.h that contains inlined references
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200248 * to those routines. We will redefine them back after qemu_pipe.h inclusion.
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800249 */
250#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200251#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800252#undef write
253#define open adb_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200254#define read adb_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800255#define write adb_write
bohuf66a7ee2017-03-29 12:19:40 -0700256#include <qemu_pipe.h>
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800257#undef open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200258#undef read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800259#undef write
260#define open ___xxx_open
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200261#define read ___xxx_read
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800262#define write ___xxx_write
263
264/* A worker thread that monitors host connections, and registers a transport for
265 * every new host connection. This thread replaces server_socket_thread on
266 * condition that adbd daemon runs inside the emulator, and emulator uses QEMUD
267 * pipe to communicate with adbd daemon inside the guest. This is done in order
268 * to provide more robust communication channel between ADB host and guest. The
269 * main issue with server_socket_thread approach is that it runs on top of TCP,
270 * and thus is sensitive to network disruptions. For instance, the
271 * ConnectionManager may decide to reset all network connections, in which case
272 * the connection between ADB host and guest will be lost. To make ADB traffic
273 * independent from the network, we use here 'adb' QEMUD service to transfer data
274 * between the host, and the guest. See external/qemu/android/adb-*.* that
275 * implements the emulator's side of the protocol. Another advantage of using
276 * QEMUD approach is that ADB will be up much sooner, since it doesn't depend
277 * anymore on network being set up.
278 * The guest side of the protocol contains the following phases:
279 * - Connect with adb QEMUD service. In this phase a handle to 'adb' QEMUD service
280 * is opened, and it becomes clear whether or not emulator supports that
281 * protocol.
282 * - Wait for the ADB host to create connection with the guest. This is done by
283 * sending an 'accept' request to the adb QEMUD service, and waiting on
284 * response.
285 * - When new ADB host connection is accepted, the connection with adb QEMUD
286 * service is registered as the transport, and a 'start' request is sent to the
287 * adb QEMUD service, indicating that the guest is ready to receive messages.
288 * Note that the guest will ignore messages sent down from the emulator before
289 * the transport registration is completed. That's why we need to send the
290 * 'start' request after the transport is registered.
291 */
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700292static void qemu_socket_thread(int port) {
Josh Gaod9db09c2016-02-12 14:31:15 -0800293 /* 'accept' request to the adb QEMUD service. */
294 static const char _accept_req[] = "accept";
295 /* 'start' request to the adb QEMUD service. */
296 static const char _start_req[] = "start";
297 /* 'ok' reply from the adb QEMUD service. */
298 static const char _ok_resp[] = "ok";
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800299
bohu8ac1b042016-02-29 13:13:19 -0800300 int fd;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800301 char tmp[256];
302 char con_name[32];
303
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700304 adb_thread_setname("qemu socket");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700305 D("transport: qemu_socket_thread() starting");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800306
307 /* adb QEMUD service connection request. */
David 'Digit' Turnerc7b098c2016-06-16 17:05:52 +0200308 snprintf(con_name, sizeof(con_name), "pipe:qemud:adb:%d", port);
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800309
310 /* Connect to the adb QEMUD service. */
311 fd = qemu_pipe_open(con_name);
312 if (fd < 0) {
313 /* This could be an older version of the emulator, that doesn't
314 * implement adb QEMUD service. Fall back to the old TCP way. */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700315 D("adb service is not available. Falling back to TCP socket.");
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700316 std::thread(server_socket_thread, port).detach();
Josh Gaod9db09c2016-02-12 14:31:15 -0800317 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800318 }
319
320 for(;;) {
321 /*
322 * Wait till the host creates a new connection.
323 */
324
325 /* Send the 'accept' request. */
bohu8ac1b042016-02-29 13:13:19 -0800326 if (WriteFdExactly(fd, _accept_req, strlen(_accept_req))) {
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800327 /* Wait for the response. In the response we expect 'ok' on success,
328 * or 'ko' on failure. */
bohu8ac1b042016-02-29 13:13:19 -0800329 if (!ReadFdExactly(fd, tmp, 2) || memcmp(tmp, _ok_resp, 2)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700330 D("Accepting ADB host connection has failed.");
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800331 adb_close(fd);
332 } else {
333 /* Host is connected. Register the transport, and start the
334 * exchange. */
Prathmesh Prabhu251d46e2016-03-09 13:51:30 -0800335 std::string serial = android::base::StringPrintf("host-%d", fd);
Yabin Cui01401822016-05-17 15:14:25 -0700336 if (register_socket_transport(fd, serial.c_str(), port, 1) != 0 ||
337 !WriteFdExactly(fd, _start_req, strlen(_start_req))) {
bohu8ac1b042016-02-29 13:13:19 -0800338 adb_close(fd);
339 }
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800340 }
341
342 /* Prepare for accepting of the next ADB host connection. */
343 fd = qemu_pipe_open(con_name);
344 if (fd < 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("adb service become unavailable.");
Josh Gaod9db09c2016-02-12 14:31:15 -0800346 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800347 }
348 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700349 D("Unable to send the '%s' request to ADB service.", _accept_req);
Josh Gaod9db09c2016-02-12 14:31:15 -0800350 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800351 }
352 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700353 D("transport: qemu_socket_thread() exiting");
Josh Gaod9db09c2016-02-12 14:31:15 -0800354 return;
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800355}
Nicolas Norvez717f1532017-06-06 13:38:20 -0700356
357// If adbd is running inside the emulator, it will normally use QEMUD pipe (aka
358// goldfish) as the transport. This can either be explicitly set by the
359// service.adb.transport property, or be inferred from ro.kernel.qemu that is
360// set to "1" for ranchu/goldfish.
361static bool use_qemu_goldfish() {
362 // Legacy way to detect if adbd should use the goldfish pipe is to check for
363 // ro.kernel.qemu, keep that behaviour for backward compatibility.
364 if (android::base::GetBoolProperty("ro.kernel.qemu", false)) {
365 return true;
366 }
367 // If service.adb.transport is present and is set to "goldfish", use the
368 // QEMUD pipe.
369 if (android::base::GetProperty("service.adb.transport", "") == "goldfish") {
370 return true;
371 }
372 return false;
373}
374
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800375#endif // !ADB_HOST
Vladimir Chtchetkinec13daef2011-12-09 15:49:47 -0800376
Mike Lockwoodff196702009-08-24 15:58:40 -0700377void local_init(int port)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378{
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700379 void (*func)(int);
Yabin Cui661327e2015-08-11 13:40:42 -0700380 const char* debug_name = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800382#if ADB_HOST
Yabin Cui661327e2015-08-11 13:40:42 -0700383 func = client_socket_thread;
384 debug_name = "client";
Vladimir Chtchetkinec4f37ee2011-12-13 12:19:29 -0800385#else
Elliott Hughesffdec182016-09-23 15:40:03 -0700386 // For the adbd daemon in the system image we need to distinguish
387 // between the device, and the emulator.
Nicolas Norvez717f1532017-06-06 13:38:20 -0700388 func = use_qemu_goldfish() ? qemu_socket_thread : server_socket_thread;
Yabin Cui661327e2015-08-11 13:40:42 -0700389 debug_name = "server";
390#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700392 D("transport: local %s init", debug_name);
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700393 std::thread(func, port).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394}
395
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396#if ADB_HOST
Josh Gaob800d882018-01-28 20:32:46 -0800397struct EmulatorConnection : public FdConnection {
398 EmulatorConnection(unique_fd fd, int local_port)
399 : FdConnection(std::move(fd)), local_port_(local_port) {}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400
Josh Gaob800d882018-01-28 20:32:46 -0800401 ~EmulatorConnection() {
402 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port_;
Yabin Cuib74c6492016-04-29 16:53:52 -0700403 std::unique_lock<std::mutex> lock(retry_ports_lock);
404 RetryPort port;
Josh Gaob800d882018-01-28 20:32:46 -0800405 port.port = local_port_;
Yabin Cuib74c6492016-04-29 16:53:52 -0700406 port.retry_count = LOCAL_PORT_RETRY_COUNT;
407 retry_ports.push_back(port);
408 retry_ports_cond.notify_one();
409 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410
Josh Gaob800d882018-01-28 20:32:46 -0800411 void Close() override {
412 std::lock_guard<std::mutex> lock(local_transports_lock);
413 local_transports.erase(local_port_);
414 FdConnection::Close();
415 }
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100416
Josh Gaob800d882018-01-28 20:32:46 -0800417 int local_port_;
418};
419
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100420/* Only call this function if you already hold local_transports_lock. */
Yabin Cuib74c6492016-04-29 16:53:52 -0700421static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Josh Gaob800d882018-01-28 20:32:46 -0800422 REQUIRES(local_transports_lock) {
423 auto it = local_transports.find(adb_port);
424 if (it == local_transports.end()) {
425 return nullptr;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100426 }
Josh Gaob800d882018-01-28 20:32:46 -0800427 return it->second;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100428}
429
Josh Gaob800d882018-01-28 20:32:46 -0800430std::string getEmulatorSerialString(int console_port) {
Lingfeng Yang11979522016-10-06 12:22:55 -0700431 return android::base::StringPrintf("emulator-%d", console_port);
432}
433
Josh Gaob800d882018-01-28 20:32:46 -0800434atransport* find_emulator_transport_by_adb_port(int adb_port) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700435 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gaob800d882018-01-28 20:32:46 -0800436 return find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100437}
438
Josh Gaob800d882018-01-28 20:32:46 -0800439atransport* find_emulator_transport_by_console_port(int console_port) {
Lingfeng Yang11979522016-10-06 12:22:55 -0700440 return find_transport(getEmulatorSerialString(console_port).c_str());
441}
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100442#endif
443
Josh Gaob800d882018-01-28 20:32:46 -0800444int init_socket_transport(atransport* t, int s, int adb_port, int local) {
445 int fail = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446
Josh Gaob800d882018-01-28 20:32:46 -0800447 unique_fd fd(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 t->sync_token = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 t->type = kTransportLocal;
450
451#if ADB_HOST
Josh Gaob800d882018-01-28 20:32:46 -0800452 // Emulator connection.
Yabin Cui661327e2015-08-11 13:40:42 -0700453 if (local) {
Josh Gaob800d882018-01-28 20:32:46 -0800454 t->connection.reset(new EmulatorConnection(std::move(fd), adb_port));
Josh Gao0cd3ae12016-09-21 12:37:10 -0700455 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700456 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700457 if (existing_transport != NULL) {
458 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
459 fail = -1;
Josh Gaob800d882018-01-28 20:32:46 -0800460 } else if (local_transports.size() >= ADB_LOCAL_TRANSPORT_MAX) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700461 // Too many emulators.
462 D("cannot register more emulators. Maximum is %d", ADB_LOCAL_TRANSPORT_MAX);
463 fail = -1;
464 } else {
Josh Gaob800d882018-01-28 20:32:46 -0800465 local_transports[adb_port] = t;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700466 }
Josh Gaob800d882018-01-28 20:32:46 -0800467
468 return fail;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 }
470#endif
Josh Gaob800d882018-01-28 20:32:46 -0800471
472 // Regular tcp connection.
473 t->connection.reset(new FdConnection(std::move(fd)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 return fail;
475}