blob: e93c31cbc0207293111d6bddd5232a639d07a6e1 [file] [log] [blame]
JP Abgrall2e5dd6e2011-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 Albert020292b2015-02-18 18:03:26 -080020#include <sys/types.h>
21
Yabin Cui3cf1b362017-03-10 16:01:01 -080022#include <atomic>
Luis Hector Chavezda74b902018-04-17 14:25:04 -070023#include <chrono>
Josh Gao715fe602018-02-16 13:24:58 -080024#include <condition_variable>
Elliott Hughes801066a2016-06-29 17:42:01 -070025#include <deque>
Josh Gao4e562502016-10-27 14:01:08 -070026#include <functional>
Yabin Cui2d4c1982015-08-28 15:09:44 -070027#include <list>
Josh Gao22cb70b2016-08-18 22:00:12 -070028#include <memory>
Yabin Cui3cf1b362017-03-10 16:01:01 -080029#include <mutex>
Elliott Hughesab882422015-04-16 22:54:44 -070030#include <string>
Cody Schuffelen637aaf52019-01-04 18:51:11 -080031#include <string_view>
Josh Gao715fe602018-02-16 13:24:58 -080032#include <thread>
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070033#include <vector>
Dan Albertb302d122015-02-24 15:51:19 -080034
Luis Hector Chavezda74b902018-04-17 14:25:04 -070035#include <android-base/macros.h>
Josh Gao13781e82018-04-03 12:55:18 -070036#include <android-base/thread_annotations.h>
Elliott Hughes801066a2016-06-29 17:42:01 -070037#include <openssl/rsa.h>
38
Josh Gao395b86a2018-01-28 20:32:46 -080039#include "adb.h"
40#include "adb_unique_fd.h"
Josh Gaoe490fbb2019-12-09 15:44:57 -080041#include "types.h"
Josh Gao395b86a2018-01-28 20:32:46 -080042
Yurii Zubrytskyi816c2d22020-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 Albertbe8e54b2015-05-18 13:06:53 -070047
Joshua Duong64fab752020-01-21 13:19:42 -080048namespace adb {
49namespace tls {
50
51class TlsConnection;
52
53} // namespace tls
54} // namespace adb
55
Dan Albertbe8e54b2015-05-18 13:06:53 -070056const FeatureSet& supported_features();
57
David Pursella07dbad2015-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 Pursell22fc5e92015-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 Pursella07dbad2015-09-22 10:43:08 -070065// Do not use any of [:;=,] in feature strings, they have special meaning
66// in the connection banner.
Todd Kennedy1e2f7dc2015-11-03 16:53:08 -080067extern const char* const kFeatureShell2;
68// The 'cmd' command is available
69extern const char* const kFeatureCmd;
Josh Gaoa2cf3752016-12-05 17:11:34 -080070extern const char* const kFeatureStat2;
Josh Gao7b083072019-08-07 14:23:17 -070071extern const char* const kFeatureLs2;
Josh Gao210b63f2017-02-22 17:07:01 -080072// The server is running with libusb enabled.
73extern const char* const kFeatureLibusb;
Josh Gao281aab72018-10-22 13:00:05 -070074// adbd supports `push --sync`.
Dan Albert27983bc2017-05-23 14:30:00 -070075extern const char* const kFeaturePushSync;
Josh Gao281aab72018-10-22 13:00:05 -070076// adbd supports installing .apex packages.
Dario Frenidcb4c362018-10-04 16:26:40 +010077extern const char* const kFeatureApex;
Josh Gao281aab72018-10-22 13:00:05 -070078// adbd has b/110953234 fixed.
79extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070080// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyye1fa8142019-01-17 13:13:56 -080081extern const char* const kFeatureAbb;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070082// adbd supports abb using raw pipe.
83extern const char* const kFeatureAbbExec;
Josh Gao9eeb9f72019-02-20 13:01:40 -080084// adbd properly updates symlink timestamps on push.
85extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao2f0f9eb2020-03-04 19:34:08 -080086// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gaof764d572019-07-11 14:15:32 -070087extern const char* const kFeatureRemountShell;
Shukang Zhou420ad552020-02-13 17:01:39 -080088// adbd supports `track-app` service reporting debuggable/profileable apps.
89extern const char* const kFeatureTrackApp;
Josh Gao2f0f9eb2020-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 Gaofb386cc2020-03-26 22:02:03 -070094// adbd supports LZ4 for send/recv v2.
95extern const char* const kFeatureSendRecv2LZ4;
Josh Gao8a410a02020-03-30 23:25:16 -070096// adbd supports dry-run send for send/recv v2.
97extern const char* const kFeatureSendRecv2DryRunSend;
David Pursell8da19a42015-08-31 10:42:13 -070098
Josh Gaob39e4152017-08-16 16:57:01 -070099TransportId NextTransportId();
100
Josh Gao715fe602018-02-16 13:24:58 -0800101// Abstraction for a non-blocking packet transport.
Josh Gao395b86a2018-01-28 20:32:46 -0800102struct Connection {
103 Connection() = default;
Josh Gao395b86a2018-01-28 20:32:46 -0800104 virtual ~Connection() = default;
105
Josh Gao715fe602018-02-16 13:24:58 -0800106 void SetTransportName(std::string transport_name) {
107 transport_name_ = std::move(transport_name);
108 }
109
110 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
111 void SetReadCallback(ReadCallback callback) {
112 CHECK(!read_callback_);
113 read_callback_ = callback;
114 }
115
116 // Called after the Connection has terminated, either by an error or because Stop was called.
117 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
118 void SetErrorCallback(ErrorCallback callback) {
119 CHECK(!error_callback_);
120 error_callback_ = callback;
121 }
122
123 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
124
125 virtual void Start() = 0;
126 virtual void Stop() = 0;
127
Joshua Duong64fab752020-01-21 13:19:42 -0800128 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
129
Josh Gao3a2172b2019-03-28 15:47:44 -0700130 // Stop, and reset the device if it's a USB connection.
131 virtual void Reset();
132
Josh Gao715fe602018-02-16 13:24:58 -0800133 std::string transport_name_;
134 ReadCallback read_callback_;
135 ErrorCallback error_callback_;
Josh Gao1e41fda2018-04-05 16:16:04 -0700136
137 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao715fe602018-02-16 13:24:58 -0800138};
139
140// Abstraction for a blocking packet transport.
141struct BlockingConnection {
142 BlockingConnection() = default;
143 BlockingConnection(const BlockingConnection& copy) = delete;
144 BlockingConnection(BlockingConnection&& move) = delete;
145
146 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
147 virtual ~BlockingConnection() = default;
148
Josh Gao395b86a2018-01-28 20:32:46 -0800149 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
150 // threads.
151 virtual bool Read(apacket* packet) = 0;
152 virtual bool Write(apacket* packet) = 0;
153
Joshua Duong64fab752020-01-21 13:19:42 -0800154 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
155
Josh Gao395b86a2018-01-28 20:32:46 -0800156 // Terminate a connection.
157 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
158 // Formerly known as 'Kick' in atransport.
159 virtual void Close() = 0;
Josh Gao3a2172b2019-03-28 15:47:44 -0700160
161 // Terminate a connection, and reset it.
162 virtual void Reset() = 0;
Josh Gao395b86a2018-01-28 20:32:46 -0800163};
164
Josh Gao715fe602018-02-16 13:24:58 -0800165struct BlockingConnectionAdapter : public Connection {
166 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
167
168 virtual ~BlockingConnectionAdapter();
169
170 virtual bool Write(std::unique_ptr<apacket> packet) override final;
171
172 virtual void Start() override final;
173 virtual void Stop() override final;
Joshua Duong64fab752020-01-21 13:19:42 -0800174 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao715fe602018-02-16 13:24:58 -0800175
Josh Gao3a2172b2019-03-28 15:47:44 -0700176 virtual void Reset() override final;
177
Joshua Duong64fab752020-01-21 13:19:42 -0800178 private:
179 void StartReadThread() REQUIRES(mutex_);
Josh Gao13781e82018-04-03 12:55:18 -0700180 bool started_ GUARDED_BY(mutex_) = false;
181 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao715fe602018-02-16 13:24:58 -0800182
183 std::unique_ptr<BlockingConnection> underlying_;
Josh Gao13781e82018-04-03 12:55:18 -0700184 std::thread read_thread_ GUARDED_BY(mutex_);
185 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800186
Josh Gao13781e82018-04-03 12:55:18 -0700187 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800188 std::mutex mutex_;
189 std::condition_variable cv_;
190
191 std::once_flag error_flag_;
192};
193
194struct FdConnection : public BlockingConnection {
Joshua Duong64fab752020-01-21 13:19:42 -0800195 explicit FdConnection(unique_fd fd);
196 ~FdConnection();
Josh Gao395b86a2018-01-28 20:32:46 -0800197
198 bool Read(apacket* packet) override final;
199 bool Write(apacket* packet) override final;
Joshua Duong64fab752020-01-21 13:19:42 -0800200 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao395b86a2018-01-28 20:32:46 -0800201
202 void Close() override;
Josh Gao3a2172b2019-03-28 15:47:44 -0700203 virtual void Reset() override final { Close(); }
Josh Gao395b86a2018-01-28 20:32:46 -0800204
205 private:
Joshua Duong64fab752020-01-21 13:19:42 -0800206 bool DispatchRead(void* buf, size_t len);
207 bool DispatchWrite(void* buf, size_t len);
208
Josh Gao395b86a2018-01-28 20:32:46 -0800209 unique_fd fd_;
Joshua Duong64fab752020-01-21 13:19:42 -0800210 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gao395b86a2018-01-28 20:32:46 -0800211};
212
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700213// Waits for a transport's connection to be not pending. This is a separate
214// object so that the transport can be destroyed and another thread can be
215// notified of it in a race-free way.
216class ConnectionWaitable {
217 public:
218 ConnectionWaitable() = default;
219 ~ConnectionWaitable() = default;
220
221 // Waits until the first CNXN packet has been received by the owning
222 // atransport, or the specified timeout has elapsed. Can be called from any
223 // thread.
224 //
225 // Returns true if the CNXN packet was received in a timely fashion, false
226 // otherwise.
227 bool WaitForConnection(std::chrono::milliseconds timeout);
228
229 // Can be called from any thread when the connection stops being pending.
230 // Only the first invocation will be acknowledged, the rest will be no-ops.
231 void SetConnectionEstablished(bool success);
232
233 private:
234 bool connection_established_ GUARDED_BY(mutex_) = false;
235 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
236 std::mutex mutex_;
237 std::condition_variable cv_;
238
239 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
240};
241
Josh Gaod24580d2018-08-30 11:37:00 -0700242enum class ReconnectResult {
243 Retry,
244 Success,
245 Abort,
246};
247
Josh Gao6b55e752020-03-27 18:09:56 -0700248#if ADB_HOST
249struct usb_handle;
250#endif
251
Josh Gaoe490fbb2019-12-09 15:44:57 -0800252class atransport : public enable_weak_from_this<atransport> {
Josh Gaob39e4152017-08-16 16:57:01 -0700253 public:
Dan Albertbe8e54b2015-05-18 13:06:53 -0700254 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
255 // historically just a struct, but making the whole thing a more idiomatic
256 // class in one go is a very large change. Given how bad our testing is,
257 // it's better to do this piece by piece.
258
Josh Gaod24580d2018-08-30 11:37:00 -0700259 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700260
261 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700262 : id(NextTransportId()),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700263 kicked_(false),
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700264 connection_state_(state),
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700265 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700266 connection_(nullptr),
267 reconnect_(std::move(reconnect)) {
Tim Murrayee7b44d2017-12-07 11:40:00 -0800268 // Initialize protocol to min version for compatibility with older versions.
269 // Version will be updated post-connect.
270 protocol_version = A_VERSION_MIN;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700271 max_payload = MAX_PAYLOAD;
272 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700273 atransport(ConnectionState state = kCsOffline)
Josh Gaod24580d2018-08-30 11:37:00 -0700274 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gaoe490fbb2019-12-09 15:44:57 -0800275 ~atransport();
Dan Albertbe8e54b2015-05-18 13:06:53 -0700276
Yabin Cui3cf1b362017-03-10 16:01:01 -0800277 int Write(apacket* p);
Josh Gao3a2172b2019-03-28 15:47:44 -0700278 void Reset();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700279 void Kick();
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700280 bool kicked() const { return kicked_; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700281
Yabin Cui3cf1b362017-03-10 16:01:01 -0800282 // ConnectionState can be read by all threads, but can only be written in the main thread.
283 ConnectionState GetConnectionState() const;
284 void SetConnectionState(ConnectionState state);
285
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700286 void SetConnection(std::unique_ptr<Connection> connection);
287 std::shared_ptr<Connection> connection() {
288 std::lock_guard<std::mutex> lock(mutex_);
289 return connection_;
290 }
291
Josh Gao6b55e752020-03-27 18:09:56 -0700292#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800293 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
294 usb_handle* GetUsbHandle() { return usb_handle_; }
Josh Gao6b55e752020-03-27 18:09:56 -0700295#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800296
Josh Gaob39e4152017-08-16 16:57:01 -0700297 const TransportId id;
Josh Gao14ed9f92019-12-09 13:45:31 -0800298
Dan Albertbe8e54b2015-05-18 13:06:53 -0700299 bool online = false;
300 TransportType type = kTransportAny;
301
Dan Albertbe8e54b2015-05-18 13:06:53 -0700302 // Used to identify transports for clients.
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700303 std::string serial;
304 std::string product;
305 std::string model;
306 std::string device;
307 std::string devpath;
Yabin Cuif401ead2016-04-29 16:53:52 -0700308
Joshua Duong64fab752020-01-21 13:19:42 -0800309 // If this is set, the transport will initiate the connection with a
310 // START_TLS command, instead of AUTH.
311 bool use_tls = false;
312 int tls_version = A_STLS_VERSION;
313 int get_tls_version() const;
314
Josh Gao7cac88a2019-10-22 12:30:39 -0700315#if !ADB_HOST
Michael Groover02b74272019-04-25 18:33:35 -0700316 // Used to provide the key to the framework.
317 std::string auth_key;
Josh Gao7cac88a2019-10-22 12:30:39 -0700318 uint64_t auth_id;
319#endif
Michael Groover02b74272019-04-25 18:33:35 -0700320
Josh Gao395b86a2018-01-28 20:32:46 -0800321 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700322
Josh Gaoeac20582016-10-05 19:02:29 -0700323#if ADB_HOST
Joshua Duong64fab752020-01-21 13:19:42 -0800324 // The current key being authorized.
325 std::shared_ptr<RSA> Key();
Josh Gao22cb70b2016-08-18 22:00:12 -0700326 std::shared_ptr<RSA> NextKey();
Josh Gaoeb656522018-12-04 01:07:50 -0800327 void ResetKeys();
Josh Gaoeac20582016-10-05 19:02:29 -0700328#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700329
Josh Gao67ac3792016-10-06 13:31:44 -0700330 char token[TOKEN_SIZE] = {};
Dan Albertbe8e54b2015-05-18 13:06:53 -0700331 size_t failed_auth_attempts = 0;
332
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700333 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gao3cd03be2018-02-28 14:44:23 -0800334 std::string connection_state_name() const;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700335
336 void update_version(int version, size_t payload);
337 int get_protocol_version() const;
338 size_t get_max_payload() const;
339
David Pursella07dbad2015-09-22 10:43:08 -0700340 const FeatureSet& features() const {
Dan Albertbe8e54b2015-05-18 13:06:53 -0700341 return features_;
342 }
343
344 bool has_feature(const std::string& feature) const;
David Pursella07dbad2015-09-22 10:43:08 -0700345
346 // Loads the transport's feature set from the given string.
347 void SetFeatures(const std::string& features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700348
Yabin Cui2d4c1982015-08-28 15:09:44 -0700349 void AddDisconnect(adisconnect* disconnect);
350 void RemoveDisconnect(adisconnect* disconnect);
351 void RunDisconnects();
352
David Pursellc929c6f2016-03-01 08:58:26 -0800353 // Returns true if |target| matches this transport. A matching |target| can be any of:
354 // * <serial>
355 // * <devpath>
356 // * product:<product>
357 // * model:<model>
358 // * device:<device>
359 //
360 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
361 // For example, serial "100.100.100.100:5555" would match any of:
362 // * 100.100.100.100
363 // * tcp:100.100.100.100
364 // * udp:100.100.100.100:5555
365 // This is to make it easier to use the same network target for both fastboot and adb.
366 bool MatchesTarget(const std::string& target) const;
367
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700368 // Notifies that the atransport is no longer waiting for the connection
369 // being established.
370 void SetConnectionEstablished(bool success);
371
372 // Gets a shared reference to the ConnectionWaitable.
373 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
374
Josh Gaod24580d2018-08-30 11:37:00 -0700375 // Attempts to reconnect with the underlying Connection.
376 ReconnectResult Reconnect();
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700377
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700378 private:
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700379 std::atomic<bool> kicked_;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700380
Dan Albertbe8e54b2015-05-18 13:06:53 -0700381 // A set of features transmitted in the banner with the initial connection.
382 // This is stored in the banner as 'features=feature0,feature1,etc'.
383 FeatureSet features_;
384 int protocol_version;
385 size_t max_payload;
386
Yabin Cui2d4c1982015-08-28 15:09:44 -0700387 // A list of adisconnect callbacks called when the transport is kicked.
388 std::list<adisconnect*> disconnects_;
389
Yabin Cui3cf1b362017-03-10 16:01:01 -0800390 std::atomic<ConnectionState> connection_state_;
Josh Gaoeac20582016-10-05 19:02:29 -0700391#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700392 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gaoeac20582016-10-05 19:02:29 -0700393#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700394
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700395 // A sharable object that can be used to wait for the atransport's
396 // connection to be established.
397 std::shared_ptr<ConnectionWaitable> connection_waitable_;
398
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700399 // The underlying connection object.
400 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
401
Josh Gao6b55e752020-03-27 18:09:56 -0700402#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800403 // USB handle for the connection, if available.
404 usb_handle* usb_handle_ = nullptr;
Josh Gao6b55e752020-03-27 18:09:56 -0700405#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800406
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700407 // A callback that will be invoked when the atransport needs to reconnect.
408 ReconnectCallback reconnect_;
409
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700410 std::mutex mutex_;
411
Dan Albertbe8e54b2015-05-18 13:06:53 -0700412 DISALLOW_COPY_AND_ASSIGN(atransport);
413};
414
Dan Albertb302d122015-02-24 15:51:19 -0800415/*
416 * Obtain a transport from the available transports.
Elliott Hughes67943d12015-10-07 14:55:10 -0700417 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob39e4152017-08-16 16:57:01 -0700418 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes67943d12015-10-07 14:55:10 -0700419 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
420 * is set to true and nullptr returned.
421 * If no suitable transport is found, error is set and nullptr returned.
Dan Albertb302d122015-02-24 15:51:19 -0800422 */
Josh Gaob39e4152017-08-16 16:57:01 -0700423atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
424 bool* is_ambiguous, std::string* error_out,
425 bool accept_any_state = false);
Josh Gao3a2172b2019-03-28 15:47:44 -0700426void kick_transport(atransport* t, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800427void update_transports(void);
428
Josh Gao1e3bf732017-05-03 22:37:10 -0700429// Iterates across all of the current and pending transports.
430// Stops iteration and returns false if fn returns false, otherwise returns true.
431bool iterate_transports(std::function<bool(const atransport*)> fn);
432
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700433void init_reconnect_handler(void);
Dan Albertb302d122015-02-24 15:51:19 -0800434void init_transport_registration(void);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700435void init_mdns_transport_discovery(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700436std::string list_transports(bool long_listing);
Dan Albertb302d122015-02-24 15:51:19 -0800437atransport* find_transport(const char* serial);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700438void kick_all_tcp_devices();
Josh Gao165460f2017-05-09 13:43:35 -0700439void kick_all_transports();
Joshua Duong64fab752020-01-21 13:19:42 -0800440void kick_all_tcp_tls_transports();
441#if !ADB_HOST
442void kick_all_transports_by_auth_key(std::string_view auth_key);
443#endif
Dan Albertb302d122015-02-24 15:51:19 -0800444
Josh Gao3a34bc52018-10-11 16:33:05 -0700445void register_transport(atransport* transport);
Josh Gao6b55e752020-03-27 18:09:56 -0700446
447#if ADB_HOST
448void init_usb_transport(atransport* t, usb_handle* usb);
449void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
450 unsigned writeable);
451
452// This should only be used for transports with connection_state == kCsNoPerm.
453void unregister_usb_transport(usb_handle* usb);
454#endif
Dan Albertb302d122015-02-24 15:51:19 -0800455
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700456/* Connect to a network address and register it as a device */
457void connect_device(const std::string& address, std::string* response);
458
Dan Albertb302d122015-02-24 15:51:19 -0800459/* cause new transports to be init'd and added to the list */
Josh Gao597044d2018-08-08 16:20:14 -0700460bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong64fab752020-01-21 13:19:42 -0800461 atransport::ReconnectCallback reconnect, bool use_tls,
462 int* error = nullptr);
Dan Albertb302d122015-02-24 15:51:19 -0800463
Josh Gao67b683a2017-05-16 15:02:45 -0700464bool check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800465
Josh Gao3a2172b2019-03-28 15:47:44 -0700466void close_usb_devices(bool reset = false);
467void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800468
469void send_packet(apacket* p, atransport* t);
470
Josh Gao32124632017-08-14 18:57:54 -0700471asocket* create_device_tracker(bool long_output);
Dan Albertb302d122015-02-24 15:51:19 -0800472
Josh Gao0560feb2019-01-22 19:36:15 -0800473#if !ADB_HOST
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900474unique_fd adb_listen(std::string_view addr, std::string* error);
475void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
476 std::string_view addr);
Josh Gao0560feb2019-01-22 19:36:15 -0800477
478#if defined(__ANDROID__)
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900479void qemu_socket_thread(std::string_view addr);
Josh Gao0560feb2019-01-22 19:36:15 -0800480bool use_qemu_goldfish();
481#endif
482
483#endif
484
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700485#endif /* __TRANSPORT_H */