blob: 9894bdf8316856e4193c4ea0e61fb2b57e01a246 [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"
40
Dan Albert1792c232015-05-18 13:06:53 -070041typedef std::unordered_set<std::string> FeatureSet;
42
43const FeatureSet& supported_features();
44
David Pursell4e2fd362015-09-22 10:43:08 -070045// Encodes and decodes FeatureSet objects into human-readable strings.
46std::string FeatureSetToString(const FeatureSet& features);
47FeatureSet StringToFeatureSet(const std::string& features_string);
48
David Pursell70ef7b42015-09-30 13:35:42 -070049// Returns true if both local features and |feature_set| support |feature|.
50bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
51
David Pursell4e2fd362015-09-22 10:43:08 -070052// Do not use any of [:;=,] in feature strings, they have special meaning
53// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080054extern const char* const kFeatureShell2;
55// The 'cmd' command is available
56extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080057extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080058// The server is running with libusb enabled.
59extern const char* const kFeatureLibusb;
Josh Gaofb085102018-10-22 13:00:05 -070060// adbd supports `push --sync`.
Dan Albert5176df82017-05-23 14:30:00 -070061extern const char* const kFeaturePushSync;
Josh Gaofb085102018-10-22 13:00:05 -070062// adbd supports installing .apex packages.
Dario Freni29814de2018-10-04 16:26:40 +010063extern const char* const kFeatureApex;
Josh Gaofb085102018-10-22 13:00:05 -070064// adbd has b/110953234 fixed.
65extern const char* const kFeatureFixedPushMkdir;
David Pursell0955c662015-08-31 10:42:13 -070066
Josh Gaob122b172017-08-16 16:57:01 -070067TransportId NextTransportId();
68
Josh Gao0bbf69c2018-02-16 13:24:58 -080069// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080070struct Connection {
71 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080072 virtual ~Connection() = default;
73
Josh Gao0bbf69c2018-02-16 13:24:58 -080074 void SetTransportName(std::string transport_name) {
75 transport_name_ = std::move(transport_name);
76 }
77
78 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
79 void SetReadCallback(ReadCallback callback) {
80 CHECK(!read_callback_);
81 read_callback_ = callback;
82 }
83
84 // Called after the Connection has terminated, either by an error or because Stop was called.
85 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
86 void SetErrorCallback(ErrorCallback callback) {
87 CHECK(!error_callback_);
88 error_callback_ = callback;
89 }
90
91 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
92
93 virtual void Start() = 0;
94 virtual void Stop() = 0;
95
96 std::string transport_name_;
97 ReadCallback read_callback_;
98 ErrorCallback error_callback_;
Josh Gao6082e7d2018-04-05 16:16:04 -070099
100 static std::unique_ptr<Connection> FromFd(unique_fd fd);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800101};
102
103// Abstraction for a blocking packet transport.
104struct BlockingConnection {
105 BlockingConnection() = default;
106 BlockingConnection(const BlockingConnection& copy) = delete;
107 BlockingConnection(BlockingConnection&& move) = delete;
108
109 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
110 virtual ~BlockingConnection() = default;
111
Josh Gaob800d882018-01-28 20:32:46 -0800112 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
113 // threads.
114 virtual bool Read(apacket* packet) = 0;
115 virtual bool Write(apacket* packet) = 0;
116
117 // Terminate a connection.
118 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
119 // Formerly known as 'Kick' in atransport.
120 virtual void Close() = 0;
121};
122
Josh Gao0bbf69c2018-02-16 13:24:58 -0800123struct BlockingConnectionAdapter : public Connection {
124 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
125
126 virtual ~BlockingConnectionAdapter();
127
128 virtual bool Write(std::unique_ptr<apacket> packet) override final;
129
130 virtual void Start() override final;
131 virtual void Stop() override final;
132
Josh Gaoc251ec52018-04-03 12:55:18 -0700133 bool started_ GUARDED_BY(mutex_) = false;
134 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao0bbf69c2018-02-16 13:24:58 -0800135
136 std::unique_ptr<BlockingConnection> underlying_;
Josh Gaoc251ec52018-04-03 12:55:18 -0700137 std::thread read_thread_ GUARDED_BY(mutex_);
138 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800139
Josh Gaoc251ec52018-04-03 12:55:18 -0700140 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao0bbf69c2018-02-16 13:24:58 -0800141 std::mutex mutex_;
142 std::condition_variable cv_;
143
144 std::once_flag error_flag_;
145};
146
147struct FdConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800148 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
149
150 bool Read(apacket* packet) override final;
151 bool Write(apacket* packet) override final;
152
153 void Close() override;
154
155 private:
156 unique_fd fd_;
157};
158
Josh Gao0bbf69c2018-02-16 13:24:58 -0800159struct UsbConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800160 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
161 ~UsbConnection();
162
163 bool Read(apacket* packet) override final;
164 bool Write(apacket* packet) override final;
165
166 void Close() override final;
167
168 usb_handle* handle_;
169};
170
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700171// Waits for a transport's connection to be not pending. This is a separate
172// object so that the transport can be destroyed and another thread can be
173// notified of it in a race-free way.
174class ConnectionWaitable {
175 public:
176 ConnectionWaitable() = default;
177 ~ConnectionWaitable() = default;
178
179 // Waits until the first CNXN packet has been received by the owning
180 // atransport, or the specified timeout has elapsed. Can be called from any
181 // thread.
182 //
183 // Returns true if the CNXN packet was received in a timely fashion, false
184 // otherwise.
185 bool WaitForConnection(std::chrono::milliseconds timeout);
186
187 // Can be called from any thread when the connection stops being pending.
188 // Only the first invocation will be acknowledged, the rest will be no-ops.
189 void SetConnectionEstablished(bool success);
190
191 private:
192 bool connection_established_ GUARDED_BY(mutex_) = false;
193 bool connection_established_ready_ GUARDED_BY(mutex_) = false;
194 std::mutex mutex_;
195 std::condition_variable cv_;
196
197 DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable);
198};
199
Josh Gaofc2e56f2018-08-30 11:37:00 -0700200enum class ReconnectResult {
201 Retry,
202 Success,
203 Abort,
204};
205
Dan Albert1792c232015-05-18 13:06:53 -0700206class atransport {
Josh Gaob122b172017-08-16 16:57:01 -0700207 public:
Dan Albert1792c232015-05-18 13:06:53 -0700208 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
209 // historically just a struct, but making the whole thing a more idiomatic
210 // class in one go is a very large change. Given how bad our testing is,
211 // it's better to do this piece by piece.
212
Josh Gaofc2e56f2018-08-30 11:37:00 -0700213 using ReconnectCallback = std::function<ReconnectResult(atransport*)>;
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700214
215 atransport(ReconnectCallback reconnect, ConnectionState state)
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700216 : id(NextTransportId()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700217 kicked_(false),
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700218 connection_state_(state),
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700219 connection_waitable_(std::make_shared<ConnectionWaitable>()),
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700220 connection_(nullptr),
221 reconnect_(std::move(reconnect)) {
Tim Murrayde471942017-12-07 11:40:00 -0800222 // Initialize protocol to min version for compatibility with older versions.
223 // Version will be updated post-connect.
224 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700225 max_payload = MAX_PAYLOAD;
226 }
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700227 atransport(ConnectionState state = kCsOffline)
Josh Gaofc2e56f2018-08-30 11:37:00 -0700228 : atransport([](atransport*) { return ReconnectResult::Abort; }, state) {}
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700229 virtual ~atransport();
Dan Albert1792c232015-05-18 13:06:53 -0700230
Yabin Cuib5e11412017-03-10 16:01:01 -0800231 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -0700232 void Kick();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700233 bool kicked() const { return kicked_; }
Dan Albert1792c232015-05-18 13:06:53 -0700234
Yabin Cuib5e11412017-03-10 16:01:01 -0800235 // ConnectionState can be read by all threads, but can only be written in the main thread.
236 ConnectionState GetConnectionState() const;
237 void SetConnectionState(ConnectionState state);
238
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700239 void SetConnection(std::unique_ptr<Connection> connection);
240 std::shared_ptr<Connection> connection() {
241 std::lock_guard<std::mutex> lock(mutex_);
242 return connection_;
243 }
244
Josh Gaob122b172017-08-16 16:57:01 -0700245 const TransportId id;
Josh Gaoe48ecce2017-09-13 13:40:57 -0700246 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700247 bool online = false;
248 TransportType type = kTransportAny;
249
Dan Albert1792c232015-05-18 13:06:53 -0700250 // Used to identify transports for clients.
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700251 std::string serial;
252 std::string product;
253 std::string model;
254 std::string device;
255 std::string devpath;
Yabin Cuib74c6492016-04-29 16:53:52 -0700256
Josh Gaob800d882018-01-28 20:32:46 -0800257 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700258
Josh Gao3bd28792016-10-05 19:02:29 -0700259#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700260 std::shared_ptr<RSA> NextKey();
Josh Gao3bd28792016-10-05 19:02:29 -0700261#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700262
Josh Gao06d61d42016-10-06 13:31:44 -0700263 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700264 size_t failed_auth_attempts = 0;
265
Luis Hector Chavez6150a372018-07-18 21:18:27 -0700266 std::string serial_name() const { return !serial.empty() ? serial : "<unknown>"; }
Josh Gaoffbd3362018-02-28 14:44:23 -0800267 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700268
269 void update_version(int version, size_t payload);
270 int get_protocol_version() const;
271 size_t get_max_payload() const;
272
David Pursell4e2fd362015-09-22 10:43:08 -0700273 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700274 return features_;
275 }
276
277 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700278
279 // Loads the transport's feature set from the given string.
280 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700281
Yabin Cuib3298242015-08-28 15:09:44 -0700282 void AddDisconnect(adisconnect* disconnect);
283 void RemoveDisconnect(adisconnect* disconnect);
284 void RunDisconnects();
285
David Pursell3f902aa2016-03-01 08:58:26 -0800286 // Returns true if |target| matches this transport. A matching |target| can be any of:
287 // * <serial>
288 // * <devpath>
289 // * product:<product>
290 // * model:<model>
291 // * device:<device>
292 //
293 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
294 // For example, serial "100.100.100.100:5555" would match any of:
295 // * 100.100.100.100
296 // * tcp:100.100.100.100
297 // * udp:100.100.100.100:5555
298 // This is to make it easier to use the same network target for both fastboot and adb.
299 bool MatchesTarget(const std::string& target) const;
300
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700301 // Notifies that the atransport is no longer waiting for the connection
302 // being established.
303 void SetConnectionEstablished(bool success);
304
305 // Gets a shared reference to the ConnectionWaitable.
306 std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; }
307
Josh Gaofc2e56f2018-08-30 11:37:00 -0700308 // Attempts to reconnect with the underlying Connection.
309 ReconnectResult Reconnect();
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700310
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700311 private:
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700312 std::atomic<bool> kicked_;
Yabin Cui7f274902016-04-18 11:22:34 -0700313
Dan Albert1792c232015-05-18 13:06:53 -0700314 // A set of features transmitted in the banner with the initial connection.
315 // This is stored in the banner as 'features=feature0,feature1,etc'.
316 FeatureSet features_;
317 int protocol_version;
318 size_t max_payload;
319
Yabin Cuib3298242015-08-28 15:09:44 -0700320 // A list of adisconnect callbacks called when the transport is kicked.
321 std::list<adisconnect*> disconnects_;
322
Yabin Cuib5e11412017-03-10 16:01:01 -0800323 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700324#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700325 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700326#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700327
Luis Hector Chavez56fe7532018-04-17 14:25:04 -0700328 // A sharable object that can be used to wait for the atransport's
329 // connection to be established.
330 std::shared_ptr<ConnectionWaitable> connection_waitable_;
331
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700332 // The underlying connection object.
333 std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_);
334
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700335 // A callback that will be invoked when the atransport needs to reconnect.
336 ReconnectCallback reconnect_;
337
Luis Hector Chavez9a388d52018-04-25 08:56:41 -0700338 std::mutex mutex_;
339
Dan Albert1792c232015-05-18 13:06:53 -0700340 DISALLOW_COPY_AND_ASSIGN(atransport);
341};
342
Dan Albert76649012015-02-24 15:51:19 -0800343/*
344 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700345 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700346 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700347 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
348 * is set to true and nullptr returned.
349 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800350 */
Josh Gaob122b172017-08-16 16:57:01 -0700351atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
352 bool* is_ambiguous, std::string* error_out,
353 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800354void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800355void update_transports(void);
356
Josh Gaofd713e52017-05-03 22:37:10 -0700357// Iterates across all of the current and pending transports.
358// Stops iteration and returns false if fn returns false, otherwise returns true.
359bool iterate_transports(std::function<bool(const atransport*)> fn);
360
Luis Hector Chavez454bc7c2018-04-20 10:31:29 -0700361void init_reconnect_handler(void);
Dan Albert76649012015-02-24 15:51:19 -0800362void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700363void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700364std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800365atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700366void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700367void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800368
Josh Gaoc51726c2018-10-11 16:33:05 -0700369void register_transport(atransport* transport);
Dan Albert76649012015-02-24 15:51:19 -0800370void register_usb_transport(usb_handle* h, const char* serial,
371 const char* devpath, unsigned writeable);
372
Casey Dahlin13a269e2016-06-23 14:19:37 -0700373/* Connect to a network address and register it as a device */
374void connect_device(const std::string& address, std::string* response);
375
Dan Albert76649012015-02-24 15:51:19 -0800376/* cause new transports to be init'd and added to the list */
Josh Gao362e6962018-08-08 16:20:14 -0700377bool register_socket_transport(unique_fd s, std::string serial, int port, int local,
378 atransport::ReconnectCallback reconnect, int* error = nullptr);
Dan Albert76649012015-02-24 15:51:19 -0800379
Dan Albertdcd78a12015-05-18 16:43:57 -0700380// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800381void unregister_usb_transport(usb_handle* usb);
382
Josh Gao36dadca2017-05-16 15:02:45 -0700383bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800384
Dan Albert76649012015-02-24 15:51:19 -0800385void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700386void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800387
388void send_packet(apacket* p, atransport* t);
389
Josh Gaob0c18022017-08-14 18:57:54 -0700390asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800391
JP Abgrall408fa572011-03-16 15:57:42 -0700392#endif /* __TRANSPORT_H */