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