blob: 26d804b3f0b09e20e911fc05344de9b50d07dc84 [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>
Dan Albert1792c232015-05-18 13:06:53 -070034#include <unordered_set>
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
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 Gao50ab0a62020-03-04 19:34:08 -080084// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gao8c2198c2019-07-11 14:15:32 -070085extern const char* const kFeatureRemountShell;
Josh Gao50ab0a62020-03-04 19:34:08 -080086// adbd supports version 2 of send/recv.
87extern const char* const kFeatureSendRecv2;
88// adbd supports brotli for send/recv v2.
89extern const char* const kFeatureSendRecv2Brotli;
David Pursell0955c662015-08-31 10:42:13 -070090
Josh Gaob122b172017-08-16 16:57:01 -070091TransportId NextTransportId();
92
Josh Gao0bbf69c2018-02-16 13:24:58 -080093// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080094struct Connection {
95 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080096 virtual ~Connection() = default;
97
Josh Gao0bbf69c2018-02-16 13:24:58 -080098 void SetTransportName(std::string transport_name) {
99 transport_name_ = std::move(transport_name);
100 }
101
102 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
103 void SetReadCallback(ReadCallback callback) {
104 CHECK(!read_callback_);
105 read_callback_ = callback;
106 }
107
108 // Called after the Connection has terminated, either by an error or because Stop was called.
109 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
110 void SetErrorCallback(ErrorCallback callback) {
111 CHECK(!error_callback_);
112 error_callback_ = callback;
113 }
114
115 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
116
117 virtual void Start() = 0;
118 virtual void Stop() = 0;
119
Joshua Duong5cf78682020-01-21 13:19:42 -0800120 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
121
Josh Gao3705b342019-03-28 15:47:44 -0700122 // Stop, and reset the device if it's a USB connection.
123 virtual void Reset();
124
Josh Gao0bbf69c2018-02-16 13:24:58 -0800125 std::string transport_name_;
126 ReadCallback read_callback_;
127 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700128
129 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800130};
131
132// Abstraction for a blocking packet transport.
133struct BlockingConnection {
134 BlockingConnection() = default;
135 BlockingConnection(const BlockingConnection& copy) = delete;
136 BlockingConnection(BlockingConnection&& move) = delete;
137
138 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
139 virtual ~BlockingConnection() = default;
140
Josh Gaob800d882018-01-28 20:32:46 -0800141 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
142 // threads.
143 virtual bool Read(apacket* packet) = 0;
144 virtual bool Write(apacket* packet) = 0;
145
Joshua Duong5cf78682020-01-21 13:19:42 -0800146 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
147
Josh Gaob800d882018-01-28 20:32:46 -0800148 // Terminate a connection.
149 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
150 // Formerly known as 'Kick' in atransport.
151 virtual void Close() = 0;
Josh Gao3705b342019-03-28 15:47:44 -0700152
153 // Terminate a connection, and reset it.
154 virtual void Reset() = 0;
Josh Gaob800d882018-01-28 20:32:46 -0800155};
156
Josh Gao0bbf69c2018-02-16 13:24:58 -0800157struct BlockingConnectionAdapter : public Connection {
158 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
159
160 virtual ~BlockingConnectionAdapter();
161
162 virtual bool Write(std::unique_ptr<apacket> packet) override final;
163
164 virtual void Start() override final;
165 virtual void Stop() override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800166 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800167
Josh Gao3705b342019-03-28 15:47:44 -0700168 virtual void Reset() override final;
169
Joshua Duong5cf78682020-01-21 13:19:42 -0800170 private:
171 void StartReadThread() REQUIRES(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700172 bool started_ GUARDED_BY(mutex_) = false;
173 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800174
175 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700176 std::thread read_thread_ GUARDED_BY(mutex_);
177 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800178
Josh Gaoc251ec52018-04-03 12:55:18 -0700179 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800180 std::mutex mutex_;
181 std::condition_variable cv_;
182
183 std::once_flag error_flag_;
184};
185
186struct FdConnection : public BlockingConnection {
Joshua Duong5cf78682020-01-21 13:19:42 -0800187 explicit FdConnection(unique_fd fd);
188 ~FdConnection();
Josh Gaob800d882018-01-28 20:32:46 -0800189
190 bool Read(apacket* packet) override final;
191 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800192 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800193
194 void Close() override;
Josh Gao3705b342019-03-28 15:47:44 -0700195 virtual void Reset() override final { Close(); }
Josh Gaob800d882018-01-28 20:32:46 -0800196
197 private:
Joshua Duong5cf78682020-01-21 13:19:42 -0800198 bool DispatchRead(void* buf, size_t len);
199 bool DispatchWrite(void* buf, size_t len);
200
Josh Gaob800d882018-01-28 20:32:46 -0800201 unique_fd fd_;
Joshua Duong5cf78682020-01-21 13:19:42 -0800202 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gaob800d882018-01-28 20:32:46 -0800203};
204
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700205// Waits for a transport's connection to be not pending. This is a separate
206// object so that the transport can be destroyed and another thread can be
207// notified of it in a race-free way.
208class ConnectionWaitable {
209 public:
210 ConnectionWaitable() = default;
211 ~ConnectionWaitable() = default;
212
213 // Waits until the first CNXN packet has been received by the owning
214 // atransport, or the specified timeout has elapsed. Can be called from any
215 // thread.
216 //
217 // Returns true if the CNXN packet was received in a timely fashion, false
218 // otherwise.
219 bool WaitForConnection(std::chrono::milliseconds timeout);
220
221 // Can be called from any thread when the connection stops being pending.
222 // Only the first invocation will be acknowledged, the rest will be no-ops.
223 void SetConnectionEstablished(bool success);
224
225 private:
226 bool connection_established_ GUARDED_BY(mutex_) = false;
227 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
228 std::mutex mutex_;
229 std::condition_variable cv_;
230
231 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
232};
233
Josh Gaofc2e56f2018-08-30 11:37:00 -0700234enum class ReconnectResult {
235 Retry,
236 Success,
237 Abort,
238};
239
Josh Gao27831222020-03-27 18:09:56 -0700240#if ADB_HOST
241struct usb_handle;
242#endif
243
Josh Gao607fd542019-12-09 15:44:57 -0800244class atransport : public enable_weak_from_this<atransport> {
Josh Gaob122b172017-08-16 16:57:01 -0700245 public:
Dan Albert1792c232015-05-18 13:06:53 -0700246 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
247 // historically just a struct, but making the whole thing a more idiomatic
248 // class in one go is a very large change. Given how bad our testing is,
249 // it's better to do this piece by piece.
250
Josh Gaofc2e56f2018-08-30 11:37:00 -0700251 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700252
253 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700254 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700255 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700256 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700257 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700258 connection_(nullptr),
259 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800260 // Initialize protocol to min version for compatibility with older versions.
261 // Version will be updated post-connect.
262 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700263 max_payload = MAX_PAYLOAD;
264 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700265 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700266 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gao607fd542019-12-09 15:44:57 -0800267 ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700268
Yabin Cuib5e11412017-03-10 16:01:01 -0800269 int Write(apacket* p);
Josh Gao3705b342019-03-28 15:47:44 -0700270 void Reset();
Yabin Cui7f274902016-04-18 11:22:34 -0700271 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700272 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700273
Yabin Cuib5e11412017-03-10 16:01:01 -0800274 // ConnectionState can be read by all threads, but can only be written in the main thread.
275 ConnectionState GetConnectionState() const;
276 void SetConnectionState(ConnectionState state);
277
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700278 void SetConnection(std::unique_ptr<Connection> connection);
279 std::shared_ptr<Connection> connection() {
280 std::lock_guard<std::mutex> lock(mutex_);
281 return connection_;
282 }
283
Josh Gao27831222020-03-27 18:09:56 -0700284#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800285 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
286 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao27831222020-03-27 18:09:56 -0700287#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800288
Josh Gaob122b172017-08-16 16:57:01 -0700289 const TransportId id;
Josh Gao9a8366b2019-12-09 13:45:31 -0800290
Dan Albert1792c232015-05-18 13:06:53 -0700291 bool online = false;
292 TransportType type = kTransportAny;
293
Dan Albert1792c232015-05-18 13:06:53 -0700294 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700295 std::string serial;
296 std::string product;
297 std::string model;
298 std::string device;
299 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700300
Joshua Duong5cf78682020-01-21 13:19:42 -0800301 // If this is set, the transport will initiate the connection with a
302 // START_TLS command, instead of AUTH.
303 bool use_tls = false;
304 int tls_version = A_STLS_VERSION;
305 int get_tls_version() const;
306
Josh Gao27523262019-10-22 12:30:39 -0700307#if !ADB_HOST
Michael Groover7eeda6b2019-04-25 18:33:35 -0700308 // Used to provide the key to the framework.
309 std::string auth_key;
Josh Gaoa5c24c32020-06-24 16:25:49 -0700310 std::optional<uint64_t> auth_id;
Josh Gao27523262019-10-22 12:30:39 -0700311#endif
Michael Groover7eeda6b2019-04-25 18:33:35 -0700312
Josh Gaob800d882018-01-28 20:32:46 -0800313 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700314
Josh Gao3bd28792016-10-05 19:02:29 -0700315#if ADB_HOST
Joshua Duong5cf78682020-01-21 13:19:42 -0800316 // The current key being authorized.
317 std::shared_ptr<RSA> Key();
Josh Gao2e671202016-08-18 22:00:12 -0700318 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800319 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700320#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700321
Josh Gao06d61d42016-10-06 13:31:44 -0700322 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700323 size_t failed_auth_attempts = 0;
324
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700325 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800326 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700327
328 void update_version(int version, size_t payload);
329 int get_protocol_version() const;
330 size_t get_max_payload() const;
331
David Pursell4e2fd362015-09-22 10:43:08 -0700332 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700333 return features_;
334 }
335
336 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700337
338 // Loads the transport's feature set from the given string.
339 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700340
Yabin Cuib3298242015-08-28 15:09:44 -0700341 void AddDisconnect(adisconnect* disconnect);
342 void RemoveDisconnect(adisconnect* disconnect);
343 void RunDisconnects();
344
David Pursell3f902aa2016-03-01 08:58:26 -0800345 // Returns true if |target| matches this transport. A matching |target| can be any of:
346 // * <serial>
347 // * <devpath>
348 // * product:<product>
349 // * model:<model>
350 // * device:<device>
351 //
352 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
353 // For example, serial "100.100.100.100:5555" would match any of:
354 // * 100.100.100.100
355 // * tcp:100.100.100.100
356 // * udp:100.100.100.100:5555
357 // This is to make it easier to use the same network target for both fastboot and adb.
358 bool MatchesTarget(const std::string& target) const;
359
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700360 // Notifies that the atransport is no longer waiting for the connection
361 // being established.
362 void SetConnectionEstablished(bool success);
363
364 // Gets a shared reference to the ConnectionWaitable.
365 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
366
Josh Gaofc2e56f2018-08-30 11:37:00 -0700367 // Attempts to reconnect with the underlying Connection.
368 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700369
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700370 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700371 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700372
Dan Albert1792c232015-05-18 13:06:53 -0700373 // A set of features transmitted in the banner with the initial connection.
374 // This is stored in the banner as 'features=feature0,feature1,etc'.
375 FeatureSet features_;
376 int protocol_version;
377 size_t max_payload;
378
Yabin Cuib3298242015-08-28 15:09:44 -0700379 // A list of adisconnect callbacks called when the transport is kicked.
380 std::list<adisconnect*> disconnects_;
381
Yabin Cuib5e11412017-03-10 16:01:01 -0800382 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700383#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700384 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700385#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700386
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700387 // A sharable object that can be used to wait for the atransport's
388 // connection to be established.
389 std::shared_ptr<ConnectionWaitable> connection_waitable_;
390
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700391 // The underlying connection object.
392 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
393
Josh Gao27831222020-03-27 18:09:56 -0700394#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800395 // USB handle for the connection, if available.
396 usb_handle* usb_handle_ = nullptr;
Josh Gao27831222020-03-27 18:09:56 -0700397#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800398
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700399 // A callback that will be invoked when the atransport needs to reconnect.
400 ReconnectCallback reconnect_;
401
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700402 std::mutex mutex_;
403
Dan Albert1792c232015-05-18 13:06:53 -0700404 DISALLOW_COPY_AND_ASSIGN(atransport);
405};
406
Dan Albert76649012015-02-24 15:51:19 -0800407/*
408 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700409 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700410 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700411 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
412 * is set to true and nullptr returned.
413 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800414 */
Josh Gaob122b172017-08-16 16:57:01 -0700415atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
416 bool* is_ambiguous, std::string* error_out,
417 bool accept_any_state = false);
Josh Gao3705b342019-03-28 15:47:44 -0700418void kick_transport(atransport* t, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800419void update_transports(void);
420
Josh Gaofd713e52017-05-03 22:37:10 -0700421// Iterates across all of the current and pending transports.
422// Stops iteration and returns false if fn returns false, otherwise returns true.
423bool iterate_transports(std::function<bool(const atransport*)> fn);
424
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700425void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800426void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700427void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700428std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800429atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700430void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700431void kick_all_transports();
Joshua Duong5cf78682020-01-21 13:19:42 -0800432void kick_all_tcp_tls_transports();
433#if !ADB_HOST
434void kick_all_transports_by_auth_key(std::string_view auth_key);
435#endif
Dan Albert76649012015-02-24 15:51:19 -0800436
Josh Gaoc51726c2018-10-11 16:33:05 -0700437void register_transport(atransport* transport);
Josh Gao27831222020-03-27 18:09:56 -0700438
439#if ADB_HOST
440void init_usb_transport(atransport* t, usb_handle* usb);
441void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
442 unsigned writeable);
443
444// This should only be used for transports with connection_state == kCsNoPerm.
445void unregister_usb_transport(usb_handle* usb);
446#endif
Dan Albert76649012015-02-24 15:51:19 -0800447
Casey Dahlin13a269e2016-06-23 14:19:37 -0700448/* Connect to a network address and register it as a device */
449void connect_device(const std::string& address, std::string* response);
450
Dan Albert76649012015-02-24 15:51:19 -0800451/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700452bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong5cf78682020-01-21 13:19:42 -0800453 atransport::ReconnectCallback reconnect, bool use_tls,
454 int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800455
Josh Gao36dadca2017-05-16 15:02:45 -0700456bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800457
Josh Gao3705b342019-03-28 15:47:44 -0700458void close_usb_devices(bool reset = false);
459void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800460
461void send_packet(apacket* p, atransport* t);
462
Josh Gaob0c18022017-08-14 18:57:54 -0700463asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800464
Josh Gao776c2ec2019-01-22 19:36:15 -0800465#if !ADB_HOST
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900466unique_fd adb_listen(std::string_view addr, std::string* error);
467void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
468 std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800469
470#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900471void qemu_socket_thread(std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800472bool use_qemu_goldfish();
473#endif
474
475#endif
476
JP Abgrall408fa572011-03-16 15:57:42 -0700477#endif /* __TRANSPORT_H */