blob: d59be59c69663398eb1a85c454611698f2f7d0fe [file] [log] [blame]
JP Abgrall408fa572011-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 Alberte9fca142015-02-18 18:03:26 -080020#include <sys/types.h>
21
Yabin Cuib5e11412017-03-10 16:01:01 -080022#include <atomic>
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070023#include <chrono>
Josh Gao0bbf69c2018-02-16 13:24:58 -080024#include <condition_variable>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070025#include <deque>
Josh Gao22d2b3e2016-10-27 14:01:08 -070026#include <functional>
Yabin Cuib3298242015-08-28 15:09:44 -070027#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070028#include <memory>
Yabin Cuib5e11412017-03-10 16:01:01 -080029#include <mutex>
Josh Gaoa5c24c32020-06-24 16:25:49 -070030#include <optional>
Elliott Hughes7be29c82015-04-16 22:54:44 -070031#include <string>
Cody Schuffelena05b64d2019-01-04 18:51:11 -080032#include <string_view>
Josh Gao0bbf69c2018-02-16 13:24:58 -080033#include <thread>
Yurii Zubrytskyi6fc26df2020-03-26 18:19:28 -070034#include <vector>
Dan Albert76649012015-02-24 15:51:19 -080035
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070036#include <android-base/macros.h>
Josh Gaoc251ec52018-04-03 12:55:18 -070037#include <android-base/thread_annotations.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070038#include <openssl/rsa.h>
39
Josh Gaob800d882018-01-28 20:32:46 -080040#include "adb.h"
41#include "adb_unique_fd.h"
Josh Gao607fd542019-12-09 15:44:57 -080042#include "types.h"
Josh Gaob800d882018-01-28 20:32:46 -080043
Yurii Zubrytskyi6fc26df2020-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 Albert1792c232015-05-18 13:06:53 -070048
Joshua Duong5cf78682020-01-21 13:19:42 -080049namespace adb {
50namespace tls {
51
52class TlsConnection;
53
54} // namespace tls
55} // namespace adb
56
Dan Albert1792c232015-05-18 13:06:53 -070057const FeatureSet& supported_features();
58
David Pursell4e2fd362015-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 Pursell70ef7b42015-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 Pursell4e2fd362015-09-22 10:43:08 -070066// Do not use any of [:;=,] in feature strings, they have special meaning
67// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080068extern const char* const kFeatureShell2;
69// The 'cmd' command is available
70extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080071extern const char* const kFeatureStat2;
Josh Gao34a478f2019-08-07 14:23:17 -070072extern const char* const kFeatureLs2;
Josh Gao5d1756c2017-02-22 17:07:01 -080073// The server is running with libusb enabled.
74extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070075// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070076extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070077// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010078extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070079// adbd has b/110953234 fixed.
80extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070081// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080082extern const char* const kFeatureAbb;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070083// adbd supports abb using raw pipe.
84extern const char* const kFeatureAbbExec;
Josh Gao7b1cb662019-02-20 13:01:40 -080085// adbd properly updates symlink timestamps on push.
86extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao939fc192020-03-04 19:34:08 -080087// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gao8c2198c2019-07-11 14:15:32 -070088extern const char* const kFeatureRemountShell;
Shukang Zhouf4ffae12020-02-13 17:01:39 -080089// adbd supports `track-app` service reporting debuggable/profileable apps.
90extern const char* const kFeatureTrackApp;
Josh Gao939fc192020-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 Gaoec44d352020-03-26 22:02:03 -070095// adbd supports LZ4 for send/recv v2.
96extern const char* const kFeatureSendRecv2LZ4;
Josh Gao317d3e12020-05-27 17:52:52 -070097// adbd supports Zstd for send/recv v2.
98extern const char* const kFeatureSendRecv2Zstd;
Josh Gao5949fcc2020-03-30 23:25:16 -070099// adbd supports dry-run send for send/recv v2.
100extern const char* const kFeatureSendRecv2DryRunSend;
David Pursell0955c662015-08-31 10:42:13 -0700101
Josh Gaob122b172017-08-16 16:57:01 -0700102TransportId NextTransportId();
103
Josh Gao0bbf69c2018-02-16 13:24:58 -0800104// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -0800105struct Connection {
106 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -0800107 virtual ~Connection() = default;
108
Josh Gao0bbf69c2018-02-16 13:24:58 -0800109 void SetTransportName(std::string transport_name) {
110 transport_name_ = std::move(transport_name);
111 }
112
113 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
114 void SetReadCallback(ReadCallback callback) {
115 CHECK(!read_callback_);
116 read_callback_ = callback;
117 }
118
119 // Called after the Connection has terminated, either by an error or because Stop was called.
120 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
121 void SetErrorCallback(ErrorCallback callback) {
122 CHECK(!error_callback_);
123 error_callback_ = callback;
124 }
125
126 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
127
128 virtual void Start() = 0;
129 virtual void Stop() = 0;
130
Joshua Duong5cf78682020-01-21 13:19:42 -0800131 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
132
Josh Gao3705b342019-03-28 15:47:44 -0700133 // Stop, and reset the device if it's a USB connection.
134 virtual void Reset();
135
Josh Gao0bbf69c2018-02-16 13:24:58 -0800136 std::string transport_name_;
137 ReadCallback read_callback_;
138 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700139
140 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800141};
142
143// Abstraction for a blocking packet transport.
144struct BlockingConnection {
145 BlockingConnection() = default;
146 BlockingConnection(const BlockingConnection& copy) = delete;
147 BlockingConnection(BlockingConnection&& move) = delete;
148
149 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
150 virtual ~BlockingConnection() = default;
151
Josh Gaob800d882018-01-28 20:32:46 -0800152 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
153 // threads.
154 virtual bool Read(apacket* packet) = 0;
155 virtual bool Write(apacket* packet) = 0;
156
Joshua Duong5cf78682020-01-21 13:19:42 -0800157 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
158
Josh Gaob800d882018-01-28 20:32:46 -0800159 // Terminate a connection.
160 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
161 // Formerly known as 'Kick' in atransport.
162 virtual void Close() = 0;
Josh Gao3705b342019-03-28 15:47:44 -0700163
164 // Terminate a connection, and reset it.
165 virtual void Reset() = 0;
Josh Gaob800d882018-01-28 20:32:46 -0800166};
167
Josh Gao0bbf69c2018-02-16 13:24:58 -0800168struct BlockingConnectionAdapter : public Connection {
169 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
170
171 virtual ~BlockingConnectionAdapter();
172
173 virtual bool Write(std::unique_ptr<apacket> packet) override final;
174
175 virtual void Start() override final;
176 virtual void Stop() override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800177 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800178
Josh Gao3705b342019-03-28 15:47:44 -0700179 virtual void Reset() override final;
180
Joshua Duong5cf78682020-01-21 13:19:42 -0800181 private:
182 void StartReadThread() REQUIRES(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700183 bool started_ GUARDED_BY(mutex_) = false;
184 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800185
186 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700187 std::thread read_thread_ GUARDED_BY(mutex_);
188 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800189
Josh Gaoc251ec52018-04-03 12:55:18 -0700190 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800191 std::mutex mutex_;
192 std::condition_variable cv_;
193
194 std::once_flag error_flag_;
195};
196
197struct FdConnection : public BlockingConnection {
Joshua Duong5cf78682020-01-21 13:19:42 -0800198 explicit FdConnection(unique_fd fd);
199 ~FdConnection();
Josh Gaob800d882018-01-28 20:32:46 -0800200
201 bool Read(apacket* packet) override final;
202 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800203 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800204
205 void Close() override;
Josh Gao3705b342019-03-28 15:47:44 -0700206 virtual void Reset() override final { Close(); }
Josh Gaob800d882018-01-28 20:32:46 -0800207
208 private:
Joshua Duong5cf78682020-01-21 13:19:42 -0800209 bool DispatchRead(void* buf, size_t len);
210 bool DispatchWrite(void* buf, size_t len);
211
Josh Gaob800d882018-01-28 20:32:46 -0800212 unique_fd fd_;
Joshua Duong5cf78682020-01-21 13:19:42 -0800213 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gaob800d882018-01-28 20:32:46 -0800214};
215
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700216// Waits for a transport's connection to be not pending. This is a separate
217// object so that the transport can be destroyed and another thread can be
218// notified of it in a race-free way.
219class ConnectionWaitable {
220 public:
221 ConnectionWaitable() = default;
222 ~ConnectionWaitable() = default;
223
224 // Waits until the first CNXN packet has been received by the owning
225 // atransport, or the specified timeout has elapsed. Can be called from any
226 // thread.
227 //
228 // Returns true if the CNXN packet was received in a timely fashion, false
229 // otherwise.
230 bool WaitForConnection(std::chrono::milliseconds timeout);
231
232 // Can be called from any thread when the connection stops being pending.
233 // Only the first invocation will be acknowledged, the rest will be no-ops.
234 void SetConnectionEstablished(bool success);
235
236 private:
237 bool connection_established_ GUARDED_BY(mutex_) = false;
238 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
239 std::mutex mutex_;
240 std::condition_variable cv_;
241
242 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
243};
244
Josh Gaofc2e56f2018-08-30 11:37:00 -0700245enum class ReconnectResult {
246 Retry,
247 Success,
248 Abort,
249};
250
Josh Gao08718242020-03-27 18:09:56 -0700251#if ADB_HOST
252struct usb_handle;
253#endif
254
Josh Gao607fd542019-12-09 15:44:57 -0800255class atransport : public enable_weak_from_this<atransport> {
Josh Gaob122b172017-08-16 16:57:01 -0700256 public:
Dan Albert1792c232015-05-18 13:06:53 -0700257 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
258 // historically just a struct, but making the whole thing a more idiomatic
259 // class in one go is a very large change. Given how bad our testing is,
260 // it's better to do this piece by piece.
261
Josh Gaofc2e56f2018-08-30 11:37:00 -0700262 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700263
264 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700265 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700266 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700267 connection_state_(state),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700268 connection_(nullptr),
269 reconnect_(std::move(reconnect)) {
Josh Gao9f3064f2020-04-22 20:57:26 -0700270#if ADB_HOST
271 connection_waitable_ = std::make_shared<ConnectionWaitable>();
272#endif
273
Tim Murrayde471942017-12-07 11:40:00 -0800274 // Initialize protocol to min version for compatibility with older versions.
275 // Version will be updated post-connect.
276 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700277 max_payload = MAX_PAYLOAD;
278 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700279 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700280 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gao607fd542019-12-09 15:44:57 -0800281 ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700282
Yabin Cuib5e11412017-03-10 16:01:01 -0800283 int Write(apacket* p);
Josh Gao3705b342019-03-28 15:47:44 -0700284 void Reset();
Yabin Cui7f274902016-04-18 11:22:34 -0700285 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700286 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700287
Yabin Cuib5e11412017-03-10 16:01:01 -0800288 // ConnectionState can be read by all threads, but can only be written in the main thread.
289 ConnectionState GetConnectionState() const;
290 void SetConnectionState(ConnectionState state);
291
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700292 void SetConnection(std::unique_ptr<Connection> connection);
293 std::shared_ptr<Connection> connection() {
294 std::lock_guard<std::mutex> lock(mutex_);
295 return connection_;
296 }
297
Josh Gao08718242020-03-27 18:09:56 -0700298#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800299 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
300 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao08718242020-03-27 18:09:56 -0700301#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800302
Josh Gaob122b172017-08-16 16:57:01 -0700303 const TransportId id;
Josh Gao9a8366b2019-12-09 13:45:31 -0800304
Dan Albert1792c232015-05-18 13:06:53 -0700305 bool online = false;
306 TransportType type = kTransportAny;
307
Dan Albert1792c232015-05-18 13:06:53 -0700308 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700309 std::string serial;
310 std::string product;
311 std::string model;
312 std::string device;
313 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700314
Joshua Duong5cf78682020-01-21 13:19:42 -0800315 // If this is set, the transport will initiate the connection with a
316 // START_TLS command, instead of AUTH.
317 bool use_tls = false;
318 int tls_version = A_STLS_VERSION;
319 int get_tls_version() const;
320
Josh Gao27523262019-10-22 12:30:39 -0700321#if !ADB_HOST
Michael Groover7eeda6b2019-04-25 18:33:35 -0700322 // Used to provide the key to the framework.
323 std::string auth_key;
Josh Gaoa5c24c32020-06-24 16:25:49 -0700324 std::optional<uint64_t> auth_id;
Josh Gao27523262019-10-22 12:30:39 -0700325#endif
Michael Groover7eeda6b2019-04-25 18:33:35 -0700326
Josh Gaob800d882018-01-28 20:32:46 -0800327 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700328
Josh Gao3bd28792016-10-05 19:02:29 -0700329#if ADB_HOST
Joshua Duong5cf78682020-01-21 13:19:42 -0800330 // The current key being authorized.
331 std::shared_ptr<RSA> Key();
Josh Gao2e671202016-08-18 22:00:12 -0700332 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800333 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700334#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700335
Josh Gao06d61d42016-10-06 13:31:44 -0700336 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700337 size_t failed_auth_attempts = 0;
338
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700339 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800340 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700341
342 void update_version(int version, size_t payload);
343 int get_protocol_version() const;
344 size_t get_max_payload() const;
345
David Pursell4e2fd362015-09-22 10:43:08 -0700346 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700347 return features_;
348 }
349
350 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700351
352 // Loads the transport's feature set from the given string.
353 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700354
Yabin Cuib3298242015-08-28 15:09:44 -0700355 void AddDisconnect(adisconnect* disconnect);
356 void RemoveDisconnect(adisconnect* disconnect);
357 void RunDisconnects();
358
Josh Gao9f3064f2020-04-22 20:57:26 -0700359#if ADB_HOST
David Pursell3f902aa2016-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 Chavez56fe7532018-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 Gaofc2e56f2018-08-30 11:37:00 -0700382 // Attempts to reconnect with the underlying Connection.
383 ReconnectResult Reconnect();
Josh Gao9f3064f2020-04-22 20:57:26 -0700384#endif
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700385
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700386 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700387 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700388
Dan Albert1792c232015-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 Cuib3298242015-08-28 15:09:44 -0700395 // A list of adisconnect callbacks called when the transport is kicked.
396 std::list<adisconnect*> disconnects_;
397
Yabin Cuib5e11412017-03-10 16:01:01 -0800398 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700399#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700400 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700401#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700402
Josh Gao9f3064f2020-04-22 20:57:26 -0700403#if ADB_HOST
Luis Hector Chavez56fe7532018-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 Gao9f3064f2020-04-22 20:57:26 -0700407#endif
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700408
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700409 // The underlying connection object.
410 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
411
Josh Gao08718242020-03-27 18:09:56 -0700412#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800413 // USB handle for the connection, if available.
414 usb_handle* usb_handle_ = nullptr;
Josh Gao08718242020-03-27 18:09:56 -0700415#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800416
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700417 // A callback that will be invoked when the atransport needs to reconnect.
418 ReconnectCallback reconnect_;
419
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700420 std::mutex mutex_;
421
Dan Albert1792c232015-05-18 13:06:53 -0700422 DISALLOW_COPY_AND_ASSIGN(atransport);
423};
424
Dan Albert76649012015-02-24 15:51:19 -0800425/*
426 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700427 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700428 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700429 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
430 * is set to true and nullptr returned.
431 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800432 */
Josh Gaob122b172017-08-16 16:57:01 -0700433atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
434 bool* is_ambiguous, std::string* error_out,
435 bool accept_any_state = false);
Josh Gao3705b342019-03-28 15:47:44 -0700436void kick_transport(atransport* t, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800437void update_transports(void);
438
Josh Gaofd713e52017-05-03 22:37:10 -0700439// Iterates across all of the current and pending transports.
440// Stops iteration and returns false if fn returns false, otherwise returns true.
441bool iterate_transports(std::function<bool(const atransport*)> fn);
442
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700443void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800444void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700445void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700446std::string list_transports(bool long_listing);
Josh Gao9f3064f2020-04-22 20:57:26 -0700447
448#if ADB_HOST
Dan Albert76649012015-02-24 15:51:19 -0800449atransport* find_transport(const char* serial);
Josh Gao9f3064f2020-04-22 20:57:26 -0700450
Yabin Cuif4b99282015-08-27 12:03:11 -0700451void kick_all_tcp_devices();
Josh Gao9f3064f2020-04-22 20:57:26 -0700452#endif
453
Josh Gao01b7bc42017-05-09 13:43:35 -0700454void kick_all_transports();
Josh Gao9f3064f2020-04-22 20:57:26 -0700455
Joshua Duong5cf78682020-01-21 13:19:42 -0800456void kick_all_tcp_tls_transports();
Josh Gao9f3064f2020-04-22 20:57:26 -0700457
Joshua Duong5cf78682020-01-21 13:19:42 -0800458#if !ADB_HOST
459void kick_all_transports_by_auth_key(std::string_view auth_key);
460#endif
Dan Albert76649012015-02-24 15:51:19 -0800461
Josh Gaoc51726c2018-10-11 16:33:05 -0700462void register_transport(atransport* transport);
Josh Gao08718242020-03-27 18:09:56 -0700463
464#if ADB_HOST
465void init_usb_transport(atransport* t, usb_handle* usb);
466void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
467 unsigned writeable);
468
469// This should only be used for transports with connection_state == kCsNoPerm.
470void unregister_usb_transport(usb_handle* usb);
471#endif
Dan Albert76649012015-02-24 15:51:19 -0800472
Casey Dahlin13a269e2016-06-23 14:19:37 -0700473/* Connect to a network address and register it as a device */
474void connect_device(const std::string& address, std::string* response);
475
Dan Albert76649012015-02-24 15:51:19 -0800476/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700477bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong5cf78682020-01-21 13:19:42 -0800478 atransport::ReconnectCallback reconnect, bool use_tls,
479 int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800480
Josh Gao36dadca2017-05-16 15:02:45 -0700481bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800482
Josh Gao3705b342019-03-28 15:47:44 -0700483void close_usb_devices(bool reset = false);
484void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800485
486void send_packet(apacket* p, atransport* t);
487
Josh Gaob0c18022017-08-14 18:57:54 -0700488asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800489
Josh Gao776c2ec2019-01-22 19:36:15 -0800490#if !ADB_HOST
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900491unique_fd adb_listen(std::string_view addr, std::string* error);
492void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
493 std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800494
495#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900496void qemu_socket_thread(std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800497bool use_qemu_goldfish();
498#endif
499
500#endif
501
JP Abgrall408fa572011-03-16 15:57:42 -0700502#endif /* __TRANSPORT_H */