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> 21 #include <stdint.h> 22 #include <sys/types.h> 23 24 #include <string> 25 26 #include <android-base/macros.h> 27 28 #include "adb_trace.h" 29 #include "fdevent.h" 30 #include "socket.h" 31 #include "usb.h" 32 33 constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024; 34 constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024; 35 constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2; 36 37 constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304; 38 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 45 #define A_AUTH 0x48545541 46 47 // ADB protocol version. 48 #define A_VERSION 0x01000000 49 50 // Used for help/version information. 51 #define ADB_VERSION_MAJOR 1 52 #define ADB_VERSION_MINOR 0 53 54 std::string adb_version(); 55 56 // Increment this when we want to force users to start a new adb server. 57 #define ADB_SERVER_VERSION 39 58 59 using TransportId = uint64_t; 60 class atransport; 61 62 struct amessage { 63 uint32_t command; /* command identifier constant */ 64 uint32_t arg0; /* first argument */ 65 uint32_t arg1; /* second argument */ 66 uint32_t data_length; /* length of payload (0 is allowed) */ 67 uint32_t data_check; /* checksum of data payload */ 68 uint32_t magic; /* command ^ 0xffffffff */ 69 }; 70 71 struct apacket 72 { 73 apacket *next; 74 75 size_t len; 76 char* ptr; 77 78 amessage msg; 79 char data[MAX_PAYLOAD]; 80 }; 81 82 uint32_t calculate_apacket_checksum(const apacket* packet); 83 84 /* the adisconnect structure is used to record a callback that 85 ** will be called whenever a transport is disconnected (e.g. by the user) 86 ** this should be used to cleanup objects that depend on the 87 ** transport (e.g. remote sockets, listeners, etc...) 88 */ 89 struct adisconnect 90 { 91 void (*func)(void* opaque, atransport* t); 92 void* opaque; 93 }; 94 95 96 // A transport object models the connection to a remote device or emulator there 97 // is one transport per connected device/emulator. A "local transport" connects 98 // through TCP (for the emulator), while a "usb transport" through USB (for real 99 // devices). 100 // 101 // Note that kTransportHost doesn't really correspond to a real transport 102 // object, it's a special value used to indicate that a client wants to connect 103 // to a service implemented within the ADB server itself. 104 enum TransportType { 105 kTransportUsb, 106 kTransportLocal, 107 kTransportAny, 108 kTransportHost, 109 }; 110 111 #define TOKEN_SIZE 20 112 113 enum ConnectionState { 114 kCsAny = -1, 115 kCsOffline = 0, 116 kCsBootloader, 117 kCsDevice, 118 kCsHost, 119 kCsRecovery, 120 kCsNoPerm, // Insufficient permissions to communicate with the device. 121 kCsSideload, 122 kCsUnauthorized, 123 }; 124 125 126 void print_packet(const char *label, apacket *p); 127 128 // These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they 129 // shouldn't be tagged with ADB_FORMAT_ARCHETYPE. 130 void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 131 void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 132 133 void handle_packet(apacket *p, atransport *t); 134 135 int launch_server(const std::string& socket_spec); 136 int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd); 137 138 /* initialize a transport object's func pointers and state */ 139 #if ADB_HOST 140 int get_available_local_transport_index(); 141 #endif 142 int init_socket_transport(atransport *t, int s, int port, int local); 143 void init_usb_transport(atransport* t, usb_handle* usb); 144 145 std::string getEmulatorSerialString(int console_port); 146 #if ADB_HOST 147 atransport* find_emulator_transport_by_adb_port(int adb_port); 148 atransport* find_emulator_transport_by_console_port(int console_port); 149 #endif 150 151 int service_to_fd(const char* name, const atransport* transport); 152 #if ADB_HOST 153 asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id); 154 #endif 155 156 #if !ADB_HOST 157 int init_jdwp(void); 158 asocket* create_jdwp_service_socket(); 159 asocket* create_jdwp_tracker_service_socket(); 160 int create_jdwp_connection_fd(int jdwp_pid); 161 #endif 162 163 int handle_forward_request(const char* service, TransportType type, const char* serial, 164 TransportId transport_id, int reply_fd); 165 166 #if !ADB_HOST 167 void framebuffer_service(int fd, void *cookie); 168 void set_verity_enabled_state_service(int fd, void* cookie); 169 #endif 170 171 /* packet allocator */ 172 apacket *get_apacket(void); 173 void put_apacket(apacket *p); 174 175 // Define it if you want to dump packets. 176 #define DEBUG_PACKETS 0 177 178 #if !DEBUG_PACKETS 179 #define print_packet(tag,p) do {} while (0) 180 #endif 181 182 #if ADB_HOST_ON_TARGET 183 /* adb and adbd are coexisting on the target, so use 5038 for adb 184 * to avoid conflicting with adbd's usage of 5037 185 */ 186 # define DEFAULT_ADB_PORT 5038 187 #else 188 # define DEFAULT_ADB_PORT 5037 189 #endif 190 191 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 192 193 #define ADB_CLASS 0xff 194 #define ADB_SUBCLASS 0x42 195 #define ADB_PROTOCOL 0x1 196 197 198 void local_init(int port); 199 bool local_connect(int port); 200 int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error); 201 202 ConnectionState connection_state(atransport *t); 203 204 extern const char* adb_device_banner; 205 206 #if !ADB_HOST 207 extern int SHELL_EXIT_NOTIFY_FD; 208 #endif // !ADB_HOST 209 210 #define CHUNK_SIZE (64*1024) 211 212 #if !ADB_HOST 213 #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" 214 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x 215 216 #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) 217 #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) 218 #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) 219 #endif 220 221 int handle_host_request(const char* service, TransportType type, const char* serial, 222 TransportId transport_id, int reply_fd, asocket* s); 223 224 void handle_online(atransport *t); 225 void handle_offline(atransport *t); 226 227 void send_connect(atransport *t); 228 229 void parse_banner(const std::string&, atransport* t); 230 231 // On startup, the adb server needs to wait until all of the connected devices are ready. 232 // To do this, we need to know when the scan has identified all of the potential new transports, and 233 // when each transport becomes ready. 234 // TODO: Do this for mDNS as well, instead of just USB? 235 236 // We've found all of the transports we potentially care about. 237 void adb_notify_device_scan_complete(); 238 239 // One or more transports have changed status, check to see if we're ready. 240 void update_transport_status(); 241 242 // Wait until device scan has completed and every transport is ready, or a timeout elapses. 243 void adb_wait_for_device_initialization(); 244 245 #endif 246