blob: a62349e63af7946691607660d91a002d36bb35db [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>
Elliott Hughes7be29c82015-04-16 22:54:44 -070030#include <string>
Cody Schuffelena05b64d2019-01-04 18:51:11 -080031#include <string_view>
Josh Gao0bbf69c2018-02-16 13:24:58 -080032#include <thread>
Dan Albert1792c232015-05-18 13:06:53 -070033#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080034
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070035#include <android-base/macros.h>
Josh Gaoc251ec52018-04-03 12:55:18 -070036#include <android-base/thread_annotations.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070037#include <openssl/rsa.h>
38
Josh Gaob800d882018-01-28 20:32:46 -080039#include "adb.h"
40#include "adb_unique_fd.h"
Josh Gao607fd542019-12-09 15:44:57 -080041#include "types.h"
Josh Gaoce5ce872018-12-11 13:11:52 -080042#include "usb.h"
Josh Gaob800d882018-01-28 20:32:46 -080043
Dan Albert1792c232015-05-18 13:06:53 -070044typedef std::unordered_set<std::string> FeatureSet;
45
Joshua Duong5cf78682020-01-21 13:19:42 -080046namespace adb {
47namespace tls {
48
49class TlsConnection;
50
51} // namespace tls
52} // namespace adb
53
Dan Albert1792c232015-05-18 13:06:53 -070054const FeatureSet& supported_features();
55
David Pursell4e2fd362015-09-22 10:43:08 -070056// Encodes and decodes FeatureSet objects into human-readable strings.
57std::string FeatureSetToString(const FeatureSet& features);
58FeatureSet StringToFeatureSet(const std::string& features_string);
59
David Pursell70ef7b42015-09-30 13:35:42 -070060// Returns true if both local features and |feature_set| support |feature|.
61bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
62
David Pursell4e2fd362015-09-22 10:43:08 -070063// Do not use any of [:;=,] in feature strings, they have special meaning
64// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080065extern const char* const kFeatureShell2;
66// The 'cmd' command is available
67extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080068extern const char* const kFeatureStat2;
Josh Gao34a478f2019-08-07 14:23:17 -070069extern const char* const kFeatureLs2;
Josh Gao5d1756c2017-02-22 17:07:01 -080070// The server is running with libusb enabled.
71extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070072// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070073extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070074// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010075extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070076// adbd has b/110953234 fixed.
77extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070078// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080079extern const char* const kFeatureAbb;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070080// adbd supports abb using raw pipe.
81extern const char* const kFeatureAbbExec;
Josh Gao7b1cb662019-02-20 13:01:40 -080082// adbd properly updates symlink timestamps on push.
83extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao8c2198c2019-07-11 14:15:32 -070084extern const char* const kFeatureRemountShell;
Shukang Zhouf4ffae12020-02-13 17:01:39 -080085// adbd supports `track-app` service reporting debuggable/profileable apps.
86extern const char* const kFeatureTrackApp;
David Pursell0955c662015-08-31 10:42:13 -070087
Josh Gaob122b172017-08-16 16:57:01 -070088TransportId NextTransportId();
89
Josh Gao0bbf69c2018-02-16 13:24:58 -080090// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080091struct Connection {
92 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080093 virtual ~Connection() = default;
94
Josh Gao0bbf69c2018-02-16 13:24:58 -080095 void SetTransportName(std::string transport_name) {
96 transport_name_ = std::move(transport_name);
97 }
98
99 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
100 void SetReadCallback(ReadCallback callback) {
101 CHECK(!read_callback_);
102 read_callback_ = callback;
103 }
104
105 // Called after the Connection has terminated, either by an error or because Stop was called.
106 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
107 void SetErrorCallback(ErrorCallback callback) {
108 CHECK(!error_callback_);
109 error_callback_ = callback;
110 }
111
112 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
113
114 virtual void Start() = 0;
115 virtual void Stop() = 0;
116
Joshua Duong5cf78682020-01-21 13:19:42 -0800117 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
118
Josh Gao3705b342019-03-28 15:47:44 -0700119 // Stop, and reset the device if it's a USB connection.
120 virtual void Reset();
121
Josh Gao0bbf69c2018-02-16 13:24:58 -0800122 std::string transport_name_;
123 ReadCallback read_callback_;
124 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700125
126 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800127};
128
129// Abstraction for a blocking packet transport.
130struct BlockingConnection {
131 BlockingConnection() = default;
132 BlockingConnection(const BlockingConnection& copy) = delete;
133 BlockingConnection(BlockingConnection&& move) = delete;
134
135 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
136 virtual ~BlockingConnection() = default;
137
Josh Gaob800d882018-01-28 20:32:46 -0800138 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
139 // threads.
140 virtual bool Read(apacket* packet) = 0;
141 virtual bool Write(apacket* packet) = 0;
142
Joshua Duong5cf78682020-01-21 13:19:42 -0800143 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
144
Josh Gaob800d882018-01-28 20:32:46 -0800145 // Terminate a connection.
146 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
147 // Formerly known as 'Kick' in atransport.
148 virtual void Close() = 0;
Josh Gao3705b342019-03-28 15:47:44 -0700149
150 // Terminate a connection, and reset it.
151 virtual void Reset() = 0;
Josh Gaob800d882018-01-28 20:32:46 -0800152};
153
Josh Gao0bbf69c2018-02-16 13:24:58 -0800154struct BlockingConnectionAdapter : public Connection {
155 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
156
157 virtual ~BlockingConnectionAdapter();
158
159 virtual bool Write(std::unique_ptr<apacket> packet) override final;
160
161 virtual void Start() override final;
162 virtual void Stop() override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800163 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800164
Josh Gao3705b342019-03-28 15:47:44 -0700165 virtual void Reset() override final;
166
Joshua Duong5cf78682020-01-21 13:19:42 -0800167 private:
168 void StartReadThread() REQUIRES(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700169 bool started_ GUARDED_BY(mutex_) = false;
170 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800171
172 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700173 std::thread read_thread_ GUARDED_BY(mutex_);
174 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800175
Josh Gaoc251ec52018-04-03 12:55:18 -0700176 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800177 std::mutex mutex_;
178 std::condition_variable cv_;
179
180 std::once_flag error_flag_;
181};
182
183struct FdConnection : public BlockingConnection {
Joshua Duong5cf78682020-01-21 13:19:42 -0800184 explicit FdConnection(unique_fd fd);
185 ~FdConnection();
Josh Gaob800d882018-01-28 20:32:46 -0800186
187 bool Read(apacket* packet) override final;
188 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800189 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800190
191 void Close() override;
Josh Gao3705b342019-03-28 15:47:44 -0700192 virtual void Reset() override final { Close(); }
Josh Gaob800d882018-01-28 20:32:46 -0800193
194 private:
Joshua Duong5cf78682020-01-21 13:19:42 -0800195 bool DispatchRead(void* buf, size_t len);
196 bool DispatchWrite(void* buf, size_t len);
197
Josh Gaob800d882018-01-28 20:32:46 -0800198 unique_fd fd_;
Joshua Duong5cf78682020-01-21 13:19:42 -0800199 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gaob800d882018-01-28 20:32:46 -0800200};
201
Josh Gao0bbf69c2018-02-16 13:24:58 -0800202struct UsbConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800203 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
204 ~UsbConnection();
205
206 bool Read(apacket* packet) override final;
207 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800208 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800209
210 void Close() override final;
Josh Gao3705b342019-03-28 15:47:44 -0700211 virtual void Reset() override final;
Josh Gaob800d882018-01-28 20:32:46 -0800212
213 usb_handle* handle_;
214};
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 Gao607fd542019-12-09 15:44:57 -0800251class atransport : public enable_weak_from_this<atransport> {
Josh Gaob122b172017-08-16 16:57:01 -0700252 public:
Dan Albert1792c232015-05-18 13:06:53 -0700253 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
254 // historically just a struct, but making the whole thing a more idiomatic
255 // class in one go is a very large change. Given how bad our testing is,
256 // it's better to do this piece by piece.
257
Josh Gaofc2e56f2018-08-30 11:37:00 -0700258 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700259
260 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700261 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700262 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700263 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700264 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700265 connection_(nullptr),
266 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800267 // Initialize protocol to min version for compatibility with older versions.
268 // Version will be updated post-connect.
269 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700270 max_payload = MAX_PAYLOAD;
271 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700272 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700273 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gao607fd542019-12-09 15:44:57 -0800274 ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700275
Yabin Cuib5e11412017-03-10 16:01:01 -0800276 int Write(apacket* p);
Josh Gao3705b342019-03-28 15:47:44 -0700277 void Reset();
Yabin Cui7f274902016-04-18 11:22:34 -0700278 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700279 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700280
Yabin Cuib5e11412017-03-10 16:01:01 -0800281 // ConnectionState can be read by all threads, but can only be written in the main thread.
282 ConnectionState GetConnectionState() const;
283 void SetConnectionState(ConnectionState state);
284
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700285 void SetConnection(std::unique_ptr<Connection> connection);
286 std::shared_ptr<Connection> connection() {
287 std::lock_guard<std::mutex> lock(mutex_);
288 return connection_;
289 }
290
Josh Gaoce5ce872018-12-11 13:11:52 -0800291 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
292 usb_handle* GetUsbHandle() { return usb_handle_; }
293
Josh Gaob122b172017-08-16 16:57:01 -0700294 const TransportId id;
Josh Gao9a8366b2019-12-09 13:45:31 -0800295
Dan Albert1792c232015-05-18 13:06:53 -0700296 bool online = false;
297 TransportType type = kTransportAny;
298
Dan Albert1792c232015-05-18 13:06:53 -0700299 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700300 std::string serial;
301 std::string product;
302 std::string model;
303 std::string device;
304 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700305
Joshua Duong5cf78682020-01-21 13:19:42 -0800306 // If this is set, the transport will initiate the connection with a
307 // START_TLS command, instead of AUTH.
308 bool use_tls = false;
309 int tls_version = A_STLS_VERSION;
310 int get_tls_version() const;
311
Josh Gao27523262019-10-22 12:30:39 -0700312#if !ADB_HOST
Michael Groover7eeda6b2019-04-25 18:33:35 -0700313 // Used to provide the key to the framework.
314 std::string auth_key;
Josh Gao27523262019-10-22 12:30:39 -0700315 uint64_t auth_id;
316#endif
Michael Groover7eeda6b2019-04-25 18:33:35 -0700317
Josh Gaob800d882018-01-28 20:32:46 -0800318 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700319
Josh Gao3bd28792016-10-05 19:02:29 -0700320#if ADB_HOST
Joshua Duong5cf78682020-01-21 13:19:42 -0800321 // The current key being authorized.
322 std::shared_ptr<RSA> Key();
Josh Gao2e671202016-08-18 22:00:12 -0700323 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800324 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700325#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700326
Josh Gao06d61d42016-10-06 13:31:44 -0700327 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700328 size_t failed_auth_attempts = 0;
329
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700330 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800331 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700332
333 void update_version(int version, size_t payload);
334 int get_protocol_version() const;
335 size_t get_max_payload() const;
336
David Pursell4e2fd362015-09-22 10:43:08 -0700337 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700338 return features_;
339 }
340
341 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700342
343 // Loads the transport's feature set from the given string.
344 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700345
Yabin Cuib3298242015-08-28 15:09:44 -0700346 void AddDisconnect(adisconnect* disconnect);
347 void RemoveDisconnect(adisconnect* disconnect);
348 void RunDisconnects();
349
David Pursell3f902aa2016-03-01 08:58:26 -0800350 // Returns true if |target| matches this transport. A matching |target| can be any of:
351 // * <serial>
352 // * <devpath>
353 // * product:<product>
354 // * model:<model>
355 // * device:<device>
356 //
357 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
358 // For example, serial "100.100.100.100:5555" would match any of:
359 // * 100.100.100.100
360 // * tcp:100.100.100.100
361 // * udp:100.100.100.100:5555
362 // This is to make it easier to use the same network target for both fastboot and adb.
363 bool MatchesTarget(const std::string& target) const;
364
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700365 // Notifies that the atransport is no longer waiting for the connection
366 // being established.
367 void SetConnectionEstablished(bool success);
368
369 // Gets a shared reference to the ConnectionWaitable.
370 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
371
Josh Gaofc2e56f2018-08-30 11:37:00 -0700372 // Attempts to reconnect with the underlying Connection.
373 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700374
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700375 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700376 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700377
Dan Albert1792c232015-05-18 13:06:53 -0700378 // A set of features transmitted in the banner with the initial connection.
379 // This is stored in the banner as 'features=feature0,feature1,etc'.
380 FeatureSet features_;
381 int protocol_version;
382 size_t max_payload;
383
Yabin Cuib3298242015-08-28 15:09:44 -0700384 // A list of adisconnect callbacks called when the transport is kicked.
385 std::list<adisconnect*> disconnects_;
386
Yabin Cuib5e11412017-03-10 16:01:01 -0800387 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700388#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700389 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700390#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700391
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700392 // A sharable object that can be used to wait for the atransport's
393 // connection to be established.
394 std::shared_ptr<ConnectionWaitable> connection_waitable_;
395
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700396 // The underlying connection object.
397 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
398
Josh Gaoce5ce872018-12-11 13:11:52 -0800399 // USB handle for the connection, if available.
400 usb_handle* usb_handle_ = nullptr;
401
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700402 // A callback that will be invoked when the atransport needs to reconnect.
403 ReconnectCallback reconnect_;
404
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700405 std::mutex mutex_;
406
Dan Albert1792c232015-05-18 13:06:53 -0700407 DISALLOW_COPY_AND_ASSIGN(atransport);
408};
409
Dan Albert76649012015-02-24 15:51:19 -0800410/*
411 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700412 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700413 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700414 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
415 * is set to true and nullptr returned.
416 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800417 */
Josh Gaob122b172017-08-16 16:57:01 -0700418atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
419 bool* is_ambiguous, std::string* error_out,
420 bool accept_any_state = false);
Josh Gao3705b342019-03-28 15:47:44 -0700421void kick_transport(atransport* t, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800422void update_transports(void);
423
Josh Gaofd713e52017-05-03 22:37:10 -0700424// Iterates across all of the current and pending transports.
425// Stops iteration and returns false if fn returns false, otherwise returns true.
426bool iterate_transports(std::function<bool(const atransport*)> fn);
427
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700428void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800429void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700430void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700431std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800432atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700433void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700434void kick_all_transports();
Joshua Duong5cf78682020-01-21 13:19:42 -0800435void kick_all_tcp_tls_transports();
436#if !ADB_HOST
437void kick_all_transports_by_auth_key(std::string_view auth_key);
438#endif
Dan Albert76649012015-02-24 15:51:19 -0800439
Josh Gaoc51726c2018-10-11 16:33:05 -0700440void register_transport(atransport* transport);
Dan Albert76649012015-02-24 15:51:19 -0800441void register_usb_transport(usb_handle* h, const char* serial,
442 const char* devpath, unsigned writeable);
443
Casey Dahlin13a269e2016-06-23 14:19:37 -0700444/* Connect to a network address and register it as a device */
445void connect_device(const std::string& address, std::string* response);
446
Dan Albert76649012015-02-24 15:51:19 -0800447/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700448bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong5cf78682020-01-21 13:19:42 -0800449 atransport::ReconnectCallback reconnect, bool use_tls,
450 int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800451
Dan Albertdcd78a12015-05-18 16:43:57 -0700452// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800453void unregister_usb_transport(usb_handle* usb);
454
Josh Gao36dadca2017-05-16 15:02:45 -0700455bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800456
Josh Gao3705b342019-03-28 15:47:44 -0700457void close_usb_devices(bool reset = false);
458void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800459
460void send_packet(apacket* p, atransport* t);
461
Josh Gaob0c18022017-08-14 18:57:54 -0700462asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800463
Josh Gao776c2ec2019-01-22 19:36:15 -0800464#if !ADB_HOST
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900465unique_fd adb_listen(std::string_view addr, std::string* error);
466void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
467 std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800468
469#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900470void qemu_socket_thread(std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800471bool use_qemu_goldfish();
472#endif
473
474#endif
475
JP Abgrall408fa572011-03-16 15:57:42 -0700476#endif /* __TRANSPORT_H */