blob: 35d7b505d19f8283dd5a8372a9a61ee787b04f17 [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 Cuib3298242015-08-28 15:09:44 -070022#include <list>
Elliott Hughes7be29c82015-04-16 22:54:44 -070023#include <string>
Dan Albert1792c232015-05-18 13:06:53 -070024#include <unordered_set>
Dan Albert76649012015-02-24 15:51:19 -080025
Elliott Hughes7be29c82015-04-16 22:54:44 -070026#include "adb.h"
Dan Albert630b9af2014-11-24 23:34:35 -080027
Dan Albert1792c232015-05-18 13:06:53 -070028typedef std::unordered_set<std::string> FeatureSet;
29
30const FeatureSet& supported_features();
31
David Pursell4e2fd362015-09-22 10:43:08 -070032// Encodes and decodes FeatureSet objects into human-readable strings.
33std::string FeatureSetToString(const FeatureSet& features);
34FeatureSet StringToFeatureSet(const std::string& features_string);
35
David Pursell70ef7b42015-09-30 13:35:42 -070036// Returns true if both local features and |feature_set| support |feature|.
37bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature);
38
David Pursell4e2fd362015-09-22 10:43:08 -070039// Do not use any of [:;=,] in feature strings, they have special meaning
40// in the connection banner.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080041extern const char* const kFeatureShell2;
42// The 'cmd' command is available
43extern const char* const kFeatureCmd;
David Pursell0955c662015-08-31 10:42:13 -070044
Dan Albert1792c232015-05-18 13:06:53 -070045class atransport {
46public:
47 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
48 // historically just a struct, but making the whole thing a more idiomatic
49 // class in one go is a very large change. Given how bad our testing is,
50 // it's better to do this piece by piece.
51
52 atransport() {
Dan Albert1792c232015-05-18 13:06:53 -070053 transport_fde = {};
54 protocol_version = A_VERSION;
55 max_payload = MAX_PAYLOAD;
56 }
57
58 virtual ~atransport() {}
59
60 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
61 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
62 void (*close)(atransport* t) = nullptr;
Yabin Cuia28918c2016-04-18 11:22:34 -070063 void SetKickFunction(void (*kick_func)(atransport*)) {
64 kick_func_ = kick_func;
65 }
66 bool IsKicked() {
67 return kicked_;
68 }
69 void Kick();
Dan Albert1792c232015-05-18 13:06:53 -070070
71 int fd = -1;
72 int transport_socket = -1;
73 fdevent transport_fde;
Yabin Cuif4b99282015-08-27 12:03:11 -070074 size_t ref_count = 0;
Dan Albert1792c232015-05-18 13:06:53 -070075 uint32_t sync_token = 0;
76 ConnectionState connection_state = kCsOffline;
77 bool online = false;
78 TransportType type = kTransportAny;
79
80 // USB handle or socket fd as needed.
81 usb_handle* usb = nullptr;
82 int sfd = -1;
83
84 // Used to identify transports for clients.
85 char* serial = nullptr;
86 char* product = nullptr;
87 char* model = nullptr;
88 char* device = nullptr;
89 char* devpath = nullptr;
90 int adb_port = -1; // Use for emulators (local transport)
Dan Albert1792c232015-05-18 13:06:53 -070091
Dan Albert1792c232015-05-18 13:06:53 -070092 void* key = nullptr;
93 unsigned char token[TOKEN_SIZE] = {};
Dan Albert1792c232015-05-18 13:06:53 -070094 size_t failed_auth_attempts = 0;
95
David Purselld2acbd12015-12-02 15:14:31 -080096 const std::string connection_state_name() const;
Dan Albert1792c232015-05-18 13:06:53 -070097
98 void update_version(int version, size_t payload);
99 int get_protocol_version() const;
100 size_t get_max_payload() const;
101
David Pursell4e2fd362015-09-22 10:43:08 -0700102 const FeatureSet& features() const {
Dan Albert1792c232015-05-18 13:06:53 -0700103 return features_;
104 }
105
106 bool has_feature(const std::string& feature) const;
David Pursell4e2fd362015-09-22 10:43:08 -0700107
108 // Loads the transport's feature set from the given string.
109 void SetFeatures(const std::string& features_string);
Dan Albert1792c232015-05-18 13:06:53 -0700110
Yabin Cuib3298242015-08-28 15:09:44 -0700111 void AddDisconnect(adisconnect* disconnect);
112 void RemoveDisconnect(adisconnect* disconnect);
113 void RunDisconnects();
114
David Pursell802c54e2016-03-01 08:58:26 -0800115 // Returns true if |target| matches this transport. A matching |target| can be any of:
116 // * <serial>
117 // * <devpath>
118 // * product:<product>
119 // * model:<model>
120 // * device:<device>
121 //
122 // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets.
123 // For example, serial "100.100.100.100:5555" would match any of:
124 // * 100.100.100.100
125 // * tcp:100.100.100.100
126 // * udp:100.100.100.100:5555
127 // This is to make it easier to use the same network target for both fastboot and adb.
128 bool MatchesTarget(const std::string& target) const;
129
Dan Albert1792c232015-05-18 13:06:53 -0700130private:
Yabin Cuia28918c2016-04-18 11:22:34 -0700131 bool kicked_ = false;
132 void (*kick_func_)(atransport*) = nullptr;
133
Dan Albert1792c232015-05-18 13:06:53 -0700134 // A set of features transmitted in the banner with the initial connection.
135 // This is stored in the banner as 'features=feature0,feature1,etc'.
136 FeatureSet features_;
137 int protocol_version;
138 size_t max_payload;
139
Yabin Cuib3298242015-08-28 15:09:44 -0700140 // A list of adisconnect callbacks called when the transport is kicked.
141 std::list<adisconnect*> disconnects_;
142
Dan Albert1792c232015-05-18 13:06:53 -0700143 DISALLOW_COPY_AND_ASSIGN(atransport);
144};
145
Dan Albert76649012015-02-24 15:51:19 -0800146/*
147 * Obtain a transport from the available transports.
Elliott Hughes8d28e192015-10-07 14:55:10 -0700148 * If serial is non-null then only the device with that serial will be chosen.
149 * If multiple devices/emulators would match, *is_ambiguous (if non-null)
150 * is set to true and nullptr returned.
151 * If no suitable transport is found, error is set and nullptr returned.
Dan Albert76649012015-02-24 15:51:19 -0800152 */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700153atransport* acquire_one_transport(TransportType type, const char* serial,
154 bool* is_ambiguous, std::string* error_out);
Dan Albert76649012015-02-24 15:51:19 -0800155void kick_transport(atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800156void update_transports(void);
157
Dan Albert76649012015-02-24 15:51:19 -0800158void init_transport_registration(void);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700159std::string list_transports(bool long_listing);
Dan Albert76649012015-02-24 15:51:19 -0800160atransport* find_transport(const char* serial);
Yabin Cuif4b99282015-08-27 12:03:11 -0700161void kick_all_tcp_devices();
Dan Albert76649012015-02-24 15:51:19 -0800162
163void register_usb_transport(usb_handle* h, const char* serial,
164 const char* devpath, unsigned writeable);
165
166/* cause new transports to be init'd and added to the list */
167int register_socket_transport(int s, const char* serial, int port, int local);
168
Dan Albertdcd78a12015-05-18 16:43:57 -0700169// This should only be used for transports with connection_state == kCsNoPerm.
Dan Albert76649012015-02-24 15:51:19 -0800170void unregister_usb_transport(usb_handle* usb);
171
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100172int check_header(apacket* p, atransport* t);
Dan Albert76649012015-02-24 15:51:19 -0800173int check_data(apacket* p);
174
175/* for MacOS X cleanup */
176void close_usb_devices();
177
178void send_packet(apacket* p, atransport* t);
179
180asocket* create_device_tracker(void);
181
JP Abgrall408fa572011-03-16 15:57:42 -0700182#endif /* __TRANSPORT_H */