blob: 7b4bb1c75b368195e10ad0605a5168d84e61d6a8 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albertb302d122015-02-24 15:51:19 -080018
Dan Albertdb6fe642015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert4895c522015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080023#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080027#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028
Spencer Low28bc2cb2015-11-07 18:51:54 -080029#include <algorithm>
Dan Albertecce5032015-05-18 16:46:31 -070030#include <list>
Josh Gaoe7daf572016-09-21 12:37:10 -070031#include <mutex>
Dan Albertecce5032015-05-18 16:46:31 -070032
Elliott Hughesf55ead92015-12-04 22:00:26 -080033#include <android-base/logging.h>
David Pursellc929c6f2016-03-01 08:58:26 -080034#include <android-base/parsenetaddress.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080035#include <android-base/stringprintf.h>
36#include <android-base/strings.h>
Elliott Hughes88b4c852015-04-30 17:32:03 -070037
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038#include "adb.h"
Elliott Hughes801066a2016-06-29 17:42:01 -070039#include "adb_auth.h"
Elliott Hughes88b4c852015-04-30 17:32:03 -070040#include "adb_utils.h"
Elliott Hughes70097492015-12-11 19:07:01 -080041#include "diagnose_usb.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042
43static void transport_unref(atransport *t);
44
Josh Gaoe3a87d02015-11-11 17:56:12 -080045static auto& transport_list = *new std::list<atransport*>();
46static auto& pending_list = *new std::list<atransport*>();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -070047
Josh Gaoe7daf572016-09-21 12:37:10 -070048static std::mutex& transport_lock = *new std::mutex();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080049
Todd Kennedyaff9c672015-11-10 00:03:25 +000050const char* const kFeatureShell2 = "shell_v2";
51const char* const kFeatureCmd = "cmd";
52
Yabin Cui19bec5b2015-09-22 15:52:57 -070053static std::string dump_packet(const char* name, const char* func, apacket* p) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +010054 unsigned command = p->msg.command;
55 int len = p->msg.data_length;
56 char cmd[9];
57 char arg0[12], arg1[12];
58 int n;
59
60 for (n = 0; n < 4; n++) {
61 int b = (command >> (n*8)) & 255;
62 if (b < 32 || b >= 127)
63 break;
64 cmd[n] = (char)b;
65 }
66 if (n == 4) {
67 cmd[4] = 0;
68 } else {
69 /* There is some non-ASCII name in the command, so dump
70 * the hexadecimal value instead */
71 snprintf(cmd, sizeof cmd, "%08x", command);
72 }
73
74 if (p->msg.arg0 < 256U)
75 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
76 else
77 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
78
79 if (p->msg.arg1 < 256U)
80 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
81 else
82 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
83
Yabin Cui19bec5b2015-09-22 15:52:57 -070084 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
85 name, func, cmd, arg0, arg1, len);
86 result += dump_hex(p->data, len);
87 return result;
David 'Digit' Turner58f59682011-01-06 14:11:07 +010088}
David 'Digit' Turner58f59682011-01-06 14:11:07 +010089
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080090static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +010091read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092{
Yabin Cuia96dabf2015-07-30 19:58:10 -070093 char buff[8];
David 'Digit' Turner58f59682011-01-06 14:11:07 +010094 if (!name) {
95 snprintf(buff, sizeof buff, "fd=%d", fd);
96 name = buff;
97 }
Yabin Cuia96dabf2015-07-30 19:58:10 -070098 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
99 int len = sizeof(apacket*);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100 while(len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700101 int r = adb_read(fd, p, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 if(r > 0) {
103 len -= r;
Yabin Cuia96dabf2015-07-30 19:58:10 -0700104 p += r;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800105 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700106 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107 return -1;
108 }
109 }
110
Yabin Cui19bec5b2015-09-22 15:52:57 -0700111 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 return 0;
113}
114
115static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100116write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800117{
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100118 char buff[8];
119 if (!name) {
120 snprintf(buff, sizeof buff, "fd=%d", fd);
121 name = buff;
122 }
Yabin Cui19bec5b2015-09-22 15:52:57 -0700123 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Yabin Cuia96dabf2015-07-30 19:58:10 -0700124 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
125 int len = sizeof(apacket*);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800126 while(len > 0) {
Yabin Cuia96dabf2015-07-30 19:58:10 -0700127 int r = adb_write(fd, p, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800128 if(r > 0) {
129 len -= r;
130 p += r;
131 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700132 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800133 return -1;
134 }
135 }
136 return 0;
137}
138
139static void transport_socket_events(int fd, unsigned events, void *_t)
140{
Dan Albertf30d73c2015-02-25 17:51:28 -0800141 atransport *t = reinterpret_cast<atransport*>(_t);
Yabin Cui815ad882015-09-02 17:44:28 -0700142 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143 if(events & FDE_READ){
144 apacket *p = 0;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100145 if(read_packet(fd, t->serial, &p)){
Yabin Cui815ad882015-09-02 17:44:28 -0700146 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147 } else {
148 handle_packet(p, (atransport *) _t);
149 }
150 }
151}
152
Josh Gao67ac3792016-10-06 13:31:44 -0700153void send_packet(apacket* p, atransport* t) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154 p->msg.magic = p->msg.command ^ 0xffffffff;
Josh Gao67ac3792016-10-06 13:31:44 -0700155 p->msg.data_check = calculate_apacket_checksum(p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800156
157 print_packet("send", p);
158
159 if (t == NULL) {
Josh Gao67ac3792016-10-06 13:31:44 -0700160 fatal("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 }
162
Josh Gao67ac3792016-10-06 13:31:44 -0700163 if (write_packet(t->transport_socket, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800164 fatal_errno("cannot enqueue packet on transport socket");
165 }
166}
167
Yabin Cui4e222292015-08-31 11:50:24 -0700168// The transport is opened by transport_register_func before
169// the read_transport and write_transport threads are started.
170//
171// The read_transport thread issues a SYNC(1, token) message to let
172// the write_transport thread know to start things up. In the event
173// of transport IO failure, the read_transport thread will post a
174// SYNC(0,0) message to ensure shutdown.
175//
176// The transport will not actually be closed until both threads exit, but the threads
177// will kick the transport on their way out to disconnect the underlying device.
178//
179// read_transport thread reads data from a transport (representing a usb/tcp connection),
180// and makes the main thread call handle_packet().
Josh Gao7d405252016-02-12 14:31:15 -0800181static void read_transport_thread(void* _t) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800182 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183 apacket *p;
184
Yabin Cui4e222292015-08-31 11:50:24 -0700185 adb_thread_setname(android::base::StringPrintf("<-%s",
186 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui815ad882015-09-02 17:44:28 -0700187 D("%s: starting read_transport thread on fd %d, SYNC online (%d)",
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100188 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189 p = get_apacket();
190 p->msg.command = A_SYNC;
191 p->msg.arg0 = 1;
192 p->msg.arg1 = ++(t->sync_token);
193 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100194 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800195 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700196 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197 goto oops;
198 }
199
Yabin Cui815ad882015-09-02 17:44:28 -0700200 D("%s: data pump started", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201 for(;;) {
202 p = get_apacket();
203
204 if(t->read_from_remote(p, t) == 0){
Yabin Cui815ad882015-09-02 17:44:28 -0700205 D("%s: received remote packet, sending to transport",
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100206 t->serial);
207 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700209 D("%s: failed to write apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210 goto oops;
211 }
212 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700213 D("%s: remote read failed for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214 put_apacket(p);
215 break;
216 }
217 }
218
Yabin Cui815ad882015-09-02 17:44:28 -0700219 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800220 p = get_apacket();
221 p->msg.command = A_SYNC;
222 p->msg.arg0 = 0;
223 p->msg.arg1 = 0;
224 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100225 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 put_apacket(p);
Yabin Cui815ad882015-09-02 17:44:28 -0700227 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800228 }
229
230oops:
Yabin Cui815ad882015-09-02 17:44:28 -0700231 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232 kick_transport(t);
233 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800234}
235
Yabin Cui4e222292015-08-31 11:50:24 -0700236// write_transport thread gets packets sent by the main thread (through send_packet()),
237// and writes to a transport (representing a usb/tcp connection).
Josh Gao7d405252016-02-12 14:31:15 -0800238static void write_transport_thread(void* _t) {
Dan Albertf30d73c2015-02-25 17:51:28 -0800239 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 apacket *p;
241 int active = 0;
242
Yabin Cui4e222292015-08-31 11:50:24 -0700243 adb_thread_setname(android::base::StringPrintf("->%s",
244 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui815ad882015-09-02 17:44:28 -0700245 D("%s: starting write_transport thread, reading from fd %d",
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100246 t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247
248 for(;;){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100249 if(read_packet(t->fd, t->serial, &p)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700250 D("%s: failed to read apacket from transport on fd %d",
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100251 t->serial, t->fd );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252 break;
253 }
254 if(p->msg.command == A_SYNC){
255 if(p->msg.arg0 == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700256 D("%s: transport SYNC offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800257 put_apacket(p);
258 break;
259 } else {
260 if(p->msg.arg1 == t->sync_token) {
Yabin Cui815ad882015-09-02 17:44:28 -0700261 D("%s: transport SYNC online", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262 active = 1;
263 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700264 D("%s: transport ignoring SYNC %d != %d",
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100265 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800266 }
267 }
268 } else {
269 if(active) {
Yabin Cui815ad882015-09-02 17:44:28 -0700270 D("%s: transport got packet, sending to remote", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271 t->write_to_remote(p, t);
272 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700273 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800274 }
275 }
276
277 put_apacket(p);
278 }
279
Yabin Cui815ad882015-09-02 17:44:28 -0700280 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800281 kick_transport(t);
282 transport_unref(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800283}
284
Yabin Cui4d64fd82015-08-27 12:03:11 -0700285void kick_transport(atransport* t) {
Josh Gaoe7daf572016-09-21 12:37:10 -0700286 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cuid78ed222016-04-05 13:50:44 -0700287 // As kick_transport() can be called from threads without guarantee that t is valid,
288 // check if the transport is in transport_list first.
289 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700290 t->Kick();
Yabin Cuid78ed222016-04-05 13:50:44 -0700291 }
Yabin Cui4d64fd82015-08-27 12:03:11 -0700292}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293
294static int transport_registration_send = -1;
295static int transport_registration_recv = -1;
296static fdevent transport_registration_fde;
297
298
299#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300
301/* this adds support required by the 'track-devices' service.
302 * this is used to send the content of "list_transport" to any
303 * number of client connections that want it through a single
304 * live TCP connection
305 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306struct device_tracker {
307 asocket socket;
308 int update_needed;
309 device_tracker* next;
310};
311
312/* linked list of all device trackers */
313static device_tracker* device_tracker_list;
314
315static void
316device_tracker_remove( device_tracker* tracker )
317{
318 device_tracker** pnode = &device_tracker_list;
319 device_tracker* node = *pnode;
320
Josh Gaoe7daf572016-09-21 12:37:10 -0700321 std::lock_guard<std::mutex> lock(transport_lock);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322 while (node) {
323 if (node == tracker) {
324 *pnode = node->next;
325 break;
326 }
327 pnode = &node->next;
328 node = *pnode;
329 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330}
331
332static void
333device_tracker_close( asocket* socket )
334{
335 device_tracker* tracker = (device_tracker*) socket;
336 asocket* peer = socket->peer;
337
Yabin Cui815ad882015-09-02 17:44:28 -0700338 D( "device tracker %p removed", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339 if (peer) {
340 peer->peer = NULL;
341 peer->close(peer);
342 }
343 device_tracker_remove(tracker);
344 free(tracker);
345}
346
347static int
348device_tracker_enqueue( asocket* socket, apacket* p )
349{
350 /* you can't read from a device tracker, close immediately */
351 put_apacket(p);
352 device_tracker_close(socket);
353 return -1;
354}
355
Elliott Hughes88b4c852015-04-30 17:32:03 -0700356static int device_tracker_send(device_tracker* tracker, const std::string& string) {
357 apacket* p = get_apacket();
358 asocket* peer = tracker->socket.peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800359
Elliott Hughes88b4c852015-04-30 17:32:03 -0700360 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
361 memcpy(&p->data[4], string.data(), string.size());
362 p->len = 4 + string.size();
363 return peer->enqueue(peer, p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800364}
365
Elliott Hughes88b4c852015-04-30 17:32:03 -0700366static void device_tracker_ready(asocket* socket) {
367 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368
Elliott Hughes88b4c852015-04-30 17:32:03 -0700369 // We want to send the device list when the tracker connects
370 // for the first time, even if no update occurred.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800371 if (tracker->update_needed > 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800372 tracker->update_needed = 0;
373
Elliott Hughes88b4c852015-04-30 17:32:03 -0700374 std::string transports = list_transports(false);
375 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800376 }
377}
378
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379asocket*
380create_device_tracker(void)
381{
Elliott Hughesd0269c92015-04-21 19:39:52 -0700382 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
383 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384
Yabin Cui815ad882015-09-02 17:44:28 -0700385 D( "device tracker %p created", tracker);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800386
387 tracker->socket.enqueue = device_tracker_enqueue;
388 tracker->socket.ready = device_tracker_ready;
389 tracker->socket.close = device_tracker_close;
390 tracker->update_needed = 1;
391
392 tracker->next = device_tracker_list;
393 device_tracker_list = tracker;
394
395 return &tracker->socket;
396}
397
398
Elliott Hughes88b4c852015-04-30 17:32:03 -0700399// Call this function each time the transport list has changed.
400void update_transports() {
401 std::string transports = list_transports(false);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402
Elliott Hughes88b4c852015-04-30 17:32:03 -0700403 device_tracker* tracker = device_tracker_list;
404 while (tracker != nullptr) {
405 device_tracker* next = tracker->next;
406 // This may destroy the tracker if the connection is closed.
407 device_tracker_send(tracker, transports);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408 tracker = next;
409 }
410}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700411
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800412#else
Elliott Hughes88b4c852015-04-30 17:32:03 -0700413
414void update_transports() {
415 // Nothing to do on the device side.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416}
Elliott Hughes88b4c852015-04-30 17:32:03 -0700417
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800418#endif // ADB_HOST
419
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800420struct tmsg
421{
422 atransport *transport;
423 int action;
424};
425
426static int
427transport_read_action(int fd, struct tmsg* m)
428{
429 char *p = (char*)m;
430 int len = sizeof(*m);
431 int r;
432
433 while(len > 0) {
434 r = adb_read(fd, p, len);
435 if(r > 0) {
436 len -= r;
437 p += r;
438 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700439 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440 return -1;
441 }
442 }
443 return 0;
444}
445
446static int
447transport_write_action(int fd, struct tmsg* m)
448{
449 char *p = (char*)m;
450 int len = sizeof(*m);
451 int r;
452
453 while(len > 0) {
454 r = adb_write(fd, p, len);
455 if(r > 0) {
456 len -= r;
457 p += r;
458 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700459 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800460 return -1;
461 }
462 }
463 return 0;
464}
465
466static void transport_registration_func(int _fd, unsigned ev, void *data)
467{
468 tmsg m;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800469 int s[2];
470 atransport *t;
471
472 if(!(ev & FDE_READ)) {
473 return;
474 }
475
476 if(transport_read_action(_fd, &m)) {
477 fatal_errno("cannot read transport registration socket");
478 }
479
480 t = m.transport;
481
Dan Albertbe8e54b2015-05-18 13:06:53 -0700482 if (m.action == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700483 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800484
485 /* IMPORTANT: the remove closes one half of the
486 ** socket pair. The close closes the other half.
487 */
488 fdevent_remove(&(t->transport_fde));
489 adb_close(t->fd);
490
Josh Gaoe7daf572016-09-21 12:37:10 -0700491 {
492 std::lock_guard<std::mutex> lock(transport_lock);
493 transport_list.remove(t);
494 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800495
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496 if (t->product)
497 free(t->product);
498 if (t->serial)
499 free(t->serial);
Scott Andersonfa020922012-05-25 14:10:02 -0700500 if (t->model)
501 free(t->model);
502 if (t->device)
503 free(t->device);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700504 if (t->devpath)
505 free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800506
Dan Albertecce5032015-05-18 16:46:31 -0700507 delete t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800508
509 update_transports();
510 return;
511 }
512
Mike Lockwoode45583f2009-08-08 12:37:44 -0400513 /* don't create transport threads for inaccessible devices */
Dan Albert9a50f4c2015-05-18 16:43:57 -0700514 if (t->connection_state != kCsNoPerm) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800515 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400516 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800517
Dan Albertecce5032015-05-18 16:46:31 -0700518 if (adb_socketpair(s)) {
Mike Lockwoode45583f2009-08-08 12:37:44 -0400519 fatal_errno("cannot open transport socketpair");
520 }
521
Yabin Cui815ad882015-09-02 17:44:28 -0700522 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400523
524 t->transport_socket = s[0];
525 t->fd = s[1];
526
Mike Lockwoode45583f2009-08-08 12:37:44 -0400527 fdevent_install(&(t->transport_fde),
528 t->transport_socket,
529 transport_socket_events,
530 t);
531
532 fdevent_set(&(t->transport_fde), FDE_READ);
533
Yabin Cui4e222292015-08-31 11:50:24 -0700534 if (!adb_thread_create(write_transport_thread, t)) {
535 fatal_errno("cannot create write_transport thread");
Mike Lockwoode45583f2009-08-08 12:37:44 -0400536 }
537
Yabin Cui4e222292015-08-31 11:50:24 -0700538 if (!adb_thread_create(read_transport_thread, t)) {
539 fatal_errno("cannot create read_transport thread");
Mike Lockwoode45583f2009-08-08 12:37:44 -0400540 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541 }
542
Josh Gaoe7daf572016-09-21 12:37:10 -0700543 {
544 std::lock_guard<std::mutex> lock(transport_lock);
545 pending_list.remove(t);
546 transport_list.push_front(t);
547 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800548
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800549 update_transports();
550}
551
552void init_transport_registration(void)
553{
554 int s[2];
555
556 if(adb_socketpair(s)){
557 fatal_errno("cannot open transport registration socketpair");
558 }
Yabin Cui815ad882015-09-02 17:44:28 -0700559 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800560
561 transport_registration_send = s[0];
562 transport_registration_recv = s[1];
563
564 fdevent_install(&transport_registration_fde,
565 transport_registration_recv,
566 transport_registration_func,
567 0);
568
569 fdevent_set(&transport_registration_fde, FDE_READ);
570}
571
572/* the fdevent select pump is single threaded */
573static void register_transport(atransport *transport)
574{
575 tmsg m;
576 m.transport = transport;
577 m.action = 1;
Yabin Cui815ad882015-09-02 17:44:28 -0700578 D("transport: %s registered", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800579 if(transport_write_action(transport_registration_send, &m)) {
580 fatal_errno("cannot write transport registration socket\n");
581 }
582}
583
584static void remove_transport(atransport *transport)
585{
586 tmsg m;
587 m.transport = transport;
588 m.action = 0;
Yabin Cui815ad882015-09-02 17:44:28 -0700589 D("transport: %s removed", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800590 if(transport_write_action(transport_registration_send, &m)) {
591 fatal_errno("cannot write transport registration socket\n");
592 }
593}
594
595
Yabin Cui4d64fd82015-08-27 12:03:11 -0700596static void transport_unref(atransport* t) {
597 CHECK(t != nullptr);
Josh Gaoe7daf572016-09-21 12:37:10 -0700598
599 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700600 CHECK_GT(t->ref_count, 0u);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400601 t->ref_count--;
Mike Lockwood01c2c302010-05-24 10:44:35 -0400602 if (t->ref_count == 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700603 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400604 t->close(t);
605 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100606 } else {
Yabin Cui815ad882015-09-02 17:44:28 -0700607 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400608 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800609}
610
Scott Anderson27042382012-05-30 18:11:27 -0700611static int qual_match(const char *to_test,
Elliott Hughesabfa7202015-04-03 16:12:15 -0700612 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson27042382012-05-30 18:11:27 -0700613{
614 if (!to_test || !*to_test)
615 /* Return true if both the qual and to_test are null strings. */
616 return !qual || !*qual;
617
618 if (!qual)
619 return 0;
620
621 if (prefix) {
622 while (*prefix) {
623 if (*prefix++ != *to_test++)
624 return 0;
625 }
626 }
627
628 while (*qual) {
629 char ch = *qual++;
Elliott Hughesabfa7202015-04-03 16:12:15 -0700630 if (sanitize_qual && !isalnum(ch))
Scott Anderson27042382012-05-30 18:11:27 -0700631 ch = '_';
632 if (ch != *to_test++)
633 return 0;
634 }
635
636 /* Everything matched so far. Return true if *to_test is a NUL. */
637 return !*to_test;
638}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800639
Elliott Hughes67943d12015-10-07 14:55:10 -0700640atransport* acquire_one_transport(TransportType type, const char* serial,
641 bool* is_ambiguous, std::string* error_out) {
642 atransport* result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800643
Elliott Hughes67943d12015-10-07 14:55:10 -0700644 if (serial) {
645 *error_out = android::base::StringPrintf("device '%s' not found", serial);
646 } else if (type == kTransportLocal) {
647 *error_out = "no emulators found";
648 } else if (type == kTransportAny) {
649 *error_out = "no devices/emulators found";
650 } else {
651 *error_out = "no devices found";
652 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800653
Josh Gaoe7daf572016-09-21 12:37:10 -0700654 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes67943d12015-10-07 14:55:10 -0700655 for (const auto& t : transport_list) {
Dan Albert9a50f4c2015-05-18 16:43:57 -0700656 if (t->connection_state == kCsNoPerm) {
Elliott Hughes70097492015-12-11 19:07:01 -0800657#if ADB_HOST
David Pursell6e5c7eb2015-12-02 15:14:31 -0800658 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes70097492015-12-11 19:07:01 -0800659#endif
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400660 continue;
661 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400662
Elliott Hughes67943d12015-10-07 14:55:10 -0700663 // Check for matching serial number.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800664 if (serial) {
David Pursellc929c6f2016-03-01 08:58:26 -0800665 if (t->MatchesTarget(serial)) {
Scott Anderson27042382012-05-30 18:11:27 -0700666 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700667 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700668 if (is_ambiguous) *is_ambiguous = true;
669 result = nullptr;
Scott Anderson27042382012-05-30 18:11:27 -0700670 break;
671 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700673 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800674 } else {
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700675 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800676 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700677 *error_out = "more than one device";
Elliott Hughes67943d12015-10-07 14:55:10 -0700678 if (is_ambiguous) *is_ambiguous = true;
679 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800680 break;
681 }
682 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700683 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800684 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700685 *error_out = "more than one emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700686 if (is_ambiguous) *is_ambiguous = true;
687 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800688 break;
689 }
690 result = t;
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700691 } else if (type == kTransportAny) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800692 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700693 *error_out = "more than one device/emulator";
Elliott Hughes67943d12015-10-07 14:55:10 -0700694 if (is_ambiguous) *is_ambiguous = true;
695 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800696 break;
697 }
698 result = t;
699 }
700 }
701 }
Josh Gaoe7daf572016-09-21 12:37:10 -0700702 lock.unlock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800703
Elliott Hughes67943d12015-10-07 14:55:10 -0700704 // Don't return unauthorized devices; the caller can't do anything with them.
705 if (result && result->connection_state == kCsUnauthorized) {
706 *error_out = "device unauthorized.\n";
707 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
708 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
709 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
710 *error_out += "\n";
711 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
712 *error_out += "Otherwise check for a confirmation dialog on your device.";
713 result = nullptr;
714 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700715
Elliott Hughes67943d12015-10-07 14:55:10 -0700716 // Don't return offline devices; the caller can't do anything with them.
717 if (result && result->connection_state == kCsOffline) {
718 *error_out = "device offline";
719 result = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800720 }
721
722 if (result) {
Elliott Hughes24f52762015-06-23 13:00:32 -0700723 *error_out = "success";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800724 }
725
726 return result;
727}
728
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700729void atransport::Kick() {
730 if (!kicked_) {
731 kicked_ = true;
732 CHECK(kick_func_ != nullptr);
733 kick_func_(this);
734 }
735}
736
David Pursell6e5c7eb2015-12-02 15:14:31 -0800737const std::string atransport::connection_state_name() const {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700738 switch (connection_state) {
David Pursell6e5c7eb2015-12-02 15:14:31 -0800739 case kCsOffline: return "offline";
740 case kCsBootloader: return "bootloader";
741 case kCsDevice: return "device";
742 case kCsHost: return "host";
743 case kCsRecovery: return "recovery";
Elliott Hughes70097492015-12-11 19:07:01 -0800744 case kCsNoPerm: return UsbNoPermissionsShortHelpText();
David Pursell6e5c7eb2015-12-02 15:14:31 -0800745 case kCsSideload: return "sideload";
746 case kCsUnauthorized: return "unauthorized";
747 default: return "unknown";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800748 }
749}
750
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100751void atransport::update_version(int version, size_t payload) {
752 protocol_version = std::min(version, A_VERSION);
753 max_payload = std::min(payload, MAX_PAYLOAD);
754}
755
756int atransport::get_protocol_version() const {
757 return protocol_version;
758}
759
760size_t atransport::get_max_payload() const {
761 return max_payload;
762}
763
David Pursella07dbad2015-09-22 10:43:08 -0700764namespace {
David Pursell8da19a42015-08-31 10:42:13 -0700765
David Pursella07dbad2015-09-22 10:43:08 -0700766constexpr char kFeatureStringDelimiter = ',';
767
768} // namespace
Dan Albertbe8e54b2015-05-18 13:06:53 -0700769
770const FeatureSet& supported_features() {
David Pursella07dbad2015-09-22 10:43:08 -0700771 // Local static allocation to avoid global non-POD variables.
772 static const FeatureSet* features = new FeatureSet{
Todd Kennedyaff9c672015-11-10 00:03:25 +0000773 kFeatureShell2,
774 kFeatureCmd
David Pursell6d5fad32015-09-25 08:37:13 -0700775 // Increment ADB_SERVER_VERSION whenever the feature list changes to
776 // make sure that the adb client and server features stay in sync
777 // (http://b/24370690).
David Pursella07dbad2015-09-22 10:43:08 -0700778 };
779
780 return *features;
781}
782
783std::string FeatureSetToString(const FeatureSet& features) {
784 return android::base::Join(features, kFeatureStringDelimiter);
785}
786
787FeatureSet StringToFeatureSet(const std::string& features_string) {
David Pursell3d9072b2015-09-25 13:04:21 -0700788 if (features_string.empty()) {
789 return FeatureSet();
790 }
791
David Pursella07dbad2015-09-22 10:43:08 -0700792 auto names = android::base::Split(features_string,
793 {kFeatureStringDelimiter});
794 return FeatureSet(names.begin(), names.end());
Dan Albertbe8e54b2015-05-18 13:06:53 -0700795}
796
David Pursell22fc5e92015-09-30 13:35:42 -0700797bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
798 return feature_set.count(feature) > 0 &&
799 supported_features().count(feature) > 0;
800}
801
Dan Albertbe8e54b2015-05-18 13:06:53 -0700802bool atransport::has_feature(const std::string& feature) const {
803 return features_.count(feature) > 0;
804}
805
David Pursella07dbad2015-09-22 10:43:08 -0700806void atransport::SetFeatures(const std::string& features_string) {
807 features_ = StringToFeatureSet(features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700808}
809
Yabin Cui2d4c1982015-08-28 15:09:44 -0700810void atransport::AddDisconnect(adisconnect* disconnect) {
811 disconnects_.push_back(disconnect);
812}
813
814void atransport::RemoveDisconnect(adisconnect* disconnect) {
815 disconnects_.remove(disconnect);
816}
817
818void atransport::RunDisconnects() {
Elliott Hughes85952832015-10-07 15:59:35 -0700819 for (const auto& disconnect : disconnects_) {
Yabin Cui2d4c1982015-08-28 15:09:44 -0700820 disconnect->func(disconnect->opaque, this);
821 }
822 disconnects_.clear();
823}
824
David Pursellc929c6f2016-03-01 08:58:26 -0800825bool atransport::MatchesTarget(const std::string& target) const {
826 if (serial) {
827 if (target == serial) {
828 return true;
829 } else if (type == kTransportLocal) {
830 // Local transports can match [tcp:|udp:]<hostname>[:port].
831 const char* local_target_ptr = target.c_str();
832
833 // For fastboot compatibility, ignore protocol prefixes.
834 if (android::base::StartsWith(target, "tcp:") ||
835 android::base::StartsWith(target, "udp:")) {
836 local_target_ptr += 4;
837 }
838
839 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
840 std::string serial_host, error;
841 int serial_port = -1;
842 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr,
843 &error)) {
844 // |target| may omit the port to default to ours.
845 std::string target_host;
846 int target_port = serial_port;
847 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
848 nullptr, &error) &&
849 serial_host == target_host && serial_port == target_port) {
850 return true;
851 }
852 }
853 }
854 }
855
856 return (devpath && target == devpath) ||
857 qual_match(target.c_str(), "product:", product, false) ||
858 qual_match(target.c_str(), "model:", model, true) ||
859 qual_match(target.c_str(), "device:", device, false);
860}
861
Elliott Hughes88b4c852015-04-30 17:32:03 -0700862#if ADB_HOST
863
Dan Alberta8c34142015-05-06 16:48:52 -0700864static void append_transport_info(std::string* result, const char* key,
865 const char* value, bool sanitize) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700866 if (value == nullptr || *value == '\0') {
Scott Anderson27042382012-05-30 18:11:27 -0700867 return;
Scott Anderson27042382012-05-30 18:11:27 -0700868 }
869
Elliott Hughes88b4c852015-04-30 17:32:03 -0700870 *result += ' ';
871 *result += key;
872
873 for (const char* p = value; *p; ++p) {
874 result->push_back((!sanitize || isalnum(*p)) ? *p : '_');
875 }
Scott Anderson27042382012-05-30 18:11:27 -0700876}
877
Dan Albertecce5032015-05-18 16:46:31 -0700878static void append_transport(const atransport* t, std::string* result,
879 bool long_listing) {
Scott Anderson27042382012-05-30 18:11:27 -0700880 const char* serial = t->serial;
Elliott Hughes88b4c852015-04-30 17:32:03 -0700881 if (!serial || !serial[0]) {
Dan Alberta8c34142015-05-06 16:48:52 -0700882 serial = "(no serial number)";
Elliott Hughes88b4c852015-04-30 17:32:03 -0700883 }
Scott Anderson27042382012-05-30 18:11:27 -0700884
885 if (!long_listing) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700886 *result += serial;
887 *result += '\t';
888 *result += t->connection_state_name();
Scott Anderson27042382012-05-30 18:11:27 -0700889 } else {
David Pursell6e5c7eb2015-12-02 15:14:31 -0800890 android::base::StringAppendF(result, "%-22s %s", serial,
891 t->connection_state_name().c_str());
Scott Anderson27042382012-05-30 18:11:27 -0700892
Elliott Hughes88b4c852015-04-30 17:32:03 -0700893 append_transport_info(result, "", t->devpath, false);
894 append_transport_info(result, "product:", t->product, false);
895 append_transport_info(result, "model:", t->model, true);
896 append_transport_info(result, "device:", t->device, false);
Scott Anderson27042382012-05-30 18:11:27 -0700897 }
Elliott Hughes88b4c852015-04-30 17:32:03 -0700898 *result += '\n';
Scott Anderson27042382012-05-30 18:11:27 -0700899}
900
Elliott Hughes88b4c852015-04-30 17:32:03 -0700901std::string list_transports(bool long_listing) {
902 std::string result;
Josh Gaoe7daf572016-09-21 12:37:10 -0700903
904 std::lock_guard<std::mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -0700905 for (const auto& t : transport_list) {
Elliott Hughes88b4c852015-04-30 17:32:03 -0700906 append_transport(t, &result, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800907 }
Elliott Hughes88b4c852015-04-30 17:32:03 -0700908 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800909}
910
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800911/* hack for osx */
Dan Albertecce5032015-05-18 16:46:31 -0700912void close_usb_devices() {
Josh Gaoe7daf572016-09-21 12:37:10 -0700913 std::lock_guard<std::mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -0700914 for (const auto& t : transport_list) {
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700915 t->Kick();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800916 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800917}
918#endif // ADB_HOST
919
Dan Albertecce5032015-05-18 16:46:31 -0700920int register_socket_transport(int s, const char *serial, int port, int local) {
921 atransport* t = new atransport();
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100922
923 if (!serial) {
Dan Albertecce5032015-05-18 16:46:31 -0700924 char buf[32];
925 snprintf(buf, sizeof(buf), "T-%p", t);
926 serial = buf;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100927 }
Dan Albertecce5032015-05-18 16:46:31 -0700928
Yabin Cui815ad882015-09-02 17:44:28 -0700929 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700930 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertecce5032015-05-18 16:46:31 -0700931 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700932 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800933 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700934
Josh Gaoe7daf572016-09-21 12:37:10 -0700935 std::unique_lock<std::mutex> lock(transport_lock);
Elliott Hughes85952832015-10-07 15:59:35 -0700936 for (const auto& transport : pending_list) {
Dan Albertecce5032015-05-18 16:46:31 -0700937 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700938 VLOG(TRANSPORT) << "socket transport " << transport->serial
939 << " is already in pending_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -0700940 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700941 return -1;
942 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800943 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700944
Elliott Hughes85952832015-10-07 15:59:35 -0700945 for (const auto& transport : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -0700946 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700947 VLOG(TRANSPORT) << "socket transport " << transport->serial
948 << " is already in transport_list and fails to register";
Dan Albertecce5032015-05-18 16:46:31 -0700949 delete t;
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700950 return -1;
951 }
952 }
953
Dan Albertecce5032015-05-18 16:46:31 -0700954 pending_list.push_front(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700955 t->serial = strdup(serial);
Josh Gaoe7daf572016-09-21 12:37:10 -0700956
957 lock.unlock();
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700958
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800959 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700960 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800961}
962
Mike Lockwood81ffe172009-10-11 23:04:18 -0400963#if ADB_HOST
Dan Albertecce5032015-05-18 16:46:31 -0700964atransport *find_transport(const char *serial) {
965 atransport* result = nullptr;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400966
Josh Gaoe7daf572016-09-21 12:37:10 -0700967 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700968 for (auto& t : transport_list) {
Dan Albertecce5032015-05-18 16:46:31 -0700969 if (t->serial && strcmp(serial, t->serial) == 0) {
970 result = t;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400971 break;
972 }
Dan Albertecce5032015-05-18 16:46:31 -0700973 }
Mike Lockwood81ffe172009-10-11 23:04:18 -0400974
Dan Albertecce5032015-05-18 16:46:31 -0700975 return result;
Mike Lockwood81ffe172009-10-11 23:04:18 -0400976}
977
Yabin Cui4d64fd82015-08-27 12:03:11 -0700978void kick_all_tcp_devices() {
Josh Gaoe7daf572016-09-21 12:37:10 -0700979 std::lock_guard<std::mutex> lock(transport_lock);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700980 for (auto& t : transport_list) {
Yabin Cuif401ead2016-04-29 16:53:52 -0700981 if (t->IsTcpDevice()) {
Yabin Cui4e222292015-08-31 11:50:24 -0700982 // Kicking breaks the read_transport thread of this transport out of any read, then
983 // the read_transport thread will notify the main thread to make this transport
984 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cui4d64fd82015-08-27 12:03:11 -0700985 // Finally, this transport will be closed and freed in the main thread.
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700986 t->Kick();
Mike Lockwood01c2c302010-05-24 10:44:35 -0400987 }
Dan Albertecce5032015-05-18 16:46:31 -0700988 }
Mike Lockwood01c2c302010-05-24 10:44:35 -0400989}
990
Mike Lockwood81ffe172009-10-11 23:04:18 -0400991#endif
992
Dan Albertecce5032015-05-18 16:46:31 -0700993void register_usb_transport(usb_handle* usb, const char* serial,
994 const char* devpath, unsigned writeable) {
995 atransport* t = new atransport();
996
Yabin Cui815ad882015-09-02 17:44:28 -0700997 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb,
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800998 serial ? serial : "");
Dan Albert9a50f4c2015-05-18 16:43:57 -0700999 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001000 if(serial) {
1001 t->serial = strdup(serial);
1002 }
Dan Albertecce5032015-05-18 16:46:31 -07001003
1004 if (devpath) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001005 t->devpath = strdup(devpath);
1006 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001007
Josh Gaoe7daf572016-09-21 12:37:10 -07001008 {
1009 std::lock_guard<std::mutex> lock(transport_lock);
1010 pending_list.push_front(t);
1011 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001012
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001013 register_transport(t);
1014}
1015
Dan Albert9a50f4c2015-05-18 16:43:57 -07001016// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertecce5032015-05-18 16:46:31 -07001017void unregister_usb_transport(usb_handle *usb) {
Josh Gaoe7daf572016-09-21 12:37:10 -07001018 std::lock_guard<std::mutex> lock(transport_lock);
Dan Albertecce5032015-05-18 16:46:31 -07001019 transport_list.remove_if([usb](atransport* t) {
1020 return t->usb == usb && t->connection_state == kCsNoPerm;
1021 });
Mike Lockwoode45583f2009-08-08 12:37:44 -04001022}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001023
Tamas Berghammera1c60c02015-07-13 19:12:28 +01001024int check_header(apacket *p, atransport *t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001025{
1026 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cui19bec5b2015-09-22 15:52:57 -07001027 VLOG(RWX) << "check_header(): invalid magic";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001028 return -1;
1029 }
1030
Tamas Berghammera1c60c02015-07-13 19:12:28 +01001031 if(p->msg.data_length > t->get_max_payload()) {
Yabin Cui19bec5b2015-09-22 15:52:57 -07001032 VLOG(RWX) << "check_header(): " << p->msg.data_length << " atransport::max_payload = "
1033 << t->get_max_payload();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001034 return -1;
1035 }
1036
1037 return 0;
1038}
1039
Josh Gao67ac3792016-10-06 13:31:44 -07001040int check_data(apacket* p) {
1041 if (calculate_apacket_checksum(p) != p->msg.data_check) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001042 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001043 }
Josh Gao67ac3792016-10-06 13:31:44 -07001044 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001045}
Elliott Hughes801066a2016-06-29 17:42:01 -07001046
Josh Gaoeac20582016-10-05 19:02:29 -07001047#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -07001048std::shared_ptr<RSA> atransport::NextKey() {
Elliott Hughes801066a2016-06-29 17:42:01 -07001049 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1050
Josh Gao22cb70b2016-08-18 22:00:12 -07001051 std::shared_ptr<RSA> result = keys_[0];
Elliott Hughes801066a2016-06-29 17:42:01 -07001052 keys_.pop_front();
1053 return result;
1054}
Josh Gaoeac20582016-10-05 19:02:29 -07001055#endif