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