blob: 4d97fc78b64f5bfc9f0f198d9ed16df0dc8103c6 [file] [log] [blame]
JP Abgrall2e5dd6e2011-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 Albert020292b2015-02-18 18:03:26 -080020#include <sys/types.h>
21
Elliott Hughes801066a2016-06-29 17:42:01 -070022#include <deque>
Josh Gao4e562502016-10-27 14:01:08 -070023#include <functional>
Yabin Cui2d4c1982015-08-28 15:09:44 -070024#include <list>
Josh Gao22cb70b2016-08-18 22:00:12 -070025#include <memory>
Elliott Hughesab882422015-04-16 22:54:44 -070026#include <string>
Dan Albertbe8e54b2015-05-18 13:06:53 -070027#include <unordered_set>
Dan Albertb302d122015-02-24 15:51:19 -080028
Elliott Hughesab882422015-04-16 22:54:44 -070029#include "adb.h"
Dan Albertbbbbea62014-11-24 23:34:35 -080030
Elliott Hughes801066a2016-06-29 17:42:01 -070031#include <openssl/rsa.h>
32
Dan Albertbe8e54b2015-05-18 13:06:53 -070033typedef std::unordered_set<std::string> FeatureSet;
34
35const FeatureSet& supported_features();
36
David Pursella07dbad2015-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 Pursell22fc5e92015-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 Pursella07dbad2015-09-22 10:43:08 -070044// Do not use any of [:;=,] in feature strings, they have special meaning
45// in the connection banner.
Todd Kennedy1e2f7dc2015-11-03 16:53:08 -080046extern const char* const kFeatureShell2;
47// The 'cmd' command is available
48extern const char* const kFeatureCmd;
Josh Gaoa2cf3752016-12-05 17:11:34 -080049extern const char* const kFeatureStat2;
Josh Gao210b63f2017-02-22 17:07:01 -080050// The server is running with libusb enabled.
51extern const char* const kFeatureLibusb;
David Pursell8da19a42015-08-31 10:42:13 -070052
Dan Albertbe8e54b2015-05-18 13:06:53 -070053class atransport {
54public:
55 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
56 // historically just a struct, but making the whole thing a more idiomatic
57 // class in one go is a very large change. Given how bad our testing is,
58 // it's better to do this piece by piece.
59
60 atransport() {
Dan Albertbe8e54b2015-05-18 13:06:53 -070061 transport_fde = {};
62 protocol_version = A_VERSION;
63 max_payload = MAX_PAYLOAD;
64 }
65
66 virtual ~atransport() {}
67
68 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
69 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
70 void (*close)(atransport* t) = nullptr;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -070071 void SetKickFunction(void (*kick_func)(atransport*)) {
72 kick_func_ = kick_func;
73 }
74 bool IsKicked() {
75 return kicked_;
76 }
77 void Kick();
Dan Albertbe8e54b2015-05-18 13:06:53 -070078
79 int fd = -1;
80 int transport_socket = -1;
81 fdevent transport_fde;
Yabin Cui4d64fd82015-08-27 12:03:11 -070082 size_t ref_count = 0;
Dan Albertbe8e54b2015-05-18 13:06:53 -070083 uint32_t sync_token = 0;
84 ConnectionState connection_state = kCsOffline;
85 bool online = false;
86 TransportType type = kTransportAny;
87
88 // USB handle or socket fd as needed.
89 usb_handle* usb = nullptr;
90 int sfd = -1;
91
92 // Used to identify transports for clients.
93 char* serial = nullptr;
94 char* product = nullptr;
95 char* model = nullptr;
96 char* device = nullptr;
97 char* devpath = nullptr;
Yabin Cuif401ead2016-04-29 16:53:52 -070098 void SetLocalPortForEmulator(int port) {
99 CHECK_EQ(local_port_for_emulator_, -1);
100 local_port_for_emulator_ = port;
101 }
102
103 bool GetLocalPortForEmulator(int* port) const {
104 if (type == kTransportLocal && local_port_for_emulator_ != -1) {
105 *port = local_port_for_emulator_;
106 return true;
107 }
108 return false;
109 }
110
111 bool IsTcpDevice() const {
112 return type == kTransportLocal && local_port_for_emulator_ == -1;
113 }
Dan Albertbe8e54b2015-05-18 13:06:53 -0700114
Josh Gaoeac20582016-10-05 19:02:29 -0700115#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700116 std::shared_ptr<RSA> NextKey();
Josh Gaoeac20582016-10-05 19:02:29 -0700117#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700118
Josh Gao67ac3792016-10-06 13:31:44 -0700119 char token[TOKEN_SIZE] = {};
Dan Albertbe8e54b2015-05-18 13:06:53 -0700120 size_t failed_auth_attempts = 0;
121
David Pursell6e5c7eb2015-12-02 15:14:31 -0800122 const std::string connection_state_name() const;
Dan Albertbe8e54b2015-05-18 13:06:53 -0700123
124 void update_version(int version, size_t payload);
125 int get_protocol_version() const;
126 size_t get_max_payload() const;
127
David Pursella07dbad2015-09-22 10:43:08 -0700128 const FeatureSet& features() const {
Dan Albertbe8e54b2015-05-18 13:06:53 -0700129 return features_;
130 }
131
132 bool has_feature(const std::string& feature) const;
David Pursella07dbad2015-09-22 10:43:08 -0700133
134 // Loads the transport's feature set from the given string.
135 void SetFeatures(const std::string& features_string);
Dan Albertbe8e54b2015-05-18 13:06:53 -0700136
Yabin Cui2d4c1982015-08-28 15:09:44 -0700137 void AddDisconnect(adisconnect* disconnect);
138 void RemoveDisconnect(adisconnect* disconnect);
139 void RunDisconnects();
140
David Pursellc929c6f2016-03-01 08:58:26 -0800141 // Returns true if |target| matches this transport. A matching |target| can be any of:
142 // * <serial>
143 // * <devpath>
144 // * product:<product>
145 // * model:<model>
146 // * device:<device>
147 //
148 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
149 // For example, serial "100.100.100.100:5555" would match any of:
150 // * 100.100.100.100
151 // * tcp:100.100.100.100
152 // * udp:100.100.100.100:5555
153 // This is to make it easier to use the same network target for both fastboot and adb.
154 bool MatchesTarget(const std::string& target) const;
155
Dan Albertbe8e54b2015-05-18 13:06:53 -0700156private:
Yabin Cuif401ead2016-04-29 16:53:52 -0700157 int local_port_for_emulator_ = -1;
Yabin Cuif2a9f9b2016-04-18 11:22:34 -0700158 bool kicked_ = false;
159 void (*kick_func_)(atransport*) = nullptr;
160
Dan Albertbe8e54b2015-05-18 13:06:53 -0700161 // A set of features transmitted in the banner with the initial connection.
162 // This is stored in the banner as 'features=feature0,feature1,etc'.
163 FeatureSet features_;
164 int protocol_version;
165 size_t max_payload;
166
Yabin Cui2d4c1982015-08-28 15:09:44 -0700167 // A list of adisconnect callbacks called when the transport is kicked.
168 std::list<adisconnect*> disconnects_;
169
Josh Gaoeac20582016-10-05 19:02:29 -0700170#if ADB_HOST
Josh Gao22cb70b2016-08-18 22:00:12 -0700171 std::deque<std::shared_ptr<RSA>> keys_;
Josh Gaoeac20582016-10-05 19:02:29 -0700172#endif
Elliott Hughes801066a2016-06-29 17:42:01 -0700173
Dan Albertbe8e54b2015-05-18 13:06:53 -0700174 DISALLOW_COPY_AND_ASSIGN(atransport);
175};
176
Dan Albertb302d122015-02-24 15:51:19 -0800177/*
178 * Obtain a transport from the available transports.
Elliott Hughes67943d12015-10-07 14:55:10 -0700179 * If serial is non-null then only the device with that serial will be chosen.
180 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
181 * is set to true and nullptr returned.
182 * If no suitable transport is found, error is set and nullptr returned.
Dan Albertb302d122015-02-24 15:51:19 -0800183 */
Elliott Hughes67943d12015-10-07 14:55:10 -0700184atransport* acquire_one_transport(TransportType type, const char* serial,
185 bool* is_ambiguous, std::string* error_out);
Dan Albertb302d122015-02-24 15:51:19 -0800186void kick_transport(atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800187void update_transports(void);
188
Dan Albertb302d122015-02-24 15:51:19 -0800189void init_transport_registration(void);
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700190void init_mdns_transport_discovery(void);
Elliott Hughes88b4c852015-04-30 17:32:03 -0700191std::string list_transports(bool long_listing);
Dan Albertb302d122015-02-24 15:51:19 -0800192atransport* find_transport(const char* serial);
Yabin Cui4d64fd82015-08-27 12:03:11 -0700193void kick_all_tcp_devices();
Dan Albertb302d122015-02-24 15:51:19 -0800194
195void register_usb_transport(usb_handle* h, const char* serial,
196 const char* devpath, unsigned writeable);
197
Casey Dahlin3122cdf2016-06-23 14:19:37 -0700198/* Connect to a network address and register it as a device */
199void connect_device(const std::string& address, std::string* response);
200
Dan Albertb302d122015-02-24 15:51:19 -0800201/* cause new transports to be init'd and added to the list */
202int register_socket_transport(int s, const char* serial, int port, int local);
203
Dan Albert9a50f4c2015-05-18 16:43:57 -0700204// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albertb302d122015-02-24 15:51:19 -0800205void unregister_usb_transport(usb_handle* usb);
206
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100207int check_header(apacket* p, atransport* t);
Dan Albertb302d122015-02-24 15:51:19 -0800208int check_data(apacket* p);
209
Dan Albertb302d122015-02-24 15:51:19 -0800210void close_usb_devices();
Josh Gao4e562502016-10-27 14:01:08 -0700211void close_usb_devices(std::function<bool(const atransport*)> predicate);
Dan Albertb302d122015-02-24 15:51:19 -0800212
213void send_packet(apacket* p, atransport* t);
214
215asocket* create_device_tracker(void);
216
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700217#endif /* __TRANSPORT_H */