blob: 5ec8e1665824d04453cb4c9ffcbaf19586d3b551 [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 -070088void connect_device(const std::string& address, std::string* response) {
89 if (address.empty()) {
90 *response = "empty address";
91 return;
92 }
93
Josh Gao8d7080c2018-08-10 16:03:09 -070094 D("connection requested to '%s'", address.c_str());
Luis Hector Chavez0aeda102018-04-20 10:31:29 -070095 unique_fd fd;
96 int port;
Jason Jeremy Iman84613872019-07-19 12:44:39 +090097 std::string serial, prefix_addr;
98
99 // If address does not match any socket type, it should default to TCP.
100 if (address.starts_with("vsock:") || address.starts_with("localfilesystem:")) {
101 prefix_addr = address;
102 } else {
103 prefix_addr = "tcp:" + address;
104 }
105
106 socket_spec_connect(&fd, prefix_addr, &port, &serial, response);
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800107 if (fd.get() == -1) {
108 return;
109 }
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900110 auto reconnect = [prefix_addr](atransport* t) {
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700111 std::string response;
112 unique_fd fd;
113 int port;
114 std::string serial;
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900115 socket_spec_connect(&fd, prefix_addr, &port, &serial, &response);
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700116 if (fd == -1) {
117 D("reconnect failed: %s", response.c_str());
Josh Gaod24580d2018-08-30 11:37:00 -0700118 return ReconnectResult::Retry;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700119 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700120 // This invokes the part of register_socket_transport() that needs to be
121 // invoked if the atransport* has already been setup. This eventually
122 // calls atransport->SetConnection() with a newly created Connection*
123 // that will in turn send the CNXN packet.
Josh Gaod24580d2018-08-30 11:37:00 -0700124 return init_socket_transport(t, std::move(fd), port, 0) >= 0 ? ReconnectResult::Success
125 : ReconnectResult::Retry;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700126 };
127
Josh Gao597044d2018-08-08 16:20:14 -0700128 int error;
Joshua Duong64fab752020-01-21 13:19:42 -0800129 if (!register_socket_transport(std::move(fd), serial, port, 0, std::move(reconnect), false,
130 &error)) {
Josh Gao597044d2018-08-08 16:20:14 -0700131 if (error == EALREADY) {
Luis Hector Chavez77719202018-04-17 14:09:21 -0700132 *response = android::base::StringPrintf("already connected to %s", serial.c_str());
Josh Gao597044d2018-08-08 16:20:14 -0700133 } else if (error == EPERM) {
134 *response = android::base::StringPrintf("failed to authenticate to %s", serial.c_str());
Luis Hector Chavez77719202018-04-17 14:09:21 -0700135 } else {
136 *response = android::base::StringPrintf("failed to connect to %s", serial.c_str());
137 }
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700138 } else {
139 *response = android::base::StringPrintf("connected to %s", serial.c_str());
140 }
141}
142
143
Elliott Hughes43df1092015-07-23 17:12:58 -0700144int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700145 unique_fd fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800146
147#if ADB_HOST
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700148 if (find_emulator_transport_by_adb_port(adb_port) != nullptr ||
149 find_emulator_transport_by_console_port(console_port) != nullptr) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700150 return -1;
151 }
152
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153 const char *host = getenv("ADBHOST");
154 if (host) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700155 fd.reset(network_connect(host, adb_port, SOCK_STREAM, 0, error));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800156 }
157#endif
158 if (fd < 0) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700159 fd.reset(network_loopback_client(adb_port, SOCK_STREAM, error));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160 }
161
162 if (fd >= 0) {
Josh Gaod8d51f42018-07-25 17:27:34 -0700163 D("client: connected on remote on fd %d", fd.get());
164 close_on_exec(fd.get());
165 disable_tcp_nagle(fd.get());
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700166 std::string serial = getEmulatorSerialString(console_port);
Joshua Duong64fab752020-01-21 13:19:42 -0800167 if (register_socket_transport(
168 std::move(fd), std::move(serial), adb_port, 1,
169 [](atransport*) { return ReconnectResult::Abort; }, false)) {
Yabin Cuid802bcf2015-07-31 14:10:54 -0700170 return 0;
171 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800172 }
173 return -1;
174}
175
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700176#if ADB_HOST
Yabin Cuif401ead2016-04-29 16:53:52 -0700177
178static void PollAllLocalPortsForEmulator() {
Yabin Cuif401ead2016-04-29 16:53:52 -0700179 // Try to connect to any number of running emulator instances.
Tim Baverstock96ee6b12019-02-08 16:27:16 +0000180 for (int port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; port <= adb_local_transport_max_port;
181 port += 2) {
182 local_connect(port); // Note, uses port and port-1, so '=max_port' is OK.
Yabin Cuif401ead2016-04-29 16:53:52 -0700183 }
184}
185
186// Retry the disconnected local port for 60 times, and sleep 1 second between two retries.
Elliott Hughesc3462f42018-09-05 12:13:11 -0700187static constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60;
188static constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s;
Yabin Cuif401ead2016-04-29 16:53:52 -0700189
190struct RetryPort {
191 int port;
192 uint32_t retry_count;
193};
194
195// Retry emulators just kicked.
196static std::vector<RetryPort>& retry_ports = *new std::vector<RetryPort>;
197std::mutex &retry_ports_lock = *new std::mutex;
198std::condition_variable &retry_ports_cond = *new std::condition_variable;
199
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900200static void client_socket_thread(std::string_view) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700201 adb_thread_setname("client_socket_thread");
Yabin Cui815ad882015-09-02 17:44:28 -0700202 D("transport: client_socket_thread() starting");
Yabin Cuif401ead2016-04-29 16:53:52 -0700203 PollAllLocalPortsForEmulator();
Yabin Cuid802bcf2015-07-31 14:10:54 -0700204 while (true) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700205 std::vector<RetryPort> ports;
206 // Collect retry ports.
207 {
208 std::unique_lock<std::mutex> lock(retry_ports_lock);
209 while (retry_ports.empty()) {
210 retry_ports_cond.wait(lock);
211 }
212 retry_ports.swap(ports);
Yabin Cuid802bcf2015-07-31 14:10:54 -0700213 }
Yabin Cuif401ead2016-04-29 16:53:52 -0700214 // Sleep here instead of the end of loop, because if we immediately try to reconnect
215 // the emulator just kicked, the adbd on the emulator may not have time to remove the
216 // just kicked transport.
Elliott Hughes73925982016-11-15 12:37:32 -0800217 std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL);
Yabin Cuif401ead2016-04-29 16:53:52 -0700218
219 // Try connecting retry ports.
220 std::vector<RetryPort> next_ports;
221 for (auto& port : ports) {
222 VLOG(TRANSPORT) << "retry port " << port.port << ", last retry_count "
223 << port.retry_count;
224 if (local_connect(port.port)) {
225 VLOG(TRANSPORT) << "retry port " << port.port << " successfully";
226 continue;
227 }
228 if (--port.retry_count > 0) {
229 next_ports.push_back(port);
230 } else {
231 VLOG(TRANSPORT) << "stop retrying port " << port.port;
232 }
233 }
234
235 // Copy back left retry ports.
236 {
237 std::unique_lock<std::mutex> lock(retry_ports_lock);
238 retry_ports.insert(retry_ports.end(), next_ports.begin(), next_ports.end());
239 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800241}
242
Josh Gao0560feb2019-01-22 19:36:15 -0800243#else // !ADB_HOST
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700244
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900245void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
246 std::string_view addr) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700247 adb_thread_setname("server socket");
Josh Gao80e81922019-01-30 13:59:51 -0800248
249 unique_fd serverfd;
250 std::string error;
251
Josh Gaofa3107e2018-07-25 17:21:49 -0700252 while (serverfd == -1) {
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800253 errno = 0;
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900254 serverfd = listen_func(addr, &error);
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800255 if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
256 D("unrecoverable error: '%s'", error.c_str());
257 return;
258 } else if (serverfd < 0) {
Josh Gaofa3107e2018-07-25 17:21:49 -0700259 D("server: cannot bind socket yet: %s", error.c_str());
260 std::this_thread::sleep_for(1s);
261 continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262 }
Josh Gaofa3107e2018-07-25 17:21:49 -0700263 close_on_exec(serverfd.get());
264 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265
Josh Gaofa3107e2018-07-25 17:21:49 -0700266 while (true) {
Josh Gao80e81922019-01-30 13:59:51 -0800267 D("server: trying to get new connection from fd %d", serverfd.get());
Josh Gaofa3107e2018-07-25 17:21:49 -0700268 unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
269 if (fd >= 0) {
270 D("server: new connection on fd %d", fd.get());
271 close_on_exec(fd.get());
272 disable_tcp_nagle(fd.get());
273 std::string serial = android::base::StringPrintf("host-%d", fd.get());
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900274 // We don't care about port value in "register_socket_transport" as it is used
275 // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
Joshua Duong64fab752020-01-21 13:19:42 -0800276 register_socket_transport(
277 std::move(fd), std::move(serial), 0, 1,
278 [](atransport*) { return ReconnectResult::Abort; }, false);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 }
280 }
Yabin Cui815ad882015-09-02 17:44:28 -0700281 D("transport: server_socket_thread() exiting");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282}
283
Josh Gao0560feb2019-01-22 19:36:15 -0800284#endif
Vladimir Chtchetkine7c9339d2011-12-09 15:49:47 -0800285
Josh Gao80e81922019-01-30 13:59:51 -0800286#if !ADB_HOST
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900287unique_fd adb_listen(std::string_view addr, std::string* error) {
288 return unique_fd{socket_spec_listen(addr, error, nullptr)};
Josh Gao80e81922019-01-30 13:59:51 -0800289}
290#endif
291
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900292void local_init(const std::string& addr) {
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800293#if ADB_HOST
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800294 D("transport: local client init");
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900295 std::thread(client_socket_thread, addr).detach();
Tim Baverstock96ee6b12019-02-08 16:27:16 +0000296 adb_local_transport_max_port_env_override();
Josh Gao0560feb2019-01-22 19:36:15 -0800297#elif !defined(__ANDROID__)
298 // Host adbd.
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800299 D("transport: local server init");
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900300 std::thread(server_socket_thread, adb_listen, addr).detach();
Vladimir Chtchetkine4bdc7762011-12-13 12:19:29 -0800301#else
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800302 D("transport: local server init");
Elliott Hughes8b249d22016-09-23 15:40:03 -0700303 // For the adbd daemon in the system image we need to distinguish
304 // between the device, and the emulator.
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900305 if (addr.starts_with("tcp:") && use_qemu_goldfish()) {
306 std::thread(qemu_socket_thread, addr).detach();
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800307 } else {
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900308 std::thread(server_socket_thread, adb_listen, addr).detach();
Cody Schuffelen637aaf52019-01-04 18:51:11 -0800309 }
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700310#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311}
312
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800313#if ADB_HOST
Josh Gao395b86a2018-01-28 20:32:46 -0800314struct EmulatorConnection : public FdConnection {
315 EmulatorConnection(unique_fd fd, int local_port)
316 : FdConnection(std::move(fd)), local_port_(local_port) {}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317
Josh Gao395b86a2018-01-28 20:32:46 -0800318 ~EmulatorConnection() {
319 VLOG(TRANSPORT) << "remote_close, local_port = " << local_port_;
Yabin Cuif401ead2016-04-29 16:53:52 -0700320 std::unique_lock<std::mutex> lock(retry_ports_lock);
321 RetryPort port;
Josh Gao395b86a2018-01-28 20:32:46 -0800322 port.port = local_port_;
Yabin Cuif401ead2016-04-29 16:53:52 -0700323 port.retry_count = LOCAL_PORT_RETRY_COUNT;
324 retry_ports.push_back(port);
325 retry_ports_cond.notify_one();
326 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800327
Josh Gao395b86a2018-01-28 20:32:46 -0800328 void Close() override {
329 std::lock_guard<std::mutex> lock(local_transports_lock);
330 local_transports.erase(local_port_);
331 FdConnection::Close();
332 }
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100333
Josh Gao395b86a2018-01-28 20:32:46 -0800334 int local_port_;
335};
336
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100337/* Only call this function if you already hold local_transports_lock. */
Yabin Cuif401ead2016-04-29 16:53:52 -0700338static atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
Josh Gao395b86a2018-01-28 20:32:46 -0800339 REQUIRES(local_transports_lock) {
340 auto it = local_transports.find(adb_port);
341 if (it == local_transports.end()) {
342 return nullptr;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100343 }
Josh Gao395b86a2018-01-28 20:32:46 -0800344 return it->second;
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100345}
346
Josh Gao395b86a2018-01-28 20:32:46 -0800347atransport* find_emulator_transport_by_adb_port(int adb_port) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700348 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gao395b86a2018-01-28 20:32:46 -0800349 return find_emulator_transport_by_adb_port_locked(adb_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100350}
351
Josh Gao395b86a2018-01-28 20:32:46 -0800352atransport* find_emulator_transport_by_console_port(int console_port) {
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700353 return find_transport(getEmulatorSerialString(console_port).c_str());
354}
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100355#endif
356
Tao Bao49042e32018-07-30 20:45:27 -0700357std::string getEmulatorSerialString(int console_port) {
358 return android::base::StringPrintf("emulator-%d", console_port);
359}
360
Josh Gaofa3107e2018-07-25 17:21:49 -0700361int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local) {
Josh Gao395b86a2018-01-28 20:32:46 -0800362 int fail = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800363
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800364 t->type = kTransportLocal;
365
366#if ADB_HOST
Josh Gao395b86a2018-01-28 20:32:46 -0800367 // Emulator connection.
Yabin Cui5fe6d0d2015-08-11 13:40:42 -0700368 if (local) {
Josh Gaof2a988c2018-03-07 16:51:08 -0800369 auto emulator_connection = std::make_unique<EmulatorConnection>(std::move(fd), adb_port);
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700370 t->SetConnection(
Joshua Duong64fab752020-01-21 13:19:42 -0800371 std::make_unique<BlockingConnectionAdapter>(std::move(emulator_connection)));
Josh Gaoe7daf572016-09-21 12:37:10 -0700372 std::lock_guard<std::mutex> lock(local_transports_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700373 atransport* existing_transport = find_emulator_transport_by_adb_port_locked(adb_port);
Yi Kong86e67182018-07-13 18:15:16 -0700374 if (existing_transport != nullptr) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700375 D("local transport for port %d already registered (%p)?", adb_port, existing_transport);
376 fail = -1;
Josh Gaoe7daf572016-09-21 12:37:10 -0700377 } else {
Josh Gao395b86a2018-01-28 20:32:46 -0800378 local_transports[adb_port] = t;
Josh Gaoe7daf572016-09-21 12:37:10 -0700379 }
Josh Gao395b86a2018-01-28 20:32:46 -0800380
381 return fail;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382 }
383#endif
Josh Gao395b86a2018-01-28 20:32:46 -0800384
385 // Regular tcp connection.
Josh Gaof2a988c2018-03-07 16:51:08 -0800386 auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700387 t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388 return fail;
389}