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