blob: 9700f445bfe3cd1b83c5127fb3d112525d0b6693 [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>
Elliott Hughes0aeb5052016-06-29 17:42:01 -070023#include <deque>
Josh Gao22d2b3e2016-10-27 14:01:08 -070024#include <functional>
Yabin Cuib3298242015-08-28 15:09:44 -070025#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070026#include <memory>
Yabin Cuib5e11412017-03-10 16:01:01 -080027#include <mutex>
Elliott Hughes7be29c82015-04-16 22:54:44 -070028#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070029#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080030
Elliott Hughes0aeb5052016-06-29 17:42:01 -070031#include <openssl/rsa.h>
32
Josh Gaob800d882018-01-28 20:32:46 -080033#include "adb.h"
34#include "adb_unique_fd.h"
35
Dan Albert1792c232015-05-18 13:06:53 -070036typedef std::unordered_set<std::string> FeatureSet;
37
38const FeatureSet& supported_features();
39
David Pursell4e2fd362015-09-22 10:43:08 -070040// Encodes and decodes FeatureSet objects into human-readable strings.
41std::string FeatureSetToString(const FeatureSet& features);
42FeatureSet StringToFeatureSet(const std::string& features_string);
43
David Pursell70ef7b42015-09-30 13:35:42 -070044// Returns true if both local features and |feature_set| support |feature|.
45bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
46
David Pursell4e2fd362015-09-22 10:43:08 -070047// Do not use any of [:;=,] in feature strings, they have special meaning
48// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080049extern const char* const kFeatureShell2;
50// The 'cmd' command is available
51extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080052extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080053// The server is running with libusb enabled.
54extern const char* const kFeatureLibusb;
Dan Albert5176df82017-05-23 14:30:00 -070055// The server supports `push --sync`.
56extern const char* const kFeaturePushSync;
David Pursell0955c662015-08-31 10:42:13 -070057
Josh Gaob122b172017-08-16 16:57:01 -070058TransportId NextTransportId();
59
Josh Gaob800d882018-01-28 20:32:46 -080060// Abstraction for a blocking packet transport.
61struct Connection {
62 Connection() = default;
63 Connection(const Connection& copy) = delete;
64 Connection(Connection&& move) = delete;
65
66 // Destroy a Connection. Formerly known as 'Close' in atransport.
67 virtual ~Connection() = default;
68
69 // Read/Write a packet. These functions are concurrently called from a transport's reader/writer
70 // threads.
71 virtual bool Read(apacket* packet) = 0;
72 virtual bool Write(apacket* packet) = 0;
73
74 // Terminate a connection.
75 // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate.
76 // Formerly known as 'Kick' in atransport.
77 virtual void Close() = 0;
78};
79
80struct FdConnection : public Connection {
81 explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {}
82
83 bool Read(apacket* packet) override final;
84 bool Write(apacket* packet) override final;
85
86 void Close() override;
87
88 private:
89 unique_fd fd_;
90};
91
92struct UsbConnection : public Connection {
93 explicit UsbConnection(usb_handle* handle) : handle_(handle) {}
94 ~UsbConnection();
95
96 bool Read(apacket* packet) override final;
97 bool Write(apacket* packet) override final;
98
99 void Close() override final;
100
101 usb_handle* handle_;
102};
103
Dan Albert1792c232015-05-18 13:06:53 -0700104class atransport {
Josh Gaob122b172017-08-16 16:57:01 -0700105 public:
Dan Albert1792c232015-05-18 13:06:53 -0700106 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
107 // historically just a struct, but making the whole thing a more idiomatic
108 // class in one go is a very large change. Given how bad our testing is,
109 // it's better to do this piece by piece.
110
Josh Gaob122b172017-08-16 16:57:01 -0700111 atransport(ConnectionState state = kCsOffline)
Josh Gaoe48ecce2017-09-13 13:40:57 -0700112 : id(NextTransportId()), connection_state_(state) {
Dan Albert1792c232015-05-18 13:06:53 -0700113 transport_fde = {};
Tim Murrayde471942017-12-07 11:40:00 -0800114 // Initialize protocol to min version for compatibility with older versions.
115 // Version will be updated post-connect.
116 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -0700117 max_payload = MAX_PAYLOAD;
118 }
Dan Albert1792c232015-05-18 13:06:53 -0700119 virtual ~atransport() {}
120
Yabin Cuib5e11412017-03-10 16:01:01 -0800121 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -0700122 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -0700123
Yabin Cuib5e11412017-03-10 16:01:01 -0800124 // ConnectionState can be read by all threads, but can only be written in the main thread.
125 ConnectionState GetConnectionState() const;
126 void SetConnectionState(ConnectionState state);
127
Josh Gaob122b172017-08-16 16:57:01 -0700128 const TransportId id;
Dan Albert1792c232015-05-18 13:06:53 -0700129 int fd = -1;
130 int transport_socket = -1;
131 fdevent transport_fde;
Josh Gaoe48ecce2017-09-13 13:40:57 -0700132 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700133 uint32_t sync_token = 0;
Dan Albert1792c232015-05-18 13:06:53 -0700134 bool online = false;
135 TransportType type = kTransportAny;
136
Josh Gaob800d882018-01-28 20:32:46 -0800137 std::unique_ptr<Connection> connection;
Dan Albert1792c232015-05-18 13:06:53 -0700138
139 // Used to identify transports for clients.
140 char* serial = nullptr;
141 char* product = nullptr;
142 char* model = nullptr;
143 char* device = nullptr;
144 char* devpath = nullptr;
Yabin Cuib74c6492016-04-29 16:53:52 -0700145
Josh Gaob800d882018-01-28 20:32:46 -0800146 bool IsTcpDevice() const { return type == kTransportLocal; }
Dan Albert1792c232015-05-18 13:06:53 -0700147
Josh Gao3bd28792016-10-05 19:02:29 -0700148#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700149 std::shared_ptr<RSA> NextKey();
Josh Gao3bd28792016-10-05 19:02:29 -0700150#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700151
Josh Gao06d61d42016-10-06 13:31:44 -0700152 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700153 size_t failed_auth_attempts = 0;
154
Yabin Cuib5e11412017-03-10 16:01:01 -0800155 const std::string serial_name() const { return serial ? serial : "<unknown>"; }
David Purselld2acbd12015-12-02 15:14:31 -0800156 const std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700157
158 void update_version(int version, size_t payload);
159 int get_protocol_version() const;
160 size_t get_max_payload() const;
161
David Pursell4e2fd362015-09-22 10:43:08 -0700162 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700163 return features_;
164 }
165
166 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700167
168 // Loads the transport's feature set from the given string.
169 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700170
Yabin Cuib3298242015-08-28 15:09:44 -0700171 void AddDisconnect(adisconnect* disconnect);
172 void RemoveDisconnect(adisconnect* disconnect);
173 void RunDisconnects();
174
David Pursell3f902aa2016-03-01 08:58:26 -0800175 // Returns true if |target| matches this transport. A matching |target| can be any of:
176 // * <serial>
177 // * <devpath>
178 // * product:<product>
179 // * model:<model>
180 // * device:<device>
181 //
182 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
183 // For example, serial "100.100.100.100:5555" would match any of:
184 // * 100.100.100.100
185 // * tcp:100.100.100.100
186 // * udp:100.100.100.100:5555
187 // This is to make it easier to use the same network target for both fastboot and adb.
188 bool MatchesTarget(const std::string& target) const;
189
Dan Albert1792c232015-05-18 13:06:53 -0700190private:
Yabin Cui7f274902016-04-18 11:22:34 -0700191 bool kicked_ = false;
Yabin Cui7f274902016-04-18 11:22:34 -0700192
Dan Albert1792c232015-05-18 13:06:53 -0700193 // A set of features transmitted in the banner with the initial connection.
194 // This is stored in the banner as 'features=feature0,feature1,etc'.
195 FeatureSet features_;
196 int protocol_version;
197 size_t max_payload;
198
Yabin Cuib3298242015-08-28 15:09:44 -0700199 // A list of adisconnect callbacks called when the transport is kicked.
200 std::list<adisconnect*> disconnects_;
201
Yabin Cuib5e11412017-03-10 16:01:01 -0800202 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700203#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700204 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700205#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700206
Dan Albert1792c232015-05-18 13:06:53 -0700207 DISALLOW_COPY_AND_ASSIGN(atransport);
208};
209
Dan Albert76649012015-02-24 15:51:19 -0800210/*
211 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700212 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700213 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700214 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
215 * is set to true and nullptr returned.
216 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800217 */
Josh Gaob122b172017-08-16 16:57:01 -0700218atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
219 bool* is_ambiguous, std::string* error_out,
220 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800221void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800222void update_transports(void);
223
Josh Gaofd713e52017-05-03 22:37:10 -0700224// Iterates across all of the current and pending transports.
225// Stops iteration and returns false if fn returns false, otherwise returns true.
226bool iterate_transports(std::function<bool(const atransport*)> fn);
227
Dan Albert76649012015-02-24 15:51:19 -0800228void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700229void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700230std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800231atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700232void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700233void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800234
235void register_usb_transport(usb_handle* h, const char* serial,
236 const char* devpath, unsigned writeable);
237
Casey Dahlin13a269e2016-06-23 14:19:37 -0700238/* Connect to a network address and register it as a device */
239void connect_device(const std::string& address, std::string* response);
240
Dan Albert76649012015-02-24 15:51:19 -0800241/* cause new transports to be init'd and added to the list */
242int register_socket_transport(int s, const char* serial, int port, int local);
243
Dan Albertdcd78a12015-05-18 16:43:57 -0700244// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800245void unregister_usb_transport(usb_handle* usb);
246
Josh Gao36dadca2017-05-16 15:02:45 -0700247bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800248
Dan Albert76649012015-02-24 15:51:19 -0800249void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700250void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800251
252void send_packet(apacket* p, atransport* t);
253
Josh Gaob0c18022017-08-14 18:57:54 -0700254asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800255
JP Abgrall408fa572011-03-16 15:57:42 -0700256#endif /* __TRANSPORT_H */