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