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