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