JP Abgrall | 408fa57 | 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 | e9fca14 | 2015-02-18 18:03:26 -0800 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 22 | #include <list> |
Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 23 | #include <string> |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 24 | #include <unordered_set> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 25 | |
Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 26 | #include "adb.h" |
Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 27 | |
Dan Albert | 1792c23 | 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 | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 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 | |
David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 36 | // Returns true if both local features and |feature_set| support |feature|. |
| 37 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); |
| 38 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 39 | // Do not use any of [:;=,] in feature strings, they have special meaning |
| 40 | // in the connection banner. |
Todd Kennedy | 6fa848a | 2015-11-03 16:53:08 -0800 | [diff] [blame] | 41 | extern const char* const kFeatureShell2; |
| 42 | // The 'cmd' command is available |
| 43 | extern const char* const kFeatureCmd; |
David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 44 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 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() { |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 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 (*kick)(atransport* t) = nullptr; |
| 64 | |
| 65 | int fd = -1; |
| 66 | int transport_socket = -1; |
| 67 | fdevent transport_fde; |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 68 | size_t ref_count = 0; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 69 | uint32_t sync_token = 0; |
| 70 | ConnectionState connection_state = kCsOffline; |
| 71 | bool online = false; |
| 72 | TransportType type = kTransportAny; |
| 73 | |
| 74 | // USB handle or socket fd as needed. |
| 75 | usb_handle* usb = nullptr; |
| 76 | int sfd = -1; |
| 77 | |
| 78 | // Used to identify transports for clients. |
| 79 | char* serial = nullptr; |
| 80 | char* product = nullptr; |
| 81 | char* model = nullptr; |
| 82 | char* device = nullptr; |
| 83 | char* devpath = nullptr; |
| 84 | int adb_port = -1; // Use for emulators (local transport) |
| 85 | bool kicked = false; |
| 86 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 87 | void* key = nullptr; |
| 88 | unsigned char token[TOKEN_SIZE] = {}; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 89 | size_t failed_auth_attempts = 0; |
| 90 | |
David Pursell | d2acbd1 | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 91 | const std::string connection_state_name() const; |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 92 | |
| 93 | void update_version(int version, size_t payload); |
| 94 | int get_protocol_version() const; |
| 95 | size_t get_max_payload() const; |
| 96 | |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 97 | const FeatureSet& features() const { |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 98 | return features_; |
| 99 | } |
| 100 | |
| 101 | bool has_feature(const std::string& feature) const; |
David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 102 | |
| 103 | // Loads the transport's feature set from the given string. |
| 104 | void SetFeatures(const std::string& features_string); |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 105 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 106 | void AddDisconnect(adisconnect* disconnect); |
| 107 | void RemoveDisconnect(adisconnect* disconnect); |
| 108 | void RunDisconnects(); |
| 109 | |
David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame^] | 110 | // Returns true if |target| matches this transport. A matching |target| can be any of: |
| 111 | // * <serial> |
| 112 | // * <devpath> |
| 113 | // * product:<product> |
| 114 | // * model:<model> |
| 115 | // * device:<device> |
| 116 | // |
| 117 | // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets. |
| 118 | // For example, serial "100.100.100.100:5555" would match any of: |
| 119 | // * 100.100.100.100 |
| 120 | // * tcp:100.100.100.100 |
| 121 | // * udp:100.100.100.100:5555 |
| 122 | // This is to make it easier to use the same network target for both fastboot and adb. |
| 123 | bool MatchesTarget(const std::string& target) const; |
| 124 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 125 | private: |
| 126 | // A set of features transmitted in the banner with the initial connection. |
| 127 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 128 | FeatureSet features_; |
| 129 | int protocol_version; |
| 130 | size_t max_payload; |
| 131 | |
Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 132 | // A list of adisconnect callbacks called when the transport is kicked. |
| 133 | std::list<adisconnect*> disconnects_; |
| 134 | |
Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 136 | }; |
| 137 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 138 | /* |
| 139 | * Obtain a transport from the available transports. |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 140 | * If serial is non-null then only the device with that serial will be chosen. |
| 141 | * If multiple devices/emulators would match, *is_ambiguous (if non-null) |
| 142 | * is set to true and nullptr returned. |
| 143 | * If no suitable transport is found, error is set and nullptr returned. |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 144 | */ |
Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 145 | atransport* acquire_one_transport(TransportType type, const char* serial, |
| 146 | bool* is_ambiguous, std::string* error_out); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 147 | void kick_transport(atransport* t); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 148 | void update_transports(void); |
| 149 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 150 | void init_transport_registration(void); |
Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 151 | std::string list_transports(bool long_listing); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 152 | atransport* find_transport(const char* serial); |
Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 153 | void kick_all_tcp_devices(); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 154 | |
| 155 | void register_usb_transport(usb_handle* h, const char* serial, |
| 156 | const char* devpath, unsigned writeable); |
| 157 | |
| 158 | /* cause new transports to be init'd and added to the list */ |
| 159 | int register_socket_transport(int s, const char* serial, int port, int local); |
| 160 | |
Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 161 | // This should only be used for transports with connection_state == kCsNoPerm. |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 162 | void unregister_usb_transport(usb_handle* usb); |
| 163 | |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 164 | int check_header(apacket* p, atransport* t); |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 165 | int check_data(apacket* p); |
| 166 | |
| 167 | /* for MacOS X cleanup */ |
| 168 | void close_usb_devices(); |
| 169 | |
| 170 | void send_packet(apacket* p, atransport* t); |
| 171 | |
| 172 | asocket* create_device_tracker(void); |
| 173 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 174 | #endif /* __TRANSPORT_H */ |