The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 __ADB_H |
| 18 | #define __ADB_H |
| 19 | |
| 20 | #include <limits.h> |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 27 | |
leozwang | f25a6a4 | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 28 | #include "adb_trace.h" |
Dan Albert | bbbbea6 | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 29 | #include "fdevent.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 30 | #include "socket.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 31 | #include "types.h" |
Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 32 | #include "usb.h" |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 33 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 34 | constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024; |
Jerry Zhang | a2cb8de | 2017-07-18 14:07:57 -0700 | [diff] [blame] | 35 | constexpr size_t MAX_PAYLOAD = 1024 * 1024; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
Jerry Zhang | f0e239c | 2017-02-10 17:45:27 -0800 | [diff] [blame] | 37 | constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304; |
| 38 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | #define A_SYNC 0x434e5953 |
| 40 | #define A_CNXN 0x4e584e43 |
| 41 | #define A_OPEN 0x4e45504f |
| 42 | #define A_OKAY 0x59414b4f |
| 43 | #define A_CLSE 0x45534c43 |
| 44 | #define A_WRTE 0x45545257 |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 45 | #define A_AUTH 0x48545541 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 47 | // ADB protocol version. |
Tim Murray | ee7b44d | 2017-12-07 11:40:00 -0800 | [diff] [blame] | 48 | // Version revision: |
| 49 | // 0x01000000: original |
| 50 | // 0x01000001: skip checksum (Dec 2017) |
| 51 | #define A_VERSION_MIN 0x01000000 |
| 52 | #define A_VERSION_SKIP_CHECKSUM 0x01000001 |
| 53 | #define A_VERSION 0x01000001 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 55 | // Used for help/version information. |
| 56 | #define ADB_VERSION_MAJOR 1 |
| 57 | #define ADB_VERSION_MINOR 0 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
Elliott Hughes | b9f0164 | 2015-08-12 08:32:10 -0700 | [diff] [blame] | 59 | std::string adb_version(); |
| 60 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 61 | // Increment this when we want to force users to start a new adb server. |
Josh Gao | 8496bf5 | 2018-11-01 13:16:15 -0700 | [diff] [blame^] | 62 | #define ADB_SERVER_VERSION 40 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 64 | using TransportId = uint64_t; |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 65 | class atransport; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 67 | uint32_t calculate_apacket_checksum(const apacket* packet); |
| 68 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | /* the adisconnect structure is used to record a callback that |
| 70 | ** will be called whenever a transport is disconnected (e.g. by the user) |
| 71 | ** this should be used to cleanup objects that depend on the |
| 72 | ** transport (e.g. remote sockets, listeners, etc...) |
| 73 | */ |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 74 | struct adisconnect { |
| 75 | void (*func)(void* opaque, atransport* t); |
| 76 | void* opaque; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 79 | // A transport object models the connection to a remote device or emulator there |
| 80 | // is one transport per connected device/emulator. A "local transport" connects |
| 81 | // through TCP (for the emulator), while a "usb transport" through USB (for real |
| 82 | // devices). |
| 83 | // |
| 84 | // Note that kTransportHost doesn't really correspond to a real transport |
| 85 | // object, it's a special value used to indicate that a client wants to connect |
| 86 | // to a service implemented within the ADB server itself. |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 87 | enum TransportType { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 88 | kTransportUsb, |
| 89 | kTransportLocal, |
| 90 | kTransportAny, |
| 91 | kTransportHost, |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 92 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 94 | #define TOKEN_SIZE 20 |
| 95 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 96 | enum ConnectionState { |
| 97 | kCsAny = -1, |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 98 | |
| 99 | kCsConnecting = 0, // Haven't received a response from the device yet. |
| 100 | kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS. |
| 101 | kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt. |
| 102 | kCsNoPerm, // Insufficient permissions to communicate with the device. |
| 103 | kCsOffline, |
| 104 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 105 | kCsBootloader, |
| 106 | kCsDevice, |
| 107 | kCsHost, |
| 108 | kCsRecovery, |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 109 | kCsSideload, |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Josh Gao | 7a7c5cb | 2018-05-04 16:04:49 -0700 | [diff] [blame] | 112 | inline bool ConnectionStateIsOnline(ConnectionState state) { |
| 113 | switch (state) { |
| 114 | case kCsBootloader: |
| 115 | case kCsDevice: |
| 116 | case kCsHost: |
| 117 | case kCsRecovery: |
| 118 | case kCsSideload: |
| 119 | return true; |
| 120 | default: |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 125 | void print_packet(const char* label, apacket* p); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 127 | void handle_packet(apacket* p, atransport* t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 129 | int launch_server(const std::string& socket_spec); |
| 130 | int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | /* initialize a transport object's func pointers and state */ |
Josh Gao | fa3107e | 2018-07-25 17:21:49 -0700 | [diff] [blame] | 133 | int init_socket_transport(atransport* t, unique_fd s, int port, int local); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 134 | void init_usb_transport(atransport* t, usb_handle* usb); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | |
Lingfeng Yang | 5edb12b | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 136 | std::string getEmulatorSerialString(int console_port); |
Stefan Hilzinger | 1ec0342 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 137 | #if ADB_HOST |
| 138 | atransport* find_emulator_transport_by_adb_port(int adb_port); |
Lingfeng Yang | 5edb12b | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 139 | atransport* find_emulator_transport_by_console_port(int console_port); |
Stefan Hilzinger | 1ec0342 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 140 | #endif |
Mike Lockwood | 5f2c4a1 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 141 | |
Josh Gao | ebc1c31 | 2018-04-13 12:17:03 -0700 | [diff] [blame] | 142 | int service_to_fd(const char* name, atransport* transport); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 143 | #if !ADB_HOST |
| 144 | unique_fd daemon_service_to_fd(const char* name, atransport* transport); |
| 145 | #endif |
| 146 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | #if ADB_HOST |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 148 | asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | #endif |
| 150 | |
| 151 | #if !ADB_HOST |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 152 | int init_jdwp(void); |
| 153 | asocket* create_jdwp_service_socket(); |
| 154 | asocket* create_jdwp_tracker_service_socket(); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 155 | unique_fd create_jdwp_connection_fd(int jdwp_pid); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | #endif |
| 157 | |
Josh Gao | dd655ec | 2018-07-30 18:49:03 -0700 | [diff] [blame] | 158 | bool handle_forward_request(const char* service, atransport* transport, int reply_fd); |
| 159 | bool handle_forward_request(const char* service, |
| 160 | std::function<atransport*(std::string* error)> transport_acquirer, |
| 161 | int reply_fd); |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 162 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 163 | /* packet allocator */ |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 164 | apacket* get_apacket(void); |
| 165 | void put_apacket(apacket* p); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | |
leozwang | 1be5462 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 167 | // Define it if you want to dump packets. |
| 168 | #define DEBUG_PACKETS 0 |
| 169 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 170 | #if !DEBUG_PACKETS |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 171 | #define print_packet(tag, p) \ |
| 172 | do { \ |
| 173 | } while (0) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | #endif |
| 175 | |
John Michelau | 8733752 | 2010-09-23 17:08:34 -0500 | [diff] [blame] | 176 | #if ADB_HOST_ON_TARGET |
| 177 | /* adb and adbd are coexisting on the target, so use 5038 for adb |
| 178 | * to avoid conflicting with adbd's usage of 5037 |
| 179 | */ |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 180 | #define DEFAULT_ADB_PORT 5038 |
John Michelau | 8733752 | 2010-09-23 17:08:34 -0500 | [diff] [blame] | 181 | #else |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 182 | #define DEFAULT_ADB_PORT 5037 |
John Michelau | 8733752 | 2010-09-23 17:08:34 -0500 | [diff] [blame] | 183 | #endif |
| 184 | |
Stefan Hilzinger | 92ca4fa | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 185 | #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 186 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 187 | #define ADB_CLASS 0xff |
| 188 | #define ADB_SUBCLASS 0x42 |
| 189 | #define ADB_PROTOCOL 0x1 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 190 | |
Mike Lockwood | a8b3875 | 2009-08-26 12:50:22 -0700 | [diff] [blame] | 191 | void local_init(int port); |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 192 | bool local_connect(int port); |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 193 | int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 195 | ConnectionState connection_state(atransport* t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 197 | extern const char* adb_device_banner; |
| 198 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 199 | #define CHUNK_SIZE (64 * 1024) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 201 | #if !ADB_HOST |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 202 | #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" |
| 203 | #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 204 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 205 | #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) |
| 206 | #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) |
| 207 | #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 208 | #endif |
| 209 | |
Josh Gao | d306747 | 2018-08-07 14:14:21 -0700 | [diff] [blame] | 210 | bool handle_host_request(const char* service, TransportType type, const char* serial, |
| 211 | TransportId transport_id, int reply_fd, asocket* s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 212 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 213 | void handle_online(atransport* t); |
| 214 | void handle_offline(atransport* t); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 215 | |
Mark Salyzyn | d4bf94d | 2017-10-04 15:05:40 -0700 | [diff] [blame] | 216 | void send_connect(atransport* t); |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 217 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 218 | void parse_banner(const std::string&, atransport* t); |
| 219 | |
Josh Gao | 1e3bf73 | 2017-05-03 22:37:10 -0700 | [diff] [blame] | 220 | // On startup, the adb server needs to wait until all of the connected devices are ready. |
| 221 | // To do this, we need to know when the scan has identified all of the potential new transports, and |
| 222 | // when each transport becomes ready. |
| 223 | // TODO: Do this for mDNS as well, instead of just USB? |
| 224 | |
| 225 | // We've found all of the transports we potentially care about. |
| 226 | void adb_notify_device_scan_complete(); |
| 227 | |
| 228 | // One or more transports have changed status, check to see if we're ready. |
| 229 | void update_transport_status(); |
| 230 | |
| 231 | // Wait until device scan has completed and every transport is ready, or a timeout elapses. |
| 232 | void adb_wait_for_device_initialization(); |
| 233 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | #endif |