blob: 7db6a4783e032567b4159b7db6fa19595343f59e [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22
23#include "sysdeps.h"
24
25#define TRACE_TAG TRACE_TRANSPORT
26#include "adb.h"
27
28static void transport_unref(atransport *t);
29
30static atransport transport_list = {
31 .next = &transport_list,
32 .prev = &transport_list,
33};
34
Benoit Goby3f9f9ce2013-03-29 18:22:36 -070035static atransport pending_list = {
36 .next = &pending_list,
37 .prev = &pending_list,
38};
39
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040ADB_MUTEX_DEFINE( transport_lock );
41
42#if ADB_TRACE
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070043#define MAX_DUMP_HEX_LEN 16
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044static void dump_hex( const unsigned char* ptr, size_t len )
45{
46 int nn, len2 = len;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070047 // Build a string instead of logging each character.
48 // MAX chars in 2 digit hex, one space, MAX chars, one '\0'.
49 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 -080050
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070051 if (len2 > MAX_DUMP_HEX_LEN) len2 = MAX_DUMP_HEX_LEN;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080052
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070053 for (nn = 0; nn < len2; nn++) {
54 sprintf(pb, "%02x", ptr[nn]);
55 pb += 2;
56 }
57 sprintf(pb++, " ");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058
59 for (nn = 0; nn < len2; nn++) {
60 int c = ptr[nn];
61 if (c < 32 || c > 127)
62 c = '.';
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070063 *pb++ = c;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064 }
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070065 *pb++ = '\0';
66 DR("%s\n", buffer);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067}
68#endif
69
70void
71kick_transport(atransport* t)
72{
73 if (t && !t->kicked)
74 {
75 int kicked;
76
77 adb_mutex_lock(&transport_lock);
78 kicked = t->kicked;
79 if (!kicked)
80 t->kicked = 1;
81 adb_mutex_unlock(&transport_lock);
82
83 if (!kicked)
84 t->kick(t);
85 }
86}
87
88void
89run_transport_disconnects(atransport* t)
90{
91 adisconnect* dis = t->disconnects.next;
92
David 'Digit' Turner58f59682011-01-06 14:11:07 +010093 D("%s: run_transport_disconnects\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094 while (dis != &t->disconnects) {
95 adisconnect* next = dis->next;
96 dis->func( dis->opaque, t );
97 dis = next;
98 }
99}
100
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100101#if ADB_TRACE
102static void
103dump_packet(const char* name, const char* func, apacket* p)
104{
105 unsigned command = p->msg.command;
106 int len = p->msg.data_length;
107 char cmd[9];
108 char arg0[12], arg1[12];
109 int n;
110
111 for (n = 0; n < 4; n++) {
112 int b = (command >> (n*8)) & 255;
113 if (b < 32 || b >= 127)
114 break;
115 cmd[n] = (char)b;
116 }
117 if (n == 4) {
118 cmd[4] = 0;
119 } else {
120 /* There is some non-ASCII name in the command, so dump
121 * the hexadecimal value instead */
122 snprintf(cmd, sizeof cmd, "%08x", command);
123 }
124
125 if (p->msg.arg0 < 256U)
126 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0);
127 else
128 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0);
129
130 if (p->msg.arg1 < 256U)
131 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1);
132 else
133 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1);
134
135 D("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ",
136 name, func, cmd, arg0, arg1, len);
137 dump_hex(p->data, len);
138}
139#endif /* ADB_TRACE */
140
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100142read_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143{
144 char *p = (char*)ppacket; /* really read a packet address */
145 int r;
146 int len = sizeof(*ppacket);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100147 char buff[8];
148 if (!name) {
149 snprintf(buff, sizeof buff, "fd=%d", fd);
150 name = buff;
151 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152 while(len > 0) {
153 r = adb_read(fd, p, len);
154 if(r > 0) {
155 len -= r;
156 p += r;
157 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100158 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 -0800159 if((r < 0) && (errno == EINTR)) continue;
160 return -1;
161 }
162 }
163
164#if ADB_TRACE
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100165 if (ADB_TRACING) {
166 dump_packet(name, "from remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800167 }
168#endif
169 return 0;
170}
171
172static int
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100173write_packet(int fd, const char* name, apacket** ppacket)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174{
175 char *p = (char*) ppacket; /* we really write the packet address */
176 int r, len = sizeof(ppacket);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100177 char buff[8];
178 if (!name) {
179 snprintf(buff, sizeof buff, "fd=%d", fd);
180 name = buff;
181 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800182
183#if ADB_TRACE
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100184 if (ADB_TRACING) {
185 dump_packet(name, "to remote", *ppacket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186 }
187#endif
188 len = sizeof(ppacket);
189 while(len > 0) {
190 r = adb_write(fd, p, len);
191 if(r > 0) {
192 len -= r;
193 p += r;
194 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100195 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 -0800196 if((r < 0) && (errno == EINTR)) continue;
197 return -1;
198 }
199 }
200 return 0;
201}
202
203static void transport_socket_events(int fd, unsigned events, void *_t)
204{
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100205 atransport *t = _t;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700206 D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800207 if(events & FDE_READ){
208 apacket *p = 0;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100209 if(read_packet(fd, t->serial, &p)){
210 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 -0800211 } else {
212 handle_packet(p, (atransport *) _t);
213 }
214 }
215}
216
217void send_packet(apacket *p, atransport *t)
218{
219 unsigned char *x;
220 unsigned sum;
221 unsigned count;
222
223 p->msg.magic = p->msg.command ^ 0xffffffff;
224
225 count = p->msg.data_length;
226 x = (unsigned char *) p->data;
227 sum = 0;
228 while(count-- > 0){
229 sum += *x++;
230 }
231 p->msg.data_check = sum;
232
233 print_packet("send", p);
234
235 if (t == NULL) {
JP Abgrall1f501f32011-02-23 18:44:39 -0800236 D("Transport is null \n");
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700237 // Zap errno because print_packet() and other stuff have errno effect.
238 errno = 0;
239 fatal_errno("Transport is null");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 }
241
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100242 if(write_packet(t->transport_socket, t->serial, &p)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800243 fatal_errno("cannot enqueue packet on transport socket");
244 }
245}
246
247/* The transport is opened by transport_register_func before
248** the input and output threads are started.
249**
250** The output thread issues a SYNC(1, token) message to let
251** the input thread know to start things up. In the event
252** of transport IO failure, the output thread will post a
253** SYNC(0,0) message to ensure shutdown.
254**
255** The transport will not actually be closed until both
256** threads exit, but the input thread will kick the transport
257** on its way out to disconnect the underlying device.
258*/
259
260static void *output_thread(void *_t)
261{
262 atransport *t = _t;
263 apacket *p;
264
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100265 D("%s: starting transport output thread on fd %d, SYNC online (%d)\n",
266 t->serial, t->fd, t->sync_token + 1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800267 p = get_apacket();
268 p->msg.command = A_SYNC;
269 p->msg.arg0 = 1;
270 p->msg.arg1 = ++(t->sync_token);
271 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100272 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800273 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100274 D("%s: failed to write SYNC packet\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800275 goto oops;
276 }
277
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100278 D("%s: data pump started\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279 for(;;) {
280 p = get_apacket();
281
282 if(t->read_from_remote(p, t) == 0){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100283 D("%s: received remote packet, sending to transport\n",
284 t->serial);
285 if(write_packet(t->fd, t->serial, &p)){
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800286 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100287 D("%s: failed to write apacket to transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800288 goto oops;
289 }
290 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100291 D("%s: remote read failed for transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 put_apacket(p);
293 break;
294 }
295 }
296
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100297 D("%s: SYNC offline for transport\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800298 p = get_apacket();
299 p->msg.command = A_SYNC;
300 p->msg.arg0 = 0;
301 p->msg.arg1 = 0;
302 p->msg.magic = A_SYNC ^ 0xffffffff;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100303 if(write_packet(t->fd, t->serial, &p)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800304 put_apacket(p);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100305 D("%s: failed to write SYNC apacket to transport", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306 }
307
308oops:
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100309 D("%s: transport output thread is exiting\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800310 kick_transport(t);
311 transport_unref(t);
312 return 0;
313}
314
315static void *input_thread(void *_t)
316{
317 atransport *t = _t;
318 apacket *p;
319 int active = 0;
320
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100321 D("%s: starting transport input thread, reading from fd %d\n",
322 t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323
324 for(;;){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100325 if(read_packet(t->fd, t->serial, &p)) {
326 D("%s: failed to read apacket from transport on fd %d\n",
327 t->serial, t->fd );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800328 break;
329 }
330 if(p->msg.command == A_SYNC){
331 if(p->msg.arg0 == 0) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100332 D("%s: transport SYNC offline\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800333 put_apacket(p);
334 break;
335 } else {
336 if(p->msg.arg1 == t->sync_token) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100337 D("%s: transport SYNC online\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800338 active = 1;
339 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100340 D("%s: transport ignoring SYNC %d != %d\n",
341 t->serial, p->msg.arg1, t->sync_token);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342 }
343 }
344 } else {
345 if(active) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100346 D("%s: transport got packet, sending to remote\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 t->write_to_remote(p, t);
348 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100349 D("%s: transport ignoring packet while offline\n", t->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350 }
351 }
352
353 put_apacket(p);
354 }
355
356 // this is necessary to avoid a race condition that occured when a transport closes
357 // while a client socket is still active.
358 close_all_sockets(t);
359
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100360 D("%s: transport input thread is exiting, fd %d\n", t->serial, t->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800361 kick_transport(t);
362 transport_unref(t);
363 return 0;
364}
365
366
367static int transport_registration_send = -1;
368static int transport_registration_recv = -1;
369static fdevent transport_registration_fde;
370
371
372#if ADB_HOST
373static int list_transports_msg(char* buffer, size_t bufferlen)
374{
375 char head[5];
376 int len;
377
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700378 len = list_transports(buffer+4, bufferlen-4, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 snprintf(head, sizeof(head), "%04x", len);
380 memcpy(buffer, head, 4);
381 len += 4;
382 return len;
383}
384
385/* this adds support required by the 'track-devices' service.
386 * this is used to send the content of "list_transport" to any
387 * number of client connections that want it through a single
388 * live TCP connection
389 */
390typedef struct device_tracker device_tracker;
391struct device_tracker {
392 asocket socket;
393 int update_needed;
394 device_tracker* next;
395};
396
397/* linked list of all device trackers */
398static device_tracker* device_tracker_list;
399
400static void
401device_tracker_remove( device_tracker* tracker )
402{
403 device_tracker** pnode = &device_tracker_list;
404 device_tracker* node = *pnode;
405
406 adb_mutex_lock( &transport_lock );
407 while (node) {
408 if (node == tracker) {
409 *pnode = node->next;
410 break;
411 }
412 pnode = &node->next;
413 node = *pnode;
414 }
415 adb_mutex_unlock( &transport_lock );
416}
417
418static void
419device_tracker_close( asocket* socket )
420{
421 device_tracker* tracker = (device_tracker*) socket;
422 asocket* peer = socket->peer;
423
424 D( "device tracker %p removed\n", tracker);
425 if (peer) {
426 peer->peer = NULL;
427 peer->close(peer);
428 }
429 device_tracker_remove(tracker);
430 free(tracker);
431}
432
433static int
434device_tracker_enqueue( asocket* socket, apacket* p )
435{
436 /* you can't read from a device tracker, close immediately */
437 put_apacket(p);
438 device_tracker_close(socket);
439 return -1;
440}
441
442static int
443device_tracker_send( device_tracker* tracker,
444 const char* buffer,
445 int len )
446{
447 apacket* p = get_apacket();
448 asocket* peer = tracker->socket.peer;
449
450 memcpy(p->data, buffer, len);
451 p->len = len;
452 return peer->enqueue( peer, p );
453}
454
455
456static void
457device_tracker_ready( asocket* socket )
458{
459 device_tracker* tracker = (device_tracker*) socket;
460
461 /* we want to send the device list when the tracker connects
462 * for the first time, even if no update occured */
463 if (tracker->update_needed > 0) {
464 char buffer[1024];
465 int len;
466
467 tracker->update_needed = 0;
468
469 len = list_transports_msg(buffer, sizeof(buffer));
470 device_tracker_send(tracker, buffer, len);
471 }
472}
473
474
475asocket*
476create_device_tracker(void)
477{
478 device_tracker* tracker = calloc(1,sizeof(*tracker));
479
480 if(tracker == 0) fatal("cannot allocate device tracker");
481
482 D( "device tracker %p created\n", tracker);
483
484 tracker->socket.enqueue = device_tracker_enqueue;
485 tracker->socket.ready = device_tracker_ready;
486 tracker->socket.close = device_tracker_close;
487 tracker->update_needed = 1;
488
489 tracker->next = device_tracker_list;
490 device_tracker_list = tracker;
491
492 return &tracker->socket;
493}
494
495
496/* call this function each time the transport list has changed */
497void update_transports(void)
498{
499 char buffer[1024];
500 int len;
501 device_tracker* tracker;
502
503 len = list_transports_msg(buffer, sizeof(buffer));
504
505 tracker = device_tracker_list;
506 while (tracker != NULL) {
507 device_tracker* next = tracker->next;
508 /* note: this may destroy the tracker if the connection is closed */
509 device_tracker_send(tracker, buffer, len);
510 tracker = next;
511 }
512}
513#else
514void update_transports(void)
515{
516 // nothing to do on the device side
517}
518#endif // ADB_HOST
519
520typedef struct tmsg tmsg;
521struct tmsg
522{
523 atransport *transport;
524 int action;
525};
526
527static int
528transport_read_action(int fd, struct tmsg* m)
529{
530 char *p = (char*)m;
531 int len = sizeof(*m);
532 int r;
533
534 while(len > 0) {
535 r = adb_read(fd, p, len);
536 if(r > 0) {
537 len -= r;
538 p += r;
539 } else {
540 if((r < 0) && (errno == EINTR)) continue;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100541 D("transport_read_action: on fd %d, error %d: %s\n",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800542 fd, errno, strerror(errno));
543 return -1;
544 }
545 }
546 return 0;
547}
548
549static int
550transport_write_action(int fd, struct tmsg* m)
551{
552 char *p = (char*)m;
553 int len = sizeof(*m);
554 int r;
555
556 while(len > 0) {
557 r = adb_write(fd, p, len);
558 if(r > 0) {
559 len -= r;
560 p += r;
561 } else {
562 if((r < 0) && (errno == EINTR)) continue;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100563 D("transport_write_action: on fd %d, error %d: %s\n",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800564 fd, errno, strerror(errno));
565 return -1;
566 }
567 }
568 return 0;
569}
570
571static void transport_registration_func(int _fd, unsigned ev, void *data)
572{
573 tmsg m;
574 adb_thread_t output_thread_ptr;
575 adb_thread_t input_thread_ptr;
576 int s[2];
577 atransport *t;
578
579 if(!(ev & FDE_READ)) {
580 return;
581 }
582
583 if(transport_read_action(_fd, &m)) {
584 fatal_errno("cannot read transport registration socket");
585 }
586
587 t = m.transport;
588
589 if(m.action == 0){
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100590 D("transport: %s removing and free'ing %d\n", t->serial, t->transport_socket);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800591
592 /* IMPORTANT: the remove closes one half of the
593 ** socket pair. The close closes the other half.
594 */
595 fdevent_remove(&(t->transport_fde));
596 adb_close(t->fd);
597
598 adb_mutex_lock(&transport_lock);
599 t->next->prev = t->prev;
600 t->prev->next = t->next;
601 adb_mutex_unlock(&transport_lock);
602
603 run_transport_disconnects(t);
604
605 if (t->product)
606 free(t->product);
607 if (t->serial)
608 free(t->serial);
Scott Andersonfa020922012-05-25 14:10:02 -0700609 if (t->model)
610 free(t->model);
611 if (t->device)
612 free(t->device);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700613 if (t->devpath)
614 free(t->devpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800615
616 memset(t,0xee,sizeof(atransport));
617 free(t);
618
619 update_transports();
620 return;
621 }
622
Mike Lockwoode45583f2009-08-08 12:37:44 -0400623 /* don't create transport threads for inaccessible devices */
624 if (t->connection_state != CS_NOPERM) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800625 /* initial references are the two threads */
Mike Lockwoode45583f2009-08-08 12:37:44 -0400626 t->ref_count = 2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800627
Mike Lockwoode45583f2009-08-08 12:37:44 -0400628 if(adb_socketpair(s)) {
629 fatal_errno("cannot open transport socketpair");
630 }
631
leozwang1be54622014-08-15 09:51:27 -0700632 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400633
634 t->transport_socket = s[0];
635 t->fd = s[1];
636
Mike Lockwoode45583f2009-08-08 12:37:44 -0400637 fdevent_install(&(t->transport_fde),
638 t->transport_socket,
639 transport_socket_events,
640 t);
641
642 fdevent_set(&(t->transport_fde), FDE_READ);
643
644 if(adb_thread_create(&input_thread_ptr, input_thread, t)){
645 fatal_errno("cannot create input thread");
646 }
647
648 if(adb_thread_create(&output_thread_ptr, output_thread, t)){
649 fatal_errno("cannot create output thread");
650 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800651 }
652
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800653 adb_mutex_lock(&transport_lock);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700654 /* remove from pending list */
655 t->next->prev = t->prev;
656 t->prev->next = t->next;
657 /* put us on the master device list */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800658 t->next = &transport_list;
659 t->prev = transport_list.prev;
660 t->next->prev = t;
661 t->prev->next = t;
662 adb_mutex_unlock(&transport_lock);
663
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800664 t->disconnects.next = t->disconnects.prev = &t->disconnects;
665
666 update_transports();
667}
668
669void init_transport_registration(void)
670{
671 int s[2];
672
673 if(adb_socketpair(s)){
674 fatal_errno("cannot open transport registration socketpair");
675 }
leozwang1be54622014-08-15 09:51:27 -0700676 D("socketpair: (%d,%d)", s[0], s[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677
678 transport_registration_send = s[0];
679 transport_registration_recv = s[1];
680
681 fdevent_install(&transport_registration_fde,
682 transport_registration_recv,
683 transport_registration_func,
684 0);
685
686 fdevent_set(&transport_registration_fde, FDE_READ);
687}
688
689/* the fdevent select pump is single threaded */
690static void register_transport(atransport *transport)
691{
692 tmsg m;
693 m.transport = transport;
694 m.action = 1;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100695 D("transport: %s registered\n", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800696 if(transport_write_action(transport_registration_send, &m)) {
697 fatal_errno("cannot write transport registration socket\n");
698 }
699}
700
701static void remove_transport(atransport *transport)
702{
703 tmsg m;
704 m.transport = transport;
705 m.action = 0;
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100706 D("transport: %s removed\n", transport->serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800707 if(transport_write_action(transport_registration_send, &m)) {
708 fatal_errno("cannot write transport registration socket\n");
709 }
710}
711
712
Mike Lockwood01c2c302010-05-24 10:44:35 -0400713static void transport_unref_locked(atransport *t)
714{
715 t->ref_count--;
Mike Lockwood01c2c302010-05-24 10:44:35 -0400716 if (t->ref_count == 0) {
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100717 D("transport: %s unref (kicking and closing)\n", t->serial);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400718 if (!t->kicked) {
719 t->kicked = 1;
720 t->kick(t);
721 }
722 t->close(t);
723 remove_transport(t);
David 'Digit' Turner58f59682011-01-06 14:11:07 +0100724 } else {
725 D("transport: %s unref (count=%d)\n", t->serial, t->ref_count);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400726 }
727}
728
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800729static void transport_unref(atransport *t)
730{
731 if (t) {
732 adb_mutex_lock(&transport_lock);
Mike Lockwood01c2c302010-05-24 10:44:35 -0400733 transport_unref_locked(t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800734 adb_mutex_unlock(&transport_lock);
735 }
736}
737
738void add_transport_disconnect(atransport* t, adisconnect* dis)
739{
740 adb_mutex_lock(&transport_lock);
741 dis->next = &t->disconnects;
742 dis->prev = dis->next->prev;
743 dis->prev->next = dis;
744 dis->next->prev = dis;
745 adb_mutex_unlock(&transport_lock);
746}
747
748void remove_transport_disconnect(atransport* t, adisconnect* dis)
749{
750 dis->prev->next = dis->next;
751 dis->next->prev = dis->prev;
752 dis->next = dis->prev = dis;
753}
754
Scott Anderson27042382012-05-30 18:11:27 -0700755static int qual_char_is_invalid(char ch)
756{
757 if ('A' <= ch && ch <= 'Z')
758 return 0;
759 if ('a' <= ch && ch <= 'z')
760 return 0;
761 if ('0' <= ch && ch <= '9')
762 return 0;
763 return 1;
764}
765
766static int qual_match(const char *to_test,
767 const char *prefix, const char *qual, int sanitize_qual)
768{
769 if (!to_test || !*to_test)
770 /* Return true if both the qual and to_test are null strings. */
771 return !qual || !*qual;
772
773 if (!qual)
774 return 0;
775
776 if (prefix) {
777 while (*prefix) {
778 if (*prefix++ != *to_test++)
779 return 0;
780 }
781 }
782
783 while (*qual) {
784 char ch = *qual++;
785 if (sanitize_qual && qual_char_is_invalid(ch))
786 ch = '_';
787 if (ch != *to_test++)
788 return 0;
789 }
790
791 /* Everything matched so far. Return true if *to_test is a NUL. */
792 return !*to_test;
793}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800794
795atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out)
796{
797 atransport *t;
798 atransport *result = NULL;
799 int ambiguous = 0;
800
801retry:
802 if (error_out)
803 *error_out = "device not found";
804
805 adb_mutex_lock(&transport_lock);
806 for (t = transport_list.next; t != &transport_list; t = t->next) {
Mike Lockwoodadc16b32009-08-08 13:53:16 -0400807 if (t->connection_state == CS_NOPERM) {
808 if (error_out)
809 *error_out = "insufficient permissions for device";
810 continue;
811 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400812
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800813 /* check for matching serial number */
814 if (serial) {
Scott Anderson27042382012-05-30 18:11:27 -0700815 if ((t->serial && !strcmp(serial, t->serial)) ||
816 (t->devpath && !strcmp(serial, t->devpath)) ||
817 qual_match(serial, "product:", t->product, 0) ||
818 qual_match(serial, "model:", t->model, 1) ||
819 qual_match(serial, "device:", t->device, 0)) {
820 if (result) {
821 if (error_out)
822 *error_out = "more than one device";
823 ambiguous = 1;
824 result = NULL;
825 break;
826 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800827 result = t;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700828 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800829 } else {
830 if (ttype == kTransportUsb && t->type == kTransportUsb) {
831 if (result) {
832 if (error_out)
833 *error_out = "more than one device";
834 ambiguous = 1;
835 result = NULL;
836 break;
837 }
838 result = t;
839 } else if (ttype == kTransportLocal && t->type == kTransportLocal) {
840 if (result) {
841 if (error_out)
842 *error_out = "more than one emulator";
843 ambiguous = 1;
844 result = NULL;
845 break;
846 }
847 result = t;
848 } else if (ttype == kTransportAny) {
849 if (result) {
850 if (error_out)
851 *error_out = "more than one device and emulator";
852 ambiguous = 1;
853 result = NULL;
854 break;
855 }
856 result = t;
857 }
858 }
859 }
860 adb_mutex_unlock(&transport_lock);
861
862 if (result) {
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700863 if (result->connection_state == CS_UNAUTHORIZED) {
864 if (error_out)
865 *error_out = "device unauthorized. Please check the confirmation dialog on your device.";
866 result = NULL;
867 }
868
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800869 /* offline devices are ignored -- they are either being born or dying */
870 if (result && result->connection_state == CS_OFFLINE) {
871 if (error_out)
872 *error_out = "device offline";
873 result = NULL;
874 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800875 /* check for required connection state */
876 if (result && state != CS_ANY && result->connection_state != state) {
877 if (error_out)
878 *error_out = "invalid device state";
879 result = NULL;
880 }
881 }
882
883 if (result) {
884 /* found one that we can take */
885 if (error_out)
886 *error_out = NULL;
887 } else if (state != CS_ANY && (serial || !ambiguous)) {
888 adb_sleep_ms(1000);
889 goto retry;
890 }
891
892 return result;
893}
894
895#if ADB_HOST
896static const char *statename(atransport *t)
897{
898 switch(t->connection_state){
899 case CS_OFFLINE: return "offline";
900 case CS_BOOTLOADER: return "bootloader";
901 case CS_DEVICE: return "device";
902 case CS_HOST: return "host";
903 case CS_RECOVERY: return "recovery";
Doug Zongker6b217ed2012-01-09 14:54:53 -0800904 case CS_SIDELOAD: return "sideload";
Mike Lockwoode45583f2009-08-08 12:37:44 -0400905 case CS_NOPERM: return "no permissions";
Benoit Goby3f9f9ce2013-03-29 18:22:36 -0700906 case CS_UNAUTHORIZED: return "unauthorized";
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800907 default: return "unknown";
908 }
909}
910
Scott Anderson27042382012-05-30 18:11:27 -0700911static void add_qual(char **buf, size_t *buf_size,
912 const char *prefix, const char *qual, int sanitize_qual)
913{
914 size_t len;
915 int prefix_len;
916
917 if (!buf || !*buf || !buf_size || !*buf_size || !qual || !*qual)
918 return;
919
920 len = snprintf(*buf, *buf_size, "%s%n%s", prefix, &prefix_len, qual);
921
922 if (sanitize_qual) {
923 char *cp;
924 for (cp = *buf + prefix_len; cp < *buf + len; cp++) {
925 if (qual_char_is_invalid(*cp))
926 *cp = '_';
927 }
928 }
929
930 *buf_size -= len;
931 *buf += len;
932}
933
934static size_t format_transport(atransport *t, char *buf, size_t bufsize,
935 int long_listing)
936{
937 const char* serial = t->serial;
938 if (!serial || !serial[0])
939 serial = "????????????";
940
941 if (!long_listing) {
942 return snprintf(buf, bufsize, "%s\t%s\n", serial, statename(t));
943 } else {
944 size_t len, remaining = bufsize;
945
946 len = snprintf(buf, remaining, "%-22s %s", serial, statename(t));
947 remaining -= len;
948 buf += len;
949
950 add_qual(&buf, &remaining, " ", t->devpath, 0);
951 add_qual(&buf, &remaining, " product:", t->product, 0);
952 add_qual(&buf, &remaining, " model:", t->model, 1);
953 add_qual(&buf, &remaining, " device:", t->device, 0);
954
955 len = snprintf(buf, remaining, "\n");
956 remaining -= len;
957
958 return bufsize - remaining;
959 }
960}
961
962int list_transports(char *buf, size_t bufsize, int long_listing)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800963{
964 char* p = buf;
965 char* end = buf + bufsize;
966 int len;
967 atransport *t;
968
969 /* XXX OVERRUN PROBLEMS XXX */
970 adb_mutex_lock(&transport_lock);
971 for(t = transport_list.next; t != &transport_list; t = t->next) {
Scott Anderson27042382012-05-30 18:11:27 -0700972 len = format_transport(t, p, end - p, long_listing);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800973 if (p + len >= end) {
974 /* discard last line if buffer is too short */
975 break;
976 }
977 p += len;
978 }
979 p[0] = 0;
980 adb_mutex_unlock(&transport_lock);
981 return p - buf;
982}
983
984
985/* hack for osx */
986void close_usb_devices()
987{
988 atransport *t;
989
990 adb_mutex_lock(&transport_lock);
991 for(t = transport_list.next; t != &transport_list; t = t->next) {
992 if ( !t->kicked ) {
993 t->kicked = 1;
994 t->kick(t);
995 }
996 }
997 adb_mutex_unlock(&transport_lock);
998}
999#endif // ADB_HOST
1000
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001001int register_socket_transport(int s, const char *serial, int port, int local)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001002{
1003 atransport *t = calloc(1, sizeof(atransport));
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001004 atransport *n;
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001005 char buff[32];
1006
1007 if (!serial) {
1008 snprintf(buff, sizeof buff, "T-%p", t);
1009 serial = buff;
1010 }
1011 D("transport: %s init'ing for socket %d, on port %d\n", serial, s, port);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001012 if (init_socket_transport(t, s, port, local) < 0) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001013 free(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001014 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001015 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001016
1017 adb_mutex_lock(&transport_lock);
1018 for (n = pending_list.next; n != &pending_list; n = n->next) {
1019 if (n->serial && !strcmp(serial, n->serial)) {
1020 adb_mutex_unlock(&transport_lock);
1021 free(t);
1022 return -1;
1023 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001024 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001025
1026 for (n = transport_list.next; n != &transport_list; n = n->next) {
1027 if (n->serial && !strcmp(serial, n->serial)) {
1028 adb_mutex_unlock(&transport_lock);
1029 free(t);
1030 return -1;
1031 }
1032 }
1033
1034 t->next = &pending_list;
1035 t->prev = pending_list.prev;
1036 t->next->prev = t;
1037 t->prev->next = t;
1038 t->serial = strdup(serial);
1039 adb_mutex_unlock(&transport_lock);
1040
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001041 register_transport(t);
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001042 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001043}
1044
Mike Lockwood81ffe172009-10-11 23:04:18 -04001045#if ADB_HOST
1046atransport *find_transport(const char *serial)
1047{
1048 atransport *t;
1049
1050 adb_mutex_lock(&transport_lock);
1051 for(t = transport_list.next; t != &transport_list; t = t->next) {
1052 if (t->serial && !strcmp(serial, t->serial)) {
1053 break;
1054 }
1055 }
1056 adb_mutex_unlock(&transport_lock);
1057
1058 if (t != &transport_list)
1059 return t;
1060 else
1061 return 0;
1062}
1063
1064void unregister_transport(atransport *t)
1065{
1066 adb_mutex_lock(&transport_lock);
1067 t->next->prev = t->prev;
1068 t->prev->next = t->next;
1069 adb_mutex_unlock(&transport_lock);
1070
1071 kick_transport(t);
1072 transport_unref(t);
1073}
1074
Mike Lockwood01c2c302010-05-24 10:44:35 -04001075// unregisters all non-emulator TCP transports
1076void unregister_all_tcp_transports()
1077{
1078 atransport *t, *next;
1079 adb_mutex_lock(&transport_lock);
1080 for (t = transport_list.next; t != &transport_list; t = next) {
1081 next = t->next;
1082 if (t->type == kTransportLocal && t->adb_port == 0) {
1083 t->next->prev = t->prev;
1084 t->prev->next = next;
1085 // we cannot call kick_transport when holding transport_lock
1086 if (!t->kicked)
1087 {
1088 t->kicked = 1;
1089 t->kick(t);
1090 }
1091 transport_unref_locked(t);
1092 }
1093 }
1094
1095 adb_mutex_unlock(&transport_lock);
1096}
1097
Mike Lockwood81ffe172009-10-11 23:04:18 -04001098#endif
1099
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001100void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001101{
1102 atransport *t = calloc(1, sizeof(atransport));
1103 D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb,
1104 serial ? serial : "");
Mike Lockwoode45583f2009-08-08 12:37:44 -04001105 init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001106 if(serial) {
1107 t->serial = strdup(serial);
1108 }
Scott Anderson6dfaf4b2012-04-20 11:21:14 -07001109 if(devpath) {
1110 t->devpath = strdup(devpath);
1111 }
Benoit Goby3f9f9ce2013-03-29 18:22:36 -07001112
1113 adb_mutex_lock(&transport_lock);
1114 t->next = &pending_list;
1115 t->prev = pending_list.prev;
1116 t->next->prev = t;
1117 t->prev->next = t;
1118 adb_mutex_unlock(&transport_lock);
1119
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001120 register_transport(t);
1121}
1122
Mike Lockwoode45583f2009-08-08 12:37:44 -04001123/* this should only be used for transports with connection_state == CS_NOPERM */
1124void unregister_usb_transport(usb_handle *usb)
1125{
1126 atransport *t;
1127 adb_mutex_lock(&transport_lock);
1128 for(t = transport_list.next; t != &transport_list; t = t->next) {
1129 if (t->usb == usb && t->connection_state == CS_NOPERM) {
1130 t->next->prev = t->prev;
1131 t->prev->next = t->next;
1132 break;
1133 }
1134 }
1135 adb_mutex_unlock(&transport_lock);
1136}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001137
1138#undef TRACE_TAG
1139#define TRACE_TAG TRACE_RWX
1140
1141int readx(int fd, void *ptr, size_t len)
1142{
1143 char *p = ptr;
1144 int r;
1145#if ADB_TRACE
Elliott Hughes9c0d9402014-01-16 10:53:11 -08001146 size_t len0 = len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001147#endif
Elliott Hughes9c0d9402014-01-16 10:53:11 -08001148 D("readx: fd=%d wanted=%zu\n", fd, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001149 while(len > 0) {
1150 r = adb_read(fd, p, len);
1151 if(r > 0) {
1152 len -= r;
1153 p += r;
1154 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001155 if (r < 0) {
1156 D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno));
1157 if (errno == EINTR)
1158 continue;
1159 } else {
1160 D("readx: fd=%d disconnected\n", fd);
1161 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001162 return -1;
1163 }
1164 }
1165
1166#if ADB_TRACE
Elliott Hughes9c0d9402014-01-16 10:53:11 -08001167 D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001168 dump_hex( ptr, len0 );
1169#endif
1170 return 0;
1171}
1172
1173int writex(int fd, const void *ptr, size_t len)
1174{
1175 char *p = (char*) ptr;
1176 int r;
1177
1178#if ADB_TRACE
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001179 D("writex: fd=%d len=%d: ", fd, (int)len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001180 dump_hex( ptr, len );
1181#endif
1182 while(len > 0) {
1183 r = adb_write(fd, p, len);
1184 if(r > 0) {
1185 len -= r;
1186 p += r;
1187 } else {
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001188 if (r < 0) {
1189 D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno));
1190 if (errno == EINTR)
1191 continue;
JP Abgrallcca32262014-03-06 19:40:45 -08001192 if (errno == EAGAIN) {
Snild Dolkow4516a872014-01-30 10:08:38 +01001193 adb_sleep_ms(1); // just yield some cpu time
1194 continue;
1195 }
David 'Digit' Turner58f59682011-01-06 14:11:07 +01001196 } else {
1197 D("writex: fd=%d disconnected\n", fd);
1198 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001199 return -1;
1200 }
1201 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001202 return 0;
1203}
1204
1205int check_header(apacket *p)
1206{
1207 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) {
1208 D("check_header(): invalid magic\n");
1209 return -1;
1210 }
1211
1212 if(p->msg.data_length > MAX_PAYLOAD) {
1213 D("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length);
1214 return -1;
1215 }
1216
1217 return 0;
1218}
1219
1220int check_data(apacket *p)
1221{
1222 unsigned count, sum;
1223 unsigned char *x;
1224
1225 count = p->msg.data_length;
1226 x = p->data;
1227 sum = 0;
1228 while(count-- > 0) {
1229 sum += *x++;
1230 }
1231
1232 if(sum != p->msg.data_check) {
1233 return -1;
1234 } else {
1235 return 0;
1236 }
1237}