blob: 71e48572577d7a4610789664071a4e68c3dc27e5 [file] [log] [blame]
JP Abgrall408fa572011-03-16 15:57:42 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __TRANSPORT_H
18#define __TRANSPORT_H
19
Dan Alberte9fca142015-02-18 18:03:26 -080020#include <sys/types.h>
21
Yabin Cuib5e11412017-03-10 16:01:01 -080022#include <atomic>
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070023#include <chrono>
Josh Gao0bbf69c2018-02-16 13:24:58 -080024#include <condition_variable>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070025#include <deque>
Josh Gao22d2b3e2016-10-27 14:01:08 -070026#include <functional>
Yabin Cuib3298242015-08-28 15:09:44 -070027#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070028#include <memory>
Yabin Cuib5e11412017-03-10 16:01:01 -080029#include <mutex>
Elliott Hughes7be29c82015-04-16 22:54:44 -070030#include <string>
Cody Schuffelena05b64d2019-01-04 18:51:11 -080031#include <string_view>
Josh Gao0bbf69c2018-02-16 13:24:58 -080032#include <thread>
Dan Albert1792c232015-05-18 13:06:53 -070033#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080034
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070035#include <android-base/macros.h>
Josh Gaoc251ec52018-04-03 12:55:18 -070036#include <android-base/thread_annotations.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070037#include <openssl/rsa.h>
38
Josh Gaob800d882018-01-28 20:32:46 -080039#include "adb.h"
40#include "adb_unique_fd.h"
Josh Gaoce5ce872018-12-11 13:11:52 -080041#include "usb.h"
Josh Gaob800d882018-01-28 20:32:46 -080042
Dan Albert1792c232015-05-18 13:06:53 -070043typedef std::unordered_set<std::string> FeatureSet;
44
45const FeatureSet& supported_features();
46
David Pursell4e2fd362015-09-22 10:43:08 -070047// Encodes and decodes FeatureSet objects into human-readable strings.
48std::string FeatureSetToString(const FeatureSet& features);
49FeatureSet StringToFeatureSet(const std::string& features_string);
50
David Pursell70ef7b42015-09-30 13:35:42 -070051// Returns true if both local features and |feature_set| support |feature|.
52bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
53
David Pursell4e2fd362015-09-22 10:43:08 -070054// Do not use any of [:;=,] in feature strings, they have special meaning
55// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080056extern const char* const kFeatureShell2;
57// The 'cmd' command is available
58extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080059extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080060// The server is running with libusb enabled.
61extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070062// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070063extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070064// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010065extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070066// adbd has b/110953234 fixed.
67extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080068// adbd supports android binder bridge (abb).
69extern const char* const kFeatureAbb;
David Pursell0955c662015-08-31 10:42:13 -070070
Josh Gaob122b172017-08-16 16:57:01 -070071TransportId NextTransportId();
72
Josh Gao0bbf69c2018-02-16 13:24:58 -080073// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080074struct Connection {
75 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080076 virtual ~Connection() = default;
77
Josh Gao0bbf69c2018-02-16 13:24:58 -080078 void SetTransportName(std::string transport_name) {
79 transport_name_ = std::move(transport_name);
80 }
81
82 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
83 void SetReadCallback(ReadCallback callback) {
84 CHECK(!read_callback_);
85 read_callback_ = callback;
86 }
87
88 // Called after the Connection has terminated, either by an error or because Stop was called.
89 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
90 void SetErrorCallback(ErrorCallback callback) {
91 CHECK(!error_callback_);
92 error_callback_ = callback;
93 }
94
95 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
96
97 virtual void Start() = 0;
98 virtual void Stop() = 0;
99
100 std::string transport_name_;
101 ReadCallback read_callback_;
102 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700103
104 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800105};
106
107// Abstraction for a blocking packet transport.
108struct BlockingConnection {
109 BlockingConnection() = default;
110 BlockingConnection(const BlockingConnection& copy) = delete;
111 BlockingConnection(BlockingConnection&& move) = delete;
112
113 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
114 virtual ~BlockingConnection() = default;
115
Josh Gaob800d882018-01-28 20:32:46 -0800116 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
117 // threads.
118 virtual bool Read(apacket* packet) = 0;
119 virtual bool Write(apacket* packet) = 0;
120
121 // Terminate a connection.
122 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
123 // Formerly known as 'Kick' in atransport.
124 virtual void Close() = 0;
125};
126
Josh Gao0bbf69c2018-02-16 13:24:58 -0800127struct BlockingConnectionAdapter : public Connection {
128 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
129
130 virtual ~BlockingConnectionAdapter();
131
132 virtual bool Write(std::unique_ptr<apacket> packet) override final;
133
134 virtual void Start() override final;
135 virtual void Stop() override final;
136
Josh Gaoc251ec52018-04-03 12:55:18 -0700137 bool started_ GUARDED_BY(mutex_) = false;
138 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800139
140 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700141 std::thread read_thread_ GUARDED_BY(mutex_);
142 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800143
Josh Gaoc251ec52018-04-03 12:55:18 -0700144 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800145 std::mutex mutex_;
146 std::condition_variable cv_;
147
148 std::once_flag error_flag_;
149};
150
151struct FdConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800152 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
153
154 bool Read(apacket* packet) override final;
155 bool Write(apacket* packet) override final;
156
157 void Close() override;
158
159 private:
160 unique_fd fd_;
161};
162
Josh Gao0bbf69c2018-02-16 13:24:58 -0800163struct UsbConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800164 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
165 ~UsbConnection();
166
167 bool Read(apacket* packet) override final;
168 bool Write(apacket* packet) override final;
169
170 void Close() override final;
171
172 usb_handle* handle_;
173};
174
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700175// Waits for a transport's connection to be not pending. This is a separate
176// object so that the transport can be destroyed and another thread can be
177// notified of it in a race-free way.
178class ConnectionWaitable {
179 public:
180 ConnectionWaitable() = default;
181 ~ConnectionWaitable() = default;
182
183 // Waits until the first CNXN packet has been received by the owning
184 // atransport, or the specified timeout has elapsed. Can be called from any
185 // thread.
186 //
187 // Returns true if the CNXN packet was received in a timely fashion, false
188 // otherwise.
189 bool WaitForConnection(std::chrono::milliseconds timeout);
190
191 // Can be called from any thread when the connection stops being pending.
192 // Only the first invocation will be acknowledged, the rest will be no-ops.
193 void SetConnectionEstablished(bool success);
194
195 private:
196 bool connection_established_ GUARDED_BY(mutex_) = false;
197 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
198 std::mutex mutex_;
199 std::condition_variable cv_;
200
201 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
202};
203
Josh Gaofc2e56f2018-08-30 11:37:00 -0700204enum class ReconnectResult {
205 Retry,
206 Success,
207 Abort,
208};
209
Dan Albert1792c232015-05-18 13:06:53 -0700210class atransport {
Josh Gaob122b172017-08-16 16:57:01 -0700211 public:
Dan Albert1792c232015-05-18 13:06:53 -0700212 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
213 // historically just a struct, but making the whole thing a more idiomatic
214 // class in one go is a very large change. Given how bad our testing is,
215 // it's better to do this piece by piece.
216
Josh Gaofc2e56f2018-08-30 11:37:00 -0700217 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700218
219 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700220 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700221 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700222 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700223 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700224 connection_(nullptr),
225 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800226 // Initialize protocol to min version for compatibility with older versions.
227 // Version will be updated post-connect.
228 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700229 max_payload = MAX_PAYLOAD;
230 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700231 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700232 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700233 virtual ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700234
Yabin Cuib5e11412017-03-10 16:01:01 -0800235 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -0700236 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700237 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700238
Yabin Cuib5e11412017-03-10 16:01:01 -0800239 // ConnectionState can be read by all threads, but can only be written in the main thread.
240 ConnectionState GetConnectionState() const;
241 void SetConnectionState(ConnectionState state);
242
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700243 void SetConnection(std::unique_ptr<Connection> connection);
244 std::shared_ptr<Connection> connection() {
245 std::lock_guard<std::mutex> lock(mutex_);
246 return connection_;
247 }
248
Josh Gaoce5ce872018-12-11 13:11:52 -0800249 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
250 usb_handle* GetUsbHandle() { return usb_handle_; }
251
Josh Gaob122b172017-08-16 16:57:01 -0700252 const TransportId id;
Josh Gaoe48ecce2017-09-13 13:40:57 -0700253 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700254 bool online = false;
255 TransportType type = kTransportAny;
256
Dan Albert1792c232015-05-18 13:06:53 -0700257 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700258 std::string serial;
259 std::string product;
260 std::string model;
261 std::string device;
262 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700263
Josh Gaob800d882018-01-28 20:32:46 -0800264 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700265
Josh Gao3bd28792016-10-05 19:02:29 -0700266#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700267 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800268 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700269#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700270
Josh Gao06d61d42016-10-06 13:31:44 -0700271 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700272 size_t failed_auth_attempts = 0;
273
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700274 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800275 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700276
277 void update_version(int version, size_t payload);
278 int get_protocol_version() const;
279 size_t get_max_payload() const;
280
David Pursell4e2fd362015-09-22 10:43:08 -0700281 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700282 return features_;
283 }
284
285 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700286
287 // Loads the transport's feature set from the given string.
288 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700289
Yabin Cuib3298242015-08-28 15:09:44 -0700290 void AddDisconnect(adisconnect* disconnect);
291 void RemoveDisconnect(adisconnect* disconnect);
292 void RunDisconnects();
293
David Pursell3f902aa2016-03-01 08:58:26 -0800294 // Returns true if |target| matches this transport. A matching |target| can be any of:
295 // * <serial>
296 // * <devpath>
297 // * product:<product>
298 // * model:<model>
299 // * device:<device>
300 //
301 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
302 // For example, serial "100.100.100.100:5555" would match any of:
303 // * 100.100.100.100
304 // * tcp:100.100.100.100
305 // * udp:100.100.100.100:5555
306 // This is to make it easier to use the same network target for both fastboot and adb.
307 bool MatchesTarget(const std::string& target) const;
308
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700309 // Notifies that the atransport is no longer waiting for the connection
310 // being established.
311 void SetConnectionEstablished(bool success);
312
313 // Gets a shared reference to the ConnectionWaitable.
314 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
315
Josh Gaofc2e56f2018-08-30 11:37:00 -0700316 // Attempts to reconnect with the underlying Connection.
317 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700318
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700319 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700320 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700321
Dan Albert1792c232015-05-18 13:06:53 -0700322 // A set of features transmitted in the banner with the initial connection.
323 // This is stored in the banner as 'features=feature0,feature1,etc'.
324 FeatureSet features_;
325 int protocol_version;
326 size_t max_payload;
327
Yabin Cuib3298242015-08-28 15:09:44 -0700328 // A list of adisconnect callbacks called when the transport is kicked.
329 std::list<adisconnect*> disconnects_;
330
Yabin Cuib5e11412017-03-10 16:01:01 -0800331 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700332#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700333 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700334#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700335
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700336 // A sharable object that can be used to wait for the atransport's
337 // connection to be established.
338 std::shared_ptr<ConnectionWaitable> connection_waitable_;
339
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700340 // The underlying connection object.
341 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
342
Josh Gaoce5ce872018-12-11 13:11:52 -0800343 // USB handle for the connection, if available.
344 usb_handle* usb_handle_ = nullptr;
345
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700346 // A callback that will be invoked when the atransport needs to reconnect.
347 ReconnectCallback reconnect_;
348
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700349 std::mutex mutex_;
350
Dan Albert1792c232015-05-18 13:06:53 -0700351 DISALLOW_COPY_AND_ASSIGN(atransport);
352};
353
Dan Albert76649012015-02-24 15:51:19 -0800354/*
355 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700356 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700357 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700358 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
359 * is set to true and nullptr returned.
360 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800361 */
Josh Gaob122b172017-08-16 16:57:01 -0700362atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
363 bool* is_ambiguous, std::string* error_out,
364 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800365void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800366void update_transports(void);
367
Josh Gaofd713e52017-05-03 22:37:10 -0700368// Iterates across all of the current and pending transports.
369// Stops iteration and returns false if fn returns false, otherwise returns true.
370bool iterate_transports(std::function<bool(const atransport*)> fn);
371
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700372void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800373void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700374void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700375std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800376atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700377void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700378void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800379
Josh Gaoc51726c2018-10-11 16:33:05 -0700380void register_transport(atransport* transport);
Dan Albert76649012015-02-24 15:51:19 -0800381void register_usb_transport(usb_handle* h, const char* serial,
382 const char* devpath, unsigned writeable);
383
Casey Dahlin13a269e2016-06-23 14:19:37 -0700384/* Connect to a network address and register it as a device */
385void connect_device(const std::string& address, std::string* response);
386
Dan Albert76649012015-02-24 15:51:19 -0800387/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700388bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
389 atransport::ReconnectCallback reconnect, int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800390
Dan Albertdcd78a12015-05-18 16:43:57 -0700391// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800392void unregister_usb_transport(usb_handle* usb);
393
Josh Gao36dadca2017-05-16 15:02:45 -0700394bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800395
Dan Albert76649012015-02-24 15:51:19 -0800396void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700397void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800398
399void send_packet(apacket* p, atransport* t);
400
Josh Gaob0c18022017-08-14 18:57:54 -0700401asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800402
Josh Gao776c2ec2019-01-22 19:36:15 -0800403#if !ADB_HOST
Cody Schuffelena05b64d2019-01-04 18:51:11 -0800404void server_socket_thread(std::string_view spec);
Josh Gao776c2ec2019-01-22 19:36:15 -0800405
406#if defined(__ANDROID__)
407void qemu_socket_thread(int port);
408bool use_qemu_goldfish();
409#endif
410
411#endif
412
JP Abgrall408fa572011-03-16 15:57:42 -0700413#endif /* __TRANSPORT_H */