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