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