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 | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 36 | #include <set> |
Josh Gao | e1dacfc | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 37 | #include <thread> |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 39 | #include <android-base/logging.h> |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 40 | #include <android-base/parsenetaddress.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
| 42 | #include <android-base/strings.h> |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 43 | #include <android-base/thread_annotations.h> |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 44 | |
Josh Gao | 2776845 | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 45 | #include <diagnose_usb.h> |
| 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "adb.h" |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 48 | #include "adb_auth.h" |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 49 | #include "adb_io.h" |
Josh Gao | cfe72e2 | 2016-11-29 09:40:29 -0800 | [diff] [blame] | 50 | #include "adb_trace.h" |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 51 | #include "adb_utils.h" |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 52 | #include "fdevent.h" |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 53 | #include "sysdeps/chrono.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 55 | static void register_transport(atransport* transport); |
| 56 | static void remove_transport(atransport* transport); |
| 57 | static void transport_unref(atransport* transport); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 59 | // TODO: unordered_map<TransportId, atransport*> |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 60 | static auto& transport_list = *new std::list<atransport*>(); |
| 61 | static auto& pending_list = *new std::list<atransport*>(); |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 62 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 63 | static auto& transport_lock = *new std::recursive_mutex(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | |
Todd Kennedy | 51c05ec | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 65 | const char* const kFeatureShell2 = "shell_v2"; |
| 66 | const char* const kFeatureCmd = "cmd"; |
Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 67 | const char* const kFeatureStat2 = "stat_v2"; |
Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 68 | const char* const kFeatureLibusb = "libusb"; |
Dan Albert | 5176df8 | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 69 | const char* const kFeaturePushSync = "push_sync"; |
Todd Kennedy | 51c05ec | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 70 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 71 | namespace { |
| 72 | |
| 73 | // A class that helps the Clang Thread Safety Analysis deal with |
| 74 | // std::unique_lock. Given that std::unique_lock is movable, and the analysis |
| 75 | // can not currently perform alias analysis, it is not annotated. In order to |
| 76 | // assert that the mutex is held, a ScopedAssumeLocked can be created just after |
| 77 | // the std::unique_lock. |
| 78 | class SCOPED_CAPABILITY ScopedAssumeLocked { |
| 79 | public: |
| 80 | ScopedAssumeLocked(std::mutex& mutex) ACQUIRE(mutex) {} |
| 81 | ~ScopedAssumeLocked() RELEASE() {} |
| 82 | }; |
| 83 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 84 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 85 | // Tracks and handles atransport*s that are attempting reconnection. |
| 86 | class ReconnectHandler { |
| 87 | public: |
| 88 | ReconnectHandler() = default; |
| 89 | ~ReconnectHandler() = default; |
| 90 | |
| 91 | // Starts the ReconnectHandler thread. |
| 92 | void Start(); |
| 93 | |
| 94 | // Requests the ReconnectHandler thread to stop. |
| 95 | void Stop(); |
| 96 | |
| 97 | // Adds the atransport* to the queue of reconnect attempts. |
| 98 | void TrackTransport(atransport* transport); |
| 99 | |
| 100 | private: |
| 101 | // The main thread loop. |
| 102 | void Run(); |
| 103 | |
| 104 | // Tracks a reconnection attempt. |
| 105 | struct ReconnectAttempt { |
| 106 | atransport* transport; |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 107 | std::chrono::steady_clock::time_point reconnect_time; |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 108 | size_t attempts_left; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 109 | |
| 110 | bool operator<(const ReconnectAttempt& rhs) const { |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 111 | if (reconnect_time == rhs.reconnect_time) { |
| 112 | return reinterpret_cast<uintptr_t>(transport) < |
| 113 | reinterpret_cast<uintptr_t>(rhs.transport); |
| 114 | } |
| 115 | return reconnect_time < rhs.reconnect_time; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 116 | } |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Only retry for up to one minute. |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 120 | static constexpr const std::chrono::seconds kDefaultTimeout = 10s; |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 121 | static constexpr const size_t kMaxAttempts = 6; |
| 122 | |
| 123 | // Protects all members. |
| 124 | std::mutex reconnect_mutex_; |
| 125 | bool running_ GUARDED_BY(reconnect_mutex_) = true; |
| 126 | std::thread handler_thread_; |
| 127 | std::condition_variable reconnect_cv_; |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 128 | std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(ReconnectHandler); |
| 131 | }; |
| 132 | |
| 133 | void ReconnectHandler::Start() { |
| 134 | check_main_thread(); |
| 135 | handler_thread_ = std::thread(&ReconnectHandler::Run, this); |
| 136 | } |
| 137 | |
| 138 | void ReconnectHandler::Stop() { |
| 139 | check_main_thread(); |
| 140 | { |
| 141 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 142 | running_ = false; |
| 143 | } |
| 144 | reconnect_cv_.notify_one(); |
| 145 | handler_thread_.join(); |
| 146 | |
| 147 | // Drain the queue to free all resources. |
| 148 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 149 | while (!reconnect_queue_.empty()) { |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 150 | ReconnectAttempt attempt = *reconnect_queue_.begin(); |
| 151 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 152 | remove_transport(attempt.transport); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void ReconnectHandler::TrackTransport(atransport* transport) { |
| 157 | check_main_thread(); |
| 158 | { |
| 159 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 160 | if (!running_) return; |
Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 161 | // Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited. |
| 162 | auto reconnect_time = std::chrono::steady_clock::now() + 250ms; |
| 163 | reconnect_queue_.emplace( |
| 164 | ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts}); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 165 | } |
| 166 | reconnect_cv_.notify_one(); |
| 167 | } |
| 168 | |
| 169 | void ReconnectHandler::Run() { |
| 170 | while (true) { |
| 171 | ReconnectAttempt attempt; |
| 172 | { |
| 173 | std::unique_lock<std::mutex> lock(reconnect_mutex_); |
| 174 | ScopedAssumeLocked assume_lock(reconnect_mutex_); |
| 175 | |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 176 | if (!reconnect_queue_.empty()) { |
| 177 | // FIXME: libstdc++ (used on Windows) implements condition_variable with |
| 178 | // system_clock as its clock, so we're probably hosed if the clock changes, |
| 179 | // even if we use steady_clock throughout. This problem goes away once we |
| 180 | // switch to libc++. |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 181 | reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time); |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 182 | } else { |
| 183 | reconnect_cv_.wait(lock); |
| 184 | } |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 185 | |
| 186 | if (!running_) return; |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 187 | if (reconnect_queue_.empty()) continue; |
| 188 | |
| 189 | // Go back to sleep in case |reconnect_cv_| woke up spuriously and we still |
| 190 | // have more time to wait for the current attempt. |
| 191 | auto now = std::chrono::steady_clock::now(); |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 192 | if (reconnect_queue_.begin()->reconnect_time > now) { |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 193 | continue; |
| 194 | } |
| 195 | |
Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame^] | 196 | attempt = *reconnect_queue_.begin(); |
| 197 | reconnect_queue_.erase(reconnect_queue_.begin()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 198 | if (attempt.transport->kicked()) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 199 | D("transport %s was kicked. giving up on it.", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 200 | remove_transport(attempt.transport); |
| 201 | continue; |
| 202 | } |
| 203 | } |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 204 | D("attempting to reconnect %s", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 205 | |
| 206 | if (!attempt.transport->Reconnect()) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 207 | D("attempting to reconnect %s failed.", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 208 | if (attempt.attempts_left == 0) { |
| 209 | D("transport %s exceeded the number of retry attempts. giving up on it.", |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 210 | attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 211 | remove_transport(attempt.transport); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | std::lock_guard<std::mutex> lock(reconnect_mutex_); |
| 216 | reconnect_queue_.emplace(ReconnectAttempt{ |
Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 217 | attempt.transport, |
| 218 | std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout, |
| 219 | attempt.attempts_left - 1}); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 220 | continue; |
| 221 | } |
| 222 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 223 | D("reconnection to %s succeeded.", attempt.transport->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 224 | register_transport(attempt.transport); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static auto& reconnect_handler = *new ReconnectHandler(); |
| 229 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 230 | #endif |
| 231 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 232 | } // namespace |
| 233 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 234 | TransportId NextTransportId() { |
| 235 | static std::atomic<TransportId> next(1); |
| 236 | return next++; |
| 237 | } |
| 238 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 239 | BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection) |
| 240 | : underlying_(std::move(connection)) {} |
| 241 | |
| 242 | BlockingConnectionAdapter::~BlockingConnectionAdapter() { |
| 243 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing"; |
| 244 | Stop(); |
| 245 | } |
| 246 | |
| 247 | void BlockingConnectionAdapter::Start() { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 248 | std::lock_guard<std::mutex> lock(mutex_); |
| 249 | if (started_) { |
| 250 | LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 251 | << "): started multiple times"; |
| 252 | } |
| 253 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 254 | read_thread_ = std::thread([this]() { |
| 255 | LOG(INFO) << this->transport_name_ << ": read thread spawning"; |
| 256 | while (true) { |
Josh Gao | 31b5be6 | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 257 | auto packet = std::make_unique<apacket>(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 258 | if (!underlying_->Read(packet.get())) { |
| 259 | PLOG(INFO) << this->transport_name_ << ": read failed"; |
| 260 | break; |
| 261 | } |
| 262 | read_callback_(this, std::move(packet)); |
| 263 | } |
| 264 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); }); |
| 265 | }); |
| 266 | |
| 267 | write_thread_ = std::thread([this]() { |
| 268 | LOG(INFO) << this->transport_name_ << ": write thread spawning"; |
| 269 | while (true) { |
| 270 | std::unique_lock<std::mutex> lock(mutex_); |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 271 | ScopedAssumeLocked assume_locked(mutex_); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 272 | cv_.wait(lock, [this]() REQUIRES(mutex_) { |
| 273 | return this->stopped_ || !this->write_queue_.empty(); |
| 274 | }); |
| 275 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 276 | if (this->stopped_) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | std::unique_ptr<apacket> packet = std::move(this->write_queue_.front()); |
| 281 | this->write_queue_.pop_front(); |
| 282 | lock.unlock(); |
| 283 | |
| 284 | if (!this->underlying_->Write(packet.get())) { |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); }); |
| 289 | }); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 290 | |
| 291 | started_ = true; |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void BlockingConnectionAdapter::Stop() { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 295 | { |
| 296 | std::lock_guard<std::mutex> lock(mutex_); |
| 297 | if (!started_) { |
| 298 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; |
| 299 | return; |
| 300 | } |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 301 | |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 302 | if (stopped_) { |
| 303 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ |
| 304 | << "): already stopped"; |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | stopped_ = true; |
| 309 | } |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 310 | |
| 311 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping"; |
| 312 | |
| 313 | this->underlying_->Close(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 314 | this->cv_.notify_one(); |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 315 | |
| 316 | // Move the threads out into locals with the lock taken, and then unlock to let them exit. |
| 317 | std::thread read_thread; |
| 318 | std::thread write_thread; |
| 319 | |
| 320 | { |
| 321 | std::lock_guard<std::mutex> lock(mutex_); |
| 322 | read_thread = std::move(read_thread_); |
| 323 | write_thread = std::move(write_thread_); |
| 324 | } |
| 325 | |
| 326 | read_thread.join(); |
| 327 | write_thread.join(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 328 | |
| 329 | LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped"; |
| 330 | std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); }); |
| 331 | } |
| 332 | |
| 333 | bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) { |
| 334 | { |
Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 335 | std::lock_guard<std::mutex> lock(this->mutex_); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 336 | write_queue_.emplace_back(std::move(packet)); |
| 337 | } |
| 338 | |
| 339 | cv_.notify_one(); |
| 340 | return true; |
| 341 | } |
| 342 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 343 | bool FdConnection::Read(apacket* packet) { |
| 344 | if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) { |
| 345 | D("remote local: read terminated (message)"); |
| 346 | return false; |
| 347 | } |
| 348 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 349 | if (packet->msg.data_length > MAX_PAYLOAD) { |
Josh Gao | 5caaebd | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 350 | D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length); |
| 351 | return false; |
| 352 | } |
| 353 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 354 | packet->payload.resize(packet->msg.data_length); |
| 355 | |
| 356 | if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 357 | D("remote local: terminated (data)"); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | bool FdConnection::Write(apacket* packet) { |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 365 | if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) { |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 366 | D("remote local: write terminated"); |
| 367 | return false; |
| 368 | } |
| 369 | |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 370 | if (packet->msg.data_length) { |
| 371 | if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) { |
| 372 | D("remote local: write terminated"); |
| 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
| 380 | void FdConnection::Close() { |
| 381 | adb_shutdown(fd_.get()); |
| 382 | fd_.reset(); |
| 383 | } |
| 384 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 385 | 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] | 386 | unsigned command = p->msg.command; |
| 387 | int len = p->msg.data_length; |
| 388 | char cmd[9]; |
| 389 | char arg0[12], arg1[12]; |
| 390 | int n; |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 391 | |
| 392 | for (n = 0; n < 4; n++) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 393 | int b = (command >> (n * 8)) & 255; |
| 394 | if (b < 32 || b >= 127) break; |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 395 | cmd[n] = (char)b; |
| 396 | } |
| 397 | if (n == 4) { |
| 398 | cmd[4] = 0; |
| 399 | } else { |
| 400 | /* There is some non-ASCII name in the command, so dump |
| 401 | * the hexadecimal value instead */ |
| 402 | snprintf(cmd, sizeof cmd, "%08x", command); |
| 403 | } |
| 404 | |
| 405 | if (p->msg.arg0 < 256U) |
| 406 | snprintf(arg0, sizeof arg0, "%d", p->msg.arg0); |
| 407 | else |
| 408 | snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0); |
| 409 | |
| 410 | if (p->msg.arg1 < 256U) |
| 411 | snprintf(arg1, sizeof arg1, "%d", p->msg.arg1); |
| 412 | else |
| 413 | snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1); |
| 414 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 415 | std::string result = android::base::StringPrintf("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", name, |
| 416 | func, cmd, arg0, arg1, len); |
Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 417 | result += dump_hex(p->payload.data(), p->payload.size()); |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 418 | return result; |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 419 | } |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 420 | |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 421 | void send_packet(apacket* p, atransport* t) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | p->msg.magic = p->msg.command ^ 0xffffffff; |
Tim Murray | de47194 | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 423 | // compute a checksum for connection/auth packets for compatibility reasons |
| 424 | if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) { |
| 425 | p->msg.data_check = 0; |
| 426 | } else { |
| 427 | p->msg.data_check = calculate_apacket_checksum(p); |
| 428 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 430 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "to remote", p); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 432 | if (t == nullptr) { |
Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 433 | fatal("Transport is null"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 436 | if (t->Write(p) != 0) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 437 | D("%s: failed to enqueue packet, closing transport", t->serial.c_str()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 438 | t->Kick(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 442 | void kick_transport(atransport* t) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 443 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 444 | // As kick_transport() can be called from threads without guarantee that t is valid, |
| 445 | // check if the transport is in transport_list first. |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 446 | // |
| 447 | // TODO(jmgao): WTF? Is this actually true? |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 448 | 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] | 449 | t->Kick(); |
Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 450 | } |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 451 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | |
| 453 | static int transport_registration_send = -1; |
| 454 | static int transport_registration_recv = -1; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 455 | static fdevent* transport_registration_fde; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 456 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | #if ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | |
| 459 | /* this adds support required by the 'track-devices' service. |
| 460 | * this is used to send the content of "list_transport" to any |
| 461 | * number of client connections that want it through a single |
| 462 | * live TCP connection |
| 463 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | struct device_tracker { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 465 | asocket socket; |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 466 | bool update_needed = false; |
| 467 | bool long_output = false; |
| 468 | device_tracker* next = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | /* linked list of all device trackers */ |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 472 | static device_tracker* device_tracker_list; |
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 | static void device_tracker_remove(device_tracker* tracker) { |
| 475 | device_tracker** pnode = &device_tracker_list; |
| 476 | device_tracker* node = *pnode; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 478 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | while (node) { |
| 480 | if (node == tracker) { |
| 481 | *pnode = node->next; |
| 482 | break; |
| 483 | } |
| 484 | pnode = &node->next; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 485 | node = *pnode; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 486 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 489 | static void device_tracker_close(asocket* socket) { |
| 490 | device_tracker* tracker = (device_tracker*)socket; |
| 491 | asocket* peer = socket->peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 493 | D("device tracker %p removed", tracker); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | if (peer) { |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 495 | peer->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | peer->close(peer); |
| 497 | } |
| 498 | device_tracker_remove(tracker); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 499 | delete tracker; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 502 | static int device_tracker_enqueue(asocket* socket, apacket::payload_type) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | /* 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] | 504 | device_tracker_close(socket); |
| 505 | return -1; |
| 506 | } |
| 507 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 508 | static int device_tracker_send(device_tracker* tracker, const std::string& string) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 509 | asocket* peer = tracker->socket.peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | |
Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 511 | apacket::payload_type data; |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 512 | data.resize(4 + string.size()); |
| 513 | char buf[5]; |
| 514 | snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size())); |
| 515 | memcpy(&data[0], buf, 4); |
| 516 | memcpy(&data[4], string.data(), string.size()); |
| 517 | return peer->enqueue(peer, std::move(data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 520 | static void device_tracker_ready(asocket* socket) { |
| 521 | device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 523 | // We want to send the device list when the tracker connects |
| 524 | // for the first time, even if no update occurred. |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 525 | if (tracker->update_needed) { |
| 526 | tracker->update_needed = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 527 | |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 528 | std::string transports = list_transports(tracker->long_output); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 529 | device_tracker_send(tracker, transports); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 533 | asocket* create_device_tracker(bool long_output) { |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 534 | device_tracker* tracker = new device_tracker(); |
Elliott Hughes | dc3b459 | 2015-04-21 19:39:52 -0700 | [diff] [blame] | 535 | if (tracker == nullptr) fatal("cannot allocate device tracker"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 537 | D("device tracker %p created", tracker); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | |
| 539 | tracker->socket.enqueue = device_tracker_enqueue; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 540 | tracker->socket.ready = device_tracker_ready; |
| 541 | tracker->socket.close = device_tracker_close; |
Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 542 | tracker->update_needed = true; |
| 543 | tracker->long_output = long_output; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 545 | tracker->next = device_tracker_list; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 546 | device_tracker_list = tracker; |
| 547 | |
| 548 | return &tracker->socket; |
| 549 | } |
| 550 | |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 551 | // Check if all of the USB transports are connected. |
| 552 | bool iterate_transports(std::function<bool(const atransport*)> fn) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 553 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 554 | for (const auto& t : transport_list) { |
| 555 | if (!fn(t)) { |
| 556 | return false; |
| 557 | } |
| 558 | } |
| 559 | for (const auto& t : pending_list) { |
| 560 | if (!fn(t)) { |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 567 | // Call this function each time the transport list has changed. |
| 568 | void update_transports() { |
Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 569 | update_transport_status(); |
| 570 | |
| 571 | // Notify `adb track-devices` clients. |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 572 | std::string transports = list_transports(false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 574 | device_tracker* tracker = device_tracker_list; |
| 575 | while (tracker != nullptr) { |
| 576 | device_tracker* next = tracker->next; |
| 577 | // This may destroy the tracker if the connection is closed. |
| 578 | device_tracker_send(tracker, transports); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | tracker = next; |
| 580 | } |
| 581 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 582 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | #else |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 584 | |
| 585 | void update_transports() { |
| 586 | // Nothing to do on the device side. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 587 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 588 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 589 | #endif // ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 591 | struct tmsg { |
| 592 | atransport* transport; |
| 593 | int action; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 594 | }; |
| 595 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 596 | static int transport_read_action(int fd, struct tmsg* m) { |
| 597 | char* p = (char*)m; |
| 598 | int len = sizeof(*m); |
| 599 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 601 | while (len > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | r = adb_read(fd, p, len); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 603 | if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 604 | len -= r; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 605 | p += r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 606 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 607 | 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] | 608 | return -1; |
| 609 | } |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 614 | static int transport_write_action(int fd, struct tmsg* m) { |
| 615 | char* p = (char*)m; |
| 616 | int len = sizeof(*m); |
| 617 | int r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 618 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 619 | while (len > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 620 | r = adb_write(fd, p, len); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 621 | if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 622 | len -= r; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 623 | p += r; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 624 | } else { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 625 | 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] | 626 | return -1; |
| 627 | } |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 632 | 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] | 633 | tmsg m; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 634 | atransport* t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 635 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 636 | if (!(ev & FDE_READ)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 640 | if (transport_read_action(_fd, &m)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 641 | fatal_errno("cannot read transport registration socket"); |
| 642 | } |
| 643 | |
| 644 | t = m.transport; |
| 645 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 646 | if (m.action == 0) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 647 | D("transport: %s deleting", t->serial.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 648 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 649 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 650 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 651 | transport_list.remove(t); |
| 652 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 653 | |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 654 | delete t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | |
| 656 | update_transports(); |
| 657 | return; |
| 658 | } |
| 659 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 660 | /* don't create transport threads for inaccessible devices */ |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 661 | if (t->GetConnectionState() != kCsNoPerm) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 662 | // The connection gets a reference to the atransport. It will release it |
| 663 | // upon a read/write error. |
| 664 | t->ref_count++; |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 665 | t->connection()->SetTransportName(t->serial_name()); |
| 666 | t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 667 | if (!check_header(p.get(), t)) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 668 | D("%s: remote read: bad header", t->serial.c_str()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 669 | return false; |
| 670 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 671 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 672 | VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 673 | apacket* packet = p.release(); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 674 | |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 675 | // TODO: Does this need to run on the main thread? |
| 676 | fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); |
| 677 | return true; |
| 678 | }); |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 679 | t->connection()->SetErrorCallback([t](Connection*, const std::string& error) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 680 | D("%s: connection terminated: %s", t->serial.c_str(), error.c_str()); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 681 | fdevent_run_on_main_thread([t]() { |
| 682 | handle_offline(t); |
| 683 | transport_unref(t); |
| 684 | }); |
| 685 | }); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 686 | |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 687 | t->connection()->Start(); |
Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 688 | #if ADB_HOST |
| 689 | send_connect(t); |
| 690 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 693 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 694 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 695 | auto it = std::find(pending_list.begin(), pending_list.end(), t); |
| 696 | if (it != pending_list.end()) { |
| 697 | pending_list.remove(t); |
| 698 | transport_list.push_front(t); |
| 699 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 700 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 701 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 702 | update_transports(); |
| 703 | } |
| 704 | |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 705 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 706 | void init_reconnect_handler(void) { |
| 707 | reconnect_handler.Start(); |
| 708 | } |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 709 | #endif |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 710 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 711 | void init_transport_registration(void) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 712 | int s[2]; |
| 713 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 714 | if (adb_socketpair(s)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 715 | fatal_errno("cannot open transport registration socketpair"); |
| 716 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 717 | D("socketpair: (%d,%d)", s[0], s[1]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 718 | |
| 719 | transport_registration_send = s[0]; |
| 720 | transport_registration_recv = s[1]; |
| 721 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 722 | transport_registration_fde = |
Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 723 | fdevent_create(transport_registration_recv, transport_registration_func, nullptr); |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 724 | fdevent_set(transport_registration_fde, FDE_READ); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void kick_all_transports() { |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 728 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 729 | reconnect_handler.Stop(); |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 730 | #endif |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 731 | // 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] | 732 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 733 | for (auto t : transport_list) { |
| 734 | t->Kick(); |
| 735 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* the fdevent select pump is single threaded */ |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 739 | static void register_transport(atransport* transport) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 740 | tmsg m; |
| 741 | m.transport = transport; |
| 742 | m.action = 1; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 743 | D("transport: %s registered", transport->serial.c_str()); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 744 | if (transport_write_action(transport_registration_send, &m)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 745 | fatal_errno("cannot write transport registration socket\n"); |
| 746 | } |
| 747 | } |
| 748 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 749 | static void remove_transport(atransport* transport) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 750 | tmsg m; |
| 751 | m.transport = transport; |
| 752 | m.action = 0; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 753 | D("transport: %s removed", transport->serial.c_str()); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 754 | if (transport_write_action(transport_registration_send, &m)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 755 | fatal_errno("cannot write transport registration socket\n"); |
| 756 | } |
| 757 | } |
| 758 | |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 759 | static void transport_unref(atransport* t) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 760 | check_main_thread(); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 761 | CHECK(t != nullptr); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 762 | |
Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 763 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
| 764 | CHECK_GT(t->ref_count, 0u); |
| 765 | t->ref_count--; |
| 766 | if (t->ref_count == 0) { |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 767 | t->connection()->Stop(); |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 768 | #if ADB_HOST |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 769 | if (t->IsTcpDevice() && !t->kicked()) { |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 770 | D("transport: %s unref (attempting reconnection)", t->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 771 | reconnect_handler.TrackTransport(t); |
| 772 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 773 | D("transport: %s unref (kicking and closing)", t->serial.c_str()); |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 774 | remove_transport(t); |
| 775 | } |
Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 776 | #else |
| 777 | D("transport: %s unref (kicking and closing)", t->serial.c_str()); |
| 778 | remove_transport(t); |
| 779 | #endif |
| 780 | |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 781 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 782 | D("transport: %s unref (count=%zu)", t->serial.c_str(), t->ref_count); |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 783 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 786 | static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual, |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 787 | bool sanitize_qual) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 788 | if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */ |
| 789 | return qual.empty(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 790 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 791 | if (qual.empty()) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 792 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 793 | const char* ptr = to_test.c_str(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 794 | if (prefix) { |
| 795 | while (*prefix) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 796 | if (*prefix++ != *ptr++) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 800 | for (char ch : qual) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 801 | if (sanitize_qual && !isalnum(ch)) ch = '_'; |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 802 | if (ch != *ptr++) return 0; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 805 | /* Everything matched so far. Return true if *ptr is a NUL. */ |
| 806 | return !*ptr; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 807 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 808 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 809 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 810 | bool* is_ambiguous, std::string* error_out, |
| 811 | bool accept_any_state) { |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 812 | atransport* result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 813 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 814 | if (transport_id != 0) { |
| 815 | *error_out = |
| 816 | android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); |
| 817 | } else if (serial) { |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 818 | *error_out = android::base::StringPrintf("device '%s' not found", serial); |
| 819 | } else if (type == kTransportLocal) { |
| 820 | *error_out = "no emulators found"; |
| 821 | } else if (type == kTransportAny) { |
| 822 | *error_out = "no devices/emulators found"; |
| 823 | } else { |
| 824 | *error_out = "no devices found"; |
| 825 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 826 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 827 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 828 | for (const auto& t : transport_list) { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 829 | if (t->GetConnectionState() == kCsNoPerm) { |
David Pursell | d2acbd1 | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 830 | *error_out = UsbNoPermissionsLongHelpText(); |
Mike Lockwood | 37d3111 | 2009-08-08 13:53:16 -0400 | [diff] [blame] | 831 | continue; |
| 832 | } |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 833 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 834 | if (transport_id) { |
| 835 | if (t->id == transport_id) { |
| 836 | result = t; |
| 837 | break; |
| 838 | } |
| 839 | } else if (serial) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 840 | if (t->MatchesTarget(serial)) { |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 841 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 842 | *error_out = "more than one device"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 843 | if (is_ambiguous) *is_ambiguous = true; |
| 844 | result = nullptr; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 845 | break; |
| 846 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 847 | result = t; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 848 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 849 | } else { |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 850 | if (type == kTransportUsb && t->type == kTransportUsb) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 851 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 852 | *error_out = "more than one device"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 853 | if (is_ambiguous) *is_ambiguous = true; |
| 854 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 855 | break; |
| 856 | } |
| 857 | result = t; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 858 | } else if (type == kTransportLocal && t->type == kTransportLocal) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 859 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 860 | *error_out = "more than one emulator"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 861 | if (is_ambiguous) *is_ambiguous = true; |
| 862 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 863 | break; |
| 864 | } |
| 865 | result = t; |
Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 866 | } else if (type == kTransportAny) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 867 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 868 | *error_out = "more than one device/emulator"; |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 869 | if (is_ambiguous) *is_ambiguous = true; |
| 870 | result = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | result = t; |
| 874 | } |
| 875 | } |
| 876 | } |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 877 | lock.unlock(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 878 | |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 879 | if (result && !accept_any_state) { |
| 880 | // The caller requires an active transport. |
| 881 | // Make sure that we're actually connected. |
| 882 | ConnectionState state = result->GetConnectionState(); |
| 883 | switch (state) { |
| 884 | case kCsConnecting: |
| 885 | *error_out = "device still connecting"; |
| 886 | result = nullptr; |
| 887 | break; |
Benoit Goby | 77e8e58 | 2013-01-15 12:36:47 -0800 | [diff] [blame] | 888 | |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 889 | case kCsAuthorizing: |
| 890 | *error_out = "device still authorizing"; |
| 891 | result = nullptr; |
| 892 | break; |
| 893 | |
| 894 | case kCsUnauthorized: { |
| 895 | *error_out = "device unauthorized.\n"; |
| 896 | char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); |
| 897 | *error_out += "This adb server's $ADB_VENDOR_KEYS is "; |
| 898 | *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; |
| 899 | *error_out += "\n"; |
| 900 | *error_out += "Try 'adb kill-server' if that seems wrong.\n"; |
| 901 | *error_out += "Otherwise check for a confirmation dialog on your device."; |
| 902 | result = nullptr; |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | case kCsOffline: |
| 907 | *error_out = "device offline"; |
| 908 | result = nullptr; |
| 909 | break; |
| 910 | |
| 911 | default: |
| 912 | break; |
| 913 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | if (result) { |
Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 917 | *error_out = "success"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | return result; |
| 921 | } |
| 922 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 923 | bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) { |
| 924 | std::unique_lock<std::mutex> lock(mutex_); |
| 925 | ScopedAssumeLocked assume_locked(mutex_); |
| 926 | return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) { |
| 927 | return connection_established_ready_; |
| 928 | }) && connection_established_; |
| 929 | } |
| 930 | |
| 931 | void ConnectionWaitable::SetConnectionEstablished(bool success) { |
| 932 | { |
| 933 | std::lock_guard<std::mutex> lock(mutex_); |
| 934 | if (connection_established_ready_) return; |
| 935 | connection_established_ready_ = true; |
| 936 | connection_established_ = success; |
| 937 | D("connection established with %d", success); |
| 938 | } |
| 939 | cv_.notify_one(); |
| 940 | } |
| 941 | |
| 942 | atransport::~atransport() { |
| 943 | // If the connection callback had not been run before, run it now. |
| 944 | SetConnectionEstablished(false); |
| 945 | } |
| 946 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 947 | int atransport::Write(apacket* p) { |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 948 | return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 951 | void atransport::Kick() { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 952 | if (!kicked_.exchange(true)) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 953 | D("kicking transport %p %s", this, this->serial.c_str()); |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 954 | this->connection()->Stop(); |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 958 | ConnectionState atransport::GetConnectionState() const { |
| 959 | return connection_state_; |
| 960 | } |
| 961 | |
| 962 | void atransport::SetConnectionState(ConnectionState state) { |
| 963 | check_main_thread(); |
| 964 | connection_state_ = state; |
| 965 | } |
| 966 | |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 967 | void atransport::SetConnection(std::unique_ptr<Connection> connection) { |
| 968 | std::lock_guard<std::mutex> lock(mutex_); |
| 969 | connection_ = std::shared_ptr<Connection>(std::move(connection)); |
| 970 | } |
| 971 | |
Josh Gao | ffbd336 | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 972 | std::string atransport::connection_state_name() const { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 973 | ConnectionState state = GetConnectionState(); |
| 974 | switch (state) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 975 | case kCsOffline: |
| 976 | return "offline"; |
| 977 | case kCsBootloader: |
| 978 | return "bootloader"; |
| 979 | case kCsDevice: |
| 980 | return "device"; |
| 981 | case kCsHost: |
| 982 | return "host"; |
| 983 | case kCsRecovery: |
| 984 | return "recovery"; |
| 985 | case kCsNoPerm: |
| 986 | return UsbNoPermissionsShortHelpText(); |
| 987 | case kCsSideload: |
| 988 | return "sideload"; |
| 989 | case kCsUnauthorized: |
| 990 | return "unauthorized"; |
Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 991 | case kCsAuthorizing: |
| 992 | return "authorizing"; |
| 993 | case kCsConnecting: |
| 994 | return "connecting"; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 995 | default: |
| 996 | return "unknown"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 1000 | void atransport::update_version(int version, size_t payload) { |
| 1001 | protocol_version = std::min(version, A_VERSION); |
| 1002 | max_payload = std::min(payload, MAX_PAYLOAD); |
| 1003 | } |
| 1004 | |
| 1005 | int atransport::get_protocol_version() const { |
| 1006 | return protocol_version; |
| 1007 | } |
| 1008 | |
| 1009 | size_t atransport::get_max_payload() const { |
| 1010 | return max_payload; |
| 1011 | } |
| 1012 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1013 | namespace { |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 1014 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1015 | constexpr char kFeatureStringDelimiter = ','; |
| 1016 | |
| 1017 | } // namespace |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1018 | |
| 1019 | const FeatureSet& supported_features() { |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1020 | // Local static allocation to avoid global non-POD variables. |
| 1021 | static const FeatureSet* features = new FeatureSet{ |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1022 | kFeatureShell2, kFeatureCmd, kFeatureStat2, |
David Pursell | bbe3d21 | 2015-09-25 08:37:13 -0700 | [diff] [blame] | 1023 | // Increment ADB_SERVER_VERSION whenever the feature list changes to |
| 1024 | // make sure that the adb client and server features stay in sync |
| 1025 | // (http://b/24370690). |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1026 | }; |
| 1027 | |
| 1028 | return *features; |
| 1029 | } |
| 1030 | |
| 1031 | std::string FeatureSetToString(const FeatureSet& features) { |
| 1032 | return android::base::Join(features, kFeatureStringDelimiter); |
| 1033 | } |
| 1034 | |
| 1035 | FeatureSet StringToFeatureSet(const std::string& features_string) { |
David Pursell | d2b588e | 2015-09-25 13:04:21 -0700 | [diff] [blame] | 1036 | if (features_string.empty()) { |
| 1037 | return FeatureSet(); |
| 1038 | } |
| 1039 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1040 | auto names = android::base::Split(features_string, {kFeatureStringDelimiter}); |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1041 | return FeatureSet(names.begin(), names.end()); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1044 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1045 | return feature_set.count(feature) > 0 && supported_features().count(feature) > 0; |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1048 | bool atransport::has_feature(const std::string& feature) const { |
| 1049 | return features_.count(feature) > 0; |
| 1050 | } |
| 1051 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1052 | void atransport::SetFeatures(const std::string& features_string) { |
| 1053 | features_ = StringToFeatureSet(features_string); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1056 | void atransport::AddDisconnect(adisconnect* disconnect) { |
| 1057 | disconnects_.push_back(disconnect); |
| 1058 | } |
| 1059 | |
| 1060 | void atransport::RemoveDisconnect(adisconnect* disconnect) { |
| 1061 | disconnects_.remove(disconnect); |
| 1062 | } |
| 1063 | |
| 1064 | void atransport::RunDisconnects() { |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1065 | for (const auto& disconnect : disconnects_) { |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 1066 | disconnect->func(disconnect->opaque, this); |
| 1067 | } |
| 1068 | disconnects_.clear(); |
| 1069 | } |
| 1070 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1071 | bool atransport::MatchesTarget(const std::string& target) const { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1072 | if (!serial.empty()) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1073 | if (target == serial) { |
| 1074 | return true; |
| 1075 | } else if (type == kTransportLocal) { |
| 1076 | // Local transports can match [tcp:|udp:]<hostname>[:port]. |
| 1077 | const char* local_target_ptr = target.c_str(); |
| 1078 | |
| 1079 | // For fastboot compatibility, ignore protocol prefixes. |
| 1080 | if (android::base::StartsWith(target, "tcp:") || |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1081 | android::base::StartsWith(target, "udp:")) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1082 | local_target_ptr += 4; |
| 1083 | } |
| 1084 | |
| 1085 | // Parse our |serial| and the given |target| to check if the hostnames and ports match. |
| 1086 | std::string serial_host, error; |
| 1087 | int serial_port = -1; |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1088 | if (android::base::ParseNetAddress(serial, &serial_host, &serial_port, nullptr, &error)) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1089 | // |target| may omit the port to default to ours. |
| 1090 | std::string target_host; |
| 1091 | int target_port = serial_port; |
| 1092 | if (android::base::ParseNetAddress(local_target_ptr, &target_host, &target_port, |
| 1093 | nullptr, &error) && |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1094 | serial_host == target_host && serial_port == target_port) { |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1095 | return true; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1101 | return (target == devpath) || qual_match(target, "product:", product, false) || |
| 1102 | qual_match(target, "model:", model, true) || |
| 1103 | qual_match(target, "device:", device, false); |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1106 | void atransport::SetConnectionEstablished(bool success) { |
| 1107 | connection_waitable_->SetConnectionEstablished(success); |
| 1108 | } |
| 1109 | |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1110 | bool atransport::Reconnect() { |
| 1111 | return reconnect_(this); |
| 1112 | } |
| 1113 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1114 | #if ADB_HOST |
| 1115 | |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1116 | // We use newline as our delimiter, make sure to never output it. |
| 1117 | static std::string sanitize(std::string str, bool alphanumeric) { |
| 1118 | auto pred = alphanumeric ? [](const char c) { return !isalnum(c); } |
| 1119 | : [](const char c) { return c == '\n'; }; |
| 1120 | std::replace_if(str.begin(), str.end(), pred, '_'); |
| 1121 | return str; |
| 1122 | } |
| 1123 | |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1124 | static void append_transport_info(std::string* result, const char* key, const std::string& value, |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1125 | bool alphanumeric) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1126 | if (value.empty()) { |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1127 | return; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1130 | *result += ' '; |
| 1131 | *result += key; |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1132 | *result += sanitize(value, alphanumeric); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1135 | static void append_transport(const atransport* t, std::string* result, bool long_listing) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1136 | std::string serial = t->serial; |
| 1137 | if (serial.empty()) { |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 1138 | serial = "(no serial number)"; |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1139 | } |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1140 | |
| 1141 | if (!long_listing) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1142 | *result += serial; |
| 1143 | *result += '\t'; |
| 1144 | *result += t->connection_state_name(); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1145 | } else { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1146 | android::base::StringAppendF(result, "%-22s %s", serial.c_str(), |
| 1147 | t->connection_state_name().c_str()); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1148 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1149 | append_transport_info(result, "", t->devpath, false); |
| 1150 | append_transport_info(result, "product:", t->product, false); |
| 1151 | append_transport_info(result, "model:", t->model, true); |
| 1152 | append_transport_info(result, "device:", t->device, false); |
Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 1153 | |
| 1154 | // Put id at the end, so that anyone parsing the output here can always find it by scanning |
| 1155 | // backwards from newlines, even with hypothetical devices named 'transport_id:1'. |
| 1156 | *result += " transport_id:"; |
| 1157 | *result += std::to_string(t->id); |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1158 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1159 | *result += '\n'; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1162 | std::string list_transports(bool long_listing) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1163 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Artem Iglikov | 04398a9 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1164 | |
| 1165 | auto sorted_transport_list = transport_list; |
| 1166 | sorted_transport_list.sort([](atransport*& x, atransport*& y) { |
| 1167 | if (x->type != y->type) { |
| 1168 | return x->type < y->type; |
| 1169 | } |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1170 | return x->serial < y->serial; |
Artem Iglikov | 04398a9 | 2017-12-17 10:56:07 +0000 | [diff] [blame] | 1171 | }); |
| 1172 | |
| 1173 | std::string result; |
| 1174 | for (const auto& t : sorted_transport_list) { |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1175 | append_transport(t, &result, long_listing); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1176 | } |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1177 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1180 | void close_usb_devices(std::function<bool(const atransport*)> predicate) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1181 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1182 | for (auto& t : transport_list) { |
| 1183 | if (predicate(t)) { |
| 1184 | t->Kick(); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1189 | /* hack for osx */ |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1190 | void close_usb_devices() { |
Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 1191 | close_usb_devices([](const atransport*) { return true; }); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1192 | } |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1193 | #endif // ADB_HOST |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1194 | |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1195 | bool register_socket_transport(unique_fd s, std::string serial, int port, int local, |
| 1196 | atransport::ReconnectCallback reconnect, int* error) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1197 | atransport* t = new atransport(std::move(reconnect), kCsOffline); |
David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 1198 | |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1199 | D("transport: %s init'ing for socket %d, on port %d", serial.c_str(), s.get(), port); |
Josh Gao | 56300c9 | 2018-07-25 17:21:49 -0700 | [diff] [blame] | 1200 | if (init_socket_transport(t, std::move(s), port, local) < 0) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1201 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1202 | if (error) *error = errno; |
| 1203 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1204 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1205 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1206 | std::unique_lock<std::recursive_mutex> lock(transport_lock); |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1207 | for (const auto& transport : pending_list) { |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1208 | if (serial == transport->serial) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1209 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1210 | << " is already in pending_list and fails to register"; |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1211 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1212 | if (error) *error = EALREADY; |
| 1213 | return false; |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1214 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1215 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1216 | |
Elliott Hughes | 65fe251 | 2015-10-07 15:59:35 -0700 | [diff] [blame] | 1217 | for (const auto& transport : transport_list) { |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1218 | if (serial == transport->serial) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1219 | VLOG(TRANSPORT) << "socket transport " << transport->serial |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1220 | << " is already in transport_list and fails to register"; |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1221 | delete t; |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1222 | if (error) *error = EALREADY; |
| 1223 | return false; |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Josh Gao | 3b4de3c | 2018-08-02 13:58:24 -0700 | [diff] [blame] | 1227 | t->serial = std::move(serial); |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1228 | pending_list.push_front(t); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1229 | |
| 1230 | lock.unlock(); |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1231 | |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1232 | auto waitable = t->connection_waitable(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1233 | register_transport(t); |
Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 1234 | |
Luis Hector Chavez | c587f02 | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1235 | if (local == 1) { |
| 1236 | // Do not wait for emulator transports. |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1237 | return true; |
Luis Hector Chavez | c587f02 | 2018-05-01 17:12:16 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Josh Gao | 362e696 | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 1240 | if (!waitable->WaitForConnection(std::chrono::seconds(10))) { |
| 1241 | if (error) *error = ETIMEDOUT; |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
| 1245 | if (t->GetConnectionState() == kCsUnauthorized) { |
| 1246 | if (error) *error = EPERM; |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
| 1250 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1253 | #if ADB_HOST |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1254 | atransport* find_transport(const char* serial) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1255 | atransport* result = nullptr; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1256 | |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1257 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1258 | for (auto& t : transport_list) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1259 | if (strcmp(serial, t->serial.c_str()) == 0) { |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1260 | result = t; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1261 | break; |
| 1262 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1263 | } |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1264 | |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1265 | return result; |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1266 | } |
| 1267 | |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1268 | void kick_all_tcp_devices() { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1269 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 1270 | for (auto& t : transport_list) { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 1271 | if (t->IsTcpDevice()) { |
Yabin Cui | d6ab3c2 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 1272 | // Kicking breaks the read_transport thread of this transport out of any read, then |
| 1273 | // the read_transport thread will notify the main thread to make this transport |
| 1274 | // 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] | 1275 | // Finally, this transport will be closed and freed in the main thread. |
Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 1276 | t->Kick(); |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1277 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1278 | } |
Mike Lockwood | cbbe79a | 2010-05-24 10:44:35 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 1281 | #endif |
| 1282 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1283 | void register_usb_transport(usb_handle* usb, const char* serial, const char* devpath, |
| 1284 | unsigned writeable) { |
Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 1285 | atransport* t = new atransport(writeable ? kCsOffline : kCsNoPerm); |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1286 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1287 | 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] | 1288 | init_usb_transport(t, usb); |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1289 | if (serial) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1290 | t->serial = serial; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1291 | } |
Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (devpath) { |
Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 1294 | t->devpath = devpath; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 1295 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1296 | |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1297 | { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1298 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 1299 | pending_list.push_front(t); |
| 1300 | } |
Benoit Goby | 1c45ee9 | 2013-03-29 18:22:36 -0700 | [diff] [blame] | 1301 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1302 | register_transport(t); |
| 1303 | } |
| 1304 | |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 1305 | // This should only be used for transports with connection_state == kCsNoPerm. |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1306 | void unregister_usb_transport(usb_handle* usb) { |
Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 1307 | std::lock_guard<std::recursive_mutex> lock(transport_lock); |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1308 | transport_list.remove_if([usb](atransport* t) { |
Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 1309 | auto connection = t->connection(); |
| 1310 | if (auto usb_connection = dynamic_cast<UsbConnection*>(connection.get())) { |
| 1311 | return usb_connection->handle_ == usb && t->GetConnectionState() == kCsNoPerm; |
Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 1312 | } |
| 1313 | return false; |
| 1314 | }); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 1315 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1316 | |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1317 | bool check_header(apacket* p, atransport* t) { |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1318 | if (p->msg.magic != (p->msg.command ^ 0xffffffff)) { |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1319 | VLOG(RWX) << "check_header(): invalid magic command = " << std::hex << p->msg.command |
| 1320 | << ", magic = " << p->msg.magic; |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1321 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1324 | if (p->msg.data_length > t->get_max_payload()) { |
| 1325 | VLOG(RWX) << "check_header(): " << p->msg.data_length |
| 1326 | << " atransport::max_payload = " << t->get_max_payload(); |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1327 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 1330 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1333 | #if ADB_HOST |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1334 | std::shared_ptr<RSA> atransport::NextKey() { |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1335 | if (keys_.empty()) keys_ = adb_auth_get_private_keys(); |
| 1336 | |
Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 1337 | std::shared_ptr<RSA> result = keys_[0]; |
Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 1338 | keys_.pop_front(); |
| 1339 | return result; |
| 1340 | } |
Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 1341 | #endif |