| 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" | 
| Josh Gao | 57e09b1 | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 52 | #include "fdevent/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"; | 
| Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 76 | const char* const kFeatureRemountShell = "remount_shell"; | 
| Todd Kennedy | 51c05ec | 2015-11-10 00:03:25 +0000 | [diff] [blame] | 77 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 78 | namespace { | 
 | 79 |  | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 80 | #if ADB_HOST | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 81 | // Tracks and handles atransport*s that are attempting reconnection. | 
 | 82 | class ReconnectHandler { | 
 | 83 |   public: | 
 | 84 |     ReconnectHandler() = default; | 
 | 85 |     ~ReconnectHandler() = default; | 
 | 86 |  | 
 | 87 |     // Starts the ReconnectHandler thread. | 
 | 88 |     void Start(); | 
 | 89 |  | 
 | 90 |     // Requests the ReconnectHandler thread to stop. | 
 | 91 |     void Stop(); | 
 | 92 |  | 
 | 93 |     // Adds the atransport* to the queue of reconnect attempts. | 
 | 94 |     void TrackTransport(atransport* transport); | 
 | 95 |  | 
| Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 96 |     // Wake up the ReconnectHandler thread to have it check for kicked transports. | 
 | 97 |     void CheckForKicked(); | 
 | 98 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 99 |   private: | 
 | 100 |     // The main thread loop. | 
 | 101 |     void Run(); | 
 | 102 |  | 
 | 103 |     // Tracks a reconnection attempt. | 
 | 104 |     struct ReconnectAttempt { | 
 | 105 |         atransport* transport; | 
| Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 106 |         std::chrono::steady_clock::time_point reconnect_time; | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 107 |         size_t attempts_left; | 
| Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 108 |  | 
 | 109 |         bool operator<(const ReconnectAttempt& rhs) const { | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 110 |             if (reconnect_time == rhs.reconnect_time) { | 
 | 111 |                 return reinterpret_cast<uintptr_t>(transport) < | 
 | 112 |                        reinterpret_cast<uintptr_t>(rhs.transport); | 
 | 113 |             } | 
 | 114 |             return reconnect_time < rhs.reconnect_time; | 
| Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 115 |         } | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 116 |     }; | 
 | 117 |  | 
 | 118 |     // Only retry for up to one minute. | 
| Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 119 |     static constexpr const std::chrono::seconds kDefaultTimeout = 10s; | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 120 |     static constexpr const size_t kMaxAttempts = 6; | 
 | 121 |  | 
 | 122 |     // Protects all members. | 
 | 123 |     std::mutex reconnect_mutex_; | 
 | 124 |     bool running_ GUARDED_BY(reconnect_mutex_) = true; | 
 | 125 |     std::thread handler_thread_; | 
 | 126 |     std::condition_variable reconnect_cv_; | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 127 |     std::set<ReconnectAttempt> reconnect_queue_ GUARDED_BY(reconnect_mutex_); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 128 |  | 
 | 129 |     DISALLOW_COPY_AND_ASSIGN(ReconnectHandler); | 
 | 130 | }; | 
 | 131 |  | 
 | 132 | void ReconnectHandler::Start() { | 
 | 133 |     check_main_thread(); | 
 | 134 |     handler_thread_ = std::thread(&ReconnectHandler::Run, this); | 
 | 135 | } | 
 | 136 |  | 
 | 137 | void ReconnectHandler::Stop() { | 
 | 138 |     check_main_thread(); | 
 | 139 |     { | 
 | 140 |         std::lock_guard<std::mutex> lock(reconnect_mutex_); | 
 | 141 |         running_ = false; | 
 | 142 |     } | 
 | 143 |     reconnect_cv_.notify_one(); | 
 | 144 |     handler_thread_.join(); | 
 | 145 |  | 
 | 146 |     // Drain the queue to free all resources. | 
 | 147 |     std::lock_guard<std::mutex> lock(reconnect_mutex_); | 
 | 148 |     while (!reconnect_queue_.empty()) { | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 149 |         ReconnectAttempt attempt = *reconnect_queue_.begin(); | 
 | 150 |         reconnect_queue_.erase(reconnect_queue_.begin()); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 151 |         remove_transport(attempt.transport); | 
 | 152 |     } | 
 | 153 | } | 
 | 154 |  | 
 | 155 | void ReconnectHandler::TrackTransport(atransport* transport) { | 
 | 156 |     check_main_thread(); | 
 | 157 |     { | 
 | 158 |         std::lock_guard<std::mutex> lock(reconnect_mutex_); | 
 | 159 |         if (!running_) return; | 
| Josh Gao | e445a6d | 2018-07-31 14:12:59 -0700 | [diff] [blame] | 160 |         // Arbitrary sleep to give adbd time to get ready, if we disconnected because it exited. | 
 | 161 |         auto reconnect_time = std::chrono::steady_clock::now() + 250ms; | 
 | 162 |         reconnect_queue_.emplace( | 
 | 163 |                 ReconnectAttempt{transport, reconnect_time, ReconnectHandler::kMaxAttempts}); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 164 |     } | 
 | 165 |     reconnect_cv_.notify_one(); | 
 | 166 | } | 
 | 167 |  | 
| Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 168 | void ReconnectHandler::CheckForKicked() { | 
 | 169 |     reconnect_cv_.notify_one(); | 
 | 170 | } | 
 | 171 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 172 | void ReconnectHandler::Run() { | 
 | 173 |     while (true) { | 
 | 174 |         ReconnectAttempt attempt; | 
 | 175 |         { | 
 | 176 |             std::unique_lock<std::mutex> lock(reconnect_mutex_); | 
| Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 177 |             ScopedLockAssertion assume_lock(reconnect_mutex_); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 178 |  | 
| Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 179 |             if (!reconnect_queue_.empty()) { | 
 | 180 |                 // FIXME: libstdc++ (used on Windows) implements condition_variable with | 
 | 181 |                 //        system_clock as its clock, so we're probably hosed if the clock changes, | 
 | 182 |                 //        even if we use steady_clock throughout. This problem goes away once we | 
 | 183 |                 //        switch to libc++. | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 184 |                 reconnect_cv_.wait_until(lock, reconnect_queue_.begin()->reconnect_time); | 
| Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 185 |             } else { | 
 | 186 |                 reconnect_cv_.wait(lock); | 
 | 187 |             } | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 188 |  | 
 | 189 |             if (!running_) return; | 
| Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 190 |  | 
 | 191 |             // Scan the whole list for kicked transports, so that we immediately handle an explicit | 
 | 192 |             // disconnect request. | 
 | 193 |             bool kicked = false; | 
 | 194 |             for (auto it = reconnect_queue_.begin(); it != reconnect_queue_.end();) { | 
 | 195 |                 if (it->transport->kicked()) { | 
 | 196 |                     D("transport %s was kicked. giving up on it.", it->transport->serial.c_str()); | 
 | 197 |                     remove_transport(it->transport); | 
 | 198 |                     it = reconnect_queue_.erase(it); | 
 | 199 |                 } else { | 
 | 200 |                     ++it; | 
 | 201 |                 } | 
 | 202 |                 kicked = true; | 
 | 203 |             } | 
 | 204 |  | 
| Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 205 |             if (reconnect_queue_.empty()) continue; | 
 | 206 |  | 
| Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 207 |             // Go back to sleep if we either woke up spuriously, or we were woken up to remove | 
 | 208 |             // 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] | 209 |             auto now = std::chrono::steady_clock::now(); | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 210 |             if (reconnect_queue_.begin()->reconnect_time > now) { | 
| Josh Gao | 95af641 | 2018-07-30 18:51:55 -0700 | [diff] [blame] | 211 |                 continue; | 
 | 212 |             } | 
 | 213 |  | 
| Josh Gao | 8a40c8a | 2018-08-10 14:28:24 -0700 | [diff] [blame] | 214 |             attempt = *reconnect_queue_.begin(); | 
 | 215 |             reconnect_queue_.erase(reconnect_queue_.begin()); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 216 |         } | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 217 |         D("attempting to reconnect %s", attempt.transport->serial.c_str()); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 218 |  | 
| Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 219 |         switch (attempt.transport->Reconnect()) { | 
 | 220 |             case ReconnectResult::Retry: { | 
 | 221 |                 D("attempting to reconnect %s failed.", attempt.transport->serial.c_str()); | 
 | 222 |                 if (attempt.attempts_left == 0) { | 
 | 223 |                     D("transport %s exceeded the number of retry attempts. giving up on it.", | 
 | 224 |                       attempt.transport->serial.c_str()); | 
 | 225 |                     remove_transport(attempt.transport); | 
 | 226 |                     continue; | 
 | 227 |                 } | 
 | 228 |  | 
 | 229 |                 std::lock_guard<std::mutex> lock(reconnect_mutex_); | 
 | 230 |                 reconnect_queue_.emplace(ReconnectAttempt{ | 
 | 231 |                         attempt.transport, | 
 | 232 |                         std::chrono::steady_clock::now() + ReconnectHandler::kDefaultTimeout, | 
 | 233 |                         attempt.attempts_left - 1}); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 234 |                 continue; | 
 | 235 |             } | 
 | 236 |  | 
| Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 237 |             case ReconnectResult::Success: | 
 | 238 |                 D("reconnection to %s succeeded.", attempt.transport->serial.c_str()); | 
 | 239 |                 register_transport(attempt.transport); | 
 | 240 |                 continue; | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 241 |  | 
| Josh Gao | fc2e56f | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 242 |             case ReconnectResult::Abort: | 
 | 243 |                 D("cancelling reconnection attempt to %s.", attempt.transport->serial.c_str()); | 
 | 244 |                 remove_transport(attempt.transport); | 
 | 245 |                 continue; | 
 | 246 |         } | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 247 |     } | 
 | 248 | } | 
 | 249 |  | 
 | 250 | static auto& reconnect_handler = *new ReconnectHandler(); | 
 | 251 |  | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 252 | #endif | 
 | 253 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 254 | }  // namespace | 
 | 255 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 256 | TransportId NextTransportId() { | 
 | 257 |     static std::atomic<TransportId> next(1); | 
 | 258 |     return next++; | 
 | 259 | } | 
 | 260 |  | 
| Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 261 | void Connection::Reset() { | 
 | 262 |     LOG(INFO) << "Connection::Reset(): stopping"; | 
 | 263 |     Stop(); | 
 | 264 | } | 
 | 265 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 266 | BlockingConnectionAdapter::BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection) | 
 | 267 |     : underlying_(std::move(connection)) {} | 
 | 268 |  | 
 | 269 | BlockingConnectionAdapter::~BlockingConnectionAdapter() { | 
 | 270 |     LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): destructing"; | 
 | 271 |     Stop(); | 
 | 272 | } | 
 | 273 |  | 
 | 274 | void BlockingConnectionAdapter::Start() { | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 275 |     std::lock_guard<std::mutex> lock(mutex_); | 
 | 276 |     if (started_) { | 
 | 277 |         LOG(FATAL) << "BlockingConnectionAdapter(" << this->transport_name_ | 
 | 278 |                    << "): started multiple times"; | 
 | 279 |     } | 
 | 280 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 281 |     read_thread_ = std::thread([this]() { | 
 | 282 |         LOG(INFO) << this->transport_name_ << ": read thread spawning"; | 
 | 283 |         while (true) { | 
| Josh Gao | 31b5be6 | 2018-03-07 16:51:08 -0800 | [diff] [blame] | 284 |             auto packet = std::make_unique<apacket>(); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 285 |             if (!underlying_->Read(packet.get())) { | 
 | 286 |                 PLOG(INFO) << this->transport_name_ << ": read failed"; | 
 | 287 |                 break; | 
 | 288 |             } | 
 | 289 |             read_callback_(this, std::move(packet)); | 
 | 290 |         } | 
 | 291 |         std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "read failed"); }); | 
 | 292 |     }); | 
 | 293 |  | 
 | 294 |     write_thread_ = std::thread([this]() { | 
 | 295 |         LOG(INFO) << this->transport_name_ << ": write thread spawning"; | 
 | 296 |         while (true) { | 
 | 297 |             std::unique_lock<std::mutex> lock(mutex_); | 
| Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 298 |             ScopedLockAssertion assume_locked(mutex_); | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 299 |             cv_.wait(lock, [this]() REQUIRES(mutex_) { | 
 | 300 |                 return this->stopped_ || !this->write_queue_.empty(); | 
 | 301 |             }); | 
 | 302 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 303 |             if (this->stopped_) { | 
 | 304 |                 return; | 
 | 305 |             } | 
 | 306 |  | 
 | 307 |             std::unique_ptr<apacket> packet = std::move(this->write_queue_.front()); | 
 | 308 |             this->write_queue_.pop_front(); | 
 | 309 |             lock.unlock(); | 
 | 310 |  | 
 | 311 |             if (!this->underlying_->Write(packet.get())) { | 
 | 312 |                 break; | 
 | 313 |             } | 
 | 314 |         } | 
 | 315 |         std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "write failed"); }); | 
 | 316 |     }); | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 317 |  | 
 | 318 |     started_ = true; | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 319 | } | 
 | 320 |  | 
| Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 321 | void BlockingConnectionAdapter::Reset() { | 
 | 322 |     { | 
 | 323 |         std::lock_guard<std::mutex> lock(mutex_); | 
 | 324 |         if (!started_) { | 
 | 325 |             LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; | 
 | 326 |             return; | 
 | 327 |         } | 
 | 328 |  | 
 | 329 |         if (stopped_) { | 
 | 330 |             LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ | 
 | 331 |                       << "): already stopped"; | 
 | 332 |             return; | 
 | 333 |         } | 
 | 334 |     } | 
 | 335 |  | 
 | 336 |     LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): resetting"; | 
 | 337 |     this->underlying_->Reset(); | 
 | 338 |     Stop(); | 
 | 339 | } | 
 | 340 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 341 | void BlockingConnectionAdapter::Stop() { | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 342 |     { | 
 | 343 |         std::lock_guard<std::mutex> lock(mutex_); | 
 | 344 |         if (!started_) { | 
 | 345 |             LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): not started"; | 
 | 346 |             return; | 
 | 347 |         } | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 348 |  | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 349 |         if (stopped_) { | 
 | 350 |             LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ | 
 | 351 |                       << "): already stopped"; | 
 | 352 |             return; | 
 | 353 |         } | 
 | 354 |  | 
 | 355 |         stopped_ = true; | 
 | 356 |     } | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 357 |  | 
 | 358 |     LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopping"; | 
 | 359 |  | 
 | 360 |     this->underlying_->Close(); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 361 |     this->cv_.notify_one(); | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 362 |  | 
 | 363 |     // Move the threads out into locals with the lock taken, and then unlock to let them exit. | 
 | 364 |     std::thread read_thread; | 
 | 365 |     std::thread write_thread; | 
 | 366 |  | 
 | 367 |     { | 
 | 368 |         std::lock_guard<std::mutex> lock(mutex_); | 
 | 369 |         read_thread = std::move(read_thread_); | 
 | 370 |         write_thread = std::move(write_thread_); | 
 | 371 |     } | 
 | 372 |  | 
 | 373 |     read_thread.join(); | 
 | 374 |     write_thread.join(); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 375 |  | 
 | 376 |     LOG(INFO) << "BlockingConnectionAdapter(" << this->transport_name_ << "): stopped"; | 
 | 377 |     std::call_once(this->error_flag_, [this]() { this->error_callback_(this, "requested stop"); }); | 
 | 378 | } | 
 | 379 |  | 
 | 380 | bool BlockingConnectionAdapter::Write(std::unique_ptr<apacket> packet) { | 
 | 381 |     { | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 382 |         std::lock_guard<std::mutex> lock(this->mutex_); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 383 |         write_queue_.emplace_back(std::move(packet)); | 
 | 384 |     } | 
 | 385 |  | 
 | 386 |     cv_.notify_one(); | 
 | 387 |     return true; | 
 | 388 | } | 
 | 389 |  | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 390 | bool FdConnection::Read(apacket* packet) { | 
 | 391 |     if (!ReadFdExactly(fd_.get(), &packet->msg, sizeof(amessage))) { | 
 | 392 |         D("remote local: read terminated (message)"); | 
 | 393 |         return false; | 
 | 394 |     } | 
 | 395 |  | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 396 |     if (packet->msg.data_length > MAX_PAYLOAD) { | 
| Josh Gao | 5caaebd | 2018-02-02 14:38:04 -0800 | [diff] [blame] | 397 |         D("remote local: read overflow (data length = %" PRIu32 ")", packet->msg.data_length); | 
 | 398 |         return false; | 
 | 399 |     } | 
 | 400 |  | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 401 |     packet->payload.resize(packet->msg.data_length); | 
 | 402 |  | 
 | 403 |     if (!ReadFdExactly(fd_.get(), &packet->payload[0], packet->payload.size())) { | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 404 |         D("remote local: terminated (data)"); | 
 | 405 |         return false; | 
 | 406 |     } | 
 | 407 |  | 
 | 408 |     return true; | 
 | 409 | } | 
 | 410 |  | 
 | 411 | bool FdConnection::Write(apacket* packet) { | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 412 |     if (!WriteFdExactly(fd_.get(), &packet->msg, sizeof(packet->msg))) { | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 413 |         D("remote local: write terminated"); | 
 | 414 |         return false; | 
 | 415 |     } | 
 | 416 |  | 
| Josh Gao | f571fcb | 2018-02-05 18:49:10 -0800 | [diff] [blame] | 417 |     if (packet->msg.data_length) { | 
 | 418 |         if (!WriteFdExactly(fd_.get(), &packet->payload[0], packet->msg.data_length)) { | 
 | 419 |             D("remote local: write terminated"); | 
 | 420 |             return false; | 
 | 421 |         } | 
 | 422 |     } | 
 | 423 |  | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 424 |     return true; | 
 | 425 | } | 
 | 426 |  | 
 | 427 | void FdConnection::Close() { | 
 | 428 |     adb_shutdown(fd_.get()); | 
 | 429 |     fd_.reset(); | 
 | 430 | } | 
 | 431 |  | 
| Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 432 | void send_packet(apacket* p, atransport* t) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 433 |     p->msg.magic = p->msg.command ^ 0xffffffff; | 
| Tim Murray | de47194 | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 434 |     // compute a checksum for connection/auth packets for compatibility reasons | 
 | 435 |     if (t->get_protocol_version() >= A_VERSION_SKIP_CHECKSUM) { | 
 | 436 |         p->msg.data_check = 0; | 
 | 437 |     } else { | 
 | 438 |         p->msg.data_check = calculate_apacket_checksum(p); | 
 | 439 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 441 |     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] | 442 |  | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 443 |     if (t == nullptr) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 444 |         LOG(FATAL) << "Transport is null"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 |     } | 
 | 446 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 447 |     if (t->Write(p) != 0) { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 448 |         D("%s: failed to enqueue packet, closing transport", t->serial.c_str()); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 449 |         t->Kick(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 |     } | 
 | 451 | } | 
 | 452 |  | 
| Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 453 | void kick_transport(atransport* t, bool reset) { | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 454 |     std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 455 |     // As kick_transport() can be called from threads without guarantee that t is valid, | 
 | 456 |     // check if the transport is in transport_list first. | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 457 |     // | 
 | 458 |     // TODO(jmgao): WTF? Is this actually true? | 
| Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 459 |     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] | 460 |         if (reset) { | 
 | 461 |             t->Reset(); | 
 | 462 |         } else { | 
 | 463 |             t->Kick(); | 
 | 464 |         } | 
| Yabin Cui | 1f4ec19 | 2016-04-05 13:50:44 -0700 | [diff] [blame] | 465 |     } | 
| Josh Gao | 902dace | 2018-08-10 14:44:54 -0700 | [diff] [blame] | 466 |  | 
 | 467 | #if ADB_HOST | 
 | 468 |     reconnect_handler.CheckForKicked(); | 
 | 469 | #endif | 
| Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 470 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 471 |  | 
 | 472 | static int transport_registration_send = -1; | 
 | 473 | static int transport_registration_recv = -1; | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 474 | static fdevent* transport_registration_fde; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 475 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | #if ADB_HOST | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 |  | 
 | 478 | /* this adds support required by the 'track-devices' service. | 
 | 479 |  * this is used to send the content of "list_transport" to any | 
 | 480 |  * number of client connections that want it through a single | 
 | 481 |  * live TCP connection | 
 | 482 |  */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | struct device_tracker { | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 484 |     asocket socket; | 
| Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 485 |     bool update_needed = false; | 
 | 486 |     bool long_output = false; | 
 | 487 |     device_tracker* next = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | }; | 
 | 489 |  | 
 | 490 | /* linked list of all device trackers */ | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 491 | static device_tracker* device_tracker_list; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 493 | static void device_tracker_remove(device_tracker* tracker) { | 
 | 494 |     device_tracker** pnode = &device_tracker_list; | 
 | 495 |     device_tracker* node = *pnode; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 |  | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 497 |     std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 498 |     while (node) { | 
 | 499 |         if (node == tracker) { | 
 | 500 |             *pnode = node->next; | 
 | 501 |             break; | 
 | 502 |         } | 
 | 503 |         pnode = &node->next; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 504 |         node = *pnode; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 506 | } | 
 | 507 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 508 | static void device_tracker_close(asocket* socket) { | 
 | 509 |     device_tracker* tracker = (device_tracker*)socket; | 
 | 510 |     asocket* peer = socket->peer; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 511 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 512 |     D("device tracker %p removed", tracker); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 513 |     if (peer) { | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 514 |         peer->peer = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 |         peer->close(peer); | 
 | 516 |     } | 
 | 517 |     device_tracker_remove(tracker); | 
| Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 518 |     delete tracker; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | } | 
 | 520 |  | 
| Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 521 | 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] | 522 |     /* 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] | 523 |     device_tracker_close(socket); | 
 | 524 |     return -1; | 
 | 525 | } | 
 | 526 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 527 | static int device_tracker_send(device_tracker* tracker, const std::string& string) { | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 528 |     asocket* peer = tracker->socket.peer; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 529 |  | 
| Josh Gao | 1ce9957 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 530 |     apacket::payload_type data; | 
| Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 531 |     data.resize(4 + string.size()); | 
 | 532 |     char buf[5]; | 
 | 533 |     snprintf(buf, sizeof(buf), "%04x", static_cast<int>(string.size())); | 
 | 534 |     memcpy(&data[0], buf, 4); | 
 | 535 |     memcpy(&data[4], string.data(), string.size()); | 
 | 536 |     return peer->enqueue(peer, std::move(data)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } | 
 | 538 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 539 | static void device_tracker_ready(asocket* socket) { | 
 | 540 |     device_tracker* tracker = reinterpret_cast<device_tracker*>(socket); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 542 |     // We want to send the device list when the tracker connects | 
 | 543 |     // for the first time, even if no update occurred. | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 544 |     if (tracker->update_needed) { | 
 | 545 |         tracker->update_needed = false; | 
| Elliott Hughes | fc2f5f6 | 2019-07-31 14:13:57 -0700 | [diff] [blame] | 546 |         device_tracker_send(tracker, list_transports(tracker->long_output)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 547 |     } | 
 | 548 | } | 
 | 549 |  | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 550 | asocket* create_device_tracker(bool long_output) { | 
| Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 551 |     device_tracker* tracker = new device_tracker(); | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 552 |     if (tracker == nullptr) LOG(FATAL) << "cannot allocate device tracker"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 554 |     D("device tracker %p created", tracker); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 |  | 
 | 556 |     tracker->socket.enqueue = device_tracker_enqueue; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 557 |     tracker->socket.ready = device_tracker_ready; | 
 | 558 |     tracker->socket.close = device_tracker_close; | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 559 |     tracker->update_needed = true; | 
 | 560 |     tracker->long_output = long_output; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 562 |     tracker->next = device_tracker_list; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 563 |     device_tracker_list = tracker; | 
 | 564 |  | 
 | 565 |     return &tracker->socket; | 
 | 566 | } | 
 | 567 |  | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 568 | // Check if all of the USB transports are connected. | 
 | 569 | bool iterate_transports(std::function<bool(const atransport*)> fn) { | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 570 |     std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 571 |     for (const auto& t : transport_list) { | 
 | 572 |         if (!fn(t)) { | 
 | 573 |             return false; | 
 | 574 |         } | 
 | 575 |     } | 
 | 576 |     for (const auto& t : pending_list) { | 
 | 577 |         if (!fn(t)) { | 
 | 578 |             return false; | 
 | 579 |         } | 
 | 580 |     } | 
 | 581 |     return true; | 
 | 582 | } | 
 | 583 |  | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 584 | // Call this function each time the transport list has changed. | 
 | 585 | void update_transports() { | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 586 |     update_transport_status(); | 
 | 587 |  | 
 | 588 |     // Notify `adb track-devices` clients. | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 589 |     device_tracker* tracker = device_tracker_list; | 
 | 590 |     while (tracker != nullptr) { | 
 | 591 |         device_tracker* next = tracker->next; | 
 | 592 |         // This may destroy the tracker if the connection is closed. | 
| Elliott Hughes | fc2f5f6 | 2019-07-31 14:13:57 -0700 | [diff] [blame] | 593 |         device_tracker_send(tracker, list_transports(tracker->long_output)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 594 |         tracker = next; | 
 | 595 |     } | 
 | 596 | } | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 597 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | #else | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 599 |  | 
 | 600 | void update_transports() { | 
 | 601 |     // Nothing to do on the device side. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | } | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 603 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 604 | #endif  // ADB_HOST | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 605 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 606 | struct tmsg { | 
 | 607 |     atransport* transport; | 
 | 608 |     int action; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | }; | 
 | 610 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 611 | static int transport_read_action(int fd, struct tmsg* m) { | 
 | 612 |     char* p = (char*)m; | 
 | 613 |     int len = sizeof(*m); | 
 | 614 |     int r; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 615 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 616 |     while (len > 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 617 |         r = adb_read(fd, p, len); | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 618 |         if (r > 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 619 |             len -= r; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 620 |             p += r; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 621 |         } else { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 622 |             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] | 623 |             return -1; | 
 | 624 |         } | 
 | 625 |     } | 
 | 626 |     return 0; | 
 | 627 | } | 
 | 628 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 629 | static int transport_write_action(int fd, struct tmsg* m) { | 
 | 630 |     char* p = (char*)m; | 
 | 631 |     int len = sizeof(*m); | 
 | 632 |     int r; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 634 |     while (len > 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 635 |         r = adb_write(fd, p, len); | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 636 |         if (r > 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 |             len -= r; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 638 |             p += r; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 639 |         } else { | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 640 |             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] | 641 |             return -1; | 
 | 642 |         } | 
 | 643 |     } | 
 | 644 |     return 0; | 
 | 645 | } | 
 | 646 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 647 | 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] | 648 |     tmsg m; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 649 |     atransport* t; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 650 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 651 |     if (!(ev & FDE_READ)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 |         return; | 
 | 653 |     } | 
 | 654 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 655 |     if (transport_read_action(_fd, &m)) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 656 |         PLOG(FATAL) << "cannot read transport registration socket"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 657 |     } | 
 | 658 |  | 
 | 659 |     t = m.transport; | 
 | 660 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 661 |     if (m.action == 0) { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 662 |         D("transport: %s deleting", t->serial.c_str()); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 663 |  | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 664 |         { | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 665 |             std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 666 |             transport_list.remove(t); | 
 | 667 |         } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 |  | 
| Dan Albert | c7915a3 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 669 |         delete t; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 670 |  | 
 | 671 |         update_transports(); | 
 | 672 |         return; | 
 | 673 |     } | 
 | 674 |  | 
| Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 675 |     /* don't create transport threads for inaccessible devices */ | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 676 |     if (t->GetConnectionState() != kCsNoPerm) { | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 677 |         // The connection gets a reference to the atransport. It will release it | 
 | 678 |         // upon a read/write error. | 
 | 679 |         t->ref_count++; | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 680 |         t->connection()->SetTransportName(t->serial_name()); | 
 | 681 |         t->connection()->SetReadCallback([t](Connection*, std::unique_ptr<apacket> p) { | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 682 |             if (!check_header(p.get(), t)) { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 683 |                 D("%s: remote read: bad header", t->serial.c_str()); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 684 |                 return false; | 
 | 685 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 686 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 687 |             VLOG(TRANSPORT) << dump_packet(t->serial.c_str(), "from remote", p.get()); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 688 |             apacket* packet = p.release(); | 
| Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 689 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 690 |             // TODO: Does this need to run on the main thread? | 
 | 691 |             fdevent_run_on_main_thread([packet, t]() { handle_packet(packet, t); }); | 
 | 692 |             return true; | 
 | 693 |         }); | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 694 |         t->connection()->SetErrorCallback([t](Connection*, const std::string& error) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 695 |             LOG(INFO) << t->serial_name() << ": connection terminated: " << error; | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 696 |             fdevent_run_on_main_thread([t]() { | 
 | 697 |                 handle_offline(t); | 
 | 698 |                 transport_unref(t); | 
 | 699 |             }); | 
 | 700 |         }); | 
| Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 701 |  | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 702 |         t->connection()->Start(); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 703 | #if ADB_HOST | 
 | 704 |         send_connect(t); | 
 | 705 | #endif | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 706 |     } | 
 | 707 |  | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 708 |     { | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 709 |         std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 710 |         auto it = std::find(pending_list.begin(), pending_list.end(), t); | 
 | 711 |         if (it != pending_list.end()) { | 
 | 712 |             pending_list.remove(t); | 
 | 713 |             transport_list.push_front(t); | 
 | 714 |         } | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 715 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 716 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 717 |     update_transports(); | 
 | 718 | } | 
 | 719 |  | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 720 | #if ADB_HOST | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 721 | void init_reconnect_handler(void) { | 
 | 722 |     reconnect_handler.Start(); | 
 | 723 | } | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 724 | #endif | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 725 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 726 | void init_transport_registration(void) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 727 |     int s[2]; | 
 | 728 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 729 |     if (adb_socketpair(s)) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 730 |         PLOG(FATAL) << "cannot open transport registration socketpair"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 731 |     } | 
| Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 732 |     D("socketpair: (%d,%d)", s[0], s[1]); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 733 |  | 
 | 734 |     transport_registration_send = s[0]; | 
 | 735 |     transport_registration_recv = s[1]; | 
 | 736 |  | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 737 |     transport_registration_fde = | 
| Yi Kong | aed415c | 2018-07-13 18:15:16 -0700 | [diff] [blame] | 738 |         fdevent_create(transport_registration_recv, transport_registration_func, nullptr); | 
| Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 739 |     fdevent_set(transport_registration_fde, FDE_READ); | 
| Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 740 | } | 
 | 741 |  | 
 | 742 | void kick_all_transports() { | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 743 | #if ADB_HOST | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 744 |     reconnect_handler.Stop(); | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 745 | #endif | 
| Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 746 |     // 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] | 747 |     std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
| Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 748 |     for (auto t : transport_list) { | 
 | 749 |         t->Kick(); | 
 | 750 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 751 | } | 
 | 752 |  | 
 | 753 | /* the fdevent select pump is single threaded */ | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 754 | void register_transport(atransport* transport) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 755 |     tmsg m; | 
 | 756 |     m.transport = transport; | 
 | 757 |     m.action = 1; | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 758 |     D("transport: %s registered", transport->serial.c_str()); | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 759 |     if (transport_write_action(transport_registration_send, &m)) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 760 |         PLOG(FATAL) << "cannot write transport registration socket"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 |     } | 
 | 762 | } | 
 | 763 |  | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 764 | static void remove_transport(atransport* transport) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 765 |     tmsg m; | 
 | 766 |     m.transport = transport; | 
 | 767 |     m.action = 0; | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 768 |     D("transport: %s removed", transport->serial.c_str()); | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 769 |     if (transport_write_action(transport_registration_send, &m)) { | 
| Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 770 |         PLOG(FATAL) << "cannot write transport registration socket"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 771 |     } | 
 | 772 | } | 
 | 773 |  | 
| Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 774 | static void transport_unref(atransport* t) { | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 775 |     check_main_thread(); | 
| Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 776 |     CHECK(t != nullptr); | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 777 |  | 
| Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 778 |     std::lock_guard<std::recursive_mutex> lock(transport_lock); | 
 | 779 |     CHECK_GT(t->ref_count, 0u); | 
 | 780 |     t->ref_count--; | 
 | 781 |     if (t->ref_count == 0) { | 
| Josh Gao | c51726c | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 782 |         LOG(INFO) << "destroying transport " << t->serial_name(); | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 783 |         t->connection()->Stop(); | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 784 | #if ADB_HOST | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 785 |         if (t->IsTcpDevice() && !t->kicked()) { | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 786 |             D("transport: %s unref (attempting reconnection)", t->serial.c_str()); | 
| Josh Gao | 4414e4c | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 787 |  | 
 | 788 |             // We need to clear the transport's keys, so that on the next connection, it tries | 
 | 789 |             // again from the beginning. | 
 | 790 |             t->ResetKeys(); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 791 |             reconnect_handler.TrackTransport(t); | 
 | 792 |         } else { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 793 |             D("transport: %s unref (kicking and closing)", t->serial.c_str()); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 794 |             remove_transport(t); | 
 | 795 |         } | 
| Josh Gao | def91c0 | 2018-07-31 18:28:32 -0700 | [diff] [blame] | 796 | #else | 
 | 797 |         D("transport: %s unref (kicking and closing)", t->serial.c_str()); | 
 | 798 |         remove_transport(t); | 
 | 799 | #endif | 
 | 800 |  | 
| David 'Digit' Turner | 730ff3b | 2011-01-06 14:11:07 +0100 | [diff] [blame] | 801 |     } else { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 802 |         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] | 803 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 804 | } | 
 | 805 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 806 | 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] | 807 |                       bool sanitize_qual) { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 808 |     if (to_test.empty()) /* Return true if both the qual and to_test are empty strings. */ | 
 | 809 |         return qual.empty(); | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 810 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 811 |     if (qual.empty()) return 0; | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 812 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 813 |     const char* ptr = to_test.c_str(); | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 814 |     if (prefix) { | 
 | 815 |         while (*prefix) { | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 816 |             if (*prefix++ != *ptr++) return 0; | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 817 |         } | 
 | 818 |     } | 
 | 819 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 820 |     for (char ch : qual) { | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 821 |         if (sanitize_qual && !isalnum(ch)) ch = '_'; | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 822 |         if (ch != *ptr++) return 0; | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 823 |     } | 
 | 824 |  | 
| Luis Hector Chavez | 6150a37 | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 825 |     /* Everything matched so far.  Return true if *ptr is a NUL. */ | 
 | 826 |     return !*ptr; | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 827 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 828 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 829 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, | 
 | 830 |                                   bool* is_ambiguous, std::string* error_out, | 
 | 831 |                                   bool accept_any_state) { | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 832 |     atransport* result = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 833 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 834 |     if (transport_id != 0) { | 
 | 835 |         *error_out = | 
 | 836 |             android::base::StringPrintf("no device with transport id '%" PRIu64 "'", transport_id); | 
 | 837 |     } else if (serial) { | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 838 |         *error_out = android::base::StringPrintf("device '%s' not found", serial); | 
 | 839 |     } else if (type == kTransportLocal) { | 
 | 840 |         *error_out = "no emulators found"; | 
 | 841 |     } else if (type == kTransportAny) { | 
 | 842 |         *error_out = "no devices/emulators found"; | 
 | 843 |     } else { | 
 | 844 |         *error_out = "no devices found"; | 
 | 845 |     } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 846 |  | 
| Josh Gao | 1db71af | 2017-08-17 13:50:51 -0700 | [diff] [blame] | 847 |     std::unique_lock<std::recursive_mutex> lock(transport_lock); | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 848 |     for (const auto& t : transport_list) { | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 849 |         if (t->GetConnectionState() == kCsNoPerm) { | 
| David Pursell | d2acbd1 | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 850 |             *error_out = UsbNoPermissionsLongHelpText(); | 
| Mike Lockwood | 37d3111 | 2009-08-08 13:53:16 -0400 | [diff] [blame] | 851 |             continue; | 
 | 852 |         } | 
| Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 853 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 854 |         if (transport_id) { | 
 | 855 |             if (t->id == transport_id) { | 
 | 856 |                 result = t; | 
 | 857 |                 break; | 
 | 858 |             } | 
 | 859 |         } else if (serial) { | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 860 |             if (t->MatchesTarget(serial)) { | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 861 |                 if (result) { | 
| Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 862 |                     *error_out = "more than one device"; | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 863 |                     if (is_ambiguous) *is_ambiguous = true; | 
 | 864 |                     result = nullptr; | 
| Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 865 |                     break; | 
 | 866 |                 } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 867 |                 result = t; | 
| Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 868 |             } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 869 |         } else { | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 870 |             if (type == kTransportUsb && t->type == kTransportUsb) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 871 |                 if (result) { | 
| Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 872 |                     *error_out = "more than one device"; | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 873 |                     if (is_ambiguous) *is_ambiguous = true; | 
 | 874 |                     result = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 875 |                     break; | 
 | 876 |                 } | 
 | 877 |                 result = t; | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 878 |             } else if (type == kTransportLocal && t->type == kTransportLocal) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 879 |                 if (result) { | 
| Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 880 |                     *error_out = "more than one emulator"; | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 881 |                     if (is_ambiguous) *is_ambiguous = true; | 
 | 882 |                     result = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 883 |                     break; | 
 | 884 |                 } | 
 | 885 |                 result = t; | 
| Elliott Hughes | 3bd73c1 | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 886 |             } else if (type == kTransportAny) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 887 |                 if (result) { | 
| Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 888 |                     *error_out = "more than one device/emulator"; | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 889 |                     if (is_ambiguous) *is_ambiguous = true; | 
 | 890 |                     result = nullptr; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 891 |                     break; | 
 | 892 |                 } | 
 | 893 |                 result = t; | 
 | 894 |             } | 
 | 895 |         } | 
 | 896 |     } | 
| Josh Gao | 0cd3ae1 | 2016-09-21 12:37:10 -0700 | [diff] [blame] | 897 |     lock.unlock(); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 898 |  | 
| Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 899 |     if (result && !accept_any_state) { | 
 | 900 |         // The caller requires an active transport. | 
 | 901 |         // Make sure that we're actually connected. | 
 | 902 |         ConnectionState state = result->GetConnectionState(); | 
 | 903 |         switch (state) { | 
 | 904 |             case kCsConnecting: | 
 | 905 |                 *error_out = "device still connecting"; | 
 | 906 |                 result = nullptr; | 
 | 907 |                 break; | 
| Benoit Goby | 77e8e58 | 2013-01-15 12:36:47 -0800 | [diff] [blame] | 908 |  | 
| Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 909 |             case kCsAuthorizing: | 
 | 910 |                 *error_out = "device still authorizing"; | 
 | 911 |                 result = nullptr; | 
 | 912 |                 break; | 
 | 913 |  | 
 | 914 |             case kCsUnauthorized: { | 
 | 915 |                 *error_out = "device unauthorized.\n"; | 
 | 916 |                 char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); | 
 | 917 |                 *error_out += "This adb server's $ADB_VENDOR_KEYS is "; | 
 | 918 |                 *error_out += ADB_VENDOR_KEYS ? ADB_VENDOR_KEYS : "not set"; | 
 | 919 |                 *error_out += "\n"; | 
 | 920 |                 *error_out += "Try 'adb kill-server' if that seems wrong.\n"; | 
 | 921 |                 *error_out += "Otherwise check for a confirmation dialog on your device."; | 
 | 922 |                 result = nullptr; | 
 | 923 |                 break; | 
 | 924 |             } | 
 | 925 |  | 
 | 926 |             case kCsOffline: | 
 | 927 |                 *error_out = "device offline"; | 
 | 928 |                 result = nullptr; | 
 | 929 |                 break; | 
 | 930 |  | 
 | 931 |             default: | 
 | 932 |                 break; | 
 | 933 |         } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 934 |     } | 
 | 935 |  | 
 | 936 |     if (result) { | 
| Elliott Hughes | e2d3677 | 2015-06-23 13:00:32 -0700 | [diff] [blame] | 937 |         *error_out = "success"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 |     } | 
 | 939 |  | 
 | 940 |     return result; | 
 | 941 | } | 
 | 942 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 943 | bool ConnectionWaitable::WaitForConnection(std::chrono::milliseconds timeout) { | 
 | 944 |     std::unique_lock<std::mutex> lock(mutex_); | 
| Josh Gao | 982f7bd | 2019-02-12 13:59:03 -0800 | [diff] [blame] | 945 |     ScopedLockAssertion assume_locked(mutex_); | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 946 |     return cv_.wait_for(lock, timeout, [&]() REQUIRES(mutex_) { | 
 | 947 |         return connection_established_ready_; | 
 | 948 |     }) && connection_established_; | 
 | 949 | } | 
 | 950 |  | 
 | 951 | void ConnectionWaitable::SetConnectionEstablished(bool success) { | 
 | 952 |     { | 
 | 953 |         std::lock_guard<std::mutex> lock(mutex_); | 
 | 954 |         if (connection_established_ready_) return; | 
 | 955 |         connection_established_ready_ = true; | 
 | 956 |         connection_established_ = success; | 
 | 957 |         D("connection established with %d", success); | 
 | 958 |     } | 
 | 959 |     cv_.notify_one(); | 
 | 960 | } | 
 | 961 |  | 
 | 962 | atransport::~atransport() { | 
 | 963 |     // If the connection callback had not been run before, run it now. | 
 | 964 |     SetConnectionEstablished(false); | 
 | 965 | } | 
 | 966 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 967 | int atransport::Write(apacket* p) { | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 968 |     return this->connection()->Write(std::unique_ptr<apacket>(p)) ? 0 : -1; | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 969 | } | 
 | 970 |  | 
| Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 971 | void atransport::Reset() { | 
 | 972 |     if (!kicked_.exchange(true)) { | 
 | 973 |         LOG(INFO) << "resetting transport " << this << " " << this->serial; | 
 | 974 |         this->connection()->Reset(); | 
 | 975 |     } | 
 | 976 | } | 
 | 977 |  | 
| Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 978 | void atransport::Kick() { | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 979 |     if (!kicked_.exchange(true)) { | 
| Josh Gao | 3705b34 | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 980 |         LOG(INFO) << "kicking transport " << this << " " << this->serial; | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 981 |         this->connection()->Stop(); | 
| Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 982 |     } | 
 | 983 | } | 
 | 984 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 985 | ConnectionState atransport::GetConnectionState() const { | 
 | 986 |     return connection_state_; | 
 | 987 | } | 
 | 988 |  | 
 | 989 | void atransport::SetConnectionState(ConnectionState state) { | 
 | 990 |     check_main_thread(); | 
 | 991 |     connection_state_ = state; | 
 | 992 | } | 
 | 993 |  | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 994 | void atransport::SetConnection(std::unique_ptr<Connection> connection) { | 
 | 995 |     std::lock_guard<std::mutex> lock(mutex_); | 
 | 996 |     connection_ = std::shared_ptr<Connection>(std::move(connection)); | 
 | 997 | } | 
 | 998 |  | 
| Josh Gao | ffbd336 | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 999 | std::string atransport::connection_state_name() const { | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 1000 |     ConnectionState state = GetConnectionState(); | 
 | 1001 |     switch (state) { | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1002 |         case kCsOffline: | 
 | 1003 |             return "offline"; | 
 | 1004 |         case kCsBootloader: | 
 | 1005 |             return "bootloader"; | 
 | 1006 |         case kCsDevice: | 
 | 1007 |             return "device"; | 
 | 1008 |         case kCsHost: | 
 | 1009 |             return "host"; | 
 | 1010 |         case kCsRecovery: | 
 | 1011 |             return "recovery"; | 
| Tao Bao | 55d407e | 2019-04-07 23:24:03 -0700 | [diff] [blame] | 1012 |         case kCsRescue: | 
 | 1013 |             return "rescue"; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1014 |         case kCsNoPerm: | 
 | 1015 |             return UsbNoPermissionsShortHelpText(); | 
 | 1016 |         case kCsSideload: | 
 | 1017 |             return "sideload"; | 
 | 1018 |         case kCsUnauthorized: | 
 | 1019 |             return "unauthorized"; | 
| Josh Gao | 704494b | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 1020 |         case kCsAuthorizing: | 
 | 1021 |             return "authorizing"; | 
 | 1022 |         case kCsConnecting: | 
 | 1023 |             return "connecting"; | 
| Josh Gao | 1290fbf | 2016-11-22 14:32:34 -0800 | [diff] [blame] | 1024 |         default: | 
 | 1025 |             return "unknown"; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1026 |     } | 
 | 1027 | } | 
 | 1028 |  | 
| Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 1029 | void atransport::update_version(int version, size_t payload) { | 
 | 1030 |     protocol_version = std::min(version, A_VERSION); | 
 | 1031 |     max_payload = std::min(payload, MAX_PAYLOAD); | 
 | 1032 | } | 
 | 1033 |  | 
 | 1034 | int atransport::get_protocol_version() const { | 
 | 1035 |     return protocol_version; | 
 | 1036 | } | 
 | 1037 |  | 
 | 1038 | size_t atransport::get_max_payload() const { | 
 | 1039 |     return max_payload; | 
 | 1040 | } | 
 | 1041 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 1042 | const FeatureSet& supported_features() { | 
| David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 1043 |     // Local static allocation to avoid global non-POD variables. | 
 | 1044 |     static const FeatureSet* features = new FeatureSet{ | 
| Josh Gao | 7b1cb66 | 2019-02-20 13:01:40 -0800 | [diff] [blame] | 1045 |             kFeatureShell2, | 
 | 1046 |             kFeatureCmd, | 
 | 1047 |             kFeatureStat2, | 
 | 1048 |             kFeatureFixedPushMkdir, | 
 | 1049 |             kFeatureApex, | 
 | 1050 |             kFeatureAbb, | 
 | 1051 |             kFeatureFixedPushSymlinkTimestamp, | 
| Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 1052 |             kFeatureAbbExec, | 
| Josh Gao | 8c2198c | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 1053 |             kFeatureRemountShell, | 
| 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 |