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