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