blob: d18c362325502879f6eb6d1a76a670953678ee0a [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>
Josh Gao715fe602018-02-16 13:24:58 -080023#include <condition_variable>
Elliott Hughes801066a2016-06-29 17:42:01 -070024#include <deque>
Josh Gao4e562502016-10-27 14:01:08 -070025#include <functional>
Yabin Cui2d4c1982015-08-28 15:09:44 -070026#include <list>
Josh Gao22cb70b2016-08-18 22:00:12 -070027#include <memory>
Yabin Cui3cf1b362017-03-10 16:01:01 -080028#include <mutex>
Elliott Hughesab882422015-04-16 22:54:44 -070029#include <string>
Josh Gao715fe602018-02-16 13:24:58 -080030#include <thread>
Dan Albertbe8e54b2015-05-18 13:06:53 -070031#include <unordered_set>
Dan Albertb302d122015-02-24 15:51:19 -080032
Josh Gao13781e82018-04-03 12:55:18 -070033#include <android-base/thread_annotations.h>
Elliott Hughes801066a2016-06-29 17:42:01 -070034#include <openssl/rsa.h>
35
Josh Gao395b86a2018-01-28 20:32:46 -080036#include "adb.h"
37#include "adb_unique_fd.h"
38
Dan Albertbe8e54b2015-05-18 13:06:53 -070039typedef std::unordered_set<std::string> FeatureSet;
40
41const FeatureSet& supported_features();
42
David Pursella07dbad2015-09-22 10:43:08 -070043// Encodes and decodes FeatureSet objects into human-readable strings.
44std::string FeatureSetToString(const FeatureSet& features);
45FeatureSet StringToFeatureSet(const std::string& features_string);
46
David Pursell22fc5e92015-09-30 13:35:42 -070047// Returns true if both local features and |feature_set| support |feature|.
48bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
49
David Pursella07dbad2015-09-22 10:43:08 -070050// Do not use any of [:;=,] in feature strings, they have special meaning
51// in the connection banner.
Todd Kennedy1e2f7dc2015-11-03 16:53:08 -080052extern const char* const kFeatureShell2;
53// The 'cmd' command is available
54extern const char* const kFeatureCmd;
Josh Gaoa2cf3752016-12-05 17:11:34 -080055extern const char* const kFeatureStat2;
Josh Gao210b63f2017-02-22 17:07:01 -080056// The server is running with libusb enabled.
57extern const char* const kFeatureLibusb;
Dan Albert27983bc2017-05-23 14:30:00 -070058// The server supports `push --sync`.
59extern const char* const kFeaturePushSync;
David Pursell8da19a42015-08-31 10:42:13 -070060
Josh Gaob39e4152017-08-16 16:57:01 -070061TransportId NextTransportId();
62
Josh Gao715fe602018-02-16 13:24:58 -080063// Abstraction for a non-blocking packet transport.
Josh Gao395b86a2018-01-28 20:32:46 -080064struct Connection {
65 Connection() = default;
Josh Gao395b86a2018-01-28 20:32:46 -080066 virtual ~Connection() = default;
67
Josh Gao715fe602018-02-16 13:24:58 -080068 void SetTransportName(std::string transport_name) {
69 transport_name_ = std::move(transport_name);
70 }
71
72 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
73 void SetReadCallback(ReadCallback callback) {
74 CHECK(!read_callback_);
75 read_callback_ = callback;
76 }
77
78 // Called after the Connection has terminated, either by an error or because Stop was called.
79 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
80 void SetErrorCallback(ErrorCallback callback) {
81 CHECK(!error_callback_);
82 error_callback_ = callback;
83 }
84
85 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
86
87 virtual void Start() = 0;
88 virtual void Stop() = 0;
89
90 std::string transport_name_;
91 ReadCallback read_callback_;
92 ErrorCallback error_callback_;
93};
94
95// Abstraction for a blocking packet transport.
96struct BlockingConnection {
97 BlockingConnection() = default;
98 BlockingConnection(const BlockingConnection& copy) = delete;
99 BlockingConnection(BlockingConnection&& move) = delete;
100
101 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
102 virtual ~BlockingConnection() = default;
103
Josh Gao395b86a2018-01-28 20:32:46 -0800104 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
105 // threads.
106 virtual bool Read(apacket* packet) = 0;
107 virtual bool Write(apacket* packet) = 0;
108
109 // Terminate a connection.
110 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
111 // Formerly known as 'Kick' in atransport.
112 virtual void Close() = 0;
113};
114
Josh Gao715fe602018-02-16 13:24:58 -0800115struct BlockingConnectionAdapter : public Connection {
116 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
117
118 virtual ~BlockingConnectionAdapter();
119
120 virtual bool Write(std::unique_ptr<apacket> packet) override final;
121
122 virtual void Start() override final;
123 virtual void Stop() override final;
124
Josh Gao13781e82018-04-03 12:55:18 -0700125 bool started_ GUARDED_BY(mutex_) = false;
126 bool stopped_ GUARDED_BY(mutex_) = false;
Josh Gao715fe602018-02-16 13:24:58 -0800127
128 std::unique_ptr<BlockingConnection> underlying_;
Josh Gao13781e82018-04-03 12:55:18 -0700129 std::thread read_thread_ GUARDED_BY(mutex_);
130 std::thread write_thread_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800131
Josh Gao13781e82018-04-03 12:55:18 -0700132 std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_);
Josh Gao715fe602018-02-16 13:24:58 -0800133 std::mutex mutex_;
134 std::condition_variable cv_;
135
136 std::once_flag error_flag_;
137};
138
139struct FdConnection : public BlockingConnection {
Josh Gao395b86a2018-01-28 20:32:46 -0800140 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
141
142 bool Read(apacket* packet) override final;
143 bool Write(apacket* packet) override final;
144
145 void Close() override;
146
147 private:
148 unique_fd fd_;
149};
150
Josh Gao715fe602018-02-16 13:24:58 -0800151struct UsbConnection : public BlockingConnection {
Josh Gao395b86a2018-01-28 20:32:46 -0800152 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
153 ~UsbConnection();
154
155 bool Read(apacket* packet) override final;
156 bool Write(apacket* packet) override final;
157
158 void Close() override final;
159
160 usb_handle* handle_;
161};
162
Dan Albertbe8e54b2015-05-18 13:06:53 -0700163class atransport {
Josh Gaob39e4152017-08-16 16:57:01 -0700164 public:
Dan Albertbe8e54b2015-05-18 13:06:53 -0700165 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
166 // historically just a struct, but making the whole thing a more idiomatic
167 // class in one go is a very large change. Given how bad our testing is,
168 // it's better to do this piece by piece.
169
Josh Gaob39e4152017-08-16 16:57:01 -0700170 atransport(ConnectionState state = kCsOffline)
Josh Gao4d299742017-09-13 13:40:57 -0700171 : id(NextTransportId()), connection_state_(state) {
Tim Murrayee7b44d2017-12-07 11:40:00 -0800172 // Initialize protocol to min version for compatibility with older versions.
173 // Version will be updated post-connect.
174 protocol_version = A_VERSION_MIN;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700175 max_payload = MAX_PAYLOAD;
176 }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700177 virtual ~atransport() {}
178
Yabin Cui3cf1b362017-03-10 16:01:01 -0800179 int Write(apacket* p);
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700180 void Kick();
Dan Albertbe8e54b2015-05-18 13:06:53 -0700181
Yabin Cui3cf1b362017-03-10 16:01:01 -0800182 // ConnectionState can be read by all threads, but can only be written in the main thread.
183 ConnectionState GetConnectionState() const;
184 void SetConnectionState(ConnectionState state);
185
Josh Gaob39e4152017-08-16 16:57:01 -0700186 const TransportId id;
Josh Gao4d299742017-09-13 13:40:57 -0700187 size_t ref_count = 0;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700188 bool online = false;
189 TransportType type = kTransportAny;
190
Josh Gao395b86a2018-01-28 20:32:46 -0800191 std::unique_ptr<Connection> connection;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700192
193 // Used to identify transports for clients.
194 char* serial = nullptr;
195 char* product = nullptr;
196 char* model = nullptr;
197 char* device = nullptr;
198 char* devpath = nullptr;
Yabin Cuif401ead2016-04-29 16:53:52 -0700199
Josh Gao395b86a2018-01-28 20:32:46 -0800200 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700201
Josh Gaoeac20582016-10-05 19:02:29 -0700202#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700203 std::shared_ptr<RSA> NextKey();
Josh Gaoeac20582016-10-05 19:02:29 -0700204#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700205
Josh Gao67ac3792016-10-06 13:31:44 -0700206 char token[TOKEN_SIZE] = {};
Dan Albertbe8e54b2015-05-18 13:06:53 -0700207 size_t failed_auth_attempts = 0;
208
Josh Gao3cd03be2018-02-28 14:44:23 -0800209 std::string serial_name() const { return serial ? serial : "<unknown>"; }
210 std::string connection_state_name() const;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700211
212 void update_version(int version, size_t payload);
213 int get_protocol_version() const;
214 size_t get_max_payload() const;
215
David Pursella07dbad2015-09-22 10:43:08 -0700216 const FeatureSet& features() const {
Dan Albertbe8e54b2015-05-18 13:06:53 -0700217 return features_;
218 }
219
220 bool has_feature(const std::string& feature) const;
David Pursella07dbad2015-09-22 10:43:08 -0700221
222 // Loads the transport's feature set from the given string.
223 void SetFeatures(const std::string& features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700224
Yabin Cui2d4c1982015-08-28 15:09:44 -0700225 void AddDisconnect(adisconnect* disconnect);
226 void RemoveDisconnect(adisconnect* disconnect);
227 void RunDisconnects();
228
David Pursellc929c6f2016-03-01 08:58:26 -0800229 // Returns true if |target| matches this transport. A matching |target| can be any of:
230 // * <serial>
231 // * <devpath>
232 // * product:<product>
233 // * model:<model>
234 // * device:<device>
235 //
236 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
237 // For example, serial "100.100.100.100:5555" would match any of:
238 // * 100.100.100.100
239 // * tcp:100.100.100.100
240 // * udp:100.100.100.100:5555
241 // This is to make it easier to use the same network target for both fastboot and adb.
242 bool MatchesTarget(const std::string& target) const;
243
Dan Albertbe8e54b2015-05-18 13:06:53 -0700244private:
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700245 bool kicked_ = false;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700246
Dan Albertbe8e54b2015-05-18 13:06:53 -0700247 // A set of features transmitted in the banner with the initial connection.
248 // This is stored in the banner as 'features=feature0,feature1,etc'.
249 FeatureSet features_;
250 int protocol_version;
251 size_t max_payload;
252
Yabin Cui2d4c1982015-08-28 15:09:44 -0700253 // A list of adisconnect callbacks called when the transport is kicked.
254 std::list<adisconnect*> disconnects_;
255
Yabin Cui3cf1b362017-03-10 16:01:01 -0800256 std::atomic<ConnectionState> connection_state_;
Josh Gaoeac20582016-10-05 19:02:29 -0700257#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700258 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gaoeac20582016-10-05 19:02:29 -0700259#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700260
Dan Albertbe8e54b2015-05-18 13:06:53 -0700261 DISALLOW_COPY_AND_ASSIGN(atransport);
262};
263
Dan Albertb302d122015-02-24 15:51:19 -0800264/*
265 * Obtain a transport from the available transports.
Elliott Hughes67943d12015-10-07 14:55:10 -0700266 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob39e4152017-08-16 16:57:01 -0700267 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes67943d12015-10-07 14:55:10 -0700268 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
269 * is set to true and nullptr returned.
270 * If no suitable transport is found, error is set and nullptr returned.
Dan Albertb302d122015-02-24 15:51:19 -0800271 */
Josh Gaob39e4152017-08-16 16:57:01 -0700272atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
273 bool* is_ambiguous, std::string* error_out,
274 bool accept_any_state = false);
Dan Albertb302d122015-02-24 15:51:19 -0800275void kick_transport(atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800276void update_transports(void);
277
Josh Gao1e3bf732017-05-03 22:37:10 -0700278// Iterates across all of the current and pending transports.
279// Stops iteration and returns false if fn returns false, otherwise returns true.
280bool iterate_transports(std::function<bool(const atransport*)> fn);
281
Dan Albertb302d122015-02-24 15:51:19 -0800282void init_transport_registration(void);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700283void init_mdns_transport_discovery(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700284std::string list_transports(bool long_listing);
Dan Albertb302d122015-02-24 15:51:19 -0800285atransport* find_transport(const char* serial);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700286void kick_all_tcp_devices();
Josh Gao165460f2017-05-09 13:43:35 -0700287void kick_all_transports();
Dan Albertb302d122015-02-24 15:51:19 -0800288
289void register_usb_transport(usb_handle* h, const char* serial,
290 const char* devpath, unsigned writeable);
291
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700292/* Connect to a network address and register it as a device */
293void connect_device(const std::string& address, std::string* response);
294
Dan Albertb302d122015-02-24 15:51:19 -0800295/* cause new transports to be init'd and added to the list */
296int register_socket_transport(int s, const char* serial, int port, int local);
297
Dan Albert9a50f4c2015-05-18 16:43:57 -0700298// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertb302d122015-02-24 15:51:19 -0800299void unregister_usb_transport(usb_handle* usb);
300
Josh Gao67b683a2017-05-16 15:02:45 -0700301bool check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800302
Dan Albertb302d122015-02-24 15:51:19 -0800303void close_usb_devices();
Josh Gao4e562502016-10-27 14:01:08 -0700304void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albertb302d122015-02-24 15:51:19 -0800305
306void send_packet(apacket* p, atransport* t);
307
Josh Gao32124632017-08-14 18:57:54 -0700308asocket* create_device_tracker(bool long_output);
Dan Albertb302d122015-02-24 15:51:19 -0800309
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700310#endif /* __TRANSPORT_H */