blob: 959681ff628470dfbb7417fbcd06b2e12d2425a6 [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>
Yabin Cuib3298242015-08-28 15:09:44 -070023#include <list>
Josh Gao2e671202016-08-18 22:00:12 -070024#include <memory>
Elliott Hughes7be29c82015-04-16 22:54:44 -070025#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070026#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080027
Elliott Hughes7be29c82015-04-16 22:54:44 -070028#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080029
Elliott Hughes0aeb5052016-06-29 17:42:01 -070030#include <openssl/rsa.h>
31
Dan Albert1792c232015-05-18 13:06:53 -070032typedef std::unordered_set<std::string> FeatureSet;
33
34const FeatureSet& supported_features();
35
David Pursell4e2fd362015-09-22 10:43:08 -070036// Encodes and decodes FeatureSet objects into human-readable strings.
37std::string FeatureSetToString(const FeatureSet& features);
38FeatureSet StringToFeatureSet(const std::string& features_string);
39
David Pursell70ef7b42015-09-30 13:35:42 -070040// Returns true if both local features and |feature_set| support |feature|.
41bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
42
David Pursell4e2fd362015-09-22 10:43:08 -070043// Do not use any of [:;=,] in feature strings, they have special meaning
44// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080045extern const char* const kFeatureShell2;
46// The 'cmd' command is available
47extern const char* const kFeatureCmd;
David Pursell0955c662015-08-31 10:42:13 -070048
Dan Albert1792c232015-05-18 13:06:53 -070049class atransport {
50public:
51 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
52 // historically just a struct, but making the whole thing a more idiomatic
53 // class in one go is a very large change. Given how bad our testing is,
54 // it's better to do this piece by piece.
55
56 atransport() {
Dan Albert1792c232015-05-18 13:06:53 -070057 transport_fde = {};
58 protocol_version = A_VERSION;
59 max_payload = MAX_PAYLOAD;
60 }
61
62 virtual ~atransport() {}
63
64 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
65 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
66 void (*close)(atransport* t) = nullptr;
Yabin Cui7f274902016-04-18 11:22:34 -070067 void SetKickFunction(void (*kick_func)(atransport*)) {
68 kick_func_ = kick_func;
69 }
70 bool IsKicked() {
71 return kicked_;
72 }
73 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -070074
75 int fd = -1;
76 int transport_socket = -1;
77 fdevent transport_fde;
Yabin Cuif4b99282015-08-27 12:03:11 -070078 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -070079 uint32_t sync_token = 0;
80 ConnectionState connection_state = kCsOffline;
81 bool online = false;
82 TransportType type = kTransportAny;
83
84 // USB handle or socket fd as needed.
85 usb_handle* usb = nullptr;
86 int sfd = -1;
87
88 // Used to identify transports for clients.
89 char* serial = nullptr;
90 char* product = nullptr;
91 char* model = nullptr;
92 char* device = nullptr;
93 char* devpath = nullptr;
Yabin Cuib74c6492016-04-29 16:53:52 -070094 void SetLocalPortForEmulator(int port) {
95 CHECK_EQ(local_port_for_emulator_, -1);
96 local_port_for_emulator_ = port;
97 }
98
99 bool GetLocalPortForEmulator(int* port) const {
100 if (type == kTransportLocal && local_port_for_emulator_ != -1) {
101 *port = local_port_for_emulator_;
102 return true;
103 }
104 return false;
105 }
106
107 bool IsTcpDevice() const {
108 return type == kTransportLocal && local_port_for_emulator_ == -1;
109 }
Dan Albert1792c232015-05-18 13:06:53 -0700110
Josh Gao2e671202016-08-18 22:00:12 -0700111 std::shared_ptr<RSA> NextKey();
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700112
Dan Albert1792c232015-05-18 13:06:53 -0700113 unsigned char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -0700114 size_t failed_auth_attempts = 0;
115
David Purselld2acbd12015-12-02 15:14:31 -0800116 const std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -0700117
118 void update_version(int version, size_t payload);
119 int get_protocol_version() const;
120 size_t get_max_payload() const;
121
David Pursell4e2fd362015-09-22 10:43:08 -0700122 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700123 return features_;
124 }
125
126 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700127
128 // Loads the transport's feature set from the given string.
129 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700130
Yabin Cuib3298242015-08-28 15:09:44 -0700131 void AddDisconnect(adisconnect* disconnect);
132 void RemoveDisconnect(adisconnect* disconnect);
133 void RunDisconnects();
134
David Pursell3f902aa2016-03-01 08:58:26 -0800135 // Returns true if |target| matches this transport. A matching |target| can be any of:
136 // * <serial>
137 // * <devpath>
138 // * product:<product>
139 // * model:<model>
140 // * device:<device>
141 //
142 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
143 // For example, serial "100.100.100.100:5555" would match any of:
144 // * 100.100.100.100
145 // * tcp:100.100.100.100
146 // * udp:100.100.100.100:5555
147 // This is to make it easier to use the same network target for both fastboot and adb.
148 bool MatchesTarget(const std::string& target) const;
149
Dan Albert1792c232015-05-18 13:06:53 -0700150private:
Yabin Cuib74c6492016-04-29 16:53:52 -0700151 int local_port_for_emulator_ = -1;
Yabin Cui7f274902016-04-18 11:22:34 -0700152 bool kicked_ = false;
153 void (*kick_func_)(atransport*) = nullptr;
154
Dan Albert1792c232015-05-18 13:06:53 -0700155 // A set of features transmitted in the banner with the initial connection.
156 // This is stored in the banner as 'features=feature0,feature1,etc'.
157 FeatureSet features_;
158 int protocol_version;
159 size_t max_payload;
160
Yabin Cuib3298242015-08-28 15:09:44 -0700161 // A list of adisconnect callbacks called when the transport is kicked.
162 std::list<adisconnect*> disconnects_;
163
Josh Gao2e671202016-08-18 22:00:12 -0700164 std::deque<std::shared_ptr<RSA>> keys_;
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700165
Dan Albert1792c232015-05-18 13:06:53 -0700166 DISALLOW_COPY_AND_ASSIGN(atransport);
167};
168
Dan Albert76649012015-02-24 15:51:19 -0800169/*
170 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700171 * If serial is non-null then only the device with that serial will be chosen.
172 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
173 * is set to true and nullptr returned.
174 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800175 */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700176atransport* acquire_one_transport(TransportType type, const char* serial,
177 bool* is_ambiguous, std::string* error_out);
Dan Albert76649012015-02-24 15:51:19 -0800178void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800179void update_transports(void);
180
Dan Albert76649012015-02-24 15:51:19 -0800181void init_transport_registration(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700182std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800183atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700184void kick_all_tcp_devices();
Dan Albert76649012015-02-24 15:51:19 -0800185
186void register_usb_transport(usb_handle* h, const char* serial,
187 const char* devpath, unsigned writeable);
188
189/* cause new transports to be init'd and added to the list */
190int register_socket_transport(int s, const char* serial, int port, int local);
191
Dan Albertdcd78a12015-05-18 16:43:57 -0700192// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800193void unregister_usb_transport(usb_handle* usb);
194
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100195int check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800196int check_data(apacket* p);
197
198/* for MacOS X cleanup */
199void close_usb_devices();
200
201void send_packet(apacket* p, atransport* t);
202
203asocket* create_device_tracker(void);
204
JP Abgrall408fa572011-03-16 15:57:42 -0700205#endif /* __TRANSPORT_H */