blob: 4a9d91a1c660f1726c015a0156995cd8e136035f [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 Albert76649012015-02-24 15:51:19 -080018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert055f1aa2015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Spencer Low363af562015-11-07 18:51:54 -080030#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070031#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070032#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070033#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070034
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080036#include <android-base/parsenetaddress.h>
Yabin Cuib5e11412017-03-10 16:01:01 -080037#include <android-base/quick_exit.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaob122b172017-08-16 16:57:01 -070040#include <android-base/thread_annotations.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070043#include "adb_auth.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080044#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070045#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080046#include "diagnose_usb.h"
Yabin Cuib5e11412017-03-10 16:01:01 -080047#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49static void transport_unref(atransport *t);
50
Josh Gaob122b172017-08-16 16:57:01 -070051// TODO: unordered_map<TransportId, atransport*>
Josh Gaob7b1edf2015-11-11 17:56:12 -080052static auto& transport_list = *new std::list<atransport*>();
53static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070054
Josh Gao1db71af2017-08-17 13:50:51 -070055static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Todd Kennedy51c05ec2015-11-10 00:03:25 +000057const char* const kFeatureShell2 = "shell_v2";
58const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080059const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080060const char* const kFeatureLibusb = "libusb";
Dan Albert5176df82017-05-23 14:30:00 -070061const char* const kFeaturePushSync = "push_sync";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000062
Josh Gaob122b172017-08-16 16:57:01 -070063TransportId NextTransportId() {
64 static std::atomic<TransportId> next(1);
65 return next++;
66}
67
Yabin Cuiaed3c612015-09-22 15:52:57 -070068static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-11-22 14:32:34 -080069 unsigned command = p->msg.command;
70 int len = p->msg.data_length;
71 char cmd[9];
72 char arg0[12], arg1[12];
73 int n;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010074
75 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -080076 int b = (command >> (n * 8)) & 255;
77 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010078 cmd[n] = (char)b;
79 }
80 if (n == 4) {
81 cmd[4] = 0;
82 } else {
83 /* There is some non-ASCII name in the command, so dump
84 * the hexadecimal value instead */
85 snprintf(cmd, sizeof cmd, "%08x", command);
86 }
87
88 if (p->msg.arg0 < 256U)
89 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
90 else
91 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
92
93 if (p->msg.arg1 < 256U)
94 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
95 else
96 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
97
Josh Gao1290fbf2016-11-22 14:32:34 -080098 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
99 func, cmd, arg0, arg1, len);
Yabin Cuiaed3c612015-09-22 15:52:57 -0700100 result += dump_hex(p->data, len);
101 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100102}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100103
Josh Gao1290fbf2016-11-22 14:32:34 -0800104static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800105 ATRACE_NAME("read_packet");
Yabin Cui62641292015-07-30 19:58:10 -0700106 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100107 if (!name) {
108 snprintf(buff, sizeof buff, "fd=%d", fd);
109 name = buff;
110 }
Josh Gao1290fbf2016-11-22 14:32:34 -0800111 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700112 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800113 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700114 int r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800115 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700117 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700119 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 return -1;
121 }
122 }
123
Yabin Cuiaed3c612015-09-22 15:52:57 -0700124 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 return 0;
126}
127
Josh Gao1290fbf2016-11-22 14:32:34 -0800128static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800129 ATRACE_NAME("write_packet");
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100130 char buff[8];
131 if (!name) {
132 snprintf(buff, sizeof buff, "fd=%d", fd);
133 name = buff;
134 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700135 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao1290fbf2016-11-22 14:32:34 -0800136 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700137 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800138 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700139 int r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800140 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 len -= r;
142 p += r;
143 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700144 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 return -1;
146 }
147 }
148 return 0;
149}
150
Josh Gao1290fbf2016-11-22 14:32:34 -0800151static void transport_socket_events(int fd, unsigned events, void* _t) {
152 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700153 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao1290fbf2016-11-22 14:32:34 -0800154 if (events & FDE_READ) {
155 apacket* p = 0;
156 if (read_packet(fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700157 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800159 handle_packet(p, (atransport*)_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 }
161 }
162}
163
Josh Gao06d61d42016-10-06 13:31:44 -0700164void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayde471942017-12-07 11:40:00 -0800166 // compute a checksum for connection/auth packets for compatibility reasons
167 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
168 p->msg.data_check = 0;
169 } else {
170 p->msg.data_check = calculate_apacket_checksum(p);
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
173 print_packet("send", p);
174
175 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700176 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178
Josh Gao06d61d42016-10-06 13:31:44 -0700179 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 fatal_errno("cannot enqueue packet on transport socket");
181 }
182}
183
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700184// The transport is opened by transport_register_func before
185// the read_transport and write_transport threads are started.
186//
187// The read_transport thread issues a SYNC(1, token) message to let
188// the write_transport thread know to start things up. In the event
189// of transport IO failure, the read_transport thread will post a
190// SYNC(0,0) message to ensure shutdown.
191//
192// The transport will not actually be closed until both threads exit, but the threads
193// will kick the transport on their way out to disconnect the underlying device.
194//
195// read_transport thread reads data from a transport (representing a usb/tcp connection),
196// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800197static void read_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800198 atransport* t = reinterpret_cast<atransport*>(_t);
199 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Josh Gao1290fbf2016-11-22 14:32:34 -0800201 adb_thread_setname(
202 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
203 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
204 t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 p = get_apacket();
206 p->msg.command = A_SYNC;
207 p->msg.arg0 = 1;
208 p->msg.arg1 = ++(t->sync_token);
209 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800210 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700212 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 goto oops;
214 }
215
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700216 D("%s: data pump started", t->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800217 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800218 ATRACE_NAME("read_transport loop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 p = get_apacket();
220
Josh Gaocfe72e22016-11-29 09:40:29 -0800221 {
222 ATRACE_NAME("read_transport read_remote");
223 if (t->read_from_remote(p, t) != 0) {
224 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800226 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 }
Yabin Cuib5e11412017-03-10 16:01:01 -0800228#if ADB_HOST
229 if (p->msg.command == 0) {
Josh Gaofb413a22018-01-29 18:09:20 -0800230 put_apacket(p);
Yabin Cuib5e11412017-03-10 16:01:01 -0800231 continue;
232 }
233#endif
Josh Gaocfe72e22016-11-29 09:40:29 -0800234 }
235
236 D("%s: received remote packet, sending to transport", t->serial);
237 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800239 D("%s: failed to write apacket to transport", t->serial);
240 goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242 }
243
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700244 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 p = get_apacket();
246 p->msg.command = A_SYNC;
247 p->msg.arg0 = 0;
248 p->msg.arg1 = 0;
249 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800250 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700252 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 }
254
255oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 kick_transport(t);
258 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259}
260
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700261// write_transport thread gets packets sent by the main thread (through send_packet()),
262// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800263static void write_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800264 atransport* t = reinterpret_cast<atransport*>(_t);
265 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 int active = 0;
267
Josh Gao1290fbf2016-11-22 14:32:34 -0800268 adb_thread_setname(
269 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
270 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
Josh Gao1290fbf2016-11-22 14:32:34 -0800272 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800273 ATRACE_NAME("write_transport loop");
Josh Gao1290fbf2016-11-22 14:32:34 -0800274 if (read_packet(t->fd, t->serial, &p)) {
275 D("%s: failed to read apacket from transport on fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 break;
277 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800278
Josh Gao1290fbf2016-11-22 14:32:34 -0800279 if (p->msg.command == A_SYNC) {
280 if (p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700281 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 put_apacket(p);
283 break;
284 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800285 if (p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700286 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 active = 1;
288 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800289 D("%s: transport ignoring SYNC %d != %d", t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 }
291 }
292 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800293 if (active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700294 D("%s: transport got packet, sending to remote", t->serial);
Josh Gaocfe72e22016-11-29 09:40:29 -0800295 ATRACE_NAME("write_transport write_remote");
Yabin Cuib5e11412017-03-10 16:01:01 -0800296 if (t->Write(p) != 0) {
297 D("%s: remote write failed for transport", t->serial);
298 put_apacket(p);
299 break;
300 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700302 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304 }
305
306 put_apacket(p);
307 }
308
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700309 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 kick_transport(t);
311 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312}
313
Yabin Cuif4b99282015-08-27 12:03:11 -0700314void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700315 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700316 // As kick_transport() can be called from threads without guarantee that t is valid,
317 // check if the transport is in transport_list first.
Josh Gaob122b172017-08-16 16:57:01 -0700318 //
319 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700320 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700321 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700322 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700323}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324
325static int transport_registration_send = -1;
326static int transport_registration_recv = -1;
327static fdevent transport_registration_fde;
328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
331/* this adds support required by the 'track-devices' service.
332 * this is used to send the content of "list_transport" to any
333 * number of client connections that want it through a single
334 * live TCP connection
335 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800337 asocket socket;
Josh Gaob0c18022017-08-14 18:57:54 -0700338 bool update_needed;
339 bool long_output;
Josh Gao1290fbf2016-11-22 14:32:34 -0800340 device_tracker* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341};
342
343/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800344static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
Josh Gao1290fbf2016-11-22 14:32:34 -0800346static void device_tracker_remove(device_tracker* tracker) {
347 device_tracker** pnode = &device_tracker_list;
348 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
Josh Gao1db71af2017-08-17 13:50:51 -0700350 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 while (node) {
352 if (node == tracker) {
353 *pnode = node->next;
354 break;
355 }
356 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800357 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359}
360
Josh Gao1290fbf2016-11-22 14:32:34 -0800361static void device_tracker_close(asocket* socket) {
362 device_tracker* tracker = (device_tracker*)socket;
363 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364
Josh Gao1290fbf2016-11-22 14:32:34 -0800365 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 if (peer) {
367 peer->peer = NULL;
368 peer->close(peer);
369 }
370 device_tracker_remove(tracker);
371 free(tracker);
372}
373
Josh Gao1290fbf2016-11-22 14:32:34 -0800374static int device_tracker_enqueue(asocket* socket, apacket* p) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 /* you can't read from a device tracker, close immediately */
376 put_apacket(p);
377 device_tracker_close(socket);
378 return -1;
379}
380
Elliott Hughese67f1f82015-04-30 17:32:03 -0700381static int device_tracker_send(device_tracker* tracker, const std::string& string) {
382 apacket* p = get_apacket();
383 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Elliott Hughese67f1f82015-04-30 17:32:03 -0700385 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
386 memcpy(&p->data[4], string.data(), string.size());
387 p->len = 4 + string.size();
388 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389}
390
Elliott Hughese67f1f82015-04-30 17:32:03 -0700391static void device_tracker_ready(asocket* socket) {
392 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Elliott Hughese67f1f82015-04-30 17:32:03 -0700394 // We want to send the device list when the tracker connects
395 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700396 if (tracker->update_needed) {
397 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
Josh Gaob0c18022017-08-14 18:57:54 -0700399 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700400 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 }
402}
403
Josh Gaob0c18022017-08-14 18:57:54 -0700404asocket* create_device_tracker(bool long_output) {
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700405 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
406 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407
Josh Gao1290fbf2016-11-22 14:32:34 -0800408 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409
410 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800411 tracker->socket.ready = device_tracker_ready;
412 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700413 tracker->update_needed = true;
414 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415
Josh Gao1290fbf2016-11-22 14:32:34 -0800416 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 device_tracker_list = tracker;
418
419 return &tracker->socket;
420}
421
Josh Gaofd713e52017-05-03 22:37:10 -0700422// Check if all of the USB transports are connected.
423bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700424 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700425 for (const auto& t : transport_list) {
426 if (!fn(t)) {
427 return false;
428 }
429 }
430 for (const auto& t : pending_list) {
431 if (!fn(t)) {
432 return false;
433 }
434 }
435 return true;
436}
437
Elliott Hughese67f1f82015-04-30 17:32:03 -0700438// Call this function each time the transport list has changed.
439void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700440 update_transport_status();
441
442 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700443 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444
Elliott Hughese67f1f82015-04-30 17:32:03 -0700445 device_tracker* tracker = device_tracker_list;
446 while (tracker != nullptr) {
447 device_tracker* next = tracker->next;
448 // This may destroy the tracker if the connection is closed.
449 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 tracker = next;
451 }
452}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700453
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700455
456void update_transports() {
457 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700459
Josh Gao1290fbf2016-11-22 14:32:34 -0800460#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461
Josh Gao1290fbf2016-11-22 14:32:34 -0800462struct tmsg {
463 atransport* transport;
464 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465};
466
Josh Gao1290fbf2016-11-22 14:32:34 -0800467static int transport_read_action(int fd, struct tmsg* m) {
468 char* p = (char*)m;
469 int len = sizeof(*m);
470 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Josh Gao1290fbf2016-11-22 14:32:34 -0800472 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800474 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800476 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700478 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return -1;
480 }
481 }
482 return 0;
483}
484
Josh Gao1290fbf2016-11-22 14:32:34 -0800485static int transport_write_action(int fd, struct tmsg* m) {
486 char* p = (char*)m;
487 int len = sizeof(*m);
488 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
Josh Gao1290fbf2016-11-22 14:32:34 -0800490 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800492 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800494 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700496 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 return -1;
498 }
499 }
500 return 0;
501}
502
Josh Gao1290fbf2016-11-22 14:32:34 -0800503static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 int s[2];
Josh Gao1290fbf2016-11-22 14:32:34 -0800506 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507
Josh Gao1290fbf2016-11-22 14:32:34 -0800508 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 return;
510 }
511
Josh Gao1290fbf2016-11-22 14:32:34 -0800512 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 fatal_errno("cannot read transport registration socket");
514 }
515
516 t = m.transport;
517
Dan Albert1792c232015-05-18 13:06:53 -0700518 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700519 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520
Josh Gao1290fbf2016-11-22 14:32:34 -0800521 /* IMPORTANT: the remove closes one half of the
522 ** socket pair. The close closes the other half.
523 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 fdevent_remove(&(t->transport_fde));
525 adb_close(t->fd);
526
Josh Gao0cd3ae12016-09-21 12:37:10 -0700527 {
Josh Gao1db71af2017-08-17 13:50:51 -0700528 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700529 transport_list.remove(t);
530 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531
Josh Gao1290fbf2016-11-22 14:32:34 -0800532 if (t->product) free(t->product);
533 if (t->serial) free(t->serial);
534 if (t->model) free(t->model);
535 if (t->device) free(t->device);
536 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537
Dan Albertc7915a32015-05-18 16:46:31 -0700538 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539
540 update_transports();
541 return;
542 }
543
Mike Lockwood0927bf92009-08-08 12:37:44 -0400544 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800545 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400547 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
Dan Albertc7915a32015-05-18 16:46:31 -0700549 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400550 fatal_errno("cannot open transport socketpair");
551 }
552
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700553 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400554
555 t->transport_socket = s[0];
556 t->fd = s[1];
557
Josh Gao1290fbf2016-11-22 14:32:34 -0800558 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400559
560 fdevent_set(&(t->transport_fde), FDE_READ);
561
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700562 std::thread(write_transport_thread, t).detach();
563 std::thread(read_transport_thread, t).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 }
565
Josh Gao0cd3ae12016-09-21 12:37:10 -0700566 {
Josh Gao1db71af2017-08-17 13:50:51 -0700567 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700568 pending_list.remove(t);
569 transport_list.push_front(t);
570 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 update_transports();
573}
574
Josh Gao1290fbf2016-11-22 14:32:34 -0800575void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 int s[2];
577
Josh Gao1290fbf2016-11-22 14:32:34 -0800578 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 fatal_errno("cannot open transport registration socketpair");
580 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700581 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582
583 transport_registration_send = s[0];
584 transport_registration_recv = s[1];
585
Josh Gao1290fbf2016-11-22 14:32:34 -0800586 fdevent_install(&transport_registration_fde, transport_registration_recv,
587 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588
589 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700590}
591
592void kick_all_transports() {
593 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700594 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700595 for (auto t : transport_list) {
596 t->Kick();
597 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598}
599
600/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800601static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602 tmsg m;
603 m.transport = transport;
604 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700605 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800606 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607 fatal_errno("cannot write transport registration socket\n");
608 }
609}
610
Josh Gao1290fbf2016-11-22 14:32:34 -0800611static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 tmsg m;
613 m.transport = transport;
614 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700615 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800616 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 fatal_errno("cannot write transport registration socket\n");
618 }
619}
620
Yabin Cuif4b99282015-08-27 12:03:11 -0700621static void transport_unref(atransport* t) {
622 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700623
Josh Gaoe48ecce2017-09-13 13:40:57 -0700624 std::lock_guard<std::recursive_mutex> lock(transport_lock);
625 CHECK_GT(t->ref_count, 0u);
626 t->ref_count--;
627 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700628 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400629 t->close(t);
630 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100631 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700632 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634}
635
Josh Gao1290fbf2016-11-22 14:32:34 -0800636static int qual_match(const char* to_test, const char* prefix, const char* qual,
637 bool sanitize_qual) {
638 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700639 return !qual || !*qual;
640
Josh Gao1290fbf2016-11-22 14:32:34 -0800641 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700642
643 if (prefix) {
644 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800645 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700646 }
647 }
648
649 while (*qual) {
650 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800651 if (sanitize_qual && !isalnum(ch)) ch = '_';
652 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700653 }
654
655 /* Everything matched so far. Return true if *to_test is a NUL. */
656 return !*to_test;
657}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658
Josh Gaob122b172017-08-16 16:57:01 -0700659atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
660 bool* is_ambiguous, std::string* error_out,
661 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700662 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663
Josh Gaob122b172017-08-16 16:57:01 -0700664 if (transport_id != 0) {
665 *error_out =
666 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
667 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700668 *error_out = android::base::StringPrintf("device '%s' not found", serial);
669 } else if (type == kTransportLocal) {
670 *error_out = "no emulators found";
671 } else if (type == kTransportAny) {
672 *error_out = "no devices/emulators found";
673 } else {
674 *error_out = "no devices found";
675 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676
Josh Gao1db71af2017-08-17 13:50:51 -0700677 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700678 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800679 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800680#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800681 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800682#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400683 continue;
684 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400685
Josh Gaob122b172017-08-16 16:57:01 -0700686 if (transport_id) {
687 if (t->id == transport_id) {
688 result = t;
689 break;
690 }
691 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800692 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700693 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700694 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700695 if (is_ambiguous) *is_ambiguous = true;
696 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700697 break;
698 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700700 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700702 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700704 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700705 if (is_ambiguous) *is_ambiguous = true;
706 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 break;
708 }
709 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700710 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700712 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700713 if (is_ambiguous) *is_ambiguous = true;
714 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 break;
716 }
717 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700718 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700720 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700721 if (is_ambiguous) *is_ambiguous = true;
722 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 break;
724 }
725 result = t;
726 }
727 }
728 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700729 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730
Elliott Hughes8d28e192015-10-07 14:55:10 -0700731 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800732 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700733 *error_out = "device unauthorized.\n";
734 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
735 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
736 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
737 *error_out += "\n";
738 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
739 *error_out += "Otherwise check for a confirmation dialog on your device.";
740 result = nullptr;
741 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800742
Elliott Hughes8d28e192015-10-07 14:55:10 -0700743 // Don't return offline devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800744 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700745 *error_out = "device offline";
746 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747 }
748
749 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700750 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 }
752
753 return result;
754}
755
Yabin Cuib5e11412017-03-10 16:01:01 -0800756int atransport::Write(apacket* p) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800757 return write_func_(p, this);
758}
759
Yabin Cui7f274902016-04-18 11:22:34 -0700760void atransport::Kick() {
761 if (!kicked_) {
762 kicked_ = true;
763 CHECK(kick_func_ != nullptr);
764 kick_func_(this);
765 }
766}
767
Yabin Cuib5e11412017-03-10 16:01:01 -0800768ConnectionState atransport::GetConnectionState() const {
769 return connection_state_;
770}
771
772void atransport::SetConnectionState(ConnectionState state) {
773 check_main_thread();
774 connection_state_ = state;
775}
776
David Purselld2acbd12015-12-02 15:14:31 -0800777const std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800778 ConnectionState state = GetConnectionState();
779 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800780 case kCsOffline:
781 return "offline";
782 case kCsBootloader:
783 return "bootloader";
784 case kCsDevice:
785 return "device";
786 case kCsHost:
787 return "host";
788 case kCsRecovery:
789 return "recovery";
790 case kCsNoPerm:
791 return UsbNoPermissionsShortHelpText();
792 case kCsSideload:
793 return "sideload";
794 case kCsUnauthorized:
795 return "unauthorized";
796 default:
797 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 }
799}
800
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100801void atransport::update_version(int version, size_t payload) {
802 protocol_version = std::min(version, A_VERSION);
803 max_payload = std::min(payload, MAX_PAYLOAD);
804}
805
806int atransport::get_protocol_version() const {
807 return protocol_version;
808}
809
810size_t atransport::get_max_payload() const {
811 return max_payload;
812}
813
David Pursell4e2fd362015-09-22 10:43:08 -0700814namespace {
David Pursell0955c662015-08-31 10:42:13 -0700815
David Pursell4e2fd362015-09-22 10:43:08 -0700816constexpr char kFeatureStringDelimiter = ',';
817
818} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700819
820const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700821 // Local static allocation to avoid global non-POD variables.
822 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800823 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700824 // Increment ADB_SERVER_VERSION whenever the feature list changes to
825 // make sure that the adb client and server features stay in sync
826 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700827 };
828
829 return *features;
830}
831
832std::string FeatureSetToString(const FeatureSet& features) {
833 return android::base::Join(features, kFeatureStringDelimiter);
834}
835
836FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700837 if (features_string.empty()) {
838 return FeatureSet();
839 }
840
Josh Gao1290fbf2016-11-22 14:32:34 -0800841 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700842 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700843}
844
David Pursell70ef7b42015-09-30 13:35:42 -0700845bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800846 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700847}
848
Dan Albert1792c232015-05-18 13:06:53 -0700849bool atransport::has_feature(const std::string& feature) const {
850 return features_.count(feature) > 0;
851}
852
David Pursell4e2fd362015-09-22 10:43:08 -0700853void atransport::SetFeatures(const std::string& features_string) {
854 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700855}
856
Yabin Cuib3298242015-08-28 15:09:44 -0700857void atransport::AddDisconnect(adisconnect* disconnect) {
858 disconnects_.push_back(disconnect);
859}
860
861void atransport::RemoveDisconnect(adisconnect* disconnect) {
862 disconnects_.remove(disconnect);
863}
864
865void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700866 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700867 disconnect->func(disconnect->opaque, this);
868 }
869 disconnects_.clear();
870}
871
David Pursell3f902aa2016-03-01 08:58:26 -0800872bool atransport::MatchesTarget(const std::string& target) const {
873 if (serial) {
874 if (target == serial) {
875 return true;
876 } else if (type == kTransportLocal) {
877 // Local transports can match [tcp:|udp:]<hostname>[:port].
878 const char* local_target_ptr = target.c_str();
879
880 // For fastboot compatibility, ignore protocol prefixes.
881 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800882 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800883 local_target_ptr += 4;
884 }
885
886 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
887 std::string serial_host, error;
888 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800889 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800890 // |target| may omit the port to default to ours.
891 std::string target_host;
892 int target_port = serial_port;
893 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
894 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800895 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800896 return true;
897 }
898 }
899 }
900 }
901
902 return (devpath && target == devpath) ||
903 qual_match(target.c_str(), "product:", product, false) ||
904 qual_match(target.c_str(), "model:", model, true) ||
905 qual_match(target.c_str(), "device:", device, false);
906}
907
Elliott Hughese67f1f82015-04-30 17:32:03 -0700908#if ADB_HOST
909
Josh Gaob122b172017-08-16 16:57:01 -0700910// We use newline as our delimiter, make sure to never output it.
911static std::string sanitize(std::string str, bool alphanumeric) {
912 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
913 : [](const char c) { return c == '\n'; };
914 std::replace_if(str.begin(), str.end(), pred, '_');
915 return str;
916}
917
Josh Gao1290fbf2016-11-22 14:32:34 -0800918static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -0700919 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700920 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700921 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700922 }
923
Elliott Hughese67f1f82015-04-30 17:32:03 -0700924 *result += ' ';
925 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -0700926 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700927}
928
Josh Gao1290fbf2016-11-22 14:32:34 -0800929static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700930 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700931 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700932 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700933 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700934
935 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700936 *result += serial;
937 *result += '\t';
938 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700939 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800940 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700941
Elliott Hughese67f1f82015-04-30 17:32:03 -0700942 append_transport_info(result, "", t->devpath, false);
943 append_transport_info(result, "product:", t->product, false);
944 append_transport_info(result, "model:", t->model, true);
945 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -0700946
947 // Put id at the end, so that anyone parsing the output here can always find it by scanning
948 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
949 *result += " transport_id:";
950 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700951 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700952 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700953}
954
Elliott Hughese67f1f82015-04-30 17:32:03 -0700955std::string list_transports(bool long_listing) {
Josh Gao1db71af2017-08-17 13:50:51 -0700956 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov04398a92017-12-17 10:56:07 +0000957
958 auto sorted_transport_list = transport_list;
959 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
960 if (x->type != y->type) {
961 return x->type < y->type;
962 }
963 return strcmp(x->serial, y->serial) < 0;
964 });
965
966 std::string result;
967 for (const auto& t : sorted_transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700968 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800969 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700970 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971}
972
Josh Gao22d2b3e2016-10-27 14:01:08 -0700973void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -0700974 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -0700975 for (auto& t : transport_list) {
976 if (predicate(t)) {
977 t->Kick();
978 }
979 }
980}
981
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800982/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700983void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -0700984 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800985}
Josh Gao1290fbf2016-11-22 14:32:34 -0800986#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800987
Josh Gao1290fbf2016-11-22 14:32:34 -0800988int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -0700989 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100990
991 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700992 char buf[32];
993 snprintf(buf, sizeof(buf), "T-%p", t);
994 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100995 }
Dan Albertc7915a32015-05-18 16:46:31 -0700996
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700997 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700998 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700999 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001000 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001002
Josh Gao1db71af2017-08-17 13:50:51 -07001003 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -07001004 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001005 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001006 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001007 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001008 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001009 return -1;
1010 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001011 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001012
Elliott Hughes65fe2512015-10-07 15:59:35 -07001013 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001014 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001015 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001016 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001017 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001018 return -1;
1019 }
1020 }
1021
Dan Albertc7915a32015-05-18 16:46:31 -07001022 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001023 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001024
1025 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -07001026
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001027 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001028 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029}
1030
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001031#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -08001032atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001033 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001034
Josh Gao1db71af2017-08-17 13:50:51 -07001035 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001036 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001037 if (t->serial && strcmp(serial, t->serial) == 0) {
1038 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001039 break;
1040 }
Dan Albertc7915a32015-05-18 16:46:31 -07001041 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001042
Dan Albertc7915a32015-05-18 16:46:31 -07001043 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001044}
1045
Yabin Cuif4b99282015-08-27 12:03:11 -07001046void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -07001047 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001048 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001049 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001050 // Kicking breaks the read_transport thread of this transport out of any read, then
1051 // the read_transport thread will notify the main thread to make this transport
1052 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001053 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001054 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001055 }
Dan Albertc7915a32015-05-18 16:46:31 -07001056 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001057}
1058
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001059#endif
1060
Josh Gao1290fbf2016-11-22 14:32:34 -08001061void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1062 unsigned writeable) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001063 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertc7915a32015-05-18 16:46:31 -07001064
Josh Gao1290fbf2016-11-22 14:32:34 -08001065 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001066 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001067 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 t->serial = strdup(serial);
1069 }
Dan Albertc7915a32015-05-18 16:46:31 -07001070
1071 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001072 t->devpath = strdup(devpath);
1073 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001074
Josh Gao0cd3ae12016-09-21 12:37:10 -07001075 {
Josh Gao1db71af2017-08-17 13:50:51 -07001076 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001077 pending_list.push_front(t);
1078 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001079
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 register_transport(t);
1081}
1082
Dan Albertdcd78a12015-05-18 16:43:57 -07001083// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001084void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001085 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao1290fbf2016-11-22 14:32:34 -08001086 transport_list.remove_if(
Yabin Cuib5e11412017-03-10 16:01:01 -08001087 [usb](atransport* t) { return t->usb == usb && t->GetConnectionState() == kCsNoPerm; });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001088}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089
Josh Gao36dadca2017-05-16 15:02:45 -07001090bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001091 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001092 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1093 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001094 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001095 }
1096
Josh Gao1290fbf2016-11-22 14:32:34 -08001097 if (p->msg.data_length > t->get_max_payload()) {
1098 VLOG(RWX) << "check_header(): " << p->msg.data_length
1099 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001100 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101 }
1102
Josh Gao36dadca2017-05-16 15:02:45 -07001103 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104}
1105
Josh Gao3bd28792016-10-05 19:02:29 -07001106#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001107std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001108 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1109
Josh Gao2e671202016-08-18 22:00:12 -07001110 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001111 keys_.pop_front();
1112 return result;
1113}
Josh Gao3bd28792016-10-05 19:02:29 -07001114#endif