blob: 5e9a0fb323d0ce5eb74939185e46ec4f7cd17307 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
JP Abgrall408fa572011-03-16 15:57:42 -070022#include "transport.h" /* readx(), writex() */
23
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#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
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070032#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
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
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070039#define ADB_SERVER_VERSION 30 // Increment this when we want to force users to start a new adb server
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41typedef struct amessage amessage;
42typedef struct apacket apacket;
43typedef struct asocket asocket;
44typedef struct alistener alistener;
45typedef struct aservice aservice;
46typedef struct atransport atransport;
47typedef struct adisconnect adisconnect;
48typedef struct usb_handle usb_handle;
49
50struct 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
59struct 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*/
74struct 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
Benoit Gobyf366b362012-03-16 14:50:07 -070090 /* flag: quit adbd when both ends close the
91 ** local service socket
92 */
93 int exit_on_close;
94
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095 /* 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
JP Abgrall0e7c4272011-02-23 18:44:39 -0800134 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 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
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700169#define TOKEN_SIZE 20
170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171struct atransport
172{
173 atransport *next;
174 atransport *prev;
175
176 int (*read_from_remote)(apacket *p, atransport *t);
177 int (*write_to_remote)(apacket *p, atransport *t);
178 void (*close)(atransport *t);
179 void (*kick)(atransport *t);
180
181 int fd;
182 int transport_socket;
183 fdevent transport_fde;
184 int ref_count;
185 unsigned sync_token;
186 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700187 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 transport_type type;
189
190 /* usb handle or socket fd as needed */
191 usb_handle *usb;
192 int sfd;
193
194 /* used to identify transports for clients */
195 char *serial;
196 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700197 char *model;
198 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700199 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100200 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
202 /* a list of adisconnect callbacks called when the transport is kicked */
203 int kicked;
204 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700205
206 void *key;
207 unsigned char token[TOKEN_SIZE];
208 fdevent auth_fde;
209 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210};
211
212
213/* A listener is an entity which binds to a local port
214** and, upon receiving a connection on that port, creates
215** an asocket to connect the new local connection to a
216** specific remote service.
217**
218** TODO: some listeners read from the new connection to
219** determine what exact service to connect to on the far
220** side.
221*/
222struct alistener
223{
224 alistener *next;
225 alistener *prev;
226
227 fdevent fde;
228 int fd;
229
230 const char *local_name;
231 const char *connect_to;
232 atransport *transport;
233 adisconnect disconnect;
234};
235
236
237void print_packet(const char *label, apacket *p);
238
239asocket *find_local_socket(unsigned id);
240void install_local_socket(asocket *s);
241void remove_socket(asocket *s);
242void close_all_sockets(atransport *t);
243
244#define LOCAL_CLIENT_PREFIX "emulator-"
245
246asocket *create_local_socket(int fd);
247asocket *create_local_service_socket(const char *destination);
248
249asocket *create_remote_socket(unsigned id, atransport *t);
250void connect_to_remote(asocket *s, const char *destination);
251void connect_to_smartsocket(asocket *s);
252
253void fatal(const char *fmt, ...);
254void fatal_errno(const char *fmt, ...);
255
256void handle_packet(apacket *p, atransport *t);
257void send_packet(apacket *p, atransport *t);
258
Alexey Tarasov31664102009-10-22 02:55:00 +1100259void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100260int launch_server(int server_port);
261int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
263
264/* transports are ref-counted
265** get_device_transport does an acquire on your behalf before returning
266*/
267void init_transport_registration(void);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700268int list_transports(char *buf, size_t bufsize, int long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269void update_transports(void);
270
271asocket* create_device_tracker(void);
272
273/* Obtain a transport from the available transports.
274** If state is != CS_ANY, only transports in that state are considered.
275** If serial is non-NULL then only the device with that serial will be chosen.
276** If no suitable transport is found, error is set.
277*/
278atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
279void add_transport_disconnect( atransport* t, adisconnect* dis );
280void remove_transport_disconnect( atransport* t, adisconnect* dis );
281void run_transport_disconnects( atransport* t );
282void kick_transport( atransport* t );
283
284/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100285#if ADB_HOST
286int get_available_local_transport_index();
287#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700288int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400289void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290
291/* for MacOS X cleanup */
292void close_usb_devices();
293
294/* cause new transports to be init'd and added to the list */
Mike Lockwood2f38b692009-08-24 15:58:40 -0700295void register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400296
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400297/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400298void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400299void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400300
Scott Andersone109d262012-04-20 11:21:14 -0700301void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400302
303/* this should only be used for transports with connection_state == CS_NOPERM */
304void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400306atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100307#if ADB_HOST
308atransport* find_emulator_transport_by_adb_port(int adb_port);
309#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400310
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311int service_to_fd(const char *name);
312#if ADB_HOST
313asocket *host_service_to_socket(const char* name, const char *serial);
314#endif
315
316#if !ADB_HOST
317int init_jdwp(void);
318asocket* create_jdwp_service_socket();
319asocket* create_jdwp_tracker_service_socket();
320int create_jdwp_connection_fd(int jdwp_pid);
321#endif
322
323#if !ADB_HOST
Christopher Tate702967a2011-05-17 15:52:54 -0700324typedef enum {
325 BACKUP,
326 RESTORE
327} BackupOperation;
328int backup_service(BackupOperation operation, char* args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329void framebuffer_service(int fd, void *cookie);
330void log_service(int fd, void *cookie);
331void remount_service(int fd, void *cookie);
332char * get_log_file_path(const char * log_name);
333#endif
334
335/* packet allocator */
336apacket *get_apacket(void);
337void put_apacket(apacket *p);
338
339int check_header(apacket *p);
340int check_data(apacket *p);
341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
343
344#define ADB_TRACE 1
345
346/* IMPORTANT: if you change the following list, don't
347 * forget to update the corresponding 'tags' table in
348 * the adb_trace_init() function implemented in adb.c
349 */
350typedef enum {
JP Abgrall408fa572011-03-16 15:57:42 -0700351 TRACE_ADB = 0, /* 0x001 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 TRACE_SOCKETS,
353 TRACE_PACKETS,
354 TRACE_TRANSPORT,
JP Abgrall408fa572011-03-16 15:57:42 -0700355 TRACE_RWX, /* 0x010 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 TRACE_USB,
357 TRACE_SYNC,
358 TRACE_SYSDEPS,
JP Abgrall408fa572011-03-16 15:57:42 -0700359 TRACE_JDWP, /* 0x100 */
360 TRACE_SERVICES,
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700361 TRACE_AUTH,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362} AdbTrace;
363
364#if ADB_TRACE
365
Vladimir Chtchetkine28781b02012-02-27 10:41:53 -0800366#if !ADB_HOST
367/*
368 * When running inside the emulator, guest's adbd can connect to 'adb-debug'
369 * qemud service that can display adb trace messages (on condition that emulator
370 * has been started with '-debug adb' option).
371 */
372
373/* Delivers a trace message to the emulator via QEMU pipe. */
374void adb_qemu_trace(const char* fmt, ...);
375/* Macro to use to send ADB trace messages to the emulator. */
376#define DQ(...) adb_qemu_trace(__VA_ARGS__)
377#else
378#define DQ(...) ((void)0)
379#endif /* !ADB_HOST */
380
JP Abgrall408fa572011-03-16 15:57:42 -0700381 extern int adb_trace_mask;
382 extern unsigned char adb_trace_output_count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 void adb_trace_init(void);
384
385# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
386
387 /* you must define TRACE_TAG before using this macro */
JP Abgrall408fa572011-03-16 15:57:42 -0700388# define D(...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 do { \
JP Abgrall408fa572011-03-16 15:57:42 -0700390 if (ADB_TRACING) { \
391 int save_errno = errno; \
392 adb_mutex_lock(&D_lock); \
393 fprintf(stderr, "%s::%s():", \
394 __FILE__, __FUNCTION__); \
395 errno = save_errno; \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 fprintf(stderr, __VA_ARGS__ ); \
JP Abgrall408fa572011-03-16 15:57:42 -0700397 fflush(stderr); \
398 adb_mutex_unlock(&D_lock); \
399 errno = save_errno; \
400 } \
401 } while (0)
402# define DR(...) \
403 do { \
404 if (ADB_TRACING) { \
405 int save_errno = errno; \
406 adb_mutex_lock(&D_lock); \
407 errno = save_errno; \
408 fprintf(stderr, __VA_ARGS__ ); \
409 fflush(stderr); \
410 adb_mutex_unlock(&D_lock); \
411 errno = save_errno; \
412 } \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 } while (0)
414#else
415# define D(...) ((void)0)
JP Abgrall408fa572011-03-16 15:57:42 -0700416# define DR(...) ((void)0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417# define ADB_TRACING 0
418#endif
419
420
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700421#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422#define print_packet(tag,p) do {} while (0)
423#endif
424
John Michelauc3188332010-09-23 17:08:34 -0500425#if ADB_HOST_ON_TARGET
426/* adb and adbd are coexisting on the target, so use 5038 for adb
427 * to avoid conflicting with adbd's usage of 5037
428 */
429# define DEFAULT_ADB_PORT 5038
430#else
431# define DEFAULT_ADB_PORT 5037
432#endif
433
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100434#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
Xavier Ducroheta481d092009-05-21 17:47:43 -0700436#define ADB_CLASS 0xff
437#define ADB_SUBCLASS 0x42
438#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700441void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100443int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444
445/* usb host/client interface */
446void usb_init();
447void usb_cleanup();
448int usb_write(usb_handle *h, const void *data, int len);
449int usb_read(usb_handle *h, void *data, int len);
450int usb_close(usb_handle *h);
451void usb_kick(usb_handle *h);
452
453/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700454#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700456#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457
458unsigned host_to_le32(unsigned n);
459int adb_commandline(int argc, char **argv);
460
461int connection_state(atransport *t);
462
463#define CS_ANY -1
464#define CS_OFFLINE 0
465#define CS_BOOTLOADER 1
466#define CS_DEVICE 2
467#define CS_HOST 3
468#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400469#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800470#define CS_SIDELOAD 6
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
472extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700473extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474
475#define CHUNK_SIZE (64*1024)
476
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100477#if !ADB_HOST
478#define USB_ADB_PATH "/dev/android_adb"
479
480#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
481#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
482
483#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
484#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
485#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
486#endif
487
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488int sendfailmsg(int fd, const char *reason);
489int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
490
491#endif