blob: 4b9eeeb7bed24c81bc78252b8a55d3f5afc3b3b6 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Albertdb6fe642015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_TRANSPORT
Dan Albertb302d122015-02-24 15:51:19 -080018
Dan Albertdb6fe642015-03-19 15:21:08 -070019#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080020#include "transport.h"
21
Dan Albert4895c522015-02-20 17:24:58 -080022#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080023#include <errno.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include <string.h>
Dan Albertb302d122015-02-24 15:51:19 -080027#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080029#include "adb.h"
30
31static void transport_unref(atransport *t);
32
33static atransport transport_list = {
34 .next = &transport_list,
35 .prev = &transport_list,
36};
37
Benoit Goby3f9f9ce2013-03-29 18:22:36 -070038static atransport pending_list = {
39 .next = &pending_list,
40 .prev = &pending_list,
41};
42
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080043ADB_MUTEX_DEFINE( transport_lock );
44
45#if ADB_TRACE
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070046#define MAX_DUMP_HEX_LEN 16
Dan Albert66a91b02015-02-24 21:26:58 -080047void dump_hex(const unsigned char* ptr, size_t len)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048{
49 int nn, len2 = len;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070050 // Build a string instead of logging each character.
51 // MAX chars in 2 digit hex, one space, MAX chars, one '\0'.
52 char buffer[MAX_DUMP_HEX_LEN *2 + 1 + MAX_DUMP_HEX_LEN + 1 ], *pb = buffer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070054 if (len2 > MAX_DUMP_HEX_LEN) len2 = MAX_DUMP_HEX_LEN;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070056 for (nn = 0; nn < len2; nn++) {
57 sprintf(pb, "%02x", ptr[nn]);
58 pb += 2;
59 }
60 sprintf(pb++, " ");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080061
62 for (nn = 0; nn < len2; nn++) {
63 int c = ptr[nn];
64 if (c < 32 || c > 127)
65 c = '.';
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070066 *pb++ = c;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070068 *pb++ = '\0';
69 DR("%s\n", buffer);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070}
71#endif
72
Dan Albert4895c522015-02-20 17:24:58 -080073void kick_transport(atransport* t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074{
75 if (t && !t->kicked)
76 {
77 int kicked;
78
79 adb_mutex_lock(&transport_lock);
80 kicked = t->kicked;
81 if (!kicked)
82 t->kicked = 1;
83 adb_mutex_unlock(&transport_lock);
84
85 if (!kicked)
86 t->kick(t);
87 }
88}
89
Dan Albert4895c522015-02-20 17:24:58 -080090// Each atransport contains a list of adisconnects (t->disconnects).
91// An adisconnect contains a link to the next/prev adisconnect, a function
92// pointer to a disconnect callback which takes a void* piece of user data and
93// the atransport, and some user data for the callback (helpfully named
94// "opaque").
95//
96// The list is circular. New items are added to the entry member of the list
97// (t->disconnects) by add_transport_disconnect.
98//
99// run_transport_disconnects invokes each function in the list.
100//
101// Gotchas:
102// * run_transport_disconnects assumes that t->disconnects is non-null, so
103// this can't be run on a zeroed atransport.
104// * The callbacks in this list are not removed when called, and this function
105// is not guarded against running more than once. As such, ensure that this
106// function is not called multiple times on the same atransport.
107// TODO(danalbert): Just fix this so that it is guarded once you have tests.
108void run_transport_disconnects(atransport* t)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109{
110 adisconnect* dis = t->disconnects.next;
111
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100112 D("%s: run_transport_disconnects\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800113 while (dis != &t->disconnects) {
114 adisconnect* next = dis->next;
115 dis->func( dis->opaque, t );
116 dis = next;
117 }
118}
119
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100120#if ADB_TRACE
121static void
122dump_packet(const char* name, const char* func, apacket* p)
123{
124 unsigned command = p->msg.command;
125 int len = p->msg.data_length;
126 char cmd[9];
127 char arg0[12], arg1[12];
128 int n;
129
130 for (n = 0; n < 4; n++) {
131 int b = (command >> (n*8)) & 255;
132 if (b < 32 || b >= 127)
133 break;
134 cmd[n] = (char)b;
135 }
136 if (n == 4) {
137 cmd[4] = 0;
138 } else {
139 /* There is some non-ASCII name in the command, so dump
140 * the hexadecimal value instead */
141 snprintf(cmd, sizeof cmd, "%08x", command);
142 }
143
144 if (p->msg.arg0 < 256U)
145 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
146 else
147 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
148
149 if (p->msg.arg1 < 256U)
150 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
151 else
152 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
153
154 D("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
155 name, func, cmd, arg0, arg1, len);
156 dump_hex(p->data, len);
157}
158#endif /* ADB_TRACE */
159
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100161read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800162{
163 char *p = (char*)ppacket; /* really read a packet address */
164 int r;
165 int len = sizeof(*ppacket);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100166 char buff[8];
167 if (!name) {
168 snprintf(buff, sizeof buff, "fd=%d", fd);
169 name = buff;
170 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171 while(len > 0) {
172 r = adb_read(fd, p, len);
173 if(r > 0) {
174 len -= r;
175 p += r;
176 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100177 D("%s: read_packet (fd=%d), error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 if((r < 0) && (errno == EINTR)) continue;
179 return -1;
180 }
181 }
182
183#if ADB_TRACE
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100184 if (ADB_TRACING) {
185 dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186 }
187#endif
188 return 0;
189}
190
191static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100192write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193{
194 char *p = (char*) ppacket; /* we really write the packet address */
195 int r, len = sizeof(ppacket);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100196 char buff[8];
197 if (!name) {
198 snprintf(buff, sizeof buff, "fd=%d", fd);
199 name = buff;
200 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201
202#if ADB_TRACE
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100203 if (ADB_TRACING) {
204 dump_packet(name, "to remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205 }
206#endif
207 len = sizeof(ppacket);
208 while(len > 0) {
209 r = adb_write(fd, p, len);
210 if(r > 0) {
211 len -= r;
212 p += r;
213 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100214 D("%s: write_packet (fd=%d) error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800215 if((r < 0) && (errno == EINTR)) continue;
216 return -1;
217 }
218 }
219 return 0;
220}
221
222static void transport_socket_events(int fd, unsigned events, void *_t)
223{
Dan Albertf30d73c2015-02-25 17:51:28 -0800224 atransport *t = reinterpret_cast<atransport*>(_t);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700225 D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226 if(events & FDE_READ){
227 apacket *p = 0;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100228 if(read_packet(fd, t->serial, &p)){
229 D("%s: failed to read packet from transport socket on fd %d\n", t->serial, fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800230 } else {
231 handle_packet(p, (atransport *) _t);
232 }
233 }
234}
235
236void send_packet(apacket *p, atransport *t)
237{
238 unsigned char *x;
239 unsigned sum;
240 unsigned count;
241
242 p->msg.magic = p->msg.command ^ 0xffffffff;
243
244 count = p->msg.data_length;
245 x = (unsigned char *) p->data;
246 sum = 0;
247 while(count-- > 0){
248 sum += *x++;
249 }
250 p->msg.data_check = sum;
251
252 print_packet("send", p);
253
254 if (t == NULL) {
JP Abgrall1f501f32011-02-23 18:44:39 -0800255 D("Transport is null \n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700256 // Zap errno because print_packet() and other stuff have errno effect.
257 errno = 0;
258 fatal_errno("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800259 }
260
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100261 if(write_packet(t->transport_socket, t->serial, &p)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262 fatal_errno("cannot enqueue packet on transport socket");
263 }
264}
265
266/* The transport is opened by transport_register_func before
267** the input and output threads are started.
268**
269** The output thread issues a SYNC(1, token) message to let
270** the input thread know to start things up. In the event
271** of transport IO failure, the output thread will post a
272** SYNC(0,0) message to ensure shutdown.
273**
274** The transport will not actually be closed until both
275** threads exit, but the input thread will kick the transport
276** on its way out to disconnect the underlying device.
277*/
278
279static void *output_thread(void *_t)
280{
Dan Albertf30d73c2015-02-25 17:51:28 -0800281 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282 apacket *p;
283
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100284 D("%s: starting transport output thread on fd %d, SYNC online (%d)\n",
285 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286 p = get_apacket();
287 p->msg.command = A_SYNC;
288 p->msg.arg0 = 1;
289 p->msg.arg1 = ++(t->sync_token);
290 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100291 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100293 D("%s: failed to write SYNC packet\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800294 goto oops;
295 }
296
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100297 D("%s: data pump started\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800298 for(;;) {
299 p = get_apacket();
300
301 if(t->read_from_remote(p, t) == 0){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100302 D("%s: received remote packet, sending to transport\n",
303 t->serial);
304 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800305 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100306 D("%s: failed to write apacket to transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307 goto oops;
308 }
309 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100310 D("%s: remote read failed for transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800311 put_apacket(p);
312 break;
313 }
314 }
315
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100316 D("%s: SYNC offline for transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 p = get_apacket();
318 p->msg.command = A_SYNC;
319 p->msg.arg0 = 0;
320 p->msg.arg1 = 0;
321 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100322 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100324 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325 }
326
327oops:
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100328 D("%s: transport output thread is exiting\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800329 kick_transport(t);
330 transport_unref(t);
331 return 0;
332}
333
334static void *input_thread(void *_t)
335{
Dan Albertf30d73c2015-02-25 17:51:28 -0800336 atransport *t = reinterpret_cast<atransport*>(_t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337 apacket *p;
338 int active = 0;
339
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100340 D("%s: starting transport input thread, reading from fd %d\n",
341 t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342
343 for(;;){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100344 if(read_packet(t->fd, t->serial, &p)) {
345 D("%s: failed to read apacket from transport on fd %d\n",
346 t->serial, t->fd );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 break;
348 }
349 if(p->msg.command == A_SYNC){
350 if(p->msg.arg0 == 0) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100351 D("%s: transport SYNC offline\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800352 put_apacket(p);
353 break;
354 } else {
355 if(p->msg.arg1 == t->sync_token) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100356 D("%s: transport SYNC online\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800357 active = 1;
358 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100359 D("%s: transport ignoring SYNC %d != %d\n",
360 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800361 }
362 }
363 } else {
364 if(active) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100365 D("%s: transport got packet, sending to remote\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800366 t->write_to_remote(p, t);
367 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100368 D("%s: transport ignoring packet while offline\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 }
370 }
371
372 put_apacket(p);
373 }
374
375 // this is necessary to avoid a race condition that occured when a transport closes
376 // while a client socket is still active.
377 close_all_sockets(t);
378
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100379 D("%s: transport input thread is exiting, fd %d\n", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380 kick_transport(t);
381 transport_unref(t);
382 return 0;
383}
384
385
386static int transport_registration_send = -1;
387static int transport_registration_recv = -1;
388static fdevent transport_registration_fde;
389
390
391#if ADB_HOST
392static int list_transports_msg(char* buffer, size_t bufferlen)
393{
394 char head[5];
395 int len;
396
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700397 len = list_transports(buffer+4, bufferlen-4, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 snprintf(head, sizeof(head), "%04x", len);
399 memcpy(buffer, head, 4);
400 len += 4;
401 return len;
402}
403
404/* this adds support required by the 'track-devices' service.
405 * this is used to send the content of "list_transport" to any
406 * number of client connections that want it through a single
407 * live TCP connection
408 */
409typedef struct device_tracker device_tracker;
410struct device_tracker {
411 asocket socket;
412 int update_needed;
413 device_tracker* next;
414};
415
416/* linked list of all device trackers */
417static device_tracker* device_tracker_list;
418
419static void
420device_tracker_remove( device_tracker* tracker )
421{
422 device_tracker** pnode = &device_tracker_list;
423 device_tracker* node = *pnode;
424
425 adb_mutex_lock( &transport_lock );
426 while (node) {
427 if (node == tracker) {
428 *pnode = node->next;
429 break;
430 }
431 pnode = &node->next;
432 node = *pnode;
433 }
434 adb_mutex_unlock( &transport_lock );
435}
436
437static void
438device_tracker_close( asocket* socket )
439{
440 device_tracker* tracker = (device_tracker*) socket;
441 asocket* peer = socket->peer;
442
443 D( "device tracker %p removed\n", tracker);
444 if (peer) {
445 peer->peer = NULL;
446 peer->close(peer);
447 }
448 device_tracker_remove(tracker);
449 free(tracker);
450}
451
452static int
453device_tracker_enqueue( asocket* socket, apacket* p )
454{
455 /* you can't read from a device tracker, close immediately */
456 put_apacket(p);
457 device_tracker_close(socket);
458 return -1;
459}
460
461static int
462device_tracker_send( device_tracker* tracker,
463 const char* buffer,
464 int len )
465{
466 apacket* p = get_apacket();
467 asocket* peer = tracker->socket.peer;
468
469 memcpy(p->data, buffer, len);
470 p->len = len;
471 return peer->enqueue( peer, p );
472}
473
474
475static void
476device_tracker_ready( asocket* socket )
477{
478 device_tracker* tracker = (device_tracker*) socket;
479
480 /* we want to send the device list when the tracker connects
481 * for the first time, even if no update occured */
482 if (tracker->update_needed > 0) {
483 char buffer[1024];
484 int len;
485
486 tracker->update_needed = 0;
487
488 len = list_transports_msg(buffer, sizeof(buffer));
489 device_tracker_send(tracker, buffer, len);
490 }
491}
492
493
494asocket*
495create_device_tracker(void)
496{
Dan Albertf30d73c2015-02-25 17:51:28 -0800497 device_tracker* tracker = reinterpret_cast<device_tracker*>(
498 calloc(1, sizeof(*tracker)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800499
500 if(tracker == 0) fatal("cannot allocate device tracker");
501
502 D( "device tracker %p created\n", tracker);
503
504 tracker->socket.enqueue = device_tracker_enqueue;
505 tracker->socket.ready = device_tracker_ready;
506 tracker->socket.close = device_tracker_close;
507 tracker->update_needed = 1;
508
509 tracker->next = device_tracker_list;
510 device_tracker_list = tracker;
511
512 return &tracker->socket;
513}
514
515
516/* call this function each time the transport list has changed */
Dan Albertb302d122015-02-24 15:51:19 -0800517void update_transports(void) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800518 char buffer[1024];
519 int len;
520 device_tracker* tracker;
521
522 len = list_transports_msg(buffer, sizeof(buffer));
523
524 tracker = device_tracker_list;
525 while (tracker != NULL) {
526 device_tracker* next = tracker->next;
527 /* note: this may destroy the tracker if the connection is closed */
528 device_tracker_send(tracker, buffer, len);
529 tracker = next;
530 }
531}
532#else
533void update_transports(void)
534{
535 // nothing to do on the device side
536}
537#endif // ADB_HOST
538
539typedef struct tmsg tmsg;
540struct tmsg
541{
542 atransport *transport;
543 int action;
544};
545
546static int
547transport_read_action(int fd, struct tmsg* m)
548{
549 char *p = (char*)m;
550 int len = sizeof(*m);
551 int r;
552
553 while(len > 0) {
554 r = adb_read(fd, p, len);
555 if(r > 0) {
556 len -= r;
557 p += r;
558 } else {
559 if((r < 0) && (errno == EINTR)) continue;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100560 D("transport_read_action: on fd %d, error %d: %s\n",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800561 fd, errno, strerror(errno));
562 return -1;
563 }
564 }
565 return 0;
566}
567
568static int
569transport_write_action(int fd, struct tmsg* m)
570{
571 char *p = (char*)m;
572 int len = sizeof(*m);
573 int r;
574
575 while(len > 0) {
576 r = adb_write(fd, p, len);
577 if(r > 0) {
578 len -= r;
579 p += r;
580 } else {
581 if((r < 0) && (errno == EINTR)) continue;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100582 D("transport_write_action: on fd %d, error %d: %s\n",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800583 fd, errno, strerror(errno));
584 return -1;
585 }
586 }
587 return 0;
588}
589
590static void transport_registration_func(int _fd, unsigned ev, void *data)
591{
592 tmsg m;
593 adb_thread_t output_thread_ptr;
594 adb_thread_t input_thread_ptr;
595 int s[2];
596 atransport *t;
597
598 if(!(ev & FDE_READ)) {
599 return;
600 }
601
602 if(transport_read_action(_fd, &m)) {
603 fatal_errno("cannot read transport registration socket");
604 }
605
606 t = m.transport;
607
608 if(m.action == 0){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100609 D("transport: %s removing and free'ing %d\n", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800610
611 /* IMPORTANT: the remove closes one half of the
612 ** socket pair. The close closes the other half.
613 */
614 fdevent_remove(&(t->transport_fde));
615 adb_close(t->fd);
616
617 adb_mutex_lock(&transport_lock);
618 t->next->prev = t->prev;
619 t->prev->next = t->next;
620 adb_mutex_unlock(&transport_lock);
621
622 run_transport_disconnects(t);
623
624 if (t->product)
625 free(t->product);
626 if (t->serial)
627 free(t->serial);
Scott Andersonfa020922012-05-25 14:10:02 -0700628 if (t->model)
629 free(t->model);
630 if (t->device)
631 free(t->device);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700632 if (t->devpath)
633 free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800634
635 memset(t,0xee,sizeof(atransport));
636 free(t);
637
638 update_transports();
639 return;
640 }
641
Mike Lockwoode45583f2009-08-08 12:37:44 -0400642 /* don't create transport threads for inaccessible devices */
643 if (t->connection_state != CS_NOPERM) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800644 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400645 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800646
Mike Lockwoode45583f2009-08-08 12:37:44 -0400647 if(adb_socketpair(s)) {
648 fatal_errno("cannot open transport socketpair");
649 }
650
leozwang1be54622014-08-15 09:51:27 -0700651 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400652
653 t->transport_socket = s[0];
654 t->fd = s[1];
655
Mike Lockwoode45583f2009-08-08 12:37:44 -0400656 fdevent_install(&(t->transport_fde),
657 t->transport_socket,
658 transport_socket_events,
659 t);
660
661 fdevent_set(&(t->transport_fde), FDE_READ);
662
663 if(adb_thread_create(&input_thread_ptr, input_thread, t)){
664 fatal_errno("cannot create input thread");
665 }
666
667 if(adb_thread_create(&output_thread_ptr, output_thread, t)){
668 fatal_errno("cannot create output thread");
669 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800670 }
671
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800672 adb_mutex_lock(&transport_lock);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700673 /* remove from pending list */
674 t->next->prev = t->prev;
675 t->prev->next = t->next;
676 /* put us on the master device list */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677 t->next = &transport_list;
678 t->prev = transport_list.prev;
679 t->next->prev = t;
680 t->prev->next = t;
681 adb_mutex_unlock(&transport_lock);
682
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800683 t->disconnects.next = t->disconnects.prev = &t->disconnects;
684
685 update_transports();
686}
687
688void init_transport_registration(void)
689{
690 int s[2];
691
692 if(adb_socketpair(s)){
693 fatal_errno("cannot open transport registration socketpair");
694 }
leozwang1be54622014-08-15 09:51:27 -0700695 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800696
697 transport_registration_send = s[0];
698 transport_registration_recv = s[1];
699
700 fdevent_install(&transport_registration_fde,
701 transport_registration_recv,
702 transport_registration_func,
703 0);
704
705 fdevent_set(&transport_registration_fde, FDE_READ);
706}
707
708/* the fdevent select pump is single threaded */
709static void register_transport(atransport *transport)
710{
711 tmsg m;
712 m.transport = transport;
713 m.action = 1;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100714 D("transport: %s registered\n", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800715 if(transport_write_action(transport_registration_send, &m)) {
716 fatal_errno("cannot write transport registration socket\n");
717 }
718}
719
720static void remove_transport(atransport *transport)
721{
722 tmsg m;
723 m.transport = transport;
724 m.action = 0;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100725 D("transport: %s removed\n", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800726 if(transport_write_action(transport_registration_send, &m)) {
727 fatal_errno("cannot write transport registration socket\n");
728 }
729}
730
731
Mike Lockwood01c2c302010-05-24 10:44:35 -0400732static void transport_unref_locked(atransport *t)
733{
734 t->ref_count--;
Mike Lockwood01c2c302010-05-24 10:44:35 -0400735 if (t->ref_count == 0) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100736 D("transport: %s unref (kicking and closing)\n", t->serial);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400737 if (!t->kicked) {
738 t->kicked = 1;
739 t->kick(t);
740 }
741 t->close(t);
742 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100743 } else {
744 D("transport: %s unref (count=%d)\n", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400745 }
746}
747
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800748static void transport_unref(atransport *t)
749{
750 if (t) {
751 adb_mutex_lock(&transport_lock);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400752 transport_unref_locked(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800753 adb_mutex_unlock(&transport_lock);
754 }
755}
756
757void add_transport_disconnect(atransport* t, adisconnect* dis)
758{
759 adb_mutex_lock(&transport_lock);
760 dis->next = &t->disconnects;
761 dis->prev = dis->next->prev;
762 dis->prev->next = dis;
763 dis->next->prev = dis;
764 adb_mutex_unlock(&transport_lock);
765}
766
767void remove_transport_disconnect(atransport* t, adisconnect* dis)
768{
769 dis->prev->next = dis->next;
770 dis->next->prev = dis->prev;
771 dis->next = dis->prev = dis;
772}
773
Scott Anderson27042382012-05-30 18:11:27 -0700774static int qual_match(const char *to_test,
Elliott Hughesabfa7202015-04-03 16:12:15 -0700775 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson27042382012-05-30 18:11:27 -0700776{
777 if (!to_test || !*to_test)
778 /* Return true if both the qual and to_test are null strings. */
779 return !qual || !*qual;
780
781 if (!qual)
782 return 0;
783
784 if (prefix) {
785 while (*prefix) {
786 if (*prefix++ != *to_test++)
787 return 0;
788 }
789 }
790
791 while (*qual) {
792 char ch = *qual++;
Elliott Hughesabfa7202015-04-03 16:12:15 -0700793 if (sanitize_qual && !isalnum(ch))
Scott Anderson27042382012-05-30 18:11:27 -0700794 ch = '_';
795 if (ch != *to_test++)
796 return 0;
797 }
798
799 /* Everything matched so far. Return true if *to_test is a NUL. */
800 return !*to_test;
801}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800802
Dan Albertf30d73c2015-02-25 17:51:28 -0800803atransport *acquire_one_transport(int state, transport_type ttype,
804 const char* serial, const char** error_out)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800805{
806 atransport *t;
807 atransport *result = NULL;
808 int ambiguous = 0;
809
810retry:
811 if (error_out)
812 *error_out = "device not found";
813
814 adb_mutex_lock(&transport_lock);
815 for (t = transport_list.next; t != &transport_list; t = t->next) {
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400816 if (t->connection_state == CS_NOPERM) {
817 if (error_out)
818 *error_out = "insufficient permissions for device";
819 continue;
820 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400821
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800822 /* check for matching serial number */
823 if (serial) {
Scott Anderson27042382012-05-30 18:11:27 -0700824 if ((t->serial && !strcmp(serial, t->serial)) ||
825 (t->devpath && !strcmp(serial, t->devpath)) ||
Elliott Hughesabfa7202015-04-03 16:12:15 -0700826 qual_match(serial, "product:", t->product, false) ||
827 qual_match(serial, "model:", t->model, true) ||
828 qual_match(serial, "device:", t->device, false)) {
Scott Anderson27042382012-05-30 18:11:27 -0700829 if (result) {
830 if (error_out)
831 *error_out = "more than one device";
832 ambiguous = 1;
833 result = NULL;
834 break;
835 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800836 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700837 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800838 } else {
839 if (ttype == kTransportUsb && t->type == kTransportUsb) {
840 if (result) {
841 if (error_out)
842 *error_out = "more than one device";
843 ambiguous = 1;
844 result = NULL;
845 break;
846 }
847 result = t;
848 } else if (ttype == kTransportLocal && t->type == kTransportLocal) {
849 if (result) {
850 if (error_out)
851 *error_out = "more than one emulator";
852 ambiguous = 1;
853 result = NULL;
854 break;
855 }
856 result = t;
857 } else if (ttype == kTransportAny) {
858 if (result) {
859 if (error_out)
860 *error_out = "more than one device and emulator";
861 ambiguous = 1;
862 result = NULL;
863 break;
864 }
865 result = t;
866 }
867 }
868 }
869 adb_mutex_unlock(&transport_lock);
870
871 if (result) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700872 if (result->connection_state == CS_UNAUTHORIZED) {
873 if (error_out)
874 *error_out = "device unauthorized. Please check the confirmation dialog on your device.";
875 result = NULL;
876 }
877
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800878 /* offline devices are ignored -- they are either being born or dying */
879 if (result && result->connection_state == CS_OFFLINE) {
880 if (error_out)
881 *error_out = "device offline";
882 result = NULL;
883 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800884 /* check for required connection state */
885 if (result && state != CS_ANY && result->connection_state != state) {
886 if (error_out)
887 *error_out = "invalid device state";
888 result = NULL;
889 }
890 }
891
892 if (result) {
893 /* found one that we can take */
894 if (error_out)
895 *error_out = NULL;
896 } else if (state != CS_ANY && (serial || !ambiguous)) {
897 adb_sleep_ms(1000);
898 goto retry;
899 }
900
901 return result;
902}
903
904#if ADB_HOST
905static const char *statename(atransport *t)
906{
907 switch(t->connection_state){
908 case CS_OFFLINE: return "offline";
909 case CS_BOOTLOADER: return "bootloader";
910 case CS_DEVICE: return "device";
911 case CS_HOST: return "host";
912 case CS_RECOVERY: return "recovery";
Doug Zongker6b217ed2012-01-09 14:54:53 -0800913 case CS_SIDELOAD: return "sideload";
Mike Lockwoode45583f2009-08-08 12:37:44 -0400914 case CS_NOPERM: return "no permissions";
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700915 case CS_UNAUTHORIZED: return "unauthorized";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800916 default: return "unknown";
917 }
918}
919
Scott Anderson27042382012-05-30 18:11:27 -0700920static void add_qual(char **buf, size_t *buf_size,
Elliott Hughesabfa7202015-04-03 16:12:15 -0700921 const char *prefix, const char *qual, bool sanitize_qual)
Scott Anderson27042382012-05-30 18:11:27 -0700922{
Scott Anderson27042382012-05-30 18:11:27 -0700923 if (!buf || !*buf || !buf_size || !*buf_size || !qual || !*qual)
924 return;
925
Elliott Hughesabfa7202015-04-03 16:12:15 -0700926 int prefix_len;
927 size_t len = snprintf(*buf, *buf_size, "%s%n%s", prefix, &prefix_len, qual);
Scott Anderson27042382012-05-30 18:11:27 -0700928
929 if (sanitize_qual) {
Elliott Hughesabfa7202015-04-03 16:12:15 -0700930 for (char* cp = *buf + prefix_len; cp < *buf + len; cp++) {
931 if (!isalnum(*cp))
Scott Anderson27042382012-05-30 18:11:27 -0700932 *cp = '_';
933 }
934 }
935
936 *buf_size -= len;
937 *buf += len;
938}
939
940static size_t format_transport(atransport *t, char *buf, size_t bufsize,
941 int long_listing)
942{
943 const char* serial = t->serial;
944 if (!serial || !serial[0])
945 serial = "????????????";
946
947 if (!long_listing) {
948 return snprintf(buf, bufsize, "%s\t%s\n", serial, statename(t));
949 } else {
950 size_t len, remaining = bufsize;
951
952 len = snprintf(buf, remaining, "%-22s %s", serial, statename(t));
953 remaining -= len;
954 buf += len;
955
Elliott Hughesabfa7202015-04-03 16:12:15 -0700956 add_qual(&buf, &remaining, " ", t->devpath, false);
957 add_qual(&buf, &remaining, " product:", t->product, false);
958 add_qual(&buf, &remaining, " model:", t->model, true);
959 add_qual(&buf, &remaining, " device:", t->device, false);
Scott Anderson27042382012-05-30 18:11:27 -0700960
961 len = snprintf(buf, remaining, "\n");
962 remaining -= len;
963
964 return bufsize - remaining;
965 }
966}
967
968int list_transports(char *buf, size_t bufsize, int long_listing)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800969{
970 char* p = buf;
971 char* end = buf + bufsize;
972 int len;
973 atransport *t;
974
975 /* XXX OVERRUN PROBLEMS XXX */
976 adb_mutex_lock(&transport_lock);
977 for(t = transport_list.next; t != &transport_list; t = t->next) {
Scott Anderson27042382012-05-30 18:11:27 -0700978 len = format_transport(t, p, end - p, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800979 if (p + len >= end) {
980 /* discard last line if buffer is too short */
981 break;
982 }
983 p += len;
984 }
985 p[0] = 0;
986 adb_mutex_unlock(&transport_lock);
987 return p - buf;
988}
989
990
991/* hack for osx */
992void close_usb_devices()
993{
994 atransport *t;
995
996 adb_mutex_lock(&transport_lock);
997 for(t = transport_list.next; t != &transport_list; t = t->next) {
998 if ( !t->kicked ) {
999 t->kicked = 1;
1000 t->kick(t);
1001 }
1002 }
1003 adb_mutex_unlock(&transport_lock);
1004}
1005#endif // ADB_HOST
1006
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001007int register_socket_transport(int s, const char *serial, int port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001008{
Dan Albertf30d73c2015-02-25 17:51:28 -08001009 atransport *t = reinterpret_cast<atransport*>(
1010 calloc(1, sizeof(atransport)));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001011 atransport *n;
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001012 char buff[32];
1013
1014 if (!serial) {
1015 snprintf(buff, sizeof buff, "T-%p", t);
1016 serial = buff;
1017 }
1018 D("transport: %s init'ing for socket %d, on port %d\n", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001019 if (init_socket_transport(t, s, port, local) < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001020 free(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001021 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001022 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001023
1024 adb_mutex_lock(&transport_lock);
1025 for (n = pending_list.next; n != &pending_list; n = n->next) {
1026 if (n->serial && !strcmp(serial, n->serial)) {
1027 adb_mutex_unlock(&transport_lock);
1028 free(t);
1029 return -1;
1030 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001031 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001032
1033 for (n = transport_list.next; n != &transport_list; n = n->next) {
1034 if (n->serial && !strcmp(serial, n->serial)) {
1035 adb_mutex_unlock(&transport_lock);
1036 free(t);
1037 return -1;
1038 }
1039 }
1040
1041 t->next = &pending_list;
1042 t->prev = pending_list.prev;
1043 t->next->prev = t;
1044 t->prev->next = t;
1045 t->serial = strdup(serial);
1046 adb_mutex_unlock(&transport_lock);
1047
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001048 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001049 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001050}
1051
Mike Lockwood81ffe172009-10-11 23:04:18 -04001052#if ADB_HOST
1053atransport *find_transport(const char *serial)
1054{
1055 atransport *t;
1056
1057 adb_mutex_lock(&transport_lock);
1058 for(t = transport_list.next; t != &transport_list; t = t->next) {
1059 if (t->serial && !strcmp(serial, t->serial)) {
1060 break;
1061 }
1062 }
1063 adb_mutex_unlock(&transport_lock);
1064
1065 if (t != &transport_list)
1066 return t;
1067 else
1068 return 0;
1069}
1070
1071void unregister_transport(atransport *t)
1072{
1073 adb_mutex_lock(&transport_lock);
1074 t->next->prev = t->prev;
1075 t->prev->next = t->next;
1076 adb_mutex_unlock(&transport_lock);
1077
1078 kick_transport(t);
1079 transport_unref(t);
1080}
1081
Mike Lockwood01c2c302010-05-24 10:44:35 -04001082// unregisters all non-emulator TCP transports
1083void unregister_all_tcp_transports()
1084{
1085 atransport *t, *next;
1086 adb_mutex_lock(&transport_lock);
1087 for (t = transport_list.next; t != &transport_list; t = next) {
1088 next = t->next;
1089 if (t->type == kTransportLocal && t->adb_port == 0) {
1090 t->next->prev = t->prev;
1091 t->prev->next = next;
1092 // we cannot call kick_transport when holding transport_lock
1093 if (!t->kicked)
1094 {
1095 t->kicked = 1;
1096 t->kick(t);
1097 }
1098 transport_unref_locked(t);
1099 }
1100 }
1101
1102 adb_mutex_unlock(&transport_lock);
1103}
1104
Mike Lockwood81ffe172009-10-11 23:04:18 -04001105#endif
1106
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001107void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001108{
Dan Albertf30d73c2015-02-25 17:51:28 -08001109 atransport *t = reinterpret_cast<atransport*>(
1110 calloc(1, sizeof(atransport)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001111 D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb,
1112 serial ? serial : "");
Mike Lockwoode45583f2009-08-08 12:37:44 -04001113 init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001114 if(serial) {
1115 t->serial = strdup(serial);
1116 }
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001117 if(devpath) {
1118 t->devpath = strdup(devpath);
1119 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001120
1121 adb_mutex_lock(&transport_lock);
1122 t->next = &pending_list;
1123 t->prev = pending_list.prev;
1124 t->next->prev = t;
1125 t->prev->next = t;
1126 adb_mutex_unlock(&transport_lock);
1127
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001128 register_transport(t);
1129}
1130
Mike Lockwoode45583f2009-08-08 12:37:44 -04001131/* this should only be used for transports with connection_state == CS_NOPERM */
1132void unregister_usb_transport(usb_handle *usb)
1133{
1134 atransport *t;
1135 adb_mutex_lock(&transport_lock);
1136 for(t = transport_list.next; t != &transport_list; t = t->next) {
1137 if (t->usb == usb && t->connection_state == CS_NOPERM) {
1138 t->next->prev = t->prev;
1139 t->prev->next = t->next;
1140 break;
1141 }
1142 }
1143 adb_mutex_unlock(&transport_lock);
1144}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001145
1146#undef TRACE_TAG
1147#define TRACE_TAG TRACE_RWX
1148
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001149int check_header(apacket *p)
1150{
1151 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
1152 D("check_header(): invalid magic\n");
1153 return -1;
1154 }
1155
1156 if(p->msg.data_length > MAX_PAYLOAD) {
1157 D("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length);
1158 return -1;
1159 }
1160
1161 return 0;
1162}
1163
1164int check_data(apacket *p)
1165{
1166 unsigned count, sum;
1167 unsigned char *x;
1168
1169 count = p->msg.data_length;
1170 x = p->data;
1171 sum = 0;
1172 while(count-- > 0) {
1173 sum += *x++;
1174 }
1175
1176 if(sum != p->msg.data_check) {
1177 return -1;
1178 } else {
1179 return 0;
1180 }
1181}