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