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