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