blob: 413b362030f70ec98aec2fbd4ff0f321232f515d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG TRANSPORT
Dan Albert76649012015-02-24 15:51:19 -080018
Dan Albert33134262015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert055f1aa2015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080023#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080027#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Spencer Low363af562015-11-07 18:51:54 -080029#include <algorithm>
Dan Albertc7915a32015-05-18 16:46:31 -070030#include <list>
31
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/logging.h>
David Pursell802c54e2016-03-01 08:58:26 -080033#include <android-base/parsenetaddress.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Elliott Hughese67f1f82015-04-30 17:32:03 -070036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include "adb.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070038#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080039#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41static void transport_unref(atransport *t);
42
Josh Gaob7b1edf2015-11-11 17:56:12 -080043static auto& transport_list = *new std::list<atransport*>();
44static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070045
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046ADB_MUTEX_DEFINE( transport_lock );
47
Todd Kennedy51c05ec2015-11-10 00:03:25 +000048const char* const kFeatureShell2 = "shell_v2";
49const char* const kFeatureCmd = "cmd";
50
Yabin Cuiaed3c612015-09-22 15:52:57 -070051static std::string dump_packet(const char* name, const char* func, apacket* p) {
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010052 unsigned command = p->msg.command;
53 int len = p->msg.data_length;
54 char cmd[9];
55 char arg0[12], arg1[12];
56 int n;
57
58 for (n = 0; n < 4; n++) {
59 int b = (command >> (n*8)) & 255;
60 if (b < 32 || b >= 127)
61 break;
62 cmd[n] = (char)b;
63 }
64 if (n == 4) {
65 cmd[4] = 0;
66 } else {
67 /* There is some non-ASCII name in the command, so dump
68 * the hexadecimal value instead */
69 snprintf(cmd, sizeof cmd, "%08x", command);
70 }
71
72 if (p->msg.arg0 < 256U)
73 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
74 else
75 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
76
77 if (p->msg.arg1 < 256U)
78 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
79 else
80 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
81
Yabin Cuiaed3c612015-09-22 15:52:57 -070082 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
83 name, func, cmd, arg0, arg1, len);
84 result += dump_hex(p->data, len);
85 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010086}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010087
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010089read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090{
Yabin Cui62641292015-07-30 19:58:10 -070091 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010092 if (!name) {
93 snprintf(buff, sizeof buff, "fd=%d", fd);
94 name = buff;
95 }
Yabin Cui62641292015-07-30 19:58:10 -070096 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
97 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -070099 int r = adb_read(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 if(r > 0) {
101 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700102 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700104 D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 return -1;
106 }
107 }
108
Yabin Cuiaed3c612015-09-22 15:52:57 -0700109 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 return 0;
111}
112
113static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100114write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115{
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100116 char buff[8];
117 if (!name) {
118 snprintf(buff, sizeof buff, "fd=%d", fd);
119 name = buff;
120 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700121 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Yabin Cui62641292015-07-30 19:58:10 -0700122 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
123 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700125 int r = adb_write(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 if(r > 0) {
127 len -= r;
128 p += r;
129 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700130 D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 return -1;
132 }
133 }
134 return 0;
135}
136
137static void transport_socket_events(int fd, unsigned events, void *_t)
138{
Dan Albertbac34742015-02-25 17:51:28 -0800139 atransport *t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700140 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 if(events & FDE_READ){
142 apacket *p = 0;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100143 if(read_packet(fd, t->serial, &p)){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700144 D("%s: failed to read packet from transport socket on fd %d", t->serial, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 } else {
146 handle_packet(p, (atransport *) _t);
147 }
148 }
149}
150
151void send_packet(apacket *p, atransport *t)
152{
153 unsigned char *x;
154 unsigned sum;
155 unsigned count;
156
157 p->msg.magic = p->msg.command ^ 0xffffffff;
158
159 count = p->msg.data_length;
160 x = (unsigned char *) p->data;
161 sum = 0;
162 while(count-- > 0){
163 sum += *x++;
164 }
165 p->msg.data_check = sum;
166
167 print_packet("send", p);
168
169 if (t == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700170 D("Transport is null");
JP Abgrall408fa572011-03-16 15:57:42 -0700171 // Zap errno because print_packet() and other stuff have errno effect.
172 errno = 0;
173 fatal_errno("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 }
175
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100176 if(write_packet(t->transport_socket, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 fatal_errno("cannot enqueue packet on transport socket");
178 }
179}
180
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700181// The transport is opened by transport_register_func before
182// the read_transport and write_transport threads are started.
183//
184// The read_transport thread issues a SYNC(1, token) message to let
185// the write_transport thread know to start things up. In the event
186// of transport IO failure, the read_transport thread will post a
187// SYNC(0,0) message to ensure shutdown.
188//
189// The transport will not actually be closed until both threads exit, but the threads
190// will kick the transport on their way out to disconnect the underlying device.
191//
192// read_transport thread reads data from a transport (representing a usb/tcp connection),
193// and makes the main thread call handle_packet().
Josh Gaod9db09c2016-02-12 14:31:15 -0800194static void read_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800195 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 apacket *p;
197
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700198 adb_thread_setname(android::base::StringPrintf("<-%s",
199 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700200 D("%s: starting read_transport thread on fd %d, SYNC online (%d)",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100201 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 p = get_apacket();
203 p->msg.command = A_SYNC;
204 p->msg.arg0 = 1;
205 p->msg.arg1 = ++(t->sync_token);
206 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100207 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700209 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 goto oops;
211 }
212
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700213 D("%s: data pump started", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 for(;;) {
215 p = get_apacket();
216
217 if(t->read_from_remote(p, t) == 0){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700218 D("%s: received remote packet, sending to transport",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100219 t->serial);
220 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700222 D("%s: failed to write apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 goto oops;
224 }
225 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700226 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 put_apacket(p);
228 break;
229 }
230 }
231
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700232 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 p = get_apacket();
234 p->msg.command = A_SYNC;
235 p->msg.arg0 = 0;
236 p->msg.arg1 = 0;
237 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100238 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700240 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242
243oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700244 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 kick_transport(t);
246 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247}
248
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700249// write_transport thread gets packets sent by the main thread (through send_packet()),
250// and writes to a transport (representing a usb/tcp connection).
Josh Gaod9db09c2016-02-12 14:31:15 -0800251static void write_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800252 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 apacket *p;
254 int active = 0;
255
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700256 adb_thread_setname(android::base::StringPrintf("->%s",
257 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700258 D("%s: starting write_transport thread, reading from fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100259 t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260
261 for(;;){
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100262 if(read_packet(t->fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700263 D("%s: failed to read apacket from transport on fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100264 t->serial, t->fd );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 break;
266 }
267 if(p->msg.command == A_SYNC){
268 if(p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700269 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 put_apacket(p);
271 break;
272 } else {
273 if(p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700274 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 active = 1;
276 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700277 D("%s: transport ignoring SYNC %d != %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100278 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 }
280 }
281 } else {
282 if(active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700283 D("%s: transport got packet, sending to remote", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 t->write_to_remote(p, t);
285 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700286 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 }
288 }
289
290 put_apacket(p);
291 }
292
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700293 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 kick_transport(t);
295 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296}
297
Yabin Cuif4b99282015-08-27 12:03:11 -0700298static void kick_transport_locked(atransport* t) {
299 CHECK(t != nullptr);
300 if (!t->kicked) {
301 t->kicked = true;
302 t->kick(t);
303 }
304}
305
306void kick_transport(atransport* t) {
307 adb_mutex_lock(&transport_lock);
Yabin Cui03468c82016-04-05 13:50:44 -0700308 // As kick_transport() can be called from threads without guarantee that t is valid,
309 // check if the transport is in transport_list first.
310 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
311 kick_transport_locked(t);
312 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700313 adb_mutex_unlock(&transport_lock);
314}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315
316static int transport_registration_send = -1;
317static int transport_registration_recv = -1;
318static fdevent transport_registration_fde;
319
320
321#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
323/* this adds support required by the 'track-devices' service.
324 * this is used to send the content of "list_transport" to any
325 * number of client connections that want it through a single
326 * live TCP connection
327 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328struct device_tracker {
329 asocket socket;
330 int update_needed;
331 device_tracker* next;
332};
333
334/* linked list of all device trackers */
335static device_tracker* device_tracker_list;
336
337static void
338device_tracker_remove( device_tracker* tracker )
339{
340 device_tracker** pnode = &device_tracker_list;
341 device_tracker* node = *pnode;
342
343 adb_mutex_lock( &transport_lock );
344 while (node) {
345 if (node == tracker) {
346 *pnode = node->next;
347 break;
348 }
349 pnode = &node->next;
350 node = *pnode;
351 }
352 adb_mutex_unlock( &transport_lock );
353}
354
355static void
356device_tracker_close( asocket* socket )
357{
358 device_tracker* tracker = (device_tracker*) socket;
359 asocket* peer = socket->peer;
360
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700361 D( "device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 if (peer) {
363 peer->peer = NULL;
364 peer->close(peer);
365 }
366 device_tracker_remove(tracker);
367 free(tracker);
368}
369
370static int
371device_tracker_enqueue( asocket* socket, apacket* p )
372{
373 /* you can't read from a device tracker, close immediately */
374 put_apacket(p);
375 device_tracker_close(socket);
376 return -1;
377}
378
Elliott Hughese67f1f82015-04-30 17:32:03 -0700379static int device_tracker_send(device_tracker* tracker, const std::string& string) {
380 apacket* p = get_apacket();
381 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
Elliott Hughese67f1f82015-04-30 17:32:03 -0700383 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
384 memcpy(&p->data[4], string.data(), string.size());
385 p->len = 4 + string.size();
386 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387}
388
Elliott Hughese67f1f82015-04-30 17:32:03 -0700389static void device_tracker_ready(asocket* socket) {
390 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391
Elliott Hughese67f1f82015-04-30 17:32:03 -0700392 // We want to send the device list when the tracker connects
393 // for the first time, even if no update occurred.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 if (tracker->update_needed > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 tracker->update_needed = 0;
396
Elliott Hughese67f1f82015-04-30 17:32:03 -0700397 std::string transports = list_transports(false);
398 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 }
400}
401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402asocket*
403create_device_tracker(void)
404{
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700405 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
406 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700408 D( "device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409
410 tracker->socket.enqueue = device_tracker_enqueue;
411 tracker->socket.ready = device_tracker_ready;
412 tracker->socket.close = device_tracker_close;
413 tracker->update_needed = 1;
414
415 tracker->next = device_tracker_list;
416 device_tracker_list = tracker;
417
418 return &tracker->socket;
419}
420
421
Elliott Hughese67f1f82015-04-30 17:32:03 -0700422// Call this function each time the transport list has changed.
423void update_transports() {
424 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
Elliott Hughese67f1f82015-04-30 17:32:03 -0700426 device_tracker* tracker = device_tracker_list;
427 while (tracker != nullptr) {
428 device_tracker* next = tracker->next;
429 // This may destroy the tracker if the connection is closed.
430 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 tracker = next;
432 }
433}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700436
437void update_transports() {
438 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441#endif // ADB_HOST
442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443struct tmsg
444{
445 atransport *transport;
446 int action;
447};
448
449static int
450transport_read_action(int fd, struct tmsg* m)
451{
452 char *p = (char*)m;
453 int len = sizeof(*m);
454 int r;
455
456 while(len > 0) {
457 r = adb_read(fd, p, len);
458 if(r > 0) {
459 len -= r;
460 p += r;
461 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700462 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return -1;
464 }
465 }
466 return 0;
467}
468
469static int
470transport_write_action(int fd, struct tmsg* m)
471{
472 char *p = (char*)m;
473 int len = sizeof(*m);
474 int r;
475
476 while(len > 0) {
477 r = adb_write(fd, p, len);
478 if(r > 0) {
479 len -= r;
480 p += r;
481 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700482 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 return -1;
484 }
485 }
486 return 0;
487}
488
489static void transport_registration_func(int _fd, unsigned ev, void *data)
490{
491 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 int s[2];
493 atransport *t;
494
495 if(!(ev & FDE_READ)) {
496 return;
497 }
498
499 if(transport_read_action(_fd, &m)) {
500 fatal_errno("cannot read transport registration socket");
501 }
502
503 t = m.transport;
504
Dan Albert1792c232015-05-18 13:06:53 -0700505 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700506 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507
508 /* IMPORTANT: the remove closes one half of the
509 ** socket pair. The close closes the other half.
510 */
511 fdevent_remove(&(t->transport_fde));
512 adb_close(t->fd);
513
514 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700515 transport_list.remove(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 adb_mutex_unlock(&transport_lock);
517
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 if (t->product)
519 free(t->product);
520 if (t->serial)
521 free(t->serial);
Scott Andersone82c2db2012-05-25 14:10:02 -0700522 if (t->model)
523 free(t->model);
524 if (t->device)
525 free(t->device);
Scott Andersone109d262012-04-20 11:21:14 -0700526 if (t->devpath)
527 free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528
Dan Albertc7915a32015-05-18 16:46:31 -0700529 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530
531 update_transports();
532 return;
533 }
534
Mike Lockwood0927bf92009-08-08 12:37:44 -0400535 /* don't create transport threads for inaccessible devices */
Dan Albertdcd78a12015-05-18 16:43:57 -0700536 if (t->connection_state != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400538 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539
Dan Albertc7915a32015-05-18 16:46:31 -0700540 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400541 fatal_errno("cannot open transport socketpair");
542 }
543
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700544 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400545
546 t->transport_socket = s[0];
547 t->fd = s[1];
548
Mike Lockwood0927bf92009-08-08 12:37:44 -0400549 fdevent_install(&(t->transport_fde),
550 t->transport_socket,
551 transport_socket_events,
552 t);
553
554 fdevent_set(&(t->transport_fde), FDE_READ);
555
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700556 if (!adb_thread_create(write_transport_thread, t)) {
557 fatal_errno("cannot create write_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400558 }
559
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700560 if (!adb_thread_create(read_transport_thread, t)) {
561 fatal_errno("cannot create read_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400562 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 }
564
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700566 pending_list.remove(t);
567 transport_list.push_front(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 adb_mutex_unlock(&transport_lock);
569
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570 update_transports();
571}
572
573void init_transport_registration(void)
574{
575 int s[2];
576
577 if(adb_socketpair(s)){
578 fatal_errno("cannot open transport registration socketpair");
579 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700580 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581
582 transport_registration_send = s[0];
583 transport_registration_recv = s[1];
584
585 fdevent_install(&transport_registration_fde,
586 transport_registration_recv,
587 transport_registration_func,
588 0);
589
590 fdevent_set(&transport_registration_fde, FDE_READ);
591}
592
593/* the fdevent select pump is single threaded */
594static void register_transport(atransport *transport)
595{
596 tmsg m;
597 m.transport = transport;
598 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700599 D("transport: %s registered", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 if(transport_write_action(transport_registration_send, &m)) {
601 fatal_errno("cannot write transport registration socket\n");
602 }
603}
604
605static void remove_transport(atransport *transport)
606{
607 tmsg m;
608 m.transport = transport;
609 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700610 D("transport: %s removed", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 if(transport_write_action(transport_registration_send, &m)) {
612 fatal_errno("cannot write transport registration socket\n");
613 }
614}
615
616
Yabin Cuif4b99282015-08-27 12:03:11 -0700617static void transport_unref(atransport* t) {
618 CHECK(t != nullptr);
619 adb_mutex_lock(&transport_lock);
620 CHECK_GT(t->ref_count, 0u);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400621 t->ref_count--;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400622 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700623 D("transport: %s unref (kicking and closing)", t->serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700624 kick_transport_locked(t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400625 t->close(t);
626 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100627 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700628 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400629 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700630 adb_mutex_unlock(&transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631}
632
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700633static int qual_match(const char *to_test,
Elliott Hughes09a45a12015-04-03 16:12:15 -0700634 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700635{
636 if (!to_test || !*to_test)
637 /* Return true if both the qual and to_test are null strings. */
638 return !qual || !*qual;
639
640 if (!qual)
641 return 0;
642
643 if (prefix) {
644 while (*prefix) {
645 if (*prefix++ != *to_test++)
646 return 0;
647 }
648 }
649
650 while (*qual) {
651 char ch = *qual++;
Elliott Hughes09a45a12015-04-03 16:12:15 -0700652 if (sanitize_qual && !isalnum(ch))
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700653 ch = '_';
654 if (ch != *to_test++)
655 return 0;
656 }
657
658 /* Everything matched so far. Return true if *to_test is a NUL. */
659 return !*to_test;
660}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661
Elliott Hughes8d28e192015-10-07 14:55:10 -0700662atransport* acquire_one_transport(TransportType type, const char* serial,
663 bool* is_ambiguous, std::string* error_out) {
664 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665
Elliott Hughes8d28e192015-10-07 14:55:10 -0700666 if (serial) {
667 *error_out = android::base::StringPrintf("device '%s' not found", serial);
668 } else if (type == kTransportLocal) {
669 *error_out = "no emulators found";
670 } else if (type == kTransportAny) {
671 *error_out = "no devices/emulators found";
672 } else {
673 *error_out = "no devices found";
674 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675
676 adb_mutex_lock(&transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700677 for (const auto& t : transport_list) {
Dan Albertdcd78a12015-05-18 16:43:57 -0700678 if (t->connection_state == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800679#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800680 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800681#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400682 continue;
683 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400684
Elliott Hughes8d28e192015-10-07 14:55:10 -0700685 // Check for matching serial number.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 if (serial) {
David Pursell802c54e2016-03-01 08:58:26 -0800687 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700688 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700689 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700690 if (is_ambiguous) *is_ambiguous = true;
691 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700692 break;
693 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700695 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700697 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700699 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700700 if (is_ambiguous) *is_ambiguous = true;
701 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 break;
703 }
704 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700705 } else if (type == kTransportLocal && t->type == kTransportLocal) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700707 *error_out = "more than one emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700708 if (is_ambiguous) *is_ambiguous = true;
709 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 break;
711 }
712 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700713 } else if (type == kTransportAny) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700715 *error_out = "more than one device/emulator";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700716 if (is_ambiguous) *is_ambiguous = true;
717 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 break;
719 }
720 result = t;
721 }
722 }
723 }
724 adb_mutex_unlock(&transport_lock);
725
Elliott Hughes8d28e192015-10-07 14:55:10 -0700726 // Don't return unauthorized devices; the caller can't do anything with them.
727 if (result && result->connection_state == kCsUnauthorized) {
728 *error_out = "device unauthorized.\n";
729 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
730 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
731 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
732 *error_out += "\n";
733 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
734 *error_out += "Otherwise check for a confirmation dialog on your device.";
735 result = nullptr;
736 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800737
Elliott Hughes8d28e192015-10-07 14:55:10 -0700738 // Don't return offline devices; the caller can't do anything with them.
739 if (result && result->connection_state == kCsOffline) {
740 *error_out = "device offline";
741 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 }
743
744 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700745 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 }
747
748 return result;
749}
750
David Purselld2acbd12015-12-02 15:14:31 -0800751const std::string atransport::connection_state_name() const {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700752 switch (connection_state) {
David Purselld2acbd12015-12-02 15:14:31 -0800753 case kCsOffline: return "offline";
754 case kCsBootloader: return "bootloader";
755 case kCsDevice: return "device";
756 case kCsHost: return "host";
757 case kCsRecovery: return "recovery";
Elliott Hughes1b708d32015-12-11 19:07:01 -0800758 case kCsNoPerm: return UsbNoPermissionsShortHelpText();
David Purselld2acbd12015-12-02 15:14:31 -0800759 case kCsSideload: return "sideload";
760 case kCsUnauthorized: return "unauthorized";
761 default: return "unknown";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762 }
763}
764
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100765void atransport::update_version(int version, size_t payload) {
766 protocol_version = std::min(version, A_VERSION);
767 max_payload = std::min(payload, MAX_PAYLOAD);
768}
769
770int atransport::get_protocol_version() const {
771 return protocol_version;
772}
773
774size_t atransport::get_max_payload() const {
775 return max_payload;
776}
777
David Pursell4e2fd362015-09-22 10:43:08 -0700778namespace {
David Pursell0955c662015-08-31 10:42:13 -0700779
David Pursell4e2fd362015-09-22 10:43:08 -0700780constexpr char kFeatureStringDelimiter = ',';
781
782} // namespace
Dan Albert1792c232015-05-18 13:06:53 -0700783
784const FeatureSet& supported_features() {
David Pursell4e2fd362015-09-22 10:43:08 -0700785 // Local static allocation to avoid global non-POD variables.
786 static const FeatureSet* features = new FeatureSet{
Todd Kennedy51c05ec2015-11-10 00:03:25 +0000787 kFeatureShell2,
788 kFeatureCmd
David Pursellbbe3d212015-09-25 08:37:13 -0700789 // Increment ADB_SERVER_VERSION whenever the feature list changes to
790 // make sure that the adb client and server features stay in sync
791 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700792 };
793
794 return *features;
795}
796
797std::string FeatureSetToString(const FeatureSet& features) {
798 return android::base::Join(features, kFeatureStringDelimiter);
799}
800
801FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700802 if (features_string.empty()) {
803 return FeatureSet();
804 }
805
David Pursell4e2fd362015-09-22 10:43:08 -0700806 auto names = android::base::Split(features_string,
807 {kFeatureStringDelimiter});
808 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700809}
810
David Pursell70ef7b42015-09-30 13:35:42 -0700811bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
812 return feature_set.count(feature) > 0 &&
813 supported_features().count(feature) > 0;
814}
815
Dan Albert1792c232015-05-18 13:06:53 -0700816bool atransport::has_feature(const std::string& feature) const {
817 return features_.count(feature) > 0;
818}
819
David Pursell4e2fd362015-09-22 10:43:08 -0700820void atransport::SetFeatures(const std::string& features_string) {
821 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700822}
823
Yabin Cuib3298242015-08-28 15:09:44 -0700824void atransport::AddDisconnect(adisconnect* disconnect) {
825 disconnects_.push_back(disconnect);
826}
827
828void atransport::RemoveDisconnect(adisconnect* disconnect) {
829 disconnects_.remove(disconnect);
830}
831
832void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700833 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700834 disconnect->func(disconnect->opaque, this);
835 }
836 disconnects_.clear();
837}
838
David Pursell802c54e2016-03-01 08:58:26 -0800839bool atransport::MatchesTarget(const std::string& target) const {
840 if (serial) {
841 if (target == serial) {
842 return true;
843 } else if (type == kTransportLocal) {
844 // Local transports can match [tcp:|udp:]<hostname>[:port].
845 const char* local_target_ptr = target.c_str();
846
847 // For fastboot compatibility, ignore protocol prefixes.
848 if (android::base::StartsWith(target, "tcp:") ||
849 android::base::StartsWith(target, "udp:")) {
850 local_target_ptr += 4;
851 }
852
853 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
854 std::string serial_host, error;
855 int serial_port = -1;
856 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr,
857 &error)) {
858 // |target| may omit the port to default to ours.
859 std::string target_host;
860 int target_port = serial_port;
861 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
862 nullptr, &error) &&
863 serial_host == target_host && serial_port == target_port) {
864 return true;
865 }
866 }
867 }
868 }
869
870 return (devpath && target == devpath) ||
871 qual_match(target.c_str(), "product:", product, false) ||
872 qual_match(target.c_str(), "model:", model, true) ||
873 qual_match(target.c_str(), "device:", device, false);
874}
875
Elliott Hughese67f1f82015-04-30 17:32:03 -0700876#if ADB_HOST
877
Dan Albertd99d9022015-05-06 16:48:52 -0700878static void append_transport_info(std::string* result, const char* key,
879 const char* value, bool sanitize) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700880 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700881 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700882 }
883
Elliott Hughese67f1f82015-04-30 17:32:03 -0700884 *result += ' ';
885 *result += key;
886
887 for (const char* p = value; *p; ++p) {
888 result->push_back((!sanitize || isalnum(*p)) ? *p : '_');
889 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700890}
891
Dan Albertc7915a32015-05-18 16:46:31 -0700892static void append_transport(const atransport* t, std::string* result,
893 bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700894 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700895 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700896 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700897 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700898
899 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700900 *result += serial;
901 *result += '\t';
902 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700903 } else {
David Purselld2acbd12015-12-02 15:14:31 -0800904 android::base::StringAppendF(result, "%-22s %s", serial,
905 t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700906
Elliott Hughese67f1f82015-04-30 17:32:03 -0700907 append_transport_info(result, "", t->devpath, false);
908 append_transport_info(result, "product:", t->product, false);
909 append_transport_info(result, "model:", t->model, true);
910 append_transport_info(result, "device:", t->device, false);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700911 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700912 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700913}
914
Elliott Hughese67f1f82015-04-30 17:32:03 -0700915std::string list_transports(bool long_listing) {
916 std::string result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800917 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700918 for (const auto& t : transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700919 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800920 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800921 adb_mutex_unlock(&transport_lock);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700922 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923}
924
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700926void close_usb_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700928 for (const auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700929 if (!t->kicked) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 t->kicked = 1;
931 t->kick(t);
932 }
933 }
934 adb_mutex_unlock(&transport_lock);
935}
936#endif // ADB_HOST
937
Dan Albertc7915a32015-05-18 16:46:31 -0700938int register_socket_transport(int s, const char *serial, int port, int local) {
939 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100940
941 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700942 char buf[32];
943 snprintf(buf, sizeof(buf), "T-%p", t);
944 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100945 }
Dan Albertc7915a32015-05-18 16:46:31 -0700946
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700947 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700948 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700949 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700950 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700952
953 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700954 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700955 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700956 adb_mutex_unlock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700957 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700958 return -1;
959 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700961
Elliott Hughes65fe2512015-10-07 15:59:35 -0700962 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700963 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700964 adb_mutex_unlock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700965 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700966 return -1;
967 }
968 }
969
Dan Albertc7915a32015-05-18 16:46:31 -0700970 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700971 t->serial = strdup(serial);
972 adb_mutex_unlock(&transport_lock);
973
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700975 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976}
977
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400978#if ADB_HOST
Dan Albertc7915a32015-05-18 16:46:31 -0700979atransport *find_transport(const char *serial) {
980 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400981
982 adb_mutex_lock(&transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700983 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700984 if (t->serial && strcmp(serial, t->serial) == 0) {
985 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400986 break;
987 }
Dan Albertc7915a32015-05-18 16:46:31 -0700988 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400989 adb_mutex_unlock(&transport_lock);
990
Dan Albertc7915a32015-05-18 16:46:31 -0700991 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400992}
993
Yabin Cuif4b99282015-08-27 12:03:11 -0700994void kick_all_tcp_devices() {
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400995 adb_mutex_lock(&transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700996 for (auto& t : transport_list) {
997 // TCP/IP devices have adb_port == 0.
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400998 if (t->type == kTransportLocal && t->adb_port == 0) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700999 // Kicking breaks the read_transport thread of this transport out of any read, then
1000 // the read_transport thread will notify the main thread to make this transport
1001 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001002 // Finally, this transport will be closed and freed in the main thread.
1003 kick_transport_locked(t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001004 }
Dan Albertc7915a32015-05-18 16:46:31 -07001005 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001006 adb_mutex_unlock(&transport_lock);
1007}
1008
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001009#endif
1010
Dan Albertc7915a32015-05-18 16:46:31 -07001011void register_usb_transport(usb_handle* usb, const char* serial,
1012 const char* devpath, unsigned writeable) {
1013 atransport* t = new atransport();
1014
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001015 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001016 serial ? serial : "");
Dan Albertdcd78a12015-05-18 16:43:57 -07001017 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018 if(serial) {
1019 t->serial = strdup(serial);
1020 }
Dan Albertc7915a32015-05-18 16:46:31 -07001021
1022 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001023 t->devpath = strdup(devpath);
1024 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001025
1026 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -07001027 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001028 adb_mutex_unlock(&transport_lock);
1029
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001030 register_transport(t);
1031}
1032
Dan Albertdcd78a12015-05-18 16:43:57 -07001033// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertc7915a32015-05-18 16:46:31 -07001034void unregister_usb_transport(usb_handle *usb) {
Mike Lockwood0927bf92009-08-08 12:37:44 -04001035 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -07001036 transport_list.remove_if([usb](atransport* t) {
1037 return t->usb == usb && t->connection_state == kCsNoPerm;
1038 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001039 adb_mutex_unlock(&transport_lock);
1040}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001042int check_header(apacket *p, atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001043{
1044 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001045 VLOG(RWX) << "check_header(): invalid magic";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001046 return -1;
1047 }
1048
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001049 if(p->msg.data_length > t->get_max_payload()) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001050 VLOG(RWX) << "check_header(): " << p->msg.data_length << " atransport::max_payload = "
1051 << t->get_max_payload();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001052 return -1;
1053 }
1054
1055 return 0;
1056}
1057
1058int check_data(apacket *p)
1059{
1060 unsigned count, sum;
1061 unsigned char *x;
1062
1063 count = p->msg.data_length;
1064 x = p->data;
1065 sum = 0;
1066 while(count-- > 0) {
1067 sum += *x++;
1068 }
1069
1070 if(sum != p->msg.data_check) {
1071 return -1;
1072 } else {
1073 return 0;
1074 }
1075}