blob: 3baeb1cf768aaaed4f60f814e044140d810e34ef [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>
Josh Gao0bbf69c2018-02-16 13:24:58 -080031#include <thread>
Dan Albert1792c232015-05-18 13:06:53 -070032#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080033
Luis Hector Chavez56fe7532018-04-17 14:25:04 -070034#include <android-base/macros.h>
Josh Gaoc251ec52018-04-03 12:55:18 -070035#include <android-base/thread_annotations.h>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070036#include <openssl/rsa.h>
37
Josh Gaob800d882018-01-28 20:32:46 -080038#include "adb.h"
39#include "adb_unique_fd.h"
Josh Gaoce5ce872018-12-11 13:11:52 -080040#include "usb.h"
Josh Gaob800d882018-01-28 20:32:46 -080041
Dan Albert1792c232015-05-18 13:06:53 -070042typedef std::unordered_set<std::string> FeatureSet;
43
44const FeatureSet& supported_features();
45
David Pursell4e2fd362015-09-22 10:43:08 -070046// Encodes and decodes FeatureSet objects into human-readable strings.
47std::string FeatureSetToString(const FeatureSet& features);
48FeatureSet StringToFeatureSet(const std::string& features_string);
49
David Pursell70ef7b42015-09-30 13:35:42 -070050// Returns true if both local features and |feature_set| support |feature|.
51bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
52
David Pursell4e2fd362015-09-22 10:43:08 -070053// Do not use any of [:;=,] in feature strings, they have special meaning
54// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080055extern const char* const kFeatureShell2;
56// The 'cmd' command is available
57extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080058extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080059// The server is running with libusb enabled.
60extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070061// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070062extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070063// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010064extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070065// adbd has b/110953234 fixed.
66extern const char* const kFeatureFixedPushMkdir;
Alex Buynytskyy01a65ee2019-01-17 13:13:56 -080067// adbd supports android binder bridge (abb).
68extern const char* const kFeatureAbb;
David Pursell0955c662015-08-31 10:42:13 -070069
Josh Gaob122b172017-08-16 16:57:01 -070070TransportId NextTransportId();
71
Josh Gao0bbf69c2018-02-16 13:24:58 -080072// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080073struct Connection {
74 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080075 virtual ~Connection() = default;
76
Josh Gao0bbf69c2018-02-16 13:24:58 -080077 void SetTransportName(std::string transport_name) {
78 transport_name_ = std::move(transport_name);
79 }
80
81 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
82 void SetReadCallback(ReadCallback callback) {
83 CHECK(!read_callback_);
84 read_callback_ = callback;
85 }
86
87 // Called after the Connection has terminated, either by an error or because Stop was called.
88 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
89 void SetErrorCallback(ErrorCallback callback) {
90 CHECK(!error_callback_);
91 error_callback_ = callback;
92 }
93
94 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
95
96 virtual void Start() = 0;
97 virtual void Stop() = 0;
98
99 std::string transport_name_;
100 ReadCallback read_callback_;
101 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -0700102
103 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800104};
105
106// Abstraction for a blocking packet transport.
107struct BlockingConnection {
108 BlockingConnection() = default;
109 BlockingConnection(const BlockingConnection& copy) = delete;
110 BlockingConnection(BlockingConnection&& move) = delete;
111
112 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
113 virtual ~BlockingConnection() = default;
114
Josh Gaob800d882018-01-28 20:32:46 -0800115 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
116 // threads.
117 virtual bool Read(apacket* packet) = 0;
118 virtual bool Write(apacket* packet) = 0;
119
120 // Terminate a connection.
121 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
122 // Formerly known as 'Kick' in atransport.
123 virtual void Close() = 0;
124};
125
Josh Gao0bbf69c2018-02-16 13:24:58 -0800126struct BlockingConnectionAdapter : public Connection {
127 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
128
129 virtual ~BlockingConnectionAdapter();
130
131 virtual bool Write(std::unique_ptr<apacket> packet) override final;
132
133 virtual void Start() override final;
134 virtual void Stop() override final;
135
Josh Gaoc251ec52018-04-03 12:55:18 -0700136 bool started_ GUARDED_BY(mutex_) = false;
137 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800138
139 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700140 std::thread read_thread_ GUARDED_BY(mutex_);
141 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800142
Josh Gaoc251ec52018-04-03 12:55:18 -0700143 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800144 std::mutex mutex_;
145 std::condition_variable cv_;
146
147 std::once_flag error_flag_;
148};
149
150struct FdConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800151 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
152
153 bool Read(apacket* packet) override final;
154 bool Write(apacket* packet) override final;
155
156 void Close() override;
157
158 private:
159 unique_fd fd_;
160};
161
Josh Gao0bbf69c2018-02-16 13:24:58 -0800162struct UsbConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800163 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
164 ~UsbConnection();
165
166 bool Read(apacket* packet) override final;
167 bool Write(apacket* packet) override final;
168
169 void Close() override final;
170
171 usb_handle* handle_;
172};
173
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700174// Waits for a transport's connection to be not pending. This is a separate
175// object so that the transport can be destroyed and another thread can be
176// notified of it in a race-free way.
177class ConnectionWaitable {
178 public:
179 ConnectionWaitable() = default;
180 ~ConnectionWaitable() = default;
181
182 // Waits until the first CNXN packet has been received by the owning
183 // atransport, or the specified timeout has elapsed. Can be called from any
184 // thread.
185 //
186 // Returns true if the CNXN packet was received in a timely fashion, false
187 // otherwise.
188 bool WaitForConnection(std::chrono::milliseconds timeout);
189
190 // Can be called from any thread when the connection stops being pending.
191 // Only the first invocation will be acknowledged, the rest will be no-ops.
192 void SetConnectionEstablished(bool success);
193
194 private:
195 bool connection_established_ GUARDED_BY(mutex_) = false;
196 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
197 std::mutex mutex_;
198 std::condition_variable cv_;
199
200 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
201};
202
Josh Gaofc2e56f2018-08-30 11:37:00 -0700203enum class ReconnectResult {
204 Retry,
205 Success,
206 Abort,
207};
208
Dan Albert1792c232015-05-18 13:06:53 -0700209class atransport {
Josh Gaob122b172017-08-16 16:57:01 -0700210 public:
Dan Albert1792c232015-05-18 13:06:53 -0700211 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
212 // historically just a struct, but making the whole thing a more idiomatic
213 // class in one go is a very large change. Given how bad our testing is,
214 // it's better to do this piece by piece.
215
Josh Gaofc2e56f2018-08-30 11:37:00 -0700216 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700217
218 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700219 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700220 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700221 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700222 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700223 connection_(nullptr),
224 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800225 // Initialize protocol to min version for compatibility with older versions.
226 // Version will be updated post-connect.
227 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700228 max_payload = MAX_PAYLOAD;
229 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700230 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700231 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700232 virtual ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700233
Yabin Cuib5e11412017-03-10 16:01:01 -0800234 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -0700235 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700236 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700237
Yabin Cuib5e11412017-03-10 16:01:01 -0800238 // ConnectionState can be read by all threads, but can only be written in the main thread.
239 ConnectionState GetConnectionState() const;
240 void SetConnectionState(ConnectionState state);
241
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700242 void SetConnection(std::unique_ptr<Connection> connection);
243 std::shared_ptr<Connection> connection() {
244 std::lock_guard<std::mutex> lock(mutex_);
245 return connection_;
246 }
247
Josh Gaoce5ce872018-12-11 13:11:52 -0800248 void SetUsbHandle(usb_handle* h) { usb_handle_ = h; }
249 usb_handle* GetUsbHandle() { return usb_handle_; }
250
Josh Gaob122b172017-08-16 16:57:01 -0700251 const TransportId id;
Josh Gaoe48ecce2017-09-13 13:40:57 -0700252 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700253 bool online = false;
254 TransportType type = kTransportAny;
255
Dan Albert1792c232015-05-18 13:06:53 -0700256 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700257 std::string serial;
258 std::string product;
259 std::string model;
260 std::string device;
261 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700262
Josh Gaob800d882018-01-28 20:32:46 -0800263 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700264
Josh Gao3bd28792016-10-05 19:02:29 -0700265#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700266 std::shared_ptr<RSA> NextKey();
Josh Gao4414e4c2018-12-04 01:07:50 -0800267 void ResetKeys();
Josh Gao3bd28792016-10-05 19:02:29 -0700268#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700269
Josh Gao06d61d42016-10-06 13:31:44 -0700270 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700271 size_t failed_auth_attempts = 0;
272
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700273 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800274 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700275
276 void update_version(int version, size_t payload);
277 int get_protocol_version() const;
278 size_t get_max_payload() const;
279
David Pursell4e2fd362015-09-22 10:43:08 -0700280 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700281 return features_;
282 }
283
284 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700285
286 // Loads the transport's feature set from the given string.
287 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700288
Yabin Cuib3298242015-08-28 15:09:44 -0700289 void AddDisconnect(adisconnect* disconnect);
290 void RemoveDisconnect(adisconnect* disconnect);
291 void RunDisconnects();
292
David Pursell3f902aa2016-03-01 08:58:26 -0800293 // Returns true if |target| matches this transport. A matching |target| can be any of:
294 // * <serial>
295 // * <devpath>
296 // * product:<product>
297 // * model:<model>
298 // * device:<device>
299 //
300 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
301 // For example, serial "100.100.100.100:5555" would match any of:
302 // * 100.100.100.100
303 // * tcp:100.100.100.100
304 // * udp:100.100.100.100:5555
305 // This is to make it easier to use the same network target for both fastboot and adb.
306 bool MatchesTarget(const std::string& target) const;
307
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700308 // Notifies that the atransport is no longer waiting for the connection
309 // being established.
310 void SetConnectionEstablished(bool success);
311
312 // Gets a shared reference to the ConnectionWaitable.
313 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
314
Josh Gaofc2e56f2018-08-30 11:37:00 -0700315 // Attempts to reconnect with the underlying Connection.
316 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700317
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700318 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700319 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700320
Dan Albert1792c232015-05-18 13:06:53 -0700321 // A set of features transmitted in the banner with the initial connection.
322 // This is stored in the banner as 'features=feature0,feature1,etc'.
323 FeatureSet features_;
324 int protocol_version;
325 size_t max_payload;
326
Yabin Cuib3298242015-08-28 15:09:44 -0700327 // A list of adisconnect callbacks called when the transport is kicked.
328 std::list<adisconnect*> disconnects_;
329
Yabin Cuib5e11412017-03-10 16:01:01 -0800330 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700331#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700332 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700333#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700334
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700335 // A sharable object that can be used to wait for the atransport's
336 // connection to be established.
337 std::shared_ptr<ConnectionWaitable> connection_waitable_;
338
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700339 // The underlying connection object.
340 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
341
Josh Gaoce5ce872018-12-11 13:11:52 -0800342 // USB handle for the connection, if available.
343 usb_handle* usb_handle_ = nullptr;
344
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700345 // A callback that will be invoked when the atransport needs to reconnect.
346 ReconnectCallback reconnect_;
347
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700348 std::mutex mutex_;
349
Dan Albert1792c232015-05-18 13:06:53 -0700350 DISALLOW_COPY_AND_ASSIGN(atransport);
351};
352
Dan Albert76649012015-02-24 15:51:19 -0800353/*
354 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700355 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700356 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700357 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
358 * is set to true and nullptr returned.
359 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800360 */
Josh Gaob122b172017-08-16 16:57:01 -0700361atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
362 bool* is_ambiguous, std::string* error_out,
363 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800364void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800365void update_transports(void);
366
Josh Gaofd713e52017-05-03 22:37:10 -0700367// Iterates across all of the current and pending transports.
368// Stops iteration and returns false if fn returns false, otherwise returns true.
369bool iterate_transports(std::function<bool(const atransport*)> fn);
370
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700371void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800372void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700373void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700374std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800375atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700376void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700377void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800378
Josh Gaoc51726c2018-10-11 16:33:05 -0700379void register_transport(atransport* transport);
Dan Albert76649012015-02-24 15:51:19 -0800380void register_usb_transport(usb_handle* h, const char* serial,
381 const char* devpath, unsigned writeable);
382
Casey Dahlin13a269e2016-06-23 14:19:37 -0700383/* Connect to a network address and register it as a device */
384void connect_device(const std::string& address, std::string* response);
385
Dan Albert76649012015-02-24 15:51:19 -0800386/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700387bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
388 atransport::ReconnectCallback reconnect, int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800389
Dan Albertdcd78a12015-05-18 16:43:57 -0700390// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800391void unregister_usb_transport(usb_handle* usb);
392
Josh Gao36dadca2017-05-16 15:02:45 -0700393bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800394
Dan Albert76649012015-02-24 15:51:19 -0800395void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700396void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800397
398void send_packet(apacket* p, atransport* t);
399
Josh Gaob0c18022017-08-14 18:57:54 -0700400asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800401
Josh Gao776c2ec2019-01-22 19:36:15 -0800402#if !ADB_HOST
403void server_socket_thread(int port);
404
405#if defined(__ANDROID__)
406void qemu_socket_thread(int port);
407bool use_qemu_goldfish();
408#endif
409
410#endif
411
JP Abgrall408fa572011-03-16 15:57:42 -0700412#endif /* __TRANSPORT_H */