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