1 /* 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 20 #include <sys/types.h> 21 22 #include <list> 23 #include <string> 24 #include <unordered_set> 25 26 #include "adb.h" 27 28 typedef std::unordered_set<std::string> FeatureSet; 29 30 const FeatureSet& supported_features(); 31 32 // Encodes and decodes FeatureSet objects into human-readable strings. 33 std::string FeatureSetToString(const FeatureSet& features); 34 FeatureSet StringToFeatureSet(const std::string& features_string); 35 36 // Returns true if both local features and |feature_set| support |feature|. 37 bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); 38 39 // Do not use any of [:;=,] in feature strings, they have special meaning 40 // in the connection banner. 41 extern const char* const kFeatureShell2; 42 // The 'cmd' command is available 43 extern const char* const kFeatureCmd; 44 45 class atransport { 46 public: 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() { 53 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; 63 void SetKickFunction(void (*kick_func)(atransport*)) { 64 kick_func_ = kick_func; 65 } 66 bool IsKicked() { 67 return kicked_; 68 } 69 void Kick(); 70 71 int fd = -1; 72 int transport_socket = -1; 73 fdevent transport_fde; 74 size_t ref_count = 0; 75 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) 91 92 void* key = nullptr; 93 unsigned char token[TOKEN_SIZE] = {}; 94 size_t failed_auth_attempts = 0; 95 96 const std::string connection_state_name() const; 97 98 void update_version(int version, size_t payload); 99 int get_protocol_version() const; 100 size_t get_max_payload() const; 101 102 const FeatureSet& features() const { 103 return features_; 104 } 105 106 bool has_feature(const std::string& feature) const; 107 108 // Loads the transport's feature set from the given string. 109 void SetFeatures(const std::string& features_string); 110 111 void AddDisconnect(adisconnect* disconnect); 112 void RemoveDisconnect(adisconnect* disconnect); 113 void RunDisconnects(); 114 115 // 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 130 private: 131 bool kicked_ = false; 132 void (*kick_func_)(atransport*) = nullptr; 133 134 // 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 140 // A list of adisconnect callbacks called when the transport is kicked. 141 std::list<adisconnect*> disconnects_; 142 143 DISALLOW_COPY_AND_ASSIGN(atransport); 144 }; 145 146 /* 147 * Obtain a transport from the available transports. 148 * 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. 152 */ 153 atransport* acquire_one_transport(TransportType type, const char* serial, 154 bool* is_ambiguous, std::string* error_out); 155 void kick_transport(atransport* t); 156 void update_transports(void); 157 158 void init_transport_registration(void); 159 std::string list_transports(bool long_listing); 160 atransport* find_transport(const char* serial); 161 void kick_all_tcp_devices(); 162 163 void 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 */ 167 int register_socket_transport(int s, const char* serial, int port, int local); 168 169 // This should only be used for transports with connection_state == kCsNoPerm. 170 void unregister_usb_transport(usb_handle* usb); 171 172 int check_header(apacket* p, atransport* t); 173 int check_data(apacket* p); 174 175 /* for MacOS X cleanup */ 176 void close_usb_devices(); 177 178 void send_packet(apacket* p, atransport* t); 179 180 asocket* create_device_tracker(void); 181 182 #endif /* __TRANSPORT_H */ 183