adb.h revision c7915a3470292349017f94ca066ed515babfcc23
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 <sys/types.h> 22 23#include <base/macros.h> 24 25#include "adb_trace.h" 26#include "fdevent.h" 27 28#define MAX_PAYLOAD 4096 29 30#define A_SYNC 0x434e5953 31#define A_CNXN 0x4e584e43 32#define A_OPEN 0x4e45504f 33#define A_OKAY 0x59414b4f 34#define A_CLSE 0x45534c43 35#define A_WRTE 0x45545257 36#define A_AUTH 0x48545541 37 38// ADB protocol version. 39#define A_VERSION 0x01000000 40 41// Used for help/version information. 42#define ADB_VERSION_MAJOR 1 43#define ADB_VERSION_MINOR 0 44 45// Increment this when we want to force users to start a new adb server. 46#define ADB_SERVER_VERSION 32 47 48class atransport; 49struct usb_handle; 50 51struct amessage { 52 unsigned command; /* command identifier constant */ 53 unsigned arg0; /* first argument */ 54 unsigned arg1; /* second argument */ 55 unsigned data_length; /* length of payload (0 is allowed) */ 56 unsigned data_check; /* checksum of data payload */ 57 unsigned magic; /* command ^ 0xffffffff */ 58}; 59 60struct apacket 61{ 62 apacket *next; 63 64 unsigned len; 65 unsigned char *ptr; 66 67 amessage msg; 68 unsigned char data[MAX_PAYLOAD]; 69}; 70 71/* An asocket represents one half of a connection between a local and 72** remote entity. A local asocket is bound to a file descriptor. A 73** remote asocket is bound to the protocol engine. 74*/ 75struct asocket { 76 /* chain pointers for the local/remote list of 77 ** asockets that this asocket lives in 78 */ 79 asocket *next; 80 asocket *prev; 81 82 /* the unique identifier for this asocket 83 */ 84 unsigned id; 85 86 /* flag: set when the socket's peer has closed 87 ** but packets are still queued for delivery 88 */ 89 int closing; 90 91 /* flag: quit adbd when both ends close the 92 ** local service socket 93 */ 94 int exit_on_close; 95 96 /* the asocket we are connected to 97 */ 98 99 asocket *peer; 100 101 /* For local asockets, the fde is used to bind 102 ** us to our fd event system. For remote asockets 103 ** these fields are not used. 104 */ 105 fdevent fde; 106 int fd; 107 108 /* queue of apackets waiting to be written 109 */ 110 apacket *pkt_first; 111 apacket *pkt_last; 112 113 /* enqueue is called by our peer when it has data 114 ** for us. It should return 0 if we can accept more 115 ** data or 1 if not. If we return 1, we must call 116 ** peer->ready() when we once again are ready to 117 ** receive data. 118 */ 119 int (*enqueue)(asocket *s, apacket *pkt); 120 121 /* ready is called by the peer when it is ready for 122 ** us to send data via enqueue again 123 */ 124 void (*ready)(asocket *s); 125 126 /* shutdown is called by the peer before it goes away. 127 ** the socket should not do any further calls on its peer. 128 ** Always followed by a call to close. Optional, i.e. can be NULL. 129 */ 130 void (*shutdown)(asocket *s); 131 132 /* close is called by the peer when it has gone away. 133 ** we are not allowed to make any further calls on the 134 ** peer once our close method is called. 135 */ 136 void (*close)(asocket *s); 137 138 /* A socket is bound to atransport */ 139 atransport *transport; 140}; 141 142 143/* the adisconnect structure is used to record a callback that 144** will be called whenever a transport is disconnected (e.g. by the user) 145** this should be used to cleanup objects that depend on the 146** transport (e.g. remote sockets, listeners, etc...) 147*/ 148struct adisconnect 149{ 150 void (*func)(void* opaque, atransport* t); 151 void* opaque; 152 adisconnect* next; 153 adisconnect* prev; 154}; 155 156 157// A transport object models the connection to a remote device or emulator there 158// is one transport per connected device/emulator. A "local transport" connects 159// through TCP (for the emulator), while a "usb transport" through USB (for real 160// devices). 161// 162// Note that kTransportHost doesn't really correspond to a real transport 163// object, it's a special value used to indicate that a client wants to connect 164// to a service implemented within the ADB server itself. 165enum TransportType { 166 kTransportUsb, 167 kTransportLocal, 168 kTransportAny, 169 kTransportHost, 170}; 171 172#define TOKEN_SIZE 20 173 174enum ConnectionState { 175 kCsAny = -1, 176 kCsOffline = 0, 177 kCsBootloader, 178 kCsDevice, 179 kCsHost, 180 kCsRecovery, 181 kCsNoPerm, // Insufficient permissions to communicate with the device. 182 kCsSideload, 183 kCsUnauthorized, 184}; 185 186class atransport { 187public: 188 // TODO(danalbert): We expose waaaaaaay too much stuff because this was 189 // historically just a struct, but making the whole thing a more idiomatic 190 // class in one go is a very large change. Given how bad our testing is, 191 // it's better to do this piece by piece. 192 193 atransport() { 194 auth_fde = {}; 195 transport_fde = {}; 196 } 197 198 virtual ~atransport() {} 199 200 int (*read_from_remote)(apacket* p, atransport* t) = nullptr; 201 int (*write_to_remote)(apacket* p, atransport* t) = nullptr; 202 void (*close)(atransport* t) = nullptr; 203 void (*kick)(atransport* t) = nullptr; 204 205 int fd = -1; 206 int transport_socket = -1; 207 fdevent transport_fde; 208 int ref_count = 0; 209 uint32_t sync_token = 0; 210 ConnectionState connection_state = kCsOffline; 211 bool online = false; 212 TransportType type = kTransportAny; 213 214 // USB handle or socket fd as needed. 215 usb_handle* usb = nullptr; 216 int sfd = -1; 217 218 // Used to identify transports for clients. 219 char* serial = nullptr; 220 char* product = nullptr; 221 char* model = nullptr; 222 char* device = nullptr; 223 char* devpath = nullptr; 224 int adb_port = -1; // Use for emulators (local transport) 225 bool kicked = false; 226 227 // A list of adisconnect callbacks called when the transport is kicked. 228 adisconnect disconnects = {}; 229 230 void* key = nullptr; 231 unsigned char token[TOKEN_SIZE] = {}; 232 fdevent auth_fde; 233 size_t failed_auth_attempts = 0; 234 235 const char* connection_state_name() const; 236 237private: 238 DISALLOW_COPY_AND_ASSIGN(atransport); 239}; 240 241 242/* A listener is an entity which binds to a local port 243** and, upon receiving a connection on that port, creates 244** an asocket to connect the new local connection to a 245** specific remote service. 246** 247** TODO: some listeners read from the new connection to 248** determine what exact service to connect to on the far 249** side. 250*/ 251struct alistener 252{ 253 alistener *next; 254 alistener *prev; 255 256 fdevent fde; 257 int fd; 258 259 char *local_name; 260 char *connect_to; 261 atransport *transport; 262 adisconnect disconnect; 263}; 264 265 266void print_packet(const char *label, apacket *p); 267 268asocket *find_local_socket(unsigned local_id, unsigned remote_id); 269void install_local_socket(asocket *s); 270void remove_socket(asocket *s); 271void close_all_sockets(atransport *t); 272 273asocket *create_local_socket(int fd); 274asocket *create_local_service_socket(const char *destination); 275 276asocket *create_remote_socket(unsigned id, atransport *t); 277void connect_to_remote(asocket *s, const char *destination); 278void connect_to_smartsocket(asocket *s); 279 280void fatal(const char *fmt, ...); 281void fatal_errno(const char *fmt, ...); 282 283void handle_packet(apacket *p, atransport *t); 284 285void get_my_path(char *s, size_t maxLen); 286int launch_server(int server_port); 287int adb_main(int is_daemon, int server_port); 288 289/* initialize a transport object's func pointers and state */ 290#if ADB_HOST 291int get_available_local_transport_index(); 292#endif 293int init_socket_transport(atransport *t, int s, int port, int local); 294void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state); 295 296#if ADB_HOST 297atransport* find_emulator_transport_by_adb_port(int adb_port); 298#endif 299 300int service_to_fd(const char *name); 301#if ADB_HOST 302asocket *host_service_to_socket(const char* name, const char *serial); 303#endif 304 305#if !ADB_HOST 306int init_jdwp(void); 307asocket* create_jdwp_service_socket(); 308asocket* create_jdwp_tracker_service_socket(); 309int create_jdwp_connection_fd(int jdwp_pid); 310#endif 311 312int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd); 313 314#if !ADB_HOST 315void framebuffer_service(int fd, void *cookie); 316void set_verity_enabled_state_service(int fd, void* cookie); 317#endif 318 319/* packet allocator */ 320apacket *get_apacket(void); 321void put_apacket(apacket *p); 322 323// Define it if you want to dump packets. 324#define DEBUG_PACKETS 0 325 326#if !DEBUG_PACKETS 327#define print_packet(tag,p) do {} while (0) 328#endif 329 330#if ADB_HOST_ON_TARGET 331/* adb and adbd are coexisting on the target, so use 5038 for adb 332 * to avoid conflicting with adbd's usage of 5037 333 */ 334# define DEFAULT_ADB_PORT 5038 335#else 336# define DEFAULT_ADB_PORT 5037 337#endif 338 339#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 340 341#define ADB_CLASS 0xff 342#define ADB_SUBCLASS 0x42 343#define ADB_PROTOCOL 0x1 344 345 346void local_init(int port); 347int local_connect(int port); 348int local_connect_arbitrary_ports(int console_port, int adb_port); 349 350/* usb host/client interface */ 351void usb_init(); 352int usb_write(usb_handle *h, const void *data, int len); 353int usb_read(usb_handle *h, void *data, int len); 354int usb_close(usb_handle *h); 355void usb_kick(usb_handle *h); 356 357/* used for USB device detection */ 358#if ADB_HOST 359int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); 360#endif 361 362int adb_commandline(int argc, const char **argv); 363 364ConnectionState connection_state(atransport *t); 365 366extern const char *adb_device_banner; 367extern int HOST; 368extern int SHELL_EXIT_NOTIFY_FD; 369 370#define CHUNK_SIZE (64*1024) 371 372#if !ADB_HOST 373#define USB_ADB_PATH "/dev/android_adb" 374 375#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" 376#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x 377 378#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) 379#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) 380#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) 381#endif 382 383int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s); 384 385void handle_online(atransport *t); 386void handle_offline(atransport *t); 387 388void send_connect(atransport *t); 389 390#endif 391