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