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 | a07dbad | 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 | 22fc5e9 | 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 | a07dbad | 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 | 1e2f7dc | 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 | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 44 | |
Dan Albert | be8e54b | 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() { |
| 53 | auth_fde = {}; |
| 54 | transport_fde = {}; |
| 55 | protocol_version = A_VERSION; |
| 56 | max_payload = MAX_PAYLOAD; |
| 57 | } |
| 58 | |
| 59 | virtual ~atransport() {} |
| 60 | |
| 61 | int (*read_from_remote)(apacket* p, atransport* t) = nullptr; |
| 62 | int (*write_to_remote)(apacket* p, atransport* t) = nullptr; |
| 63 | void (*close)(atransport* t) = nullptr; |
| 64 | void (*kick)(atransport* t) = nullptr; |
| 65 | |
| 66 | int fd = -1; |
| 67 | int transport_socket = -1; |
| 68 | fdevent transport_fde; |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 69 | size_t ref_count = 0; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 70 | uint32_t sync_token = 0; |
| 71 | ConnectionState connection_state = kCsOffline; |
| 72 | bool online = false; |
| 73 | TransportType type = kTransportAny; |
| 74 | |
| 75 | // USB handle or socket fd as needed. |
| 76 | usb_handle* usb = nullptr; |
| 77 | int sfd = -1; |
| 78 | |
| 79 | // Used to identify transports for clients. |
| 80 | char* serial = nullptr; |
| 81 | char* product = nullptr; |
| 82 | char* model = nullptr; |
| 83 | char* device = nullptr; |
| 84 | char* devpath = nullptr; |
| 85 | int adb_port = -1; // Use for emulators (local transport) |
| 86 | bool kicked = false; |
| 87 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 88 | void* key = nullptr; |
| 89 | unsigned char token[TOKEN_SIZE] = {}; |
| 90 | fdevent auth_fde; |
| 91 | size_t failed_auth_attempts = 0; |
| 92 | |
David Pursell | 6e5c7eb | 2015-12-02 15:14:31 -0800 | [diff] [blame] | 93 | const std::string connection_state_name() const; |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 94 | |
| 95 | void update_version(int version, size_t payload); |
| 96 | int get_protocol_version() const; |
| 97 | size_t get_max_payload() const; |
| 98 | |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 99 | const FeatureSet& features() const { |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 100 | return features_; |
| 101 | } |
| 102 | |
| 103 | bool has_feature(const std::string& feature) const; |
David Pursell | a07dbad | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 104 | |
| 105 | // Loads the transport's feature set from the given string. |
| 106 | void SetFeatures(const std::string& features_string); |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 107 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 108 | void AddDisconnect(adisconnect* disconnect); |
| 109 | void RemoveDisconnect(adisconnect* disconnect); |
| 110 | void RunDisconnects(); |
| 111 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 112 | private: |
| 113 | // A set of features transmitted in the banner with the initial connection. |
| 114 | // This is stored in the banner as 'features=feature0,feature1,etc'. |
| 115 | FeatureSet features_; |
| 116 | int protocol_version; |
| 117 | size_t max_payload; |
| 118 | |
Yabin Cui | 2d4c198 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 119 | // A list of adisconnect callbacks called when the transport is kicked. |
| 120 | std::list<adisconnect*> disconnects_; |
| 121 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(atransport); |
| 123 | }; |
| 124 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 125 | /* |
| 126 | * Obtain a transport from the available transports. |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 127 | * If serial is non-null then only the device with that serial will be chosen. |
| 128 | * If multiple devices/emulators would match, *is_ambiguous (if non-null) |
| 129 | * is set to true and nullptr returned. |
| 130 | * If no suitable transport is found, error is set and nullptr returned. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 131 | */ |
Elliott Hughes | 67943d1 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 132 | atransport* acquire_one_transport(TransportType type, const char* serial, |
| 133 | bool* is_ambiguous, std::string* error_out); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 134 | void kick_transport(atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 135 | void update_transports(void); |
| 136 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 137 | void init_transport_registration(void); |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 138 | std::string list_transports(bool long_listing); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 139 | atransport* find_transport(const char* serial); |
Yabin Cui | 4d64fd8 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 140 | void kick_all_tcp_devices(); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 141 | |
| 142 | void register_usb_transport(usb_handle* h, const char* serial, |
| 143 | const char* devpath, unsigned writeable); |
| 144 | |
| 145 | /* cause new transports to be init'd and added to the list */ |
| 146 | int register_socket_transport(int s, const char* serial, int port, int local); |
| 147 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 148 | // This should only be used for transports with connection_state == kCsNoPerm. |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 149 | void unregister_usb_transport(usb_handle* usb); |
| 150 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 151 | int check_header(apacket* p, atransport* t); |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 152 | int check_data(apacket* p); |
| 153 | |
| 154 | /* for MacOS X cleanup */ |
| 155 | void close_usb_devices(); |
| 156 | |
| 157 | void send_packet(apacket* p, atransport* t); |
| 158 | |
| 159 | asocket* create_device_tracker(void); |
| 160 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 161 | #endif /* __TRANSPORT_H */ |