| 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 | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 22 | #include <atomic> | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 23 | #include <chrono> | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 24 | #include <condition_variable> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 25 | #include <deque> | 
| Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 26 | #include <functional> | 
| Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 27 | #include <list> | 
| Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 28 | #include <memory> | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 29 | #include <mutex> | 
| Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 30 | #include <string> | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 31 | #include <thread> | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 32 | #include <unordered_set> | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 33 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 34 | #include <android-base/macros.h> | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 35 | #include <android-base/thread_annotations.h> | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 36 | #include <openssl/rsa.h> | 
 | 37 |  | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 38 | #include "adb.h" | 
 | 39 | #include "adb_unique_fd.h" | 
 | 40 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 41 | typedef std::unordered_set<std::string> FeatureSet; | 
 | 42 |  | 
 | 43 | const FeatureSet& supported_features(); | 
 | 44 |  | 
| David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 45 | // Encodes and decodes FeatureSet objects into human-readable strings. | 
 | 46 | std::string FeatureSetToString(const FeatureSet& features); | 
 | 47 | FeatureSet StringToFeatureSet(const std::string& features_string); | 
 | 48 |  | 
| David Pursell | 70ef7b4 | 2015-09-30 13:35:42 -0700 | [diff] [blame] | 49 | // Returns true if both local features and |feature_set| support |feature|. | 
 | 50 | bool CanUseFeature(const FeatureSet& feature_set, const std::string& feature); | 
 | 51 |  | 
| David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 52 | // Do not use any of [:;=,] in feature strings, they have special meaning | 
 | 53 | // in the connection banner. | 
| Todd Kennedy | 6fa848a | 2015-11-03 16:53:08 -0800 | [diff] [blame] | 54 | extern const char* const kFeatureShell2; | 
 | 55 | // The 'cmd' command is available | 
 | 56 | extern const char* const kFeatureCmd; | 
| Josh Gao | 5a1e3fd | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 57 | extern const char* const kFeatureStat2; | 
| Josh Gao | 5d1756c | 2017-02-22 17:07:01 -0800 | [diff] [blame] | 58 | // The server is running with libusb enabled. | 
 | 59 | extern const char* const kFeatureLibusb; | 
| Dan Albert | 5176df8 | 2017-05-23 14:30:00 -0700 | [diff] [blame] | 60 | // The server supports `push --sync`. | 
 | 61 | extern const char* const kFeaturePushSync; | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 62 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 63 | TransportId NextTransportId(); | 
 | 64 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 65 | // Abstraction for a non-blocking packet transport. | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 66 | struct Connection { | 
 | 67 |     Connection() = default; | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 68 |     virtual ~Connection() = default; | 
 | 69 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 70 |     void SetTransportName(std::string transport_name) { | 
 | 71 |         transport_name_ = std::move(transport_name); | 
 | 72 |     } | 
 | 73 |  | 
 | 74 |     using ReadCallback = std::function<bool(Connection*, std::unique_ptr<apacket>)>; | 
 | 75 |     void SetReadCallback(ReadCallback callback) { | 
 | 76 |         CHECK(!read_callback_); | 
 | 77 |         read_callback_ = callback; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |     // Called after the Connection has terminated, either by an error or because Stop was called. | 
 | 81 |     using ErrorCallback = std::function<void(Connection*, const std::string&)>; | 
 | 82 |     void SetErrorCallback(ErrorCallback callback) { | 
 | 83 |         CHECK(!error_callback_); | 
 | 84 |         error_callback_ = callback; | 
 | 85 |     } | 
 | 86 |  | 
 | 87 |     virtual bool Write(std::unique_ptr<apacket> packet) = 0; | 
 | 88 |  | 
 | 89 |     virtual void Start() = 0; | 
 | 90 |     virtual void Stop() = 0; | 
 | 91 |  | 
 | 92 |     std::string transport_name_; | 
 | 93 |     ReadCallback read_callback_; | 
 | 94 |     ErrorCallback error_callback_; | 
 | 95 | }; | 
 | 96 |  | 
 | 97 | // Abstraction for a blocking packet transport. | 
 | 98 | struct BlockingConnection { | 
 | 99 |     BlockingConnection() = default; | 
 | 100 |     BlockingConnection(const BlockingConnection& copy) = delete; | 
 | 101 |     BlockingConnection(BlockingConnection&& move) = delete; | 
 | 102 |  | 
 | 103 |     // Destroy a BlockingConnection. Formerly known as 'Close' in atransport. | 
 | 104 |     virtual ~BlockingConnection() = default; | 
 | 105 |  | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 106 |     // Read/Write a packet. These functions are concurrently called from a transport's reader/writer | 
 | 107 |     // threads. | 
 | 108 |     virtual bool Read(apacket* packet) = 0; | 
 | 109 |     virtual bool Write(apacket* packet) = 0; | 
 | 110 |  | 
 | 111 |     // Terminate a connection. | 
 | 112 |     // This method must be thread-safe, and must cause concurrent Reads/Writes to terminate. | 
 | 113 |     // Formerly known as 'Kick' in atransport. | 
 | 114 |     virtual void Close() = 0; | 
 | 115 | }; | 
 | 116 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 117 | struct BlockingConnectionAdapter : public Connection { | 
 | 118 |     explicit BlockingConnectionAdapter(std::unique_ptr<BlockingConnection> connection); | 
 | 119 |  | 
 | 120 |     virtual ~BlockingConnectionAdapter(); | 
 | 121 |  | 
 | 122 |     virtual bool Write(std::unique_ptr<apacket> packet) override final; | 
 | 123 |  | 
 | 124 |     virtual void Start() override final; | 
 | 125 |     virtual void Stop() override final; | 
 | 126 |  | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 127 |     bool started_ GUARDED_BY(mutex_) = false; | 
 | 128 |     bool stopped_ GUARDED_BY(mutex_) = false; | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 129 |  | 
 | 130 |     std::unique_ptr<BlockingConnection> underlying_; | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 131 |     std::thread read_thread_ GUARDED_BY(mutex_); | 
 | 132 |     std::thread write_thread_ GUARDED_BY(mutex_); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 133 |  | 
| Josh Gao | c251ec5 | 2018-04-03 12:55:18 -0700 | [diff] [blame] | 134 |     std::deque<std::unique_ptr<apacket>> write_queue_ GUARDED_BY(mutex_); | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 135 |     std::mutex mutex_; | 
 | 136 |     std::condition_variable cv_; | 
 | 137 |  | 
 | 138 |     std::once_flag error_flag_; | 
 | 139 | }; | 
 | 140 |  | 
 | 141 | struct FdConnection : public BlockingConnection { | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 142 |     explicit FdConnection(unique_fd fd) : fd_(std::move(fd)) {} | 
 | 143 |  | 
 | 144 |     bool Read(apacket* packet) override final; | 
 | 145 |     bool Write(apacket* packet) override final; | 
 | 146 |  | 
 | 147 |     void Close() override; | 
 | 148 |  | 
 | 149 |   private: | 
 | 150 |     unique_fd fd_; | 
 | 151 | }; | 
 | 152 |  | 
| Josh Gao | 0bbf69c | 2018-02-16 13:24:58 -0800 | [diff] [blame] | 153 | struct UsbConnection : public BlockingConnection { | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 154 |     explicit UsbConnection(usb_handle* handle) : handle_(handle) {} | 
 | 155 |     ~UsbConnection(); | 
 | 156 |  | 
 | 157 |     bool Read(apacket* packet) override final; | 
 | 158 |     bool Write(apacket* packet) override final; | 
 | 159 |  | 
 | 160 |     void Close() override final; | 
 | 161 |  | 
 | 162 |     usb_handle* handle_; | 
 | 163 | }; | 
 | 164 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 165 | // Waits for a transport's connection to be not pending. This is a separate | 
 | 166 | // object so that the transport can be destroyed and another thread can be | 
 | 167 | // notified of it in a race-free way. | 
 | 168 | class ConnectionWaitable { | 
 | 169 |   public: | 
 | 170 |     ConnectionWaitable() = default; | 
 | 171 |     ~ConnectionWaitable() = default; | 
 | 172 |  | 
 | 173 |     // Waits until the first CNXN packet has been received by the owning | 
 | 174 |     // atransport, or the specified timeout has elapsed. Can be called from any | 
 | 175 |     // thread. | 
 | 176 |     // | 
 | 177 |     // Returns true if the CNXN packet was received in a timely fashion, false | 
 | 178 |     // otherwise. | 
 | 179 |     bool WaitForConnection(std::chrono::milliseconds timeout); | 
 | 180 |  | 
 | 181 |     // Can be called from any thread when the connection stops being pending. | 
 | 182 |     // Only the first invocation will be acknowledged, the rest will be no-ops. | 
 | 183 |     void SetConnectionEstablished(bool success); | 
 | 184 |  | 
 | 185 |   private: | 
 | 186 |     bool connection_established_ GUARDED_BY(mutex_) = false; | 
 | 187 |     bool connection_established_ready_ GUARDED_BY(mutex_) = false; | 
 | 188 |     std::mutex mutex_; | 
 | 189 |     std::condition_variable cv_; | 
 | 190 |  | 
 | 191 |     DISALLOW_COPY_AND_ASSIGN(ConnectionWaitable); | 
 | 192 | }; | 
 | 193 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 194 | class atransport { | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 195 |   public: | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 196 |     // TODO(danalbert): We expose waaaaaaay too much stuff because this was | 
 | 197 |     // historically just a struct, but making the whole thing a more idiomatic | 
 | 198 |     // class in one go is a very large change. Given how bad our testing is, | 
 | 199 |     // it's better to do this piece by piece. | 
 | 200 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 201 |     using ReconnectCallback = std::function<bool(atransport*)>; | 
 | 202 |  | 
 | 203 |     atransport(ReconnectCallback reconnect, ConnectionState state) | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 204 |         : id(NextTransportId()), | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 205 |           kicked_(false), | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 206 |           connection_state_(state), | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 207 |           connection_waitable_(std::make_shared<ConnectionWaitable>()), | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 208 |           connection_(nullptr), | 
 | 209 |           reconnect_(std::move(reconnect)) { | 
| Tim Murray | de47194 | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 210 |         // Initialize protocol to min version for compatibility with older versions. | 
 | 211 |         // Version will be updated post-connect. | 
 | 212 |         protocol_version = A_VERSION_MIN; | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 213 |         max_payload = MAX_PAYLOAD; | 
 | 214 |     } | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 215 |     atransport(ConnectionState state = kCsOffline) | 
 | 216 |         : atransport([](atransport*) { return false; }, state) {} | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 217 |     virtual ~atransport(); | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 218 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 219 |     int Write(apacket* p); | 
| Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 220 |     void Kick(); | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 221 |     bool kicked() const { return kicked_; } | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 222 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 223 |     // ConnectionState can be read by all threads, but can only be written in the main thread. | 
 | 224 |     ConnectionState GetConnectionState() const; | 
 | 225 |     void SetConnectionState(ConnectionState state); | 
 | 226 |  | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 227 |     void SetConnection(std::unique_ptr<Connection> connection); | 
 | 228 |     std::shared_ptr<Connection> connection() { | 
 | 229 |         std::lock_guard<std::mutex> lock(mutex_); | 
 | 230 |         return connection_; | 
 | 231 |     } | 
 | 232 |  | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 233 |     const TransportId id; | 
| Josh Gao | e48ecce | 2017-09-13 13:40:57 -0700 | [diff] [blame] | 234 |     size_t ref_count = 0; | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 235 |     bool online = false; | 
 | 236 |     TransportType type = kTransportAny; | 
 | 237 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 238 |     // Used to identify transports for clients. | 
 | 239 |     char* serial = nullptr; | 
 | 240 |     char* product = nullptr; | 
 | 241 |     char* model = nullptr; | 
 | 242 |     char* device = nullptr; | 
 | 243 |     char* devpath = nullptr; | 
| Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 244 |  | 
| Josh Gao | b800d88 | 2018-01-28 20:32:46 -0800 | [diff] [blame] | 245 |     bool IsTcpDevice() const { return type == kTransportLocal; } | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 246 |  | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 247 | #if ADB_HOST | 
| Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 248 |     std::shared_ptr<RSA> NextKey(); | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 249 | #endif | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 250 |  | 
| Josh Gao | 06d61d4 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 251 |     char token[TOKEN_SIZE] = {}; | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 252 |     size_t failed_auth_attempts = 0; | 
 | 253 |  | 
| Josh Gao | ffbd336 | 2018-02-28 14:44:23 -0800 | [diff] [blame] | 254 |     std::string serial_name() const { return serial ? serial : "<unknown>"; } | 
 | 255 |     std::string connection_state_name() const; | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 256 |  | 
 | 257 |     void update_version(int version, size_t payload); | 
 | 258 |     int get_protocol_version() const; | 
 | 259 |     size_t get_max_payload() const; | 
 | 260 |  | 
| David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 261 |     const FeatureSet& features() const { | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 262 |         return features_; | 
 | 263 |     } | 
 | 264 |  | 
 | 265 |     bool has_feature(const std::string& feature) const; | 
| David Pursell | 4e2fd36 | 2015-09-22 10:43:08 -0700 | [diff] [blame] | 266 |  | 
 | 267 |     // Loads the transport's feature set from the given string. | 
 | 268 |     void SetFeatures(const std::string& features_string); | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 269 |  | 
| Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 270 |     void AddDisconnect(adisconnect* disconnect); | 
 | 271 |     void RemoveDisconnect(adisconnect* disconnect); | 
 | 272 |     void RunDisconnects(); | 
 | 273 |  | 
| David Pursell | 3f902aa | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 274 |     // Returns true if |target| matches this transport. A matching |target| can be any of: | 
 | 275 |     //   * <serial> | 
 | 276 |     //   * <devpath> | 
 | 277 |     //   * product:<product> | 
 | 278 |     //   * model:<model> | 
 | 279 |     //   * device:<device> | 
 | 280 |     // | 
 | 281 |     // If this is a local transport, serial will also match [tcp:|udp:]<hostname>[:port] targets. | 
 | 282 |     // For example, serial "100.100.100.100:5555" would match any of: | 
 | 283 |     //   * 100.100.100.100 | 
 | 284 |     //   * tcp:100.100.100.100 | 
 | 285 |     //   * udp:100.100.100.100:5555 | 
 | 286 |     // This is to make it easier to use the same network target for both fastboot and adb. | 
 | 287 |     bool MatchesTarget(const std::string& target) const; | 
 | 288 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 289 |     // Notifies that the atransport is no longer waiting for the connection | 
 | 290 |     // being established. | 
 | 291 |     void SetConnectionEstablished(bool success); | 
 | 292 |  | 
 | 293 |     // Gets a shared reference to the ConnectionWaitable. | 
 | 294 |     std::shared_ptr<ConnectionWaitable> connection_waitable() { return connection_waitable_; } | 
 | 295 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 296 |     // Attempts to reconnect with the underlying Connection. Returns true if the | 
 | 297 |     // reconnection attempt succeeded. | 
 | 298 |     bool Reconnect(); | 
 | 299 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 300 |   private: | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 301 |     std::atomic<bool> kicked_; | 
| Yabin Cui | 7f27490 | 2016-04-18 11:22:34 -0700 | [diff] [blame] | 302 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 303 |     // A set of features transmitted in the banner with the initial connection. | 
 | 304 |     // This is stored in the banner as 'features=feature0,feature1,etc'. | 
 | 305 |     FeatureSet features_; | 
 | 306 |     int protocol_version; | 
 | 307 |     size_t max_payload; | 
 | 308 |  | 
| Yabin Cui | b329824 | 2015-08-28 15:09:44 -0700 | [diff] [blame] | 309 |     // A list of adisconnect callbacks called when the transport is kicked. | 
 | 310 |     std::list<adisconnect*> disconnects_; | 
 | 311 |  | 
| Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 312 |     std::atomic<ConnectionState> connection_state_; | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 313 | #if ADB_HOST | 
| Josh Gao | 2e67120 | 2016-08-18 22:00:12 -0700 | [diff] [blame] | 314 |     std::deque<std::shared_ptr<RSA>> keys_; | 
| Josh Gao | 3bd2879 | 2016-10-05 19:02:29 -0700 | [diff] [blame] | 315 | #endif | 
| Elliott Hughes | 0aeb505 | 2016-06-29 17:42:01 -0700 | [diff] [blame] | 316 |  | 
| Luis Hector Chavez | 56fe753 | 2018-04-17 14:25:04 -0700 | [diff] [blame] | 317 |     // A sharable object that can be used to wait for the atransport's | 
 | 318 |     // connection to be established. | 
 | 319 |     std::shared_ptr<ConnectionWaitable> connection_waitable_; | 
 | 320 |  | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 321 |     // The underlying connection object. | 
 | 322 |     std::shared_ptr<Connection> connection_ GUARDED_BY(mutex_); | 
 | 323 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 324 |     // A callback that will be invoked when the atransport needs to reconnect. | 
 | 325 |     ReconnectCallback reconnect_; | 
 | 326 |  | 
| Luis Hector Chavez | 9a388d5 | 2018-04-25 08:56:41 -0700 | [diff] [blame] | 327 |     std::mutex mutex_; | 
 | 328 |  | 
| Dan Albert | 1792c23 | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 329 |     DISALLOW_COPY_AND_ASSIGN(atransport); | 
 | 330 | }; | 
 | 331 |  | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 332 | /* | 
 | 333 |  * Obtain a transport from the available transports. | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 334 |  * If serial is non-null then only the device with that serial will be chosen. | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 335 |  * If transport_id is non-zero then only the device with that transport ID will be chosen. | 
| Elliott Hughes | 8d28e19 | 2015-10-07 14:55:10 -0700 | [diff] [blame] | 336 |  * If multiple devices/emulators would match, *is_ambiguous (if non-null) | 
 | 337 |  * is set to true and nullptr returned. | 
 | 338 |  * If no suitable transport is found, error is set and nullptr returned. | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 339 |  */ | 
| Josh Gao | b122b17 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 340 | atransport* acquire_one_transport(TransportType type, const char* serial, TransportId transport_id, | 
 | 341 |                                   bool* is_ambiguous, std::string* error_out, | 
 | 342 |                                   bool accept_any_state = false); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 343 | void kick_transport(atransport* t); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 344 | void update_transports(void); | 
 | 345 |  | 
| Josh Gao | fd713e5 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 346 | // Iterates across all of the current and pending transports. | 
 | 347 | // Stops iteration and returns false if fn returns false, otherwise returns true. | 
 | 348 | bool iterate_transports(std::function<bool(const atransport*)> fn); | 
 | 349 |  | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 350 | void init_reconnect_handler(void); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 351 | void init_transport_registration(void); | 
| Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 352 | void init_mdns_transport_discovery(void); | 
| Elliott Hughes | e67f1f8 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 353 | std::string list_transports(bool long_listing); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 354 | atransport* find_transport(const char* serial); | 
| Yabin Cui | f4b9928 | 2015-08-27 12:03:11 -0700 | [diff] [blame] | 355 | void kick_all_tcp_devices(); | 
| Josh Gao | 01b7bc4 | 2017-05-09 13:43:35 -0700 | [diff] [blame] | 356 | void kick_all_transports(); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 357 |  | 
 | 358 | void register_usb_transport(usb_handle* h, const char* serial, | 
 | 359 |                             const char* devpath, unsigned writeable); | 
 | 360 |  | 
| Casey Dahlin | 13a269e | 2016-06-23 14:19:37 -0700 | [diff] [blame] | 361 | /* Connect to a network address and register it as a device */ | 
 | 362 | void connect_device(const std::string& address, std::string* response); | 
 | 363 |  | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 364 | /* cause new transports to be init'd and added to the list */ | 
| Luis Hector Chavez | 454bc7c | 2018-04-20 10:31:29 -0700 | [diff] [blame] | 365 | int register_socket_transport(int s, const char* serial, int port, int local, | 
 | 366 |                               atransport::ReconnectCallback reconnect); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 367 |  | 
| Dan Albert | dcd78a1 | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 368 | // This should only be used for transports with connection_state == kCsNoPerm. | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 369 | void unregister_usb_transport(usb_handle* usb); | 
 | 370 |  | 
| Josh Gao | 36dadca | 2017-05-16 15:02:45 -0700 | [diff] [blame] | 371 | bool check_header(apacket* p, atransport* t); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 372 |  | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 373 | void close_usb_devices(); | 
| Josh Gao | 22d2b3e | 2016-10-27 14:01:08 -0700 | [diff] [blame] | 374 | void close_usb_devices(std::function<bool(const atransport*)> predicate); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 375 |  | 
 | 376 | void send_packet(apacket* p, atransport* t); | 
 | 377 |  | 
| Josh Gao | b0c1802 | 2017-08-14 18:57:54 -0700 | [diff] [blame] | 378 | asocket* create_device_tracker(bool long_output); | 
| Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 379 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 380 | #endif   /* __TRANSPORT_H */ |