blob: 861869369eadb3f6696aff32633ffe8afeb53776 [file] [log] [blame]
JP Abgrall2e5dd6e2011-03-16 15:57:42 -07001/*
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 Albert020292b2015-02-18 18:03:26 -080020#include <sys/types.h>
21
Yabin Cui3cf1b362017-03-10 16:01:01 -080022#include <atomic>
Luis Hector Chavezda74b902018-04-17 14:25:04 -070023#include <chrono>
Josh Gao715fe602018-02-16 13:24:58 -080024#include <condition_variable>
Elliott Hughes801066a2016-06-29 17:42:01 -070025#include <deque>
Josh Gao4e562502016-10-27 14:01:08 -070026#include <functional>
Yabin Cui2d4c1982015-08-28 15:09:44 -070027#include <list>
Josh Gao22cb70b2016-08-18 22:00:12 -070028#include <memory>
Yabin Cui3cf1b362017-03-10 16:01:01 -080029#include <mutex>
Josh Gao887ab012020-06-24 16:25:49 -070030#include <optional>
Elliott Hughesab882422015-04-16 22:54:44 -070031#include <string>
Cody Schuffelen637aaf52019-01-04 18:51:11 -080032#include <string_view>
Josh Gao715fe602018-02-16 13:24:58 -080033#include <thread>
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070034#include <vector>
Dan Albertb302d122015-02-24 15:51:19 -080035
Luis Hector Chavezda74b902018-04-17 14:25:04 -070036#include <android-base/macros.h>
Josh Gao13781e82018-04-03 12:55:18 -070037#include <android-base/thread_annotations.h>
Elliott Hughes801066a2016-06-29 17:42:01 -070038#include <openssl/rsa.h>
39
Josh Gao395b86a2018-01-28 20:32:46 -080040#include "adb.h"
41#include "adb_unique_fd.h"
Josh Gaoe490fbb2019-12-09 15:44:57 -080042#include "types.h"
Josh Gao395b86a2018-01-28 20:32:46 -080043
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070044// 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.
47using FeatureSet = std::vector<std::string>;
Dan Albertbe8e54b2015-05-18 13:06:53 -070048
Joshua Duong64fab752020-01-21 13:19:42 -080049namespace adb {
50namespace tls {
51
52class TlsConnection;
53
54} // namespace tls
55} // namespace adb
56
Dan Albertbe8e54b2015-05-18 13:06:53 -070057const FeatureSet& supported_features();
58
David Pursella07dbad2015-09-22 10:43:08 -070059// Encodes and decodes FeatureSet objects into human-readable strings.
60std::string FeatureSetToString(const FeatureSet& features);
61FeatureSet StringToFeatureSet(const std::string& features_string);
62
David Pursell22fc5e92015-09-30 13:35:42 -070063// Returns true if both local features and |feature_set| support |feature|.
64bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
65
David Pursella07dbad2015-09-22 10:43:08 -070066// Do not use any of [:;=,] in feature strings, they have special meaning
67// in the connection banner.
Todd Kennedy1e2f7dc2015-11-03 16:53:08 -080068extern const char* const kFeatureShell2;
69// The 'cmd' command is available
70extern const char* const kFeatureCmd;
Josh Gaoa2cf3752016-12-05 17:11:34 -080071extern const char* const kFeatureStat2;
Josh Gao7b083072019-08-07 14:23:17 -070072extern const char* const kFeatureLs2;
Josh Gao210b63f2017-02-22 17:07:01 -080073// The server is running with libusb enabled.
74extern const char* const kFeatureLibusb;
Josh Gao281aab72018-10-22 13:00:05 -070075// adbd supports `push --sync`.
Dan Albert27983bc2017-05-23 14:30:00 -070076extern const char* const kFeaturePushSync;
Josh Gao281aab72018-10-22 13:00:05 -070077// adbd supports installing .apex packages.
Dario Frenidcb4c362018-10-04 16:26:40 +010078extern const char* const kFeatureApex;
Josh Gao281aab72018-10-22 13:00:05 -070079// adbd has b/110953234 fixed.
80extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070081// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyye1fa8142019-01-17 13:13:56 -080082extern const char* const kFeatureAbb;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070083// adbd supports abb using raw pipe.
84extern const char* const kFeatureAbbExec;
Josh Gao9eeb9f72019-02-20 13:01:40 -080085// adbd properly updates symlink timestamps on push.
86extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao2f0f9eb2020-03-04 19:34:08 -080087// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gaof764d572019-07-11 14:15:32 -070088extern const char* const kFeatureRemountShell;
Shukang Zhou420ad552020-02-13 17:01:39 -080089// adbd supports `track-app` service reporting debuggable/profileable apps.
90extern const char* const kFeatureTrackApp;
Josh Gao2f0f9eb2020-03-04 19:34:08 -080091// adbd supports version 2 of send/recv.
92extern const char* const kFeatureSendRecv2;
93// adbd supports brotli for send/recv v2.
94extern const char* const kFeatureSendRecv2Brotli;
Josh Gaofb386cc2020-03-26 22:02:03 -070095// adbd supports LZ4 for send/recv v2.
96extern const char* const kFeatureSendRecv2LZ4;
Josh Gaobdebc9b2020-05-27 17:52:52 -070097// adbd supports Zstd for send/recv v2.
98extern const char* const kFeatureSendRecv2Zstd;
Josh Gao8a410a02020-03-30 23:25:16 -070099// adbd supports dry-run send for send/recv v2.
100extern const char* const kFeatureSendRecv2DryRunSend;
David Pursell8da19a42015-08-31 10:42:13 -0700101
Josh Gaob39e4152017-08-16 16:57:01 -0700102TransportId NextTransportId();
103
Josh Gao715fe602018-02-16 13:24:58 -0800104// Abstraction for a non-blocking packet transport.
Josh Gao395b86a2018-01-28 20:32:46 -0800105struct Connection {
106 Connection() = default;
Josh Gao395b86a2018-01-28 20:32:46 -0800107 virtual ~Connection() = default;
108
Josh Gao7852ca42021-06-17 04:17:25 -0700109 void SetTransport(atransport* transport) { transport_ = transport; }
Josh Gao715fe602018-02-16 13:24:58 -0800110
111 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
112
113 virtual void Start() = 0;
114 virtual void Stop() = 0;
115
Joshua Duong64fab752020-01-21 13:19:42 -0800116 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
117
Josh Gao3a2172b2019-03-28 15:47:44 -0700118 // Stop, and reset the device if it's a USB connection.
119 virtual void Reset();
120
Josh Gao8e7c9722021-06-17 04:19:45 -0700121 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 Gao7852ca42021-06-17 04:17:25 -0700131 std::string Serial() const;
132
133 atransport* transport_ = nullptr;
Josh Gao1e41fda2018-04-05 16:16:04 -0700134
135 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao715fe602018-02-16 13:24:58 -0800136};
137
138// Abstraction for a blocking packet transport.
139struct 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 Gao395b86a2018-01-28 20:32:46 -0800147 // 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 Duong64fab752020-01-21 13:19:42 -0800152 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
153
Josh Gao395b86a2018-01-28 20:32:46 -0800154 // 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 Gao3a2172b2019-03-28 15:47:44 -0700158
159 // Terminate a connection, and reset it.
160 virtual void Reset() = 0;
Josh Gao395b86a2018-01-28 20:32:46 -0800161};
162
Josh Gao715fe602018-02-16 13:24:58 -0800163struct 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 Duong64fab752020-01-21 13:19:42 -0800172 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao715fe602018-02-16 13:24:58 -0800173
Josh Gao3a2172b2019-03-28 15:47:44 -0700174 virtual void Reset() override final;
175
Joshua Duong64fab752020-01-21 13:19:42 -0800176 private:
177 void StartReadThread() REQUIRES(mutex_);
Josh Gao13781e82018-04-03 12:55:18 -0700178 bool started_ GUARDED_BY(mutex_) = false;
179 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao715fe602018-02-16 13:24:58 -0800180
181 std::unique_ptr<BlockingConnection> underlying_;
Josh Gao13781e82018-04-03 12:55:18 -0700182 std::thread read_thread_ GUARDED_BY(mutex_);
183 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800184
Josh Gao13781e82018-04-03 12:55:18 -0700185 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800186 std::mutex mutex_;
187 std::condition_variable cv_;
188
189 std::once_flag error_flag_;
190};
191
192struct FdConnection : public BlockingConnection {
Joshua Duong64fab752020-01-21 13:19:42 -0800193 explicit FdConnection(unique_fd fd);
194 ~FdConnection();
Josh Gao395b86a2018-01-28 20:32:46 -0800195
196 bool Read(apacket* packet) override final;
197 bool Write(apacket* packet) override final;
Joshua Duong64fab752020-01-21 13:19:42 -0800198 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao395b86a2018-01-28 20:32:46 -0800199
200 void Close() override;
Josh Gao3a2172b2019-03-28 15:47:44 -0700201 virtual void Reset() override final { Close(); }
Josh Gao395b86a2018-01-28 20:32:46 -0800202
203 private:
Joshua Duong64fab752020-01-21 13:19:42 -0800204 bool DispatchRead(void* buf, size_t len);
205 bool DispatchWrite(void* buf, size_t len);
206
Josh Gao395b86a2018-01-28 20:32:46 -0800207 unique_fd fd_;
Joshua Duong64fab752020-01-21 13:19:42 -0800208 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gao395b86a2018-01-28 20:32:46 -0800209};
210
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700211// 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.
214class 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 Gaod24580d2018-08-30 11:37:00 -0700240enum class ReconnectResult {
241 Retry,
242 Success,
243 Abort,
244};
245
Josh Gao6b55e752020-03-27 18:09:56 -0700246#if ADB_HOST
247struct usb_handle;
248#endif
249
Josh Gaoe490fbb2019-12-09 15:44:57 -0800250class atransport : public enable_weak_from_this<atransport> {
Josh Gaob39e4152017-08-16 16:57:01 -0700251 public:
Dan Albertbe8e54b2015-05-18 13:06:53 -0700252 // 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 Gaod24580d2018-08-30 11:37:00 -0700257 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700258
259 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700260 : id(NextTransportId()),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700261 kicked_(false),
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700262 connection_state_(state),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700263 connection_(nullptr),
264 reconnect_(std::move(reconnect)) {
Josh Gao65d18e22020-04-22 20:57:26 -0700265#if ADB_HOST
266 connection_waitable_ = std::make_shared<ConnectionWaitable>();
267#endif
268
Tim Murrayee7b44d2017-12-07 11:40:00 -0800269 // Initialize protocol to min version for compatibility with older versions.
270 // Version will be updated post-connect.
271 protocol_version = A_VERSION_MIN;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700272 max_payload = MAX_PAYLOAD;
273 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700274 atransport(ConnectionState state = kCsOffline)
Josh Gaod24580d2018-08-30 11:37:00 -0700275 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gaoe490fbb2019-12-09 15:44:57 -0800276 ~atransport();
Dan Albertbe8e54b2015-05-18 13:06:53 -0700277
Yabin Cui3cf1b362017-03-10 16:01:01 -0800278 int Write(apacket* p);
Josh Gao3a2172b2019-03-28 15:47:44 -0700279 void Reset();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700280 void Kick();
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700281 bool kicked() const { return kicked_; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700282
Yabin Cui3cf1b362017-03-10 16:01:01 -0800283 // 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 Gaof2d6af52021-04-27 20:32:02 -0700287 void SetConnection(std::shared_ptr<Connection> connection);
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700288 std::shared_ptr<Connection> connection() {
289 std::lock_guard<std::mutex> lock(mutex_);
290 return connection_;
291 }
292
Josh Gao7852ca42021-06-17 04:17:25 -0700293 bool HandleRead(std::unique_ptr<apacket> p);
294 void HandleError(const std::string& error);
295
Josh Gao6b55e752020-03-27 18:09:56 -0700296#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800297 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
298 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao6b55e752020-03-27 18:09:56 -0700299#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800300
Josh Gaob39e4152017-08-16 16:57:01 -0700301 const TransportId id;
Josh Gao14ed9f92019-12-09 13:45:31 -0800302
Dan Albertbe8e54b2015-05-18 13:06:53 -0700303 bool online = false;
304 TransportType type = kTransportAny;
305
Dan Albertbe8e54b2015-05-18 13:06:53 -0700306 // Used to identify transports for clients.
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700307 std::string serial;
308 std::string product;
309 std::string model;
310 std::string device;
311 std::string devpath;
Yabin Cuif401ead2016-04-29 16:53:52 -0700312
Joshua Duong64fab752020-01-21 13:19:42 -0800313 // 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 Gao7cac88a2019-10-22 12:30:39 -0700319#if !ADB_HOST
Michael Groover02b74272019-04-25 18:33:35 -0700320 // Used to provide the key to the framework.
321 std::string auth_key;
Josh Gao887ab012020-06-24 16:25:49 -0700322 std::optional<uint64_t> auth_id;
Josh Gao7cac88a2019-10-22 12:30:39 -0700323#endif
Michael Groover02b74272019-04-25 18:33:35 -0700324
Josh Gao395b86a2018-01-28 20:32:46 -0800325 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700326
Josh Gaoeac20582016-10-05 19:02:29 -0700327#if ADB_HOST
Joshua Duong64fab752020-01-21 13:19:42 -0800328 // The current key being authorized.
329 std::shared_ptr<RSA> Key();
Josh Gao22cb70b2016-08-18 22:00:12 -0700330 std::shared_ptr<RSA> NextKey();
Josh Gaoeb656522018-12-04 01:07:50 -0800331 void ResetKeys();
Josh Gaoeac20582016-10-05 19:02:29 -0700332#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700333
Josh Gao67ac3792016-10-06 13:31:44 -0700334 char token[TOKEN_SIZE] = {};
Dan Albertbe8e54b2015-05-18 13:06:53 -0700335 size_t failed_auth_attempts = 0;
336
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700337 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700338
339 void update_version(int version, size_t payload);
340 int get_protocol_version() const;
341 size_t get_max_payload() const;
342
Josh Gaof2d6af52021-04-27 20:32:02 -0700343 const FeatureSet& features() const { return features_; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700344
345 bool has_feature(const std::string& feature) const;
David Pursella07dbad2015-09-22 10:43:08 -0700346
347 // Loads the transport's feature set from the given string.
348 void SetFeatures(const std::string& features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700349
Yabin Cui2d4c1982015-08-28 15:09:44 -0700350 void AddDisconnect(adisconnect* disconnect);
351 void RemoveDisconnect(adisconnect* disconnect);
352 void RunDisconnects();
353
Josh Gao65d18e22020-04-22 20:57:26 -0700354#if ADB_HOST
Josh Gao8e7c9722021-06-17 04:19:45 -0700355 bool Attach(std::string* error);
356 bool Detach(std::string* error);
357#endif
358
359#if ADB_HOST
David Pursellc929c6f2016-03-01 08:58:26 -0800360 // 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 Chavezda74b902018-04-17 14:25:04 -0700375 // 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 Gaod24580d2018-08-30 11:37:00 -0700382 // Attempts to reconnect with the underlying Connection.
383 ReconnectResult Reconnect();
Josh Gao65d18e22020-04-22 20:57:26 -0700384#endif
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700385
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700386 private:
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700387 std::atomic<bool> kicked_;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700388
Dan Albertbe8e54b2015-05-18 13:06:53 -0700389 // 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 Cui2d4c1982015-08-28 15:09:44 -0700395 // A list of adisconnect callbacks called when the transport is kicked.
396 std::list<adisconnect*> disconnects_;
397
Yabin Cui3cf1b362017-03-10 16:01:01 -0800398 std::atomic<ConnectionState> connection_state_;
Josh Gaoeac20582016-10-05 19:02:29 -0700399#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700400 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gaoeac20582016-10-05 19:02:29 -0700401#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700402
Josh Gao65d18e22020-04-22 20:57:26 -0700403#if ADB_HOST
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700404 // 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 Gao65d18e22020-04-22 20:57:26 -0700407#endif
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700408
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700409 // The underlying connection object.
410 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
411
Josh Gao6b55e752020-03-27 18:09:56 -0700412#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800413 // USB handle for the connection, if available.
414 usb_handle* usb_handle_ = nullptr;
Josh Gao6b55e752020-03-27 18:09:56 -0700415#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800416
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700417 // A callback that will be invoked when the atransport needs to reconnect.
418 ReconnectCallback reconnect_;
419
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700420 std::mutex mutex_;
421
Dan Albertbe8e54b2015-05-18 13:06:53 -0700422 DISALLOW_COPY_AND_ASSIGN(atransport);
423};
424
Vince Harron5703eb32021-11-12 12:40:58 -0800425// --one-device command line parameter is eventually put here.
426void 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.
429const char* transport_get_one_device();
430
431// Returns true if the adb server owns all devices, or `serial`.
432bool transport_server_owns_device(std::string_view serial);
433
434// Returns true if the adb server owns all devices, `serial`, or `dev_path`.
435bool transport_server_owns_device(std::string_view dev_path, std::string_view serial);
436
Dan Albertb302d122015-02-24 15:51:19 -0800437/*
438 * Obtain a transport from the available transports.
Elliott Hughes67943d12015-10-07 14:55:10 -0700439 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob39e4152017-08-16 16:57:01 -0700440 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes67943d12015-10-07 14:55:10 -0700441 * 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 Albertb302d122015-02-24 15:51:19 -0800444 */
Josh Gaob39e4152017-08-16 16:57:01 -0700445atransport* 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 Gao3a2172b2019-03-28 15:47:44 -0700448void kick_transport(atransport* t, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800449void update_transports(void);
450
Josh Gao1e3bf732017-05-03 22:37:10 -0700451// Iterates across all of the current and pending transports.
452// Stops iteration and returns false if fn returns false, otherwise returns true.
453bool iterate_transports(std::function<bool(const atransport*)> fn);
454
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700455void init_reconnect_handler(void);
Dan Albertb302d122015-02-24 15:51:19 -0800456void init_transport_registration(void);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700457void init_mdns_transport_discovery(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700458std::string list_transports(bool long_listing);
Josh Gao65d18e22020-04-22 20:57:26 -0700459
460#if ADB_HOST
Dan Albertb302d122015-02-24 15:51:19 -0800461atransport* find_transport(const char* serial);
Josh Gao65d18e22020-04-22 20:57:26 -0700462
Yabin Cui4d64fd82015-08-27 12:03:11 -0700463void kick_all_tcp_devices();
Josh Gao65d18e22020-04-22 20:57:26 -0700464#endif
465
Josh Gao165460f2017-05-09 13:43:35 -0700466void kick_all_transports();
Josh Gao65d18e22020-04-22 20:57:26 -0700467
Joshua Duong64fab752020-01-21 13:19:42 -0800468void kick_all_tcp_tls_transports();
Josh Gao65d18e22020-04-22 20:57:26 -0700469
Joshua Duong64fab752020-01-21 13:19:42 -0800470#if !ADB_HOST
471void kick_all_transports_by_auth_key(std::string_view auth_key);
472#endif
Dan Albertb302d122015-02-24 15:51:19 -0800473
Josh Gao3a34bc52018-10-11 16:33:05 -0700474void register_transport(atransport* transport);
Josh Gao6b55e752020-03-27 18:09:56 -0700475
476#if ADB_HOST
477void init_usb_transport(atransport* t, usb_handle* usb);
Josh Gaof2d6af52021-04-27 20:32:02 -0700478
479void register_usb_transport(std::shared_ptr<Connection> connection, const char* serial,
480 const char* devpath, unsigned writeable);
Josh Gao6b55e752020-03-27 18:09:56 -0700481void 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.
485void unregister_usb_transport(usb_handle* usb);
486#endif
Dan Albertb302d122015-02-24 15:51:19 -0800487
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700488/* Connect to a network address and register it as a device */
489void connect_device(const std::string& address, std::string* response);
490
Dan Albertb302d122015-02-24 15:51:19 -0800491/* cause new transports to be init'd and added to the list */
Josh Gao597044d2018-08-08 16:20:14 -0700492bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong64fab752020-01-21 13:19:42 -0800493 atransport::ReconnectCallback reconnect, bool use_tls,
494 int* error = nullptr);
Dan Albertb302d122015-02-24 15:51:19 -0800495
Josh Gao67b683a2017-05-16 15:02:45 -0700496bool check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800497
Josh Gao3a2172b2019-03-28 15:47:44 -0700498void close_usb_devices(bool reset = false);
499void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800500
501void send_packet(apacket* p, atransport* t);
502
Josh Gao32124632017-08-14 18:57:54 -0700503asocket* create_device_tracker(bool long_output);
Dan Albertb302d122015-02-24 15:51:19 -0800504
Josh Gao0560feb2019-01-22 19:36:15 -0800505#if !ADB_HOST
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900506unique_fd adb_listen(std::string_view addr, std::string* error);
507void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
508 std::string_view addr);
Josh Gao0560feb2019-01-22 19:36:15 -0800509#endif
510
Josh Gaof2d6af52021-04-27 20:32:02 -0700511#endif /* __TRANSPORT_H */