blob: 86cd9928c8cd60a5288c7ca650b391ddc5ea9e54 [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 Hughes7be29c82015-04-16 22:54:44 -070031#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080032
Elliott Hughes0aeb5052016-06-29 17:42:01 -070033#include <openssl/rsa.h>
34
Dan Albert1792c232015-05-18 13:06:53 -070035typedef std::unordered_set<std::string> FeatureSet;
36
37const FeatureSet& supported_features();
38
David Pursell4e2fd362015-09-22 10:43:08 -070039// Encodes and decodes FeatureSet objects into human-readable strings.
40std::string FeatureSetToString(const FeatureSet& features);
41FeatureSet StringToFeatureSet(const std::string& features_string);
42
David Pursell70ef7b42015-09-30 13:35:42 -070043// Returns true if both local features and |feature_set| support |feature|.
44bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
45
David Pursell4e2fd362015-09-22 10:43:08 -070046// Do not use any of [:;=,] in feature strings, they have special meaning
47// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080048extern const char* const kFeatureShell2;
49// The 'cmd' command is available
50extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080051extern const char* const kFeatureStat2;
Josh Gao5d1756c2017-02-22 17:07:01 -080052// The server is running with libusb enabled.
53extern const char* const kFeatureLibusb;
Dan Albert5176df82017-05-23 14:30:00 -070054// The server supports `push --sync`.
55extern const char* const kFeaturePushSync;
David Pursell0955c662015-08-31 10:42:13 -070056
Josh Gaob122b172017-08-16 16:57:01 -070057TransportId NextTransportId();
58
Dan Albert1792c232015-05-18 13:06:53 -070059class atransport {
Josh Gaob122b172017-08-16 16:57:01 -070060 public:
Dan Albert1792c232015-05-18 13:06:53 -070061 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
62 // historically just a struct, but making the whole thing a more idiomatic
63 // class in one go is a very large change. Given how bad our testing is,
64 // it's better to do this piece by piece.
65
Josh Gaob122b172017-08-16 16:57:01 -070066 atransport(ConnectionState state = kCsOffline)
Josh Gaoe48ecce2017-09-13 13:40:57 -070067 : id(NextTransportId()), connection_state_(state) {
Dan Albert1792c232015-05-18 13:06:53 -070068 transport_fde = {};
Tim Murrayde471942017-12-07 11:40:00 -080069 // Initialize protocol to min version for compatibility with older versions.
70 // Version will be updated post-connect.
71 protocol_version = A_VERSION_MIN;
Dan Albert1792c232015-05-18 13:06:53 -070072 max_payload = MAX_PAYLOAD;
73 }
Dan Albert1792c232015-05-18 13:06:53 -070074 virtual ~atransport() {}
75
76 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
Dan Albert1792c232015-05-18 13:06:53 -070077 void (*close)(atransport* t) = nullptr;
Yabin Cuib5e11412017-03-10 16:01:01 -080078
79 void SetWriteFunction(int (*write_func)(apacket*, atransport*)) { write_func_ = write_func; }
Josh Gaob122b172017-08-16 16:57:01 -070080 void SetKickFunction(void (*kick_func)(atransport*)) { kick_func_ = kick_func; }
81 bool IsKicked() { return kicked_; }
Yabin Cuib5e11412017-03-10 16:01:01 -080082 int Write(apacket* p);
Yabin Cui7f274902016-04-18 11:22:34 -070083 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -070084
Yabin Cuib5e11412017-03-10 16:01:01 -080085 // ConnectionState can be read by all threads, but can only be written in the main thread.
86 ConnectionState GetConnectionState() const;
87 void SetConnectionState(ConnectionState state);
88
Josh Gaob122b172017-08-16 16:57:01 -070089 const TransportId id;
Dan Albert1792c232015-05-18 13:06:53 -070090 int fd = -1;
91 int transport_socket = -1;
92 fdevent transport_fde;
Josh Gaoe48ecce2017-09-13 13:40:57 -070093 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -070094 uint32_t sync_token = 0;
Dan Albert1792c232015-05-18 13:06:53 -070095 bool online = false;
96 TransportType type = kTransportAny;
97
98 // USB handle or socket fd as needed.
99 usb_handle* usb = nullptr;
100 int sfd = -1;
101
102 // Used to identify transports for clients.
103 char* serial = nullptr;
104 char* product = nullptr;
105 char* model = nullptr;
106 char* device = nullptr;
107 char* devpath = nullptr;
Yabin Cuib74c6492016-04-29 16:53:52 -0700108 void SetLocalPortForEmulator(int port) {
109 CHECK_EQ(local_port_for_emulator_, -1);
110 local_port_for_emulator_ = port;
111 }
112
113 bool GetLocalPortForEmulator(int* port) const {
114 if (type == kTransportLocal && local_port_for_emulator_ != -1) {
115 *port = local_port_for_emulator_;
116 return true;
117 }
118 return false;
119 }
120
121 bool IsTcpDevice() const {
122 return type == kTransportLocal && local_port_for_emulator_ == -1;
123 }
Dan Albert1792c232015-05-18 13:06:53 -0700124
Josh Gao3bd28792016-10-05 19:02:29 -0700125#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700126 std::shared_ptr<RSA> NextKey();
Josh Gao3bd28792016-10-05 19:02:29 -0700127#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700128
Josh Gao06d61d42016-10-06 13:31:44 -0700129 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700130 size_t failed_auth_attempts = 0;
131
Yabin Cuib5e11412017-03-10 16:01:01 -0800132 const std::string serial_name() const { return serial ? serial : "<unknown>"; }
David Purselld2acbd12015-12-02 15:14:31 -0800133 const std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700134
135 void update_version(int version, size_t payload);
136 int get_protocol_version() const;
137 size_t get_max_payload() const;
138
David Pursell4e2fd362015-09-22 10:43:08 -0700139 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700140 return features_;
141 }
142
143 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700144
145 // Loads the transport's feature set from the given string.
146 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700147
Yabin Cuib3298242015-08-28 15:09:44 -0700148 void AddDisconnect(adisconnect* disconnect);
149 void RemoveDisconnect(adisconnect* disconnect);
150 void RunDisconnects();
151
David Pursell3f902aa2016-03-01 08:58:26 -0800152 // Returns true if |target| matches this transport. A matching |target| can be any of:
153 // * <serial>
154 // * <devpath>
155 // * product:<product>
156 // * model:<model>
157 // * device:<device>
158 //
159 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
160 // For example, serial "100.100.100.100:5555" would match any of:
161 // * 100.100.100.100
162 // * tcp:100.100.100.100
163 // * udp:100.100.100.100:5555
164 // This is to make it easier to use the same network target for both fastboot and adb.
165 bool MatchesTarget(const std::string& target) const;
166
Dan Albert1792c232015-05-18 13:06:53 -0700167private:
Yabin Cuib74c6492016-04-29 16:53:52 -0700168 int local_port_for_emulator_ = -1;
Yabin Cui7f274902016-04-18 11:22:34 -0700169 bool kicked_ = false;
170 void (*kick_func_)(atransport*) = nullptr;
Yabin Cuib5e11412017-03-10 16:01:01 -0800171 int (*write_func_)(apacket*, atransport*) = nullptr;
Yabin Cui7f274902016-04-18 11:22:34 -0700172
Dan Albert1792c232015-05-18 13:06:53 -0700173 // A set of features transmitted in the banner with the initial connection.
174 // This is stored in the banner as 'features=feature0,feature1,etc'.
175 FeatureSet features_;
176 int protocol_version;
177 size_t max_payload;
178
Yabin Cuib3298242015-08-28 15:09:44 -0700179 // A list of adisconnect callbacks called when the transport is kicked.
180 std::list<adisconnect*> disconnects_;
181
Yabin Cuib5e11412017-03-10 16:01:01 -0800182 std::atomic<ConnectionState> connection_state_;
Josh Gao3bd28792016-10-05 19:02:29 -0700183#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700184 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700185#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700186
Dan Albert1792c232015-05-18 13:06:53 -0700187 DISALLOW_COPY_AND_ASSIGN(atransport);
188};
189
Dan Albert76649012015-02-24 15:51:19 -0800190/*
191 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700192 * If serial is non-null then only the device with that serial will be chosen.
Josh Gaob122b172017-08-16 16:57:01 -0700193 * If transport_id is non-zero then only the device with that transport ID will be chosen.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700194 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
195 * is set to true and nullptr returned.
196 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800197 */
Josh Gaob122b172017-08-16 16:57:01 -0700198atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id,
199 bool* is_ambiguous, std::string* error_out,
200 bool accept_any_state = false);
Dan Albert76649012015-02-24 15:51:19 -0800201void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800202void update_transports(void);
203
Josh Gaofd713e52017-05-03 22:37:10 -0700204// Iterates across all of the current and pending transports.
205// Stops iteration and returns false if fn returns false, otherwise returns true.
206bool iterate_transports(std::function<bool(const atransport*)> fn);
207
Dan Albert76649012015-02-24 15:51:19 -0800208void init_transport_registration(void);
Casey Dahlin13a269e2016-06-23 14:19:37 -0700209void init_mdns_transport_discovery(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700210std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800211atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700212void kick_all_tcp_devices();
Josh Gao01b7bc42017-05-09 13:43:35 -0700213void kick_all_transports();
Dan Albert76649012015-02-24 15:51:19 -0800214
215void register_usb_transport(usb_handle* h, const char* serial,
216 const char* devpath, unsigned writeable);
217
Casey Dahlin13a269e2016-06-23 14:19:37 -0700218/* Connect to a network address and register it as a device */
219void connect_device(const std::string& address, std::string* response);
220
Dan Albert76649012015-02-24 15:51:19 -0800221/* cause new transports to be init'd and added to the list */
222int register_socket_transport(int s, const char* serial, int port, int local);
223
Dan Albertdcd78a12015-05-18 16:43:57 -0700224// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800225void unregister_usb_transport(usb_handle* usb);
226
Josh Gao36dadca2017-05-16 15:02:45 -0700227bool check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800228
Dan Albert76649012015-02-24 15:51:19 -0800229void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700230void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800231
232void send_packet(apacket* p, atransport* t);
233
Josh Gaob0c18022017-08-14 18:57:54 -0700234asocket* create_device_tracker(bool long_output);
Dan Albert76649012015-02-24 15:51:19 -0800235
JP Abgrall408fa572011-03-16 15:57:42 -0700236#endif /* __TRANSPORT_H */