blob: f2c40d2f3463163ae93cdb3a4f0d6f1e50567cef [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"
Josh Gao31b5be62018-03-07 16:51:08 -080020#include "sysdeps/memory.h"
21
Dan Albert76649012015-02-24 15:51:19 -080022#include "transport.h"
23
Dan Albert055f1aa2015-02-20 17:24:58 -080024#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080025#include <errno.h>
Josh Gaob122b172017-08-16 16:57:01 -070026#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <stdio.h>
28#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080030#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Spencer Low363af562015-11-07 18:51:54 -080032#include <algorithm>
Josh Gao0bbf69c2018-02-16 13:24:58 -080033#include <deque>
Dan Albertc7915a32015-05-18 16:46:31 -070034#include <list>
Josh Gao0cd3ae12016-09-21 12:37:10 -070035#include <mutex>
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -070036#include <queue>
Josh Gaoe1dacfc2017-04-12 17:00:49 -070037#include <thread>
Dan Albertc7915a32015-05-18 16:46:31 -070038
Elliott Hughes4f713192015-12-04 22:00:26 -080039#include <android-base/logging.h>
David Pursell3f902aa2016-03-01 08:58:26 -080040#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080041#include <android-base/stringprintf.h>
42#include <android-base/strings.h>
Josh Gaob122b172017-08-16 16:57:01 -070043#include <android-base/thread_annotations.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070044
Josh Gao27768452018-01-02 12:01:43 -080045#include <diagnose_usb.h>
46
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "adb.h"
Elliott Hughes0aeb5052016-06-29 17:42:01 -070048#include "adb_auth.h"
Josh Gaob800d882018-01-28 20:32:46 -080049#include "adb_io.h"
Josh Gaocfe72e22016-11-29 09:40:29 -080050#include "adb_trace.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070051#include "adb_utils.h"
Yabin Cuib5e11412017-03-10 16:01:01 -080052#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -070054static void register_transport(atransport* transport);
55static void remove_transport(atransport* transport);
56static void transport_unref(atransport* transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Josh Gaob122b172017-08-16 16:57:01 -070058// TODO: unordered_map<TransportId, atransport*>
Josh Gaob7b1edf2015-11-11 17:56:12 -080059static auto& transport_list = *new std::list<atransport*>();
60static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070061
Josh Gao1db71af2017-08-17 13:50:51 -070062static auto& transport_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Todd Kennedy51c05ec2015-11-10 00:03:25 +000064const char* const kFeatureShell2 = "shell_v2";
65const char* const kFeatureCmd = "cmd";
Josh Gao5a1e3fd2016-12-05 17:11:34 -080066const char* const kFeatureStat2 = "stat_v2";
Josh Gao5d1756c2017-02-22 17:07:01 -080067const char* const kFeatureLibusb = "libusb";
Dan Albert5176df82017-05-23 14:30:00 -070068const char* const kFeaturePushSync = "push_sync";
Todd Kennedy51c05ec2015-11-10 00:03:25 +000069
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070070namespace {
71
72// A class that helps the Clang Thread Safety Analysis deal with
73// std::unique_lock. Given that std::unique_lock is movable, and the analysis
74// can not currently perform alias analysis, it is not annotated. In order to
75// assert that the mutex is held, a ScopedAssumeLocked can be created just after
76// the std::unique_lock.
77class SCOPED_CAPABILITY ScopedAssumeLocked {
78 public:
79 ScopedAssumeLocked(std::mutex& mutex) ACQUIRE(mutex) {}
80 ~ScopedAssumeLocked() RELEASE() {}
81};
82
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -070083// Tracks and handles atransport*s that are attempting reconnection.
84class ReconnectHandler {
85 public:
86 ReconnectHandler() = default;
87 ~ReconnectHandler() = default;
88
89 // Starts the ReconnectHandler thread.
90 void Start();
91
92 // Requests the ReconnectHandler thread to stop.
93 void Stop();
94
95 // Adds the atransport* to the queue of reconnect attempts.
96 void TrackTransport(atransport* transport);
97
98 private:
99 // The main thread loop.
100 void Run();
101
102 // Tracks a reconnection attempt.
103 struct ReconnectAttempt {
104 atransport* transport;
105 std::chrono::system_clock::time_point deadline;
106 size_t attempts_left;
107 };
108
109 // Only retry for up to one minute.
110 static constexpr const std::chrono::seconds kDefaultTimeout = std::chrono::seconds(10);
111 static constexpr const size_t kMaxAttempts = 6;
112
113 // Protects all members.
114 std::mutex reconnect_mutex_;
115 bool running_ GUARDED_BY(reconnect_mutex_) = true;
116 std::thread handler_thread_;
117 std::condition_variable reconnect_cv_;
118 std::queue<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_);
119
120 DISALLOW_COPY_AND_ASSIGN(ReconnectHandler);
121};
122
123void ReconnectHandler::Start() {
124 check_main_thread();
125 handler_thread_ = std::thread(&ReconnectHandler::Run, this);
126}
127
128void ReconnectHandler::Stop() {
129 check_main_thread();
130 {
131 std::lock_guard<std::mutex> lock(reconnect_mutex_);
132 running_ = false;
133 }
134 reconnect_cv_.notify_one();
135 handler_thread_.join();
136
137 // Drain the queue to free all resources.
138 std::lock_guard<std::mutex> lock(reconnect_mutex_);
139 while (!reconnect_queue_.empty()) {
140 ReconnectAttempt attempt = reconnect_queue_.front();
141 reconnect_queue_.pop();
142 remove_transport(attempt.transport);
143 }
144}
145
146void ReconnectHandler::TrackTransport(atransport* transport) {
147 check_main_thread();
148 {
149 std::lock_guard<std::mutex> lock(reconnect_mutex_);
150 if (!running_) return;
151 reconnect_queue_.emplace(ReconnectAttempt{
152 transport, std::chrono::system_clock::now() + ReconnectHandler::kDefaultTimeout,
153 ReconnectHandler::kMaxAttempts});
154 }
155 reconnect_cv_.notify_one();
156}
157
158void ReconnectHandler::Run() {
159 while (true) {
160 ReconnectAttempt attempt;
161 {
162 std::unique_lock<std::mutex> lock(reconnect_mutex_);
163 ScopedAssumeLocked assume_lock(reconnect_mutex_);
164
165 auto deadline = std::chrono::time_point<std::chrono::system_clock>::max();
166 if (!reconnect_queue_.empty()) deadline = reconnect_queue_.front().deadline;
167 reconnect_cv_.wait_until(lock, deadline, [&]() REQUIRES(reconnect_mutex_) {
168 return !running_ ||
169 (!reconnect_queue_.empty() && reconnect_queue_.front().deadline < deadline);
170 });
171
172 if (!running_) return;
173 attempt = reconnect_queue_.front();
174 reconnect_queue_.pop();
175 if (attempt.transport->kicked()) {
176 D("transport %s was kicked. giving up on it.", attempt.transport->serial);
177 remove_transport(attempt.transport);
178 continue;
179 }
180 }
181 D("attempting to reconnect %s", attempt.transport->serial);
182
183 if (!attempt.transport->Reconnect()) {
184 D("attempting to reconnect %s failed.", attempt.transport->serial);
185 if (attempt.attempts_left == 0) {
186 D("transport %s exceeded the number of retry attempts. giving up on it.",
187 attempt.transport->serial);
188 remove_transport(attempt.transport);
189 continue;
190 }
191
192 std::lock_guard<std::mutex> lock(reconnect_mutex_);
193 reconnect_queue_.emplace(ReconnectAttempt{
194 attempt.transport,
195 std::chrono::system_clock::now() + ReconnectHandler::kDefaultTimeout,
196 attempt.attempts_left - 1});
197 continue;
198 }
199
200 D("reconnection to %s succeeded.", attempt.transport->serial);
201 register_transport(attempt.transport);
202 }
203}
204
205static auto& reconnect_handler = *new ReconnectHandler();
206
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700207} // namespace
208
Josh Gaob122b172017-08-16 16:57:01 -0700209TransportId NextTransportId() {
210 static std::atomic<TransportId> next(1);
211 return next++;
212}
213
Josh Gao0bbf69c2018-02-16 13:24:58 -0800214BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection)
215 : underlying_(std::move(connection)) {}
216
217BlockingConnectionAdapter::~BlockingConnectionAdapter() {
218 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing";
219 Stop();
220}
221
222void BlockingConnectionAdapter::Start() {
Josh Gaoc251ec52018-04-03 12:55:18 -0700223 std::lock_guard<std::mutex> lock(mutex_);
224 if (started_) {
225 LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_
226 << "): started multiple times";
227 }
228
Josh Gao0bbf69c2018-02-16 13:24:58 -0800229 read_thread_ = std::thread([this]() {
230 LOG(INFO) << this->transport_name_ << ": read thread spawning";
231 while (true) {
Josh Gao31b5be62018-03-07 16:51:08 -0800232 auto packet = std::make_unique<apacket>();
Josh Gao0bbf69c2018-02-16 13:24:58 -0800233 if (!underlying_->Read(packet.get())) {
234 PLOG(INFO) << this->transport_name_ << ": read failed";
235 break;
236 }
237 read_callback_(this, std::move(packet));
238 }
239 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); });
240 });
241
242 write_thread_ = std::thread([this]() {
243 LOG(INFO) << this->transport_name_ << ": write thread spawning";
244 while (true) {
245 std::unique_lock<std::mutex> lock(mutex_);
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700246 ScopedAssumeLocked assume_locked(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700247 cv_.wait(lock, [this]() REQUIRES(mutex_) {
248 return this->stopped_ || !this->write_queue_.empty();
249 });
250
Josh Gao0bbf69c2018-02-16 13:24:58 -0800251 if (this->stopped_) {
252 return;
253 }
254
255 std::unique_ptr<apacket> packet = std::move(this->write_queue_.front());
256 this->write_queue_.pop_front();
257 lock.unlock();
258
259 if (!this->underlying_->Write(packet.get())) {
260 break;
261 }
262 }
263 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); });
264 });
Josh Gaoc251ec52018-04-03 12:55:18 -0700265
266 started_ = true;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800267}
268
269void BlockingConnectionAdapter::Stop() {
Josh Gaoc251ec52018-04-03 12:55:18 -0700270 {
271 std::lock_guard<std::mutex> lock(mutex_);
272 if (!started_) {
273 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started";
274 return;
275 }
Josh Gao0bbf69c2018-02-16 13:24:58 -0800276
Josh Gaoc251ec52018-04-03 12:55:18 -0700277 if (stopped_) {
278 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_
279 << "): already stopped";
280 return;
281 }
282
283 stopped_ = true;
284 }
Josh Gao0bbf69c2018-02-16 13:24:58 -0800285
286 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping";
287
288 this->underlying_->Close();
Josh Gao0bbf69c2018-02-16 13:24:58 -0800289 this->cv_.notify_one();
Josh Gaoc251ec52018-04-03 12:55:18 -0700290
291 // Move the threads out into locals with the lock taken, and then unlock to let them exit.
292 std::thread read_thread;
293 std::thread write_thread;
294
295 {
296 std::lock_guard<std::mutex> lock(mutex_);
297 read_thread = std::move(read_thread_);
298 write_thread = std::move(write_thread_);
299 }
300
301 read_thread.join();
302 write_thread.join();
Josh Gao0bbf69c2018-02-16 13:24:58 -0800303
304 LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped";
305 std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); });
306}
307
308bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) {
309 {
Josh Gaoc251ec52018-04-03 12:55:18 -0700310 std::lock_guard<std::mutex> lock(this->mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800311 write_queue_.emplace_back(std::move(packet));
312 }
313
314 cv_.notify_one();
315 return true;
316}
317
Josh Gaob800d882018-01-28 20:32:46 -0800318bool FdConnection::Read(apacket* packet) {
319 if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) {
320 D("remote local: read terminated (message)");
321 return false;
322 }
323
Josh Gaof571fcb2018-02-05 18:49:10 -0800324 if (packet->msg.data_length > MAX_PAYLOAD) {
Josh Gao5caaebd2018-02-02 14:38:04 -0800325 D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length);
326 return false;
327 }
328
Josh Gaof571fcb2018-02-05 18:49:10 -0800329 packet->payload.resize(packet->msg.data_length);
330
331 if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) {
Josh Gaob800d882018-01-28 20:32:46 -0800332 D("remote local: terminated (data)");
333 return false;
334 }
335
336 return true;
337}
338
339bool FdConnection::Write(apacket* packet) {
Josh Gaof571fcb2018-02-05 18:49:10 -0800340 if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) {
Josh Gaob800d882018-01-28 20:32:46 -0800341 D("remote local: write terminated");
342 return false;
343 }
344
Josh Gaof571fcb2018-02-05 18:49:10 -0800345 if (packet->msg.data_length) {
346 if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) {
347 D("remote local: write terminated");
348 return false;
349 }
350 }
351
Josh Gaob800d882018-01-28 20:32:46 -0800352 return true;
353}
354
355void FdConnection::Close() {
356 adb_shutdown(fd_.get());
357 fd_.reset();
358}
359
Yabin Cuiaed3c612015-09-22 15:52:57 -0700360static std::string dump_packet(const char* name, const char* func, apacket* p) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800361 unsigned command = p->msg.command;
362 int len = p->msg.data_length;
363 char cmd[9];
364 char arg0[12], arg1[12];
365 int n;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100366
367 for (n = 0; n < 4; n++) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800368 int b = (command >> (n * 8)) & 255;
369 if (b < 32 || b >= 127) break;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100370 cmd[n] = (char)b;
371 }
372 if (n == 4) {
373 cmd[4] = 0;
374 } else {
375 /* There is some non-ASCII name in the command, so dump
376 * the hexadecimal value instead */
377 snprintf(cmd, sizeof cmd, "%08x", command);
378 }
379
380 if (p->msg.arg0 < 256U)
381 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
382 else
383 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
384
385 if (p->msg.arg1 < 256U)
386 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
387 else
388 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
389
Josh Gao1290fbf2016-11-22 14:32:34 -0800390 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name,
391 func, cmd, arg0, arg1, len);
Josh Gaof571fcb2018-02-05 18:49:10 -0800392 result += dump_hex(p->payload.data(), p->payload.size());
Yabin Cuiaed3c612015-09-22 15:52:57 -0700393 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100394}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100395
Josh Gao06d61d42016-10-06 13:31:44 -0700396void send_packet(apacket* p, atransport* t) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 p->msg.magic = p->msg.command ^ 0xffffffff;
Tim Murrayde471942017-12-07 11:40:00 -0800398 // compute a checksum for connection/auth packets for compatibility reasons
399 if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) {
400 p->msg.data_check = 0;
401 } else {
402 p->msg.data_check = calculate_apacket_checksum(p);
403 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404
Josh Gao0bbf69c2018-02-16 13:24:58 -0800405 VLOG(TRANSPORT) << dump_packet(t->serial, "to remote", p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406
Yi Kongaed415c2018-07-13 18:15:16 -0700407 if (t == nullptr) {
Josh Gao06d61d42016-10-06 13:31:44 -0700408 fatal("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 }
410
Josh Gao0bbf69c2018-02-16 13:24:58 -0800411 if (t->Write(p) != 0) {
412 D("%s: failed to enqueue packet, closing transport", t->serial);
413 t->Kick();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 }
415}
416
Yabin Cuif4b99282015-08-27 12:03:11 -0700417void kick_transport(atransport* t) {
Josh Gao1db71af2017-08-17 13:50:51 -0700418 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700419 // As kick_transport() can be called from threads without guarantee that t is valid,
420 // check if the transport is in transport_list first.
Josh Gaob122b172017-08-16 16:57:01 -0700421 //
422 // TODO(jmgao): WTF? Is this actually true?
Yabin Cui1f4ec192016-04-05 13:50:44 -0700423 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700424 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700425 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700426}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427
428static int transport_registration_send = -1;
429static int transport_registration_recv = -1;
Josh Gao71f775a2018-05-14 11:14:33 -0700430static fdevent* transport_registration_fde;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433
434/* this adds support required by the 'track-devices' service.
435 * this is used to send the content of "list_transport" to any
436 * number of client connections that want it through a single
437 * live TCP connection
438 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439struct device_tracker {
Josh Gao1290fbf2016-11-22 14:32:34 -0800440 asocket socket;
Josh Gaoe0361d12018-02-12 17:24:00 -0800441 bool update_needed = false;
442 bool long_output = false;
443 device_tracker* next = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444};
445
446/* linked list of all device trackers */
Josh Gao1290fbf2016-11-22 14:32:34 -0800447static device_tracker* device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Josh Gao1290fbf2016-11-22 14:32:34 -0800449static void device_tracker_remove(device_tracker* tracker) {
450 device_tracker** pnode = &device_tracker_list;
451 device_tracker* node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
Josh Gao1db71af2017-08-17 13:50:51 -0700453 std::lock_guard<std::recursive_mutex> lock(transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 while (node) {
455 if (node == tracker) {
456 *pnode = node->next;
457 break;
458 }
459 pnode = &node->next;
Josh Gao1290fbf2016-11-22 14:32:34 -0800460 node = *pnode;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462}
463
Josh Gao1290fbf2016-11-22 14:32:34 -0800464static void device_tracker_close(asocket* socket) {
465 device_tracker* tracker = (device_tracker*)socket;
466 asocket* peer = socket->peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467
Josh Gao1290fbf2016-11-22 14:32:34 -0800468 D("device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 if (peer) {
Yi Kongaed415c2018-07-13 18:15:16 -0700470 peer->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 peer->close(peer);
472 }
473 device_tracker_remove(tracker);
Josh Gaoe0361d12018-02-12 17:24:00 -0800474 delete tracker;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475}
476
Josh Gao1ce99572018-03-07 16:52:28 -0800477static int device_tracker_enqueue(asocket* socket, apacket::payload_type) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 /* you can't read from a device tracker, close immediately */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 device_tracker_close(socket);
480 return -1;
481}
482
Elliott Hughese67f1f82015-04-30 17:32:03 -0700483static int device_tracker_send(device_tracker* tracker, const std::string& string) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700484 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485
Josh Gao1ce99572018-03-07 16:52:28 -0800486 apacket::payload_type data;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800487 data.resize(4 + string.size());
488 char buf[5];
489 snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size()));
490 memcpy(&data[0], buf, 4);
491 memcpy(&data[4], string.data(), string.size());
492 return peer->enqueue(peer, std::move(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493}
494
Elliott Hughese67f1f82015-04-30 17:32:03 -0700495static void device_tracker_ready(asocket* socket) {
496 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497
Elliott Hughese67f1f82015-04-30 17:32:03 -0700498 // We want to send the device list when the tracker connects
499 // for the first time, even if no update occurred.
Josh Gaob0c18022017-08-14 18:57:54 -0700500 if (tracker->update_needed) {
501 tracker->update_needed = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502
Josh Gaob0c18022017-08-14 18:57:54 -0700503 std::string transports = list_transports(tracker->long_output);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700504 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 }
506}
507
Josh Gaob0c18022017-08-14 18:57:54 -0700508asocket* create_device_tracker(bool long_output) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800509 device_tracker* tracker = new device_tracker();
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700510 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511
Josh Gao1290fbf2016-11-22 14:32:34 -0800512 D("device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513
514 tracker->socket.enqueue = device_tracker_enqueue;
Josh Gao1290fbf2016-11-22 14:32:34 -0800515 tracker->socket.ready = device_tracker_ready;
516 tracker->socket.close = device_tracker_close;
Josh Gaob0c18022017-08-14 18:57:54 -0700517 tracker->update_needed = true;
518 tracker->long_output = long_output;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519
Josh Gao1290fbf2016-11-22 14:32:34 -0800520 tracker->next = device_tracker_list;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 device_tracker_list = tracker;
522
523 return &tracker->socket;
524}
525
Josh Gaofd713e52017-05-03 22:37:10 -0700526// Check if all of the USB transports are connected.
527bool iterate_transports(std::function<bool(const atransport*)> fn) {
Josh Gao1db71af2017-08-17 13:50:51 -0700528 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaofd713e52017-05-03 22:37:10 -0700529 for (const auto& t : transport_list) {
530 if (!fn(t)) {
531 return false;
532 }
533 }
534 for (const auto& t : pending_list) {
535 if (!fn(t)) {
536 return false;
537 }
538 }
539 return true;
540}
541
Elliott Hughese67f1f82015-04-30 17:32:03 -0700542// Call this function each time the transport list has changed.
543void update_transports() {
Josh Gaofd713e52017-05-03 22:37:10 -0700544 update_transport_status();
545
546 // Notify `adb track-devices` clients.
Elliott Hughese67f1f82015-04-30 17:32:03 -0700547 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
Elliott Hughese67f1f82015-04-30 17:32:03 -0700549 device_tracker* tracker = device_tracker_list;
550 while (tracker != nullptr) {
551 device_tracker* next = tracker->next;
552 // This may destroy the tracker if the connection is closed.
553 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 tracker = next;
555 }
556}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700559
560void update_transports() {
561 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700563
Josh Gao1290fbf2016-11-22 14:32:34 -0800564#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565
Josh Gao1290fbf2016-11-22 14:32:34 -0800566struct tmsg {
567 atransport* transport;
568 int action;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800569};
570
Josh Gao1290fbf2016-11-22 14:32:34 -0800571static int transport_read_action(int fd, struct tmsg* m) {
572 char* p = (char*)m;
573 int len = sizeof(*m);
574 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575
Josh Gao1290fbf2016-11-22 14:32:34 -0800576 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 r = adb_read(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800578 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800580 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700582 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 return -1;
584 }
585 }
586 return 0;
587}
588
Josh Gao1290fbf2016-11-22 14:32:34 -0800589static int transport_write_action(int fd, struct tmsg* m) {
590 char* p = (char*)m;
591 int len = sizeof(*m);
592 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593
Josh Gao1290fbf2016-11-22 14:32:34 -0800594 while (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 r = adb_write(fd, p, len);
Josh Gao1290fbf2016-11-22 14:32:34 -0800596 if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 len -= r;
Josh Gao1290fbf2016-11-22 14:32:34 -0800598 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700600 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601 return -1;
602 }
603 }
604 return 0;
605}
606
Josh Gao0bbf69c2018-02-16 13:24:58 -0800607static void transport_registration_func(int _fd, unsigned ev, void*) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 tmsg m;
Josh Gao1290fbf2016-11-22 14:32:34 -0800609 atransport* t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610
Josh Gao1290fbf2016-11-22 14:32:34 -0800611 if (!(ev & FDE_READ)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 return;
613 }
614
Josh Gao1290fbf2016-11-22 14:32:34 -0800615 if (transport_read_action(_fd, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 fatal_errno("cannot read transport registration socket");
617 }
618
619 t = m.transport;
620
Dan Albert1792c232015-05-18 13:06:53 -0700621 if (m.action == 0) {
Josh Gao0bbf69c2018-02-16 13:24:58 -0800622 D("transport: %s deleting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623
Josh Gao0cd3ae12016-09-21 12:37:10 -0700624 {
Josh Gao1db71af2017-08-17 13:50:51 -0700625 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700626 transport_list.remove(t);
627 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628
Josh Gao1290fbf2016-11-22 14:32:34 -0800629 if (t->product) free(t->product);
630 if (t->serial) free(t->serial);
631 if (t->model) free(t->model);
632 if (t->device) free(t->device);
633 if (t->devpath) free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634
Dan Albertc7915a32015-05-18 16:46:31 -0700635 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636
637 update_transports();
638 return;
639 }
640
Mike Lockwood0927bf92009-08-08 12:37:44 -0400641 /* don't create transport threads for inaccessible devices */
Yabin Cuib5e11412017-03-10 16:01:01 -0800642 if (t->GetConnectionState() != kCsNoPerm) {
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700643 // The connection gets a reference to the atransport. It will release it
644 // upon a read/write error.
645 t->ref_count++;
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700646 t->connection()->SetTransportName(t->serial_name());
647 t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) {
Josh Gao0bbf69c2018-02-16 13:24:58 -0800648 if (!check_header(p.get(), t)) {
649 D("%s: remote read: bad header", t->serial);
650 return false;
651 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652
Josh Gao0bbf69c2018-02-16 13:24:58 -0800653 VLOG(TRANSPORT) << dump_packet(t->serial, "from remote", p.get());
654 apacket* packet = p.release();
Mike Lockwood0927bf92009-08-08 12:37:44 -0400655
Josh Gao0bbf69c2018-02-16 13:24:58 -0800656 // TODO: Does this need to run on the main thread?
657 fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); });
658 return true;
659 });
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700660 t->connection()->SetErrorCallback([t](Connection*, const std::string& error) {
Josh Gao0bbf69c2018-02-16 13:24:58 -0800661 D("%s: connection terminated: %s", t->serial, error.c_str());
662 fdevent_run_on_main_thread([t]() {
663 handle_offline(t);
664 transport_unref(t);
665 });
666 });
Mike Lockwood0927bf92009-08-08 12:37:44 -0400667
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700668 t->connection()->Start();
Josh Gao0bbf69c2018-02-16 13:24:58 -0800669#if ADB_HOST
670 send_connect(t);
671#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 }
673
Josh Gao0cd3ae12016-09-21 12:37:10 -0700674 {
Josh Gao1db71af2017-08-17 13:50:51 -0700675 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700676 auto it = std::find(pending_list.begin(), pending_list.end(), t);
677 if (it != pending_list.end()) {
678 pending_list.remove(t);
679 transport_list.push_front(t);
680 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700681 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 update_transports();
684}
685
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700686void init_reconnect_handler(void) {
687 reconnect_handler.Start();
688}
689
Josh Gao1290fbf2016-11-22 14:32:34 -0800690void init_transport_registration(void) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 int s[2];
692
Josh Gao1290fbf2016-11-22 14:32:34 -0800693 if (adb_socketpair(s)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 fatal_errno("cannot open transport registration socketpair");
695 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700696 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697
698 transport_registration_send = s[0];
699 transport_registration_recv = s[1];
700
Josh Gao71f775a2018-05-14 11:14:33 -0700701 transport_registration_fde =
Yi Kongaed415c2018-07-13 18:15:16 -0700702 fdevent_create(transport_registration_recv, transport_registration_func, nullptr);
Josh Gao71f775a2018-05-14 11:14:33 -0700703 fdevent_set(transport_registration_fde, FDE_READ);
Josh Gao01b7bc42017-05-09 13:43:35 -0700704}
705
706void kick_all_transports() {
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700707 reconnect_handler.Stop();
Josh Gao01b7bc42017-05-09 13:43:35 -0700708 // To avoid only writing part of a packet to a transport after exit, kick all transports.
Josh Gao1db71af2017-08-17 13:50:51 -0700709 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao01b7bc42017-05-09 13:43:35 -0700710 for (auto t : transport_list) {
711 t->Kick();
712 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713}
714
715/* the fdevent select pump is single threaded */
Josh Gao1290fbf2016-11-22 14:32:34 -0800716static void register_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 tmsg m;
718 m.transport = transport;
719 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700720 D("transport: %s registered", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800721 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 fatal_errno("cannot write transport registration socket\n");
723 }
724}
725
Josh Gao1290fbf2016-11-22 14:32:34 -0800726static void remove_transport(atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727 tmsg m;
728 m.transport = transport;
729 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700730 D("transport: %s removed", transport->serial);
Josh Gao1290fbf2016-11-22 14:32:34 -0800731 if (transport_write_action(transport_registration_send, &m)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732 fatal_errno("cannot write transport registration socket\n");
733 }
734}
735
Yabin Cuif4b99282015-08-27 12:03:11 -0700736static void transport_unref(atransport* t) {
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700737 check_main_thread();
Yabin Cuif4b99282015-08-27 12:03:11 -0700738 CHECK(t != nullptr);
Josh Gao0cd3ae12016-09-21 12:37:10 -0700739
Josh Gaoe48ecce2017-09-13 13:40:57 -0700740 std::lock_guard<std::recursive_mutex> lock(transport_lock);
741 CHECK_GT(t->ref_count, 0u);
742 t->ref_count--;
743 if (t->ref_count == 0) {
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700744 t->connection()->Stop();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700745 if (t->IsTcpDevice() && !t->kicked()) {
746 D("transport: %s unref (attempting reconnection) %d", t->serial, t->kicked());
747 reconnect_handler.TrackTransport(t);
748 } else {
749 D("transport: %s unref (kicking and closing)", t->serial);
750 remove_transport(t);
751 }
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100752 } else {
Josh Gaoe48ecce2017-09-13 13:40:57 -0700753 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400754 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755}
756
Josh Gao1290fbf2016-11-22 14:32:34 -0800757static int qual_match(const char* to_test, const char* prefix, const char* qual,
758 bool sanitize_qual) {
759 if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700760 return !qual || !*qual;
761
Josh Gao1290fbf2016-11-22 14:32:34 -0800762 if (!qual) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700763
764 if (prefix) {
765 while (*prefix) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800766 if (*prefix++ != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700767 }
768 }
769
770 while (*qual) {
771 char ch = *qual++;
Josh Gao1290fbf2016-11-22 14:32:34 -0800772 if (sanitize_qual && !isalnum(ch)) ch = '_';
773 if (ch != *to_test++) return 0;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700774 }
775
776 /* Everything matched so far. Return true if *to_test is a NUL. */
777 return !*to_test;
778}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779
Josh Gaob122b172017-08-16 16:57:01 -0700780atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
781 bool* is_ambiguous, std::string* error_out,
782 bool accept_any_state) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700783 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784
Josh Gaob122b172017-08-16 16:57:01 -0700785 if (transport_id != 0) {
786 *error_out =
787 android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id);
788 } else if (serial) {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700789 *error_out = android::base::StringPrintf("device '%s' not found", serial);
790 } else if (type == kTransportLocal) {
791 *error_out = "no emulators found";
792 } else if (type == kTransportAny) {
793 *error_out = "no devices/emulators found";
794 } else {
795 *error_out = "no devices found";
796 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797
Josh Gao1db71af2017-08-17 13:50:51 -0700798 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700799 for (const auto& t : transport_list) {
Yabin Cuib5e11412017-03-10 16:01:01 -0800800 if (t->GetConnectionState() == kCsNoPerm) {
David Purselld2acbd12015-12-02 15:14:31 -0800801 *error_out = UsbNoPermissionsLongHelpText();
Mike Lockwood37d31112009-08-08 13:53:16 -0400802 continue;
803 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400804
Josh Gaob122b172017-08-16 16:57:01 -0700805 if (transport_id) {
806 if (t->id == transport_id) {
807 result = t;
808 break;
809 }
810 } else if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800811 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700812 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700813 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700814 if (is_ambiguous) *is_ambiguous = true;
815 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700816 break;
817 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700819 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700821 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700823 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700824 if (is_ambiguous) *is_ambiguous = true;
825 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 break;
827 }
828 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700829 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700831 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700832 if (is_ambiguous) *is_ambiguous = true;
833 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800834 break;
835 }
836 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700837 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700839 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700840 if (is_ambiguous) *is_ambiguous = true;
841 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 break;
843 }
844 result = t;
845 }
846 }
847 }
Josh Gao0cd3ae12016-09-21 12:37:10 -0700848 lock.unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849
Josh Gao704494b2018-05-04 16:04:49 -0700850 if (result && !accept_any_state) {
851 // The caller requires an active transport.
852 // Make sure that we're actually connected.
853 ConnectionState state = result->GetConnectionState();
854 switch (state) {
855 case kCsConnecting:
856 *error_out = "device still connecting";
857 result = nullptr;
858 break;
Benoit Goby77e8e582013-01-15 12:36:47 -0800859
Josh Gao704494b2018-05-04 16:04:49 -0700860 case kCsAuthorizing:
861 *error_out = "device still authorizing";
862 result = nullptr;
863 break;
864
865 case kCsUnauthorized: {
866 *error_out = "device unauthorized.\n";
867 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
868 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
869 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
870 *error_out += "\n";
871 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
872 *error_out += "Otherwise check for a confirmation dialog on your device.";
873 result = nullptr;
874 break;
875 }
876
877 case kCsOffline:
878 *error_out = "device offline";
879 result = nullptr;
880 break;
881
882 default:
883 break;
884 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 }
886
887 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700888 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800889 }
890
891 return result;
892}
893
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700894bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) {
895 std::unique_lock<std::mutex> lock(mutex_);
896 ScopedAssumeLocked assume_locked(mutex_);
897 return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) {
898 return connection_established_ready_;
899 }) && connection_established_;
900}
901
902void ConnectionWaitable::SetConnectionEstablished(bool success) {
903 {
904 std::lock_guard<std::mutex> lock(mutex_);
905 if (connection_established_ready_) return;
906 connection_established_ready_ = true;
907 connection_established_ = success;
908 D("connection established with %d", success);
909 }
910 cv_.notify_one();
911}
912
913atransport::~atransport() {
914 // If the connection callback had not been run before, run it now.
915 SetConnectionEstablished(false);
916}
917
Yabin Cuib5e11412017-03-10 16:01:01 -0800918int atransport::Write(apacket* p) {
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700919 return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1;
Yabin Cuib5e11412017-03-10 16:01:01 -0800920}
921
Yabin Cui7f274902016-04-18 11:22:34 -0700922void atransport::Kick() {
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700923 if (!kicked_.exchange(true)) {
924 D("kicking transport %p %s", this, this->serial);
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700925 this->connection()->Stop();
Yabin Cui7f274902016-04-18 11:22:34 -0700926 }
927}
928
Yabin Cuib5e11412017-03-10 16:01:01 -0800929ConnectionState atransport::GetConnectionState() const {
930 return connection_state_;
931}
932
933void atransport::SetConnectionState(ConnectionState state) {
934 check_main_thread();
935 connection_state_ = state;
936}
937
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700938void atransport::SetConnection(std::unique_ptr<Connection> connection) {
939 std::lock_guard<std::mutex> lock(mutex_);
940 connection_ = std::shared_ptr<Connection>(std::move(connection));
941}
942
Josh Gaoffbd3362018-02-28 14:44:23 -0800943std::string atransport::connection_state_name() const {
Yabin Cuib5e11412017-03-10 16:01:01 -0800944 ConnectionState state = GetConnectionState();
945 switch (state) {
Josh Gao1290fbf2016-11-22 14:32:34 -0800946 case kCsOffline:
947 return "offline";
948 case kCsBootloader:
949 return "bootloader";
950 case kCsDevice:
951 return "device";
952 case kCsHost:
953 return "host";
954 case kCsRecovery:
955 return "recovery";
956 case kCsNoPerm:
957 return UsbNoPermissionsShortHelpText();
958 case kCsSideload:
959 return "sideload";
960 case kCsUnauthorized:
961 return "unauthorized";
Josh Gao704494b2018-05-04 16:04:49 -0700962 case kCsAuthorizing:
963 return "authorizing";
964 case kCsConnecting:
965 return "connecting";
Josh Gao1290fbf2016-11-22 14:32:34 -0800966 default:
967 return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968 }
969}
970
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100971void atransport::update_version(int version, size_t payload) {
972 protocol_version = std::min(version, A_VERSION);
973 max_payload = std::min(payload, MAX_PAYLOAD);
974}
975
976int atransport::get_protocol_version() const {
977 return protocol_version;
978}
979
980size_t atransport::get_max_payload() const {
981 return max_payload;
982}
983
David Pursell4e2fd362015-09-22 10:43:08 -0700984namespace {
David Pursell0955c662015-08-31 10:42:13 -0700985
David Pursell4e2fd362015-09-22 10:43:08 -0700986constexpr char kFeatureStringDelimiter = ',';
987
988} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700989
990const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700991 // Local static allocation to avoid global non-POD variables.
992 static const FeatureSet* features = new FeatureSet{
Josh Gao1290fbf2016-11-22 14:32:34 -0800993 kFeatureShell2, kFeatureCmd, kFeatureStat2,
David Pursellbbe3d212015-09-25 08:37:13 -0700994 // Increment ADB_SERVER_VERSION whenever the feature list changes to
995 // make sure that the adb client and server features stay in sync
996 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700997 };
998
999 return *features;
1000}
1001
1002std::string FeatureSetToString(const FeatureSet& features) {
1003 return android::base::Join(features, kFeatureStringDelimiter);
1004}
1005
1006FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -07001007 if (features_string.empty()) {
1008 return FeatureSet();
1009 }
1010
Josh Gao1290fbf2016-11-22 14:32:34 -08001011 auto names = android::base::Split(features_string, {kFeatureStringDelimiter});
David Pursell4e2fd362015-09-22 10:43:08 -07001012 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -07001013}
1014
David Pursell70ef7b42015-09-30 13:35:42 -07001015bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001016 return feature_set.count(feature) > 0 && supported_features().count(feature) > 0;
David Pursell70ef7b42015-09-30 13:35:42 -07001017}
1018
Dan Albert1792c232015-05-18 13:06:53 -07001019bool atransport::has_feature(const std::string& feature) const {
1020 return features_.count(feature) > 0;
1021}
1022
David Pursell4e2fd362015-09-22 10:43:08 -07001023void atransport::SetFeatures(const std::string& features_string) {
1024 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -07001025}
1026
Yabin Cuib3298242015-08-28 15:09:44 -07001027void atransport::AddDisconnect(adisconnect* disconnect) {
1028 disconnects_.push_back(disconnect);
1029}
1030
1031void atransport::RemoveDisconnect(adisconnect* disconnect) {
1032 disconnects_.remove(disconnect);
1033}
1034
1035void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -07001036 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -07001037 disconnect->func(disconnect->opaque, this);
1038 }
1039 disconnects_.clear();
1040}
1041
David Pursell3f902aa2016-03-01 08:58:26 -08001042bool atransport::MatchesTarget(const std::string& target) const {
1043 if (serial) {
1044 if (target == serial) {
1045 return true;
1046 } else if (type == kTransportLocal) {
1047 // Local transports can match [tcp:|udp:]<hostname>[:port].
1048 const char* local_target_ptr = target.c_str();
1049
1050 // For fastboot compatibility, ignore protocol prefixes.
1051 if (android::base::StartsWith(target, "tcp:") ||
Josh Gao1290fbf2016-11-22 14:32:34 -08001052 android::base::StartsWith(target, "udp:")) {
David Pursell3f902aa2016-03-01 08:58:26 -08001053 local_target_ptr += 4;
1054 }
1055
1056 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
1057 std::string serial_host, error;
1058 int serial_port = -1;
Josh Gao1290fbf2016-11-22 14:32:34 -08001059 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) {
David Pursell3f902aa2016-03-01 08:58:26 -08001060 // |target| may omit the port to default to ours.
1061 std::string target_host;
1062 int target_port = serial_port;
1063 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
1064 nullptr, &error) &&
Josh Gao1290fbf2016-11-22 14:32:34 -08001065 serial_host == target_host && serial_port == target_port) {
David Pursell3f902aa2016-03-01 08:58:26 -08001066 return true;
1067 }
1068 }
1069 }
1070 }
1071
1072 return (devpath && target == devpath) ||
1073 qual_match(target.c_str(), "product:", product, false) ||
1074 qual_match(target.c_str(), "model:", model, true) ||
1075 qual_match(target.c_str(), "device:", device, false);
1076}
1077
Luis Hector Chavez56fe7532018-04-17 14:25:04 -07001078void atransport::SetConnectionEstablished(bool success) {
1079 connection_waitable_->SetConnectionEstablished(success);
1080}
1081
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -07001082bool atransport::Reconnect() {
1083 return reconnect_(this);
1084}
1085
Elliott Hughese67f1f82015-04-30 17:32:03 -07001086#if ADB_HOST
1087
Josh Gaob122b172017-08-16 16:57:01 -07001088// We use newline as our delimiter, make sure to never output it.
1089static std::string sanitize(std::string str, bool alphanumeric) {
1090 auto pred = alphanumeric ? [](const char c) { return !isalnum(c); }
1091 : [](const char c) { return c == '\n'; };
1092 std::replace_if(str.begin(), str.end(), pred, '_');
1093 return str;
1094}
1095
Josh Gao1290fbf2016-11-22 14:32:34 -08001096static void append_transport_info(std::string* result, const char* key, const char* value,
Josh Gaob122b172017-08-16 16:57:01 -07001097 bool alphanumeric) {
Elliott Hughese67f1f82015-04-30 17:32:03 -07001098 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001099 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001100 }
1101
Elliott Hughese67f1f82015-04-30 17:32:03 -07001102 *result += ' ';
1103 *result += key;
Josh Gaob122b172017-08-16 16:57:01 -07001104 *result += sanitize(value, alphanumeric);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001105}
1106
Josh Gao1290fbf2016-11-22 14:32:34 -08001107static void append_transport(const atransport* t, std::string* result, bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001108 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -07001109 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -07001110 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -07001111 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001112
1113 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -07001114 *result += serial;
1115 *result += '\t';
1116 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001117 } else {
Josh Gao1290fbf2016-11-22 14:32:34 -08001118 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001119
Elliott Hughese67f1f82015-04-30 17:32:03 -07001120 append_transport_info(result, "", t->devpath, false);
1121 append_transport_info(result, "product:", t->product, false);
1122 append_transport_info(result, "model:", t->model, true);
1123 append_transport_info(result, "device:", t->device, false);
Josh Gaob122b172017-08-16 16:57:01 -07001124
1125 // Put id at the end, so that anyone parsing the output here can always find it by scanning
1126 // backwards from newlines, even with hypothetical devices named 'transport_id:1'.
1127 *result += " transport_id:";
1128 *result += std::to_string(t->id);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001129 }
Elliott Hughese67f1f82015-04-30 17:32:03 -07001130 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -07001131}
1132
Elliott Hughese67f1f82015-04-30 17:32:03 -07001133std::string list_transports(bool long_listing) {
Josh Gao1db71af2017-08-17 13:50:51 -07001134 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Artem Iglikov04398a92017-12-17 10:56:07 +00001135
1136 auto sorted_transport_list = transport_list;
1137 sorted_transport_list.sort([](atransport*& x, atransport*& y) {
1138 if (x->type != y->type) {
1139 return x->type < y->type;
1140 }
1141 return strcmp(x->serial, y->serial) < 0;
1142 });
1143
1144 std::string result;
1145 for (const auto& t : sorted_transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -07001146 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001147 }
Elliott Hughese67f1f82015-04-30 17:32:03 -07001148 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149}
1150
Josh Gao22d2b3e2016-10-27 14:01:08 -07001151void close_usb_devices(std::function<bool(const atransport*)> predicate) {
Josh Gao1db71af2017-08-17 13:50:51 -07001152 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao22d2b3e2016-10-27 14:01:08 -07001153 for (auto& t : transport_list) {
1154 if (predicate(t)) {
1155 t->Kick();
1156 }
1157 }
1158}
1159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001160/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -07001161void close_usb_devices() {
Josh Gao22d2b3e2016-10-27 14:01:08 -07001162 close_usb_devices([](const atransport*) { return true; });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163}
Josh Gao1290fbf2016-11-22 14:32:34 -08001164#endif // ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001165
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -07001166int register_socket_transport(int s, const char* serial, int port, int local,
1167 atransport::ReconnectCallback reconnect) {
1168 atransport* t = new atransport(std::move(reconnect), kCsOffline);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001169
1170 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001171 char buf[32];
1172 snprintf(buf, sizeof(buf), "T-%p", t);
1173 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +01001174 }
Dan Albertc7915a32015-05-18 16:46:31 -07001175
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001176 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001177 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -07001178 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001179 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001180 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001181
Josh Gao1db71af2017-08-17 13:50:51 -07001182 std::unique_lock<std::recursive_mutex> lock(transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -07001183 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001184 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001185 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001186 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001187 delete t;
Luis Hector Chavez5d395852018-04-17 14:09:21 -07001188 return -EALREADY;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001191
Elliott Hughes65fe2512015-10-07 15:59:35 -07001192 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001193 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001194 VLOG(TRANSPORT) << "socket transport " << transport->serial
Josh Gao1290fbf2016-11-22 14:32:34 -08001195 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -07001196 delete t;
Luis Hector Chavez5d395852018-04-17 14:09:21 -07001197 return -EALREADY;
Benoit Goby1c45ee92013-03-29 18:22:36 -07001198 }
1199 }
1200
Dan Albertc7915a32015-05-18 16:46:31 -07001201 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001202 t->serial = strdup(serial);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001203
1204 lock.unlock();
Benoit Goby1c45ee92013-03-29 18:22:36 -07001205
Luis Hector Chavez56fe7532018-04-17 14:25:04 -07001206 auto waitable = t->connection_waitable();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 register_transport(t);
Luis Hector Chavez56fe7532018-04-17 14:25:04 -07001208
Luis Hector Chavezc587f022018-05-01 17:12:16 -07001209 if (local == 1) {
1210 // Do not wait for emulator transports.
1211 return 0;
1212 }
1213
Luis Hector Chavez56fe7532018-04-17 14:25:04 -07001214 return waitable->WaitForConnection(std::chrono::seconds(10)) ? 0 : -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215}
1216
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001217#if ADB_HOST
Josh Gao1290fbf2016-11-22 14:32:34 -08001218atransport* find_transport(const char* serial) {
Dan Albertc7915a32015-05-18 16:46:31 -07001219 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001220
Josh Gao1db71af2017-08-17 13:50:51 -07001221 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001222 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -07001223 if (t->serial && strcmp(serial, t->serial) == 0) {
1224 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001225 break;
1226 }
Dan Albertc7915a32015-05-18 16:46:31 -07001227 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001228
Dan Albertc7915a32015-05-18 16:46:31 -07001229 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001230}
1231
Yabin Cuif4b99282015-08-27 12:03:11 -07001232void kick_all_tcp_devices() {
Josh Gao1db71af2017-08-17 13:50:51 -07001233 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -07001234 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001235 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001236 // Kicking breaks the read_transport thread of this transport out of any read, then
1237 // the read_transport thread will notify the main thread to make this transport
1238 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001239 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001240 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001241 }
Dan Albertc7915a32015-05-18 16:46:31 -07001242 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001243}
1244
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001245#endif
1246
Josh Gao1290fbf2016-11-22 14:32:34 -08001247void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath,
1248 unsigned writeable) {
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -07001249 atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm);
Dan Albertc7915a32015-05-18 16:46:31 -07001250
Josh Gao1290fbf2016-11-22 14:32:34 -08001251 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : "");
Yabin Cuib5e11412017-03-10 16:01:01 -08001252 init_usb_transport(t, usb);
Josh Gao1290fbf2016-11-22 14:32:34 -08001253 if (serial) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254 t->serial = strdup(serial);
1255 }
Dan Albertc7915a32015-05-18 16:46:31 -07001256
1257 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001258 t->devpath = strdup(devpath);
1259 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001260
Josh Gao0cd3ae12016-09-21 12:37:10 -07001261 {
Josh Gao1db71af2017-08-17 13:50:51 -07001262 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gao0cd3ae12016-09-21 12:37:10 -07001263 pending_list.push_front(t);
1264 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001265
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001266 register_transport(t);
1267}
1268
Dan Albertdcd78a12015-05-18 16:43:57 -07001269// This should only be used for transports with connection_state == kCsNoPerm.
Josh Gao1290fbf2016-11-22 14:32:34 -08001270void unregister_usb_transport(usb_handle* usb) {
Josh Gao1db71af2017-08-17 13:50:51 -07001271 std::lock_guard<std::recursive_mutex> lock(transport_lock);
Josh Gaob800d882018-01-28 20:32:46 -08001272 transport_list.remove_if([usb](atransport* t) {
Luis Hector Chavez9a388d52018-04-25 08:56:41 -07001273 auto connection = t->connection();
1274 if (auto usb_connection = dynamic_cast<UsbConnection*>(connection.get())) {
1275 return usb_connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm;
Josh Gaob800d882018-01-28 20:32:46 -08001276 }
1277 return false;
1278 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001279}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001280
Josh Gao36dadca2017-05-16 15:02:45 -07001281bool check_header(apacket* p, atransport* t) {
Josh Gao1290fbf2016-11-22 14:32:34 -08001282 if (p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuib5e11412017-03-10 16:01:01 -08001283 VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command
1284 << ", magic = " << p->msg.magic;
Josh Gao36dadca2017-05-16 15:02:45 -07001285 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286 }
1287
Josh Gao1290fbf2016-11-22 14:32:34 -08001288 if (p->msg.data_length > t->get_max_payload()) {
1289 VLOG(RWX) << "check_header(): " << p->msg.data_length
1290 << " atransport::max_payload = " << t->get_max_payload();
Josh Gao36dadca2017-05-16 15:02:45 -07001291 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 }
1293
Josh Gao36dadca2017-05-16 15:02:45 -07001294 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001295}
1296
Josh Gao3bd28792016-10-05 19:02:29 -07001297#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -07001298std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001299 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1300
Josh Gao2e671202016-08-18 22:00:12 -07001301 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001302 keys_.pop_front();
1303 return result;
1304}
Josh Gao3bd28792016-10-05 19:02:29 -07001305#endif