blob: e0216e372c11567a6634fdd49e8320ca2b8b391b [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 Pursell3f902aa2016-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 Hughes0aeb5052016-06-29 17:42:01 -070038#include "adb_auth.h"
Elliott Hughese67f1f82015-04-30 17:32:03 -070039#include "adb_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080040#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
42static void transport_unref(atransport *t);
43
Josh Gaob7b1edf2015-11-11 17:56:12 -080044static auto& transport_list = *new std::list<atransport*>();
45static auto& pending_list = *new std::list<atransport*>();
Benoit Goby1c45ee92013-03-29 18:22:36 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047ADB_MUTEX_DEFINE( transport_lock );
48
Todd Kennedy6fa848a2015-11-03 16:53:08 -080049const char* const kFeatureShell2 = "shell_v2";
50const char* const kFeatureCmd = "cmd";
51
Yabin Cuiaed3c612015-09-22 15:52:57 -070052static std::string dump_packet(const char* name, const char* func, apacket* p) {
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010053 unsigned command = p->msg.command;
54 int len = p->msg.data_length;
55 char cmd[9];
56 char arg0[12], arg1[12];
57 int n;
58
59 for (n = 0; n < 4; n++) {
60 int b = (command >> (n*8)) & 255;
61 if (b < 32 || b >= 127)
62 break;
63 cmd[n] = (char)b;
64 }
65 if (n == 4) {
66 cmd[4] = 0;
67 } else {
68 /* There is some non-ASCII name in the command, so dump
69 * the hexadecimal value instead */
70 snprintf(cmd, sizeof cmd, "%08x", command);
71 }
72
73 if (p->msg.arg0 < 256U)
74 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
75 else
76 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
77
78 if (p->msg.arg1 < 256U)
79 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
80 else
81 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
82
Yabin Cuiaed3c612015-09-22 15:52:57 -070083 std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
84 name, func, cmd, arg0, arg1, len);
85 result += dump_hex(p->data, len);
86 return result;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010087}
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010088
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010090read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091{
Yabin Cui62641292015-07-30 19:58:10 -070092 char buff[8];
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +010093 if (!name) {
94 snprintf(buff, sizeof buff, "fd=%d", fd);
95 name = buff;
96 }
Yabin Cui62641292015-07-30 19:58:10 -070097 char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */
98 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700100 int r = adb_read(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 if(r > 0) {
102 len -= r;
Yabin Cui62641292015-07-30 19:58:10 -0700103 p += r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700105 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 -0800106 return -1;
107 }
108 }
109
Yabin Cuiaed3c612015-09-22 15:52:57 -0700110 VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 return 0;
112}
113
114static int
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100115write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116{
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100117 char buff[8];
118 if (!name) {
119 snprintf(buff, sizeof buff, "fd=%d", fd);
120 name = buff;
121 }
Yabin Cuiaed3c612015-09-22 15:52:57 -0700122 VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket);
Yabin Cui62641292015-07-30 19:58:10 -0700123 char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */
124 int len = sizeof(apacket*);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 while(len > 0) {
Yabin Cui62641292015-07-30 19:58:10 -0700126 int r = adb_write(fd, p, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 if(r > 0) {
128 len -= r;
129 p += r;
130 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700131 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 -0800132 return -1;
133 }
134 }
135 return 0;
136}
137
138static void transport_socket_events(int fd, unsigned events, void *_t)
139{
Dan Albertbac34742015-02-25 17:51:28 -0800140 atransport *t = reinterpret_cast<atransport*>(_t);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700141 D("transport_socket_events(fd=%d, events=%04x,...)", fd, events);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 if(events & FDE_READ){
143 apacket *p = 0;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100144 if(read_packet(fd, t->serial, &p)){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700145 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 -0800146 } else {
147 handle_packet(p, (atransport *) _t);
148 }
149 }
150}
151
152void send_packet(apacket *p, atransport *t)
153{
154 unsigned char *x;
155 unsigned sum;
156 unsigned count;
157
158 p->msg.magic = p->msg.command ^ 0xffffffff;
159
160 count = p->msg.data_length;
161 x = (unsigned char *) p->data;
162 sum = 0;
163 while(count-- > 0){
164 sum += *x++;
165 }
166 p->msg.data_check = sum;
167
168 print_packet("send", p);
169
170 if (t == NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700171 D("Transport is null");
JP Abgrall408fa572011-03-16 15:57:42 -0700172 // Zap errno because print_packet() and other stuff have errno effect.
173 errno = 0;
174 fatal_errno("Transport is null");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 }
176
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100177 if(write_packet(t->transport_socket, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 fatal_errno("cannot enqueue packet on transport socket");
179 }
180}
181
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700182// The transport is opened by transport_register_func before
183// the read_transport and write_transport threads are started.
184//
185// The read_transport thread issues a SYNC(1, token) message to let
186// the write_transport thread know to start things up. In the event
187// of transport IO failure, the read_transport thread will post a
188// SYNC(0,0) message to ensure shutdown.
189//
190// The transport will not actually be closed until both threads exit, but the threads
191// will kick the transport on their way out to disconnect the underlying device.
192//
193// read_transport thread reads data from a transport (representing a usb/tcp connection),
194// and makes the main thread call handle_packet().
Josh Gaob5fea142016-02-12 14:31:15 -0800195static void read_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800196 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 apacket *p;
198
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700199 adb_thread_setname(android::base::StringPrintf("<-%s",
200 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700201 D("%s: starting read_transport thread on fd %d, SYNC online (%d)",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100202 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 p = get_apacket();
204 p->msg.command = A_SYNC;
205 p->msg.arg0 = 1;
206 p->msg.arg1 = ++(t->sync_token);
207 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100208 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700210 D("%s: failed to write SYNC packet", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 goto oops;
212 }
213
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700214 D("%s: data pump started", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 for(;;) {
216 p = get_apacket();
217
218 if(t->read_from_remote(p, t) == 0){
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700219 D("%s: received remote packet, sending to transport",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100220 t->serial);
221 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700223 D("%s: failed to write apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 goto oops;
225 }
226 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700227 D("%s: remote read failed for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 put_apacket(p);
229 break;
230 }
231 }
232
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700233 D("%s: SYNC offline for transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 p = get_apacket();
235 p->msg.command = A_SYNC;
236 p->msg.arg0 = 0;
237 p->msg.arg1 = 0;
238 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100239 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 put_apacket(p);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700241 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244oops:
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700245 D("%s: read_transport thread is exiting", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 kick_transport(t);
247 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248}
249
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700250// write_transport thread gets packets sent by the main thread (through send_packet()),
251// and writes to a transport (representing a usb/tcp connection).
Josh Gaob5fea142016-02-12 14:31:15 -0800252static void write_transport_thread(void* _t) {
Dan Albertbac34742015-02-25 17:51:28 -0800253 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 apacket *p;
255 int active = 0;
256
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700257 adb_thread_setname(android::base::StringPrintf("->%s",
258 (t->serial != nullptr ? t->serial : "transport")));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700259 D("%s: starting write_transport thread, reading from fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100260 t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
262 for(;;){
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100263 if(read_packet(t->fd, t->serial, &p)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700264 D("%s: failed to read apacket from transport on fd %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100265 t->serial, t->fd );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 break;
267 }
268 if(p->msg.command == A_SYNC){
269 if(p->msg.arg0 == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700270 D("%s: transport SYNC offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 put_apacket(p);
272 break;
273 } else {
274 if(p->msg.arg1 == t->sync_token) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700275 D("%s: transport SYNC online", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 active = 1;
277 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700278 D("%s: transport ignoring SYNC %d != %d",
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100279 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 }
281 }
282 } else {
283 if(active) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700284 D("%s: transport got packet, sending to remote", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 t->write_to_remote(p, t);
286 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700287 D("%s: transport ignoring packet while offline", t->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 }
289 }
290
291 put_apacket(p);
292 }
293
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700294 D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 kick_transport(t);
296 transport_unref(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}
298
Yabin Cuif4b99282015-08-27 12:03:11 -0700299void kick_transport(atransport* t) {
300 adb_mutex_lock(&transport_lock);
Yabin Cui1f4ec192016-04-05 13:50:44 -0700301 // As kick_transport() can be called from threads without guarantee that t is valid,
302 // check if the transport is in transport_list first.
303 if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) {
Yabin Cui7f274902016-04-18 11:22:34 -0700304 t->Kick();
Yabin Cui1f4ec192016-04-05 13:50:44 -0700305 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700306 adb_mutex_unlock(&transport_lock);
307}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308
309static int transport_registration_send = -1;
310static int transport_registration_recv = -1;
311static fdevent transport_registration_fde;
312
313
314#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315
316/* this adds support required by the 'track-devices' service.
317 * this is used to send the content of "list_transport" to any
318 * number of client connections that want it through a single
319 * live TCP connection
320 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321struct device_tracker {
322 asocket socket;
323 int update_needed;
324 device_tracker* next;
325};
326
327/* linked list of all device trackers */
328static device_tracker* device_tracker_list;
329
330static void
331device_tracker_remove( device_tracker* tracker )
332{
333 device_tracker** pnode = &device_tracker_list;
334 device_tracker* node = *pnode;
335
336 adb_mutex_lock( &transport_lock );
337 while (node) {
338 if (node == tracker) {
339 *pnode = node->next;
340 break;
341 }
342 pnode = &node->next;
343 node = *pnode;
344 }
345 adb_mutex_unlock( &transport_lock );
346}
347
348static void
349device_tracker_close( asocket* socket )
350{
351 device_tracker* tracker = (device_tracker*) socket;
352 asocket* peer = socket->peer;
353
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700354 D( "device tracker %p removed", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 if (peer) {
356 peer->peer = NULL;
357 peer->close(peer);
358 }
359 device_tracker_remove(tracker);
360 free(tracker);
361}
362
363static int
364device_tracker_enqueue( asocket* socket, apacket* p )
365{
366 /* you can't read from a device tracker, close immediately */
367 put_apacket(p);
368 device_tracker_close(socket);
369 return -1;
370}
371
Elliott Hughese67f1f82015-04-30 17:32:03 -0700372static int device_tracker_send(device_tracker* tracker, const std::string& string) {
373 apacket* p = get_apacket();
374 asocket* peer = tracker->socket.peer;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375
Elliott Hughese67f1f82015-04-30 17:32:03 -0700376 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size()));
377 memcpy(&p->data[4], string.data(), string.size());
378 p->len = 4 + string.size();
379 return peer->enqueue(peer, p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380}
381
Elliott Hughese67f1f82015-04-30 17:32:03 -0700382static void device_tracker_ready(asocket* socket) {
383 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Elliott Hughese67f1f82015-04-30 17:32:03 -0700385 // We want to send the device list when the tracker connects
386 // for the first time, even if no update occurred.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 if (tracker->update_needed > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 tracker->update_needed = 0;
389
Elliott Hughese67f1f82015-04-30 17:32:03 -0700390 std::string transports = list_transports(false);
391 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 }
393}
394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395asocket*
396create_device_tracker(void)
397{
Elliott Hughesdc3b4592015-04-21 19:39:52 -0700398 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker)));
399 if (tracker == nullptr) fatal("cannot allocate device tracker");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700401 D( "device tracker %p created", tracker);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
403 tracker->socket.enqueue = device_tracker_enqueue;
404 tracker->socket.ready = device_tracker_ready;
405 tracker->socket.close = device_tracker_close;
406 tracker->update_needed = 1;
407
408 tracker->next = device_tracker_list;
409 device_tracker_list = tracker;
410
411 return &tracker->socket;
412}
413
414
Elliott Hughese67f1f82015-04-30 17:32:03 -0700415// Call this function each time the transport list has changed.
416void update_transports() {
417 std::string transports = list_transports(false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418
Elliott Hughese67f1f82015-04-30 17:32:03 -0700419 device_tracker* tracker = device_tracker_list;
420 while (tracker != nullptr) {
421 device_tracker* next = tracker->next;
422 // This may destroy the tracker if the connection is closed.
423 device_tracker_send(tracker, transports);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 tracker = next;
425 }
426}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700427
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428#else
Elliott Hughese67f1f82015-04-30 17:32:03 -0700429
430void update_transports() {
431 // Nothing to do on the device side.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432}
Elliott Hughese67f1f82015-04-30 17:32:03 -0700433
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434#endif // ADB_HOST
435
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436struct tmsg
437{
438 atransport *transport;
439 int action;
440};
441
442static int
443transport_read_action(int fd, struct tmsg* m)
444{
445 char *p = (char*)m;
446 int len = sizeof(*m);
447 int r;
448
449 while(len > 0) {
450 r = adb_read(fd, p, len);
451 if(r > 0) {
452 len -= r;
453 p += r;
454 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700455 D("transport_read_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return -1;
457 }
458 }
459 return 0;
460}
461
462static int
463transport_write_action(int fd, struct tmsg* m)
464{
465 char *p = (char*)m;
466 int len = sizeof(*m);
467 int r;
468
469 while(len > 0) {
470 r = adb_write(fd, p, len);
471 if(r > 0) {
472 len -= r;
473 p += r;
474 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700475 D("transport_write_action: on fd %d: %s", fd, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return -1;
477 }
478 }
479 return 0;
480}
481
482static void transport_registration_func(int _fd, unsigned ev, void *data)
483{
484 tmsg m;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 int s[2];
486 atransport *t;
487
488 if(!(ev & FDE_READ)) {
489 return;
490 }
491
492 if(transport_read_action(_fd, &m)) {
493 fatal_errno("cannot read transport registration socket");
494 }
495
496 t = m.transport;
497
Dan Albert1792c232015-05-18 13:06:53 -0700498 if (m.action == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700499 D("transport: %s removing and free'ing %d", t->serial, t->transport_socket);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500
501 /* IMPORTANT: the remove closes one half of the
502 ** socket pair. The close closes the other half.
503 */
504 fdevent_remove(&(t->transport_fde));
505 adb_close(t->fd);
506
507 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700508 transport_list.remove(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 adb_mutex_unlock(&transport_lock);
510
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 if (t->product)
512 free(t->product);
513 if (t->serial)
514 free(t->serial);
Scott Andersone82c2db2012-05-25 14:10:02 -0700515 if (t->model)
516 free(t->model);
517 if (t->device)
518 free(t->device);
Scott Andersone109d262012-04-20 11:21:14 -0700519 if (t->devpath)
520 free(t->devpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Dan Albertc7915a32015-05-18 16:46:31 -0700522 delete t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523
524 update_transports();
525 return;
526 }
527
Mike Lockwood0927bf92009-08-08 12:37:44 -0400528 /* don't create transport threads for inaccessible devices */
Dan Albertdcd78a12015-05-18 16:43:57 -0700529 if (t->connection_state != kCsNoPerm) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 /* initial references are the two threads */
Mike Lockwood0927bf92009-08-08 12:37:44 -0400531 t->ref_count = 2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532
Dan Albertc7915a32015-05-18 16:46:31 -0700533 if (adb_socketpair(s)) {
Mike Lockwood0927bf92009-08-08 12:37:44 -0400534 fatal_errno("cannot open transport socketpair");
535 }
536
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400538
539 t->transport_socket = s[0];
540 t->fd = s[1];
541
Mike Lockwood0927bf92009-08-08 12:37:44 -0400542 fdevent_install(&(t->transport_fde),
543 t->transport_socket,
544 transport_socket_events,
545 t);
546
547 fdevent_set(&(t->transport_fde), FDE_READ);
548
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700549 if (!adb_thread_create(write_transport_thread, t)) {
550 fatal_errno("cannot create write_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400551 }
552
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700553 if (!adb_thread_create(read_transport_thread, t)) {
554 fatal_errno("cannot create read_transport thread");
Mike Lockwood0927bf92009-08-08 12:37:44 -0400555 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 }
557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -0700559 pending_list.remove(t);
560 transport_list.push_front(t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 adb_mutex_unlock(&transport_lock);
562
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 update_transports();
564}
565
566void init_transport_registration(void)
567{
568 int s[2];
569
570 if(adb_socketpair(s)){
571 fatal_errno("cannot open transport registration socketpair");
572 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700573 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574
575 transport_registration_send = s[0];
576 transport_registration_recv = s[1];
577
578 fdevent_install(&transport_registration_fde,
579 transport_registration_recv,
580 transport_registration_func,
581 0);
582
583 fdevent_set(&transport_registration_fde, FDE_READ);
584}
585
586/* the fdevent select pump is single threaded */
587static void register_transport(atransport *transport)
588{
589 tmsg m;
590 m.transport = transport;
591 m.action = 1;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700592 D("transport: %s registered", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593 if(transport_write_action(transport_registration_send, &m)) {
594 fatal_errno("cannot write transport registration socket\n");
595 }
596}
597
598static void remove_transport(atransport *transport)
599{
600 tmsg m;
601 m.transport = transport;
602 m.action = 0;
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700603 D("transport: %s removed", transport->serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 if(transport_write_action(transport_registration_send, &m)) {
605 fatal_errno("cannot write transport registration socket\n");
606 }
607}
608
609
Yabin Cuif4b99282015-08-27 12:03:11 -0700610static void transport_unref(atransport* t) {
611 CHECK(t != nullptr);
612 adb_mutex_lock(&transport_lock);
613 CHECK_GT(t->ref_count, 0u);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400614 t->ref_count--;
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400615 if (t->ref_count == 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700616 D("transport: %s unref (kicking and closing)", t->serial);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400617 t->close(t);
618 remove_transport(t);
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100619 } else {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700620 D("transport: %s unref (count=%zu)", t->serial, t->ref_count);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400621 }
Yabin Cuif4b99282015-08-27 12:03:11 -0700622 adb_mutex_unlock(&transport_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623}
624
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700625static int qual_match(const char *to_test,
Elliott Hughes09a45a12015-04-03 16:12:15 -0700626 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700627{
628 if (!to_test || !*to_test)
629 /* Return true if both the qual and to_test are null strings. */
630 return !qual || !*qual;
631
632 if (!qual)
633 return 0;
634
635 if (prefix) {
636 while (*prefix) {
637 if (*prefix++ != *to_test++)
638 return 0;
639 }
640 }
641
642 while (*qual) {
643 char ch = *qual++;
Elliott Hughes09a45a12015-04-03 16:12:15 -0700644 if (sanitize_qual && !isalnum(ch))
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700645 ch = '_';
646 if (ch != *to_test++)
647 return 0;
648 }
649
650 /* Everything matched so far. Return true if *to_test is a NUL. */
651 return !*to_test;
652}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653
Elliott Hughes8d28e192015-10-07 14:55:10 -0700654atransport* acquire_one_transport(TransportType type, const char* serial,
655 bool* is_ambiguous, std::string* error_out) {
656 atransport* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657
Elliott Hughes8d28e192015-10-07 14:55:10 -0700658 if (serial) {
659 *error_out = android::base::StringPrintf("device '%s' not found", serial);
660 } else if (type == kTransportLocal) {
661 *error_out = "no emulators found";
662 } else if (type == kTransportAny) {
663 *error_out = "no devices/emulators found";
664 } else {
665 *error_out = "no devices found";
666 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667
668 adb_mutex_lock(&transport_lock);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700669 for (const auto& t : transport_list) {
Dan Albertdcd78a12015-05-18 16:43:57 -0700670 if (t->connection_state == kCsNoPerm) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800671#if ADB_HOST
David Purselld2acbd12015-12-02 15:14:31 -0800672 *error_out = UsbNoPermissionsLongHelpText();
Elliott Hughes1b708d32015-12-11 19:07:01 -0800673#endif
Mike Lockwood37d31112009-08-08 13:53:16 -0400674 continue;
675 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400676
Elliott Hughes8d28e192015-10-07 14:55:10 -0700677 // Check for matching serial number.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 if (serial) {
David Pursell3f902aa2016-03-01 08:58:26 -0800679 if (t->MatchesTarget(serial)) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700680 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700681 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700682 if (is_ambiguous) *is_ambiguous = true;
683 result = nullptr;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700684 break;
685 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 result = t;
Scott Andersone109d262012-04-20 11:21:14 -0700687 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 } else {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700689 if (type == kTransportUsb && t->type == kTransportUsb) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700691 *error_out = "more than one device";
Elliott Hughes8d28e192015-10-07 14:55:10 -0700692 if (is_ambiguous) *is_ambiguous = true;
693 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 break;
695 }
696 result = t;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700697 } else if (type == kTransportLocal && t->type == kTransportLocal) {
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 emulator";
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 == kTransportAny) {
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 device/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;
713 }
714 }
715 }
716 adb_mutex_unlock(&transport_lock);
717
Elliott Hughes8d28e192015-10-07 14:55:10 -0700718 // Don't return unauthorized devices; the caller can't do anything with them.
719 if (result && result->connection_state == kCsUnauthorized) {
720 *error_out = "device unauthorized.\n";
721 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
722 *error_out += "This adb server's $ADB_VENDOR_KEYS is ";
723 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set";
724 *error_out += "\n";
725 *error_out += "Try 'adb kill-server' if that seems wrong.\n";
726 *error_out += "Otherwise check for a confirmation dialog on your device.";
727 result = nullptr;
728 }
Benoit Goby77e8e582013-01-15 12:36:47 -0800729
Elliott Hughes8d28e192015-10-07 14:55:10 -0700730 // Don't return offline devices; the caller can't do anything with them.
731 if (result && result->connection_state == kCsOffline) {
732 *error_out = "device offline";
733 result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734 }
735
736 if (result) {
Elliott Hughese2d36772015-06-23 13:00:32 -0700737 *error_out = "success";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738 }
739
740 return result;
741}
742
Yabin Cui7f274902016-04-18 11:22:34 -0700743void atransport::Kick() {
744 if (!kicked_) {
745 kicked_ = true;
746 CHECK(kick_func_ != nullptr);
747 kick_func_(this);
748 }
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 Kennedy6fa848a2015-11-03 16:53:08 -0800787 kFeatureShell2,
Elliott Hughes79e1c7a2015-11-06 18:05:16 -0800788 // Internal master has 'cmd'. AOSP master doesn't.
789 // kFeatureCmd
790
David Pursellbbe3d212015-09-25 08:37:13 -0700791 // Increment ADB_SERVER_VERSION whenever the feature list changes to
792 // make sure that the adb client and server features stay in sync
793 // (http://b/24370690).
David Pursell4e2fd362015-09-22 10:43:08 -0700794 };
795
796 return *features;
797}
798
799std::string FeatureSetToString(const FeatureSet& features) {
800 return android::base::Join(features, kFeatureStringDelimiter);
801}
802
803FeatureSet StringToFeatureSet(const std::string& features_string) {
David Purselld2b588e2015-09-25 13:04:21 -0700804 if (features_string.empty()) {
805 return FeatureSet();
806 }
807
David Pursell4e2fd362015-09-22 10:43:08 -0700808 auto names = android::base::Split(features_string,
809 {kFeatureStringDelimiter});
810 return FeatureSet(names.begin(), names.end());
Dan Albert1792c232015-05-18 13:06:53 -0700811}
812
David Pursell70ef7b42015-09-30 13:35:42 -0700813bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) {
814 return feature_set.count(feature) > 0 &&
815 supported_features().count(feature) > 0;
816}
817
Dan Albert1792c232015-05-18 13:06:53 -0700818bool atransport::has_feature(const std::string& feature) const {
819 return features_.count(feature) > 0;
820}
821
David Pursell4e2fd362015-09-22 10:43:08 -0700822void atransport::SetFeatures(const std::string& features_string) {
823 features_ = StringToFeatureSet(features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700824}
825
Yabin Cuib3298242015-08-28 15:09:44 -0700826void atransport::AddDisconnect(adisconnect* disconnect) {
827 disconnects_.push_back(disconnect);
828}
829
830void atransport::RemoveDisconnect(adisconnect* disconnect) {
831 disconnects_.remove(disconnect);
832}
833
834void atransport::RunDisconnects() {
Elliott Hughes65fe2512015-10-07 15:59:35 -0700835 for (const auto& disconnect : disconnects_) {
Yabin Cuib3298242015-08-28 15:09:44 -0700836 disconnect->func(disconnect->opaque, this);
837 }
838 disconnects_.clear();
839}
840
David Pursell3f902aa2016-03-01 08:58:26 -0800841bool atransport::MatchesTarget(const std::string& target) const {
842 if (serial) {
843 if (target == serial) {
844 return true;
845 } else if (type == kTransportLocal) {
846 // Local transports can match [tcp:|udp:]<hostname>[:port].
847 const char* local_target_ptr = target.c_str();
848
849 // For fastboot compatibility, ignore protocol prefixes.
850 if (android::base::StartsWith(target, "tcp:") ||
851 android::base::StartsWith(target, "udp:")) {
852 local_target_ptr += 4;
853 }
854
855 // Parse our |serial| and the given |target| to check if the hostnames and ports match.
856 std::string serial_host, error;
857 int serial_port = -1;
858 if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr,
859 &error)) {
860 // |target| may omit the port to default to ours.
861 std::string target_host;
862 int target_port = serial_port;
863 if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port,
864 nullptr, &error) &&
865 serial_host == target_host && serial_port == target_port) {
866 return true;
867 }
868 }
869 }
870 }
871
872 return (devpath && target == devpath) ||
873 qual_match(target.c_str(), "product:", product, false) ||
874 qual_match(target.c_str(), "model:", model, true) ||
875 qual_match(target.c_str(), "device:", device, false);
876}
877
Elliott Hughese67f1f82015-04-30 17:32:03 -0700878#if ADB_HOST
879
Dan Albertd99d9022015-05-06 16:48:52 -0700880static void append_transport_info(std::string* result, const char* key,
881 const char* value, bool sanitize) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700882 if (value == nullptr || *value == '\0') {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700883 return;
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700884 }
885
Elliott Hughese67f1f82015-04-30 17:32:03 -0700886 *result += ' ';
887 *result += key;
888
889 for (const char* p = value; *p; ++p) {
890 result->push_back((!sanitize || isalnum(*p)) ? *p : '_');
891 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700892}
893
Dan Albertc7915a32015-05-18 16:46:31 -0700894static void append_transport(const atransport* t, std::string* result,
895 bool long_listing) {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700896 const char* serial = t->serial;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700897 if (!serial || !serial[0]) {
Dan Albertd99d9022015-05-06 16:48:52 -0700898 serial = "(no serial number)";
Elliott Hughese67f1f82015-04-30 17:32:03 -0700899 }
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700900
901 if (!long_listing) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700902 *result += serial;
903 *result += '\t';
904 *result += t->connection_state_name();
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700905 } else {
David Purselld2acbd12015-12-02 15:14:31 -0800906 android::base::StringAppendF(result, "%-22s %s", serial,
907 t->connection_state_name().c_str());
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700908
Elliott Hughese67f1f82015-04-30 17:32:03 -0700909 append_transport_info(result, "", t->devpath, false);
910 append_transport_info(result, "product:", t->product, false);
911 append_transport_info(result, "model:", t->model, true);
912 append_transport_info(result, "device:", t->device, false);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700913 }
Elliott Hughese67f1f82015-04-30 17:32:03 -0700914 *result += '\n';
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700915}
916
Elliott Hughese67f1f82015-04-30 17:32:03 -0700917std::string list_transports(bool long_listing) {
918 std::string result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700920 for (const auto& t : transport_list) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700921 append_transport(t, &result, long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923 adb_mutex_unlock(&transport_lock);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700924 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925}
926
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927/* hack for osx */
Dan Albertc7915a32015-05-18 16:46:31 -0700928void close_usb_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800929 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700930 for (const auto& t : transport_list) {
Yabin Cui7f274902016-04-18 11:22:34 -0700931 t->Kick();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932 }
933 adb_mutex_unlock(&transport_lock);
934}
935#endif // ADB_HOST
936
Dan Albertc7915a32015-05-18 16:46:31 -0700937int register_socket_transport(int s, const char *serial, int port, int local) {
938 atransport* t = new atransport();
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100939
940 if (!serial) {
Dan Albertc7915a32015-05-18 16:46:31 -0700941 char buf[32];
942 snprintf(buf, sizeof(buf), "T-%p", t);
943 serial = buf;
David 'Digit' Turner730ff3b2011-01-06 14:11:07 +0100944 }
Dan Albertc7915a32015-05-18 16:46:31 -0700945
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700946 D("transport: %s init'ing for socket %d, on port %d", serial, s, port);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700947 if (init_socket_transport(t, s, port, local) < 0) {
Dan Albertc7915a32015-05-18 16:46:31 -0700948 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700949 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800950 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700951
952 adb_mutex_lock(&transport_lock);
Elliott Hughes65fe2512015-10-07 15:59:35 -0700953 for (const auto& transport : pending_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700954 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700955 adb_mutex_unlock(&transport_lock);
Yabin Cuib74c6492016-04-29 16:53:52 -0700956 VLOG(TRANSPORT) << "socket transport " << transport->serial
957 << " is already in pending_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700958 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700959 return -1;
960 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800961 }
Benoit Goby1c45ee92013-03-29 18:22:36 -0700962
Elliott Hughes65fe2512015-10-07 15:59:35 -0700963 for (const auto& transport : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700964 if (transport->serial && strcmp(serial, transport->serial) == 0) {
Benoit Goby1c45ee92013-03-29 18:22:36 -0700965 adb_mutex_unlock(&transport_lock);
Yabin Cuib74c6492016-04-29 16:53:52 -0700966 VLOG(TRANSPORT) << "socket transport " << transport->serial
967 << " is already in transport_list and fails to register";
Dan Albertc7915a32015-05-18 16:46:31 -0700968 delete t;
Benoit Goby1c45ee92013-03-29 18:22:36 -0700969 return -1;
970 }
971 }
972
Dan Albertc7915a32015-05-18 16:46:31 -0700973 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700974 t->serial = strdup(serial);
975 adb_mutex_unlock(&transport_lock);
976
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 register_transport(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -0700978 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979}
980
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400981#if ADB_HOST
Dan Albertc7915a32015-05-18 16:46:31 -0700982atransport *find_transport(const char *serial) {
983 atransport* result = nullptr;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400984
985 adb_mutex_lock(&transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700986 for (auto& t : transport_list) {
Dan Albertc7915a32015-05-18 16:46:31 -0700987 if (t->serial && strcmp(serial, t->serial) == 0) {
988 result = t;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400989 break;
990 }
Dan Albertc7915a32015-05-18 16:46:31 -0700991 }
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400992 adb_mutex_unlock(&transport_lock);
993
Dan Albertc7915a32015-05-18 16:46:31 -0700994 return result;
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400995}
996
Yabin Cuif4b99282015-08-27 12:03:11 -0700997void kick_all_tcp_devices() {
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400998 adb_mutex_lock(&transport_lock);
Yabin Cuif4b99282015-08-27 12:03:11 -0700999 for (auto& t : transport_list) {
Yabin Cuib74c6492016-04-29 16:53:52 -07001000 if (t->IsTcpDevice()) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -07001001 // Kicking breaks the read_transport thread of this transport out of any read, then
1002 // the read_transport thread will notify the main thread to make this transport
1003 // offline. Then the main thread will notify the write_transport thread to exit.
Yabin Cuif4b99282015-08-27 12:03:11 -07001004 // Finally, this transport will be closed and freed in the main thread.
Yabin Cui7f274902016-04-18 11:22:34 -07001005 t->Kick();
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001006 }
Dan Albertc7915a32015-05-18 16:46:31 -07001007 }
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -04001008 adb_mutex_unlock(&transport_lock);
1009}
1010
Mike Lockwood8cf0d592009-10-11 23:04:18 -04001011#endif
1012
Dan Albertc7915a32015-05-18 16:46:31 -07001013void register_usb_transport(usb_handle* usb, const char* serial,
1014 const char* devpath, unsigned writeable) {
1015 atransport* t = new atransport();
1016
Yabin Cui7a3f8d62015-09-02 17:44:28 -07001017 D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001018 serial ? serial : "");
Dan Albertdcd78a12015-05-18 16:43:57 -07001019 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001020 if(serial) {
1021 t->serial = strdup(serial);
1022 }
Dan Albertc7915a32015-05-18 16:46:31 -07001023
1024 if (devpath) {
Scott Andersone109d262012-04-20 11:21:14 -07001025 t->devpath = strdup(devpath);
1026 }
Benoit Goby1c45ee92013-03-29 18:22:36 -07001027
1028 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -07001029 pending_list.push_front(t);
Benoit Goby1c45ee92013-03-29 18:22:36 -07001030 adb_mutex_unlock(&transport_lock);
1031
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001032 register_transport(t);
1033}
1034
Dan Albertdcd78a12015-05-18 16:43:57 -07001035// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertc7915a32015-05-18 16:46:31 -07001036void unregister_usb_transport(usb_handle *usb) {
Mike Lockwood0927bf92009-08-08 12:37:44 -04001037 adb_mutex_lock(&transport_lock);
Dan Albertc7915a32015-05-18 16:46:31 -07001038 transport_list.remove_if([usb](atransport* t) {
1039 return t->usb == usb && t->connection_state == kCsNoPerm;
1040 });
Mike Lockwood0927bf92009-08-08 12:37:44 -04001041 adb_mutex_unlock(&transport_lock);
1042}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001043
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001044int check_header(apacket *p, atransport *t)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001045{
1046 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001047 VLOG(RWX) << "check_header(): invalid magic";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048 return -1;
1049 }
1050
Tamas Berghammer3d2904c2015-07-13 19:12:28 +01001051 if(p->msg.data_length > t->get_max_payload()) {
Yabin Cuiaed3c612015-09-22 15:52:57 -07001052 VLOG(RWX) << "check_header(): " << p->msg.data_length << " atransport::max_payload = "
1053 << t->get_max_payload();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 return -1;
1055 }
1056
1057 return 0;
1058}
1059
1060int check_data(apacket *p)
1061{
1062 unsigned count, sum;
1063 unsigned char *x;
1064
1065 count = p->msg.data_length;
1066 x = p->data;
1067 sum = 0;
1068 while(count-- > 0) {
1069 sum += *x++;
1070 }
1071
1072 if(sum != p->msg.data_check) {
1073 return -1;
1074 } else {
1075 return 0;
1076 }
1077}
Elliott Hughes0aeb5052016-06-29 17:42:01 -07001078
1079RSA* atransport::NextKey() {
1080 if (keys_.empty()) keys_ = adb_auth_get_private_keys();
1081
1082 RSA* result = keys_[0];
1083 keys_.pop_front();
1084 return result;
1085}