blob: b1f27445cbb908575f2edcd0911fd35d9f8e188f [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>
Yurii Zubrytskyi6fc26df2020-03-26 18:19:28 -070033#include <vector>
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
Yurii Zubrytskyi6fc26df2020-03-26 18:19:28 -070043// Even though the feature set is used as a set, we only have a dozen or two
44// of available features at any moment. Vector works much better in terms of
45// both memory usage and performance for these sizes.
46using FeatureSet = std::vector<std::string>;
Dan Albert1792c232015-05-18 13:06:53 -070047
Joshua Duong5cf78682020-01-21 13:19:42 -080048namespace adb {
49namespace tls {
50
51class TlsConnection;
52
53} // namespace tls
54} // namespace adb
55
Dan Albert1792c232015-05-18 13:06:53 -070056const FeatureSet& supported_features();
57
David Pursell4e2fd362015-09-22 10:43:08 -070058// Encodes and decodes FeatureSet objects into human-readable strings.
59std::string FeatureSetToString(const FeatureSet& features);
60FeatureSet StringToFeatureSet(const std::string& features_string);
61
David Pursell70ef7b42015-09-30 13:35:42 -070062// Returns true if both local features and |feature_set| support |feature|.
63bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
64
David Pursell4e2fd362015-09-22 10:43:08 -070065// Do not use any of [:;=,] in feature strings, they have special meaning
66// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080067extern const char* const kFeatureShell2;
68// The 'cmd' command is available
69extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080070extern const char* const kFeatureStat2;
Josh Gao34a478f2019-08-07 14:23:17 -070071extern const char* const kFeatureLs2;
Josh Gao5d1756c2017-02-22 17:07:01 -080072// The server is running with libusb enabled.
73extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070074// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070075extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070076// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010077extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070078// adbd has b/110953234 fixed.
79extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070080// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080081extern const char* const kFeatureAbb;
Alex Buynytskyy7ea92d82019-09-13 14:19:01 -070082// adbd supports abb using raw pipe.
83extern const char* const kFeatureAbbExec;
Josh Gao7b1cb662019-02-20 13:01:40 -080084// adbd properly updates symlink timestamps on push.
85extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao939fc192020-03-04 19:34:08 -080086// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gao8c2198c2019-07-11 14:15:32 -070087extern const char* const kFeatureRemountShell;
Shukang Zhouf4ffae12020-02-13 17:01:39 -080088// adbd supports `track-app` service reporting debuggable/profileable apps.
89extern const char* const kFeatureTrackApp;
Josh Gao939fc192020-03-04 19:34:08 -080090// adbd supports version 2 of send/recv.
91extern const char* const kFeatureSendRecv2;
92// adbd supports brotli for send/recv v2.
93extern const char* const kFeatureSendRecv2Brotli;
Josh Gaoec44d352020-03-26 22:02:03 -070094// adbd supports LZ4 for send/recv v2.
95extern const char* const kFeatureSendRecv2LZ4;
Josh Gao317d3e12020-05-27 17:52:52 -070096// adbd supports Zstd for send/recv v2.
97extern const char* const kFeatureSendRecv2Zstd;
Josh Gao5949fcc2020-03-30 23:25:16 -070098// adbd supports dry-run send for send/recv v2.
99extern const char* const kFeatureSendRecv2DryRunSend;
David Pursell0955c662015-08-31 10:42:13 -0700100
Josh Gaob122b172017-08-16 16:57:01 -0700101TransportId NextTransportId();
102
Josh Gao0bbf69c2018-02-16 13:24:58 -0800103// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -0800104struct Connection {
105 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -0800106 virtual ~Connection() = default;
107
Josh Gao0bbf69c2018-02-16 13:24:58 -0800108 void SetTransportName(std::string transport_name) {
109 transport_name_ = std::move(transport_name);
110 }
111
112 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
113 void SetReadCallback(ReadCallback callback) {
114 CHECK(!read_callback_);
115 read_callback_ = callback;
116 }
117
118 // Called after the Connection has terminated, either by an error or because Stop was called.
119 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
120 void SetErrorCallback(ErrorCallback callback) {
121 CHECK(!error_callback_);
122 error_callback_ = callback;
123 }
124
125 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
126
127 virtual void Start() = 0;
128 virtual void Stop() = 0;
129
Joshua Duong5cf78682020-01-21 13:19:42 -0800130 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
131
Josh Gao3705b342019-03-28 15:47:44 -0700132 // Stop, and reset the device if it's a USB connection.
133 virtual void Reset();
134
Josh Gao0bbf69c2018-02-16 13:24:58 -0800135 std::string transport_name_;
136 ReadCallback read_callback_;
137 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700138
139 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800140};
141
142// Abstraction for a blocking packet transport.
143struct BlockingConnection {
144 BlockingConnection() = default;
145 BlockingConnection(const BlockingConnection& copy) = delete;
146 BlockingConnection(BlockingConnection&& move) = delete;
147
148 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
149 virtual ~BlockingConnection() = default;
150
Josh Gaob800d882018-01-28 20:32:46 -0800151 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
152 // threads.
153 virtual bool Read(apacket* packet) = 0;
154 virtual bool Write(apacket* packet) = 0;
155
Joshua Duong5cf78682020-01-21 13:19:42 -0800156 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
157
Josh Gaob800d882018-01-28 20:32:46 -0800158 // Terminate a connection.
159 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
160 // Formerly known as 'Kick' in atransport.
161 virtual void Close() = 0;
Josh Gao3705b342019-03-28 15:47:44 -0700162
163 // Terminate a connection, and reset it.
164 virtual void Reset() = 0;
Josh Gaob800d882018-01-28 20:32:46 -0800165};
166
Josh Gao0bbf69c2018-02-16 13:24:58 -0800167struct BlockingConnectionAdapter : public Connection {
168 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
169
170 virtual ~BlockingConnectionAdapter();
171
172 virtual bool Write(std::unique_ptr<apacket> packet) override final;
173
174 virtual void Start() override final;
175 virtual void Stop() override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800176 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800177
Josh Gao3705b342019-03-28 15:47:44 -0700178 virtual void Reset() override final;
179
Joshua Duong5cf78682020-01-21 13:19:42 -0800180 private:
181 void StartReadThread() REQUIRES(mutex_);
Josh Gaoc251ec52018-04-03 12:55:18 -0700182 bool started_ GUARDED_BY(mutex_) = false;
183 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800184
185 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700186 std::thread read_thread_ GUARDED_BY(mutex_);
187 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800188
Josh Gaoc251ec52018-04-03 12:55:18 -0700189 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800190 std::mutex mutex_;
191 std::condition_variable cv_;
192
193 std::once_flag error_flag_;
194};
195
196struct FdConnection : public BlockingConnection {
Joshua Duong5cf78682020-01-21 13:19:42 -0800197 explicit FdConnection(unique_fd fd);
198 ~FdConnection();
Josh Gaob800d882018-01-28 20:32:46 -0800199
200 bool Read(apacket* packet) override final;
201 bool Write(apacket* packet) override final;
Joshua Duong5cf78682020-01-21 13:19:42 -0800202 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gaob800d882018-01-28 20:32:46 -0800203
204 void Close() override;
Josh Gao3705b342019-03-28 15:47:44 -0700205 virtual void Reset() override final { Close(); }
Josh Gaob800d882018-01-28 20:32:46 -0800206
207 private:
Joshua Duong5cf78682020-01-21 13:19:42 -0800208 bool DispatchRead(void* buf, size_t len);
209 bool DispatchWrite(void* buf, size_t len);
210
Josh Gaob800d882018-01-28 20:32:46 -0800211 unique_fd fd_;
Joshua Duong5cf78682020-01-21 13:19:42 -0800212 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gaob800d882018-01-28 20:32:46 -0800213};
214
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700215// Waits for a transport's connection to be not pending. This is a separate
216// object so that the transport can be destroyed and another thread can be
217// notified of it in a race-free way.
218class ConnectionWaitable {
219 public:
220 ConnectionWaitable() = default;
221 ~ConnectionWaitable() = default;
222
223 // Waits until the first CNXN packet has been received by the owning
224 // atransport, or the specified timeout has elapsed. Can be called from any
225 // thread.
226 //
227 // Returns true if the CNXN packet was received in a timely fashion, false
228 // otherwise.
229 bool WaitForConnection(std::chrono::milliseconds timeout);
230
231 // Can be called from any thread when the connection stops being pending.
232 // Only the first invocation will be acknowledged, the rest will be no-ops.
233 void SetConnectionEstablished(bool success);
234
235 private:
236 bool connection_established_ GUARDED_BY(mutex_) = false;
237 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
238 std::mutex mutex_;
239 std::condition_variable cv_;
240
241 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
242};
243
Josh Gaofc2e56f2018-08-30 11:37:00 -0700244enum class ReconnectResult {
245 Retry,
246 Success,
247 Abort,
248};
249
Josh Gao08718242020-03-27 18:09:56 -0700250#if ADB_HOST
251struct usb_handle;
252#endif
253
Josh Gao607fd542019-12-09 15:44:57 -0800254class atransport : public enable_weak_from_this<atransport> {
Josh Gaob122b172017-08-16 16:57:01 -0700255 public:
Dan Albert1792c232015-05-18 13:06:53 -0700256 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
257 // historically just a struct, but making the whole thing a more idiomatic
258 // class in one go is a very large change. Given how bad our testing is,
259 // it's better to do this piece by piece.
260
Josh Gaofc2e56f2018-08-30 11:37:00 -0700261 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700262
263 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700264 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700265 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700266 connection_state_(state),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700267 connection_(nullptr),
268 reconnect_(std::move(reconnect)) {
Josh Gao9f3064f2020-04-22 20:57:26 -0700269#if ADB_HOST
270 connection_waitable_ = std::make_shared<ConnectionWaitable>();
271#endif
272
Tim Murrayde471942017-12-07 11:40:00 -0800273 // Initialize protocol to min version for compatibility with older versions.
274 // Version will be updated post-connect.
275 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700276 max_payload = MAX_PAYLOAD;
277 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700278 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700279 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gao607fd542019-12-09 15:44:57 -0800280 ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700281
Yabin Cuib5e11412017-03-10 16:01:01 -0800282 int Write(apacket* p);
Josh Gao3705b342019-03-28 15:47:44 -0700283 void Reset();
Yabin Cui7f274902016-04-18 11:22:34 -0700284 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700285 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700286
Yabin Cuib5e11412017-03-10 16:01:01 -0800287 // ConnectionState can be read by all threads, but can only be written in the main thread.
288 ConnectionState GetConnectionState() const;
289 void SetConnectionState(ConnectionState state);
290
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700291 void SetConnection(std::unique_ptr<Connection> connection);
292 std::shared_ptr<Connection> connection() {
293 std::lock_guard<std::mutex> lock(mutex_);
294 return connection_;
295 }
296
Josh Gao08718242020-03-27 18:09:56 -0700297#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800298 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
299 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao08718242020-03-27 18:09:56 -0700300#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800301
Josh Gaob122b172017-08-16 16:57:01 -0700302 const TransportId id;
Josh Gao9a8366b2019-12-09 13:45:31 -0800303
Dan Albert1792c232015-05-18 13:06:53 -0700304 bool online = false;
305 TransportType type = kTransportAny;
306
Dan Albert1792c232015-05-18 13:06:53 -0700307 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700308 std::string serial;
309 std::string product;
310 std::string model;
311 std::string device;
312 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700313
Joshua Duong5cf78682020-01-21 13:19:42 -0800314 // If this is set, the transport will initiate the connection with a
315 // START_TLS command, instead of AUTH.
316 bool use_tls = false;
317 int tls_version = A_STLS_VERSION;
318 int get_tls_version() const;
319
Josh Gao27523262019-10-22 12:30:39 -0700320#if !ADB_HOST
Michael Groover7eeda6b2019-04-25 18:33:35 -0700321 // Used to provide the key to the framework.
322 std::string auth_key;
Josh Gao27523262019-10-22 12:30:39 -0700323 uint64_t auth_id;
324#endif
Michael Groover7eeda6b2019-04-25 18:33:35 -0700325
Josh Gaob800d882018-01-28 20:32:46 -0800326 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700327
Josh Gao3bd28792016-10-05 19:02:29 -0700328#if ADB_HOST
Joshua Duong5cf78682020-01-21 13:19:42 -0800329 // The current key being authorized.
330 std::shared_ptr<RSA> Key();
Josh Gao2e671202016-08-18 22:00:12 -0700331 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800332 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700333#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700334
Josh Gao06d61d42016-10-06 13:31:44 -0700335 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700336 size_t failed_auth_attempts = 0;
337
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700338 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800339 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700340
341 void update_version(int version, size_t payload);
342 int get_protocol_version() const;
343 size_t get_max_payload() const;
344
David Pursell4e2fd362015-09-22 10:43:08 -0700345 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700346 return features_;
347 }
348
349 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700350
351 // Loads the transport's feature set from the given string.
352 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700353
Yabin Cuib3298242015-08-28 15:09:44 -0700354 void AddDisconnect(adisconnect* disconnect);
355 void RemoveDisconnect(adisconnect* disconnect);
356 void RunDisconnects();
357
Josh Gao9f3064f2020-04-22 20:57:26 -0700358#if ADB_HOST
David Pursell3f902aa2016-03-01 08:58:26 -0800359 // Returns true if |target| matches this transport. A matching |target| can be any of:
360 // * <serial>
361 // * <devpath>
362 // * product:<product>
363 // * model:<model>
364 // * device:<device>
365 //
366 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
367 // For example, serial "100.100.100.100:5555" would match any of:
368 // * 100.100.100.100
369 // * tcp:100.100.100.100
370 // * udp:100.100.100.100:5555
371 // This is to make it easier to use the same network target for both fastboot and adb.
372 bool MatchesTarget(const std::string& target) const;
373
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700374 // Notifies that the atransport is no longer waiting for the connection
375 // being established.
376 void SetConnectionEstablished(bool success);
377
378 // Gets a shared reference to the ConnectionWaitable.
379 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
380
Josh Gaofc2e56f2018-08-30 11:37:00 -0700381 // Attempts to reconnect with the underlying Connection.
382 ReconnectResult Reconnect();
Josh Gao9f3064f2020-04-22 20:57:26 -0700383#endif
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700384
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700385 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700386 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700387
Dan Albert1792c232015-05-18 13:06:53 -0700388 // A set of features transmitted in the banner with the initial connection.
389 // This is stored in the banner as 'features=feature0,feature1,etc'.
390 FeatureSet features_;
391 int protocol_version;
392 size_t max_payload;
393
Yabin Cuib3298242015-08-28 15:09:44 -0700394 // A list of adisconnect callbacks called when the transport is kicked.
395 std::list<adisconnect*> disconnects_;
396
Yabin Cuib5e11412017-03-10 16:01:01 -0800397 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700398#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700399 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700400#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700401
Josh Gao9f3064f2020-04-22 20:57:26 -0700402#if ADB_HOST
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700403 // A sharable object that can be used to wait for the atransport's
404 // connection to be established.
405 std::shared_ptr<ConnectionWaitable> connection_waitable_;
Josh Gao9f3064f2020-04-22 20:57:26 -0700406#endif
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700407
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700408 // The underlying connection object.
409 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
410
Josh Gao08718242020-03-27 18:09:56 -0700411#if ADB_HOST
Josh Gaoce5ce872018-12-11 13:11:52 -0800412 // USB handle for the connection, if available.
413 usb_handle* usb_handle_ = nullptr;
Josh Gao08718242020-03-27 18:09:56 -0700414#endif
Josh Gaoce5ce872018-12-11 13:11:52 -0800415
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700416 // A callback that will be invoked when the atransport needs to reconnect.
417 ReconnectCallback reconnect_;
418
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700419 std::mutex mutex_;
420
Dan Albert1792c232015-05-18 13:06:53 -0700421 DISALLOW_COPY_AND_ASSIGN(atransport);
422};
423
Dan Albert76649012015-02-24 15:51:19 -0800424/*
425 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700426 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700427 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700428 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
429 * is set to true and nullptr returned.
430 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800431 */
Josh Gaob122b172017-08-16 16:57:01 -0700432atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
433 bool* is_ambiguous, std::string* error_out,
434 bool accept_any_state = false);
Josh Gao3705b342019-03-28 15:47:44 -0700435void kick_transport(atransport* t, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800436void update_transports(void);
437
Josh Gaofd713e52017-05-03 22:37:10 -0700438// Iterates across all of the current and pending transports.
439// Stops iteration and returns false if fn returns false, otherwise returns true.
440bool iterate_transports(std::function<bool(const atransport*)> fn);
441
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700442void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800443void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700444void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700445std::string list_transports(bool long_listing);
Josh Gao9f3064f2020-04-22 20:57:26 -0700446
447#if ADB_HOST
Dan Albert76649012015-02-24 15:51:19 -0800448atransport* find_transport(const char* serial);
Josh Gao9f3064f2020-04-22 20:57:26 -0700449
Yabin Cuif4b99282015-08-27 12:03:11 -0700450void kick_all_tcp_devices();
Josh Gao9f3064f2020-04-22 20:57:26 -0700451#endif
452
Josh Gao01b7bc42017-05-09 13:43:35 -0700453void kick_all_transports();
Josh Gao9f3064f2020-04-22 20:57:26 -0700454
Joshua Duong5cf78682020-01-21 13:19:42 -0800455void kick_all_tcp_tls_transports();
Josh Gao9f3064f2020-04-22 20:57:26 -0700456
Joshua Duong5cf78682020-01-21 13:19:42 -0800457#if !ADB_HOST
458void kick_all_transports_by_auth_key(std::string_view auth_key);
459#endif
Dan Albert76649012015-02-24 15:51:19 -0800460
Josh Gaoc51726c2018-10-11 16:33:05 -0700461void register_transport(atransport* transport);
Josh Gao08718242020-03-27 18:09:56 -0700462
463#if ADB_HOST
464void init_usb_transport(atransport* t, usb_handle* usb);
465void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
466 unsigned writeable);
467
468// This should only be used for transports with connection_state == kCsNoPerm.
469void unregister_usb_transport(usb_handle* usb);
470#endif
Dan Albert76649012015-02-24 15:51:19 -0800471
Casey Dahlin13a269e2016-06-23 14:19:37 -0700472/* Connect to a network address and register it as a device */
473void connect_device(const std::string& address, std::string* response);
474
Dan Albert76649012015-02-24 15:51:19 -0800475/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700476bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong5cf78682020-01-21 13:19:42 -0800477 atransport::ReconnectCallback reconnect, bool use_tls,
478 int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800479
Josh Gao36dadca2017-05-16 15:02:45 -0700480bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800481
Josh Gao3705b342019-03-28 15:47:44 -0700482void close_usb_devices(bool reset = false);
483void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albert76649012015-02-24 15:51:19 -0800484
485void send_packet(apacket* p, atransport* t);
486
Josh Gaob0c18022017-08-14 18:57:54 -0700487asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800488
Josh Gao776c2ec2019-01-22 19:36:15 -0800489#if !ADB_HOST
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900490unique_fd adb_listen(std::string_view addr, std::string* error);
491void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
492 std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800493
494#if defined(__ANDROID__)
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900495void qemu_socket_thread(std::string_view addr);
Josh Gao776c2ec2019-01-22 19:36:15 -0800496bool use_qemu_goldfish();
497#endif
498
499#endif
500
JP Abgrall408fa572011-03-16 15:57:42 -0700501#endif /* __TRANSPORT_H */