blob: 6b1a00bf191d4cc8438d081e8a359676674ce0e2 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert76649012015-02-24 15:51:19 -080018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert055f1aa2015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070024#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdio.h>
26#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080028#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
Spencer Low363af562015-11-07 18:51:54 -080030#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070031#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070032#include <mutex>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070033#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070034
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080036#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/stringprintf.h>
38#include <android-base/strings.h>
Josh Gaob122b172017-08-16 16:57:01 -070039#include <android-base/thread_annotations.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070040
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070042#include "adb_auth.h"
Josh Gaob800d882018-01-28 20:32:46 -080043#include "adb_io.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080044#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070045#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080046#include "diagnose_usb.h"
Yabin Cuib5e11412017-03-10 16:01:01 -080047#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49static void transport_unref(atransport *t);
50
Josh Gaob122b172017-08-16 16:57:01 -070051// TODO: unordered_map<TransportId, atransport*>
Josh Gaob7b1edf2015-11-11 17:56:12 -080052static auto& transport_list = *new std::list<atransport*>();
53static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070054
Josh Gao1db71af2017-08-17 13:50:51 -070055static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Todd Kennedy51c05ec2015-11-10 00:03:25 +000057const char* const kFeatureShell2 = "shell_v2";
58const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080059const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080060const char* const kFeatureLibusb = "libusb";
Dan Albert5176df82017-05-23 14:30:00 -070061const char* const kFeaturePushSync = "push_sync";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000062
Josh Gaob122b172017-08-16 16:57:01 -070063TransportId NextTransportId() {
64 static std::atomic<TransportId> next(1);
65 return next++;
66}
67
Josh Gaob800d882018-01-28 20:32:46 -080068bool FdConnection::Read(apacket* packet) {
69 if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) {
70 D("remote local: read terminated (message)");
71 return false;
72 }
73
Josh Gaof571fcb2018-02-05 18:49:10 -080074 if (packet->msg.data_length > MAX_PAYLOAD) {
Josh Gao5caaebd2018-02-02 14:38:04 -080075 D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
76 return false;
77 }
78
Josh Gaof571fcb2018-02-05 18:49:10 -080079 packet->payload.resize(packet->msg.data_length);
80
81 if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) {
Josh Gaob800d882018-01-28 20:32:46 -080082 D("remote local: terminated (data)");
83 return false;
84 }
85
86 return true;
87}
88
89bool FdConnection::Write(apacket* packet) {
Josh Gaof571fcb2018-02-05 18:49:10 -080090 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) {
Josh Gaob800d882018-01-28 20:32:46 -080091 D("remote local: write terminated");
92 return false;
93 }
94
Josh Gaof571fcb2018-02-05 18:49:10 -080095 if (packet->msg.data_length) {
96 if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) {
97 D("remote local: write terminated");
98 return false;
99 }
100 }
101
Josh Gaob800d882018-01-28 20:32:46 -0800102 return true;
103}
104
105void FdConnection::Close() {
106 adb_shutdown(fd_.get());
107 fd_.reset();
108}
109
Yabin Cuiaed3c612015-09-22 15:52:57 -0700110static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800111 unsigned command = p->msg.command;
112 int len = p->msg.data_length;
113 char cmd[9];
114 char arg0[12], arg1[12];
115 int n;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100116
117 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800118 int b = (command >> (n * 8)) & 255;
119 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100120 cmd[n] = (char)b;
121 }
122 if (n == 4) {
123 cmd[4] = 0;
124 } else {
125 /* There is some non-ASCII name in the command, so dump
126 * the hexadecimal value instead */
127 snprintf(cmd, sizeof cmd, "%08x", command);
128 }
129
130 if (p->msg.arg0 < 256U)
131 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
132 else
133 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
134
135 if (p->msg.arg1 < 256U)
136 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
137 else
138 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
139
Josh Gao1290fbf2016-11-22 14:32:34 -0800140 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
141 func, cmd, arg0, arg1, len);
Josh Gaof571fcb2018-02-05 18:49:10 -0800142 result += dump_hex(p->payload.data(), p->payload.size());
Yabin Cuiaed3c612015-09-22 15:52:57 -0700143 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100144}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100145
Josh Gao1290fbf2016-11-22 14:32:34 -0800146static int read_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800147 ATRACE_NAME("read_packet");
Yabin Cui62641292015-07-30 19:58:10 -0700148 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100149 if (!name) {
150 snprintf(buff, sizeof buff, "fd=%d", fd);
151 name = buff;
152 }
Josh Gao1290fbf2016-11-22 14:32:34 -0800153 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700154 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800155 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700156 int r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800157 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700159 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700161 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 -0800162 return -1;
163 }
164 }
165
Yabin Cuiaed3c612015-09-22 15:52:57 -0700166 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 return 0;
168}
169
Josh Gao1290fbf2016-11-22 14:32:34 -0800170static int write_packet(int fd, const char* name, apacket** ppacket) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800171 ATRACE_NAME("write_packet");
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100172 char buff[8];
173 if (!name) {
174 snprintf(buff, sizeof buff, "fd=%d", fd);
175 name = buff;
176 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700177 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Josh Gao1290fbf2016-11-22 14:32:34 -0800178 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
Yabin Cui62641292015-07-30 19:58:10 -0700179 int len = sizeof(apacket*);
Josh Gao1290fbf2016-11-22 14:32:34 -0800180 while (len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700181 int r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800182 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 len -= r;
184 p += r;
185 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700186 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 -0800187 return -1;
188 }
189 }
190 return 0;
191}
192
Josh Gao1290fbf2016-11-22 14:32:34 -0800193static void transport_socket_events(int fd, unsigned events, void* _t) {
194 atransport* t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700195 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
Josh Gao1290fbf2016-11-22 14:32:34 -0800196 if (events & FDE_READ) {
197 apacket* p = 0;
198 if (read_packet(fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700199 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
Josh Gaof571fcb2018-02-05 18:49:10 -0800200 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 }
Josh Gaof571fcb2018-02-05 18:49:10 -0800202
203 handle_packet(p, (atransport*)_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 }
205}
206
Josh Gao06d61d42016-10-06 13:31:44 -0700207void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayde471942017-12-07 11:40:00 -0800209 // compute a checksum for connection/auth packets for compatibility reasons
210 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
211 p->msg.data_check = 0;
212 } else {
213 p->msg.data_check = calculate_apacket_checksum(p);
214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
216 print_packet("send", p);
217
218 if (t == NULL) {
Josh Gao06d61d42016-10-06 13:31:44 -0700219 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 }
221
Josh Gao06d61d42016-10-06 13:31:44 -0700222 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 fatal_errno("cannot enqueue packet on transport socket");
224 }
225}
226
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700227// The transport is opened by transport_register_func before
228// the read_transport and write_transport threads are started.
229//
230// The read_transport thread issues a SYNC(1, token) message to let
231// the write_transport thread know to start things up. In the event
232// of transport IO failure, the read_transport thread will post a
233// SYNC(0,0) message to ensure shutdown.
234//
235// The transport will not actually be closed until both threads exit, but the threads
236// will kick the transport on their way out to disconnect the underlying device.
237//
238// read_transport thread reads data from a transport (representing a usb/tcp connection),
239// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800240static void read_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800241 atransport* t = reinterpret_cast<atransport*>(_t);
242 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
Josh Gao1290fbf2016-11-22 14:32:34 -0800244 adb_thread_setname(
245 android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport")));
246 D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd,
247 t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 p = get_apacket();
249 p->msg.command = A_SYNC;
250 p->msg.arg0 = 1;
251 p->msg.arg1 = ++(t->sync_token);
252 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gaof571fcb2018-02-05 18:49:10 -0800253 D("sending SYNC packet (len = %u, payload.size() = %zu)", p->msg.data_length, p->payload.size());
Josh Gao1290fbf2016-11-22 14:32:34 -0800254 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700256 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 goto oops;
258 }
259
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700260 D("%s: data pump started", t->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800261 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800262 ATRACE_NAME("read_transport loop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 p = get_apacket();
264
Josh Gaocfe72e22016-11-29 09:40:29 -0800265 {
266 ATRACE_NAME("read_transport read_remote");
Josh Gaob800d882018-01-28 20:32:46 -0800267 if (!t->connection->Read(p)) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800268 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800270 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 }
Josh Gaob800d882018-01-28 20:32:46 -0800272
273 if (!check_header(p, t)) {
274 D("%s: remote read: bad header", t->serial);
275 put_apacket(p);
276 break;
277 }
278
Yabin Cuib5e11412017-03-10 16:01:01 -0800279#if ADB_HOST
280 if (p->msg.command == 0) {
Josh Gaofb413a22018-01-29 18:09:20 -0800281 put_apacket(p);
Yabin Cuib5e11412017-03-10 16:01:01 -0800282 continue;
283 }
284#endif
Josh Gaocfe72e22016-11-29 09:40:29 -0800285 }
286
287 D("%s: received remote packet, sending to transport", t->serial);
288 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 put_apacket(p);
Josh Gaocfe72e22016-11-29 09:40:29 -0800290 D("%s: failed to write apacket to transport", t->serial);
291 goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
293 }
294
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700295 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 p = get_apacket();
297 p->msg.command = A_SYNC;
298 p->msg.arg0 = 0;
299 p->msg.arg1 = 0;
300 p->msg.magic = A_SYNC ^ 0xffffffff;
Josh Gao1290fbf2016-11-22 14:32:34 -0800301 if (write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700303 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
305
306oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700307 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 kick_transport(t);
309 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310}
311
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700312// write_transport thread gets packets sent by the main thread (through send_packet()),
313// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800314static void write_transport_thread(void* _t) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800315 atransport* t = reinterpret_cast<atransport*>(_t);
316 apacket* p;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 int active = 0;
318
Josh Gao1290fbf2016-11-22 14:32:34 -0800319 adb_thread_setname(
320 android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport")));
321 D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
Josh Gao1290fbf2016-11-22 14:32:34 -0800323 for (;;) {
Josh Gaocfe72e22016-11-29 09:40:29 -0800324 ATRACE_NAME("write_transport loop");
Josh Gao1290fbf2016-11-22 14:32:34 -0800325 if (read_packet(t->fd, t->serial, &p)) {
326 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 -0800327 break;
328 }
Josh Gaocfe72e22016-11-29 09:40:29 -0800329
Josh Gao1290fbf2016-11-22 14:32:34 -0800330 if (p->msg.command == A_SYNC) {
331 if (p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700332 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 put_apacket(p);
334 break;
335 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800336 if (p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700337 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 active = 1;
339 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800340 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 -0800341 }
342 }
343 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800344 if (active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700345 D("%s: transport got packet, sending to remote", t->serial);
Josh Gaocfe72e22016-11-29 09:40:29 -0800346 ATRACE_NAME("write_transport write_remote");
Josh Gaof571fcb2018-02-05 18:49:10 -0800347
348 // Allow sending the payload's implicit null terminator.
349 if (p->msg.data_length != p->payload.size()) {
350 LOG(FATAL) << "packet data length doesn't match payload: msg.data_length = "
351 << p->msg.data_length << ", payload.size() = " << p->payload.size();
352 }
353
Yabin Cuib5e11412017-03-10 16:01:01 -0800354 if (t->Write(p) != 0) {
355 D("%s: remote write failed for transport", t->serial);
356 put_apacket(p);
357 break;
358 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700360 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 }
362 }
363
364 put_apacket(p);
365 }
366
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700367 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 kick_transport(t);
369 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370}
371
Yabin Cuif4b99282015-08-27 12:03:11 -0700372void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700373 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700374 // As kick_transport() can be called from threads without guarantee that t is valid,
375 // check if the transport is in transport_list first.
Josh Gaob122b172017-08-16 16:57:01 -0700376 //
377 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700378 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700379 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700380 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700381}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
383static int transport_registration_send = -1;
384static int transport_registration_recv = -1;
385static fdevent transport_registration_fde;
386
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
389/* this adds support required by the 'track-devices' service.
390 * this is used to send the content of "list_transport" to any
391 * number of client connections that want it through a single
392 * live TCP connection
393 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800395 asocket socket;
Josh Gaoe0361d12018-02-12 17:24:00 -0800396 bool update_needed = false;
397 bool long_output = false;
398 device_tracker* next = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399};
400
401/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800402static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
Josh Gao1290fbf2016-11-22 14:32:34 -0800404static void device_tracker_remove(device_tracker* tracker) {
405 device_tracker** pnode = &device_tracker_list;
406 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407
Josh Gao1db71af2017-08-17 13:50:51 -0700408 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 while (node) {
410 if (node == tracker) {
411 *pnode = node->next;
412 break;
413 }
414 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800415 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417}
418
Josh Gao1290fbf2016-11-22 14:32:34 -0800419static void device_tracker_close(asocket* socket) {
420 device_tracker* tracker = (device_tracker*)socket;
421 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422
Josh Gao1290fbf2016-11-22 14:32:34 -0800423 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 if (peer) {
425 peer->peer = NULL;
426 peer->close(peer);
427 }
428 device_tracker_remove(tracker);
Josh Gaoe0361d12018-02-12 17:24:00 -0800429 delete tracker;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430}
431
Josh Gao27cb7dc2018-02-01 13:17:50 -0800432static int device_tracker_enqueue(asocket* socket, std::string) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 /* you can't read from a device tracker, close immediately */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 device_tracker_close(socket);
435 return -1;
436}
437
Elliott Hughese67f1f82015-04-30 17:32:03 -0700438static int device_tracker_send(device_tracker* tracker, const std::string& string) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700439 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Josh Gao27cb7dc2018-02-01 13:17:50 -0800441 std::string data;
442 data.resize(4 + string.size());
443 char buf[5];
444 snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
445 memcpy(&data[0], buf, 4);
446 memcpy(&data[4], string.data(), string.size());
447 return peer->enqueue(peer, std::move(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448}
449
Elliott Hughese67f1f82015-04-30 17:32:03 -0700450static void device_tracker_ready(asocket* socket) {
451 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
Elliott Hughese67f1f82015-04-30 17:32:03 -0700453 // We want to send the device list when the tracker connects
454 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700455 if (tracker->update_needed) {
456 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457
Josh Gaob0c18022017-08-14 18:57:54 -0700458 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700459 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 }
461}
462
Josh Gaob0c18022017-08-14 18:57:54 -0700463asocket* create_device_tracker(bool long_output) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800464 device_tracker* tracker = new device_tracker();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700465 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
Josh Gao1290fbf2016-11-22 14:32:34 -0800467 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
469 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800470 tracker->socket.ready = device_tracker_ready;
471 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700472 tracker->update_needed = true;
473 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474
Josh Gao1290fbf2016-11-22 14:32:34 -0800475 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 device_tracker_list = tracker;
477
478 return &tracker->socket;
479}
480
Josh Gaofd713e52017-05-03 22:37:10 -0700481// Check if all of the USB transports are connected.
482bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700483 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700484 for (const auto& t : transport_list) {
485 if (!fn(t)) {
486 return false;
487 }
488 }
489 for (const auto& t : pending_list) {
490 if (!fn(t)) {
491 return false;
492 }
493 }
494 return true;
495}
496
Elliott Hughese67f1f82015-04-30 17:32:03 -0700497// Call this function each time the transport list has changed.
498void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700499 update_transport_status();
500
501 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700502 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503
Elliott Hughese67f1f82015-04-30 17:32:03 -0700504 device_tracker* tracker = device_tracker_list;
505 while (tracker != nullptr) {
506 device_tracker* next = tracker->next;
507 // This may destroy the tracker if the connection is closed.
508 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 tracker = next;
510 }
511}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700512
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700514
515void update_transports() {
516 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700518
Josh Gao1290fbf2016-11-22 14:32:34 -0800519#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520
Josh Gao1290fbf2016-11-22 14:32:34 -0800521struct tmsg {
522 atransport* transport;
523 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524};
525
Josh Gao1290fbf2016-11-22 14:32:34 -0800526static int transport_read_action(int fd, struct tmsg* m) {
527 char* p = (char*)m;
528 int len = sizeof(*m);
529 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530
Josh Gao1290fbf2016-11-22 14:32:34 -0800531 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800533 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800535 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 return -1;
539 }
540 }
541 return 0;
542}
543
Josh Gao1290fbf2016-11-22 14:32:34 -0800544static int transport_write_action(int fd, struct tmsg* m) {
545 char* p = (char*)m;
546 int len = sizeof(*m);
547 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
Josh Gao1290fbf2016-11-22 14:32:34 -0800549 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800550 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800551 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800553 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700555 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 return -1;
557 }
558 }
559 return 0;
560}
561
Josh Gao1290fbf2016-11-22 14:32:34 -0800562static void transport_registration_func(int _fd, unsigned ev, void* data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 int s[2];
Josh Gao1290fbf2016-11-22 14:32:34 -0800565 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566
Josh Gao1290fbf2016-11-22 14:32:34 -0800567 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 return;
569 }
570
Josh Gao1290fbf2016-11-22 14:32:34 -0800571 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 fatal_errno("cannot read transport registration socket");
573 }
574
575 t = m.transport;
576
Dan Albert1792c232015-05-18 13:06:53 -0700577 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700578 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579
Josh Gao1290fbf2016-11-22 14:32:34 -0800580 /* IMPORTANT: the remove closes one half of the
581 ** socket pair. The close closes the other half.
582 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 fdevent_remove(&(t->transport_fde));
584 adb_close(t->fd);
585
Josh Gao0cd3ae12016-09-21 12:37:10 -0700586 {
Josh Gao1db71af2017-08-17 13:50:51 -0700587 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700588 transport_list.remove(t);
589 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590
Josh Gao1290fbf2016-11-22 14:32:34 -0800591 if (t->product) free(t->product);
592 if (t->serial) free(t->serial);
593 if (t->model) free(t->model);
594 if (t->device) free(t->device);
595 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596
Dan Albertc7915a32015-05-18 16:46:31 -0700597 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598
599 update_transports();
600 return;
601 }
602
Mike Lockwood0927bf92009-08-08 12:37:44 -0400603 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800604 if (t->GetConnectionState() != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400606 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800607
Dan Albertc7915a32015-05-18 16:46:31 -0700608 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400609 fatal_errno("cannot open transport socketpair");
610 }
611
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700612 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400613
614 t->transport_socket = s[0];
615 t->fd = s[1];
616
Josh Gao1290fbf2016-11-22 14:32:34 -0800617 fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400618
619 fdevent_set(&(t->transport_fde), FDE_READ);
620
Josh Gaoe1dacfc2017-04-12 17:00:49 -0700621 std::thread(write_transport_thread, t).detach();
622 std::thread(read_transport_thread, t).detach();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 }
624
Josh Gao0cd3ae12016-09-21 12:37:10 -0700625 {
Josh Gao1db71af2017-08-17 13:50:51 -0700626 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700627 pending_list.remove(t);
628 transport_list.push_front(t);
629 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 update_transports();
632}
633
Josh Gao1290fbf2016-11-22 14:32:34 -0800634void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800635 int s[2];
636
Josh Gao1290fbf2016-11-22 14:32:34 -0800637 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 fatal_errno("cannot open transport registration socketpair");
639 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700640 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641
642 transport_registration_send = s[0];
643 transport_registration_recv = s[1];
644
Josh Gao1290fbf2016-11-22 14:32:34 -0800645 fdevent_install(&transport_registration_fde, transport_registration_recv,
646 transport_registration_func, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647
648 fdevent_set(&transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700649}
650
651void kick_all_transports() {
652 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700653 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700654 for (auto t : transport_list) {
655 t->Kick();
656 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657}
658
659/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800660static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 tmsg m;
662 m.transport = transport;
663 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700664 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800665 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 fatal_errno("cannot write transport registration socket\n");
667 }
668}
669
Josh Gao1290fbf2016-11-22 14:32:34 -0800670static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 tmsg m;
672 m.transport = transport;
673 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700674 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800675 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 fatal_errno("cannot write transport registration socket\n");
677 }
678}
679
Yabin Cuif4b99282015-08-27 12:03:11 -0700680static void transport_unref(atransport* t) {
681 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700682
Josh Gaoe48ecce2017-09-13 13:40:57 -0700683 std::lock_guard<std::recursive_mutex> lock(transport_lock);
684 CHECK_GT(t->ref_count, 0u);
685 t->ref_count--;
686 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700687 D("transport: %s unref (kicking and closing)", t->serial);
Josh Gaob800d882018-01-28 20:32:46 -0800688 t->connection->Close();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400689 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100690 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700691 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400692 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693}
694
Josh Gao1290fbf2016-11-22 14:32:34 -0800695static int qual_match(const char* to_test, const char* prefix, const char* qual,
696 bool sanitize_qual) {
697 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700698 return !qual || !*qual;
699
Josh Gao1290fbf2016-11-22 14:32:34 -0800700 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700701
702 if (prefix) {
703 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800704 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700705 }
706 }
707
708 while (*qual) {
709 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800710 if (sanitize_qual && !isalnum(ch)) ch = '_';
711 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700712 }
713
714 /* Everything matched so far. Return true if *to_test is a NUL. */
715 return !*to_test;
716}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717
Josh Gaob122b172017-08-16 16:57:01 -0700718atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
719 bool* is_ambiguous, std::string* error_out,
720 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700721 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722
Josh Gaob122b172017-08-16 16:57:01 -0700723 if (transport_id != 0) {
724 *error_out =
725 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
726 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700727 *error_out = android::base::StringPrintf("device '%s' not found", serial);
728 } else if (type == kTransportLocal) {
729 *error_out = "no emulators found";
730 } else if (type == kTransportAny) {
731 *error_out = "no devices/emulators found";
732 } else {
733 *error_out = "no devices found";
734 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735
Josh Gao1db71af2017-08-17 13:50:51 -0700736 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700737 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800738 if (t->GetConnectionState() == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800739#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800740 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800741#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400742 continue;
743 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400744
Josh Gaob122b172017-08-16 16:57:01 -0700745 if (transport_id) {
746 if (t->id == transport_id) {
747 result = t;
748 break;
749 }
750 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800751 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700752 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700753 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700754 if (is_ambiguous) *is_ambiguous = true;
755 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700756 break;
757 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700759 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700761 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700763 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700764 if (is_ambiguous) *is_ambiguous = true;
765 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 break;
767 }
768 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700769 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700771 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700772 if (is_ambiguous) *is_ambiguous = true;
773 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774 break;
775 }
776 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700777 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700779 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700780 if (is_ambiguous) *is_ambiguous = true;
781 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 break;
783 }
784 result = t;
785 }
786 }
787 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700788 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800789
Elliott Hughes8d28e192015-10-07 14:55:10 -0700790 // Don't return unauthorized devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800791 if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700792 *error_out = "device unauthorized.\n";
793 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
794 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
795 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
796 *error_out += "\n";
797 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
798 *error_out += "Otherwise check for a confirmation dialog on your device.";
799 result = nullptr;
800 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800801
Elliott Hughes8d28e192015-10-07 14:55:10 -0700802 // Don't return offline devices; the caller can't do anything with them.
Yabin Cuib5e11412017-03-10 16:01:01 -0800803 if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700804 *error_out = "device offline";
805 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 }
807
808 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700809 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 }
811
812 return result;
813}
814
Yabin Cuib5e11412017-03-10 16:01:01 -0800815int atransport::Write(apacket* p) {
Josh Gaob800d882018-01-28 20:32:46 -0800816 return this->connection->Write(p) ? 0 : -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800817}
818
Yabin Cui7f274902016-04-18 11:22:34 -0700819void atransport::Kick() {
820 if (!kicked_) {
Josh Gaob800d882018-01-28 20:32:46 -0800821 D("kicking transport %s", this->serial);
Yabin Cui7f274902016-04-18 11:22:34 -0700822 kicked_ = true;
Josh Gaob800d882018-01-28 20:32:46 -0800823 this->connection->Close();
Yabin Cui7f274902016-04-18 11:22:34 -0700824 }
825}
826
Yabin Cuib5e11412017-03-10 16:01:01 -0800827ConnectionState atransport::GetConnectionState() const {
828 return connection_state_;
829}
830
831void atransport::SetConnectionState(ConnectionState state) {
832 check_main_thread();
833 connection_state_ = state;
834}
835
David Purselld2acbd12015-12-02 15:14:31 -0800836const std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800837 ConnectionState state = GetConnectionState();
838 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800839 case kCsOffline:
840 return "offline";
841 case kCsBootloader:
842 return "bootloader";
843 case kCsDevice:
844 return "device";
845 case kCsHost:
846 return "host";
847 case kCsRecovery:
848 return "recovery";
849 case kCsNoPerm:
850 return UsbNoPermissionsShortHelpText();
851 case kCsSideload:
852 return "sideload";
853 case kCsUnauthorized:
854 return "unauthorized";
855 default:
856 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800857 }
858}
859
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100860void atransport::update_version(int version, size_t payload) {
861 protocol_version = std::min(version, A_VERSION);
862 max_payload = std::min(payload, MAX_PAYLOAD);
863}
864
865int atransport::get_protocol_version() const {
866 return protocol_version;
867}
868
869size_t atransport::get_max_payload() const {
870 return max_payload;
871}
872
David Pursell4e2fd362015-09-22 10:43:08 -0700873namespace {
David Pursell0955c662015-08-31 10:42:13 -0700874
David Pursell4e2fd362015-09-22 10:43:08 -0700875constexpr char kFeatureStringDelimiter = ',';
876
877} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700878
879const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700880 // Local static allocation to avoid global non-POD variables.
881 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800882 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700883 // Increment ADB_SERVER_VERSION whenever the feature list changes to
884 // make sure that the adb client and server features stay in sync
885 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700886 };
887
888 return *features;
889}
890
891std::string FeatureSetToString(const FeatureSet& features) {
892 return android::base::Join(features, kFeatureStringDelimiter);
893}
894
895FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700896 if (features_string.empty()) {
897 return FeatureSet();
898 }
899
Josh Gao1290fbf2016-11-22 14:32:34 -0800900 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -0700901 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700902}
903
David Pursell70ef7b42015-09-30 13:35:42 -0700904bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800905 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -0700906}
907
Dan Albert1792c232015-05-18 13:06:53 -0700908bool atransport::has_feature(const std::string& feature) const {
909 return features_.count(feature) > 0;
910}
911
David Pursell4e2fd362015-09-22 10:43:08 -0700912void atransport::SetFeatures(const std::string& features_string) {
913 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700914}
915
Yabin Cuib3298242015-08-28 15:09:44 -0700916void atransport::AddDisconnect(adisconnect* disconnect) {
917 disconnects_.push_back(disconnect);
918}
919
920void atransport::RemoveDisconnect(adisconnect* disconnect) {
921 disconnects_.remove(disconnect);
922}
923
924void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700925 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700926 disconnect->func(disconnect->opaque, this);
927 }
928 disconnects_.clear();
929}
930
David Pursell3f902aa2016-03-01 08:58:26 -0800931bool atransport::MatchesTarget(const std::string& target) const {
932 if (serial) {
933 if (target == serial) {
934 return true;
935 } else if (type == kTransportLocal) {
936 // Local transports can match [tcp:|udp:]<hostname>[:port].
937 const char* local_target_ptr = target.c_str();
938
939 // For fastboot compatibility, ignore protocol prefixes.
940 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -0800941 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -0800942 local_target_ptr += 4;
943 }
944
945 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
946 std::string serial_host, error;
947 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -0800948 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -0800949 // |target| may omit the port to default to ours.
950 std::string target_host;
951 int target_port = serial_port;
952 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
953 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -0800954 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -0800955 return true;
956 }
957 }
958 }
959 }
960
961 return (devpath && target == devpath) ||
962 qual_match(target.c_str(), "product:", product, false) ||
963 qual_match(target.c_str(), "model:", model, true) ||
964 qual_match(target.c_str(), "device:", device, false);
965}
966
Elliott Hughese67f1f82015-04-30 17:32:03 -0700967#if ADB_HOST
968
Josh Gaob122b172017-08-16 16:57:01 -0700969// We use newline as our delimiter, make sure to never output it.
970static std::string sanitize(std::string str, bool alphanumeric) {
971 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
972 : [](const char c) { return c == '\n'; };
973 std::replace_if(str.begin(), str.end(), pred, '_');
974 return str;
975}
976
Josh Gao1290fbf2016-11-22 14:32:34 -0800977static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -0700978 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700979 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700980 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700981 }
982
Elliott Hughese67f1f82015-04-30 17:32:03 -0700983 *result += ' ';
984 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -0700985 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700986}
987
Josh Gao1290fbf2016-11-22 14:32:34 -0800988static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700989 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700990 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700991 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700992 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700993
994 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700995 *result += serial;
996 *result += '\t';
997 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700998 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -0800999 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001000
Elliott Hughese67f1f82015-04-30 17:32:03 -07001001 append_transport_info(result, "", t->devpath, false);
1002 append_transport_info(result, "product:", t->product, false);
1003 append_transport_info(result, "model:", t->model, true);
1004 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -07001005
1006 // Put id at the end, so that anyone parsing the output here can always find it by scanning
1007 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
1008 *result += " transport_id:";
1009 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001010 }
Elliott Hughese67f1f82015-04-30 17:32:03 -07001011 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001012}
1013
Elliott Hughese67f1f82015-04-30 17:32:03 -07001014std::string list_transports(bool long_listing) {
Josh Gao1db71af2017-08-17 13:50:51 -07001015 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov04398a92017-12-17 10:56:07 +00001016
1017 auto sorted_transport_list = transport_list;
1018 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
1019 if (x->type != y->type) {
1020 return x->type < y->type;
1021 }
1022 return strcmp(x->serial, y->serial) < 0;
1023 });
1024
1025 std::string result;
1026 for (const auto& t : sorted_transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -07001027 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001028 }
Elliott Hughese67f1f82015-04-30 17:32:03 -07001029 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001030}
1031
Josh Gao22d2b3e2016-10-27 14:01:08 -07001032void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -07001033 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -07001034 for (auto& t : transport_list) {
1035 if (predicate(t)) {
1036 t->Kick();
1037 }
1038 }
1039}
1040
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -07001042void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -07001043 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001044}
Josh Gao1290fbf2016-11-22 14:32:34 -08001045#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001046
Josh Gao1290fbf2016-11-22 14:32:34 -08001047int register_socket_transport(int s, const char* serial, int port, int local) {
Dan Albertc7915a32015-05-18 16:46:31 -07001048 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001049
1050 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001051 char buf[32];
1052 snprintf(buf, sizeof(buf), "T-%p", t);
1053 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001054 }
Dan Albertc7915a32015-05-18 16:46:31 -07001055
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001056 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001057 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -07001058 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001059 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001061
Josh Gao1db71af2017-08-17 13:50:51 -07001062 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -07001063 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001064 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001065 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001066 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001067 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001068 return -1;
1069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001071
Elliott Hughes65fe2512015-10-07 15:59:35 -07001072 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001073 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001074 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001075 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001076 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001077 return -1;
1078 }
1079 }
1080
Dan Albertc7915a32015-05-18 16:46:31 -07001081 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001082 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001083
1084 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -07001085
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001086 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001087 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001088}
1089
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001090#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -08001091atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001092 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001093
Josh Gao1db71af2017-08-17 13:50:51 -07001094 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001095 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001096 if (t->serial && strcmp(serial, t->serial) == 0) {
1097 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001098 break;
1099 }
Dan Albertc7915a32015-05-18 16:46:31 -07001100 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001101
Dan Albertc7915a32015-05-18 16:46:31 -07001102 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001103}
1104
Yabin Cuif4b99282015-08-27 12:03:11 -07001105void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -07001106 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001107 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001108 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001109 // Kicking breaks the read_transport thread of this transport out of any read, then
1110 // the read_transport thread will notify the main thread to make this transport
1111 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001112 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001113 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001114 }
Dan Albertc7915a32015-05-18 16:46:31 -07001115 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001116}
1117
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001118#endif
1119
Josh Gao1290fbf2016-11-22 14:32:34 -08001120void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1121 unsigned writeable) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001122 atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm));
Dan Albertc7915a32015-05-18 16:46:31 -07001123
Josh Gao1290fbf2016-11-22 14:32:34 -08001124 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001125 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001126 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001127 t->serial = strdup(serial);
1128 }
Dan Albertc7915a32015-05-18 16:46:31 -07001129
1130 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001131 t->devpath = strdup(devpath);
1132 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001133
Josh Gao0cd3ae12016-09-21 12:37:10 -07001134 {
Josh Gao1db71af2017-08-17 13:50:51 -07001135 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001136 pending_list.push_front(t);
1137 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139 register_transport(t);
1140}
1141
Dan Albertdcd78a12015-05-18 16:43:57 -07001142// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001143void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001144 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaob800d882018-01-28 20:32:46 -08001145 transport_list.remove_if([usb](atransport* t) {
1146 if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) {
1147 return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
1148 }
1149 return false;
1150 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001151}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152
Josh Gao36dadca2017-05-16 15:02:45 -07001153bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001154 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001155 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1156 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001157 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158 }
1159
Josh Gao1290fbf2016-11-22 14:32:34 -08001160 if (p->msg.data_length > t->get_max_payload()) {
1161 VLOG(RWX) << "check_header(): " << p->msg.data_length
1162 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001163 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164 }
1165
Josh Gao36dadca2017-05-16 15:02:45 -07001166 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001167}
1168
Josh Gao3bd28792016-10-05 19:02:29 -07001169#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001170std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001171 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1172
Josh Gao2e671202016-08-18 22:00:12 -07001173 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001174 keys_.pop_front();
1175 return result;
1176}
Josh Gao3bd28792016-10-05 19:02:29 -07001177#endif