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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG 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> |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <string.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 28 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 30 | #include <algorithm> |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 31 | #include <list> |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 32 | #include <mutex> |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 33 | #include <thread> |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 35 | #include <android-base/logging.h> |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 36 | #include <android-base/parsenetaddress.h> |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 37 | #include <android-base/quick_exit.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
| 39 | #include <android-base/strings.h> |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 40 | #include <android-base/thread_annotations.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 41 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | #include "adb.h" |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 43 | #include "adb_auth.h" |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 44 | #include "adb_io.h" |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 45 | #include "adb_trace.h" |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 46 | #include "adb_utils.h" |
Elliott Hughes | 7009749 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 47 | #include "diagnose_usb.h" |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 48 | #include "fdevent.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
| 50 | static void transport_unref(atransport *t); |
| 51 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 52 | // TODO: unordered_map<TransportId, atransport*> |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 53 | static auto& transport_list = *new std::list<atransport*>(); |
| 54 | static auto& pending_list = *new std::list<atransport*>(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 55 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 56 | static auto& transport_lock = *new std::recursive_mutex(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | |
Todd Kennedy | aff9c67 | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 58 | const char* const kFeatureShell2 = "shell_v2"; |
| 59 | const char* const kFeatureCmd = "cmd"; |
Josh Gao | a2cf375 | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 60 | const char* const kFeatureStat2 = "stat_v2"; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 61 | const char* const kFeatureLibusb = "libusb"; |
Dan Albert | 27983bc | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 62 | const char* const kFeaturePushSync = "push_sync"; |
Todd Kennedy | aff9c67 | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 63 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 64 | TransportId NextTransportId() { |
| 65 | static std::atomic<TransportId> next(1); |
| 66 | return next++; |
| 67 | } |
| 68 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 69 | bool FdConnection::Read(apacket* packet) { |
| 70 | if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) { |
| 71 | D("remote local: read terminated (message)"); |
| 72 | return false; |
| 73 | } |
| 74 | |
Josh Gao | b14756a | 2018-02-02 14:38:04 -0800 | [diff] [blame^] | 75 | if (packet->msg.data_length > sizeof(packet->data)) { |
| 76 | D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length); |
| 77 | return false; |
| 78 | } |
| 79 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 80 | if (!ReadFdExactly(fd_.get(), &packet->data, packet->msg.data_length)) { |
| 81 | D("remote local: terminated (data)"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool FdConnection::Write(apacket* packet) { |
| 89 | uint32_t length = packet->msg.data_length; |
| 90 | |
| 91 | if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(amessage) + length)) { |
| 92 | D("remote local: write terminated"); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | void FdConnection::Close() { |
| 100 | adb_shutdown(fd_.get()); |
| 101 | fd_.reset(); |
| 102 | } |
| 103 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 104 | static std::string dump_packet(const char* name, const char* func, apacket* p) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 105 | unsigned command = p->msg.command; |
| 106 | int len = p->msg.data_length; |
| 107 | char cmd[9]; |
| 108 | char arg0[12], arg1[12]; |
| 109 | int n; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 110 | |
| 111 | for (n = 0; n < 4; n++) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 112 | int b = (command >> (n * 8)) & 255; |
| 113 | if (b < 32 || b >= 127) break; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 114 | cmd[n] = (char)b; |
| 115 | } |
| 116 | if (n == 4) { |
| 117 | cmd[4] = 0; |
| 118 | } else { |
| 119 | /* There is some non-ASCII name in the command, so dump |
| 120 | * the hexadecimal value instead */ |
| 121 | snprintf(cmd, sizeof cmd, "%08x", command); |
| 122 | } |
| 123 | |
| 124 | if (p->msg.arg0 < 256U) |
| 125 | snprintf(arg0, sizeof arg0, "%d", p->msg.arg0); |
| 126 | else |
| 127 | snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0); |
| 128 | |
| 129 | if (p->msg.arg1 < 256U) |
| 130 | snprintf(arg1, sizeof arg1, "%d", p->msg.arg1); |
| 131 | else |
| 132 | snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1); |
| 133 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 134 | std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name, |
| 135 | func, cmd, arg0, arg1, len); |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 136 | result += dump_hex(p->data, len); |
| 137 | return result; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 138 | } |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 139 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 140 | static int read_packet(int fd, const char* name, apacket** ppacket) { |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 141 | ATRACE_NAME("read_packet"); |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 142 | char buff[8]; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 143 | if (!name) { |
| 144 | snprintf(buff, sizeof buff, "fd=%d", fd); |
| 145 | name = buff; |
| 146 | } |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 147 | char* p = reinterpret_cast<char*>(ppacket); /* really read a packet address */ |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 148 | int len = sizeof(apacket*); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 149 | while (len > 0) { |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 150 | int r = adb_read(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 151 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | len -= r; |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 153 | p += r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 154 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 155 | D("%s: read_packet (fd=%d), error ret=%d: %s", name, fd, r, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | return -1; |
| 157 | } |
| 158 | } |
| 159 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 160 | VLOG(TRANSPORT) << dump_packet(name, "from remote", *ppacket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 164 | static int write_packet(int fd, const char* name, apacket** ppacket) { |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 165 | ATRACE_NAME("write_packet"); |
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 | } |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 171 | VLOG(TRANSPORT) << dump_packet(name, "to remote", *ppacket); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 172 | char* p = reinterpret_cast<char*>(ppacket); /* we really write the packet address */ |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 173 | int len = sizeof(apacket*); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 174 | while (len > 0) { |
Yabin Cui | a96dabf | 2015-07-30 19:58:10 -0700 | [diff] [blame] | 175 | int r = adb_write(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 176 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 177 | len -= r; |
| 178 | p += r; |
| 179 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 180 | D("%s: write_packet (fd=%d) error ret=%d: %s", name, fd, r, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 187 | static void transport_socket_events(int fd, unsigned events, void* _t) { |
| 188 | atransport* t = reinterpret_cast<atransport*>(_t); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 189 | D("transport_socket_events(fd=%d, events=%04x,...)", fd, events); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 190 | if (events & FDE_READ) { |
| 191 | apacket* p = 0; |
| 192 | if (read_packet(fd, t->serial, &p)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 193 | D("%s: failed to read packet from transport socket on fd %d", t->serial, fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 195 | handle_packet(p, (atransport*)_t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 200 | void send_packet(apacket* p, atransport* t) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | p->msg.magic = p->msg.command ^ 0xffffffff; |
Tim Murray | ee7b44d | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 202 | // compute a checksum for connection/auth packets for compatibility reasons |
| 203 | if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) { |
| 204 | p->msg.data_check = 0; |
| 205 | } else { |
| 206 | p->msg.data_check = calculate_apacket_checksum(p); |
| 207 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 208 | |
| 209 | print_packet("send", p); |
| 210 | |
| 211 | if (t == NULL) { |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 212 | fatal("Transport is null"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 215 | if (write_packet(t->transport_socket, t->serial, &p)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 216 | fatal_errno("cannot enqueue packet on transport socket"); |
| 217 | } |
| 218 | } |
| 219 | |
Yabin Cui | 4e22229 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 220 | // The transport is opened by transport_register_func before |
| 221 | // the read_transport and write_transport threads are started. |
| 222 | // |
| 223 | // The read_transport thread issues a SYNC(1, token) message to let |
| 224 | // the write_transport thread know to start things up. In the event |
| 225 | // of transport IO failure, the read_transport thread will post a |
| 226 | // SYNC(0,0) message to ensure shutdown. |
| 227 | // |
| 228 | // The transport will not actually be closed until both threads exit, but the threads |
| 229 | // will kick the transport on their way out to disconnect the underlying device. |
| 230 | // |
| 231 | // read_transport thread reads data from a transport (representing a usb/tcp connection), |
| 232 | // and makes the main thread call handle_packet(). |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 233 | static void read_transport_thread(void* _t) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 234 | atransport* t = reinterpret_cast<atransport*>(_t); |
| 235 | apacket* p; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 237 | adb_thread_setname( |
| 238 | android::base::StringPrintf("<-%s", (t->serial != nullptr ? t->serial : "transport"))); |
| 239 | D("%s: starting read_transport thread on fd %d, SYNC online (%d)", t->serial, t->fd, |
| 240 | t->sync_token + 1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | p = get_apacket(); |
| 242 | p->msg.command = A_SYNC; |
| 243 | p->msg.arg0 = 1; |
| 244 | p->msg.arg1 = ++(t->sync_token); |
| 245 | p->msg.magic = A_SYNC ^ 0xffffffff; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 246 | if (write_packet(t->fd, t->serial, &p)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | put_apacket(p); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 248 | D("%s: failed to write SYNC packet", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | goto oops; |
| 250 | } |
| 251 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 252 | D("%s: data pump started", t->serial); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 253 | for (;;) { |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 254 | ATRACE_NAME("read_transport loop"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | p = get_apacket(); |
| 256 | |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 257 | { |
| 258 | ATRACE_NAME("read_transport read_remote"); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 259 | if (!t->connection->Read(p)) { |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 260 | D("%s: remote read failed for transport", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | put_apacket(p); |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 262 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | } |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 264 | |
| 265 | if (!check_header(p, t)) { |
| 266 | D("%s: remote read: bad header", t->serial); |
| 267 | put_apacket(p); |
| 268 | break; |
| 269 | } |
| 270 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 271 | #if ADB_HOST |
| 272 | if (p->msg.command == 0) { |
Josh Gao | 30c4266 | 2018-01-29 18:09:20 -0800 | [diff] [blame] | 273 | put_apacket(p); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 274 | continue; |
| 275 | } |
| 276 | #endif |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | D("%s: received remote packet, sending to transport", t->serial); |
| 280 | if (write_packet(t->fd, t->serial, &p)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 281 | put_apacket(p); |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 282 | D("%s: failed to write apacket to transport", t->serial); |
| 283 | goto oops; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 287 | D("%s: SYNC offline for transport", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | p = get_apacket(); |
| 289 | p->msg.command = A_SYNC; |
| 290 | p->msg.arg0 = 0; |
| 291 | p->msg.arg1 = 0; |
| 292 | p->msg.magic = A_SYNC ^ 0xffffffff; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 293 | if (write_packet(t->fd, t->serial, &p)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 294 | put_apacket(p); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 295 | 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] | 296 | } |
| 297 | |
| 298 | oops: |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 299 | D("%s: read_transport thread is exiting", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | kick_transport(t); |
| 301 | transport_unref(t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Yabin Cui | 4e22229 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 304 | // write_transport thread gets packets sent by the main thread (through send_packet()), |
| 305 | // and writes to a transport (representing a usb/tcp connection). |
Josh Gao | 7d40525 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 306 | static void write_transport_thread(void* _t) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 307 | atransport* t = reinterpret_cast<atransport*>(_t); |
| 308 | apacket* p; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | int active = 0; |
| 310 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 311 | adb_thread_setname( |
| 312 | android::base::StringPrintf("->%s", (t->serial != nullptr ? t->serial : "transport"))); |
| 313 | D("%s: starting write_transport thread, reading from fd %d", t->serial, t->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 315 | for (;;) { |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 316 | ATRACE_NAME("write_transport loop"); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 317 | if (read_packet(t->fd, t->serial, &p)) { |
| 318 | D("%s: failed to read apacket from transport on fd %d", t->serial, t->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 319 | break; |
| 320 | } |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 321 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 322 | if (p->msg.command == A_SYNC) { |
| 323 | if (p->msg.arg0 == 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 324 | D("%s: transport SYNC offline", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | put_apacket(p); |
| 326 | break; |
| 327 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 328 | if (p->msg.arg1 == t->sync_token) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 329 | D("%s: transport SYNC online", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | active = 1; |
| 331 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 332 | D("%s: transport ignoring SYNC %d != %d", t->serial, p->msg.arg1, t->sync_token); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 336 | if (active) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 337 | D("%s: transport got packet, sending to remote", t->serial); |
Josh Gao | 9e09a97 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 338 | ATRACE_NAME("write_transport write_remote"); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 339 | if (t->Write(p) != 0) { |
| 340 | D("%s: remote write failed for transport", t->serial); |
| 341 | put_apacket(p); |
| 342 | break; |
| 343 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 345 | D("%s: transport ignoring packet while offline", t->serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | put_apacket(p); |
| 350 | } |
| 351 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 352 | D("%s: write_transport thread is exiting, fd %d", t->serial, t->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | kick_transport(t); |
| 354 | transport_unref(t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 357 | void kick_transport(atransport* t) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 358 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 359 | // As kick_transport() can be called from threads without guarantee that t is valid, |
| 360 | // check if the transport is in transport_list first. |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 361 | // |
| 362 | // TODO(jmgao): WTF? Is this actually true? |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 363 | if (std::find(transport_list.begin(), transport_list.end(), t) != transport_list.end()) { |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 364 | t->Kick(); |
Yabin Cui | d78ed22 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 365 | } |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 366 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | |
| 368 | static int transport_registration_send = -1; |
| 369 | static int transport_registration_recv = -1; |
| 370 | static fdevent transport_registration_fde; |
| 371 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | #if ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 373 | |
| 374 | /* this adds support required by the 'track-devices' service. |
| 375 | * this is used to send the content of "list_transport" to any |
| 376 | * number of client connections that want it through a single |
| 377 | * live TCP connection |
| 378 | */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | struct device_tracker { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 380 | asocket socket; |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 381 | bool update_needed; |
| 382 | bool long_output; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 383 | device_tracker* next; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | /* linked list of all device trackers */ |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 387 | static device_tracker* device_tracker_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 389 | static void device_tracker_remove(device_tracker* tracker) { |
| 390 | device_tracker** pnode = &device_tracker_list; |
| 391 | device_tracker* node = *pnode; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 393 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 394 | while (node) { |
| 395 | if (node == tracker) { |
| 396 | *pnode = node->next; |
| 397 | break; |
| 398 | } |
| 399 | pnode = &node->next; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 400 | node = *pnode; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 404 | static void device_tracker_close(asocket* socket) { |
| 405 | device_tracker* tracker = (device_tracker*)socket; |
| 406 | asocket* peer = socket->peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 408 | D("device tracker %p removed", tracker); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | if (peer) { |
| 410 | peer->peer = NULL; |
| 411 | peer->close(peer); |
| 412 | } |
| 413 | device_tracker_remove(tracker); |
| 414 | free(tracker); |
| 415 | } |
| 416 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 417 | static int device_tracker_enqueue(asocket* socket, apacket* p) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 418 | /* you can't read from a device tracker, close immediately */ |
| 419 | put_apacket(p); |
| 420 | device_tracker_close(socket); |
| 421 | return -1; |
| 422 | } |
| 423 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 424 | static int device_tracker_send(device_tracker* tracker, const std::string& string) { |
| 425 | apacket* p = get_apacket(); |
| 426 | asocket* peer = tracker->socket.peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 428 | snprintf(reinterpret_cast<char*>(p->data), 5, "%04x", static_cast<int>(string.size())); |
| 429 | memcpy(&p->data[4], string.data(), string.size()); |
| 430 | p->len = 4 + string.size(); |
| 431 | return peer->enqueue(peer, p); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 434 | static void device_tracker_ready(asocket* socket) { |
| 435 | device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 436 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 437 | // We want to send the device list when the tracker connects |
| 438 | // for the first time, even if no update occurred. |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 439 | if (tracker->update_needed) { |
| 440 | tracker->update_needed = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 442 | std::string transports = list_transports(tracker->long_output); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 443 | device_tracker_send(tracker, transports); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 447 | asocket* create_device_tracker(bool long_output) { |
Elliott Hughes | d0269c9 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 448 | device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker))); |
| 449 | if (tracker == nullptr) fatal("cannot allocate device tracker"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 451 | D("device tracker %p created", tracker); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | |
| 453 | tracker->socket.enqueue = device_tracker_enqueue; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 454 | tracker->socket.ready = device_tracker_ready; |
| 455 | tracker->socket.close = device_tracker_close; |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 456 | tracker->update_needed = true; |
| 457 | tracker->long_output = long_output; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 459 | tracker->next = device_tracker_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | device_tracker_list = tracker; |
| 461 | |
| 462 | return &tracker->socket; |
| 463 | } |
| 464 | |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 465 | // Check if all of the USB transports are connected. |
| 466 | bool iterate_transports(std::function<bool(const atransport*)> fn) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 467 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 468 | for (const auto& t : transport_list) { |
| 469 | if (!fn(t)) { |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | for (const auto& t : pending_list) { |
| 474 | if (!fn(t)) { |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | return true; |
| 479 | } |
| 480 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 481 | // Call this function each time the transport list has changed. |
| 482 | void update_transports() { |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 483 | update_transport_status(); |
| 484 | |
| 485 | // Notify `adb track-devices` clients. |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 486 | std::string transports = list_transports(false); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 488 | device_tracker* tracker = device_tracker_list; |
| 489 | while (tracker != nullptr) { |
| 490 | device_tracker* next = tracker->next; |
| 491 | // This may destroy the tracker if the connection is closed. |
| 492 | device_tracker_send(tracker, transports); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | tracker = next; |
| 494 | } |
| 495 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 496 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 497 | #else |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 498 | |
| 499 | void update_transports() { |
| 500 | // Nothing to do on the device side. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 502 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 503 | #endif // ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 504 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 505 | struct tmsg { |
| 506 | atransport* transport; |
| 507 | int action; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 508 | }; |
| 509 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 510 | static int transport_read_action(int fd, struct tmsg* m) { |
| 511 | char* p = (char*)m; |
| 512 | int len = sizeof(*m); |
| 513 | int r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 515 | while (len > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | r = adb_read(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 517 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | len -= r; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 519 | p += r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 520 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 521 | D("transport_read_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 | return -1; |
| 523 | } |
| 524 | } |
| 525 | return 0; |
| 526 | } |
| 527 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 528 | static int transport_write_action(int fd, struct tmsg* m) { |
| 529 | char* p = (char*)m; |
| 530 | int len = sizeof(*m); |
| 531 | int r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 533 | while (len > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | r = adb_write(fd, p, len); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 535 | if (r > 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | len -= r; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 537 | p += r; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | } else { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 539 | D("transport_write_action: on fd %d: %s", fd, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 540 | return -1; |
| 541 | } |
| 542 | } |
| 543 | return 0; |
| 544 | } |
| 545 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 546 | static void transport_registration_func(int _fd, unsigned ev, void* data) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 | tmsg m; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | int s[2]; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 549 | atransport* t; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 550 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 551 | if (!(ev & FDE_READ)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 555 | if (transport_read_action(_fd, &m)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | fatal_errno("cannot read transport registration socket"); |
| 557 | } |
| 558 | |
| 559 | t = m.transport; |
| 560 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 561 | if (m.action == 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 562 | D("transport: %s removing and free'ing %d", t->serial, t->transport_socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 563 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 564 | /* IMPORTANT: the remove closes one half of the |
| 565 | ** socket pair. The close closes the other half. |
| 566 | */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 567 | fdevent_remove(&(t->transport_fde)); |
| 568 | adb_close(t->fd); |
| 569 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 570 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 571 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 572 | transport_list.remove(t); |
| 573 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 574 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 575 | if (t->product) free(t->product); |
| 576 | if (t->serial) free(t->serial); |
| 577 | if (t->model) free(t->model); |
| 578 | if (t->device) free(t->device); |
| 579 | if (t->devpath) free(t->devpath); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 581 | delete t; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 | |
| 583 | update_transports(); |
| 584 | return; |
| 585 | } |
| 586 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 587 | /* don't create transport threads for inaccessible devices */ |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 588 | if (t->GetConnectionState() != kCsNoPerm) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 589 | /* initial references are the two threads */ |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 590 | t->ref_count = 2; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 591 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 592 | if (adb_socketpair(s)) { |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 593 | fatal_errno("cannot open transport socketpair"); |
| 594 | } |
| 595 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 596 | 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] | 597 | |
| 598 | t->transport_socket = s[0]; |
| 599 | t->fd = s[1]; |
| 600 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 601 | fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 602 | |
| 603 | fdevent_set(&(t->transport_fde), FDE_READ); |
| 604 | |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 605 | std::thread(write_transport_thread, t).detach(); |
| 606 | std::thread(read_transport_thread, t).detach(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 609 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 610 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 611 | pending_list.remove(t); |
| 612 | transport_list.push_front(t); |
| 613 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 614 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 615 | update_transports(); |
| 616 | } |
| 617 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 618 | void init_transport_registration(void) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 619 | int s[2]; |
| 620 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 621 | if (adb_socketpair(s)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | fatal_errno("cannot open transport registration socketpair"); |
| 623 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 624 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 625 | |
| 626 | transport_registration_send = s[0]; |
| 627 | transport_registration_recv = s[1]; |
| 628 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 629 | fdevent_install(&transport_registration_fde, transport_registration_recv, |
| 630 | transport_registration_func, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | |
| 632 | fdevent_set(&transport_registration_fde, FDE_READ); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void kick_all_transports() { |
| 636 | // To avoid only writing part of a packet to a transport after exit, kick all transports. |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 637 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 638 | for (auto t : transport_list) { |
| 639 | t->Kick(); |
| 640 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* the fdevent select pump is single threaded */ |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 644 | static void register_transport(atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 645 | tmsg m; |
| 646 | m.transport = transport; |
| 647 | m.action = 1; |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 648 | D("transport: %s registered", transport->serial); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 649 | if (transport_write_action(transport_registration_send, &m)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 650 | fatal_errno("cannot write transport registration socket\n"); |
| 651 | } |
| 652 | } |
| 653 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 654 | static void remove_transport(atransport* transport) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | tmsg m; |
| 656 | m.transport = transport; |
| 657 | m.action = 0; |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 658 | D("transport: %s removed", transport->serial); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 659 | if (transport_write_action(transport_registration_send, &m)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 660 | fatal_errno("cannot write transport registration socket\n"); |
| 661 | } |
| 662 | } |
| 663 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 664 | static void transport_unref(atransport* t) { |
| 665 | CHECK(t != nullptr); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 666 | |
Josh Gao | 4d29974 | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 667 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
| 668 | CHECK_GT(t->ref_count, 0u); |
| 669 | t->ref_count--; |
| 670 | if (t->ref_count == 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 671 | D("transport: %s unref (kicking and closing)", t->serial); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 672 | t->connection->Close(); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 673 | remove_transport(t); |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 674 | } else { |
Josh Gao | 4d29974 | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 675 | D("transport: %s unref (count=%zu)", t->serial, t->ref_count); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 676 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 679 | static int qual_match(const char* to_test, const char* prefix, const char* qual, |
| 680 | bool sanitize_qual) { |
| 681 | if (!to_test || !*to_test) /* Return true if both the qual and to_test are null strings. */ |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 682 | return !qual || !*qual; |
| 683 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 684 | if (!qual) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 685 | |
| 686 | if (prefix) { |
| 687 | while (*prefix) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 688 | if (*prefix++ != *to_test++) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | while (*qual) { |
| 693 | char ch = *qual++; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 694 | if (sanitize_qual && !isalnum(ch)) ch = '_'; |
| 695 | if (ch != *to_test++) return 0; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* Everything matched so far. Return true if *to_test is a NUL. */ |
| 699 | return !*to_test; |
| 700 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 701 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 702 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 703 | bool* is_ambiguous, std::string* error_out, |
| 704 | bool accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 705 | atransport* result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 706 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 707 | if (transport_id != 0) { |
| 708 | *error_out = |
| 709 | android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); |
| 710 | } else if (serial) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 711 | *error_out = android::base::StringPrintf("device '%s' not found", serial); |
| 712 | } else if (type == kTransportLocal) { |
| 713 | *error_out = "no emulators found"; |
| 714 | } else if (type == kTransportAny) { |
| 715 | *error_out = "no devices/emulators found"; |
| 716 | } else { |
| 717 | *error_out = "no devices found"; |
| 718 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 719 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 720 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 721 | for (const auto& t : transport_list) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 722 | if (t->GetConnectionState() == kCsNoPerm) { |
Elliott Hughes | 7009749 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 723 | #if ADB_HOST |
David Pursell | 6e5c7eb | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 724 | *error_out = UsbNoPermissionsLongHelpText(); |
Elliott Hughes | 7009749 | 2015-12-11 19:07:01 -0800 | [diff] [blame] | 725 | #endif |
Mike Lockwood | adc16b3 | 2009-08-08 13:53:16 -0400 | [diff] [blame] | 726 | continue; |
| 727 | } |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 728 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 729 | if (transport_id) { |
| 730 | if (t->id == transport_id) { |
| 731 | result = t; |
| 732 | break; |
| 733 | } |
| 734 | } else if (serial) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 735 | if (t->MatchesTarget(serial)) { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 736 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 737 | *error_out = "more than one device"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 738 | if (is_ambiguous) *is_ambiguous = true; |
| 739 | result = nullptr; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 740 | break; |
| 741 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 742 | result = t; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 743 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 744 | } else { |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 745 | if (type == kTransportUsb && t->type == kTransportUsb) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 746 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 747 | *error_out = "more than one device"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 748 | if (is_ambiguous) *is_ambiguous = true; |
| 749 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | result = t; |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 753 | } else if (type == kTransportLocal && t->type == kTransportLocal) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 754 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 755 | *error_out = "more than one emulator"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 756 | if (is_ambiguous) *is_ambiguous = true; |
| 757 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 758 | break; |
| 759 | } |
| 760 | result = t; |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 761 | } else if (type == kTransportAny) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 762 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 763 | *error_out = "more than one device/emulator"; |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 764 | if (is_ambiguous) *is_ambiguous = true; |
| 765 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | result = t; |
| 769 | } |
| 770 | } |
| 771 | } |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 772 | lock.unlock(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 773 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 774 | // Don't return unauthorized devices; the caller can't do anything with them. |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 775 | if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 776 | *error_out = "device unauthorized.\n"; |
| 777 | char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); |
| 778 | *error_out += "This adb server's $ADB_VENDOR_KEYS is "; |
| 779 | *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; |
| 780 | *error_out += "\n"; |
| 781 | *error_out += "Try 'adb kill-server' if that seems wrong.\n"; |
| 782 | *error_out += "Otherwise check for a confirmation dialog on your device."; |
| 783 | result = nullptr; |
| 784 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 785 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 786 | // Don't return offline devices; the caller can't do anything with them. |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 787 | if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 788 | *error_out = "device offline"; |
| 789 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 793 | *error_out = "success"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | return result; |
| 797 | } |
| 798 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 799 | int atransport::Write(apacket* p) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 800 | return this->connection->Write(p) ? 0 : -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 803 | void atransport::Kick() { |
| 804 | if (!kicked_) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 805 | D("kicking transport %s", this->serial); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 806 | kicked_ = true; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 807 | this->connection->Close(); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 811 | ConnectionState atransport::GetConnectionState() const { |
| 812 | return connection_state_; |
| 813 | } |
| 814 | |
| 815 | void atransport::SetConnectionState(ConnectionState state) { |
| 816 | check_main_thread(); |
| 817 | connection_state_ = state; |
| 818 | } |
| 819 | |
David Pursell | 6e5c7eb | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 820 | const std::string atransport::connection_state_name() const { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 821 | ConnectionState state = GetConnectionState(); |
| 822 | switch (state) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 823 | case kCsOffline: |
| 824 | return "offline"; |
| 825 | case kCsBootloader: |
| 826 | return "bootloader"; |
| 827 | case kCsDevice: |
| 828 | return "device"; |
| 829 | case kCsHost: |
| 830 | return "host"; |
| 831 | case kCsRecovery: |
| 832 | return "recovery"; |
| 833 | case kCsNoPerm: |
| 834 | return UsbNoPermissionsShortHelpText(); |
| 835 | case kCsSideload: |
| 836 | return "sideload"; |
| 837 | case kCsUnauthorized: |
| 838 | return "unauthorized"; |
| 839 | default: |
| 840 | return "unknown"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 844 | void atransport::update_version(int version, size_t payload) { |
| 845 | protocol_version = std::min(version, A_VERSION); |
| 846 | max_payload = std::min(payload, MAX_PAYLOAD); |
| 847 | } |
| 848 | |
| 849 | int atransport::get_protocol_version() const { |
| 850 | return protocol_version; |
| 851 | } |
| 852 | |
| 853 | size_t atransport::get_max_payload() const { |
| 854 | return max_payload; |
| 855 | } |
| 856 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 857 | namespace { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 858 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 859 | constexpr char kFeatureStringDelimiter = ','; |
| 860 | |
| 861 | } // namespace |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 862 | |
| 863 | const FeatureSet& supported_features() { |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 864 | // Local static allocation to avoid global non-POD variables. |
| 865 | static const FeatureSet* features = new FeatureSet{ |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 866 | kFeatureShell2, kFeatureCmd, kFeatureStat2, |
David Pursell | 6d5fad3 | 2015-09-25 08:37:13 -0700 | [diff] [blame] | 867 | // Increment ADB_SERVER_VERSION whenever the feature list changes to |
| 868 | // make sure that the adb client and server features stay in sync |
| 869 | // (http://b/24370690). |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | return *features; |
| 873 | } |
| 874 | |
| 875 | std::string FeatureSetToString(const FeatureSet& features) { |
| 876 | return android::base::Join(features, kFeatureStringDelimiter); |
| 877 | } |
| 878 | |
| 879 | FeatureSet StringToFeatureSet(const std::string& features_string) { |
David Pursell | 3d9072b | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 880 | if (features_string.empty()) { |
| 881 | return FeatureSet(); |
| 882 | } |
| 883 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 884 | auto names = android::base::Split(features_string, {kFeatureStringDelimiter}); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 885 | return FeatureSet(names.begin(), names.end()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 886 | } |
| 887 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 888 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 889 | return feature_set.count(feature) > 0 && supported_features().count(feature) > 0; |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 892 | bool atransport::has_feature(const std::string& feature) const { |
| 893 | return features_.count(feature) > 0; |
| 894 | } |
| 895 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 896 | void atransport::SetFeatures(const std::string& features_string) { |
| 897 | features_ = StringToFeatureSet(features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 900 | void atransport::AddDisconnect(adisconnect* disconnect) { |
| 901 | disconnects_.push_back(disconnect); |
| 902 | } |
| 903 | |
| 904 | void atransport::RemoveDisconnect(adisconnect* disconnect) { |
| 905 | disconnects_.remove(disconnect); |
| 906 | } |
| 907 | |
| 908 | void atransport::RunDisconnects() { |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 909 | for (const auto& disconnect : disconnects_) { |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 910 | disconnect->func(disconnect->opaque, this); |
| 911 | } |
| 912 | disconnects_.clear(); |
| 913 | } |
| 914 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 915 | bool atransport::MatchesTarget(const std::string& target) const { |
| 916 | if (serial) { |
| 917 | if (target == serial) { |
| 918 | return true; |
| 919 | } else if (type == kTransportLocal) { |
| 920 | // Local transports can match [tcp:|udp:]<hostname>[:port]. |
| 921 | const char* local_target_ptr = target.c_str(); |
| 922 | |
| 923 | // For fastboot compatibility, ignore protocol prefixes. |
| 924 | if (android::base::StartsWith(target, "tcp:") || |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 925 | android::base::StartsWith(target, "udp:")) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 926 | local_target_ptr += 4; |
| 927 | } |
| 928 | |
| 929 | // Parse our |serial| and the given |target| to check if the hostnames and ports match. |
| 930 | std::string serial_host, error; |
| 931 | int serial_port = -1; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 932 | if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 933 | // |target| may omit the port to default to ours. |
| 934 | std::string target_host; |
| 935 | int target_port = serial_port; |
| 936 | if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port, |
| 937 | nullptr, &error) && |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 938 | serial_host == target_host && serial_port == target_port) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 939 | return true; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | return (devpath && target == devpath) || |
| 946 | qual_match(target.c_str(), "product:", product, false) || |
| 947 | qual_match(target.c_str(), "model:", model, true) || |
| 948 | qual_match(target.c_str(), "device:", device, false); |
| 949 | } |
| 950 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 951 | #if ADB_HOST |
| 952 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 953 | // We use newline as our delimiter, make sure to never output it. |
| 954 | static std::string sanitize(std::string str, bool alphanumeric) { |
| 955 | auto pred = alphanumeric ? [](const char c) { return !isalnum(c); } |
| 956 | : [](const char c) { return c == '\n'; }; |
| 957 | std::replace_if(str.begin(), str.end(), pred, '_'); |
| 958 | return str; |
| 959 | } |
| 960 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 961 | static void append_transport_info(std::string* result, const char* key, const char* value, |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 962 | bool alphanumeric) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 963 | if (value == nullptr || *value == '\0') { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 964 | return; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 967 | *result += ' '; |
| 968 | *result += key; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 969 | *result += sanitize(value, alphanumeric); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 972 | static void append_transport(const atransport* t, std::string* result, bool long_listing) { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 973 | const char* serial = t->serial; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 974 | if (!serial || !serial[0]) { |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 975 | serial = "(no serial number)"; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 976 | } |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 977 | |
| 978 | if (!long_listing) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 979 | *result += serial; |
| 980 | *result += '\t'; |
| 981 | *result += t->connection_state_name(); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 982 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 983 | android::base::StringAppendF(result, "%-22s %s", serial, t->connection_state_name().c_str()); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 984 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 985 | append_transport_info(result, "", t->devpath, false); |
| 986 | append_transport_info(result, "product:", t->product, false); |
| 987 | append_transport_info(result, "model:", t->model, true); |
| 988 | append_transport_info(result, "device:", t->device, false); |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 989 | |
| 990 | // Put id at the end, so that anyone parsing the output here can always find it by scanning |
| 991 | // backwards from newlines, even with hypothetical devices named 'transport_id:1'. |
| 992 | *result += " transport_id:"; |
| 993 | *result += std::to_string(t->id); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 994 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 995 | *result += '\n'; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 998 | std::string list_transports(bool long_listing) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 999 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Artem Iglikov | 4dd3501 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1000 | |
| 1001 | auto sorted_transport_list = transport_list; |
| 1002 | sorted_transport_list.sort([](atransport*& x, atransport*& y) { |
| 1003 | if (x->type != y->type) { |
| 1004 | return x->type < y->type; |
| 1005 | } |
| 1006 | return strcmp(x->serial, y->serial) < 0; |
| 1007 | }); |
| 1008 | |
| 1009 | std::string result; |
| 1010 | for (const auto& t : sorted_transport_list) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1011 | append_transport(t, &result, long_listing); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1012 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1013 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1016 | void close_usb_devices(std::function<bool(const atransport*)> predicate) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1017 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1018 | for (auto& t : transport_list) { |
| 1019 | if (predicate(t)) { |
| 1020 | t->Kick(); |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1025 | /* hack for osx */ |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1026 | void close_usb_devices() { |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1027 | close_usb_devices([](const atransport*) { return true; }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1028 | } |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1029 | #endif // ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1030 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1031 | int register_socket_transport(int s, const char* serial, int port, int local) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1032 | atransport* t = new atransport(); |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1033 | |
| 1034 | if (!serial) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1035 | char buf[32]; |
| 1036 | snprintf(buf, sizeof(buf), "T-%p", t); |
| 1037 | serial = buf; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1038 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1039 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 1040 | D("transport: %s init'ing for socket %d, on port %d", serial, s, port); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1041 | if (init_socket_transport(t, s, port, local) < 0) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1042 | delete t; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1043 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1044 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1045 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1046 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1047 | for (const auto& transport : pending_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1048 | if (transport->serial && strcmp(serial, transport->serial) == 0) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1049 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1050 | << " is already in pending_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1051 | delete t; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1054 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1055 | |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1056 | for (const auto& transport : transport_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1057 | if (transport->serial && strcmp(serial, transport->serial) == 0) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1058 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1059 | << " is already in transport_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1060 | delete t; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1061 | return -1; |
| 1062 | } |
| 1063 | } |
| 1064 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1065 | pending_list.push_front(t); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1066 | t->serial = strdup(serial); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1067 | |
| 1068 | lock.unlock(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1069 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1070 | register_transport(t); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1071 | return 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1074 | #if ADB_HOST |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1075 | atransport* find_transport(const char* serial) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1076 | atransport* result = nullptr; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1077 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1078 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1079 | for (auto& t : transport_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1080 | if (t->serial && strcmp(serial, t->serial) == 0) { |
| 1081 | result = t; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1082 | break; |
| 1083 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1084 | } |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1085 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1086 | return result; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1087 | } |
| 1088 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1089 | void kick_all_tcp_devices() { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1090 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1091 | for (auto& t : transport_list) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1092 | if (t->IsTcpDevice()) { |
Yabin Cui | 4e22229 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 1093 | // Kicking breaks the read_transport thread of this transport out of any read, then |
| 1094 | // the read_transport thread will notify the main thread to make this transport |
| 1095 | // offline. Then the main thread will notify the write_transport thread to exit. |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1096 | // Finally, this transport will be closed and freed in the main thread. |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1097 | t->Kick(); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1098 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1099 | } |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1102 | #endif |
| 1103 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1104 | void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath, |
| 1105 | unsigned writeable) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1106 | atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm)); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1107 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1108 | D("transport: %p init'ing for usb_handle %p (sn='%s')", t, usb, serial ? serial : ""); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1109 | init_usb_transport(t, usb); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1110 | if (serial) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1111 | t->serial = strdup(serial); |
| 1112 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1113 | |
| 1114 | if (devpath) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1115 | t->devpath = strdup(devpath); |
| 1116 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1117 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1118 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1119 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1120 | pending_list.push_front(t); |
| 1121 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1122 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1123 | register_transport(t); |
| 1124 | } |
| 1125 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 1126 | // This should only be used for transports with connection_state == kCsNoPerm. |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1127 | void unregister_usb_transport(usb_handle* usb) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1128 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1129 | transport_list.remove_if([usb](atransport* t) { |
| 1130 | if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) { |
| 1131 | return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm; |
| 1132 | } |
| 1133 | return false; |
| 1134 | }); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 1135 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1136 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1137 | bool check_header(apacket* p, atransport* t) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1138 | if (p->msg.magic != (p->msg.command ^ 0xffffffff)) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1139 | VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command |
| 1140 | << ", magic = " << p->msg.magic; |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1141 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1144 | if (p->msg.data_length > t->get_max_payload()) { |
| 1145 | VLOG(RWX) << "check_header(): " << p->msg.data_length |
| 1146 | << " atransport::max_payload = " << t->get_max_payload(); |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1147 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1150 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1151 | } |
| 1152 | |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1153 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1154 | std::shared_ptr<RSA> atransport::NextKey() { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1155 | if (keys_.empty()) keys_ = adb_auth_get_private_keys(); |
| 1156 | |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1157 | std::shared_ptr<RSA> result = keys_[0]; |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1158 | keys_.pop_front(); |
| 1159 | return result; |
| 1160 | } |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1161 | #endif |