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