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; |
| 520 | t->connection->SetTransportName(t->serial_name()); |
| 521 | t->connection->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { |
| 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 | }); |
| 534 | t->connection->SetErrorCallback([t](Connection*, const std::string& error) { |
| 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 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 542 | t->connection->Start(); |
| 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); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [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 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 711 | // 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] | 712 | if (result && result->GetConnectionState() == kCsUnauthorized && !accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 713 | *error_out = "device unauthorized.\n"; |
| 714 | char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); |
| 715 | *error_out += "This adb server's $ADB_VENDOR_KEYS is "; |
| 716 | *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; |
| 717 | *error_out += "\n"; |
| 718 | *error_out += "Try 'adb kill-server' if that seems wrong.\n"; |
| 719 | *error_out += "Otherwise check for a confirmation dialog on your device."; |
| 720 | result = nullptr; |
| 721 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 722 | |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 723 | // 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] | 724 | if (result && result->GetConnectionState() == kCsOffline && !accept_any_state) { |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 725 | *error_out = "device offline"; |
| 726 | result = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | if (result) { |
Elliott Hughes | 24f5276 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 730 | *error_out = "success"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return result; |
| 734 | } |
| 735 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 736 | bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) { |
| 737 | std::unique_lock<std::mutex> lock(mutex_); |
| 738 | ScopedAssumeLocked assume_locked(mutex_); |
| 739 | return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) { |
| 740 | return connection_established_ready_; |
| 741 | }) && connection_established_; |
| 742 | } |
| 743 | |
| 744 | void ConnectionWaitable::SetConnectionEstablished(bool success) { |
| 745 | { |
| 746 | std::lock_guard<std::mutex> lock(mutex_); |
| 747 | if (connection_established_ready_) return; |
| 748 | connection_established_ready_ = true; |
| 749 | connection_established_ = success; |
| 750 | D("connection established with %d", success); |
| 751 | } |
| 752 | cv_.notify_one(); |
| 753 | } |
| 754 | |
| 755 | atransport::~atransport() { |
| 756 | // If the connection callback had not been run before, run it now. |
| 757 | SetConnectionEstablished(false); |
| 758 | } |
| 759 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 760 | int atransport::Write(apacket* p) { |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 761 | return this->connection->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 764 | void atransport::Kick() { |
| 765 | if (!kicked_) { |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 766 | D("kicking transport %s", this->serial); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 767 | kicked_ = true; |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 768 | this->connection->Stop(); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 772 | ConnectionState atransport::GetConnectionState() const { |
| 773 | return connection_state_; |
| 774 | } |
| 775 | |
| 776 | void atransport::SetConnectionState(ConnectionState state) { |
| 777 | check_main_thread(); |
| 778 | connection_state_ = state; |
| 779 | } |
| 780 | |
Josh Gao | 3cd03be | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 781 | std::string atransport::connection_state_name() const { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 782 | ConnectionState state = GetConnectionState(); |
| 783 | switch (state) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 784 | case kCsOffline: |
| 785 | return "offline"; |
| 786 | case kCsBootloader: |
| 787 | return "bootloader"; |
| 788 | case kCsDevice: |
| 789 | return "device"; |
| 790 | case kCsHost: |
| 791 | return "host"; |
| 792 | case kCsRecovery: |
| 793 | return "recovery"; |
| 794 | case kCsNoPerm: |
| 795 | return UsbNoPermissionsShortHelpText(); |
| 796 | case kCsSideload: |
| 797 | return "sideload"; |
| 798 | case kCsUnauthorized: |
| 799 | return "unauthorized"; |
| 800 | default: |
| 801 | return "unknown"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 805 | void atransport::update_version(int version, size_t payload) { |
| 806 | protocol_version = std::min(version, A_VERSION); |
| 807 | max_payload = std::min(payload, MAX_PAYLOAD); |
| 808 | } |
| 809 | |
| 810 | int atransport::get_protocol_version() const { |
| 811 | return protocol_version; |
| 812 | } |
| 813 | |
| 814 | size_t atransport::get_max_payload() const { |
| 815 | return max_payload; |
| 816 | } |
| 817 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 818 | namespace { |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 819 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 820 | constexpr char kFeatureStringDelimiter = ','; |
| 821 | |
| 822 | } // namespace |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 823 | |
| 824 | const FeatureSet& supported_features() { |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 825 | // Local static allocation to avoid global non-POD variables. |
| 826 | static const FeatureSet* features = new FeatureSet{ |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 827 | kFeatureShell2, kFeatureCmd, kFeatureStat2, |
David Pursell | 6d5fad3 | 2015-09-25 08:37:13 -0700 | [diff] [blame] | 828 | // Increment ADB_SERVER_VERSION whenever the feature list changes to |
| 829 | // make sure that the adb client and server features stay in sync |
| 830 | // (http://b/24370690). |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 831 | }; |
| 832 | |
| 833 | return *features; |
| 834 | } |
| 835 | |
| 836 | std::string FeatureSetToString(const FeatureSet& features) { |
| 837 | return android::base::Join(features, kFeatureStringDelimiter); |
| 838 | } |
| 839 | |
| 840 | FeatureSet StringToFeatureSet(const std::string& features_string) { |
David Pursell | 3d9072b | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 841 | if (features_string.empty()) { |
| 842 | return FeatureSet(); |
| 843 | } |
| 844 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 845 | auto names = android::base::Split(features_string, {kFeatureStringDelimiter}); |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 846 | return FeatureSet(names.begin(), names.end()); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 847 | } |
| 848 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 849 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 850 | return feature_set.count(feature) > 0 && supported_features().count(feature) > 0; |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 853 | bool atransport::has_feature(const std::string& feature) const { |
| 854 | return features_.count(feature) > 0; |
| 855 | } |
| 856 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 857 | void atransport::SetFeatures(const std::string& features_string) { |
| 858 | features_ = StringToFeatureSet(features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 861 | void atransport::AddDisconnect(adisconnect* disconnect) { |
| 862 | disconnects_.push_back(disconnect); |
| 863 | } |
| 864 | |
| 865 | void atransport::RemoveDisconnect(adisconnect* disconnect) { |
| 866 | disconnects_.remove(disconnect); |
| 867 | } |
| 868 | |
| 869 | void atransport::RunDisconnects() { |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 870 | for (const auto& disconnect : disconnects_) { |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 871 | disconnect->func(disconnect->opaque, this); |
| 872 | } |
| 873 | disconnects_.clear(); |
| 874 | } |
| 875 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 876 | bool atransport::MatchesTarget(const std::string& target) const { |
| 877 | if (serial) { |
| 878 | if (target == serial) { |
| 879 | return true; |
| 880 | } else if (type == kTransportLocal) { |
| 881 | // Local transports can match [tcp:|udp:]<hostname>[:port]. |
| 882 | const char* local_target_ptr = target.c_str(); |
| 883 | |
| 884 | // For fastboot compatibility, ignore protocol prefixes. |
| 885 | if (android::base::StartsWith(target, "tcp:") || |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 886 | android::base::StartsWith(target, "udp:")) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 887 | local_target_ptr += 4; |
| 888 | } |
| 889 | |
| 890 | // Parse our |serial| and the given |target| to check if the hostnames and ports match. |
| 891 | std::string serial_host, error; |
| 892 | int serial_port = -1; |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 893 | if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 894 | // |target| may omit the port to default to ours. |
| 895 | std::string target_host; |
| 896 | int target_port = serial_port; |
| 897 | if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port, |
| 898 | nullptr, &error) && |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 899 | serial_host == target_host && serial_port == target_port) { |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 900 | return true; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | return (devpath && target == devpath) || |
| 907 | qual_match(target.c_str(), "product:", product, false) || |
| 908 | qual_match(target.c_str(), "model:", model, true) || |
| 909 | qual_match(target.c_str(), "device:", device, false); |
| 910 | } |
| 911 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 912 | void atransport::SetConnectionEstablished(bool success) { |
| 913 | connection_waitable_->SetConnectionEstablished(success); |
| 914 | } |
| 915 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 916 | #if ADB_HOST |
| 917 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 918 | // We use newline as our delimiter, make sure to never output it. |
| 919 | static std::string sanitize(std::string str, bool alphanumeric) { |
| 920 | auto pred = alphanumeric ? [](const char c) { return !isalnum(c); } |
| 921 | : [](const char c) { return c == '\n'; }; |
| 922 | std::replace_if(str.begin(), str.end(), pred, '_'); |
| 923 | return str; |
| 924 | } |
| 925 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 926 | 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] | 927 | bool alphanumeric) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 928 | if (value == nullptr || *value == '\0') { |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 929 | return; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 932 | *result += ' '; |
| 933 | *result += key; |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 934 | *result += sanitize(value, alphanumeric); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 937 | 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] | 938 | const char* serial = t->serial; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 939 | if (!serial || !serial[0]) { |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 940 | serial = "(no serial number)"; |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 941 | } |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 942 | |
| 943 | if (!long_listing) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 944 | *result += serial; |
| 945 | *result += '\t'; |
| 946 | *result += t->connection_state_name(); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 947 | } else { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 948 | 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] | 949 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 950 | append_transport_info(result, "", t->devpath, false); |
| 951 | append_transport_info(result, "product:", t->product, false); |
| 952 | append_transport_info(result, "model:", t->model, true); |
| 953 | append_transport_info(result, "device:", t->device, false); |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 954 | |
| 955 | // Put id at the end, so that anyone parsing the output here can always find it by scanning |
| 956 | // backwards from newlines, even with hypothetical devices named 'transport_id:1'. |
| 957 | *result += " transport_id:"; |
| 958 | *result += std::to_string(t->id); |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 959 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 960 | *result += '\n'; |
Scott Anderson | 2704238 | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 963 | std::string list_transports(bool long_listing) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 964 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Artem Iglikov | 4dd3501 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 965 | |
| 966 | auto sorted_transport_list = transport_list; |
| 967 | sorted_transport_list.sort([](atransport*& x, atransport*& y) { |
| 968 | if (x->type != y->type) { |
| 969 | return x->type < y->type; |
| 970 | } |
| 971 | return strcmp(x->serial, y->serial) < 0; |
| 972 | }); |
| 973 | |
| 974 | std::string result; |
| 975 | for (const auto& t : sorted_transport_list) { |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 976 | append_transport(t, &result, long_listing); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 977 | } |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 978 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 981 | void close_usb_devices(std::function<bool(const atransport*)> predicate) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 982 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 983 | for (auto& t : transport_list) { |
| 984 | if (predicate(t)) { |
| 985 | t->Kick(); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 990 | /* hack for osx */ |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 991 | void close_usb_devices() { |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 992 | close_usb_devices([](const atransport*) { return true; }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 993 | } |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 994 | #endif // ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 995 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 996 | 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] | 997 | atransport* t = new atransport(); |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 998 | |
| 999 | if (!serial) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1000 | char buf[32]; |
| 1001 | snprintf(buf, sizeof(buf), "T-%p", t); |
| 1002 | serial = buf; |
David 'Digit' Turner | 58f5968 | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1003 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1004 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 1005 | 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] | 1006 | if (init_socket_transport(t, s, port, local) < 0) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1007 | delete t; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1008 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1009 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1010 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1011 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1012 | for (const auto& transport : pending_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1013 | if (transport->serial && strcmp(serial, transport->serial) == 0) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1014 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1015 | << " is already in pending_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1016 | delete t; |
Luis Hector Chavez | 7771920 | 2018-04-17 14:09:21 -0700 | [diff] [blame] | 1017 | return -EALREADY; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1018 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1019 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1020 | |
Elliott Hughes | 8595283 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1021 | for (const auto& transport : transport_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1022 | if (transport->serial && strcmp(serial, transport->serial) == 0) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1023 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1024 | << " is already in transport_list and fails to register"; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1025 | delete t; |
Luis Hector Chavez | 7771920 | 2018-04-17 14:09:21 -0700 | [diff] [blame] | 1026 | return -EALREADY; |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1030 | pending_list.push_front(t); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1031 | t->serial = strdup(serial); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1032 | |
| 1033 | lock.unlock(); |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1034 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 1035 | auto waitable = t->connection_waitable(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1036 | register_transport(t); |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame^] | 1037 | |
| 1038 | return waitable->WaitForConnection(std::chrono::seconds(10)) ? 0 : -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1041 | #if ADB_HOST |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1042 | atransport* find_transport(const char* serial) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1043 | atransport* result = nullptr; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1044 | |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1045 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1046 | for (auto& t : transport_list) { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1047 | if (t->serial && strcmp(serial, t->serial) == 0) { |
| 1048 | result = t; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1049 | break; |
| 1050 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1051 | } |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1052 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1053 | return result; |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1056 | void kick_all_tcp_devices() { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1057 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1058 | for (auto& t : transport_list) { |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1059 | if (t->IsTcpDevice()) { |
Yabin Cui | 4e22229 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 1060 | // Kicking breaks the read_transport thread of this transport out of any read, then |
| 1061 | // the read_transport thread will notify the main thread to make this transport |
| 1062 | // 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] | 1063 | // Finally, this transport will be closed and freed in the main thread. |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1064 | t->Kick(); |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1065 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1066 | } |
Mike Lockwood | 01c2c30 | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1069 | #endif |
| 1070 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1071 | void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath, |
| 1072 | unsigned writeable) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1073 | atransport* t = new atransport((writeable ? kCsOffline : kCsNoPerm)); |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1074 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1075 | 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] | 1076 | init_usb_transport(t, usb); |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1077 | if (serial) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1078 | t->serial = strdup(serial); |
| 1079 | } |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1080 | |
| 1081 | if (devpath) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1082 | t->devpath = strdup(devpath); |
| 1083 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1084 | |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1085 | { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1086 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | e7daf57 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1087 | pending_list.push_front(t); |
| 1088 | } |
Benoit Goby | 3f9f9ce | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1089 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1090 | register_transport(t); |
| 1091 | } |
| 1092 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 1093 | // This should only be used for transports with connection_state == kCsNoPerm. |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1094 | void unregister_usb_transport(usb_handle* usb) { |
Josh Gao | aaa82f7 | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1095 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1096 | transport_list.remove_if([usb](atransport* t) { |
| 1097 | if (auto connection = dynamic_cast<UsbConnection*>(t->connection.get())) { |
| 1098 | return connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm; |
| 1099 | } |
| 1100 | return false; |
| 1101 | }); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 1102 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1103 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1104 | bool check_header(apacket* p, atransport* t) { |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1105 | if (p->msg.magic != (p->msg.command ^ 0xffffffff)) { |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1106 | VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command |
| 1107 | << ", magic = " << p->msg.magic; |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1108 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1109 | } |
| 1110 | |
Josh Gao | 0d6aa99 | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1111 | if (p->msg.data_length > t->get_max_payload()) { |
| 1112 | VLOG(RWX) << "check_header(): " << p->msg.data_length |
| 1113 | << " atransport::max_payload = " << t->get_max_payload(); |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1114 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1117 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1120 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1121 | std::shared_ptr<RSA> atransport::NextKey() { |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1122 | if (keys_.empty()) keys_ = adb_auth_get_private_keys(); |
| 1123 | |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1124 | std::shared_ptr<RSA> result = keys_[0]; |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1125 | keys_.pop_front(); |
| 1126 | return result; |
| 1127 | } |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1128 | #endif |