blob: 089a1ecf4dc37ad395fb4181a78495be7a4ee7e3 [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;
Josh Gao06d61d42016-10-06 13:31:44 -0700166 p->msg.data_check = calculate_apacket_checksum(p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
168 print_packet("send", p);
169
170 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700171 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 }
173
Josh Gao06d61d42016-10-06 13:31:44 -0700174 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 fatal_errno("cannot enqueue packet on transport socket");
176 }
177}
178
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700179// The transport is opened by transport_register_func before
180// the read_transport and write_transport threads are started.
181//
182// The read_transport thread issues a SYNC(1, token) message to let
183// the write_transport thread know to start things up. In the event
184// of transport IO failure, the read_transport thread will post a
185// SYNC(0,0) message to ensure shutdown.
186//
187// The transport will not actually be closed until both threads exit, but the threads
188// will kick the transport on their way out to disconnect the underlying device.
189//
190// read_transport thread reads data from a transport (representing a usb/tcp connection),
191// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800192static void read_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800193 atransport* t = reinterpret_cast<atransport*>(_t);
194 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Josh Gao1290fbf2016-11-22 14:32:34 -0800196 adb_thread_setname(
197 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
198 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
199 t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 p = get_apacket();
201 p->msg.command = A_SYNC;
202 p->msg.arg0 = 1;
203 p->msg.arg1 = ++(t->sync_token);
204 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800205 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700207 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 goto oops;
209 }
210
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700211 D("%s: data pump started", t->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800212 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800213 ATRACE_NAME("read_transport loop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 p = get_apacket();
215
Josh Gaocfe72e22016-11-29 09:40:29 -0800216 {
217 ATRACE_NAME("read_transport read_remote");
218 if (t->read_from_remote(p, t) != 0) {
219 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800221 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
Yabin Cuib5e11412017-03-10 16:01:01 -0800223#if ADB_HOST
224 if (p->msg.command == 0) {
225 continue;
226 }
227#endif
Josh Gaocfe72e22016-11-29 09:40:29 -0800228 }
229
230 D("%s: received remote packet, sending to transport", t->serial);
231 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800233 D("%s: failed to write apacket to transport", t->serial);
234 goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236 }
237
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700238 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 p = get_apacket();
240 p->msg.command = A_SYNC;
241 p->msg.arg0 = 0;
242 p->msg.arg1 = 0;
243 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800244 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700246 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 }
248
249oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700250 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 kick_transport(t);
252 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253}
254
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700255// write_transport thread gets packets sent by the main thread (through send_packet()),
256// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800257static void write_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800258 atransport* t = reinterpret_cast<atransport*>(_t);
259 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 int active = 0;
261
Josh Gao1290fbf2016-11-22 14:32:34 -0800262 adb_thread_setname(
263 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
264 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
Josh Gao1290fbf2016-11-22 14:32:34 -0800266 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800267 ATRACE_NAME("write_transport loop");
Josh Gao1290fbf2016-11-22 14:32:34 -0800268 if (read_packet(t->fd, t->serial, &p)) {
269 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 -0800270 break;
271 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800272
Josh Gao1290fbf2016-11-22 14:32:34 -0800273 if (p->msg.command == A_SYNC) {
274 if (p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700275 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 put_apacket(p);
277 break;
278 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800279 if (p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700280 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 active = 1;
282 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800283 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 -0800284 }
285 }
286 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800287 if (active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700288 D("%s: transport got packet, sending to remote", t->serial);
Josh Gaocfe72e22016-11-29 09:40:29 -0800289 ATRACE_NAME("write_transport write_remote");
Yabin Cuib5e11412017-03-10 16:01:01 -0800290 if (t->Write(p) != 0) {
291 D("%s: remote write failed for transport", t->serial);
292 put_apacket(p);
293 break;
294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700296 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 }
298 }
299
300 put_apacket(p);
301 }
302
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700303 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 kick_transport(t);
305 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306}
307
Yabin Cuif4b99282015-08-27 12:03:11 -0700308void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700309 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700310 // As kick_transport() can be called from threads without guarantee that t is valid,
311 // check if the transport is in transport_list first.
Josh Gaob122b172017-08-16 16:57:01 -0700312 //
313 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700314 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700315 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700316 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700317}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318
319static int transport_registration_send = -1;
320static int transport_registration_recv = -1;
321static fdevent transport_registration_fde;
322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324
325/* this adds support required by the 'track-devices' service.
326 * this is used to send the content of "list_transport" to any
327 * number of client connections that want it through a single
328 * live TCP connection
329 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800331 asocket socket;
Josh Gaob0c18022017-08-14 18:57:54 -0700332 bool update_needed;
333 bool long_output;
Josh Gao1290fbf2016-11-22 14:32:34 -0800334 device_tracker* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335};
336
337/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800338static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339
Josh Gao1290fbf2016-11-22 14:32:34 -0800340static void device_tracker_remove(device_tracker* tracker) {
341 device_tracker** pnode = &device_tracker_list;
342 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343
Josh Gao1db71af2017-08-17 13:50:51 -0700344 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 while (node) {
346 if (node == tracker) {
347 *pnode = node->next;
348 break;
349 }
350 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800351 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353}
354
Josh Gao1290fbf2016-11-22 14:32:34 -0800355static void device_tracker_close(asocket* socket) {
356 device_tracker* tracker = (device_tracker*)socket;
357 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
Josh Gao1290fbf2016-11-22 14:32:34 -0800359 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 if (peer) {
361 peer->peer = NULL;
362 peer->close(peer);
363 }
364 device_tracker_remove(tracker);
365 free(tracker);
366}
367
Josh Gao1290fbf2016-11-22 14:32:34 -0800368static int device_tracker_enqueue(asocket* socket, apacket* p) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 /* you can't read from a device tracker, close immediately */
370 put_apacket(p);
371 device_tracker_close(socket);
372 return -1;
373}
374
Elliott Hughese67f1f82015-04-30 17:32:03 -0700375static int device_tracker_send(device_tracker* tracker, const std::string& string) {
376 apacket* p = get_apacket();
377 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Elliott Hughese67f1f82015-04-30 17:32:03 -0700379 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
380 memcpy(&p->data[4], string.data(), string.size());
381 p->len = 4 + string.size();
382 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383}
384
Elliott Hughese67f1f82015-04-30 17:32:03 -0700385static void device_tracker_ready(asocket* socket) {
386 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387
Elliott Hughese67f1f82015-04-30 17:32:03 -0700388 // We want to send the device list when the tracker connects
389 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700390 if (tracker->update_needed) {
391 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392
Josh Gaob0c18022017-08-14 18:57:54 -0700393 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700394 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 }
396}
397
Josh Gaob0c18022017-08-14 18:57:54 -0700398asocket* create_device_tracker(bool long_output) {
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700399 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
400 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401
Josh Gao1290fbf2016-11-22 14:32:34 -0800402 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
404 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800405 tracker->socket.ready = device_tracker_ready;
406 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700407 tracker->update_needed = true;
408 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409
Josh Gao1290fbf2016-11-22 14:32:34 -0800410 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 device_tracker_list = tracker;
412
413 return &tracker->socket;
414}
415
Josh Gaofd713e52017-05-03 22:37:10 -0700416// Check if all of the USB transports are connected.
417bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700418 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700419 for (const auto& t : transport_list) {
420 if (!fn(t)) {
421 return false;
422 }
423 }
424 for (const auto& t : pending_list) {
425 if (!fn(t)) {
426 return false;
427 }
428 }
429 return true;
430}
431
Elliott Hughese67f1f82015-04-30 17:32:03 -0700432// Call this function each time the transport list has changed.
433void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700434 update_transport_status();
435
436 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700437 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438
Elliott Hughese67f1f82015-04-30 17:32:03 -0700439 device_tracker* tracker = device_tracker_list;
440 while (tracker != nullptr) {
441 device_tracker* next = tracker->next;
442 // This may destroy the tracker if the connection is closed.
443 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 tracker = next;
445 }
446}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700449
450void update_transports() {
451 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700453
Josh Gao1290fbf2016-11-22 14:32:34 -0800454#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455
Josh Gao1290fbf2016-11-22 14:32:34 -0800456struct tmsg {
457 atransport* transport;
458 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459};
460
Josh Gao1290fbf2016-11-22 14:32:34 -0800461static int transport_read_action(int fd, struct tmsg* m) {
462 char* p = (char*)m;
463 int len = sizeof(*m);
464 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
Josh Gao1290fbf2016-11-22 14:32:34 -0800466 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800468 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800470 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700472 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 return -1;
474 }
475 }
476 return 0;
477}
478
Josh Gao1290fbf2016-11-22 14:32:34 -0800479static int transport_write_action(int fd, struct tmsg* m) {
480 char* p = (char*)m;
481 int len = sizeof(*m);
482 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483
Josh Gao1290fbf2016-11-22 14:32:34 -0800484 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800486 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800488 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700490 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 return -1;
492 }
493 }
494 return 0;
495}
496
Josh Gao1290fbf2016-11-22 14:32:34 -0800497static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 int s[2];
Josh Gao1290fbf2016-11-22 14:32:34 -0800500 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501
Josh Gao1290fbf2016-11-22 14:32:34 -0800502 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 return;
504 }
505
Josh Gao1290fbf2016-11-22 14:32:34 -0800506 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 fatal_errno("cannot read transport registration socket");
508 }
509
510 t = m.transport;
511
Dan Albert1792c232015-05-18 13:06:53 -0700512 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700513 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514
Josh Gao1290fbf2016-11-22 14:32:34 -0800515 /* IMPORTANT: the remove closes one half of the
516 ** socket pair. The close closes the other half.
517 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 fdevent_remove(&(t->transport_fde));
519 adb_close(t->fd);
520
Josh Gao0cd3ae12016-09-21 12:37:10 -0700521 {
Josh Gao1db71af2017-08-17 13:50:51 -0700522 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700523 transport_list.remove(t);
524 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
Josh Gao1290fbf2016-11-22 14:32:34 -0800526 if (t->product) free(t->product);
527 if (t->serial) free(t->serial);
528 if (t->model) free(t->model);
529 if (t->device) free(t->device);
530 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531
Dan Albertc7915a32015-05-18 16:46:31 -0700532 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
534 update_transports();
535 return;
536 }
537
Mike Lockwood0927bf92009-08-08 12:37:44 -0400538 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800539 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400541 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542
Dan Albertc7915a32015-05-18 16:46:31 -0700543 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400544 fatal_errno("cannot open transport socketpair");
545 }
546
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700547 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400548
549 t->transport_socket = s[0];
550 t->fd = s[1];
551
Josh Gao1290fbf2016-11-22 14:32:34 -0800552 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400553
554 fdevent_set(&(t->transport_fde), FDE_READ);
555
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700556 std::thread(write_transport_thread, t).detach();
557 std::thread(read_transport_thread, t).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 }
559
Josh Gao0cd3ae12016-09-21 12:37:10 -0700560 {
Josh Gao1db71af2017-08-17 13:50:51 -0700561 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700562 pending_list.remove(t);
563 transport_list.push_front(t);
564 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566 update_transports();
567}
568
Josh Gao1290fbf2016-11-22 14:32:34 -0800569void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570 int s[2];
571
Josh Gao1290fbf2016-11-22 14:32:34 -0800572 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573 fatal_errno("cannot open transport registration socketpair");
574 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700575 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576
577 transport_registration_send = s[0];
578 transport_registration_recv = s[1];
579
Josh Gao1290fbf2016-11-22 14:32:34 -0800580 fdevent_install(&transport_registration_fde, transport_registration_recv,
581 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582
583 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700584}
585
586void kick_all_transports() {
587 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700588 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700589 for (auto t : transport_list) {
590 t->Kick();
591 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592}
593
594/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800595static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 tmsg m;
597 m.transport = transport;
598 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700599 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800600 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 fatal_errno("cannot write transport registration socket\n");
602 }
603}
604
Josh Gao1290fbf2016-11-22 14:32:34 -0800605static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 tmsg m;
607 m.transport = transport;
608 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700609 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800610 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 fatal_errno("cannot write transport registration socket\n");
612 }
613}
614
Yabin Cuif4b99282015-08-27 12:03:11 -0700615static void transport_unref(atransport* t) {
616 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700617
Josh Gaoe48ecce2017-09-13 13:40:57 -0700618 std::lock_guard<std::recursive_mutex> lock(transport_lock);
619 CHECK_GT(t->ref_count, 0u);
620 t->ref_count--;
621 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700622 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400623 t->close(t);
624 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100625 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700626 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400627 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628}
629
Josh Gao1290fbf2016-11-22 14:32:34 -0800630static int qual_match(const char* to_test, const char* prefix, const char* qual,
631 bool sanitize_qual) {
632 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700633 return !qual || !*qual;
634
Josh Gao1290fbf2016-11-22 14:32:34 -0800635 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700636
637 if (prefix) {
638 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800639 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700640 }
641 }
642
643 while (*qual) {
644 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800645 if (sanitize_qual && !isalnum(ch)) ch = '_';
646 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700647 }
648
649 /* Everything matched so far. Return true if *to_test is a NUL. */
650 return !*to_test;
651}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652
Josh Gaob122b172017-08-16 16:57:01 -0700653atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
654 bool* is_ambiguous, std::string* error_out,
655 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700656 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657
Josh Gaob122b172017-08-16 16:57:01 -0700658 if (transport_id != 0) {
659 *error_out =
660 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
661 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700662 *error_out = android::base::StringPrintf("device '%s' not found", serial);
663 } else if (type == kTransportLocal) {
664 *error_out = "no emulators found";
665 } else if (type == kTransportAny) {
666 *error_out = "no devices/emulators found";
667 } else {
668 *error_out = "no devices found";
669 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670
Josh Gao1db71af2017-08-17 13:50:51 -0700671 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700672 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800673 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800674#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800675 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800676#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400677 continue;
678 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400679
Josh Gaob122b172017-08-16 16:57:01 -0700680 if (transport_id) {
681 if (t->id == transport_id) {
682 result = t;
683 break;
684 }
685 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800686 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700687 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700688 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700689 if (is_ambiguous) *is_ambiguous = true;
690 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700691 break;
692 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700694 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700696 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700698 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700699 if (is_ambiguous) *is_ambiguous = true;
700 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 break;
702 }
703 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700704 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700706 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700707 if (is_ambiguous) *is_ambiguous = true;
708 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 break;
710 }
711 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700712 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700714 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700715 if (is_ambiguous) *is_ambiguous = true;
716 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 break;
718 }
719 result = t;
720 }
721 }
722 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700723 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724
Elliott Hughes8d28e192015-10-07 14:55:10 -0700725 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800726 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700727 *error_out = "device unauthorized.\n";
728 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
729 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
730 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
731 *error_out += "\n";
732 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
733 *error_out += "Otherwise check for a confirmation dialog on your device.";
734 result = nullptr;
735 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800736
Elliott Hughes8d28e192015-10-07 14:55:10 -0700737 // Don't return offline devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800738 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700739 *error_out = "device offline";
740 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 }
742
743 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700744 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745 }
746
747 return result;
748}
749
Yabin Cuib5e11412017-03-10 16:01:01 -0800750int atransport::Write(apacket* p) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800751 return write_func_(p, this);
752}
753
Yabin Cui7f274902016-04-18 11:22:34 -0700754void atransport::Kick() {
755 if (!kicked_) {
756 kicked_ = true;
757 CHECK(kick_func_ != nullptr);
758 kick_func_(this);
759 }
760}
761
Yabin Cuib5e11412017-03-10 16:01:01 -0800762ConnectionState atransport::GetConnectionState() const {
763 return connection_state_;
764}
765
766void atransport::SetConnectionState(ConnectionState state) {
767 check_main_thread();
768 connection_state_ = state;
769}
770
David Purselld2acbd12015-12-02 15:14:31 -0800771const std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800772 ConnectionState state = GetConnectionState();
773 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800774 case kCsOffline:
775 return "offline";
776 case kCsBootloader:
777 return "bootloader";
778 case kCsDevice:
779 return "device";
780 case kCsHost:
781 return "host";
782 case kCsRecovery:
783 return "recovery";
784 case kCsNoPerm:
785 return UsbNoPermissionsShortHelpText();
786 case kCsSideload:
787 return "sideload";
788 case kCsUnauthorized:
789 return "unauthorized";
790 default:
791 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 }
793}
794
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100795void atransport::update_version(int version, size_t payload) {
796 protocol_version = std::min(version, A_VERSION);
797 max_payload = std::min(payload, MAX_PAYLOAD);
798}
799
800int atransport::get_protocol_version() const {
801 return protocol_version;
802}
803
804size_t atransport::get_max_payload() const {
805 return max_payload;
806}
807
David Pursell4e2fd362015-09-22 10:43:08 -0700808namespace {
David Pursell0955c662015-08-31 10:42:13 -0700809
David Pursell4e2fd362015-09-22 10:43:08 -0700810constexpr char kFeatureStringDelimiter = ',';
811
812} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700813
814const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700815 // Local static allocation to avoid global non-POD variables.
816 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800817 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700818 // Increment ADB_SERVER_VERSION whenever the feature list changes to
819 // make sure that the adb client and server features stay in sync
820 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700821 };
822
823 return *features;
824}
825
826std::string FeatureSetToString(const FeatureSet& features) {
827 return android::base::Join(features, kFeatureStringDelimiter);
828}
829
830FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700831 if (features_string.empty()) {
832 return FeatureSet();
833 }
834
Josh Gao1290fbf2016-11-22 14:32:34 -0800835 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700836 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700837}
838
David Pursell70ef7b42015-09-30 13:35:42 -0700839bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800840 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700841}
842
Dan Albert1792c232015-05-18 13:06:53 -0700843bool atransport::has_feature(const std::string& feature) const {
844 return features_.count(feature) > 0;
845}
846
David Pursell4e2fd362015-09-22 10:43:08 -0700847void atransport::SetFeatures(const std::string& features_string) {
848 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700849}
850
Yabin Cuib3298242015-08-28 15:09:44 -0700851void atransport::AddDisconnect(adisconnect* disconnect) {
852 disconnects_.push_back(disconnect);
853}
854
855void atransport::RemoveDisconnect(adisconnect* disconnect) {
856 disconnects_.remove(disconnect);
857}
858
859void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700860 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700861 disconnect->func(disconnect->opaque, this);
862 }
863 disconnects_.clear();
864}
865
David Pursell3f902aa2016-03-01 08:58:26 -0800866bool atransport::MatchesTarget(const std::string& target) const {
867 if (serial) {
868 if (target == serial) {
869 return true;
870 } else if (type == kTransportLocal) {
871 // Local transports can match [tcp:|udp:]<hostname>[:port].
872 const char* local_target_ptr = target.c_str();
873
874 // For fastboot compatibility, ignore protocol prefixes.
875 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800876 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800877 local_target_ptr += 4;
878 }
879
880 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
881 std::string serial_host, error;
882 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800883 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800884 // |target| may omit the port to default to ours.
885 std::string target_host;
886 int target_port = serial_port;
887 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
888 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800889 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800890 return true;
891 }
892 }
893 }
894 }
895
896 return (devpath && target == devpath) ||
897 qual_match(target.c_str(), "product:", product, false) ||
898 qual_match(target.c_str(), "model:", model, true) ||
899 qual_match(target.c_str(), "device:", device, false);
900}
901
Elliott Hughese67f1f82015-04-30 17:32:03 -0700902#if ADB_HOST
903
Josh Gaob122b172017-08-16 16:57:01 -0700904// We use newline as our delimiter, make sure to never output it.
905static std::string sanitize(std::string str, bool alphanumeric) {
906 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
907 : [](const char c) { return c == '\n'; };
908 std::replace_if(str.begin(), str.end(), pred, '_');
909 return str;
910}
911
Josh Gao1290fbf2016-11-22 14:32:34 -0800912static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -0700913 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700914 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700915 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700916 }
917
Elliott Hughese67f1f82015-04-30 17:32:03 -0700918 *result += ' ';
919 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -0700920 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700921}
922
Josh Gao1290fbf2016-11-22 14:32:34 -0800923static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700924 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700925 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700926 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700927 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700928
929 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700930 *result += serial;
931 *result += '\t';
932 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700933 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800934 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700935
Elliott Hughese67f1f82015-04-30 17:32:03 -0700936 append_transport_info(result, "", t->devpath, false);
937 append_transport_info(result, "product:", t->product, false);
938 append_transport_info(result, "model:", t->model, true);
939 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -0700940
941 // Put id at the end, so that anyone parsing the output here can always find it by scanning
942 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
943 *result += " transport_id:";
944 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700945 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700946 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700947}
948
Elliott Hughese67f1f82015-04-30 17:32:03 -0700949std::string list_transports(bool long_listing) {
950 std::string result;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700951
Josh Gao1db71af2017-08-17 13:50:51 -0700952 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700953 for (const auto& t : transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700954 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800955 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700956 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957}
958
Josh Gao22d2b3e2016-10-27 14:01:08 -0700959void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -0700960 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -0700961 for (auto& t : transport_list) {
962 if (predicate(t)) {
963 t->Kick();
964 }
965 }
966}
967
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700969void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -0700970 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971}
Josh Gao1290fbf2016-11-22 14:32:34 -0800972#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973
Josh Gao1290fbf2016-11-22 14:32:34 -0800974int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -0700975 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100976
977 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700978 char buf[32];
979 snprintf(buf, sizeof(buf), "T-%p", t);
980 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100981 }
Dan Albertc7915a32015-05-18 16:46:31 -0700982
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700983 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700984 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700985 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700986 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800987 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700988
Josh Gao1db71af2017-08-17 13:50:51 -0700989 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700990 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700991 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700992 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -0800993 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700994 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700995 return -1;
996 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800997 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700998
Elliott Hughes65fe2512015-10-07 15:59:35 -0700999 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001000 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001001 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001002 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001003 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001004 return -1;
1005 }
1006 }
1007
Dan Albertc7915a32015-05-18 16:46:31 -07001008 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001009 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001010
1011 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -07001012
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001014 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015}
1016
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001017#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -08001018atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001019 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001020
Josh Gao1db71af2017-08-17 13:50:51 -07001021 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001022 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001023 if (t->serial && strcmp(serial, t->serial) == 0) {
1024 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001025 break;
1026 }
Dan Albertc7915a32015-05-18 16:46:31 -07001027 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001028
Dan Albertc7915a32015-05-18 16:46:31 -07001029 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001030}
1031
Yabin Cuif4b99282015-08-27 12:03:11 -07001032void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -07001033 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001034 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001035 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001036 // Kicking breaks the read_transport thread of this transport out of any read, then
1037 // the read_transport thread will notify the main thread to make this transport
1038 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001039 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001040 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001041 }
Dan Albertc7915a32015-05-18 16:46:31 -07001042 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001043}
1044
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001045#endif
1046
Josh Gao1290fbf2016-11-22 14:32:34 -08001047void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1048 unsigned writeable) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001049 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertc7915a32015-05-18 16:46:31 -07001050
Josh Gao1290fbf2016-11-22 14:32:34 -08001051 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001052 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001053 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 t->serial = strdup(serial);
1055 }
Dan Albertc7915a32015-05-18 16:46:31 -07001056
1057 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001058 t->devpath = strdup(devpath);
1059 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001060
Josh Gao0cd3ae12016-09-21 12:37:10 -07001061 {
Josh Gao1db71af2017-08-17 13:50:51 -07001062 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001063 pending_list.push_front(t);
1064 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001065
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066 register_transport(t);
1067}
1068
Dan Albertdcd78a12015-05-18 16:43:57 -07001069// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001070void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001071 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao1290fbf2016-11-22 14:32:34 -08001072 transport_list.remove_if(
Yabin Cuib5e11412017-03-10 16:01:01 -08001073 [usb](atransport* t) { return t->usb == usb && t->GetConnectionState() == kCsNoPerm; });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001074}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075
Josh Gao36dadca2017-05-16 15:02:45 -07001076bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001077 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001078 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1079 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001080 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 }
1082
Josh Gao1290fbf2016-11-22 14:32:34 -08001083 if (p->msg.data_length > t->get_max_payload()) {
1084 VLOG(RWX) << "check_header(): " << p->msg.data_length
1085 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001086 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 }
1088
Josh Gao36dadca2017-05-16 15:02:45 -07001089 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090}
1091
Josh Gao36dadca2017-05-16 15:02:45 -07001092bool check_data(apacket* p) {
1093 return calculate_apacket_checksum(p) == p->msg.data_check;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094}
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001095
Josh Gao3bd28792016-10-05 19:02:29 -07001096#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001097std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001098 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1099
Josh Gao2e671202016-08-18 22:00:12 -07001100 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001101 keys_.pop_front();
1102 return result;
1103}
Josh Gao3bd28792016-10-05 19:02:29 -07001104#endif