blob: b1984db304d4a5ee0257a7206ae3613c3c655ca2 [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 Gaob800d882018-01-28 20:32:46 -080042
Dan Albert1792c232015-05-18 13:06:53 -070043typedef std::unordered_set<std::string> FeatureSet;
44
Joshua Duong5cf78682020-01-21 13:19:42 -080045namespace adb {
46namespace tls {
47
48class TlsConnection;
49
50} // namespace tls
51} // namespace adb
52
Dan Albert1792c232015-05-18 13:06:53 -070053const FeatureSet& supported_features();
54
David Pursell4e2fd362015-09-22 10:43:08 -070055// Encodes and decodes FeatureSet objects into human-readable strings.
56std::string FeatureSetToString(const FeatureSet& features);
57FeatureSet StringToFeatureSet(const std::string& features_string);
58
David Pursell70ef7b42015-09-30 13:35:42 -070059// Returns true if both local features and |feature_set| support |feature|.
60bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
61
David Pursell4e2fd362015-09-22 10:43:08 -070062// Do not use any of [:;=,] in feature strings, they have special meaning
63// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080064extern const char* const kFeatureShell2;
65// The 'cmd' command is available
66extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080067extern const char* const kFeatureStat2;
Josh Gao34a478f2019-08-07 14:23:17 -070068extern const char* const kFeatureLs2;
Josh Gao5d1756c2017-02-22 17:07:01 -080069// The server is running with libusb enabled.
70extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070071// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070072extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070073// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010074extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070075// adbd has b/110953234 fixed.
76extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070077// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080078extern const char* const kFeatureAbb;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070079// adbd supports abb using raw pipe.
80extern const char* const kFeatureAbbExec;
Josh Gao7b1cb662019-02-20 13:01:40 -080081// adbd properly updates symlink timestamps on push.
82extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao939fc192020-03-04 19:34:08 -080083// Implement `adb remount` via shelling out to /system/bin/remount.
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;
Josh Gao939fc192020-03-04 19:34:08 -080087// adbd supports version 2 of send/recv.
88extern const char* const kFeatureSendRecv2;
89// adbd supports brotli for send/recv v2.
90extern const char* const kFeatureSendRecv2Brotli;
David Pursell0955c662015-08-31 10:42:13 -070091
Josh Gaob122b172017-08-16 16:57:01 -070092TransportId NextTransportId();
93
Josh Gao0bbf69c2018-02-16 13:24:58 -080094// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080095struct Connection {
96 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080097 virtual ~Connection() = default;
98
Josh Gao0bbf69c2018-02-16 13:24:58 -080099 void SetTransportName(std::string transport_name) {
100 transport_name_ = std::move(transport_name);
101 }
102
103 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
104 void SetReadCallback(ReadCallback callback) {
105 CHECK(!read_callback_);
106 read_callback_ = callback;
107 }
108
109 // Called after the Connection has terminated, either by an error or because Stop was called.
110 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
111 void SetErrorCallback(ErrorCallback callback) {
112 CHECK(!error_callback_);
113 error_callback_ = callback;
114 }
115
116 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
117
118 virtual void Start() = 0;
119 virtual void Stop() = 0;
120
Joshua Duong5cf78682020-01-21 13:19:42 -0800121 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
122
Josh Gao3705b342019-03-28 15:47:44 -0700123 // Stop, and reset the device if it's a USB connection.
124 virtual void Reset();
125
Josh Gao0bbf69c2018-02-16 13:24:58 -0800126 std::string transport_name_;
127 ReadCallback read_callback_;
128 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700129
130 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800131};
132
133// Abstraction for a blocking packet transport.
134struct BlockingConnection {
135 BlockingConnection() = default;
136 BlockingConnection(const BlockingConnection& copy) = delete;
137 BlockingConnection(BlockingConnection&& move) = delete;
138
139 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
140 virtual ~BlockingConnection() = default;
141
Josh Gaob800d882018-01-28 20:32:46 -0800142 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
143 // threads.
144 virtual bool Read(apacket* packet) = 0;
145 virtual bool Write(apacket* packet) = 0;
146
Joshua Duong5cf78682020-01-21 13:19:42 -0800147 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
148
Josh Gaob800d882018-01-28 20:32:46 -0800149 // Terminate a connection.
150 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
151 // Formerly known as 'Kick' in atransport.
152 virtual void Close() = 0;
Josh Gao3705b342019-03-28 15:47:44 -0700153
154 // Terminate a connection, and reset it.
155 virtual void Reset() = 0;
Josh Gaob800d882018-01-28 20:32:46 -0800156};
157
Josh Gao0bbf69c2018-02-16 13:24:58 -0800158struct BlockingConnectionAdapter : public Connection {
159 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
160
161 virtual ~BlockingConnectionAdapter();
162
163 virtual bool Write(std::unique_ptr<apacket> packet) override final;
164
165 virtual void Start() override final;
166 virtual void Stop() override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800167 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800168
Josh Gao3705b342019-03-28 15:47:44 -0700169 virtual void Reset() override final;
170
Joshua Duong5cf78682020-01-21 13:19:42 -0800171 private:
172 void StartReadThread() REQUIRES(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700173 bool started_ GUARDED_BY(mutex_) = false;
174 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800175
176 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700177 std::thread read_thread_ GUARDED_BY(mutex_);
178 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800179
Josh Gaoc251ec52018-04-03 12:55:18 -0700180 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800181 std::mutex mutex_;
182 std::condition_variable cv_;
183
184 std::once_flag error_flag_;
185};
186
187struct FdConnection : public BlockingConnection {
Joshua Duong5cf78682020-01-21 13:19:42 -0800188 explicit FdConnection(unique_fd fd);
189 ~FdConnection();
Josh Gaob800d882018-01-28 20:32:46 -0800190
191 bool Read(apacket* packet) override final;
192 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800193 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800194
195 void Close() override;
Josh Gao3705b342019-03-28 15:47:44 -0700196 virtual void Reset() override final { Close(); }
Josh Gaob800d882018-01-28 20:32:46 -0800197
198 private:
Joshua Duong5cf78682020-01-21 13:19:42 -0800199 bool DispatchRead(void* buf, size_t len);
200 bool DispatchWrite(void* buf, size_t len);
201
Josh Gaob800d882018-01-28 20:32:46 -0800202 unique_fd fd_;
Joshua Duong5cf78682020-01-21 13:19:42 -0800203 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gaob800d882018-01-28 20:32:46 -0800204};
205
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700206// Waits for a transport's connection to be not pending. This is a separate
207// object so that the transport can be destroyed and another thread can be
208// notified of it in a race-free way.
209class ConnectionWaitable {
210 public:
211 ConnectionWaitable() = default;
212 ~ConnectionWaitable() = default;
213
214 // Waits until the first CNXN packet has been received by the owning
215 // atransport, or the specified timeout has elapsed. Can be called from any
216 // thread.
217 //
218 // Returns true if the CNXN packet was received in a timely fashion, false
219 // otherwise.
220 bool WaitForConnection(std::chrono::milliseconds timeout);
221
222 // Can be called from any thread when the connection stops being pending.
223 // Only the first invocation will be acknowledged, the rest will be no-ops.
224 void SetConnectionEstablished(bool success);
225
226 private:
227 bool connection_established_ GUARDED_BY(mutex_) = false;
228 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
229 std::mutex mutex_;
230 std::condition_variable cv_;
231
232 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
233};
234
Josh Gaofc2e56f2018-08-30 11:37:00 -0700235enum class ReconnectResult {
236 Retry,
237 Success,
238 Abort,
239};
240
Josh Gao08718242020-03-27 18:09:56 -0700241#if ADB_HOST
242struct usb_handle;
243#endif
244
Josh Gao607fd542019-12-09 15:44:57 -0800245class atransport : public enable_weak_from_this<atransport> {
Josh Gaob122b172017-08-16 16:57:01 -0700246 public:
Dan Albert1792c232015-05-18 13:06:53 -0700247 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
248 // historically just a struct, but making the whole thing a more idiomatic
249 // class in one go is a very large change. Given how bad our testing is,
250 // it's better to do this piece by piece.
251
Josh Gaofc2e56f2018-08-30 11:37:00 -0700252 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700253
254 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700255 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700256 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700257 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700258 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700259 connection_(nullptr),
260 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800261 // Initialize protocol to min version for compatibility with older versions.
262 // Version will be updated post-connect.
263 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700264 max_payload = MAX_PAYLOAD;
265 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700266 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700267 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gao607fd542019-12-09 15:44:57 -0800268 ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700269
Yabin Cuib5e11412017-03-10 16:01:01 -0800270 int Write(apacket* p);
Josh Gao3705b342019-03-28 15:47:44 -0700271 void Reset();
Yabin Cui7f274902016-04-18 11:22:34 -0700272 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700273 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700274
Yabin Cuib5e11412017-03-10 16:01:01 -0800275 // ConnectionState can be read by all threads, but can only be written in the main thread.
276 ConnectionState GetConnectionState() const;
277 void SetConnectionState(ConnectionState state);
278
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700279 void SetConnection(std::unique_ptr<Connection> connection);
280 std::shared_ptr<Connection> connection() {
281 std::lock_guard<std::mutex> lock(mutex_);
282 return connection_;
283 }
284
Josh Gao08718242020-03-27 18:09:56 -0700285#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800286 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
287 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao08718242020-03-27 18:09:56 -0700288#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800289
Josh Gaob122b172017-08-16 16:57:01 -0700290 const TransportId id;
Josh Gao9a8366b2019-12-09 13:45:31 -0800291
Dan Albert1792c232015-05-18 13:06:53 -0700292 bool online = false;
293 TransportType type = kTransportAny;
294
Dan Albert1792c232015-05-18 13:06:53 -0700295 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700296 std::string serial;
297 std::string product;
298 std::string model;
299 std::string device;
300 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700301
Joshua Duong5cf78682020-01-21 13:19:42 -0800302 // If this is set, the transport will initiate the connection with a
303 // START_TLS command, instead of AUTH.
304 bool use_tls = false;
305 int tls_version = A_STLS_VERSION;
306 int get_tls_version() const;
307
Josh Gao27523262019-10-22 12:30:39 -0700308#if !ADB_HOST
Michael Groover7eeda6b2019-04-25 18:33:35 -0700309 // Used to provide the key to the framework.
310 std::string auth_key;
Josh Gao27523262019-10-22 12:30:39 -0700311 uint64_t auth_id;
312#endif
Michael Groover7eeda6b2019-04-25 18:33:35 -0700313
Josh Gaob800d882018-01-28 20:32:46 -0800314 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700315
Josh Gao3bd28792016-10-05 19:02:29 -0700316#if ADB_HOST
Joshua Duong5cf78682020-01-21 13:19:42 -0800317 // The current key being authorized.
318 std::shared_ptr<RSA> Key();
Josh Gao2e671202016-08-18 22:00:12 -0700319 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800320 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700321#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700322
Josh Gao06d61d42016-10-06 13:31:44 -0700323 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700324 size_t failed_auth_attempts = 0;
325
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700326 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800327 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700328
329 void update_version(int version, size_t payload);
330 int get_protocol_version() const;
331 size_t get_max_payload() const;
332
David Pursell4e2fd362015-09-22 10:43:08 -0700333 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700334 return features_;
335 }
336
337 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700338
339 // Loads the transport's feature set from the given string.
340 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700341
Yabin Cuib3298242015-08-28 15:09:44 -0700342 void AddDisconnect(adisconnect* disconnect);
343 void RemoveDisconnect(adisconnect* disconnect);
344 void RunDisconnects();
345
David Pursell3f902aa2016-03-01 08:58:26 -0800346 // Returns true if |target| matches this transport. A matching |target| can be any of:
347 // * <serial>
348 // * <devpath>
349 // * product:<product>
350 // * model:<model>
351 // * device:<device>
352 //
353 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
354 // For example, serial "100.100.100.100:5555" would match any of:
355 // * 100.100.100.100
356 // * tcp:100.100.100.100
357 // * udp:100.100.100.100:5555
358 // This is to make it easier to use the same network target for both fastboot and adb.
359 bool MatchesTarget(const std::string& target) const;
360
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700361 // Notifies that the atransport is no longer waiting for the connection
362 // being established.
363 void SetConnectionEstablished(bool success);
364
365 // Gets a shared reference to the ConnectionWaitable.
366 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
367
Josh Gaofc2e56f2018-08-30 11:37:00 -0700368 // Attempts to reconnect with the underlying Connection.
369 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700370
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700371 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700372 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700373
Dan Albert1792c232015-05-18 13:06:53 -0700374 // A set of features transmitted in the banner with the initial connection.
375 // This is stored in the banner as 'features=feature0,feature1,etc'.
376 FeatureSet features_;
377 int protocol_version;
378 size_t max_payload;
379
Yabin Cuib3298242015-08-28 15:09:44 -0700380 // A list of adisconnect callbacks called when the transport is kicked.
381 std::list<adisconnect*> disconnects_;
382
Yabin Cuib5e11412017-03-10 16:01:01 -0800383 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700384#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700385 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700386#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700387
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700388 // A sharable object that can be used to wait for the atransport's
389 // connection to be established.
390 std::shared_ptr<ConnectionWaitable> connection_waitable_;
391
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700392 // The underlying connection object.
393 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
394
Josh Gao08718242020-03-27 18:09:56 -0700395#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800396 // USB handle for the connection, if available.
397 usb_handle* usb_handle_ = nullptr;
Josh Gao08718242020-03-27 18:09:56 -0700398#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800399
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700400 // A callback that will be invoked when the atransport needs to reconnect.
401 ReconnectCallback reconnect_;
402
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700403 std::mutex mutex_;
404
Dan Albert1792c232015-05-18 13:06:53 -0700405 DISALLOW_COPY_AND_ASSIGN(atransport);
406};
407
Dan Albert76649012015-02-24 15:51:19 -0800408/*
409 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700410 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700411 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700412 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
413 * is set to true and nullptr returned.
414 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800415 */
Josh Gaob122b172017-08-16 16:57:01 -0700416atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
417 bool* is_ambiguous, std::string* error_out,
418 bool accept_any_state = false);
Josh Gao3705b342019-03-28 15:47:44 -0700419void kick_transport(atransport* t, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800420void update_transports(void);
421
Josh Gaofd713e52017-05-03 22:37:10 -0700422// Iterates across all of the current and pending transports.
423// Stops iteration and returns false if fn returns false, otherwise returns true.
424bool iterate_transports(std::function<bool(const atransport*)> fn);
425
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700426void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800427void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700428void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700429std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800430atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700431void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700432void kick_all_transports();
Joshua Duong5cf78682020-01-21 13:19:42 -0800433void kick_all_tcp_tls_transports();
434#if !ADB_HOST
435void kick_all_transports_by_auth_key(std::string_view auth_key);
436#endif
Dan Albert76649012015-02-24 15:51:19 -0800437
Josh Gaoc51726c2018-10-11 16:33:05 -0700438void register_transport(atransport* transport);
Josh Gao08718242020-03-27 18:09:56 -0700439
440#if ADB_HOST
441void init_usb_transport(atransport* t, usb_handle* usb);
442void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
443 unsigned writeable);
444
445// This should only be used for transports with connection_state == kCsNoPerm.
446void unregister_usb_transport(usb_handle* usb);
447#endif
Dan Albert76649012015-02-24 15:51:19 -0800448
Casey Dahlin13a269e2016-06-23 14:19:37 -0700449/* Connect to a network address and register it as a device */
450void connect_device(const std::string& address, std::string* response);
451
Dan Albert76649012015-02-24 15:51:19 -0800452/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700453bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong5cf78682020-01-21 13:19:42 -0800454 atransport::ReconnectCallback reconnect, bool use_tls,
455 int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800456
Josh Gao36dadca2017-05-16 15:02:45 -0700457bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800458
Josh Gao3705b342019-03-28 15:47:44 -0700459void close_usb_devices(bool reset = false);
460void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800461
462void send_packet(apacket* p, atransport* t);
463
Josh Gaob0c18022017-08-14 18:57:54 -0700464asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800465
Josh Gao776c2ec2019-01-22 19:36:15 -0800466#if !ADB_HOST
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900467unique_fd adb_listen(std::string_view addr, std::string* error);
468void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
469 std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800470
471#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900472void qemu_socket_thread(std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800473bool use_qemu_goldfish();
474#endif
475
476#endif
477
JP Abgrall408fa572011-03-16 15:57:42 -0700478#endif /* __TRANSPORT_H */