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