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