JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 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 | |
Dan Albert | 020292b | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 22 | #include <list> |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 23 | #include <string> |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 24 | #include <unordered_set> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 25 | |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 26 | #include "adb.h" |
Dan Albert | bbbbea6 | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 27 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 28 | typedef std::unordered_set<std::string> FeatureSet; |
| 29 | |
| 30 | const FeatureSet& supported_features(); |
| 31 | |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 32 | const extern char kFeatureShell2[]; |
| 33 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 34 | class atransport { |
| 35 | public: |
| 36 | // TODO(danalbert): We expose waaaaaaay too much stuff because this was |
| 37 | // historically just a struct, but making the whole thing a more idiomatic |
| 38 | // class in one go is a very large change. Given how bad our testing is, |
| 39 | // it's better to do this piece by piece. |
| 40 | |
| 41 | atransport() { |
| 42 | auth_fde = {}; |
| 43 | transport_fde = {}; |
| 44 | protocol_version = A_VERSION; |
| 45 | max_payload = MAX_PAYLOAD; |
| 46 | } |
| 47 | |
| 48 | virtual ~atransport() {} |
| 49 | |
| 50 | int (*read_from_remote)(apacket* p, atransport* t) = nullptr; |
| 51 | int (*write_to_remote)(apacket* p, atransport* t) = nullptr; |
| 52 | void (*close)(atransport* t) = nullptr; |
| 53 | void (*kick)(atransport* t) = nullptr; |
| 54 | |
| 55 | int fd = -1; |
| 56 | int transport_socket = -1; |
| 57 | fdevent transport_fde; |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 58 | size_t ref_count = 0; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 59 | uint32_t sync_token = 0; |
| 60 | ConnectionState connection_state = kCsOffline; |
| 61 | bool online = false; |
| 62 | TransportType type = kTransportAny; |
| 63 | |
| 64 | // USB handle or socket fd as needed. |
| 65 | usb_handle* usb = nullptr; |
| 66 | int sfd = -1; |
| 67 | |
| 68 | // Used to identify transports for clients. |
| 69 | char* serial = nullptr; |
| 70 | char* product = nullptr; |
| 71 | char* model = nullptr; |
| 72 | char* device = nullptr; |
| 73 | char* devpath = nullptr; |
| 74 | int adb_port = -1; // Use for emulators (local transport) |
| 75 | bool kicked = false; |
| 76 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 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 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 99 | void AddDisconnect(adisconnect* disconnect); |
| 100 | void RemoveDisconnect(adisconnect* disconnect); |
| 101 | void RunDisconnects(); |
| 102 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 103 | private: |
| 104 | // A set of features transmitted in the banner with the initial connection. |
| 105 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 106 | FeatureSet features_; |
| 107 | int protocol_version; |
| 108 | size_t max_payload; |
| 109 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 110 | // A list of adisconnect callbacks called when the transport is kicked. |
| 111 | std::list<adisconnect*> disconnects_; |
| 112 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 114 | }; |
| 115 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Obtain a transport from the available transports. |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 118 | * If state is != kCsAny, only transports in that state are considered. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 119 | * If serial is non-NULL then only the device with that serial will be chosen. |
| 120 | * If no suitable transport is found, error is set. |
| 121 | */ |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 122 | atransport* acquire_one_transport(ConnectionState state, TransportType type, |
Elliott Hughes | ab88242 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 123 | const char* serial, std::string* error_out); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 124 | void kick_transport(atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 125 | void update_transports(void); |
| 126 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 127 | void init_transport_registration(void); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 128 | std::string list_transports(bool long_listing); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 129 | atransport* find_transport(const char* serial); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 130 | void kick_all_tcp_devices(); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 131 | |
| 132 | void register_usb_transport(usb_handle* h, const char* serial, |
| 133 | const char* devpath, unsigned writeable); |
| 134 | |
| 135 | /* cause new transports to be init'd and added to the list */ |
| 136 | int register_socket_transport(int s, const char* serial, int port, int local); |
| 137 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 138 | // This should only be used for transports with connection_state == kCsNoPerm. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 139 | void unregister_usb_transport(usb_handle* usb); |
| 140 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 141 | int check_header(apacket* p, atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 142 | int check_data(apacket* p); |
| 143 | |
| 144 | /* for MacOS X cleanup */ |
| 145 | void close_usb_devices(); |
| 146 | |
| 147 | void send_packet(apacket* p, atransport* t); |
| 148 | |
| 149 | asocket* create_device_tracker(void); |
| 150 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 151 | #endif /* __TRANSPORT_H */ |