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