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