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