blob: fc0e322d5319c867ab820af541fe3357b4cf5ede [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>
Josh Gao887ab012020-06-24 16:25:49 -070030#include <optional>
Elliott Hughesab882422015-04-16 22:54:44 -070031#include <string>
Cody Schuffelen637aaf52019-01-04 18:51:11 -080032#include <string_view>
Josh Gao715fe602018-02-16 13:24:58 -080033#include <thread>
Shaju Mathewd17f4462022-06-25 14:57:31 +000034#include <unordered_map>
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070035#include <vector>
Dan Albertb302d122015-02-24 15:51:19 -080036
Luis Hector Chavezda74b902018-04-17 14:25:04 -070037#include <android-base/macros.h>
Josh Gao13781e82018-04-03 12:55:18 -070038#include <android-base/thread_annotations.h>
Elliott Hughes801066a2016-06-29 17:42:01 -070039#include <openssl/rsa.h>
40
Josh Gao395b86a2018-01-28 20:32:46 -080041#include "adb.h"
42#include "adb_unique_fd.h"
Josh Gaoe490fbb2019-12-09 15:44:57 -080043#include "types.h"
Josh Gao395b86a2018-01-28 20:32:46 -080044
Yurii Zubrytskyi816c2d22020-03-26 18:19:28 -070045// Even though the feature set is used as a set, we only have a dozen or two
46// of available features at any moment. Vector works much better in terms of
47// both memory usage and performance for these sizes.
48using FeatureSet = std::vector<std::string>;
Dan Albertbe8e54b2015-05-18 13:06:53 -070049
Joshua Duong64fab752020-01-21 13:19:42 -080050namespace adb {
51namespace tls {
52
53class TlsConnection;
54
55} // namespace tls
56} // namespace adb
57
Dan Albertbe8e54b2015-05-18 13:06:53 -070058const FeatureSet& supported_features();
59
David Pursella07dbad2015-09-22 10:43:08 -070060// Encodes and decodes FeatureSet objects into human-readable strings.
61std::string FeatureSetToString(const FeatureSet& features);
62FeatureSet StringToFeatureSet(const std::string& features_string);
63
David Pursell22fc5e92015-09-30 13:35:42 -070064// Returns true if both local features and |feature_set| support |feature|.
65bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
66
David Pursella07dbad2015-09-22 10:43:08 -070067// Do not use any of [:;=,] in feature strings, they have special meaning
68// in the connection banner.
Todd Kennedy1e2f7dc2015-11-03 16:53:08 -080069extern const char* const kFeatureShell2;
70// The 'cmd' command is available
71extern const char* const kFeatureCmd;
Josh Gaoa2cf3752016-12-05 17:11:34 -080072extern const char* const kFeatureStat2;
Josh Gao7b083072019-08-07 14:23:17 -070073extern const char* const kFeatureLs2;
Josh Gao210b63f2017-02-22 17:07:01 -080074// The server is running with libusb enabled.
75extern const char* const kFeatureLibusb;
Josh Gao281aab72018-10-22 13:00:05 -070076// adbd supports `push --sync`.
Dan Albert27983bc2017-05-23 14:30:00 -070077extern const char* const kFeaturePushSync;
Josh Gao281aab72018-10-22 13:00:05 -070078// adbd supports installing .apex packages.
Dario Frenidcb4c362018-10-04 16:26:40 +010079extern const char* const kFeatureApex;
Josh Gao281aab72018-10-22 13:00:05 -070080// adbd has b/110953234 fixed.
81extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070082// adbd supports android binder bridge (abb) in interactive mode using shell protocol.
Alex Buynytskyye1fa8142019-01-17 13:13:56 -080083extern const char* const kFeatureAbb;
Alex Buynytskyybdff85c2019-09-13 14:19:01 -070084// adbd supports abb using raw pipe.
85extern const char* const kFeatureAbbExec;
Josh Gao9eeb9f72019-02-20 13:01:40 -080086// adbd properly updates symlink timestamps on push.
87extern const char* const kFeatureFixedPushSymlinkTimestamp;
Josh Gao2f0f9eb2020-03-04 19:34:08 -080088// Implement `adb remount` via shelling out to /system/bin/remount.
Josh Gaof764d572019-07-11 14:15:32 -070089extern const char* const kFeatureRemountShell;
Shukang Zhou420ad552020-02-13 17:01:39 -080090// adbd supports `track-app` service reporting debuggable/profileable apps.
91extern const char* const kFeatureTrackApp;
Josh Gao2f0f9eb2020-03-04 19:34:08 -080092// adbd supports version 2 of send/recv.
93extern const char* const kFeatureSendRecv2;
94// adbd supports brotli for send/recv v2.
95extern const char* const kFeatureSendRecv2Brotli;
Josh Gaofb386cc2020-03-26 22:02:03 -070096// adbd supports LZ4 for send/recv v2.
97extern const char* const kFeatureSendRecv2LZ4;
Josh Gaobdebc9b2020-05-27 17:52:52 -070098// adbd supports Zstd for send/recv v2.
99extern const char* const kFeatureSendRecv2Zstd;
Josh Gao8a410a02020-03-30 23:25:16 -0700100// adbd supports dry-run send for send/recv v2.
101extern const char* const kFeatureSendRecv2DryRunSend;
Josh Gaobd1f3002021-12-29 21:15:12 -0800102// adbd supports delayed acks.
103extern const char* const kFeatureDelayedAck;
David Pursell8da19a42015-08-31 10:42:13 -0700104
Josh Gaob39e4152017-08-16 16:57:01 -0700105TransportId NextTransportId();
106
Josh Gao715fe602018-02-16 13:24:58 -0800107// Abstraction for a non-blocking packet transport.
Josh Gao395b86a2018-01-28 20:32:46 -0800108struct Connection {
109 Connection() = default;
Josh Gao395b86a2018-01-28 20:32:46 -0800110 virtual ~Connection() = default;
111
Josh Gao7852ca42021-06-17 04:17:25 -0700112 void SetTransport(atransport* transport) { transport_ = transport; }
Josh Gao715fe602018-02-16 13:24:58 -0800113
114 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
115
116 virtual void Start() = 0;
117 virtual void Stop() = 0;
118
Joshua Duong64fab752020-01-21 13:19:42 -0800119 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
120
Josh Gao3a2172b2019-03-28 15:47:44 -0700121 // Stop, and reset the device if it's a USB connection.
122 virtual void Reset();
123
Josh Gao8e7c9722021-06-17 04:19:45 -0700124 virtual bool Attach(std::string* error) {
125 *error = "transport type doesn't support attach";
126 return false;
127 }
128
129 virtual bool Detach(std::string* error) {
130 *error = "transport type doesn't support detach";
131 return false;
132 }
133
Josh Gao7852ca42021-06-17 04:17:25 -0700134 std::string Serial() const;
135
136 atransport* transport_ = nullptr;
Josh Gao1e41fda2018-04-05 16:16:04 -0700137
138 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao715fe602018-02-16 13:24:58 -0800139};
140
141// Abstraction for a blocking packet transport.
142struct BlockingConnection {
143 BlockingConnection() = default;
144 BlockingConnection(const BlockingConnection& copy) = delete;
145 BlockingConnection(BlockingConnection&& move) = delete;
146
147 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
148 virtual ~BlockingConnection() = default;
149
Josh Gao395b86a2018-01-28 20:32:46 -0800150 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
151 // threads.
152 virtual bool Read(apacket* packet) = 0;
153 virtual bool Write(apacket* packet) = 0;
154
Joshua Duong64fab752020-01-21 13:19:42 -0800155 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key = nullptr) = 0;
156
Josh Gao395b86a2018-01-28 20:32:46 -0800157 // Terminate a connection.
158 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
159 // Formerly known as 'Kick' in atransport.
160 virtual void Close() = 0;
Josh Gao3a2172b2019-03-28 15:47:44 -0700161
162 // Terminate a connection, and reset it.
163 virtual void Reset() = 0;
Josh Gao395b86a2018-01-28 20:32:46 -0800164};
165
Josh Gao715fe602018-02-16 13:24:58 -0800166struct BlockingConnectionAdapter : public Connection {
167 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
168
169 virtual ~BlockingConnectionAdapter();
170
171 virtual bool Write(std::unique_ptr<apacket> packet) override final;
172
173 virtual void Start() override final;
174 virtual void Stop() override final;
Joshua Duong64fab752020-01-21 13:19:42 -0800175 virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao715fe602018-02-16 13:24:58 -0800176
Josh Gao3a2172b2019-03-28 15:47:44 -0700177 virtual void Reset() override final;
178
Joshua Duong64fab752020-01-21 13:19:42 -0800179 private:
180 void StartReadThread() REQUIRES(mutex_);
Josh Gao13781e82018-04-03 12:55:18 -0700181 bool started_ GUARDED_BY(mutex_) = false;
182 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao715fe602018-02-16 13:24:58 -0800183
184 std::unique_ptr<BlockingConnection> underlying_;
Josh Gao13781e82018-04-03 12:55:18 -0700185 std::thread read_thread_ GUARDED_BY(mutex_);
186 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800187
Josh Gao13781e82018-04-03 12:55:18 -0700188 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800189 std::mutex mutex_;
190 std::condition_variable cv_;
191
192 std::once_flag error_flag_;
193};
194
195struct FdConnection : public BlockingConnection {
Joshua Duong64fab752020-01-21 13:19:42 -0800196 explicit FdConnection(unique_fd fd);
197 ~FdConnection();
Josh Gao395b86a2018-01-28 20:32:46 -0800198
199 bool Read(apacket* packet) override final;
200 bool Write(apacket* packet) override final;
Joshua Duong64fab752020-01-21 13:19:42 -0800201 bool DoTlsHandshake(RSA* key, std::string* auth_key) override final;
Josh Gao395b86a2018-01-28 20:32:46 -0800202
203 void Close() override;
Josh Gao3a2172b2019-03-28 15:47:44 -0700204 virtual void Reset() override final { Close(); }
Josh Gao395b86a2018-01-28 20:32:46 -0800205
206 private:
Joshua Duong64fab752020-01-21 13:19:42 -0800207 bool DispatchRead(void* buf, size_t len);
208 bool DispatchWrite(void* buf, size_t len);
209
Josh Gao395b86a2018-01-28 20:32:46 -0800210 unique_fd fd_;
Joshua Duong64fab752020-01-21 13:19:42 -0800211 std::unique_ptr<adb::tls::TlsConnection> tls_;
Josh Gao395b86a2018-01-28 20:32:46 -0800212};
213
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700214// Waits for a transport's connection to be not pending. This is a separate
215// object so that the transport can be destroyed and another thread can be
216// notified of it in a race-free way.
217class ConnectionWaitable {
218 public:
219 ConnectionWaitable() = default;
220 ~ConnectionWaitable() = default;
221
222 // Waits until the first CNXN packet has been received by the owning
223 // atransport, or the specified timeout has elapsed. Can be called from any
224 // thread.
225 //
226 // Returns true if the CNXN packet was received in a timely fashion, false
227 // otherwise.
228 bool WaitForConnection(std::chrono::milliseconds timeout);
229
230 // Can be called from any thread when the connection stops being pending.
231 // Only the first invocation will be acknowledged, the rest will be no-ops.
232 void SetConnectionEstablished(bool success);
233
234 private:
235 bool connection_established_ GUARDED_BY(mutex_) = false;
236 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
237 std::mutex mutex_;
238 std::condition_variable cv_;
239
240 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
241};
242
Josh Gaod24580d2018-08-30 11:37:00 -0700243enum class ReconnectResult {
244 Retry,
245 Success,
246 Abort,
247};
248
Josh Gao6b55e752020-03-27 18:09:56 -0700249#if ADB_HOST
250struct usb_handle;
251#endif
252
Josh Gaoe490fbb2019-12-09 15:44:57 -0800253class atransport : public enable_weak_from_this<atransport> {
Josh Gaob39e4152017-08-16 16:57:01 -0700254 public:
Dan Albertbe8e54b2015-05-18 13:06:53 -0700255 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
256 // historically just a struct, but making the whole thing a more idiomatic
257 // class in one go is a very large change. Given how bad our testing is,
258 // it's better to do this piece by piece.
259
Josh Gaod24580d2018-08-30 11:37:00 -0700260 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700261
262 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700263 : id(NextTransportId()),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700264 kicked_(false),
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700265 connection_state_(state),
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700266 connection_(nullptr),
267 reconnect_(std::move(reconnect)) {
Josh Gao65d18e22020-04-22 20:57:26 -0700268#if ADB_HOST
269 connection_waitable_ = std::make_shared<ConnectionWaitable>();
270#endif
271
Tim Murrayee7b44d2017-12-07 11:40:00 -0800272 // Initialize protocol to min version for compatibility with older versions.
273 // Version will be updated post-connect.
274 protocol_version = A_VERSION_MIN;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700275 max_payload = MAX_PAYLOAD;
276 }
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700277 atransport(ConnectionState state = kCsOffline)
Josh Gaod24580d2018-08-30 11:37:00 -0700278 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Josh Gaoe490fbb2019-12-09 15:44:57 -0800279 ~atransport();
Dan Albertbe8e54b2015-05-18 13:06:53 -0700280
Yabin Cui3cf1b362017-03-10 16:01:01 -0800281 int Write(apacket* p);
Josh Gao3a2172b2019-03-28 15:47:44 -0700282 void Reset();
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700283 void Kick();
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700284 bool kicked() const { return kicked_; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700285
Yabin Cui3cf1b362017-03-10 16:01:01 -0800286 // ConnectionState can be read by all threads, but can only be written in the main thread.
287 ConnectionState GetConnectionState() const;
288 void SetConnectionState(ConnectionState state);
289
Josh Gaof2d6af52021-04-27 20:32:02 -0700290 void SetConnection(std::shared_ptr<Connection> connection);
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700291 std::shared_ptr<Connection> connection() {
292 std::lock_guard<std::mutex> lock(mutex_);
293 return connection_;
294 }
295
Josh Gao7852ca42021-06-17 04:17:25 -0700296 bool HandleRead(std::unique_ptr<apacket> p);
297 void HandleError(const std::string& error);
298
Josh Gao6b55e752020-03-27 18:09:56 -0700299#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800300 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
301 usb_handle* GetUsbHandle() { return usb_handle_; }
Shaju Mathewd17f4462022-06-25 14:57:31 +0000302
303 // Interface for management/filter on forward:reverse: configuration.
304 void UpdateReverseConfig(std::string_view service_addr);
305 bool IsReverseConfigured(const std::string& local_addr);
Josh Gao6b55e752020-03-27 18:09:56 -0700306#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800307
Josh Gaob39e4152017-08-16 16:57:01 -0700308 const TransportId id;
Josh Gao14ed9f92019-12-09 13:45:31 -0800309
Dan Albertbe8e54b2015-05-18 13:06:53 -0700310 bool online = false;
311 TransportType type = kTransportAny;
312
Dan Albertbe8e54b2015-05-18 13:06:53 -0700313 // Used to identify transports for clients.
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700314 std::string serial;
315 std::string product;
316 std::string model;
317 std::string device;
318 std::string devpath;
Yabin Cuif401ead2016-04-29 16:53:52 -0700319
Joshua Duong64fab752020-01-21 13:19:42 -0800320 // If this is set, the transport will initiate the connection with a
321 // START_TLS command, instead of AUTH.
322 bool use_tls = false;
323 int tls_version = A_STLS_VERSION;
324 int get_tls_version() const;
325
Josh Gao7cac88a2019-10-22 12:30:39 -0700326#if !ADB_HOST
Michael Groover02b74272019-04-25 18:33:35 -0700327 // Used to provide the key to the framework.
328 std::string auth_key;
Josh Gao887ab012020-06-24 16:25:49 -0700329 std::optional<uint64_t> auth_id;
Josh Gao7cac88a2019-10-22 12:30:39 -0700330#endif
Michael Groover02b74272019-04-25 18:33:35 -0700331
Josh Gao395b86a2018-01-28 20:32:46 -0800332 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700333
Josh Gaoeac20582016-10-05 19:02:29 -0700334#if ADB_HOST
Joshua Duong64fab752020-01-21 13:19:42 -0800335 // The current key being authorized.
336 std::shared_ptr<RSA> Key();
Josh Gao22cb70b2016-08-18 22:00:12 -0700337 std::shared_ptr<RSA> NextKey();
Josh Gaoeb656522018-12-04 01:07:50 -0800338 void ResetKeys();
Josh Gaoeac20582016-10-05 19:02:29 -0700339#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700340
Josh Gao67ac3792016-10-06 13:31:44 -0700341 char token[TOKEN_SIZE] = {};
Dan Albertbe8e54b2015-05-18 13:06:53 -0700342 size_t failed_auth_attempts = 0;
343
Luis Hector Chavezb4edbdf2018-07-18 21:18:27 -0700344 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700345
346 void update_version(int version, size_t payload);
347 int get_protocol_version() const;
348 size_t get_max_payload() const;
349
Josh Gaof2d6af52021-04-27 20:32:02 -0700350 const FeatureSet& features() const { return features_; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700351
352 bool has_feature(const std::string& feature) const;
David Pursella07dbad2015-09-22 10:43:08 -0700353
Josh Gaobd1f3002021-12-29 21:15:12 -0800354 bool SupportsDelayedAck() const {
355 return delayed_ack_;
356 }
357
David Pursella07dbad2015-09-22 10:43:08 -0700358 // Loads the transport's feature set from the given string.
359 void SetFeatures(const std::string& features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700360
Yabin Cui2d4c1982015-08-28 15:09:44 -0700361 void AddDisconnect(adisconnect* disconnect);
362 void RemoveDisconnect(adisconnect* disconnect);
363 void RunDisconnects();
364
Josh Gao65d18e22020-04-22 20:57:26 -0700365#if ADB_HOST
Josh Gao8e7c9722021-06-17 04:19:45 -0700366 bool Attach(std::string* error);
367 bool Detach(std::string* error);
368#endif
369
370#if ADB_HOST
David Pursellc929c6f2016-03-01 08:58:26 -0800371 // Returns true if |target| matches this transport. A matching |target| can be any of:
372 // * <serial>
373 // * <devpath>
374 // * product:<product>
375 // * model:<model>
376 // * device:<device>
377 //
378 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
379 // For example, serial "100.100.100.100:5555" would match any of:
380 // * 100.100.100.100
381 // * tcp:100.100.100.100
382 // * udp:100.100.100.100:5555
383 // This is to make it easier to use the same network target for both fastboot and adb.
384 bool MatchesTarget(const std::string& target) const;
385
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700386 // Notifies that the atransport is no longer waiting for the connection
387 // being established.
388 void SetConnectionEstablished(bool success);
389
390 // Gets a shared reference to the ConnectionWaitable.
391 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
392
Josh Gaod24580d2018-08-30 11:37:00 -0700393 // Attempts to reconnect with the underlying Connection.
394 ReconnectResult Reconnect();
Josh Gao65d18e22020-04-22 20:57:26 -0700395#endif
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700396
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700397 private:
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700398 std::atomic<bool> kicked_;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700399
Dan Albertbe8e54b2015-05-18 13:06:53 -0700400 // A set of features transmitted in the banner with the initial connection.
401 // This is stored in the banner as 'features=feature0,feature1,etc'.
402 FeatureSet features_;
403 int protocol_version;
404 size_t max_payload;
405
Yabin Cui2d4c1982015-08-28 15:09:44 -0700406 // A list of adisconnect callbacks called when the transport is kicked.
407 std::list<adisconnect*> disconnects_;
408
Yabin Cui3cf1b362017-03-10 16:01:01 -0800409 std::atomic<ConnectionState> connection_state_;
Josh Gaoeac20582016-10-05 19:02:29 -0700410#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700411 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gaoeac20582016-10-05 19:02:29 -0700412#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700413
Josh Gao65d18e22020-04-22 20:57:26 -0700414#if ADB_HOST
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700415 // A sharable object that can be used to wait for the atransport's
416 // connection to be established.
417 std::shared_ptr<ConnectionWaitable> connection_waitable_;
Josh Gao65d18e22020-04-22 20:57:26 -0700418#endif
Luis Hector Chavezda74b902018-04-17 14:25:04 -0700419
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700420 // The underlying connection object.
421 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
422
Josh Gao6b55e752020-03-27 18:09:56 -0700423#if ADB_HOST
Josh Gao68f2c382018-12-11 13:11:52 -0800424 // USB handle for the connection, if available.
425 usb_handle* usb_handle_ = nullptr;
Josh Gao6b55e752020-03-27 18:09:56 -0700426#endif
Josh Gao68f2c382018-12-11 13:11:52 -0800427
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700428 // A callback that will be invoked when the atransport needs to reconnect.
429 ReconnectCallback reconnect_;
430
Luis Hector Chavez3c7881d2018-04-25 08:56:41 -0700431 std::mutex mutex_;
432
Josh Gaobd1f3002021-12-29 21:15:12 -0800433 bool delayed_ack_ = false;
434
Shaju Mathewd17f4462022-06-25 14:57:31 +0000435#if ADB_HOST
436 // Track remote addresses against local addresses (configured)
437 // through `adb reverse` commands.
438 // Access constrained to primary thread by virtue of check_main_thread().
439 std::unordered_map<std::string, std::string> reverse_forwards_;
440#endif
441
Dan Albertbe8e54b2015-05-18 13:06:53 -0700442 DISALLOW_COPY_AND_ASSIGN(atransport);
443};
444
Vince Harron5703eb32021-11-12 12:40:58 -0800445// --one-device command line parameter is eventually put here.
446void transport_set_one_device(const char* adb_one_device);
447
448// Returns one device owned by this server of nullptr if all devices belong to server.
449const char* transport_get_one_device();
450
451// Returns true if the adb server owns all devices, or `serial`.
452bool transport_server_owns_device(std::string_view serial);
453
454// Returns true if the adb server owns all devices, `serial`, or `dev_path`.
455bool transport_server_owns_device(std::string_view dev_path, std::string_view serial);
456
Dan Albertb302d122015-02-24 15:51:19 -0800457/*
458 * Obtain a transport from the available transports.
Elliott Hughes67943d12015-10-07 14:55:10 -0700459 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob39e4152017-08-16 16:57:01 -0700460 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes67943d12015-10-07 14:55:10 -0700461 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
462 * is set to true and nullptr returned.
463 * If no suitable transport is found, error is set and nullptr returned.
Dan Albertb302d122015-02-24 15:51:19 -0800464 */
Josh Gaob39e4152017-08-16 16:57:01 -0700465atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
466 bool* is_ambiguous, std::string* error_out,
467 bool accept_any_state = false);
Josh Gao3a2172b2019-03-28 15:47:44 -0700468void kick_transport(atransport* t, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800469void update_transports(void);
470
Josh Gao1e3bf732017-05-03 22:37:10 -0700471// Iterates across all of the current and pending transports.
472// Stops iteration and returns false if fn returns false, otherwise returns true.
473bool iterate_transports(std::function<bool(const atransport*)> fn);
474
Luis Hector Chavez0aeda102018-04-20 10:31:29 -0700475void init_reconnect_handler(void);
Dan Albertb302d122015-02-24 15:51:19 -0800476void init_transport_registration(void);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700477void init_mdns_transport_discovery(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700478std::string list_transports(bool long_listing);
Josh Gao65d18e22020-04-22 20:57:26 -0700479
480#if ADB_HOST
Dan Albertb302d122015-02-24 15:51:19 -0800481atransport* find_transport(const char* serial);
Josh Gao65d18e22020-04-22 20:57:26 -0700482
Yabin Cui4d64fd82015-08-27 12:03:11 -0700483void kick_all_tcp_devices();
Josh Gao65d18e22020-04-22 20:57:26 -0700484#endif
485
Josh Gao165460f2017-05-09 13:43:35 -0700486void kick_all_transports();
Josh Gao65d18e22020-04-22 20:57:26 -0700487
Joshua Duong64fab752020-01-21 13:19:42 -0800488void kick_all_tcp_tls_transports();
Josh Gao65d18e22020-04-22 20:57:26 -0700489
Joshua Duong64fab752020-01-21 13:19:42 -0800490#if !ADB_HOST
491void kick_all_transports_by_auth_key(std::string_view auth_key);
492#endif
Dan Albertb302d122015-02-24 15:51:19 -0800493
Josh Gao3a34bc52018-10-11 16:33:05 -0700494void register_transport(atransport* transport);
Josh Gao6b55e752020-03-27 18:09:56 -0700495
496#if ADB_HOST
497void init_usb_transport(atransport* t, usb_handle* usb);
Josh Gaof2d6af52021-04-27 20:32:02 -0700498
499void register_usb_transport(std::shared_ptr<Connection> connection, const char* serial,
500 const char* devpath, unsigned writeable);
Josh Gao6b55e752020-03-27 18:09:56 -0700501void register_usb_transport(usb_handle* h, const char* serial, const char* devpath,
502 unsigned writeable);
503
504// This should only be used for transports with connection_state == kCsNoPerm.
505void unregister_usb_transport(usb_handle* usb);
506#endif
Dan Albertb302d122015-02-24 15:51:19 -0800507
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700508/* Connect to a network address and register it as a device */
509void connect_device(const std::string& address, std::string* response);
510
Dan Albertb302d122015-02-24 15:51:19 -0800511/* cause new transports to be init'd and added to the list */
Josh Gao597044d2018-08-08 16:20:14 -0700512bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
Joshua Duong64fab752020-01-21 13:19:42 -0800513 atransport::ReconnectCallback reconnect, bool use_tls,
514 int* error = nullptr);
Dan Albertb302d122015-02-24 15:51:19 -0800515
Josh Gao67b683a2017-05-16 15:02:45 -0700516bool check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800517
Josh Gao3a2172b2019-03-28 15:47:44 -0700518void close_usb_devices(bool reset = false);
519void close_usb_devices(std::function<bool(const atransport*)> predicate, bool reset = false);
Dan Albertb302d122015-02-24 15:51:19 -0800520
521void send_packet(apacket* p, atransport* t);
522
Josh Gao32124632017-08-14 18:57:54 -0700523asocket* create_device_tracker(bool long_output);
Dan Albertb302d122015-02-24 15:51:19 -0800524
Josh Gao0560feb2019-01-22 19:36:15 -0800525#if !ADB_HOST
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900526unique_fd adb_listen(std::string_view addr, std::string* error);
527void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
528 std::string_view addr);
Josh Gao0560feb2019-01-22 19:36:15 -0800529#endif
530
Josh Gaof2d6af52021-04-27 20:32:02 -0700531#endif /* __TRANSPORT_H */