blob: 3306388292848d1c18290aaf7c394233c770b9a0 [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
Elliott Hughes0aeb5052016-06-29 17:42:01 -070022#include <deque>
Josh Gao22d2b3e2016-10-27 14:01:08 -070023#include <functional>
Yabin Cuib3298242015-08-28 15:09:44 -070024#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070025#include <memory>
Elliott Hughes7be29c82015-04-16 22:54:44 -070026#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070027#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080028
Elliott Hughes7be29c82015-04-16 22:54:44 -070029#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080030
Elliott Hughes0aeb5052016-06-29 17:42:01 -070031#include <openssl/rsa.h>
32
Dan Albert1792c232015-05-18 13:06:53 -070033typedef std::unordered_set<std::string> FeatureSet;
34
35const FeatureSet& supported_features();
36
David Pursell4e2fd362015-09-22 10:43:08 -070037// Encodes and decodes FeatureSet objects into human-readable strings.
38std::string FeatureSetToString(const FeatureSet& features);
39FeatureSet StringToFeatureSet(const std::string& features_string);
40
David Pursell70ef7b42015-09-30 13:35:42 -070041// Returns true if both local features and |feature_set| support |feature|.
42bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
43
David Pursell4e2fd362015-09-22 10:43:08 -070044// Do not use any of [:;=,] in feature strings, they have special meaning
45// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080046extern const char* const kFeatureShell2;
47// The 'cmd' command is available
48extern const char* const kFeatureCmd;
Josh Gao5a1e3fd2016-12-05 17:11:34 -080049extern const char* const kFeatureStat2;
David Pursell0955c662015-08-31 10:42:13 -070050
Dan Albert1792c232015-05-18 13:06:53 -070051class atransport {
52public:
53 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
54 // historically just a struct, but making the whole thing a more idiomatic
55 // class in one go is a very large change. Given how bad our testing is,
56 // it's better to do this piece by piece.
57
58 atransport() {
Dan Albert1792c232015-05-18 13:06:53 -070059 transport_fde = {};
60 protocol_version = A_VERSION;
61 max_payload = MAX_PAYLOAD;
62 }
63
64 virtual ~atransport() {}
65
66 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
67 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
68 void (*close)(atransport* t) = nullptr;
Yabin Cui7f274902016-04-18 11:22:34 -070069 void SetKickFunction(void (*kick_func)(atransport*)) {
70 kick_func_ = kick_func;
71 }
72 bool IsKicked() {
73 return kicked_;
74 }
75 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -070076
77 int fd = -1;
78 int transport_socket = -1;
79 fdevent transport_fde;
Yabin Cuif4b99282015-08-27 12:03:11 -070080 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -070081 uint32_t sync_token = 0;
82 ConnectionState connection_state = kCsOffline;
83 bool online = false;
84 TransportType type = kTransportAny;
85
86 // USB handle or socket fd as needed.
87 usb_handle* usb = nullptr;
88 int sfd = -1;
89
90 // Used to identify transports for clients.
91 char* serial = nullptr;
92 char* product = nullptr;
93 char* model = nullptr;
94 char* device = nullptr;
95 char* devpath = nullptr;
Yabin Cuib74c6492016-04-29 16:53:52 -070096 void SetLocalPortForEmulator(int port) {
97 CHECK_EQ(local_port_for_emulator_, -1);
98 local_port_for_emulator_ = port;
99 }
100
101 bool GetLocalPortForEmulator(int* port) const {
102 if (type == kTransportLocal && local_port_for_emulator_ != -1) {
103 *port = local_port_for_emulator_;
104 return true;
105 }
106 return false;
107 }
108
109 bool IsTcpDevice() const {
110 return type == kTransportLocal && local_port_for_emulator_ == -1;
111 }
Dan Albert1792c232015-05-18 13:06:53 -0700112
Josh Gao3bd28792016-10-05 19:02:29 -0700113#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700114 std::shared_ptr<RSA> NextKey();
Josh Gao3bd28792016-10-05 19:02:29 -0700115#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700116
Josh Gao06d61d42016-10-06 13:31:44 -0700117 char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700118 size_t failed_auth_attempts = 0;
119
David Purselld2acbd12015-12-02 15:14:31 -0800120 const std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700121
122 void update_version(int version, size_t payload);
123 int get_protocol_version() const;
124 size_t get_max_payload() const;
125
David Pursell4e2fd362015-09-22 10:43:08 -0700126 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700127 return features_;
128 }
129
130 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700131
132 // Loads the transport's feature set from the given string.
133 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700134
Yabin Cuib3298242015-08-28 15:09:44 -0700135 void AddDisconnect(adisconnect* disconnect);
136 void RemoveDisconnect(adisconnect* disconnect);
137 void RunDisconnects();
138
David Pursell3f902aa2016-03-01 08:58:26 -0800139 // Returns true if |target| matches this transport. A matching |target| can be any of:
140 // * <serial>
141 // * <devpath>
142 // * product:<product>
143 // * model:<model>
144 // * device:<device>
145 //
146 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
147 // For example, serial "100.100.100.100:5555" would match any of:
148 // * 100.100.100.100
149 // * tcp:100.100.100.100
150 // * udp:100.100.100.100:5555
151 // This is to make it easier to use the same network target for both fastboot and adb.
152 bool MatchesTarget(const std::string& target) const;
153
Dan Albert1792c232015-05-18 13:06:53 -0700154private:
Yabin Cuib74c6492016-04-29 16:53:52 -0700155 int local_port_for_emulator_ = -1;
Yabin Cui7f274902016-04-18 11:22:34 -0700156 bool kicked_ = false;
157 void (*kick_func_)(atransport*) = nullptr;
158
Dan Albert1792c232015-05-18 13:06:53 -0700159 // A set of features transmitted in the banner with the initial connection.
160 // This is stored in the banner as 'features=feature0,feature1,etc'.
161 FeatureSet features_;
162 int protocol_version;
163 size_t max_payload;
164
Yabin Cuib3298242015-08-28 15:09:44 -0700165 // A list of adisconnect callbacks called when the transport is kicked.
166 std::list<adisconnect*> disconnects_;
167
Josh Gao3bd28792016-10-05 19:02:29 -0700168#if ADB_HOST
Josh Gao2e671202016-08-18 22:00:12 -0700169 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gao3bd28792016-10-05 19:02:29 -0700170#endif
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700171
Dan Albert1792c232015-05-18 13:06:53 -0700172 DISALLOW_COPY_AND_ASSIGN(atransport);
173};
174
Dan Albert76649012015-02-24 15:51:19 -0800175/*
176 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700177 * If serial is non-null then only the device with that serial will be chosen.
178 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
179 * is set to true and nullptr returned.
180 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800181 */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700182atransport* acquire_one_transport(TransportType type, const char* serial,
183 bool* is_ambiguous, std::string* error_out);
Dan Albert76649012015-02-24 15:51:19 -0800184void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800185void update_transports(void);
186
Dan Albert76649012015-02-24 15:51:19 -0800187void init_transport_registration(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700188std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800189atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700190void kick_all_tcp_devices();
Dan Albert76649012015-02-24 15:51:19 -0800191
192void register_usb_transport(usb_handle* h, const char* serial,
193 const char* devpath, unsigned writeable);
194
195/* cause new transports to be init'd and added to the list */
196int register_socket_transport(int s, const char* serial, int port, int local);
197
Dan Albertdcd78a12015-05-18 16:43:57 -0700198// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800199void unregister_usb_transport(usb_handle* usb);
200
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100201int check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800202int check_data(apacket* p);
203
Dan Albert76649012015-02-24 15:51:19 -0800204void close_usb_devices();
Josh Gao22d2b3e2016-10-27 14:01:08 -0700205void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albert76649012015-02-24 15:51:19 -0800206
207void send_packet(apacket* p, atransport* t);
208
209asocket* create_device_tracker(void);
210
JP Abgrall408fa572011-03-16 15:57:42 -0700211#endif /* __TRANSPORT_H */