transport.cpp revision dcd78a15d0be143d48fc93af6a9fa5748dbf9790
1/* 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#define TRACE_TAG TRACE_TRANSPORT 18 19#include "sysdeps.h" 20#include "transport.h" 21 22#include <ctype.h> 23#include <errno.h> 24#include <stdio.h> 25#include <stdlib.h> 26#include <string.h> 27#include <unistd.h> 28 29#include <base/stringprintf.h> 30 31#include "adb.h" 32#include "adb_utils.h" 33 34static void transport_unref(atransport *t); 35 36static atransport transport_list = { 37 .next = &transport_list, 38 .prev = &transport_list, 39}; 40 41static atransport pending_list = { 42 .next = &pending_list, 43 .prev = &pending_list, 44}; 45 46ADB_MUTEX_DEFINE( transport_lock ); 47 48void kick_transport(atransport* t) 49{ 50 if (t && !t->kicked) 51 { 52 int kicked; 53 54 adb_mutex_lock(&transport_lock); 55 kicked = t->kicked; 56 if (!kicked) 57 t->kicked = 1; 58 adb_mutex_unlock(&transport_lock); 59 60 if (!kicked) 61 t->kick(t); 62 } 63} 64 65// Each atransport contains a list of adisconnects (t->disconnects). 66// An adisconnect contains a link to the next/prev adisconnect, a function 67// pointer to a disconnect callback which takes a void* piece of user data and 68// the atransport, and some user data for the callback (helpfully named 69// "opaque"). 70// 71// The list is circular. New items are added to the entry member of the list 72// (t->disconnects) by add_transport_disconnect. 73// 74// run_transport_disconnects invokes each function in the list. 75// 76// Gotchas: 77// * run_transport_disconnects assumes that t->disconnects is non-null, so 78// this can't be run on a zeroed atransport. 79// * The callbacks in this list are not removed when called, and this function 80// is not guarded against running more than once. As such, ensure that this 81// function is not called multiple times on the same atransport. 82// TODO(danalbert): Just fix this so that it is guarded once you have tests. 83void run_transport_disconnects(atransport* t) 84{ 85 adisconnect* dis = t->disconnects.next; 86 87 D("%s: run_transport_disconnects\n", t->serial); 88 while (dis != &t->disconnects) { 89 adisconnect* next = dis->next; 90 dis->func( dis->opaque, t ); 91 dis = next; 92 } 93} 94 95static void dump_packet(const char* name, const char* func, apacket* p) { 96 unsigned command = p->msg.command; 97 int len = p->msg.data_length; 98 char cmd[9]; 99 char arg0[12], arg1[12]; 100 int n; 101 102 for (n = 0; n < 4; n++) { 103 int b = (command >> (n*8)) & 255; 104 if (b < 32 || b >= 127) 105 break; 106 cmd[n] = (char)b; 107 } 108 if (n == 4) { 109 cmd[4] = 0; 110 } else { 111 /* There is some non-ASCII name in the command, so dump 112 * the hexadecimal value instead */ 113 snprintf(cmd, sizeof cmd, "%08x", command); 114 } 115 116 if (p->msg.arg0 < 256U) 117 snprintf(arg0, sizeof arg0, "%d", p->msg.arg0); 118 else 119 snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0); 120 121 if (p->msg.arg1 < 256U) 122 snprintf(arg1, sizeof arg1, "%d", p->msg.arg1); 123 else 124 snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1); 125 126 D("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", 127 name, func, cmd, arg0, arg1, len); 128 dump_hex(p->data, len); 129} 130 131static int 132read_packet(int fd, const char* name, apacket** ppacket) 133{ 134 char *p = (char*)ppacket; /* really read a packet address */ 135 int r; 136 int len = sizeof(*ppacket); 137 char buff[8]; 138 if (!name) { 139 snprintf(buff, sizeof buff, "fd=%d", fd); 140 name = buff; 141 } 142 while(len > 0) { 143 r = adb_read(fd, p, len); 144 if(r > 0) { 145 len -= r; 146 p += r; 147 } else { 148 D("%s: read_packet (fd=%d), error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno)); 149 if((r < 0) && (errno == EINTR)) continue; 150 return -1; 151 } 152 } 153 154 if (ADB_TRACING) { 155 dump_packet(name, "from remote", *ppacket); 156 } 157 return 0; 158} 159 160static int 161write_packet(int fd, const char* name, apacket** ppacket) 162{ 163 char *p = (char*) ppacket; /* we really write the packet address */ 164 int r, len = sizeof(ppacket); 165 char buff[8]; 166 if (!name) { 167 snprintf(buff, sizeof buff, "fd=%d", fd); 168 name = buff; 169 } 170 171 if (ADB_TRACING) { 172 dump_packet(name, "to remote", *ppacket); 173 } 174 len = sizeof(ppacket); 175 while(len > 0) { 176 r = adb_write(fd, p, len); 177 if(r > 0) { 178 len -= r; 179 p += r; 180 } else { 181 D("%s: write_packet (fd=%d) error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno)); 182 if((r < 0) && (errno == EINTR)) continue; 183 return -1; 184 } 185 } 186 return 0; 187} 188 189static void transport_socket_events(int fd, unsigned events, void *_t) 190{ 191 atransport *t = reinterpret_cast<atransport*>(_t); 192 D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events); 193 if(events & FDE_READ){ 194 apacket *p = 0; 195 if(read_packet(fd, t->serial, &p)){ 196 D("%s: failed to read packet from transport socket on fd %d\n", t->serial, fd); 197 } else { 198 handle_packet(p, (atransport *) _t); 199 } 200 } 201} 202 203void send_packet(apacket *p, atransport *t) 204{ 205 unsigned char *x; 206 unsigned sum; 207 unsigned count; 208 209 p->msg.magic = p->msg.command ^ 0xffffffff; 210 211 count = p->msg.data_length; 212 x = (unsigned char *) p->data; 213 sum = 0; 214 while(count-- > 0){ 215 sum += *x++; 216 } 217 p->msg.data_check = sum; 218 219 print_packet("send", p); 220 221 if (t == NULL) { 222 D("Transport is null \n"); 223 // Zap errno because print_packet() and other stuff have errno effect. 224 errno = 0; 225 fatal_errno("Transport is null"); 226 } 227 228 if(write_packet(t->transport_socket, t->serial, &p)){ 229 fatal_errno("cannot enqueue packet on transport socket"); 230 } 231} 232 233/* The transport is opened by transport_register_func before 234** the input and output threads are started. 235** 236** The output thread issues a SYNC(1, token) message to let 237** the input thread know to start things up. In the event 238** of transport IO failure, the output thread will post a 239** SYNC(0,0) message to ensure shutdown. 240** 241** The transport will not actually be closed until both 242** threads exit, but the input thread will kick the transport 243** on its way out to disconnect the underlying device. 244*/ 245 246static void *output_thread(void *_t) 247{ 248 atransport *t = reinterpret_cast<atransport*>(_t); 249 apacket *p; 250 251 D("%s: starting transport output thread on fd %d, SYNC online (%d)\n", 252 t->serial, t->fd, t->sync_token + 1); 253 p = get_apacket(); 254 p->msg.command = A_SYNC; 255 p->msg.arg0 = 1; 256 p->msg.arg1 = ++(t->sync_token); 257 p->msg.magic = A_SYNC ^ 0xffffffff; 258 if(write_packet(t->fd, t->serial, &p)) { 259 put_apacket(p); 260 D("%s: failed to write SYNC packet\n", t->serial); 261 goto oops; 262 } 263 264 D("%s: data pump started\n", t->serial); 265 for(;;) { 266 p = get_apacket(); 267 268 if(t->read_from_remote(p, t) == 0){ 269 D("%s: received remote packet, sending to transport\n", 270 t->serial); 271 if(write_packet(t->fd, t->serial, &p)){ 272 put_apacket(p); 273 D("%s: failed to write apacket to transport\n", t->serial); 274 goto oops; 275 } 276 } else { 277 D("%s: remote read failed for transport\n", t->serial); 278 put_apacket(p); 279 break; 280 } 281 } 282 283 D("%s: SYNC offline for transport\n", t->serial); 284 p = get_apacket(); 285 p->msg.command = A_SYNC; 286 p->msg.arg0 = 0; 287 p->msg.arg1 = 0; 288 p->msg.magic = A_SYNC ^ 0xffffffff; 289 if(write_packet(t->fd, t->serial, &p)) { 290 put_apacket(p); 291 D("%s: failed to write SYNC apacket to transport", t->serial); 292 } 293 294oops: 295 D("%s: transport output thread is exiting\n", t->serial); 296 kick_transport(t); 297 transport_unref(t); 298 return 0; 299} 300 301static void *input_thread(void *_t) 302{ 303 atransport *t = reinterpret_cast<atransport*>(_t); 304 apacket *p; 305 int active = 0; 306 307 D("%s: starting transport input thread, reading from fd %d\n", 308 t->serial, t->fd); 309 310 for(;;){ 311 if(read_packet(t->fd, t->serial, &p)) { 312 D("%s: failed to read apacket from transport on fd %d\n", 313 t->serial, t->fd ); 314 break; 315 } 316 if(p->msg.command == A_SYNC){ 317 if(p->msg.arg0 == 0) { 318 D("%s: transport SYNC offline\n", t->serial); 319 put_apacket(p); 320 break; 321 } else { 322 if(p->msg.arg1 == t->sync_token) { 323 D("%s: transport SYNC online\n", t->serial); 324 active = 1; 325 } else { 326 D("%s: transport ignoring SYNC %d != %d\n", 327 t->serial, p->msg.arg1, t->sync_token); 328 } 329 } 330 } else { 331 if(active) { 332 D("%s: transport got packet, sending to remote\n", t->serial); 333 t->write_to_remote(p, t); 334 } else { 335 D("%s: transport ignoring packet while offline\n", t->serial); 336 } 337 } 338 339 put_apacket(p); 340 } 341 342 // this is necessary to avoid a race condition that occured when a transport closes 343 // while a client socket is still active. 344 close_all_sockets(t); 345 346 D("%s: transport input thread is exiting, fd %d\n", t->serial, t->fd); 347 kick_transport(t); 348 transport_unref(t); 349 return 0; 350} 351 352 353static int transport_registration_send = -1; 354static int transport_registration_recv = -1; 355static fdevent transport_registration_fde; 356 357 358#if ADB_HOST 359 360/* this adds support required by the 'track-devices' service. 361 * this is used to send the content of "list_transport" to any 362 * number of client connections that want it through a single 363 * live TCP connection 364 */ 365struct device_tracker { 366 asocket socket; 367 int update_needed; 368 device_tracker* next; 369}; 370 371/* linked list of all device trackers */ 372static device_tracker* device_tracker_list; 373 374static void 375device_tracker_remove( device_tracker* tracker ) 376{ 377 device_tracker** pnode = &device_tracker_list; 378 device_tracker* node = *pnode; 379 380 adb_mutex_lock( &transport_lock ); 381 while (node) { 382 if (node == tracker) { 383 *pnode = node->next; 384 break; 385 } 386 pnode = &node->next; 387 node = *pnode; 388 } 389 adb_mutex_unlock( &transport_lock ); 390} 391 392static void 393device_tracker_close( asocket* socket ) 394{ 395 device_tracker* tracker = (device_tracker*) socket; 396 asocket* peer = socket->peer; 397 398 D( "device tracker %p removed\n", tracker); 399 if (peer) { 400 peer->peer = NULL; 401 peer->close(peer); 402 } 403 device_tracker_remove(tracker); 404 free(tracker); 405} 406 407static int 408device_tracker_enqueue( asocket* socket, apacket* p ) 409{ 410 /* you can't read from a device tracker, close immediately */ 411 put_apacket(p); 412 device_tracker_close(socket); 413 return -1; 414} 415 416static int device_tracker_send(device_tracker* tracker, const std::string& string) { 417 apacket* p = get_apacket(); 418 asocket* peer = tracker->socket.peer; 419 420 snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size())); 421 memcpy(&p->data[4], string.data(), string.size()); 422 p->len = 4 + string.size(); 423 return peer->enqueue(peer, p); 424} 425 426static void device_tracker_ready(asocket* socket) { 427 device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); 428 429 // We want to send the device list when the tracker connects 430 // for the first time, even if no update occurred. 431 if (tracker->update_needed > 0) { 432 tracker->update_needed = 0; 433 434 std::string transports = list_transports(false); 435 device_tracker_send(tracker, transports); 436 } 437} 438 439asocket* 440create_device_tracker(void) 441{ 442 device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker))); 443 if (tracker == nullptr) fatal("cannot allocate device tracker"); 444 445 D( "device tracker %p created\n", tracker); 446 447 tracker->socket.enqueue = device_tracker_enqueue; 448 tracker->socket.ready = device_tracker_ready; 449 tracker->socket.close = device_tracker_close; 450 tracker->update_needed = 1; 451 452 tracker->next = device_tracker_list; 453 device_tracker_list = tracker; 454 455 return &tracker->socket; 456} 457 458 459// Call this function each time the transport list has changed. 460void update_transports() { 461 std::string transports = list_transports(false); 462 463 device_tracker* tracker = device_tracker_list; 464 while (tracker != nullptr) { 465 device_tracker* next = tracker->next; 466 // This may destroy the tracker if the connection is closed. 467 device_tracker_send(tracker, transports); 468 tracker = next; 469 } 470} 471 472#else 473 474void update_transports() { 475 // Nothing to do on the device side. 476} 477 478#endif // ADB_HOST 479 480struct tmsg 481{ 482 atransport *transport; 483 int action; 484}; 485 486static int 487transport_read_action(int fd, struct tmsg* m) 488{ 489 char *p = (char*)m; 490 int len = sizeof(*m); 491 int r; 492 493 while(len > 0) { 494 r = adb_read(fd, p, len); 495 if(r > 0) { 496 len -= r; 497 p += r; 498 } else { 499 if((r < 0) && (errno == EINTR)) continue; 500 D("transport_read_action: on fd %d, error %d: %s\n", 501 fd, errno, strerror(errno)); 502 return -1; 503 } 504 } 505 return 0; 506} 507 508static int 509transport_write_action(int fd, struct tmsg* m) 510{ 511 char *p = (char*)m; 512 int len = sizeof(*m); 513 int r; 514 515 while(len > 0) { 516 r = adb_write(fd, p, len); 517 if(r > 0) { 518 len -= r; 519 p += r; 520 } else { 521 if((r < 0) && (errno == EINTR)) continue; 522 D("transport_write_action: on fd %d, error %d: %s\n", 523 fd, errno, strerror(errno)); 524 return -1; 525 } 526 } 527 return 0; 528} 529 530static void transport_registration_func(int _fd, unsigned ev, void *data) 531{ 532 tmsg m; 533 int s[2]; 534 atransport *t; 535 536 if(!(ev & FDE_READ)) { 537 return; 538 } 539 540 if(transport_read_action(_fd, &m)) { 541 fatal_errno("cannot read transport registration socket"); 542 } 543 544 t = m.transport; 545 546 if(m.action == 0){ 547 D("transport: %s removing and free'ing %d\n", t->serial, t->transport_socket); 548 549 /* IMPORTANT: the remove closes one half of the 550 ** socket pair. The close closes the other half. 551 */ 552 fdevent_remove(&(t->transport_fde)); 553 adb_close(t->fd); 554 555 adb_mutex_lock(&transport_lock); 556 t->next->prev = t->prev; 557 t->prev->next = t->next; 558 adb_mutex_unlock(&transport_lock); 559 560 run_transport_disconnects(t); 561 562 if (t->product) 563 free(t->product); 564 if (t->serial) 565 free(t->serial); 566 if (t->model) 567 free(t->model); 568 if (t->device) 569 free(t->device); 570 if (t->devpath) 571 free(t->devpath); 572 573 memset(t,0xee,sizeof(atransport)); 574 free(t); 575 576 update_transports(); 577 return; 578 } 579 580 /* don't create transport threads for inaccessible devices */ 581 if (t->connection_state != kCsNoPerm) { 582 /* initial references are the two threads */ 583 t->ref_count = 2; 584 585 if(adb_socketpair(s)) { 586 fatal_errno("cannot open transport socketpair"); 587 } 588 589 D("transport: %s socketpair: (%d,%d) starting", t->serial, s[0], s[1]); 590 591 t->transport_socket = s[0]; 592 t->fd = s[1]; 593 594 fdevent_install(&(t->transport_fde), 595 t->transport_socket, 596 transport_socket_events, 597 t); 598 599 fdevent_set(&(t->transport_fde), FDE_READ); 600 601 if (!adb_thread_create(input_thread, t)) { 602 fatal_errno("cannot create input thread"); 603 } 604 605 if (!adb_thread_create(output_thread, t)) { 606 fatal_errno("cannot create output thread"); 607 } 608 } 609 610 adb_mutex_lock(&transport_lock); 611 /* remove from pending list */ 612 t->next->prev = t->prev; 613 t->prev->next = t->next; 614 /* put us on the master device list */ 615 t->next = &transport_list; 616 t->prev = transport_list.prev; 617 t->next->prev = t; 618 t->prev->next = t; 619 adb_mutex_unlock(&transport_lock); 620 621 t->disconnects.next = t->disconnects.prev = &t->disconnects; 622 623 update_transports(); 624} 625 626void init_transport_registration(void) 627{ 628 int s[2]; 629 630 if(adb_socketpair(s)){ 631 fatal_errno("cannot open transport registration socketpair"); 632 } 633 D("socketpair: (%d,%d)", s[0], s[1]); 634 635 transport_registration_send = s[0]; 636 transport_registration_recv = s[1]; 637 638 fdevent_install(&transport_registration_fde, 639 transport_registration_recv, 640 transport_registration_func, 641 0); 642 643 fdevent_set(&transport_registration_fde, FDE_READ); 644} 645 646/* the fdevent select pump is single threaded */ 647static void register_transport(atransport *transport) 648{ 649 tmsg m; 650 m.transport = transport; 651 m.action = 1; 652 D("transport: %s registered\n", transport->serial); 653 if(transport_write_action(transport_registration_send, &m)) { 654 fatal_errno("cannot write transport registration socket\n"); 655 } 656} 657 658static void remove_transport(atransport *transport) 659{ 660 tmsg m; 661 m.transport = transport; 662 m.action = 0; 663 D("transport: %s removed\n", transport->serial); 664 if(transport_write_action(transport_registration_send, &m)) { 665 fatal_errno("cannot write transport registration socket\n"); 666 } 667} 668 669 670static void transport_unref_locked(atransport *t) 671{ 672 t->ref_count--; 673 if (t->ref_count == 0) { 674 D("transport: %s unref (kicking and closing)\n", t->serial); 675 if (!t->kicked) { 676 t->kicked = 1; 677 t->kick(t); 678 } 679 t->close(t); 680 remove_transport(t); 681 } else { 682 D("transport: %s unref (count=%d)\n", t->serial, t->ref_count); 683 } 684} 685 686static void transport_unref(atransport *t) 687{ 688 if (t) { 689 adb_mutex_lock(&transport_lock); 690 transport_unref_locked(t); 691 adb_mutex_unlock(&transport_lock); 692 } 693} 694 695void add_transport_disconnect(atransport* t, adisconnect* dis) 696{ 697 adb_mutex_lock(&transport_lock); 698 dis->next = &t->disconnects; 699 dis->prev = dis->next->prev; 700 dis->prev->next = dis; 701 dis->next->prev = dis; 702 adb_mutex_unlock(&transport_lock); 703} 704 705void remove_transport_disconnect(atransport* t, adisconnect* dis) 706{ 707 dis->prev->next = dis->next; 708 dis->next->prev = dis->prev; 709 dis->next = dis->prev = dis; 710} 711 712static int qual_match(const char *to_test, 713 const char *prefix, const char *qual, bool sanitize_qual) 714{ 715 if (!to_test || !*to_test) 716 /* Return true if both the qual and to_test are null strings. */ 717 return !qual || !*qual; 718 719 if (!qual) 720 return 0; 721 722 if (prefix) { 723 while (*prefix) { 724 if (*prefix++ != *to_test++) 725 return 0; 726 } 727 } 728 729 while (*qual) { 730 char ch = *qual++; 731 if (sanitize_qual && !isalnum(ch)) 732 ch = '_'; 733 if (ch != *to_test++) 734 return 0; 735 } 736 737 /* Everything matched so far. Return true if *to_test is a NUL. */ 738 return !*to_test; 739} 740 741atransport* acquire_one_transport(ConnectionState state, TransportType type, 742 const char* serial, std::string* error_out) { 743 atransport *t; 744 atransport *result = NULL; 745 int ambiguous = 0; 746 747retry: 748 if (error_out) *error_out = android::base::StringPrintf("device '%s' not found", serial); 749 750 adb_mutex_lock(&transport_lock); 751 for (t = transport_list.next; t != &transport_list; t = t->next) { 752 if (t->connection_state == kCsNoPerm) { 753 if (error_out) *error_out = "insufficient permissions for device"; 754 continue; 755 } 756 757 /* check for matching serial number */ 758 if (serial) { 759 if ((t->serial && !strcmp(serial, t->serial)) || 760 (t->devpath && !strcmp(serial, t->devpath)) || 761 qual_match(serial, "product:", t->product, false) || 762 qual_match(serial, "model:", t->model, true) || 763 qual_match(serial, "device:", t->device, false)) { 764 if (result) { 765 if (error_out) *error_out = "more than one device"; 766 ambiguous = 1; 767 result = NULL; 768 break; 769 } 770 result = t; 771 } 772 } else { 773 if (type == kTransportUsb && t->type == kTransportUsb) { 774 if (result) { 775 if (error_out) *error_out = "more than one device"; 776 ambiguous = 1; 777 result = NULL; 778 break; 779 } 780 result = t; 781 } else if (type == kTransportLocal && t->type == kTransportLocal) { 782 if (result) { 783 if (error_out) *error_out = "more than one emulator"; 784 ambiguous = 1; 785 result = NULL; 786 break; 787 } 788 result = t; 789 } else if (type == kTransportAny) { 790 if (result) { 791 if (error_out) *error_out = "more than one device/emulator"; 792 ambiguous = 1; 793 result = NULL; 794 break; 795 } 796 result = t; 797 } 798 } 799 } 800 adb_mutex_unlock(&transport_lock); 801 802 if (result) { 803 if (result->connection_state == kCsUnauthorized) { 804 if (error_out) { 805 *error_out = "device unauthorized.\n"; 806 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); 807 *error_out += "This adbd's $ADB_VENDOR_KEYS is "; 808 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; 809 *error_out += "; try 'adb kill-server' if that seems wrong.\n"; 810 *error_out += "Otherwise check for a confirmation dialog on your device."; 811 } 812 result = NULL; 813 } 814 815 /* offline devices are ignored -- they are either being born or dying */ 816 if (result && result->connection_state == kCsOffline) { 817 if (error_out) *error_out = "device offline"; 818 result = NULL; 819 } 820 821 /* check for required connection state */ 822 if (result && state != kCsAny && result->connection_state != state) { 823 if (error_out) *error_out = "invalid device state"; 824 result = NULL; 825 } 826 } 827 828 if (result) { 829 /* found one that we can take */ 830 if (error_out) *error_out = "success"; 831 } else if (state != kCsAny && (serial || !ambiguous)) { 832 adb_sleep_ms(1000); 833 goto retry; 834 } 835 836 return result; 837} 838 839const char* atransport::connection_state_name() const { 840 switch (connection_state) { 841 case kCsOffline: return "offline"; 842 case kCsBootloader: return "bootloader"; 843 case kCsDevice: return "device"; 844 case kCsHost: return "host"; 845 case kCsRecovery: return "recovery"; 846 case kCsNoPerm: return "no permissions"; 847 case kCsSideload: return "sideload"; 848 case kCsUnauthorized: return "unauthorized"; 849 default: return "unknown"; 850 } 851} 852 853#if ADB_HOST 854 855static void append_transport_info(std::string* result, const char* key, 856 const char* value, bool sanitize) { 857 if (value == nullptr || *value == '\0') { 858 return; 859 } 860 861 *result += ' '; 862 *result += key; 863 864 for (const char* p = value; *p; ++p) { 865 result->push_back((!sanitize || isalnum(*p)) ? *p : '_'); 866 } 867} 868 869static void append_transport(atransport* t, std::string* result, bool long_listing) { 870 const char* serial = t->serial; 871 if (!serial || !serial[0]) { 872 serial = "(no serial number)"; 873 } 874 875 if (!long_listing) { 876 *result += serial; 877 *result += '\t'; 878 *result += t->connection_state_name(); 879 } else { 880 android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name()); 881 882 append_transport_info(result, "", t->devpath, false); 883 append_transport_info(result, "product:", t->product, false); 884 append_transport_info(result, "model:", t->model, true); 885 append_transport_info(result, "device:", t->device, false); 886 } 887 *result += '\n'; 888} 889 890std::string list_transports(bool long_listing) { 891 std::string result; 892 adb_mutex_lock(&transport_lock); 893 for (atransport* t = transport_list.next; t != &transport_list; t = t->next) { 894 append_transport(t, &result, long_listing); 895 } 896 adb_mutex_unlock(&transport_lock); 897 return result; 898} 899 900/* hack for osx */ 901void close_usb_devices() 902{ 903 adb_mutex_lock(&transport_lock); 904 for (atransport* t = transport_list.next; t != &transport_list; t = t->next) { 905 if ( !t->kicked ) { 906 t->kicked = 1; 907 t->kick(t); 908 } 909 } 910 adb_mutex_unlock(&transport_lock); 911} 912#endif // ADB_HOST 913 914int register_socket_transport(int s, const char *serial, int port, int local) 915{ 916 atransport *t = reinterpret_cast<atransport*>(calloc(1, sizeof(atransport))); 917 if (t == nullptr) { 918 return -1; 919 } 920 921 atransport *n; 922 char buff[32]; 923 924 if (!serial) { 925 snprintf(buff, sizeof buff, "T-%p", t); 926 serial = buff; 927 } 928 D("transport: %s init'ing for socket %d, on port %d\n", serial, s, port); 929 if (init_socket_transport(t, s, port, local) < 0) { 930 free(t); 931 return -1; 932 } 933 934 adb_mutex_lock(&transport_lock); 935 for (n = pending_list.next; n != &pending_list; n = n->next) { 936 if (n->serial && !strcmp(serial, n->serial)) { 937 adb_mutex_unlock(&transport_lock); 938 free(t); 939 return -1; 940 } 941 } 942 943 for (n = transport_list.next; n != &transport_list; n = n->next) { 944 if (n->serial && !strcmp(serial, n->serial)) { 945 adb_mutex_unlock(&transport_lock); 946 free(t); 947 return -1; 948 } 949 } 950 951 t->next = &pending_list; 952 t->prev = pending_list.prev; 953 t->next->prev = t; 954 t->prev->next = t; 955 t->serial = strdup(serial); 956 adb_mutex_unlock(&transport_lock); 957 958 register_transport(t); 959 return 0; 960} 961 962#if ADB_HOST 963atransport *find_transport(const char *serial) 964{ 965 atransport *t; 966 967 adb_mutex_lock(&transport_lock); 968 for(t = transport_list.next; t != &transport_list; t = t->next) { 969 if (t->serial && !strcmp(serial, t->serial)) { 970 break; 971 } 972 } 973 adb_mutex_unlock(&transport_lock); 974 975 if (t != &transport_list) 976 return t; 977 else 978 return 0; 979} 980 981void unregister_transport(atransport *t) 982{ 983 adb_mutex_lock(&transport_lock); 984 t->next->prev = t->prev; 985 t->prev->next = t->next; 986 adb_mutex_unlock(&transport_lock); 987 988 kick_transport(t); 989 transport_unref(t); 990} 991 992// unregisters all non-emulator TCP transports 993void unregister_all_tcp_transports() 994{ 995 atransport *t, *next; 996 adb_mutex_lock(&transport_lock); 997 for (t = transport_list.next; t != &transport_list; t = next) { 998 next = t->next; 999 if (t->type == kTransportLocal && t->adb_port == 0) { 1000 t->next->prev = t->prev; 1001 t->prev->next = next; 1002 // we cannot call kick_transport when holding transport_lock 1003 if (!t->kicked) 1004 { 1005 t->kicked = 1; 1006 t->kick(t); 1007 } 1008 transport_unref_locked(t); 1009 } 1010 } 1011 1012 adb_mutex_unlock(&transport_lock); 1013} 1014 1015#endif 1016 1017void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable) 1018{ 1019 atransport *t = reinterpret_cast<atransport*>(calloc(1, sizeof(atransport))); 1020 if (t == nullptr) fatal("cannot allocate USB atransport"); 1021 D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, 1022 serial ? serial : ""); 1023 init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm)); 1024 if(serial) { 1025 t->serial = strdup(serial); 1026 } 1027 if(devpath) { 1028 t->devpath = strdup(devpath); 1029 } 1030 1031 adb_mutex_lock(&transport_lock); 1032 t->next = &pending_list; 1033 t->prev = pending_list.prev; 1034 t->next->prev = t; 1035 t->prev->next = t; 1036 adb_mutex_unlock(&transport_lock); 1037 1038 register_transport(t); 1039} 1040 1041// This should only be used for transports with connection_state == kCsNoPerm. 1042void unregister_usb_transport(usb_handle* usb) { 1043 adb_mutex_lock(&transport_lock); 1044 for (atransport* t = transport_list.next; t != &transport_list; 1045 t = t->next) { 1046 if (t->usb == usb && t->connection_state == kCsNoPerm) { 1047 t->next->prev = t->prev; 1048 t->prev->next = t->next; 1049 break; 1050 } 1051 } 1052 adb_mutex_unlock(&transport_lock); 1053} 1054 1055#undef TRACE_TAG 1056#define TRACE_TAG TRACE_RWX 1057 1058int check_header(apacket *p) 1059{ 1060 if(p->msg.magic != (p->msg.command ^ 0xffffffff)) { 1061 D("check_header(): invalid magic\n"); 1062 return -1; 1063 } 1064 1065 if(p->msg.data_length > MAX_PAYLOAD) { 1066 D("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length); 1067 return -1; 1068 } 1069 1070 return 0; 1071} 1072 1073int check_data(apacket *p) 1074{ 1075 unsigned count, sum; 1076 unsigned char *x; 1077 1078 count = p->msg.data_length; 1079 x = p->data; 1080 sum = 0; 1081 while(count-- > 0) { 1082 sum += *x++; 1083 } 1084 1085 if(sum != p->msg.data_check) { 1086 return -1; 1087 } else { 1088 return 0; 1089 } 1090} 1091