blob: 8b71e34059ab9e0c01ea8c0619e6664eff6c40f6 [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>
Josh Gao0bbf69c2018-02-16 13:24:58 -080023#include <condition_variable>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070024#include <deque>
Josh Gao22d2b3e2016-10-27 14:01:08 -070025#include <functional>
Yabin Cuib3298242015-08-28 15:09:44 -070026#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070027#include <memory>
Yabin Cuib5e11412017-03-10 16:01:01 -080028#include <mutex>
Elliott Hughes7be29c82015-04-16 22:54:44 -070029#include <string>
Josh Gao0bbf69c2018-02-16 13:24:58 -080030#include <thread>
Dan Albert1792c232015-05-18 13:06:53 -070031#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080032
Elliott Hughes0aeb5052016-06-29 17:42:01 -070033#include <openssl/rsa.h>
34
Josh Gaob800d882018-01-28 20:32:46 -080035#include "adb.h"
36#include "adb_unique_fd.h"
37
Dan Albert1792c232015-05-18 13:06:53 -070038typedef std::unordered_set<std::string> FeatureSet;
39
40const FeatureSet& supported_features();
41
David Pursell4e2fd362015-09-22 10:43:08 -070042// Encodes and decodes FeatureSet objects into human-readable strings.
43std::string FeatureSetToString(const FeatureSet& features);
44FeatureSet StringToFeatureSet(const std::string& features_string);
45
David Pursell70ef7b42015-09-30 13:35:42 -070046// Returns true if both local features and |feature_set| support |feature|.
47bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
48
David Pursell4e2fd362015-09-22 10:43:08 -070049// Do not use any of [:;=,] in feature strings, they have special meaning
50// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080051extern const char* const kFeatureShell2;
52// The 'cmd' command is available
53extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080054extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080055// The server is running with libusb enabled.
56extern const char* const kFeatureLibusb;
Dan Albert5176df82017-05-23 14:30:00 -070057// The server supports `push --sync`.
58extern const char* const kFeaturePushSync;
David Pursell0955c662015-08-31 10:42:13 -070059
Josh Gaob122b172017-08-16 16:57:01 -070060TransportId NextTransportId();
61
Josh Gao0bbf69c2018-02-16 13:24:58 -080062// Abstraction for a non-blocking packet transport.
Josh Gaob800d882018-01-28 20:32:46 -080063struct Connection {
64 Connection() = default;
Josh Gaob800d882018-01-28 20:32:46 -080065 virtual ~Connection() = default;
66
Josh Gao0bbf69c2018-02-16 13:24:58 -080067 void SetTransportName(std::string transport_name) {
68 transport_name_ = std::move(transport_name);
69 }
70
71 using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>;
72 void SetReadCallback(ReadCallback callback) {
73 CHECK(!read_callback_);
74 read_callback_ = callback;
75 }
76
77 // Called after the Connection has terminated, either by an error or because Stop was called.
78 using ErrorCallback = std::function<void(Connection*, const std::string&)>;
79 void SetErrorCallback(ErrorCallback callback) {
80 CHECK(!error_callback_);
81 error_callback_ = callback;
82 }
83
84 virtual bool Write(std::unique_ptr<apacket> packet) = 0;
85
86 virtual void Start() = 0;
87 virtual void Stop() = 0;
88
89 std::string transport_name_;
90 ReadCallback read_callback_;
91 ErrorCallback error_callback_;
92};
93
94// Abstraction for a blocking packet transport.
95struct BlockingConnection {
96 BlockingConnection() = default;
97 BlockingConnection(const BlockingConnection& copy) = delete;
98 BlockingConnection(BlockingConnection&& move) = delete;
99
100 // Destroy a BlockingConnection. Formerly known as 'Close' in atransport.
101 virtual ~BlockingConnection() = default;
102
Josh Gaob800d882018-01-28 20:32:46 -0800103 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
104 // threads.
105 virtual bool Read(apacket* packet) = 0;
106 virtual bool Write(apacket* packet) = 0;
107
108 // Terminate a connection.
109 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
110 // Formerly known as 'Kick' in atransport.
111 virtual void Close() = 0;
112};
113
Josh Gao0bbf69c2018-02-16 13:24:58 -0800114struct BlockingConnectionAdapter : public Connection {
115 explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection);
116
117 virtual ~BlockingConnectionAdapter();
118
119 virtual bool Write(std::unique_ptr<apacket> packet) override final;
120
121 virtual void Start() override final;
122 virtual void Stop() override final;
123
124 bool stopped_ = false;
125
126 std::unique_ptr<BlockingConnection> underlying_;
127 std::thread read_thread_;
128 std::thread write_thread_;
129
130 std::deque<std::unique_ptr<apacket>> write_queue_;
131 std::mutex mutex_;
132 std::condition_variable cv_;
133
134 std::once_flag error_flag_;
135};
136
137struct FdConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800138 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
139
140 bool Read(apacket* packet) override final;
141 bool Write(apacket* packet) override final;
142
143 void Close() override;
144
145 private:
146 unique_fd fd_;
147};
148
Josh Gao0bbf69c2018-02-16 13:24:58 -0800149struct UsbConnection : public BlockingConnection {
Josh Gaob800d882018-01-28 20:32:46 -0800150 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
151 ~UsbConnection();
152
153 bool Read(apacket* packet) override final;
154 bool Write(apacket* packet) override final;
155
156 void Close() override final;
157
158 usb_handle* handle_;
159};
160
Dan Albert1792c232015-05-18 13:06:53 -0700161class atransport {
Josh Gaob122b172017-08-16 16:57:01 -0700162 public:
Dan Albert1792c232015-05-18 13:06:53 -0700163 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
164 // historically just a struct, but making the whole thing a more idiomatic
165 // class in one go is a very large change. Given how bad our testing is,
166 // it's better to do this piece by piece.
167
Josh Gaob122b172017-08-16 16:57:01 -0700168 atransport(ConnectionState state = kCsOffline)
Josh Gaoe48ecce2017-09-13 13:40:57 -0700169 : id(NextTransportId()), connection_state_(state) {
Tim Murrayde471942017-12-07 11:40:00 -0800170 // Initialize protocol to min version for compatibility with older versions.
171 // Version will be updated post-connect.
172 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700173 max_payload = MAX_PAYLOAD;
174 }
Dan Albert1792c232015-05-18 13:06:53 -0700175 virtual ~atransport() {}
176
Yabin Cuib5e11412017-03-10 16:01:01 -0800177 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -0700178 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -0700179
Yabin Cuib5e11412017-03-10 16:01:01 -0800180 // ConnectionState can be read by all threads, but can only be written in the main thread.
181 ConnectionState GetConnectionState() const;
182 void SetConnectionState(ConnectionState state);
183
Josh Gaob122b172017-08-16 16:57:01 -0700184 const TransportId id;
Josh Gaoe48ecce2017-09-13 13:40:57 -0700185 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700186 bool online = false;
187 TransportType type = kTransportAny;
188
Josh Gaob800d882018-01-28 20:32:46 -0800189 std::unique_ptr<Connection> connection;
Dan Albert1792c232015-05-18 13:06:53 -0700190
191 // Used to identify transports for clients.
192 char* serial = nullptr;
193 char* product = nullptr;
194 char* model = nullptr;
195 char* device = nullptr;
196 char* devpath = nullptr;
Yabin Cuib74c6492016-04-29 16:53:52 -0700197
Josh Gaob800d882018-01-28 20:32:46 -0800198 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700199
Josh Gao3bd28792016-10-05 19:02:29 -0700200#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700201 std::shared_ptr<RSA> NextKey();
Josh Gao3bd28792016-10-05 19:02:29 -0700202#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700203
Josh Gao06d61d42016-10-06 13:31:44 -0700204 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700205 size_t failed_auth_attempts = 0;
206
Josh Gaoffbd3362018-02-28 14:44:23 -0800207 std::string serial_name() const { return serial ? serial : "<unknown>"; }
208 std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700209
210 void update_version(int version, size_t payload);
211 int get_protocol_version() const;
212 size_t get_max_payload() const;
213
David Pursell4e2fd362015-09-22 10:43:08 -0700214 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700215 return features_;
216 }
217
218 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700219
220 // Loads the transport's feature set from the given string.
221 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700222
Yabin Cuib3298242015-08-28 15:09:44 -0700223 void AddDisconnect(adisconnect* disconnect);
224 void RemoveDisconnect(adisconnect* disconnect);
225 void RunDisconnects();
226
David Pursell3f902aa2016-03-01 08:58:26 -0800227 // Returns true if |target| matches this transport. A matching |target| can be any of:
228 // * <serial>
229 // * <devpath>
230 // * product:<product>
231 // * model:<model>
232 // * device:<device>
233 //
234 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
235 // For example, serial "100.100.100.100:5555" would match any of:
236 // * 100.100.100.100
237 // * tcp:100.100.100.100
238 // * udp:100.100.100.100:5555
239 // This is to make it easier to use the same network target for both fastboot and adb.
240 bool MatchesTarget(const std::string& target) const;
241
Dan Albert1792c232015-05-18 13:06:53 -0700242private:
Yabin Cui7f274902016-04-18 11:22:34 -0700243 bool kicked_ = false;
Yabin Cui7f274902016-04-18 11:22:34 -0700244
Dan Albert1792c232015-05-18 13:06:53 -0700245 // A set of features transmitted in the banner with the initial connection.
246 // This is stored in the banner as 'features=feature0,feature1,etc'.
247 FeatureSet features_;
248 int protocol_version;
249 size_t max_payload;
250
Yabin Cuib3298242015-08-28 15:09:44 -0700251 // A list of adisconnect callbacks called when the transport is kicked.
252 std::list<adisconnect*> disconnects_;
253
Yabin Cuib5e11412017-03-10 16:01:01 -0800254 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700255#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700256 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700257#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700258
Dan Albert1792c232015-05-18 13:06:53 -0700259 DISALLOW_COPY_AND_ASSIGN(atransport);
260};
261
Dan Albert76649012015-02-24 15:51:19 -0800262/*
263 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700264 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700265 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700266 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
267 * is set to true and nullptr returned.
268 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800269 */
Josh Gaob122b172017-08-16 16:57:01 -0700270atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
271 bool* is_ambiguous, std::string* error_out,
272 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800273void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800274void update_transports(void);
275
Josh Gaofd713e52017-05-03 22:37:10 -0700276// Iterates across all of the current and pending transports.
277// Stops iteration and returns false if fn returns false, otherwise returns true.
278bool iterate_transports(std::function<bool(const atransport*)> fn);
279
Dan Albert76649012015-02-24 15:51:19 -0800280void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700281void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700282std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800283atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700284void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700285void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800286
287void register_usb_transport(usb_handle* h, const char* serial,
288 const char* devpath, unsigned writeable);
289
Casey Dahlin13a269e2016-06-23 14:19:37 -0700290/* Connect to a network address and register it as a device */
291void connect_device(const std::string& address, std::string* response);
292
Dan Albert76649012015-02-24 15:51:19 -0800293/* cause new transports to be init'd and added to the list */
294int register_socket_transport(int s, const char* serial, int port, int local);
295
Dan Albertdcd78a12015-05-18 16:43:57 -0700296// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800297void unregister_usb_transport(usb_handle* usb);
298
Josh Gao36dadca2017-05-16 15:02:45 -0700299bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800300
Dan Albert76649012015-02-24 15:51:19 -0800301void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700302void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800303
304void send_packet(apacket* p, atransport* t);
305
Josh Gaob0c18022017-08-14 18:57:54 -0700306asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800307
JP Abgrall408fa572011-03-16 15:57:42 -0700308#endif /* __TRANSPORT_H */