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