blob: 4686841ececb02a09954aba636a250cf57d79785 [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Spencer Low363af562015-11-07 18:51:54 -080029#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070030#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070031#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070032#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070033
Elliott Hughes4f713192015-12-04 22:00:26 -080034#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080035#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/stringprintf.h>
37#include <android-base/strings.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070038
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070040#include "adb_auth.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080041#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070042#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080043#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
45static void transport_unref(atransport *t);
46
Josh Gaob7b1edf2015-11-11 17:56:12 -080047static auto& transport_list = *new std::list<atransport*>();
48static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070049
Josh Gao0cd3ae12016-09-21 12:37:10 -070050static std::mutex& transport_lock = *new std::mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Todd Kennedy51c05ec2015-11-10 00:03:25 +000052const char* const kFeatureShell2 = "shell_v2";
53const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080054const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080055const char* const kFeatureLibusb = "libusb";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000056
Yabin Cuiaed3c612015-09-22 15:52:57 -070057static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-11-22 14:32:34 -080058 unsigned command = p->msg.command;
59 int len = p->msg.data_length;
60 char cmd[9];
61 char arg0[12], arg1[12];
62 int n;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010063
64 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -080065 int b = (command >> (n * 8)) & 255;
66 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010067 cmd[n] = (char)b;
68 }
69 if (n == 4) {
70 cmd[4] = 0;
71 } else {
72 /* There is some non-ASCII name in the command, so dump
73 * the hexadecimal value instead */
74 snprintf(cmd, sizeof cmd, "%08x", command);
75 }
76
77 if (p->msg.arg0 < 256U)
78 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
79 else
80 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
81
82 if (p->msg.arg1 < 256U)
83 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
84 else
85 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
86
Josh Gao1290fbf2016-11-22 14:32:34 -080087 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
88 func, cmd, arg0, arg1, len);
Yabin Cuiaed3c612015-09-22 15:52:57 -070089 result += dump_hex(p->data, len);
90 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010091}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010092
Josh Gao1290fbf2016-11-22 14:32:34 -080093static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -080094 ATRACE_NAME("read_packet");
Yabin Cui62641292015-07-30 19:58:10 -070095 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010096 if (!name) {
97 snprintf(buff, sizeof buff, "fd=%d", fd);
98 name = buff;
99 }
Josh Gao1290fbf2016-11-22 14:32:34 -0800100 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700101 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800102 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700103 int r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800104 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700106 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700108 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 -0800109 return -1;
110 }
111 }
112
Yabin Cuiaed3c612015-09-22 15:52:57 -0700113 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 return 0;
115}
116
Josh Gao1290fbf2016-11-22 14:32:34 -0800117static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800118 ATRACE_NAME("write_packet");
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100119 char buff[8];
120 if (!name) {
121 snprintf(buff, sizeof buff, "fd=%d", fd);
122 name = buff;
123 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700124 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao1290fbf2016-11-22 14:32:34 -0800125 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700126 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800127 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700128 int r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800129 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 len -= r;
131 p += r;
132 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700133 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 -0800134 return -1;
135 }
136 }
137 return 0;
138}
139
Josh Gao1290fbf2016-11-22 14:32:34 -0800140static void transport_socket_events(int fd, unsigned events, void* _t) {
141 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700142 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao1290fbf2016-11-22 14:32:34 -0800143 if (events & FDE_READ) {
144 apacket* p = 0;
145 if (read_packet(fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700146 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 -0800147 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800148 handle_packet(p, (atransport*)_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 }
150 }
151}
152
Josh Gao06d61d42016-10-06 13:31:44 -0700153void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 p->msg.magic = p->msg.command ^ 0xffffffff;
Josh Gao06d61d42016-10-06 13:31:44 -0700155 p->msg.data_check = calculate_apacket_checksum(p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
157 print_packet("send", p);
158
159 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700160 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 }
162
Josh Gao06d61d42016-10-06 13:31:44 -0700163 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 fatal_errno("cannot enqueue packet on transport socket");
165 }
166}
167
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700168// The transport is opened by transport_register_func before
169// the read_transport and write_transport threads are started.
170//
171// The read_transport thread issues a SYNC(1, token) message to let
172// the write_transport thread know to start things up. In the event
173// of transport IO failure, the read_transport thread will post a
174// SYNC(0,0) message to ensure shutdown.
175//
176// The transport will not actually be closed until both threads exit, but the threads
177// will kick the transport on their way out to disconnect the underlying device.
178//
179// read_transport thread reads data from a transport (representing a usb/tcp connection),
180// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800181static void read_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800182 atransport* t = reinterpret_cast<atransport*>(_t);
183 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Josh Gao1290fbf2016-11-22 14:32:34 -0800185 adb_thread_setname(
186 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
187 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
188 t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 p = get_apacket();
190 p->msg.command = A_SYNC;
191 p->msg.arg0 = 1;
192 p->msg.arg1 = ++(t->sync_token);
193 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800194 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700196 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 goto oops;
198 }
199
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700200 D("%s: data pump started", t->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800201 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800202 ATRACE_NAME("read_transport loop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 p = get_apacket();
204
Josh Gaocfe72e22016-11-29 09:40:29 -0800205 {
206 ATRACE_NAME("read_transport read_remote");
207 if (t->read_from_remote(p, t) != 0) {
208 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800210 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800212 }
213
214 D("%s: received remote packet, sending to transport", t->serial);
215 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800217 D("%s: failed to write apacket to transport", t->serial);
218 goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 }
220 }
221
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700222 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 p = get_apacket();
224 p->msg.command = A_SYNC;
225 p->msg.arg0 = 0;
226 p->msg.arg1 = 0;
227 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800228 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700230 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
233oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700234 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 kick_transport(t);
236 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237}
238
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700239// write_transport thread gets packets sent by the main thread (through send_packet()),
240// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800241static void write_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800242 atransport* t = reinterpret_cast<atransport*>(_t);
243 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 int active = 0;
245
Josh Gao1290fbf2016-11-22 14:32:34 -0800246 adb_thread_setname(
247 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
248 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
Josh Gao1290fbf2016-11-22 14:32:34 -0800250 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800251 ATRACE_NAME("write_transport loop");
Josh Gao1290fbf2016-11-22 14:32:34 -0800252 if (read_packet(t->fd, t->serial, &p)) {
253 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 -0800254 break;
255 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800256
Josh Gao1290fbf2016-11-22 14:32:34 -0800257 if (p->msg.command == A_SYNC) {
258 if (p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700259 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 put_apacket(p);
261 break;
262 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800263 if (p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 active = 1;
266 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800267 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 -0800268 }
269 }
270 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800271 if (active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700272 D("%s: transport got packet, sending to remote", t->serial);
Josh Gaocfe72e22016-11-29 09:40:29 -0800273 ATRACE_NAME("write_transport write_remote");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 t->write_to_remote(p, t);
275 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700276 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 }
278 }
279
280 put_apacket(p);
281 }
282
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700283 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 kick_transport(t);
285 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286}
287
Yabin Cuif4b99282015-08-27 12:03:11 -0700288void kick_transport(atransport* t) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700289 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700290 // As kick_transport() can be called from threads without guarantee that t is valid,
291 // check if the transport is in transport_list first.
292 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700293 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700294 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700295}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296
297static int transport_registration_send = -1;
298static int transport_registration_recv = -1;
299static fdevent transport_registration_fde;
300
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
303/* this adds support required by the 'track-devices' service.
304 * this is used to send the content of "list_transport" to any
305 * number of client connections that want it through a single
306 * live TCP connection
307 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800309 asocket socket;
310 int update_needed;
311 device_tracker* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312};
313
314/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800315static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316
Josh Gao1290fbf2016-11-22 14:32:34 -0800317static void device_tracker_remove(device_tracker* tracker) {
318 device_tracker** pnode = &device_tracker_list;
319 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320
Josh Gao0cd3ae12016-09-21 12:37:10 -0700321 std::lock_guard<std::mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 while (node) {
323 if (node == tracker) {
324 *pnode = node->next;
325 break;
326 }
327 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800328 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330}
331
Josh Gao1290fbf2016-11-22 14:32:34 -0800332static void device_tracker_close(asocket* socket) {
333 device_tracker* tracker = (device_tracker*)socket;
334 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335
Josh Gao1290fbf2016-11-22 14:32:34 -0800336 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 if (peer) {
338 peer->peer = NULL;
339 peer->close(peer);
340 }
341 device_tracker_remove(tracker);
342 free(tracker);
343}
344
Josh Gao1290fbf2016-11-22 14:32:34 -0800345static int device_tracker_enqueue(asocket* socket, apacket* p) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 /* you can't read from a device tracker, close immediately */
347 put_apacket(p);
348 device_tracker_close(socket);
349 return -1;
350}
351
Elliott Hughese67f1f82015-04-30 17:32:03 -0700352static int device_tracker_send(device_tracker* tracker, const std::string& string) {
353 apacket* p = get_apacket();
354 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355
Elliott Hughese67f1f82015-04-30 17:32:03 -0700356 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
357 memcpy(&p->data[4], string.data(), string.size());
358 p->len = 4 + string.size();
359 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360}
361
Elliott Hughese67f1f82015-04-30 17:32:03 -0700362static void device_tracker_ready(asocket* socket) {
363 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364
Elliott Hughese67f1f82015-04-30 17:32:03 -0700365 // We want to send the device list when the tracker connects
366 // for the first time, even if no update occurred.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 if (tracker->update_needed > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 tracker->update_needed = 0;
369
Elliott Hughese67f1f82015-04-30 17:32:03 -0700370 std::string transports = list_transports(false);
371 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
373}
374
Josh Gao1290fbf2016-11-22 14:32:34 -0800375asocket* create_device_tracker(void) {
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700376 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
377 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Josh Gao1290fbf2016-11-22 14:32:34 -0800379 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380
381 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800382 tracker->socket.ready = device_tracker_ready;
383 tracker->socket.close = device_tracker_close;
384 tracker->update_needed = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
Josh Gao1290fbf2016-11-22 14:32:34 -0800386 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 device_tracker_list = tracker;
388
389 return &tracker->socket;
390}
391
Elliott Hughese67f1f82015-04-30 17:32:03 -0700392// Call this function each time the transport list has changed.
393void update_transports() {
394 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
Elliott Hughese67f1f82015-04-30 17:32:03 -0700396 device_tracker* tracker = device_tracker_list;
397 while (tracker != nullptr) {
398 device_tracker* next = tracker->next;
399 // This may destroy the tracker if the connection is closed.
400 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 tracker = next;
402 }
403}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700406
407void update_transports() {
408 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700410
Josh Gao1290fbf2016-11-22 14:32:34 -0800411#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412
Josh Gao1290fbf2016-11-22 14:32:34 -0800413struct tmsg {
414 atransport* transport;
415 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416};
417
Josh Gao1290fbf2016-11-22 14:32:34 -0800418static int transport_read_action(int fd, struct tmsg* m) {
419 char* p = (char*)m;
420 int len = sizeof(*m);
421 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422
Josh Gao1290fbf2016-11-22 14:32:34 -0800423 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800425 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800427 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700429 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 return -1;
431 }
432 }
433 return 0;
434}
435
Josh Gao1290fbf2016-11-22 14:32:34 -0800436static int transport_write_action(int fd, struct tmsg* m) {
437 char* p = (char*)m;
438 int len = sizeof(*m);
439 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Josh Gao1290fbf2016-11-22 14:32:34 -0800441 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800443 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800445 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700447 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 return -1;
449 }
450 }
451 return 0;
452}
453
Josh Gao1290fbf2016-11-22 14:32:34 -0800454static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 int s[2];
Josh Gao1290fbf2016-11-22 14:32:34 -0800457 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Josh Gao1290fbf2016-11-22 14:32:34 -0800459 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return;
461 }
462
Josh Gao1290fbf2016-11-22 14:32:34 -0800463 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 fatal_errno("cannot read transport registration socket");
465 }
466
467 t = m.transport;
468
Dan Albert1792c232015-05-18 13:06:53 -0700469 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700470 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Josh Gao1290fbf2016-11-22 14:32:34 -0800472 /* IMPORTANT: the remove closes one half of the
473 ** socket pair. The close closes the other half.
474 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 fdevent_remove(&(t->transport_fde));
476 adb_close(t->fd);
477
Josh Gao0cd3ae12016-09-21 12:37:10 -0700478 {
479 std::lock_guard<std::mutex> lock(transport_lock);
480 transport_list.remove(t);
481 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482
Josh Gao1290fbf2016-11-22 14:32:34 -0800483 if (t->product) free(t->product);
484 if (t->serial) free(t->serial);
485 if (t->model) free(t->model);
486 if (t->device) free(t->device);
487 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Dan Albertc7915a32015-05-18 16:46:31 -0700489 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
491 update_transports();
492 return;
493 }
494
Mike Lockwood0927bf92009-08-08 12:37:44 -0400495 /* don't create transport threads for inaccessible devices */
Dan Albertdcd78a12015-05-18 16:43:57 -0700496 if (t->connection_state != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400498 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Dan Albertc7915a32015-05-18 16:46:31 -0700500 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400501 fatal_errno("cannot open transport socketpair");
502 }
503
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700504 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400505
506 t->transport_socket = s[0];
507 t->fd = s[1];
508
Josh Gao1290fbf2016-11-22 14:32:34 -0800509 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400510
511 fdevent_set(&(t->transport_fde), FDE_READ);
512
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700513 std::thread(write_transport_thread, t).detach();
514 std::thread(read_transport_thread, t).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 }
516
Josh Gao0cd3ae12016-09-21 12:37:10 -0700517 {
518 std::lock_guard<std::mutex> lock(transport_lock);
519 pending_list.remove(t);
520 transport_list.push_front(t);
521 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 update_transports();
524}
525
Josh Gao1290fbf2016-11-22 14:32:34 -0800526void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 int s[2];
528
Josh Gao1290fbf2016-11-22 14:32:34 -0800529 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 fatal_errno("cannot open transport registration socketpair");
531 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700532 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533
534 transport_registration_send = s[0];
535 transport_registration_recv = s[1];
536
Josh Gao1290fbf2016-11-22 14:32:34 -0800537 fdevent_install(&transport_registration_fde, transport_registration_recv,
538 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539
540 fdevent_set(&transport_registration_fde, FDE_READ);
541}
542
543/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800544static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 tmsg m;
546 m.transport = transport;
547 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700548 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800549 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800550 fatal_errno("cannot write transport registration socket\n");
551 }
552}
553
Josh Gao1290fbf2016-11-22 14:32:34 -0800554static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555 tmsg m;
556 m.transport = transport;
557 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700558 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800559 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 fatal_errno("cannot write transport registration socket\n");
561 }
562}
563
Yabin Cuif4b99282015-08-27 12:03:11 -0700564static void transport_unref(atransport* t) {
565 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700566
567 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700568 CHECK_GT(t->ref_count, 0u);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400569 t->ref_count--;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400570 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700571 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400572 t->close(t);
573 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100574 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700575 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400576 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577}
578
Josh Gao1290fbf2016-11-22 14:32:34 -0800579static int qual_match(const char* to_test, const char* prefix, const char* qual,
580 bool sanitize_qual) {
581 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700582 return !qual || !*qual;
583
Josh Gao1290fbf2016-11-22 14:32:34 -0800584 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700585
586 if (prefix) {
587 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800588 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700589 }
590 }
591
592 while (*qual) {
593 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800594 if (sanitize_qual && !isalnum(ch)) ch = '_';
595 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700596 }
597
598 /* Everything matched so far. Return true if *to_test is a NUL. */
599 return !*to_test;
600}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601
Josh Gao1290fbf2016-11-22 14:32:34 -0800602atransport* acquire_one_transport(TransportType type, const char* serial, bool* is_ambiguous,
603 std::string* error_out) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700604 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605
Elliott Hughes8d28e192015-10-07 14:55:10 -0700606 if (serial) {
607 *error_out = android::base::StringPrintf("device '%s' not found", serial);
608 } else if (type == kTransportLocal) {
609 *error_out = "no emulators found";
610 } else if (type == kTransportAny) {
611 *error_out = "no devices/emulators found";
612 } else {
613 *error_out = "no devices found";
614 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615
Josh Gao0cd3ae12016-09-21 12:37:10 -0700616 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700617 for (const auto& t : transport_list) {
Dan Albertdcd78a12015-05-18 16:43:57 -0700618 if (t->connection_state == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800619#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800620 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800621#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400622 continue;
623 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400624
Elliott Hughes8d28e192015-10-07 14:55:10 -0700625 // Check for matching serial number.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626 if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800627 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700628 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700629 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700630 if (is_ambiguous) *is_ambiguous = true;
631 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700632 break;
633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700635 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700637 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700639 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700640 if (is_ambiguous) *is_ambiguous = true;
641 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 break;
643 }
644 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700645 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700647 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700648 if (is_ambiguous) *is_ambiguous = true;
649 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 break;
651 }
652 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700653 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700655 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700656 if (is_ambiguous) *is_ambiguous = true;
657 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 break;
659 }
660 result = t;
661 }
662 }
663 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700664 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665
Elliott Hughes8d28e192015-10-07 14:55:10 -0700666 // Don't return unauthorized devices; the caller can't do anything with them.
667 if (result && result->connection_state == kCsUnauthorized) {
668 *error_out = "device unauthorized.\n";
669 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
670 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
671 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
672 *error_out += "\n";
673 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
674 *error_out += "Otherwise check for a confirmation dialog on your device.";
675 result = nullptr;
676 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800677
Elliott Hughes8d28e192015-10-07 14:55:10 -0700678 // Don't return offline devices; the caller can't do anything with them.
679 if (result && result->connection_state == kCsOffline) {
680 *error_out = "device offline";
681 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 }
683
684 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700685 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
688 return result;
689}
690
Yabin Cui7f274902016-04-18 11:22:34 -0700691void atransport::Kick() {
692 if (!kicked_) {
693 kicked_ = true;
694 CHECK(kick_func_ != nullptr);
695 kick_func_(this);
696 }
697}
698
David Purselld2acbd12015-12-02 15:14:31 -0800699const std::string atransport::connection_state_name() const {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700700 switch (connection_state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800701 case kCsOffline:
702 return "offline";
703 case kCsBootloader:
704 return "bootloader";
705 case kCsDevice:
706 return "device";
707 case kCsHost:
708 return "host";
709 case kCsRecovery:
710 return "recovery";
711 case kCsNoPerm:
712 return UsbNoPermissionsShortHelpText();
713 case kCsSideload:
714 return "sideload";
715 case kCsUnauthorized:
716 return "unauthorized";
717 default:
718 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 }
720}
721
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100722void atransport::update_version(int version, size_t payload) {
723 protocol_version = std::min(version, A_VERSION);
724 max_payload = std::min(payload, MAX_PAYLOAD);
725}
726
727int atransport::get_protocol_version() const {
728 return protocol_version;
729}
730
731size_t atransport::get_max_payload() const {
732 return max_payload;
733}
734
David Pursell4e2fd362015-09-22 10:43:08 -0700735namespace {
David Pursell0955c662015-08-31 10:42:13 -0700736
David Pursell4e2fd362015-09-22 10:43:08 -0700737constexpr char kFeatureStringDelimiter = ',';
738
739} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700740
741const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700742 // Local static allocation to avoid global non-POD variables.
743 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800744 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700745 // Increment ADB_SERVER_VERSION whenever the feature list changes to
746 // make sure that the adb client and server features stay in sync
747 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700748 };
749
750 return *features;
751}
752
753std::string FeatureSetToString(const FeatureSet& features) {
754 return android::base::Join(features, kFeatureStringDelimiter);
755}
756
757FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700758 if (features_string.empty()) {
759 return FeatureSet();
760 }
761
Josh Gao1290fbf2016-11-22 14:32:34 -0800762 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700763 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700764}
765
David Pursell70ef7b42015-09-30 13:35:42 -0700766bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800767 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700768}
769
Dan Albert1792c232015-05-18 13:06:53 -0700770bool atransport::has_feature(const std::string& feature) const {
771 return features_.count(feature) > 0;
772}
773
David Pursell4e2fd362015-09-22 10:43:08 -0700774void atransport::SetFeatures(const std::string& features_string) {
775 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700776}
777
Yabin Cuib3298242015-08-28 15:09:44 -0700778void atransport::AddDisconnect(adisconnect* disconnect) {
779 disconnects_.push_back(disconnect);
780}
781
782void atransport::RemoveDisconnect(adisconnect* disconnect) {
783 disconnects_.remove(disconnect);
784}
785
786void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700787 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700788 disconnect->func(disconnect->opaque, this);
789 }
790 disconnects_.clear();
791}
792
David Pursell3f902aa2016-03-01 08:58:26 -0800793bool atransport::MatchesTarget(const std::string& target) const {
794 if (serial) {
795 if (target == serial) {
796 return true;
797 } else if (type == kTransportLocal) {
798 // Local transports can match [tcp:|udp:]<hostname>[:port].
799 const char* local_target_ptr = target.c_str();
800
801 // For fastboot compatibility, ignore protocol prefixes.
802 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800803 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800804 local_target_ptr += 4;
805 }
806
807 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
808 std::string serial_host, error;
809 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800810 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800811 // |target| may omit the port to default to ours.
812 std::string target_host;
813 int target_port = serial_port;
814 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
815 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800816 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800817 return true;
818 }
819 }
820 }
821 }
822
823 return (devpath && target == devpath) ||
824 qual_match(target.c_str(), "product:", product, false) ||
825 qual_match(target.c_str(), "model:", model, true) ||
826 qual_match(target.c_str(), "device:", device, false);
827}
828
Elliott Hughese67f1f82015-04-30 17:32:03 -0700829#if ADB_HOST
830
Josh Gao1290fbf2016-11-22 14:32:34 -0800831static void append_transport_info(std::string* result, const char* key, const char* value,
832 bool sanitize) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700833 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700834 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700835 }
836
Elliott Hughese67f1f82015-04-30 17:32:03 -0700837 *result += ' ';
838 *result += key;
839
840 for (const char* p = value; *p; ++p) {
841 result->push_back((!sanitize || isalnum(*p)) ? *p : '_');
842 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700843}
844
Josh Gao1290fbf2016-11-22 14:32:34 -0800845static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700846 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700847 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700848 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700849 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700850
851 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700852 *result += serial;
853 *result += '\t';
854 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700855 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800856 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700857
Elliott Hughese67f1f82015-04-30 17:32:03 -0700858 append_transport_info(result, "", t->devpath, false);
859 append_transport_info(result, "product:", t->product, false);
860 append_transport_info(result, "model:", t->model, true);
861 append_transport_info(result, "device:", t->device, false);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700862 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700863 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700864}
865
Elliott Hughese67f1f82015-04-30 17:32:03 -0700866std::string list_transports(bool long_listing) {
867 std::string result;
Josh Gao0cd3ae12016-09-21 12:37:10 -0700868
869 std::lock_guard<std::mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700870 for (const auto& t : transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700871 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800872 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700873 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874}
875
Josh Gao22d2b3e2016-10-27 14:01:08 -0700876void close_usb_devices(std::function<bool(const atransport*)> predicate) {
877 std::lock_guard<std::mutex> lock(transport_lock);
878 for (auto& t : transport_list) {
879 if (predicate(t)) {
880 t->Kick();
881 }
882 }
883}
884
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700886void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -0700887 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800888}
Josh Gao1290fbf2016-11-22 14:32:34 -0800889#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890
Josh Gao1290fbf2016-11-22 14:32:34 -0800891int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -0700892 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100893
894 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700895 char buf[32];
896 snprintf(buf, sizeof(buf), "T-%p", t);
897 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100898 }
Dan Albertc7915a32015-05-18 16:46:31 -0700899
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700900 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700901 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700902 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700903 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700905
Josh Gao0cd3ae12016-09-21 12:37:10 -0700906 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700907 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700908 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700909 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -0800910 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700911 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700912 return -1;
913 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800914 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700915
Elliott Hughes65fe2512015-10-07 15:59:35 -0700916 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700917 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700918 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -0800919 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700920 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700921 return -1;
922 }
923 }
924
Dan Albertc7915a32015-05-18 16:46:31 -0700925 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700926 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700927
928 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -0700929
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700931 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932}
933
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400934#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -0800935atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700936 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400937
Josh Gao0cd3ae12016-09-21 12:37:10 -0700938 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700939 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700940 if (t->serial && strcmp(serial, t->serial) == 0) {
941 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400942 break;
943 }
Dan Albertc7915a32015-05-18 16:46:31 -0700944 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400945
Dan Albertc7915a32015-05-18 16:46:31 -0700946 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400947}
948
Yabin Cuif4b99282015-08-27 12:03:11 -0700949void kick_all_tcp_devices() {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700950 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700951 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -0700952 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700953 // Kicking breaks the read_transport thread of this transport out of any read, then
954 // the read_transport thread will notify the main thread to make this transport
955 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -0700956 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -0700957 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400958 }
Dan Albertc7915a32015-05-18 16:46:31 -0700959 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400960}
961
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400962#endif
963
Josh Gao1290fbf2016-11-22 14:32:34 -0800964void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
965 unsigned writeable) {
Dan Albertc7915a32015-05-18 16:46:31 -0700966 atransport* t = new atransport();
967
Josh Gao1290fbf2016-11-22 14:32:34 -0800968 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Dan Albertdcd78a12015-05-18 16:43:57 -0700969 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
Josh Gao1290fbf2016-11-22 14:32:34 -0800970 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 t->serial = strdup(serial);
972 }
Dan Albertc7915a32015-05-18 16:46:31 -0700973
974 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -0700975 t->devpath = strdup(devpath);
976 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700977
Josh Gao0cd3ae12016-09-21 12:37:10 -0700978 {
979 std::lock_guard<std::mutex> lock(transport_lock);
980 pending_list.push_front(t);
981 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700982
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983 register_transport(t);
984}
985
Dan Albertdcd78a12015-05-18 16:43:57 -0700986// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -0800987void unregister_usb_transport(usb_handle* usb) {
Josh Gao0cd3ae12016-09-21 12:37:10 -0700988 std::lock_guard<std::mutex> lock(transport_lock);
Josh Gao1290fbf2016-11-22 14:32:34 -0800989 transport_list.remove_if(
990 [usb](atransport* t) { return t->usb == usb && t->connection_state == kCsNoPerm; });
Mike Lockwood0927bf92009-08-08 12:37:44 -0400991}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800992
Josh Gao1290fbf2016-11-22 14:32:34 -0800993int check_header(apacket* p, atransport* t) {
994 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuiaed3c612015-09-22 15:52:57 -0700995 VLOG(RWX) << "check_header(): invalid magic";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 return -1;
997 }
998
Josh Gao1290fbf2016-11-22 14:32:34 -0800999 if (p->msg.data_length > t->get_max_payload()) {
1000 VLOG(RWX) << "check_header(): " << p->msg.data_length
1001 << " atransport::max_payload = " << t->get_max_payload();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001002 return -1;
1003 }
1004
1005 return 0;
1006}
1007
Josh Gao06d61d42016-10-06 13:31:44 -07001008int check_data(apacket* p) {
1009 if (calculate_apacket_checksum(p) != p->msg.data_check) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001011 }
Josh Gao06d61d42016-10-06 13:31:44 -07001012 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013}
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001014
Josh Gao3bd28792016-10-05 19:02:29 -07001015#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001016std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001017 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1018
Josh Gao2e671202016-08-18 22:00:12 -07001019 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001020 keys_.pop_front();
1021 return result;
1022}
Josh Gao3bd28792016-10-05 19:02:29 -07001023#endif