adb.h revision 2ca3e6b35f79136418ebc32fef57580698dbd045
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 22#include "transport.h" /* readx(), writex() */ 23 24#define MAX_PAYLOAD 4096 25 26#define A_SYNC 0x434e5953 27#define A_CNXN 0x4e584e43 28#define A_OPEN 0x4e45504f 29#define A_OKAY 0x59414b4f 30#define A_CLSE 0x45534c43 31#define A_WRTE 0x45545257 32 33#define A_VERSION 0x01000000 // ADB protocol version 34 35#define ADB_VERSION_MAJOR 1 // Used for help/version information 36#define ADB_VERSION_MINOR 0 // Used for help/version information 37 38#define ADB_SERVER_VERSION 29 // Increment this when we want to force users to start a new adb server 39 40typedef struct amessage amessage; 41typedef struct apacket apacket; 42typedef struct asocket asocket; 43typedef struct alistener alistener; 44typedef struct aservice aservice; 45typedef struct atransport atransport; 46typedef struct adisconnect adisconnect; 47typedef struct usb_handle usb_handle; 48 49struct amessage { 50 unsigned command; /* command identifier constant */ 51 unsigned arg0; /* first argument */ 52 unsigned arg1; /* second argument */ 53 unsigned data_length; /* length of payload (0 is allowed) */ 54 unsigned data_check; /* checksum of data payload */ 55 unsigned magic; /* command ^ 0xffffffff */ 56}; 57 58struct apacket 59{ 60 apacket *next; 61 62 unsigned len; 63 unsigned char *ptr; 64 65 amessage msg; 66 unsigned char data[MAX_PAYLOAD]; 67}; 68 69/* An asocket represents one half of a connection between a local and 70** remote entity. A local asocket is bound to a file descriptor. A 71** remote asocket is bound to the protocol engine. 72*/ 73struct asocket { 74 /* chain pointers for the local/remote list of 75 ** asockets that this asocket lives in 76 */ 77 asocket *next; 78 asocket *prev; 79 80 /* the unique identifier for this asocket 81 */ 82 unsigned id; 83 84 /* flag: set when the socket's peer has closed 85 ** but packets are still queued for delivery 86 */ 87 int closing; 88 89 /* the asocket we are connected to 90 */ 91 92 asocket *peer; 93 94 /* For local asockets, the fde is used to bind 95 ** us to our fd event system. For remote asockets 96 ** these fields are not used. 97 */ 98 fdevent fde; 99 int fd; 100 101 /* queue of apackets waiting to be written 102 */ 103 apacket *pkt_first; 104 apacket *pkt_last; 105 106 /* enqueue is called by our peer when it has data 107 ** for us. It should return 0 if we can accept more 108 ** data or 1 if not. If we return 1, we must call 109 ** peer->ready() when we once again are ready to 110 ** receive data. 111 */ 112 int (*enqueue)(asocket *s, apacket *pkt); 113 114 /* ready is called by the peer when it is ready for 115 ** us to send data via enqueue again 116 */ 117 void (*ready)(asocket *s); 118 119 /* close is called by the peer when it has gone away. 120 ** we are not allowed to make any further calls on the 121 ** peer once our close method is called. 122 */ 123 void (*close)(asocket *s); 124 125 /* socket-type-specific extradata */ 126 void *extra; 127 128 /* A socket is bound to atransport */ 129 atransport *transport; 130}; 131 132 133/* the adisconnect structure is used to record a callback that 134** will be called whenever a transport is disconnected (e.g. by the user) 135** this should be used to cleanup objects that depend on the 136** transport (e.g. remote sockets, listeners, etc...) 137*/ 138struct adisconnect 139{ 140 void (*func)(void* opaque, atransport* t); 141 void* opaque; 142 adisconnect* next; 143 adisconnect* prev; 144}; 145 146 147/* a transport object models the connection to a remote device or emulator 148** there is one transport per connected device/emulator. a "local transport" 149** connects through TCP (for the emulator), while a "usb transport" through 150** USB (for real devices) 151** 152** note that kTransportHost doesn't really correspond to a real transport 153** object, it's a special value used to indicate that a client wants to 154** connect to a service implemented within the ADB server itself. 155*/ 156typedef enum transport_type { 157 kTransportUsb, 158 kTransportLocal, 159 kTransportAny, 160 kTransportHost, 161} transport_type; 162 163struct atransport 164{ 165 atransport *next; 166 atransport *prev; 167 168 int (*read_from_remote)(apacket *p, atransport *t); 169 int (*write_to_remote)(apacket *p, atransport *t); 170 void (*close)(atransport *t); 171 void (*kick)(atransport *t); 172 173 int fd; 174 int transport_socket; 175 fdevent transport_fde; 176 int ref_count; 177 unsigned sync_token; 178 int connection_state; 179 transport_type type; 180 181 /* usb handle or socket fd as needed */ 182 usb_handle *usb; 183 int sfd; 184 185 /* used to identify transports for clients */ 186 char *serial; 187 char *product; 188 char *model; 189 char *device; 190 char *devpath; 191 int adb_port; // Use for emulators (local transport) 192 193 /* a list of adisconnect callbacks called when the transport is kicked */ 194 int kicked; 195 adisconnect disconnects; 196}; 197 198 199/* A listener is an entity which binds to a local port 200** and, upon receiving a connection on that port, creates 201** an asocket to connect the new local connection to a 202** specific remote service. 203** 204** TODO: some listeners read from the new connection to 205** determine what exact service to connect to on the far 206** side. 207*/ 208struct alistener 209{ 210 alistener *next; 211 alistener *prev; 212 213 fdevent fde; 214 int fd; 215 216 const char *local_name; 217 const char *connect_to; 218 atransport *transport; 219 adisconnect disconnect; 220}; 221 222 223void print_packet(const char *label, apacket *p); 224 225asocket *find_local_socket(unsigned id); 226void install_local_socket(asocket *s); 227void remove_socket(asocket *s); 228void close_all_sockets(atransport *t); 229 230#define LOCAL_CLIENT_PREFIX "emulator-" 231 232asocket *create_local_socket(int fd); 233asocket *create_local_service_socket(const char *destination); 234 235asocket *create_remote_socket(unsigned id, atransport *t); 236void connect_to_remote(asocket *s, const char *destination); 237void connect_to_smartsocket(asocket *s); 238 239void fatal(const char *fmt, ...); 240void fatal_errno(const char *fmt, ...); 241 242void handle_packet(apacket *p, atransport *t); 243void send_packet(apacket *p, atransport *t); 244 245void get_my_path(char *s, size_t maxLen); 246int launch_server(int server_port); 247int adb_main(int is_daemon, int server_port); 248 249 250/* transports are ref-counted 251** get_device_transport does an acquire on your behalf before returning 252*/ 253void init_transport_registration(void); 254int list_transports(char *buf, size_t bufsize, int long_listing); 255void update_transports(void); 256 257asocket* create_device_tracker(void); 258 259/* Obtain a transport from the available transports. 260** If state is != CS_ANY, only transports in that state are considered. 261** If serial is non-NULL then only the device with that serial will be chosen. 262** If no suitable transport is found, error is set. 263*/ 264atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out); 265void add_transport_disconnect( atransport* t, adisconnect* dis ); 266void remove_transport_disconnect( atransport* t, adisconnect* dis ); 267void run_transport_disconnects( atransport* t ); 268void kick_transport( atransport* t ); 269 270/* initialize a transport object's func pointers and state */ 271#if ADB_HOST 272int get_available_local_transport_index(); 273#endif 274int init_socket_transport(atransport *t, int s, int port, int local); 275void init_usb_transport(atransport *t, usb_handle *usb, int state); 276 277/* for MacOS X cleanup */ 278void close_usb_devices(); 279 280/* cause new transports to be init'd and added to the list */ 281void register_socket_transport(int s, const char *serial, int port, int local); 282 283/* these should only be used for the "adb disconnect" command */ 284void unregister_transport(atransport *t); 285void unregister_all_tcp_transports(); 286 287void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable); 288 289/* this should only be used for transports with connection_state == CS_NOPERM */ 290void unregister_usb_transport(usb_handle *usb); 291 292atransport *find_transport(const char *serial); 293#if ADB_HOST 294atransport* find_emulator_transport_by_adb_port(int adb_port); 295#endif 296 297int service_to_fd(const char *name); 298#if ADB_HOST 299asocket *host_service_to_socket(const char* name, const char *serial); 300#endif 301 302#if !ADB_HOST 303int init_jdwp(void); 304asocket* create_jdwp_service_socket(); 305asocket* create_jdwp_tracker_service_socket(); 306int create_jdwp_connection_fd(int jdwp_pid); 307#endif 308 309#if !ADB_HOST 310typedef enum { 311 BACKUP, 312 RESTORE 313} BackupOperation; 314int backup_service(BackupOperation operation, char* args); 315void framebuffer_service(int fd, void *cookie); 316void log_service(int fd, void *cookie); 317void remount_service(int fd, void *cookie); 318char * get_log_file_path(const char * log_name); 319#endif 320 321/* packet allocator */ 322apacket *get_apacket(void); 323void put_apacket(apacket *p); 324 325int check_header(apacket *p); 326int check_data(apacket *p); 327 328/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */ 329 330#define ADB_TRACE 1 331 332/* IMPORTANT: if you change the following list, don't 333 * forget to update the corresponding 'tags' table in 334 * the adb_trace_init() function implemented in adb.c 335 */ 336typedef enum { 337 TRACE_ADB = 0, /* 0x001 */ 338 TRACE_SOCKETS, 339 TRACE_PACKETS, 340 TRACE_TRANSPORT, 341 TRACE_RWX, /* 0x010 */ 342 TRACE_USB, 343 TRACE_SYNC, 344 TRACE_SYSDEPS, 345 TRACE_JDWP, /* 0x100 */ 346 TRACE_SERVICES, 347} AdbTrace; 348 349#if ADB_TRACE 350 351 extern int adb_trace_mask; 352 extern unsigned char adb_trace_output_count; 353 void adb_trace_init(void); 354 355# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0) 356 357 /* you must define TRACE_TAG before using this macro */ 358# define D(...) \ 359 do { \ 360 if (ADB_TRACING) { \ 361 int save_errno = errno; \ 362 adb_mutex_lock(&D_lock); \ 363 fprintf(stderr, "%s::%s():", \ 364 __FILE__, __FUNCTION__); \ 365 errno = save_errno; \ 366 fprintf(stderr, __VA_ARGS__ ); \ 367 fflush(stderr); \ 368 adb_mutex_unlock(&D_lock); \ 369 errno = save_errno; \ 370 } \ 371 } while (0) 372# define DR(...) \ 373 do { \ 374 if (ADB_TRACING) { \ 375 int save_errno = errno; \ 376 adb_mutex_lock(&D_lock); \ 377 errno = save_errno; \ 378 fprintf(stderr, __VA_ARGS__ ); \ 379 fflush(stderr); \ 380 adb_mutex_unlock(&D_lock); \ 381 errno = save_errno; \ 382 } \ 383 } while (0) 384#else 385# define D(...) ((void)0) 386# define DR(...) ((void)0) 387# define ADB_TRACING 0 388#endif 389 390 391#if !TRACE_PACKETS 392#define print_packet(tag,p) do {} while (0) 393#endif 394 395#if ADB_HOST_ON_TARGET 396/* adb and adbd are coexisting on the target, so use 5038 for adb 397 * to avoid conflicting with adbd's usage of 5037 398 */ 399# define DEFAULT_ADB_PORT 5038 400#else 401# define DEFAULT_ADB_PORT 5037 402#endif 403 404#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 405 406#define ADB_CLASS 0xff 407#define ADB_SUBCLASS 0x42 408#define ADB_PROTOCOL 0x1 409 410 411void local_init(int port); 412int local_connect(int port); 413int local_connect_arbitrary_ports(int console_port, int adb_port); 414 415/* usb host/client interface */ 416void usb_init(); 417void usb_cleanup(); 418int usb_write(usb_handle *h, const void *data, int len); 419int usb_read(usb_handle *h, void *data, int len); 420int usb_close(usb_handle *h); 421void usb_kick(usb_handle *h); 422 423/* used for USB device detection */ 424#if ADB_HOST 425int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); 426#endif 427 428unsigned host_to_le32(unsigned n); 429int adb_commandline(int argc, char **argv); 430 431int connection_state(atransport *t); 432 433#define CS_ANY -1 434#define CS_OFFLINE 0 435#define CS_BOOTLOADER 1 436#define CS_DEVICE 2 437#define CS_HOST 3 438#define CS_RECOVERY 4 439#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */ 440 441extern int HOST; 442extern int SHELL_EXIT_NOTIFY_FD; 443 444#define CHUNK_SIZE (64*1024) 445 446int sendfailmsg(int fd, const char *reason); 447int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); 448 449#endif 450