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