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 class atransport; 60 61 struct amessage { 62 uint32_t command; /* command identifier constant */ 63 uint32_t arg0; /* first argument */ 64 uint32_t arg1; /* second argument */ 65 uint32_t data_length; /* length of payload (0 is allowed) */ 66 uint32_t data_check; /* checksum of data payload */ 67 uint32_t magic; /* command ^ 0xffffffff */ 68 }; 69 70 struct apacket 71 { 72 apacket *next; 73 74 size_t len; 75 char* ptr; 76 77 amessage msg; 78 char data[MAX_PAYLOAD]; 79 }; 80 81 uint32_t calculate_apacket_checksum(const apacket* packet); 82 83 /* the adisconnect structure is used to record a callback that 84 ** will be called whenever a transport is disconnected (e.g. by the user) 85 ** this should be used to cleanup objects that depend on the 86 ** transport (e.g. remote sockets, listeners, etc...) 87 */ 88 struct adisconnect 89 { 90 void (*func)(void* opaque, atransport* t); 91 void* opaque; 92 }; 93 94 95 // A transport object models the connection to a remote device or emulator there 96 // is one transport per connected device/emulator. A "local transport" connects 97 // through TCP (for the emulator), while a "usb transport" through USB (for real 98 // devices). 99 // 100 // Note that kTransportHost doesn't really correspond to a real transport 101 // object, it's a special value used to indicate that a client wants to connect 102 // to a service implemented within the ADB server itself. 103 enum TransportType { 104 kTransportUsb, 105 kTransportLocal, 106 kTransportAny, 107 kTransportHost, 108 }; 109 110 #define TOKEN_SIZE 20 111 112 enum ConnectionState { 113 kCsAny = -1, 114 kCsOffline = 0, 115 kCsBootloader, 116 kCsDevice, 117 kCsHost, 118 kCsRecovery, 119 kCsNoPerm, // Insufficient permissions to communicate with the device. 120 kCsSideload, 121 kCsUnauthorized, 122 }; 123 124 125 void print_packet(const char *label, apacket *p); 126 127 // These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they 128 // shouldn't be tagged with ADB_FORMAT_ARCHETYPE. 129 void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 130 void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 131 132 void handle_packet(apacket *p, atransport *t); 133 134 int launch_server(const std::string& socket_spec); 135 int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd); 136 137 /* initialize a transport object's func pointers and state */ 138 #if ADB_HOST 139 int get_available_local_transport_index(); 140 #endif 141 int init_socket_transport(atransport *t, int s, int port, int local); 142 void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state); 143 144 std::string getEmulatorSerialString(int console_port); 145 #if ADB_HOST 146 atransport* find_emulator_transport_by_adb_port(int adb_port); 147 atransport* find_emulator_transport_by_console_port(int console_port); 148 #endif 149 150 int service_to_fd(const char* name, const atransport* transport); 151 #if ADB_HOST 152 asocket *host_service_to_socket(const char* name, const char *serial); 153 #endif 154 155 #if !ADB_HOST 156 int init_jdwp(void); 157 asocket* create_jdwp_service_socket(); 158 asocket* create_jdwp_tracker_service_socket(); 159 int create_jdwp_connection_fd(int jdwp_pid); 160 #endif 161 162 int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd); 163 164 #if !ADB_HOST 165 void framebuffer_service(int fd, void *cookie); 166 void set_verity_enabled_state_service(int fd, void* cookie); 167 #endif 168 169 /* packet allocator */ 170 apacket *get_apacket(void); 171 void put_apacket(apacket *p); 172 173 // Define it if you want to dump packets. 174 #define DEBUG_PACKETS 0 175 176 #if !DEBUG_PACKETS 177 #define print_packet(tag,p) do {} while (0) 178 #endif 179 180 #if ADB_HOST_ON_TARGET 181 /* adb and adbd are coexisting on the target, so use 5038 for adb 182 * to avoid conflicting with adbd's usage of 5037 183 */ 184 # define DEFAULT_ADB_PORT 5038 185 #else 186 # define DEFAULT_ADB_PORT 5037 187 #endif 188 189 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 190 191 #define ADB_CLASS 0xff 192 #define ADB_SUBCLASS 0x42 193 #define ADB_PROTOCOL 0x1 194 195 196 void local_init(int port); 197 bool local_connect(int port); 198 int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error); 199 200 ConnectionState connection_state(atransport *t); 201 202 extern const char* adb_device_banner; 203 204 #if !ADB_HOST 205 extern int SHELL_EXIT_NOTIFY_FD; 206 #endif // !ADB_HOST 207 208 #define CHUNK_SIZE (64*1024) 209 210 #if !ADB_HOST 211 #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" 212 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x 213 214 #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) 215 #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) 216 #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) 217 #endif 218 219 int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s); 220 221 void handle_online(atransport *t); 222 void handle_offline(atransport *t); 223 224 void send_connect(atransport *t); 225 226 void parse_banner(const std::string&, atransport* t); 227 228 #endif 229