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