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