blob: 14888ab7036aa01e2a4bf5dfd89ef1c0520887b6 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertb302d122015-02-24 15:51:19 -080018
Dan Albertdb6fe642015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert4895c522015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob39e4152017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029
Spencer Low28bc2cb2015-11-07 18:51:54 -080030#include <algorithm>
Dan Albertecce5032015-05-18 16:46:31 -070031#include <list>
Josh Gaoe7daf572016-09-21 12:37:10 -070032#include <mutex>
Josh Gao0f3312a2017-04-12 17:00:49 -070033#include <thread>
Dan Albertecce5032015-05-18 16:46:31 -070034
Elliott Hughesf55ead92015-12-04 22:00:26 -080035#include <android-base/logging.h>
David Pursellc929c6f2016-03-01 08:58:26 -080036#include <android-base/parsenetaddress.h>
Yabin Cui3cf1b362017-03-10 16:01:01 -080037#include <android-base/quick_exit.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080038#include <android-base/stringprintf.h>
39#include <android-base/strings.h>
Josh Gaob39e4152017-08-16 16:57:01 -070040#include <android-base/thread_annotations.h>
Elliott Hughes88b4c852015-04-30 17:32:03 -070041
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#include "adb.h"
Elliott Hughes801066a2016-06-29 17:42:01 -070043#include "adb_auth.h"
Josh Gao395b86a2018-01-28 20:32:46 -080044#include "adb_io.h"
Josh Gao9e09a972016-11-29 09:40:29 -080045#include "adb_trace.h"
Elliott Hughes88b4c852015-04-30 17:32:03 -070046#include "adb_utils.h"
Elliott Hughes70097492015-12-11 19:07:01 -080047#include "diagnose_usb.h"
Yabin Cui3cf1b362017-03-10 16:01:01 -080048#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049
50static void transport_unref(atransport *t);
51
Josh Gaob39e4152017-08-16 16:57:01 -070052// TODO: unordered_map<TransportId, atransport*>
Josh Gaoe3a87d02015-11-11 17:56:12 -080053static auto& transport_list = *new std::list<atransport*>();
54static auto& pending_list = *new std::list<atransport*>();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -070055
Josh Gaoaaa82f72017-08-17 13:50:51 -070056static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057
Todd Kennedyaff9c672015-11-10 00:03:25 +000058const char* const kFeatureShell2 = "shell_v2";
59const char* const kFeatureCmd = "cmd";
Josh Gaoa2cf3752016-12-05 17:11:34 -080060const char* const kFeatureStat2 = "stat_v2";
Josh Gao210b63f2017-02-22 17:07:01 -080061const char* const kFeatureLibusb = "libusb";
Dan Albert27983bc2017-05-23 14:30:00 -070062const char* const kFeaturePushSync = "push_sync";
Todd Kennedyaff9c672015-11-10 00:03:25 +000063
Josh Gaob39e4152017-08-16 16:57:01 -070064TransportId NextTransportId() {
65 static std::atomic<TransportId> next(1);
66 return next++;
67}
68
Josh Gao395b86a2018-01-28 20:32:46 -080069bool FdConnection::Read(apacket* packet) {
70 if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) {
71 D("remote local: read terminated (message)");
72 return false;
73 }
74
Josh Gao839b9322018-02-05 18:49:10 -080075 if (packet->msg.data_length > MAX_PAYLOAD) {
Josh Gaob14756a2018-02-02 14:38:04 -080076 D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
77 return false;
78 }
79
Josh Gao839b9322018-02-05 18:49:10 -080080 packet->payload.resize(packet->msg.data_length);
81
82 if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) {
Josh Gao395b86a2018-01-28 20:32:46 -080083 D("remote local: terminated (data)");
84 return false;
85 }
86
87 return true;
88}
89
90bool FdConnection::Write(apacket* packet) {
Josh Gao839b9322018-02-05 18:49:10 -080091 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) {
Josh Gao395b86a2018-01-28 20:32:46 -080092 D("remote local: write terminated");
93 return false;
94 }
95
Josh Gao839b9322018-02-05 18:49:10 -080096 if (packet->msg.data_length) {
97 if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) {
98 D("remote local: write terminated");
99 return false;
100 }
101 }
102
Josh Gao395b86a2018-01-28 20:32:46 -0800103 return true;
104}
105
106void FdConnection::Close() {
107 adb_shutdown(fd_.get());
108 fd_.reset();
109}
110
Yabin Cui19bec5b2015-09-22 15:52:57 -0700111static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800112 unsigned command = p->msg.command;
113 int len = p->msg.data_length;
114 char cmd[9];
115 char arg0[12], arg1[12];
116 int n;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100117
118 for (n = 0; n < 4; n++) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800119 int b = (command >> (n * 8)) & 255;
120 if (b < 32 || b >= 127) break;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100121 cmd[n] = (char)b;
122 }
123 if (n == 4) {
124 cmd[4] = 0;
125 } else {
126 /* There is some non-ASCII name in the command, so dump
127 * the hexadecimal value instead */
128 snprintf(cmd, sizeof cmd, "%08x", command);
129 }
130
131 if (p->msg.arg0 < 256U)
132 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
133 else
134 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
135
136 if (p->msg.arg1 < 256U)
137 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
138 else
139 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
140
Josh Gao0d6aa992016-11-22 14:32:34 -0800141 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
142 func, cmd, arg0, arg1, len);
Josh Gao839b9322018-02-05 18:49:10 -0800143 result += dump_hex(p->payload.data(), p->payload.size());
Yabin Cui19bec5b2015-09-22 15:52:57 -0700144 return result;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100145}
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100146
Josh Gao0d6aa992016-11-22 14:32:34 -0800147static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800148 ATRACE_NAME("read_packet");
Yabin Cuia96dabf2015-07-30 19:58:10 -0700149 char buff[8];
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100150 if (!name) {
151 snprintf(buff, sizeof buff, "fd=%d", fd);
152 name = buff;
153 }
Josh Gao0d6aa992016-11-22 14:32:34 -0800154 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700155 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800156 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700157 int r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800158 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800159 len -= r;
Yabin Cuia96dabf2015-07-30 19:58:10 -0700160 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700162 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800163 return -1;
164 }
165 }
166
Yabin Cui19bec5b2015-09-22 15:52:57 -0700167 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800168 return 0;
169}
170
Josh Gao0d6aa992016-11-22 14:32:34 -0800171static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gao9e09a972016-11-29 09:40:29 -0800172 ATRACE_NAME("write_packet");
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100173 char buff[8];
174 if (!name) {
175 snprintf(buff, sizeof buff, "fd=%d", fd);
176 name = buff;
177 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700178 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao0d6aa992016-11-22 14:32:34 -0800179 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cuia96dabf2015-07-30 19:58:10 -0700180 int len = sizeof(apacket*);
Josh Gao0d6aa992016-11-22 14:32:34 -0800181 while (len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700182 int r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800183 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184 len -= r;
185 p += r;
186 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700187 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188 return -1;
189 }
190 }
191 return 0;
192}
193
Josh Gao0d6aa992016-11-22 14:32:34 -0800194static void transport_socket_events(int fd, unsigned events, void* _t) {
195 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui815ad882015-09-02 17:44:28 -0700196 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao0d6aa992016-11-22 14:32:34 -0800197 if (events & FDE_READ) {
198 apacket* p = 0;
199 if (read_packet(fd, t->serial, &p)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700200 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
Josh Gao839b9322018-02-05 18:49:10 -0800201 return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202 }
Josh Gao839b9322018-02-05 18:49:10 -0800203
204 handle_packet(p, (atransport*)_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205 }
206}
207
Josh Gao67ac3792016-10-06 13:31:44 -0700208void send_packet(apacket* p, atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800209 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayee7b44d2017-12-07 11:40:00 -0800210 // compute a checksum for connection/auth packets for compatibility reasons
211 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
212 p->msg.data_check = 0;
213 } else {
214 p->msg.data_check = calculate_apacket_checksum(p);
215 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800216
217 print_packet("send", p);
218
219 if (t == NULL) {
Josh Gao67ac3792016-10-06 13:31:44 -0700220 fatal("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800221 }
222
Josh Gao67ac3792016-10-06 13:31:44 -0700223 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800224 fatal_errno("cannot enqueue packet on transport socket");
225 }
226}
227
Yabin Cui4e222292015-08-31 11:50:24 -0700228// The transport is opened by transport_register_func before
229// the read_transport and write_transport threads are started.
230//
231// The read_transport thread issues a SYNC(1, token) message to let
232// the write_transport thread know to start things up. In the event
233// of transport IO failure, the read_transport thread will post a
234// SYNC(0,0) message to ensure shutdown.
235//
236// The transport will not actually be closed until both threads exit, but the threads
237// will kick the transport on their way out to disconnect the underlying device.
238//
239// read_transport thread reads data from a transport (representing a usb/tcp connection),
240// and makes the main thread call handle_packet().
Josh Gao7d405252016-02-12 14:31:15 -0800241static void read_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800242 atransport* t = reinterpret_cast<atransport*>(_t);
243 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244
Josh Gao0d6aa992016-11-22 14:32:34 -0800245 adb_thread_setname(
246 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
247 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
248 t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 p = get_apacket();
250 p->msg.command = A_SYNC;
251 p->msg.arg0 = 1;
252 p->msg.arg1 = ++(t->sync_token);
253 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao839b9322018-02-05 18:49:10 -0800254 D("sending SYNC packet (len = %u, payload.size() = %zu)", p->msg.data_length, p->payload.size());
Josh Gao0d6aa992016-11-22 14:32:34 -0800255 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700257 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800258 goto oops;
259 }
260
Yabin Cui815ad882015-09-02 17:44:28 -0700261 D("%s: data pump started", t->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800262 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800263 ATRACE_NAME("read_transport loop");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800264 p = get_apacket();
265
Josh Gao9e09a972016-11-29 09:40:29 -0800266 {
267 ATRACE_NAME("read_transport read_remote");
Josh Gao395b86a2018-01-28 20:32:46 -0800268 if (!t->connection->Read(p)) {
Josh Gao9e09a972016-11-29 09:40:29 -0800269 D("%s: remote read failed for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800270 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800271 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272 }
Josh Gao395b86a2018-01-28 20:32:46 -0800273
274 if (!check_header(p, t)) {
275 D("%s: remote read: bad header", t->serial);
276 put_apacket(p);
277 break;
278 }
279
Yabin Cui3cf1b362017-03-10 16:01:01 -0800280#if ADB_HOST
281 if (p->msg.command == 0) {
Josh Gao30c42662018-01-29 18:09:20 -0800282 put_apacket(p);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800283 continue;
284 }
285#endif
Josh Gao9e09a972016-11-29 09:40:29 -0800286 }
287
288 D("%s: received remote packet, sending to transport", t->serial);
289 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800290 put_apacket(p);
Josh Gao9e09a972016-11-29 09:40:29 -0800291 D("%s: failed to write apacket to transport", t->serial);
292 goto oops;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 }
294 }
295
Yabin Cui815ad882015-09-02 17:44:28 -0700296 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297 p = get_apacket();
298 p->msg.command = A_SYNC;
299 p->msg.arg0 = 0;
300 p->msg.arg1 = 0;
301 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao0d6aa992016-11-22 14:32:34 -0800302 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700304 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800305 }
306
307oops:
Yabin Cui815ad882015-09-02 17:44:28 -0700308 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800309 kick_transport(t);
310 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311}
312
Yabin Cui4e222292015-08-31 11:50:24 -0700313// write_transport thread gets packets sent by the main thread (through send_packet()),
314// and writes to a transport (representing a usb/tcp connection).
Josh Gao7d405252016-02-12 14:31:15 -0800315static void write_transport_thread(void* _t) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800316 atransport* t = reinterpret_cast<atransport*>(_t);
317 apacket* p;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 int active = 0;
319
Josh Gao0d6aa992016-11-22 14:32:34 -0800320 adb_thread_setname(
321 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
322 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323
Josh Gao0d6aa992016-11-22 14:32:34 -0800324 for (;;) {
Josh Gao9e09a972016-11-29 09:40:29 -0800325 ATRACE_NAME("write_transport loop");
Josh Gao0d6aa992016-11-22 14:32:34 -0800326 if (read_packet(t->fd, t->serial, &p)) {
327 D("%s: failed to read apacket from transport on fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328 break;
329 }
Josh Gao9e09a972016-11-29 09:40:29 -0800330
Josh Gao0d6aa992016-11-22 14:32:34 -0800331 if (p->msg.command == A_SYNC) {
332 if (p->msg.arg0 == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700333 D("%s: transport SYNC offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800334 put_apacket(p);
335 break;
336 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800337 if (p->msg.arg1 == t->sync_token) {
Yabin Cui815ad882015-09-02 17:44:28 -0700338 D("%s: transport SYNC online", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339 active = 1;
340 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800341 D("%s: transport ignoring SYNC %d != %d", t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 }
343 }
344 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -0800345 if (active) {
Yabin Cui815ad882015-09-02 17:44:28 -0700346 D("%s: transport got packet, sending to remote", t->serial);
Josh Gao9e09a972016-11-29 09:40:29 -0800347 ATRACE_NAME("write_transport write_remote");
Josh Gao839b9322018-02-05 18:49:10 -0800348
349 // Allow sending the payload's implicit null terminator.
350 if (p->msg.data_length != p->payload.size()) {
351 LOG(FATAL) << "packet data length doesn't match payload: msg.data_length = "
352 << p->msg.data_length << ", payload.size() = " << p->payload.size();
353 }
354
Yabin Cui3cf1b362017-03-10 16:01:01 -0800355 if (t->Write(p) != 0) {
356 D("%s: remote write failed for transport", t->serial);
357 put_apacket(p);
358 break;
359 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800360 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700361 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800362 }
363 }
364
365 put_apacket(p);
366 }
367
Yabin Cui815ad882015-09-02 17:44:28 -0700368 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 kick_transport(t);
370 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371}
372
Yabin Cui4d64fd82015-08-27 12:03:11 -0700373void kick_transport(atransport* t) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700374 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuid78ed222016-04-05 13:50:44 -0700375 // As kick_transport() can be called from threads without guarantee that t is valid,
376 // check if the transport is in transport_list first.
Josh Gaob39e4152017-08-16 16:57:01 -0700377 //
378 // TODO(jmgao): WTF? Is this actually true?
Yabin Cuid78ed222016-04-05 13:50:44 -0700379 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700380 t->Kick();
Yabin Cuid78ed222016-04-05 13:50:44 -0700381 }
Yabin Cui4d64fd82015-08-27 12:03:11 -0700382}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383
384static int transport_registration_send = -1;
385static int transport_registration_recv = -1;
386static fdevent transport_registration_fde;
387
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389
390/* this adds support required by the 'track-devices' service.
391 * this is used to send the content of "list_transport" to any
392 * number of client connections that want it through a single
393 * live TCP connection
394 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800395struct device_tracker {
Josh Gao0d6aa992016-11-22 14:32:34 -0800396 asocket socket;
Josh Gao5cb76ce2018-02-12 17:24:00 -0800397 bool update_needed = false;
398 bool long_output = false;
399 device_tracker* next = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400};
401
402/* linked list of all device trackers */
Josh Gao0d6aa992016-11-22 14:32:34 -0800403static device_tracker* device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404
Josh Gao0d6aa992016-11-22 14:32:34 -0800405static void device_tracker_remove(device_tracker* tracker) {
406 device_tracker** pnode = &device_tracker_list;
407 device_tracker* node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408
Josh Gaoaaa82f72017-08-17 13:50:51 -0700409 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800410 while (node) {
411 if (node == tracker) {
412 *pnode = node->next;
413 break;
414 }
415 pnode = &node->next;
Josh Gao0d6aa992016-11-22 14:32:34 -0800416 node = *pnode;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418}
419
Josh Gao0d6aa992016-11-22 14:32:34 -0800420static void device_tracker_close(asocket* socket) {
421 device_tracker* tracker = (device_tracker*)socket;
422 asocket* peer = socket->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423
Josh Gao0d6aa992016-11-22 14:32:34 -0800424 D("device tracker %p removed", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800425 if (peer) {
426 peer->peer = NULL;
427 peer->close(peer);
428 }
429 device_tracker_remove(tracker);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800430 delete tracker;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800431}
432
Josh Gaoa7d9d712018-02-01 13:17:50 -0800433static int device_tracker_enqueue(asocket* socket, std::string) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800434 /* you can't read from a device tracker, close immediately */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800435 device_tracker_close(socket);
436 return -1;
437}
438
Elliott Hughes88b4c852015-04-30 17:32:03 -0700439static int device_tracker_send(device_tracker* tracker, const std::string& string) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700440 asocket* peer = tracker->socket.peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441
Josh Gaoa7d9d712018-02-01 13:17:50 -0800442 std::string data;
443 data.resize(4 + string.size());
444 char buf[5];
445 snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
446 memcpy(&data[0], buf, 4);
447 memcpy(&data[4], string.data(), string.size());
448 return peer->enqueue(peer, std::move(data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800449}
450
Elliott Hughes88b4c852015-04-30 17:32:03 -0700451static void device_tracker_ready(asocket* socket) {
452 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453
Elliott Hughes88b4c852015-04-30 17:32:03 -0700454 // We want to send the device list when the tracker connects
455 // for the first time, even if no update occurred.
Josh Gao32124632017-08-14 18:57:54 -0700456 if (tracker->update_needed) {
457 tracker->update_needed = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458
Josh Gao32124632017-08-14 18:57:54 -0700459 std::string transports = list_transports(tracker->long_output);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700460 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800461 }
462}
463
Josh Gao32124632017-08-14 18:57:54 -0700464asocket* create_device_tracker(bool long_output) {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800465 device_tracker* tracker = new device_tracker();
Elliott Hughesd0269c92015-04-21 19:39:52 -0700466 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800467
Josh Gao0d6aa992016-11-22 14:32:34 -0800468 D("device tracker %p created", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800469
470 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao0d6aa992016-11-22 14:32:34 -0800471 tracker->socket.ready = device_tracker_ready;
472 tracker->socket.close = device_tracker_close;
Josh Gao32124632017-08-14 18:57:54 -0700473 tracker->update_needed = true;
474 tracker->long_output = long_output;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800475
Josh Gao0d6aa992016-11-22 14:32:34 -0800476 tracker->next = device_tracker_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800477 device_tracker_list = tracker;
478
479 return &tracker->socket;
480}
481
Josh Gao1e3bf732017-05-03 22:37:10 -0700482// Check if all of the USB transports are connected.
483bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700484 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao1e3bf732017-05-03 22:37:10 -0700485 for (const auto& t : transport_list) {
486 if (!fn(t)) {
487 return false;
488 }
489 }
490 for (const auto& t : pending_list) {
491 if (!fn(t)) {
492 return false;
493 }
494 }
495 return true;
496}
497
Elliott Hughes88b4c852015-04-30 17:32:03 -0700498// Call this function each time the transport list has changed.
499void update_transports() {
Josh Gao1e3bf732017-05-03 22:37:10 -0700500 update_transport_status();
501
502 // Notify `adb track-devices` clients.
Elliott Hughes88b4c852015-04-30 17:32:03 -0700503 std::string transports = list_transports(false);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800504
Elliott Hughes88b4c852015-04-30 17:32:03 -0700505 device_tracker* tracker = device_tracker_list;
506 while (tracker != nullptr) {
507 device_tracker* next = tracker->next;
508 // This may destroy the tracker if the connection is closed.
509 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800510 tracker = next;
511 }
512}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700513
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800514#else
Elliott Hughes88b4c852015-04-30 17:32:03 -0700515
516void update_transports() {
517 // Nothing to do on the device side.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700519
Josh Gao0d6aa992016-11-22 14:32:34 -0800520#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800521
Josh Gao0d6aa992016-11-22 14:32:34 -0800522struct tmsg {
523 atransport* transport;
524 int action;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525};
526
Josh Gao0d6aa992016-11-22 14:32:34 -0800527static int transport_read_action(int fd, struct tmsg* m) {
528 char* p = (char*)m;
529 int len = sizeof(*m);
530 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800531
Josh Gao0d6aa992016-11-22 14:32:34 -0800532 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800533 r = adb_read(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800534 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800535 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800536 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800537 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700538 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539 return -1;
540 }
541 }
542 return 0;
543}
544
Josh Gao0d6aa992016-11-22 14:32:34 -0800545static int transport_write_action(int fd, struct tmsg* m) {
546 char* p = (char*)m;
547 int len = sizeof(*m);
548 int r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800549
Josh Gao0d6aa992016-11-22 14:32:34 -0800550 while (len > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800551 r = adb_write(fd, p, len);
Josh Gao0d6aa992016-11-22 14:32:34 -0800552 if (r > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800553 len -= r;
Josh Gao0d6aa992016-11-22 14:32:34 -0800554 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800555 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700556 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800557 return -1;
558 }
559 }
560 return 0;
561}
562
Josh Gao0d6aa992016-11-22 14:32:34 -0800563static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800564 tmsg m;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800565 int s[2];
Josh Gao0d6aa992016-11-22 14:32:34 -0800566 atransport* t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800567
Josh Gao0d6aa992016-11-22 14:32:34 -0800568 if (!(ev & FDE_READ)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800569 return;
570 }
571
Josh Gao0d6aa992016-11-22 14:32:34 -0800572 if (transport_read_action(_fd, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800573 fatal_errno("cannot read transport registration socket");
574 }
575
576 t = m.transport;
577
Dan Albertbe8e54b2015-05-18 13:06:53 -0700578 if (m.action == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700579 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800580
Josh Gao0d6aa992016-11-22 14:32:34 -0800581 /* IMPORTANT: the remove closes one half of the
582 ** socket pair. The close closes the other half.
583 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800584 fdevent_remove(&(t->transport_fde));
585 adb_close(t->fd);
586
Josh Gaoe7daf572016-09-21 12:37:10 -0700587 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700588 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700589 transport_list.remove(t);
590 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800591
Josh Gao0d6aa992016-11-22 14:32:34 -0800592 if (t->product) free(t->product);
593 if (t->serial) free(t->serial);
594 if (t->model) free(t->model);
595 if (t->device) free(t->device);
596 if (t->devpath) free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800597
Dan Albertecce5032015-05-18 16:46:31 -0700598 delete t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800599
600 update_transports();
601 return;
602 }
603
Mike Lockwoode45583f2009-08-08 12:37:44 -0400604 /* don't create transport threads for inaccessible devices */
Yabin Cui3cf1b362017-03-10 16:01:01 -0800605 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800606 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400607 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800608
Dan Albertecce5032015-05-18 16:46:31 -0700609 if (adb_socketpair(s)) {
Mike Lockwoode45583f2009-08-08 12:37:44 -0400610 fatal_errno("cannot open transport socketpair");
611 }
612
Yabin Cui815ad882015-09-02 17:44:28 -0700613 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400614
615 t->transport_socket = s[0];
616 t->fd = s[1];
617
Josh Gao0d6aa992016-11-22 14:32:34 -0800618 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400619
620 fdevent_set(&(t->transport_fde), FDE_READ);
621
Josh Gao0f3312a2017-04-12 17:00:49 -0700622 std::thread(write_transport_thread, t).detach();
623 std::thread(read_transport_thread, t).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800624 }
625
Josh Gaoe7daf572016-09-21 12:37:10 -0700626 {
Josh Gaoaaa82f72017-08-17 13:50:51 -0700627 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -0700628 pending_list.remove(t);
629 transport_list.push_front(t);
630 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800631
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800632 update_transports();
633}
634
Josh Gao0d6aa992016-11-22 14:32:34 -0800635void init_transport_registration(void) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800636 int s[2];
637
Josh Gao0d6aa992016-11-22 14:32:34 -0800638 if (adb_socketpair(s)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800639 fatal_errno("cannot open transport registration socketpair");
640 }
Yabin Cui815ad882015-09-02 17:44:28 -0700641 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800642
643 transport_registration_send = s[0];
644 transport_registration_recv = s[1];
645
Josh Gao0d6aa992016-11-22 14:32:34 -0800646 fdevent_install(&transport_registration_fde, transport_registration_recv,
647 transport_registration_func, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800648
649 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao165460f2017-05-09 13:43:35 -0700650}
651
652void kick_all_transports() {
653 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gaoaaa82f72017-08-17 13:50:51 -0700654 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao165460f2017-05-09 13:43:35 -0700655 for (auto t : transport_list) {
656 t->Kick();
657 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800658}
659
660/* the fdevent select pump is single threaded */
Josh Gao0d6aa992016-11-22 14:32:34 -0800661static void register_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800662 tmsg m;
663 m.transport = transport;
664 m.action = 1;
Yabin Cui815ad882015-09-02 17:44:28 -0700665 D("transport: %s registered", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800666 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800667 fatal_errno("cannot write transport registration socket\n");
668 }
669}
670
Josh Gao0d6aa992016-11-22 14:32:34 -0800671static void remove_transport(atransport* transport) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672 tmsg m;
673 m.transport = transport;
674 m.action = 0;
Yabin Cui815ad882015-09-02 17:44:28 -0700675 D("transport: %s removed", transport->serial);
Josh Gao0d6aa992016-11-22 14:32:34 -0800676 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677 fatal_errno("cannot write transport registration socket\n");
678 }
679}
680
Yabin Cui4d64fd82015-08-27 12:03:11 -0700681static void transport_unref(atransport* t) {
682 CHECK(t != nullptr);
Josh Gaoe7daf572016-09-21 12:37:10 -0700683
Josh Gao4d299742017-09-13 13:40:57 -0700684 std::lock_guard<std::recursive_mutex> lock(transport_lock);
685 CHECK_GT(t->ref_count, 0u);
686 t->ref_count--;
687 if (t->ref_count == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700688 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gao395b86a2018-01-28 20:32:46 -0800689 t->connection->Close();
Mike Lockwood01c2c302010-05-24 10:44:35 -0400690 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100691 } else {
Josh Gao4d299742017-09-13 13:40:57 -0700692 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400693 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800694}
695
Josh Gao0d6aa992016-11-22 14:32:34 -0800696static int qual_match(const char* to_test, const char* prefix, const char* qual,
697 bool sanitize_qual) {
698 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson27042382012-05-30 18:11:27 -0700699 return !qual || !*qual;
700
Josh Gao0d6aa992016-11-22 14:32:34 -0800701 if (!qual) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700702
703 if (prefix) {
704 while (*prefix) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800705 if (*prefix++ != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700706 }
707 }
708
709 while (*qual) {
710 char ch = *qual++;
Josh Gao0d6aa992016-11-22 14:32:34 -0800711 if (sanitize_qual && !isalnum(ch)) ch = '_';
712 if (ch != *to_test++) return 0;
Scott Anderson27042382012-05-30 18:11:27 -0700713 }
714
715 /* Everything matched so far. Return true if *to_test is a NUL. */
716 return !*to_test;
717}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800718
Josh Gaob39e4152017-08-16 16:57:01 -0700719atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
720 bool* is_ambiguous, std::string* error_out,
721 bool accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700722 atransport* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800723
Josh Gaob39e4152017-08-16 16:57:01 -0700724 if (transport_id != 0) {
725 *error_out =
726 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
727 } else if (serial) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700728 *error_out = android::base::StringPrintf("device '%s' not found", serial);
729 } else if (type == kTransportLocal) {
730 *error_out = "no emulators found";
731 } else if (type == kTransportAny) {
732 *error_out = "no devices/emulators found";
733 } else {
734 *error_out = "no devices found";
735 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800736
Josh Gaoaaa82f72017-08-17 13:50:51 -0700737 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes67943d12015-10-07 14:55:10 -0700738 for (const auto& t : transport_list) {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800739 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes70097492015-12-11 19:07:01 -0800740#if ADB_HOST
David Pursell6e5c7eb2015-12-02 15:14:31 -0800741 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes70097492015-12-11 19:07:01 -0800742#endif
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400743 continue;
744 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400745
Josh Gaob39e4152017-08-16 16:57:01 -0700746 if (transport_id) {
747 if (t->id == transport_id) {
748 result = t;
749 break;
750 }
751 } else if (serial) {
David Pursellc929c6f2016-03-01 08:58:26 -0800752 if (t->MatchesTarget(serial)) {
Scott Anderson27042382012-05-30 18:11:27 -0700753 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700754 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700755 if (is_ambiguous) *is_ambiguous = true;
756 result = nullptr;
Scott Anderson27042382012-05-30 18:11:27 -0700757 break;
758 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800759 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700760 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800761 } else {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700762 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800763 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700764 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700765 if (is_ambiguous) *is_ambiguous = true;
766 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800767 break;
768 }
769 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700770 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800771 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700772 *error_out = "more than one emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700773 if (is_ambiguous) *is_ambiguous = true;
774 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800775 break;
776 }
777 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700778 } else if (type == kTransportAny) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800779 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700780 *error_out = "more than one device/emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700781 if (is_ambiguous) *is_ambiguous = true;
782 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800783 break;
784 }
785 result = t;
786 }
787 }
788 }
Josh Gaoe7daf572016-09-21 12:37:10 -0700789 lock.unlock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800790
Elliott Hughes67943d12015-10-07 14:55:10 -0700791 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800792 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700793 *error_out = "device unauthorized.\n";
794 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
795 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
796 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
797 *error_out += "\n";
798 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
799 *error_out += "Otherwise check for a confirmation dialog on your device.";
800 result = nullptr;
801 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700802
Elliott Hughes67943d12015-10-07 14:55:10 -0700803 // Don't return offline devices; the caller can't do anything with them.
Yabin Cui3cf1b362017-03-10 16:01:01 -0800804 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes67943d12015-10-07 14:55:10 -0700805 *error_out = "device offline";
806 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800807 }
808
809 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700810 *error_out = "success";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800811 }
812
813 return result;
814}
815
Yabin Cui3cf1b362017-03-10 16:01:01 -0800816int atransport::Write(apacket* p) {
Josh Gao395b86a2018-01-28 20:32:46 -0800817 return this->connection->Write(p) ? 0 : -1;
Yabin Cui3cf1b362017-03-10 16:01:01 -0800818}
819
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700820void atransport::Kick() {
821 if (!kicked_) {
Josh Gao395b86a2018-01-28 20:32:46 -0800822 D("kicking transport %s", this->serial);
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700823 kicked_ = true;
Josh Gao395b86a2018-01-28 20:32:46 -0800824 this->connection->Close();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700825 }
826}
827
Yabin Cui3cf1b362017-03-10 16:01:01 -0800828ConnectionState atransport::GetConnectionState() const {
829 return connection_state_;
830}
831
832void atransport::SetConnectionState(ConnectionState state) {
833 check_main_thread();
834 connection_state_ = state;
835}
836
David Pursell6e5c7eb2015-12-02 15:14:31 -0800837const std::string atransport::connection_state_name() const {
Yabin Cui3cf1b362017-03-10 16:01:01 -0800838 ConnectionState state = GetConnectionState();
839 switch (state) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800840 case kCsOffline:
841 return "offline";
842 case kCsBootloader:
843 return "bootloader";
844 case kCsDevice:
845 return "device";
846 case kCsHost:
847 return "host";
848 case kCsRecovery:
849 return "recovery";
850 case kCsNoPerm:
851 return UsbNoPermissionsShortHelpText();
852 case kCsSideload:
853 return "sideload";
854 case kCsUnauthorized:
855 return "unauthorized";
856 default:
857 return "unknown";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800858 }
859}
860
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100861void atransport::update_version(int version, size_t payload) {
862 protocol_version = std::min(version, A_VERSION);
863 max_payload = std::min(payload, MAX_PAYLOAD);
864}
865
866int atransport::get_protocol_version() const {
867 return protocol_version;
868}
869
870size_t atransport::get_max_payload() const {
871 return max_payload;
872}
873
David Pursella07dbad2015-09-22 10:43:08 -0700874namespace {
David Pursell8da19a42015-08-31 10:42:13 -0700875
David Pursella07dbad2015-09-22 10:43:08 -0700876constexpr char kFeatureStringDelimiter = ',';
877
878} // namespace
Dan Albertbe8e54b2015-05-18 13:06:53 -0700879
880const FeatureSet& supported_features() {
David Pursella07dbad2015-09-22 10:43:08 -0700881 // Local static allocation to avoid global non-POD variables.
882 static const FeatureSet* features = new FeatureSet{
Josh Gao0d6aa992016-11-22 14:32:34 -0800883 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursell6d5fad32015-09-25 08:37:13 -0700884 // Increment ADB_SERVER_VERSION whenever the feature list changes to
885 // make sure that the adb client and server features stay in sync
886 // (http://b/24370690).
David Pursella07dbad2015-09-22 10:43:08 -0700887 };
888
889 return *features;
890}
891
892std::string FeatureSetToString(const FeatureSet& features) {
893 return android::base::Join(features, kFeatureStringDelimiter);
894}
895
896FeatureSet StringToFeatureSet(const std::string& features_string) {
David Pursell3d9072b2015-09-25 13:04:21 -0700897 if (features_string.empty()) {
898 return FeatureSet();
899 }
900
Josh Gao0d6aa992016-11-22 14:32:34 -0800901 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursella07dbad2015-09-22 10:43:08 -0700902 return FeatureSet(names.begin(), names.end());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700903}
904
David Pursell22fc5e92015-09-30 13:35:42 -0700905bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao0d6aa992016-11-22 14:32:34 -0800906 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell22fc5e92015-09-30 13:35:42 -0700907}
908
Dan Albertbe8e54b2015-05-18 13:06:53 -0700909bool atransport::has_feature(const std::string& feature) const {
910 return features_.count(feature) > 0;
911}
912
David Pursella07dbad2015-09-22 10:43:08 -0700913void atransport::SetFeatures(const std::string& features_string) {
914 features_ = StringToFeatureSet(features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700915}
916
Yabin Cui2d4c1982015-08-28 15:09:44 -0700917void atransport::AddDisconnect(adisconnect* disconnect) {
918 disconnects_.push_back(disconnect);
919}
920
921void atransport::RemoveDisconnect(adisconnect* disconnect) {
922 disconnects_.remove(disconnect);
923}
924
925void atransport::RunDisconnects() {
Elliott Hughes85952832015-10-07 15:59:35 -0700926 for (const auto& disconnect : disconnects_) {
Yabin Cui2d4c1982015-08-28 15:09:44 -0700927 disconnect->func(disconnect->opaque, this);
928 }
929 disconnects_.clear();
930}
931
David Pursellc929c6f2016-03-01 08:58:26 -0800932bool atransport::MatchesTarget(const std::string& target) const {
933 if (serial) {
934 if (target == serial) {
935 return true;
936 } else if (type == kTransportLocal) {
937 // Local transports can match [tcp:|udp:]<hostname>[:port].
938 const char* local_target_ptr = target.c_str();
939
940 // For fastboot compatibility, ignore protocol prefixes.
941 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao0d6aa992016-11-22 14:32:34 -0800942 android::base::StartsWith(target, "udp:")) {
David Pursellc929c6f2016-03-01 08:58:26 -0800943 local_target_ptr += 4;
944 }
945
946 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
947 std::string serial_host, error;
948 int serial_port = -1;
Josh Gao0d6aa992016-11-22 14:32:34 -0800949 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursellc929c6f2016-03-01 08:58:26 -0800950 // |target| may omit the port to default to ours.
951 std::string target_host;
952 int target_port = serial_port;
953 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
954 nullptr, &error) &&
Josh Gao0d6aa992016-11-22 14:32:34 -0800955 serial_host == target_host && serial_port == target_port) {
David Pursellc929c6f2016-03-01 08:58:26 -0800956 return true;
957 }
958 }
959 }
960 }
961
962 return (devpath && target == devpath) ||
963 qual_match(target.c_str(), "product:", product, false) ||
964 qual_match(target.c_str(), "model:", model, true) ||
965 qual_match(target.c_str(), "device:", device, false);
966}
967
Elliott Hughes88b4c852015-04-30 17:32:03 -0700968#if ADB_HOST
969
Josh Gaob39e4152017-08-16 16:57:01 -0700970// We use newline as our delimiter, make sure to never output it.
971static std::string sanitize(std::string str, bool alphanumeric) {
972 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
973 : [](const char c) { return c == '\n'; };
974 std::replace_if(str.begin(), str.end(), pred, '_');
975 return str;
976}
977
Josh Gao0d6aa992016-11-22 14:32:34 -0800978static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob39e4152017-08-16 16:57:01 -0700979 bool alphanumeric) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700980 if (value == nullptr || *value == '\0') {
Scott Anderson27042382012-05-30 18:11:27 -0700981 return;
Scott Anderson27042382012-05-30 18:11:27 -0700982 }
983
Elliott Hughes88b4c852015-04-30 17:32:03 -0700984 *result += ' ';
985 *result += key;
Josh Gaob39e4152017-08-16 16:57:01 -0700986 *result += sanitize(value, alphanumeric);
Scott Anderson27042382012-05-30 18:11:27 -0700987}
988
Josh Gao0d6aa992016-11-22 14:32:34 -0800989static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson27042382012-05-30 18:11:27 -0700990 const char* serial = t->serial;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700991 if (!serial || !serial[0]) {
Dan Alberta8c34142015-05-06 16:48:52 -0700992 serial = "(no serial number)";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700993 }
Scott Anderson27042382012-05-30 18:11:27 -0700994
995 if (!long_listing) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700996 *result += serial;
997 *result += '\t';
998 *result += t->connection_state_name();
Scott Anderson27042382012-05-30 18:11:27 -0700999 } else {
Josh Gao0d6aa992016-11-22 14:32:34 -08001000 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson27042382012-05-30 18:11:27 -07001001
Elliott Hughes88b4c852015-04-30 17:32:03 -07001002 append_transport_info(result, "", t->devpath, false);
1003 append_transport_info(result, "product:", t->product, false);
1004 append_transport_info(result, "model:", t->model, true);
1005 append_transport_info(result, "device:", t->device, false);
Josh Gaob39e4152017-08-16 16:57:01 -07001006
1007 // Put id at the end, so that anyone parsing the output here can always find it by scanning
1008 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
1009 *result += " transport_id:";
1010 *result += std::to_string(t->id);
Scott Anderson27042382012-05-30 18:11:27 -07001011 }
Elliott Hughes88b4c852015-04-30 17:32:03 -07001012 *result += '\n';
Scott Anderson27042382012-05-30 18:11:27 -07001013}
1014
Elliott Hughes88b4c852015-04-30 17:32:03 -07001015std::string list_transports(bool long_listing) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001016 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov4dd35012017-12-17 10:56:07 +00001017
1018 auto sorted_transport_list = transport_list;
1019 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
1020 if (x->type != y->type) {
1021 return x->type < y->type;
1022 }
1023 return strcmp(x->serial, y->serial) < 0;
1024 });
1025
1026 std::string result;
1027 for (const auto& t : sorted_transport_list) {
Elliott Hughes88b4c852015-04-30 17:32:03 -07001028 append_transport(t, &result, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001029 }
Elliott Hughes88b4c852015-04-30 17:32:03 -07001030 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001031}
1032
Josh Gao4e562502016-10-27 14:01:08 -07001033void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001034 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao4e562502016-10-27 14:01:08 -07001035 for (auto& t : transport_list) {
1036 if (predicate(t)) {
1037 t->Kick();
1038 }
1039 }
1040}
1041
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001042/* hack for osx */
Dan Albertecce5032015-05-18 16:46:31 -07001043void close_usb_devices() {
Josh Gao4e562502016-10-27 14:01:08 -07001044 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001045}
Josh Gao0d6aa992016-11-22 14:32:34 -08001046#endif // ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001047
Josh Gao0d6aa992016-11-22 14:32:34 -08001048int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertecce5032015-05-18 16:46:31 -07001049 atransport* t = new atransport();
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001050
1051 if (!serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001052 char buf[32];
1053 snprintf(buf, sizeof(buf), "T-%p", t);
1054 serial = buf;
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001055 }
Dan Albertecce5032015-05-18 16:46:31 -07001056
Yabin Cui815ad882015-09-02 17:44:28 -07001057 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001058 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertecce5032015-05-18 16:46:31 -07001059 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001060 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001061 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001062
Josh Gaoaaa82f72017-08-17 13:50:51 -07001063 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -07001064 for (const auto& transport : pending_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001065 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001066 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001067 << " is already in pending_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001068 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001069 return -1;
1070 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001071 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001072
Elliott Hughes85952832015-10-07 15:59:35 -07001073 for (const auto& transport : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001074 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001075 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao0d6aa992016-11-22 14:32:34 -08001076 << " is already in transport_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -07001077 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001078 return -1;
1079 }
1080 }
1081
Dan Albertecce5032015-05-18 16:46:31 -07001082 pending_list.push_front(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001083 t->serial = strdup(serial);
Josh Gaoe7daf572016-09-21 12:37:10 -07001084
1085 lock.unlock();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001086
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001087 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001088 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001089}
1090
Mike Lockwood81ffe172009-10-11 23:04:18 -04001091#if ADB_HOST
Josh Gao0d6aa992016-11-22 14:32:34 -08001092atransport* find_transport(const char* serial) {
Dan Albertecce5032015-05-18 16:46:31 -07001093 atransport* result = nullptr;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001094
Josh Gaoaaa82f72017-08-17 13:50:51 -07001095 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001096 for (auto& t : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -07001097 if (t->serial && strcmp(serial, t->serial) == 0) {
1098 result = t;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001099 break;
1100 }
Dan Albertecce5032015-05-18 16:46:31 -07001101 }
Mike Lockwood81ffe172009-10-11 23:04:18 -04001102
Dan Albertecce5032015-05-18 16:46:31 -07001103 return result;
Mike Lockwood81ffe172009-10-11 23:04:18 -04001104}
1105
Yabin Cui4d64fd82015-08-27 12:03:11 -07001106void kick_all_tcp_devices() {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001107 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -07001108 for (auto& t : transport_list) {
Yabin Cuif401ead2016-04-29 16:53:52 -07001109 if (t->IsTcpDevice()) {
Yabin Cui4e222292015-08-31 11:50:24 -07001110 // Kicking breaks the read_transport thread of this transport out of any read, then
1111 // the read_transport thread will notify the main thread to make this transport
1112 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cui4d64fd82015-08-27 12:03:11 -07001113 // Finally, this transport will be closed and freed in the main thread.
Yabin Cuif2a9f9b2016-04-18 11:22:34 -07001114 t->Kick();
Mike Lockwood01c2c302010-05-24 10:44:35 -04001115 }
Dan Albertecce5032015-05-18 16:46:31 -07001116 }
Mike Lockwood01c2c302010-05-24 10:44:35 -04001117}
1118
Mike Lockwood81ffe172009-10-11 23:04:18 -04001119#endif
1120
Josh Gao0d6aa992016-11-22 14:32:34 -08001121void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1122 unsigned writeable) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001123 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertecce5032015-05-18 16:46:31 -07001124
Josh Gao0d6aa992016-11-22 14:32:34 -08001125 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cui3cf1b362017-03-10 16:01:01 -08001126 init_usb_transport(t, usb);
Josh Gao0d6aa992016-11-22 14:32:34 -08001127 if (serial) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001128 t->serial = strdup(serial);
1129 }
Dan Albertecce5032015-05-18 16:46:31 -07001130
1131 if (devpath) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001132 t->devpath = strdup(devpath);
1133 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001134
Josh Gaoe7daf572016-09-21 12:37:10 -07001135 {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001136 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaoe7daf572016-09-21 12:37:10 -07001137 pending_list.push_front(t);
1138 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001139
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001140 register_transport(t);
1141}
1142
Dan Albert9a50f4c2015-05-18 16:43:57 -07001143// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao0d6aa992016-11-22 14:32:34 -08001144void unregister_usb_transport(usb_handle* usb) {
Josh Gaoaaa82f72017-08-17 13:50:51 -07001145 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao395b86a2018-01-28 20:32:46 -08001146 transport_list.remove_if([usb](atransport* t) {
1147 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1148 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1149 }
1150 return false;
1151 });
Mike Lockwoode45583f2009-08-08 12:37:44 -04001152}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001153
Josh Gao67b683a2017-05-16 15:02:45 -07001154bool check_header(apacket* p, atransport* t) {
Josh Gao0d6aa992016-11-22 14:32:34 -08001155 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cui3cf1b362017-03-10 16:01:01 -08001156 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1157 << ", magic = " << p->msg.magic;
Josh Gao67b683a2017-05-16 15:02:45 -07001158 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001159 }
1160
Josh Gao0d6aa992016-11-22 14:32:34 -08001161 if (p->msg.data_length > t->get_max_payload()) {
1162 VLOG(RWX) << "check_header(): " << p->msg.data_length
1163 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao67b683a2017-05-16 15:02:45 -07001164 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001165 }
1166
Josh Gao67b683a2017-05-16 15:02:45 -07001167 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001168}
1169
Josh Gaoeac20582016-10-05 19:02:29 -07001170#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -07001171std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes801066a2016-06-29 17:42:01 -07001172 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1173
Josh Gao22cb70b2016-08-18 22:00:12 -07001174 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes801066a2016-06-29 17:42:01 -07001175 keys_.pop_front();
1176 return result;
1177}
Josh Gaoeac20582016-10-05 19:02:29 -07001178#endif