JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #ifndef __TRANSPORT_H |
| 18 | #define __TRANSPORT_H |
| 19 | |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 22 | #include <atomic> |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 23 | #include <chrono> |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 24 | #include <condition_variable> |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 25 | #include <deque> |
Josh Gao | 4e56250 | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 26 | #include <functional> |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 27 | #include <list> |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 28 | #include <memory> |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 29 | #include <mutex> |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 30 | #include <string> |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 31 | #include <string_view> |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 32 | #include <thread> |
Yurii Zubrytskyi | 816c2d2 | 2020-03-26 18:19:28 -0700 | [diff] [blame^] | 33 | #include <vector> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 34 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 35 | #include <android-base/macros.h> |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 36 | #include <android-base/thread_annotations.h> |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 37 | #include <openssl/rsa.h> |
| 38 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 39 | #include "adb.h" |
| 40 | #include "adb_unique_fd.h" |
Josh Gao | e490fbb | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 41 | #include "types.h" |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 42 | |
Yurii Zubrytskyi | 816c2d2 | 2020-03-26 18:19:28 -0700 | [diff] [blame^] | 43 | // Even though the feature set is used as a set, we only have a dozen or two |
| 44 | // of available features at any moment. Vector works much better in terms of |
| 45 | // both memory usage and performance for these sizes. |
| 46 | using FeatureSet = std::vector<std::string>; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 47 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 48 | namespace adb { |
| 49 | namespace tls { |
| 50 | |
| 51 | class TlsConnection; |
| 52 | |
| 53 | } // namespace tls |
| 54 | } // namespace adb |
| 55 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 56 | const FeatureSet& supported_features(); |
| 57 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 58 | // Encodes and decodes FeatureSet objects into human-readable strings. |
| 59 | std::string FeatureSetToString(const FeatureSet& features); |
| 60 | FeatureSet StringToFeatureSet(const std::string& features_string); |
| 61 | |
David Pursell | 22fc5e9 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 62 | // Returns true if both local features and |feature_set| support |feature|. |
| 63 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); |
| 64 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 65 | // Do not use any of [:;=,] in feature strings, they have special meaning |
| 66 | // in the connection banner. |
Todd Kennedy | 1e2f7dc | 2015-11-03 16:53:08 -0800 | [diff] [blame] | 67 | extern const char* const kFeatureShell2; |
| 68 | // The 'cmd' command is available |
| 69 | extern const char* const kFeatureCmd; |
Josh Gao | a2cf375 | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 70 | extern const char* const kFeatureStat2; |
Josh Gao | 7b08307 | 2019-08-07 14:23:17 -0700 | [diff] [blame] | 71 | extern const char* const kFeatureLs2; |
Josh Gao | 210b63f | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 72 | // The server is running with libusb enabled. |
| 73 | extern const char* const kFeatureLibusb; |
Josh Gao | 281aab7 | 2018-10-22 13:00:05 -0700 | [diff] [blame] | 74 | // adbd supports `push --sync`. |
Dan Albert | 27983bc | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 75 | extern const char* const kFeaturePushSync; |
Josh Gao | 281aab7 | 2018-10-22 13:00:05 -0700 | [diff] [blame] | 76 | // adbd supports installing .apex packages. |
Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 77 | extern const char* const kFeatureApex; |
Josh Gao | 281aab7 | 2018-10-22 13:00:05 -0700 | [diff] [blame] | 78 | // adbd has b/110953234 fixed. |
| 79 | extern const char* const kFeatureFixedPushMkdir; |
Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 80 | // adbd supports android binder bridge (abb) in interactive mode using shell protocol. |
Alex Buynytskyy | e1fa814 | 2019-01-17 13:13:56 -0800 | [diff] [blame] | 81 | extern const char* const kFeatureAbb; |
Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 82 | // adbd supports abb using raw pipe. |
| 83 | extern const char* const kFeatureAbbExec; |
Josh Gao | 9eeb9f7 | 2019-02-20 13:01:40 -0800 | [diff] [blame] | 84 | // adbd properly updates symlink timestamps on push. |
| 85 | extern const char* const kFeatureFixedPushSymlinkTimestamp; |
Josh Gao | 2f0f9eb | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 86 | // Implement `adb remount` via shelling out to /system/bin/remount. |
Josh Gao | f764d57 | 2019-07-11 14:15:32 -0700 | [diff] [blame] | 87 | extern const char* const kFeatureRemountShell; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame] | 88 | // adbd supports `track-app` service reporting debuggable/profileable apps. |
| 89 | extern const char* const kFeatureTrackApp; |
Josh Gao | 2f0f9eb | 2020-03-04 19:34:08 -0800 | [diff] [blame] | 90 | // adbd supports version 2 of send/recv. |
| 91 | extern const char* const kFeatureSendRecv2; |
| 92 | // adbd supports brotli for send/recv v2. |
| 93 | extern const char* const kFeatureSendRecv2Brotli; |
Josh Gao | fb386cc | 2020-03-26 22:02:03 -0700 | [diff] [blame] | 94 | // adbd supports LZ4 for send/recv v2. |
| 95 | extern const char* const kFeatureSendRecv2LZ4; |
Josh Gao | 8a410a0 | 2020-03-30 23:25:16 -0700 | [diff] [blame] | 96 | // adbd supports dry-run send for send/recv v2. |
| 97 | extern const char* const kFeatureSendRecv2DryRunSend; |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 98 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 99 | TransportId NextTransportId(); |
| 100 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 101 | // Abstraction for a non-blocking packet transport. |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 102 | struct Connection { |
| 103 | Connection() = default; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 104 | virtual ~Connection() = default; |
| 105 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 106 | void SetTransportName(std::string transport_name) { |
| 107 | transport_name_ = std::move(transport_name); |
| 108 | } |
| 109 | |
| 110 | using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>; |
| 111 | void SetReadCallback(ReadCallback callback) { |
| 112 | CHECK(!read_callback_); |
| 113 | read_callback_ = callback; |
| 114 | } |
| 115 | |
| 116 | // Called after the Connection has terminated, either by an error or because Stop was called. |
| 117 | using ErrorCallback = std::function<void(Connection*, const std::string&)>; |
| 118 | void SetErrorCallback(ErrorCallback callback) { |
| 119 | CHECK(!error_callback_); |
| 120 | error_callback_ = callback; |
| 121 | } |
| 122 | |
| 123 | virtual bool Write(std::unique_ptr<apacket> packet) = 0; |
| 124 | |
| 125 | virtual void Start() = 0; |
| 126 | virtual void Stop() = 0; |
| 127 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 128 | virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0; |
| 129 | |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 130 | // Stop, and reset the device if it's a USB connection. |
| 131 | virtual void Reset(); |
| 132 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 133 | std::string transport_name_; |
| 134 | ReadCallback read_callback_; |
| 135 | ErrorCallback error_callback_; |
Josh Gao | 1e41fda | 2018-04-05 16:16:04 -0700 | [diff] [blame] | 136 | |
| 137 | static std::unique_ptr<Connection> FromFd(unique_fd fd); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // Abstraction for a blocking packet transport. |
| 141 | struct BlockingConnection { |
| 142 | BlockingConnection() = default; |
| 143 | BlockingConnection(const BlockingConnection& copy) = delete; |
| 144 | BlockingConnection(BlockingConnection&& move) = delete; |
| 145 | |
| 146 | // Destroy a BlockingConnection. Formerly known as 'Close' in atransport. |
| 147 | virtual ~BlockingConnection() = default; |
| 148 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 149 | // Read/Write a packet. These functions are concurrently called from a transport's reader/writer |
| 150 | // threads. |
| 151 | virtual bool Read(apacket* packet) = 0; |
| 152 | virtual bool Write(apacket* packet) = 0; |
| 153 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 154 | virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0; |
| 155 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 156 | // Terminate a connection. |
| 157 | // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate. |
| 158 | // Formerly known as 'Kick' in atransport. |
| 159 | virtual void Close() = 0; |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 160 | |
| 161 | // Terminate a connection, and reset it. |
| 162 | virtual void Reset() = 0; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 165 | struct BlockingConnectionAdapter : public Connection { |
| 166 | explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection); |
| 167 | |
| 168 | virtual ~BlockingConnectionAdapter(); |
| 169 | |
| 170 | virtual bool Write(std::unique_ptr<apacket> packet) override final; |
| 171 | |
| 172 | virtual void Start() override final; |
| 173 | virtual void Stop() override final; |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 174 | virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final; |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 175 | |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 176 | virtual void Reset() override final; |
| 177 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 178 | private: |
| 179 | void StartReadThread() REQUIRES(mutex_); |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 180 | bool started_ GUARDED_BY(mutex_) = false; |
| 181 | bool stopped_ GUARDED_BY(mutex_) = false; |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 182 | |
| 183 | std::unique_ptr<BlockingConnection> underlying_; |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 184 | std::thread read_thread_ GUARDED_BY(mutex_); |
| 185 | std::thread write_thread_ GUARDED_BY(mutex_); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 186 | |
Josh Gao | 13781e8 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 187 | std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_); |
Josh Gao | 715fe60 | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 188 | std::mutex mutex_; |
| 189 | std::condition_variable cv_; |
| 190 | |
| 191 | std::once_flag error_flag_; |
| 192 | }; |
| 193 | |
| 194 | struct FdConnection : public BlockingConnection { |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 195 | explicit FdConnection(unique_fd fd); |
| 196 | ~FdConnection(); |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 197 | |
| 198 | bool Read(apacket* packet) override final; |
| 199 | bool Write(apacket* packet) override final; |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 200 | bool DoTlsHandshake(RSA* key, std::string* auth_key) override final; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 201 | |
| 202 | void Close() override; |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 203 | virtual void Reset() override final { Close(); } |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 204 | |
| 205 | private: |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 206 | bool DispatchRead(void* buf, size_t len); |
| 207 | bool DispatchWrite(void* buf, size_t len); |
| 208 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 209 | unique_fd fd_; |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 210 | std::unique_ptr<adb::tls::TlsConnection> tls_; |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 211 | }; |
| 212 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 213 | // Waits for a transport's connection to be not pending. This is a separate |
| 214 | // object so that the transport can be destroyed and another thread can be |
| 215 | // notified of it in a race-free way. |
| 216 | class ConnectionWaitable { |
| 217 | public: |
| 218 | ConnectionWaitable() = default; |
| 219 | ~ConnectionWaitable() = default; |
| 220 | |
| 221 | // Waits until the first CNXN packet has been received by the owning |
| 222 | // atransport, or the specified timeout has elapsed. Can be called from any |
| 223 | // thread. |
| 224 | // |
| 225 | // Returns true if the CNXN packet was received in a timely fashion, false |
| 226 | // otherwise. |
| 227 | bool WaitForConnection(std::chrono::milliseconds timeout); |
| 228 | |
| 229 | // Can be called from any thread when the connection stops being pending. |
| 230 | // Only the first invocation will be acknowledged, the rest will be no-ops. |
| 231 | void SetConnectionEstablished(bool success); |
| 232 | |
| 233 | private: |
| 234 | bool connection_established_ GUARDED_BY(mutex_) = false; |
| 235 | bool connection_established_ready_ GUARDED_BY(mutex_) = false; |
| 236 | std::mutex mutex_; |
| 237 | std::condition_variable cv_; |
| 238 | |
| 239 | DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable); |
| 240 | }; |
| 241 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 242 | enum class ReconnectResult { |
| 243 | Retry, |
| 244 | Success, |
| 245 | Abort, |
| 246 | }; |
| 247 | |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 248 | #if ADB_HOST |
| 249 | struct usb_handle; |
| 250 | #endif |
| 251 | |
Josh Gao | e490fbb | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 252 | class atransport : public enable_weak_from_this<atransport> { |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 253 | public: |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 254 | // TODO(danalbert): We expose waaaaaaay too much stuff because this was |
| 255 | // historically just a struct, but making the whole thing a more idiomatic |
| 256 | // class in one go is a very large change. Given how bad our testing is, |
| 257 | // it's better to do this piece by piece. |
| 258 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 259 | using ReconnectCallback = std::function<ReconnectResult(atransport*)>; |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 260 | |
| 261 | atransport(ReconnectCallback reconnect, ConnectionState state) |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 262 | : id(NextTransportId()), |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 263 | kicked_(false), |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 264 | connection_state_(state), |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 265 | connection_waitable_(std::make_shared<ConnectionWaitable>()), |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 266 | connection_(nullptr), |
| 267 | reconnect_(std::move(reconnect)) { |
Tim Murray | ee7b44d | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 268 | // Initialize protocol to min version for compatibility with older versions. |
| 269 | // Version will be updated post-connect. |
| 270 | protocol_version = A_VERSION_MIN; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 271 | max_payload = MAX_PAYLOAD; |
| 272 | } |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 273 | atransport(ConnectionState state = kCsOffline) |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 274 | : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {} |
Josh Gao | e490fbb | 2019-12-09 15:44:57 -0800 | [diff] [blame] | 275 | ~atransport(); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 276 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 277 | int Write(apacket* p); |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 278 | void Reset(); |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 279 | void Kick(); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 280 | bool kicked() const { return kicked_; } |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 281 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 282 | // ConnectionState can be read by all threads, but can only be written in the main thread. |
| 283 | ConnectionState GetConnectionState() const; |
| 284 | void SetConnectionState(ConnectionState state); |
| 285 | |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 286 | void SetConnection(std::unique_ptr<Connection> connection); |
| 287 | std::shared_ptr<Connection> connection() { |
| 288 | std::lock_guard<std::mutex> lock(mutex_); |
| 289 | return connection_; |
| 290 | } |
| 291 | |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 292 | #if ADB_HOST |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 293 | void SetUsbHandle(usb_handle* h) { usb_handle_ = h; } |
| 294 | usb_handle* GetUsbHandle() { return usb_handle_; } |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 295 | #endif |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 296 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 297 | const TransportId id; |
Josh Gao | 14ed9f9 | 2019-12-09 13:45:31 -0800 | [diff] [blame] | 298 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 299 | bool online = false; |
| 300 | TransportType type = kTransportAny; |
| 301 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 302 | // Used to identify transports for clients. |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 303 | std::string serial; |
| 304 | std::string product; |
| 305 | std::string model; |
| 306 | std::string device; |
| 307 | std::string devpath; |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 308 | |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 309 | // If this is set, the transport will initiate the connection with a |
| 310 | // START_TLS command, instead of AUTH. |
| 311 | bool use_tls = false; |
| 312 | int tls_version = A_STLS_VERSION; |
| 313 | int get_tls_version() const; |
| 314 | |
Josh Gao | 7cac88a | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 315 | #if !ADB_HOST |
Michael Groover | 02b7427 | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 316 | // Used to provide the key to the framework. |
| 317 | std::string auth_key; |
Josh Gao | 7cac88a | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 318 | uint64_t auth_id; |
| 319 | #endif |
Michael Groover | 02b7427 | 2019-04-25 18:33:35 -0700 | [diff] [blame] | 320 | |
Josh Gao | 395b86a | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 321 | bool IsTcpDevice() const { return type == kTransportLocal; } |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 322 | |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 323 | #if ADB_HOST |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 324 | // The current key being authorized. |
| 325 | std::shared_ptr<RSA> Key(); |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 326 | std::shared_ptr<RSA> NextKey(); |
Josh Gao | eb65652 | 2018-12-04 01:07:50 -0800 | [diff] [blame] | 327 | void ResetKeys(); |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 328 | #endif |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 329 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 330 | char token[TOKEN_SIZE] = {}; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 331 | size_t failed_auth_attempts = 0; |
| 332 | |
Luis Hector Chavez | b4edbdf | 2018-07-18 21:18:27 -0700 | [diff] [blame] | 333 | std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; } |
Josh Gao | 3cd03be | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 334 | std::string connection_state_name() const; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 335 | |
| 336 | void update_version(int version, size_t payload); |
| 337 | int get_protocol_version() const; |
| 338 | size_t get_max_payload() const; |
| 339 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 340 | const FeatureSet& features() const { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 341 | return features_; |
| 342 | } |
| 343 | |
| 344 | bool has_feature(const std::string& feature) const; |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 345 | |
| 346 | // Loads the transport's feature set from the given string. |
| 347 | void SetFeatures(const std::string& features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 348 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 349 | void AddDisconnect(adisconnect* disconnect); |
| 350 | void RemoveDisconnect(adisconnect* disconnect); |
| 351 | void RunDisconnects(); |
| 352 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 353 | // Returns true if |target| matches this transport. A matching |target| can be any of: |
| 354 | // * <serial> |
| 355 | // * <devpath> |
| 356 | // * product:<product> |
| 357 | // * model:<model> |
| 358 | // * device:<device> |
| 359 | // |
| 360 | // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets. |
| 361 | // For example, serial "100.100.100.100:5555" would match any of: |
| 362 | // * 100.100.100.100 |
| 363 | // * tcp:100.100.100.100 |
| 364 | // * udp:100.100.100.100:5555 |
| 365 | // This is to make it easier to use the same network target for both fastboot and adb. |
| 366 | bool MatchesTarget(const std::string& target) const; |
| 367 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 368 | // Notifies that the atransport is no longer waiting for the connection |
| 369 | // being established. |
| 370 | void SetConnectionEstablished(bool success); |
| 371 | |
| 372 | // Gets a shared reference to the ConnectionWaitable. |
| 373 | std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; } |
| 374 | |
Josh Gao | d24580d | 2018-08-30 11:37:00 -0700 | [diff] [blame] | 375 | // Attempts to reconnect with the underlying Connection. |
| 376 | ReconnectResult Reconnect(); |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 377 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 378 | private: |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 379 | std::atomic<bool> kicked_; |
Yabin Cui | f2a9f9b | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 380 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 381 | // A set of features transmitted in the banner with the initial connection. |
| 382 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 383 | FeatureSet features_; |
| 384 | int protocol_version; |
| 385 | size_t max_payload; |
| 386 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 387 | // A list of adisconnect callbacks called when the transport is kicked. |
| 388 | std::list<adisconnect*> disconnects_; |
| 389 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 390 | std::atomic<ConnectionState> connection_state_; |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 391 | #if ADB_HOST |
Josh Gao | 22cb70b | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 392 | std::deque<std::shared_ptr<RSA>> keys_; |
Josh Gao | eac2058 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 393 | #endif |
Elliott Hughes | 801066a | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 394 | |
Luis Hector Chavez | da74b90 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 395 | // A sharable object that can be used to wait for the atransport's |
| 396 | // connection to be established. |
| 397 | std::shared_ptr<ConnectionWaitable> connection_waitable_; |
| 398 | |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 399 | // The underlying connection object. |
| 400 | std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_); |
| 401 | |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 402 | #if ADB_HOST |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 403 | // USB handle for the connection, if available. |
| 404 | usb_handle* usb_handle_ = nullptr; |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 405 | #endif |
Josh Gao | 68f2c38 | 2018-12-11 13:11:52 -0800 | [diff] [blame] | 406 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 407 | // A callback that will be invoked when the atransport needs to reconnect. |
| 408 | ReconnectCallback reconnect_; |
| 409 | |
Luis Hector Chavez | 3c7881d | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 410 | std::mutex mutex_; |
| 411 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 412 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 413 | }; |
| 414 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 415 | /* |
| 416 | * Obtain a transport from the available transports. |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 417 | * If serial is non-null then only the device with that serial will be chosen. |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 418 | * If transport_id is non-zero then only the device with that transport ID will be chosen. |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 419 | * If multiple devices/emulators would match, *is_ambiguous (if non-null) |
| 420 | * is set to true and nullptr returned. |
| 421 | * If no suitable transport is found, error is set and nullptr returned. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 422 | */ |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 423 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, |
| 424 | bool* is_ambiguous, std::string* error_out, |
| 425 | bool accept_any_state = false); |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 426 | void kick_transport(atransport* t, bool reset = false); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 427 | void update_transports(void); |
| 428 | |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 429 | // Iterates across all of the current and pending transports. |
| 430 | // Stops iteration and returns false if fn returns false, otherwise returns true. |
| 431 | bool iterate_transports(std::function<bool(const atransport*)> fn); |
| 432 | |
Luis Hector Chavez | 0aeda10 | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 433 | void init_reconnect_handler(void); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 434 | void init_transport_registration(void); |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 435 | void init_mdns_transport_discovery(void); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 436 | std::string list_transports(bool long_listing); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 437 | atransport* find_transport(const char* serial); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 438 | void kick_all_tcp_devices(); |
Josh Gao | 165460f | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 439 | void kick_all_transports(); |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 440 | void kick_all_tcp_tls_transports(); |
| 441 | #if !ADB_HOST |
| 442 | void kick_all_transports_by_auth_key(std::string_view auth_key); |
| 443 | #endif |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 444 | |
Josh Gao | 3a34bc5 | 2018-10-11 16:33:05 -0700 | [diff] [blame] | 445 | void register_transport(atransport* transport); |
Josh Gao | 6b55e75 | 2020-03-27 18:09:56 -0700 | [diff] [blame] | 446 | |
| 447 | #if ADB_HOST |
| 448 | void init_usb_transport(atransport* t, usb_handle* usb); |
| 449 | void register_usb_transport(usb_handle* h, const char* serial, const char* devpath, |
| 450 | unsigned writeable); |
| 451 | |
| 452 | // This should only be used for transports with connection_state == kCsNoPerm. |
| 453 | void unregister_usb_transport(usb_handle* usb); |
| 454 | #endif |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 455 | |
Casey Dahlin | 3122cdf | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 456 | /* Connect to a network address and register it as a device */ |
| 457 | void connect_device(const std::string& address, std::string* response); |
| 458 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 459 | /* cause new transports to be init'd and added to the list */ |
Josh Gao | 597044d | 2018-08-08 16:20:14 -0700 | [diff] [blame] | 460 | bool register_socket_transport(unique_fd s, std::string serial, int port, int local, |
Joshua Duong | 64fab75 | 2020-01-21 13:19:42 -0800 | [diff] [blame] | 461 | atransport::ReconnectCallback reconnect, bool use_tls, |
| 462 | int* error = nullptr); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 463 | |
Josh Gao | 67b683a | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 464 | bool check_header(apacket* p, atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 465 | |
Josh Gao | 3a2172b | 2019-03-28 15:47:44 -0700 | [diff] [blame] | 466 | void close_usb_devices(bool reset = false); |
| 467 | void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 468 | |
| 469 | void send_packet(apacket* p, atransport* t); |
| 470 | |
Josh Gao | 3212463 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 471 | asocket* create_device_tracker(bool long_output); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 472 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 473 | #if !ADB_HOST |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 474 | unique_fd adb_listen(std::string_view addr, std::string* error); |
| 475 | void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func, |
| 476 | std::string_view addr); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 477 | |
| 478 | #if defined(__ANDROID__) |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 479 | void qemu_socket_thread(std::string_view addr); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 480 | bool use_qemu_goldfish(); |
| 481 | #endif |
| 482 | |
| 483 | #endif |
| 484 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 485 | #endif /* __TRANSPORT_H */ |