blob: b9f738dfce273f821f6f6b7032141b75b03d71d9 [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>
Cody Schuffelen637aaf52019-01-04 18:51:11 -080029#include <functional>
Pirama Arumuga Nainar5231aff2018-08-08 10:33:24 -070030#include <memory>
Josh Gaoe7daf572016-09-21 12:37:10 -070031#include <mutex>
Elliott Hughes73925982016-11-15 12:37:32 -080032#include <thread>
Josh Gao395b86a2018-01-28 20:32:46 -080033#include <unordered_map>
Yabin Cuif401ead2016-04-29 16:53:52 -070034#include <vector>
35
Casey Dahlin3122cdf2016-06-23 14:19:37 -070036#include <android-base/parsenetaddress.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080037#include <android-base/stringprintf.h>
Josh Gao395b86a2018-01-28 20:32:46 -080038#include <android-base/thread_annotations.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070039#include <cutils/sockets.h>
Elliott Hughesfb596842015-05-01 17:04:38 -070040
Dan Albertb302d122015-02-24 15:51:19 -080041#if !ADB_HOST
Elliott Hughes8b249d22016-09-23 15:40:03 -070042#include <android-base/properties.h>
Dan Albertb302d122015-02-24 15:51:19 -080043#endif
Dan Albertdb6fe642015-03-19 15:21:08 -070044
45#include "adb.h"
46#include "adb_io.h"
Josh Gao395b86a2018-01-28 20:32:46 -080047#include "adb_unique_fd.h"
Elliott Hughes43df1092015-07-23 17:12:58 -070048#include "adb_utils.h"
Cody Schuffelen331a9082019-01-02 14:17:29 -080049#include "socket_spec.h"
Josh Gao70267e42016-11-15 18:55:47 -080050#include "sysdeps/chrono.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080052#if ADB_HOST
Josh Gao89513a52016-05-06 02:37:24 -070053
54// Android Wear has been using port 5601 in all of its documentation/tooling,
55// but we search for emulators on ports [5554, 5555 + ADB_LOCAL_TRANSPORT_MAX].
Tim Baverstock96ee6b12019-02-08 16:27:16 +000056// Avoid stomping on their port by restricting the active scanning range.
57// Once emulators self-(re-)register, they'll have to avoid 5601 in their own way.
58static int adb_local_transport_max_port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT + 16 * 2 - 1;
Josh Gao89513a52016-05-06 02:37:24 -070059
Josh Gaoe7daf572016-09-21 12:37:10 -070060static std::mutex& local_transports_lock = *new std::mutex();
Josh Gao89513a52016-05-06 02:37:24 -070061
Tim Baverstock96ee6b12019-02-08 16:27:16 +000062static void adb_local_transport_max_port_env_override() {
63 const char* env_max_s = getenv("ADB_LOCAL_TRANSPORT_MAX_PORT");
64 if (env_max_s != nullptr) {
65 size_t env_max;
66 if (ParseUint(&env_max, env_max_s, nullptr) && env_max < 65536) {
67 // < DEFAULT_ADB_LOCAL_TRANSPORT_PORT harmlessly mimics ADB_EMU=0
68 adb_local_transport_max_port = env_max;
69 D("transport: ADB_LOCAL_TRANSPORT_MAX_PORT read as %d", adb_local_transport_max_port);
70 } else {
71 D("transport: ADB_LOCAL_TRANSPORT_MAX_PORT '%s' invalid or >= 65536, so ignored",
72 env_max_s);
73 }
74 }
75}
76
Josh Gao395b86a2018-01-28 20:32:46 -080077// We keep a map from emulator port to transport.
78// TODO: weak_ptr?
79static auto& local_transports GUARDED_BY(local_transports_lock) =
80 *new std::unordered_map<int, atransport*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081#endif /* ADB_HOST */
82
Yabin Cuif401ead2016-04-29 16:53:52 -070083bool local_connect(int port) {
Elliott Hughes43df1092015-07-23 17:12:58 -070084 std::string dummy;
Josh Gao395b86a2018-01-28 20:32:46 -080085 return local_connect_arbitrary_ports(port - 1, port, &dummy) == 0;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +010086}
87
Luis Hector Chavez0aeda102018-04-20 10:31:29 -070088std::tuple<unique_fd, int, std::string> tcp_connect(const std::string& address,
89 std::string* response) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080090 unique_fd fd;
Casey Dahlin3122cdf2016-06-23 14:19:37 -070091 int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT;
Cody Schuffelen331a9082019-01-02 14:17:29 -080092 std::string serial;
Cody Schuffelen637aaf52019-01-04 18:51:11 -080093 std::string prefix_addr = address.starts_with("vsock:") ? address : "tcp:" + address;
94 if (socket_spec_connect(&fd, prefix_addr, &port, &serial, response)) {
Cody Schuffelen331a9082019-01-02 14:17:29 -080095 close_on_exec(fd);
96 if (!set_tcp_keepalive(fd, 1)) {
97 D("warning: failed to configure TCP keepalives (%s)", strerror(errno));
98 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -070099 return std::make_tuple(std::move(fd), port, serial);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700100 }
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800101 return std::make_tuple(unique_fd(), 0, serial);
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700102}
103
104void connect_device(const std::string& address, std::string* response) {
105 if (address.empty()) {
106 *response = "empty address";
107 return;
108 }
109
Josh Gao8d7080c2018-08-10 16:03:09 -0700110 D("connection requested to '%s'", address.c_str());
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700111 unique_fd fd;
112 int port;
113 std::string serial;
114 std::tie(fd, port, serial) = tcp_connect(address, response);
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800115 if (fd.get() == -1) {
116 return;
117 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700118 auto reconnect = [address](atransport* t) {
119 std::string response;
120 unique_fd fd;
121 int port;
122 std::string serial;
123 std::tie(fd, port, serial) = tcp_connect(address, &response);
124 if (fd == -1) {
125 D("reconnect failed: %s", response.c_str());
Josh Gaod24580d2018-08-30 11:37:00 -0700126 return ReconnectResult::Retry;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700127 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700128 // This invokes the part of register_socket_transport() that needs to be
129 // invoked if the atransport* has already been setup. This eventually
130 // calls atransport->SetConnection() with a newly created Connection*
131 // that will in turn send the CNXN packet.
Josh Gaod24580d2018-08-30 11:37:00 -0700132 return init_socket_transport(t, std::move(fd), port, 0) >= 0 ? ReconnectResult::Success
133 : ReconnectResult::Retry;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700134 };
135
Josh Gao597044d2018-08-08 16:20:14 -0700136 int error;
137 if (!register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect), &error)) {
138 if (error == EALREADY) {
Luis Hector Chavez77719202018-04-17 14:09:21 -0700139 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
Josh Gao597044d2018-08-08 16:20:14 -0700140 } else if (error == EPERM) {
141 *response = android::base::StringPrintf("failed to authenticate to %s", serial.c_str());
Luis Hector Chavez77719202018-04-17 14:09:21 -0700142 } else {
143 *response = android::base::StringPrintf("failed to connect to %s", serial.c_str());
144 }
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700145 } else {
146 *response = android::base::StringPrintf("connected to %s", serial.c_str());
147 }
148}
149
150
Elliott Hughes43df1092015-07-23 17:12:58 -0700151int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700152 unique_fd fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153
154#if ADB_HOST
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700155 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
156 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700157 return -1;
158 }
159
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160 const char *host = getenv("ADBHOST");
161 if (host) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700162 fd.reset(network_connect(host, adb_port, SOCK_STREAM, 0, error));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800163 }
164#endif
165 if (fd < 0) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700166 fd.reset(network_loopback_client(adb_port, SOCK_STREAM, error));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800167 }
168
169 if (fd >= 0) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700170 D("client: connected on remote on fd %d", fd.get());
171 close_on_exec(fd.get());
172 disable_tcp_nagle(fd.get());
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700173 std::string serial = getEmulatorSerialString(console_port);
Josh Gaoa3a71472018-08-02 13:58:24 -0700174 if (register_socket_transport(std::move(fd), std::move(serial), adb_port, 1,
Josh Gaod24580d2018-08-30 11:37:00 -0700175 [](atransport*) { return ReconnectResult::Abort; })) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700176 return 0;
177 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 }
179 return -1;
180}
181
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700182#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700183
184static void PollAllLocalPortsForEmulator() {
Yabin Cuif401ead2016-04-29 16:53:52 -0700185 // Try to connect to any number of running emulator instances.
Tim Baverstock96ee6b12019-02-08 16:27:16 +0000186 for (int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; port <= adb_local_transport_max_port;
187 port += 2) {
188 local_connect(port); // Note, uses port and port-1, so '=max_port' is OK.
Yabin Cuif401ead2016-04-29 16:53:52 -0700189 }
190}
191
192// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
Elliott Hughesc3462f42018-09-05 12:13:11 -0700193static constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
194static constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
Yabin Cuif401ead2016-04-29 16:53:52 -0700195
196struct RetryPort {
197 int port;
198 uint32_t retry_count;
199};
200
201// Retry emulators just kicked.
202static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
203std::mutex &retry_ports_lock = *new std::mutex;
204std::condition_variable &retry_ports_cond = *new std::condition_variable;
205
Josh Gao0f3312a2017-04-12 17:00:49 -0700206static void client_socket_thread(int) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700207 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700208 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700209 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700210 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700211 std::vector<RetryPort> ports;
212 // Collect retry ports.
213 {
214 std::unique_lock<std::mutex> lock(retry_ports_lock);
215 while (retry_ports.empty()) {
216 retry_ports_cond.wait(lock);
217 }
218 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700219 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700220 // Sleep here instead of the end of loop, because if we immediately try to reconnect
221 // the emulator just kicked, the adbd on the emulator may not have time to remove the
222 // just kicked transport.
Elliott Hughes73925982016-11-15 12:37:32 -0800223 std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
Yabin Cuif401ead2016-04-29 16:53:52 -0700224
225 // Try connecting retry ports.
226 std::vector<RetryPort> next_ports;
227 for (auto& port : ports) {
228 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
229 << port.retry_count;
230 if (local_connect(port.port)) {
231 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
232 continue;
233 }
234 if (--port.retry_count > 0) {
235 next_ports.push_back(port);
236 } else {
237 VLOG(TRANSPORT) << "stop retrying port " << port.port;
238 }
239 }
240
241 // Copy back left retry ports.
242 {
243 std::unique_lock<std::mutex> lock(retry_ports_lock);
244 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
245 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247}
248
Josh Gao0560feb2019-01-22 19:36:15 -0800249#else // !ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700250
Josh Gao80e81922019-01-30 13:59:51 -0800251void server_socket_thread(std::function<unique_fd(int, std::string*)> listen_func, int port) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700252 adb_thread_setname("server socket");
Josh Gao80e81922019-01-30 13:59:51 -0800253
254 unique_fd serverfd;
255 std::string error;
256
Josh Gaofa3107e2018-07-25 17:21:49 -0700257 while (serverfd == -1) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800258 errno = 0;
Josh Gao80e81922019-01-30 13:59:51 -0800259 serverfd = listen_func(port, &error);
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800260 if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
261 D("unrecoverable error: '%s'", error.c_str());
262 return;
263 } else if (serverfd < 0) {
Josh Gaofa3107e2018-07-25 17:21:49 -0700264 D("server: cannot bind socket yet: %s", error.c_str());
265 std::this_thread::sleep_for(1s);
266 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800267 }
Josh Gaofa3107e2018-07-25 17:21:49 -0700268 close_on_exec(serverfd.get());
269 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270
Josh Gaofa3107e2018-07-25 17:21:49 -0700271 while (true) {
Josh Gao80e81922019-01-30 13:59:51 -0800272 D("server: trying to get new connection from fd %d", serverfd.get());
Josh Gaofa3107e2018-07-25 17:21:49 -0700273 unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
274 if (fd >= 0) {
275 D("server: new connection on fd %d", fd.get());
276 close_on_exec(fd.get());
277 disable_tcp_nagle(fd.get());
278 std::string serial = android::base::StringPrintf("host-%d", fd.get());
Josh Gaoa3a71472018-08-02 13:58:24 -0700279 register_socket_transport(std::move(fd), std::move(serial), port, 1,
Josh Gaod24580d2018-08-30 11:37:00 -0700280 [](atransport*) { return ReconnectResult::Abort; });
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 }
282 }
Yabin Cui815ad882015-09-02 17:44:28 -0700283 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284}
285
Josh Gao0560feb2019-01-22 19:36:15 -0800286#endif
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800287
Josh Gao80e81922019-01-30 13:59:51 -0800288unique_fd tcp_listen_inaddr_any(int port, std::string* error) {
289 return unique_fd{network_inaddr_any_server(port, SOCK_STREAM, error)};
290}
291
292#if !ADB_HOST
293static unique_fd vsock_listen(int port, std::string* error) {
294 return unique_fd{
295 socket_spec_listen(android::base::StringPrintf("vsock:%d", port), error, nullptr)
296 };
297}
298#endif
299
Josh Gao0560feb2019-01-22 19:36:15 -0800300void local_init(int port) {
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800301#if ADB_HOST
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800302 D("transport: local client init");
303 std::thread(client_socket_thread, port).detach();
Tim Baverstock96ee6b12019-02-08 16:27:16 +0000304 adb_local_transport_max_port_env_override();
Josh Gao0560feb2019-01-22 19:36:15 -0800305#elif !defined(__ANDROID__)
306 // Host adbd.
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800307 D("transport: local server init");
Josh Gao80e81922019-01-30 13:59:51 -0800308 std::thread(server_socket_thread, tcp_listen_inaddr_any, port).detach();
309 std::thread(server_socket_thread, vsock_listen, port).detach();
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800310#else
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800311 D("transport: local server init");
Elliott Hughes8b249d22016-09-23 15:40:03 -0700312 // For the adbd daemon in the system image we need to distinguish
313 // between the device, and the emulator.
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800314 if (use_qemu_goldfish()) {
315 std::thread(qemu_socket_thread, port).detach();
316 } else {
Josh Gao80e81922019-01-30 13:59:51 -0800317 std::thread(server_socket_thread, tcp_listen_inaddr_any, port).detach();
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800318 }
Josh Gao80e81922019-01-30 13:59:51 -0800319 std::thread(server_socket_thread, vsock_listen, port).detach();
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700320#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800321}
322
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323#if ADB_HOST
Josh Gao395b86a2018-01-28 20:32:46 -0800324struct EmulatorConnection : public FdConnection {
325 EmulatorConnection(unique_fd fd, int local_port)
326 : FdConnection(std::move(fd)), local_port_(local_port) {}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800327
Josh Gao395b86a2018-01-28 20:32:46 -0800328 ~EmulatorConnection() {
329 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port_;
Yabin Cuif401ead2016-04-29 16:53:52 -0700330 std::unique_lock<std::mutex> lock(retry_ports_lock);
331 RetryPort port;
Josh Gao395b86a2018-01-28 20:32:46 -0800332 port.port = local_port_;
Yabin Cuif401ead2016-04-29 16:53:52 -0700333 port.retry_count = LOCAL_PORT_RETRY_COUNT;
334 retry_ports.push_back(port);
335 retry_ports_cond.notify_one();
336 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337
Josh Gao395b86a2018-01-28 20:32:46 -0800338 void Close() override {
339 std::lock_guard<std::mutex> lock(local_transports_lock);
340 local_transports.erase(local_port_);
341 FdConnection::Close();
342 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100343
Josh Gao395b86a2018-01-28 20:32:46 -0800344 int local_port_;
345};
346
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100347/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700348static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Josh Gao395b86a2018-01-28 20:32:46 -0800349 REQUIRES(local_transports_lock) {
350 auto it = local_transports.find(adb_port);
351 if (it == local_transports.end()) {
352 return nullptr;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100353 }
Josh Gao395b86a2018-01-28 20:32:46 -0800354 return it->second;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100355}
356
Josh Gao395b86a2018-01-28 20:32:46 -0800357atransport* find_emulator_transport_by_adb_port(int adb_port) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700358 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gao395b86a2018-01-28 20:32:46 -0800359 return find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100360}
361
Josh Gao395b86a2018-01-28 20:32:46 -0800362atransport* find_emulator_transport_by_console_port(int console_port) {
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700363 return find_transport(getEmulatorSerialString(console_port).c_str());
364}
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100365#endif
366
Tao Bao49042e32018-07-30 20:45:27 -0700367std::string getEmulatorSerialString(int console_port) {
368 return android::base::StringPrintf("emulator-%d", console_port);
369}
370
Josh Gaofa3107e2018-07-25 17:21:49 -0700371int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local) {
Josh Gao395b86a2018-01-28 20:32:46 -0800372 int fail = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800373
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800374 t->type = kTransportLocal;
375
376#if ADB_HOST
Josh Gao395b86a2018-01-28 20:32:46 -0800377 // Emulator connection.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700378 if (local) {
Josh Gaof2a988c2018-03-07 16:51:08 -0800379 auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port);
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700380 t->SetConnection(
381 std::make_unique<BlockingConnectionAdapter>(std::move(emulator_connection)));
Josh Gaoe7daf572016-09-21 12:37:10 -0700382 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700383 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
Yi Kong86e67182018-07-13 18:15:16 -0700384 if (existing_transport != nullptr) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700385 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
386 fail = -1;
Josh Gaoe7daf572016-09-21 12:37:10 -0700387 } else {
Josh Gao395b86a2018-01-28 20:32:46 -0800388 local_transports[adb_port] = t;
Josh Gaoe7daf572016-09-21 12:37:10 -0700389 }
Josh Gao395b86a2018-01-28 20:32:46 -0800390
391 return fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800392 }
393#endif
Josh Gao395b86a2018-01-28 20:32:46 -0800394
395 // Regular tcp connection.
Josh Gaof2a988c2018-03-07 16:51:08 -0800396 auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700397 t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 return fail;
399}