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 | |
| 260 | void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable); |
| 261 | |
| 262 | /* this should only be used for transports with connection_state == CS_NOPERM */ |
| 263 | void unregister_usb_transport(usb_handle *usb); |
| 264 | |
| 265 | atransport *find_transport(const char *serial); |
| 266 | #if ADB_HOST |
| 267 | atransport* find_emulator_transport_by_adb_port(int adb_port); |
| 268 | #endif |
| 269 | |
| 270 | int service_to_fd(const char *name); |
| 271 | #if ADB_HOST |
| 272 | asocket *host_service_to_socket(const char* name, const char *serial); |
| 273 | #endif |
| 274 | |
| 275 | #if !ADB_HOST |
| 276 | typedef enum { |
| 277 | BACKUP, |
| 278 | RESTORE |
| 279 | } BackupOperation; |
| 280 | int backup_service(BackupOperation operation, char* args); |
| 281 | void framebuffer_service(int fd, void *cookie); |
| 282 | void log_service(int fd, void *cookie); |
| 283 | void remount_service(int fd, void *cookie); |
| 284 | char * get_log_file_path(const char * log_name); |
| 285 | #endif |
| 286 | |
| 287 | /* packet allocator */ |
| 288 | apacket *get_apacket(void); |
| 289 | void put_apacket(apacket *p); |
| 290 | |
| 291 | int check_header(apacket *p); |
| 292 | int check_data(apacket *p); |
| 293 | |
| 294 | /* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */ |
| 295 | |
| 296 | #define ADB_TRACE 1 |
| 297 | |
| 298 | /* IMPORTANT: if you change the following list, don't |
| 299 | * forget to update the corresponding 'tags' table in |
| 300 | * the adb_trace_init() function implemented in adb.c |
| 301 | */ |
| 302 | typedef enum { |
| 303 | TRACE_ADB = 0, /* 0x001 */ |
| 304 | TRACE_SOCKETS, |
| 305 | TRACE_PACKETS, |
| 306 | TRACE_TRANSPORT, |
| 307 | TRACE_RWX, /* 0x010 */ |
| 308 | TRACE_USB, |
| 309 | TRACE_SYNC, |
| 310 | TRACE_SYSDEPS, |
| 311 | TRACE_JDWP, /* 0x100 */ |
| 312 | TRACE_SERVICES, |
| 313 | } AdbTrace; |
| 314 | |
| 315 | #if ADB_TRACE |
| 316 | |
| 317 | extern int adb_trace_mask; |
| 318 | extern unsigned char adb_trace_output_count; |
| 319 | void adb_trace_init(void); |
| 320 | |
| 321 | # define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0) |
| 322 | |
| 323 | /* you must define TRACE_TAG before using this macro */ |
| 324 | # define D(...) \ |
| 325 | do { \ |
| 326 | if (ADB_TRACING) { \ |
| 327 | int save_errno = errno; \ |
| 328 | adb_mutex_lock(&D_lock); \ |
| 329 | fprintf(stderr, "%s::%s():", \ |
| 330 | __FILE__, __FUNCTION__); \ |
| 331 | errno = save_errno; \ |
| 332 | fprintf(stderr, __VA_ARGS__ ); \ |
| 333 | fflush(stderr); \ |
| 334 | adb_mutex_unlock(&D_lock); \ |
| 335 | errno = save_errno; \ |
| 336 | } \ |
| 337 | } while (0) |
| 338 | # define DR(...) \ |
| 339 | do { \ |
| 340 | if (ADB_TRACING) { \ |
| 341 | int save_errno = errno; \ |
| 342 | adb_mutex_lock(&D_lock); \ |
| 343 | errno = save_errno; \ |
| 344 | fprintf(stderr, __VA_ARGS__ ); \ |
| 345 | fflush(stderr); \ |
| 346 | adb_mutex_unlock(&D_lock); \ |
| 347 | errno = save_errno; \ |
| 348 | } \ |
| 349 | } while (0) |
| 350 | #else |
| 351 | # define D(...) ((void)0) |
| 352 | # define DR(...) ((void)0) |
| 353 | # define ADB_TRACING 0 |
| 354 | #endif |
| 355 | |
| 356 | |
| 357 | #if !TRACE_PACKETS |
| 358 | #define print_packet(tag,p) do {} while (0) |
| 359 | #endif |
| 360 | |
| 361 | #if ADB_HOST_ON_TARGET |
| 362 | /* adb and adbd are coexisting on the target, so use 5038 for adb |
| 363 | * to avoid conflicting with adbd's usage of 5037 |
| 364 | */ |
| 365 | # define DEFAULT_ADB_PORT 5038 |
| 366 | #else |
| 367 | # define DEFAULT_ADB_PORT 5037 |
| 368 | #endif |
| 369 | |
| 370 | #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 |
| 371 | |
| 372 | #define ADB_CLASS 0xff |
| 373 | #define ADB_SUBCLASS 0x42 |
| 374 | #define ADB_PROTOCOL 0x1 |
| 375 | |
| 376 | |
| 377 | void local_init(int port); |
| 378 | int local_connect(int port); |
| 379 | int local_connect_arbitrary_ports(int console_port, int adb_port); |
| 380 | |
| 381 | /* usb host/client interface */ |
| 382 | void usb_init(); |
| 383 | void usb_cleanup(); |
| 384 | int usb_write(usb_handle *h, const void *data, int len); |
| 385 | int usb_read(usb_handle *h, void *data, int len); |
| 386 | int usb_close(usb_handle *h); |
| 387 | void usb_kick(usb_handle *h); |
| 388 | |
| 389 | /* used for USB device detection */ |
| 390 | #if ADB_HOST |
| 391 | int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); |
| 392 | #endif |
| 393 | |
| 394 | unsigned host_to_le32(unsigned n); |
| 395 | int adb_commandline(int argc, char **argv); |
| 396 | |
| 397 | int connection_state(atransport *t); |
| 398 | |
| 399 | #define CS_ANY -1 |
| 400 | #define CS_OFFLINE 0 |
| 401 | #define CS_BOOTLOADER 1 |
| 402 | #define CS_DEVICE 2 |
| 403 | #define CS_HOST 3 |
| 404 | #define CS_RECOVERY 4 |
| 405 | #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */ |
| 406 | #define CS_SIDELOAD 6 |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 407 | #define CS_UNAUTHORIZED 7 |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 408 | |
| 409 | extern int HOST; |
| 410 | extern int SHELL_EXIT_NOTIFY_FD; |
| 411 | |
| 412 | #define CHUNK_SIZE (64*1024) |
| 413 | |
Da Zhou | 65ad928 | 2014-01-17 01:04:07 -0800 | [diff] [blame] | 414 | #if !ADB_HOST |
| 415 | #define USB_ADB_PATH "/dev/android_adb" |
| 416 | |
| 417 | #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" |
| 418 | #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x |
| 419 | |
| 420 | #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) |
| 421 | #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) |
| 422 | #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) |
| 423 | #endif |
| 424 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 425 | int sendfailmsg(int fd, const char *reason); |
| 426 | int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); |
| 427 | |
Dan Albert | d808d21 | 2015-02-18 10:21:54 -0800 | [diff] [blame] | 428 | #ifdef __cplusplus |
| 429 | } |
| 430 | #endif |
| 431 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 432 | #endif |