transport.h revision f4b9928563e97620fc1d9bd5c2efdaa0ded96488
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 <string> 23#include <unordered_set> 24 25#include "adb.h" 26 27typedef std::unordered_set<std::string> FeatureSet; 28 29const FeatureSet& supported_features(); 30 31class atransport { 32public: 33 // TODO(danalbert): We expose waaaaaaay too much stuff because this was 34 // historically just a struct, but making the whole thing a more idiomatic 35 // class in one go is a very large change. Given how bad our testing is, 36 // it's better to do this piece by piece. 37 38 atransport() { 39 auth_fde = {}; 40 transport_fde = {}; 41 protocol_version = A_VERSION; 42 max_payload = MAX_PAYLOAD; 43 } 44 45 virtual ~atransport() {} 46 47 int (*read_from_remote)(apacket* p, atransport* t) = nullptr; 48 int (*write_to_remote)(apacket* p, atransport* t) = nullptr; 49 void (*close)(atransport* t) = nullptr; 50 void (*kick)(atransport* t) = nullptr; 51 52 int fd = -1; 53 int transport_socket = -1; 54 fdevent transport_fde; 55 size_t ref_count = 0; 56 uint32_t sync_token = 0; 57 ConnectionState connection_state = kCsOffline; 58 bool online = false; 59 TransportType type = kTransportAny; 60 61 // USB handle or socket fd as needed. 62 usb_handle* usb = nullptr; 63 int sfd = -1; 64 65 // Used to identify transports for clients. 66 char* serial = nullptr; 67 char* product = nullptr; 68 char* model = nullptr; 69 char* device = nullptr; 70 char* devpath = nullptr; 71 int adb_port = -1; // Use for emulators (local transport) 72 bool kicked = false; 73 74 // A list of adisconnect callbacks called when the transport is kicked. 75 adisconnect disconnects = {}; 76 77 void* key = nullptr; 78 unsigned char token[TOKEN_SIZE] = {}; 79 fdevent auth_fde; 80 size_t failed_auth_attempts = 0; 81 82 const char* connection_state_name() const; 83 84 void update_version(int version, size_t payload); 85 int get_protocol_version() const; 86 size_t get_max_payload() const; 87 88 inline const FeatureSet features() const { 89 return features_; 90 } 91 92 bool has_feature(const std::string& feature) const; 93 void add_feature(const std::string& feature); 94 95 // Returns true if both we and the other end of the transport support the 96 // feature. 97 bool CanUseFeature(const std::string& feature) const; 98 99private: 100 // A set of features transmitted in the banner with the initial connection. 101 // This is stored in the banner as 'features=feature0,feature1,etc'. 102 FeatureSet features_; 103 int protocol_version; 104 size_t max_payload; 105 106 DISALLOW_COPY_AND_ASSIGN(atransport); 107}; 108 109/* 110 * Obtain a transport from the available transports. 111 * If state is != kCsAny, only transports in that state are considered. 112 * If serial is non-NULL then only the device with that serial will be chosen. 113 * If no suitable transport is found, error is set. 114 */ 115atransport* acquire_one_transport(ConnectionState state, TransportType type, 116 const char* serial, std::string* error_out); 117void add_transport_disconnect(atransport* t, adisconnect* dis); 118void remove_transport_disconnect(atransport* t, adisconnect* dis); 119void kick_transport(atransport* t); 120void run_transport_disconnects(atransport* t); 121void update_transports(void); 122 123void init_transport_registration(void); 124std::string list_transports(bool long_listing); 125atransport* find_transport(const char* serial); 126void kick_all_tcp_devices(); 127 128void register_usb_transport(usb_handle* h, const char* serial, 129 const char* devpath, unsigned writeable); 130 131/* cause new transports to be init'd and added to the list */ 132int register_socket_transport(int s, const char* serial, int port, int local); 133 134// This should only be used for transports with connection_state == kCsNoPerm. 135void unregister_usb_transport(usb_handle* usb); 136 137int check_header(apacket* p, atransport* t); 138int check_data(apacket* p); 139 140/* for MacOS X cleanup */ 141void close_usb_devices(); 142 143void send_packet(apacket* p, atransport* t); 144 145asocket* create_device_tracker(void); 146 147#endif /* __TRANSPORT_H */ 148