blob: 2908f1e5a4e4506bbf8d1cf130f764257b705530 [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
32
33#define A_VERSION 0x01000000 // ADB protocol version
34
35#define ADB_VERSION_MAJOR 1 // Used for help/version information
36#define ADB_VERSION_MINOR 0 // Used for help/version information
37
Mike Lockwood505bd6e2010-02-19 17:45:57 -050038#define ADB_SERVER_VERSION 26 // Increment this when we want to force users to start a new adb server
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40typedef struct amessage amessage;
41typedef struct apacket apacket;
42typedef struct asocket asocket;
43typedef struct alistener alistener;
44typedef struct aservice aservice;
45typedef struct atransport atransport;
46typedef struct adisconnect adisconnect;
47typedef struct usb_handle usb_handle;
48
49struct amessage {
50 unsigned command; /* command identifier constant */
51 unsigned arg0; /* first argument */
52 unsigned arg1; /* second argument */
53 unsigned data_length; /* length of payload (0 is allowed) */
54 unsigned data_check; /* checksum of data payload */
55 unsigned magic; /* command ^ 0xffffffff */
56};
57
58struct apacket
59{
60 apacket *next;
61
62 unsigned len;
63 unsigned char *ptr;
64
65 amessage msg;
66 unsigned char data[MAX_PAYLOAD];
67};
68
69/* An asocket represents one half of a connection between a local and
70** remote entity. A local asocket is bound to a file descriptor. A
71** remote asocket is bound to the protocol engine.
72*/
73struct asocket {
74 /* chain pointers for the local/remote list of
75 ** asockets that this asocket lives in
76 */
77 asocket *next;
78 asocket *prev;
79
80 /* the unique identifier for this asocket
81 */
82 unsigned id;
83
84 /* flag: set when the socket's peer has closed
85 ** but packets are still queued for delivery
86 */
87 int closing;
88
89 /* the asocket we are connected to
90 */
91
92 asocket *peer;
93
94 /* For local asockets, the fde is used to bind
95 ** us to our fd event system. For remote asockets
96 ** these fields are not used.
97 */
98 fdevent fde;
99 int fd;
100
101 /* queue of apackets waiting to be written
102 */
103 apacket *pkt_first;
104 apacket *pkt_last;
105
106 /* enqueue is called by our peer when it has data
107 ** for us. It should return 0 if we can accept more
108 ** data or 1 if not. If we return 1, we must call
109 ** peer->ready() when we once again are ready to
110 ** receive data.
111 */
112 int (*enqueue)(asocket *s, apacket *pkt);
113
114 /* ready is called by the peer when it is ready for
115 ** us to send data via enqueue again
116 */
117 void (*ready)(asocket *s);
118
119 /* close is called by the peer when it has gone away.
120 ** we are not allowed to make any further calls on the
121 ** peer once our close method is called.
122 */
123 void (*close)(asocket *s);
124
125 /* socket-type-specific extradata */
126 void *extra;
127
JP Abgrall0e7c4272011-02-23 18:44:39 -0800128 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 atransport *transport;
130};
131
132
133/* the adisconnect structure is used to record a callback that
134** will be called whenever a transport is disconnected (e.g. by the user)
135** this should be used to cleanup objects that depend on the
136** transport (e.g. remote sockets, listeners, etc...)
137*/
138struct adisconnect
139{
140 void (*func)(void* opaque, atransport* t);
141 void* opaque;
142 adisconnect* next;
143 adisconnect* prev;
144};
145
146
147/* a transport object models the connection to a remote device or emulator
148** there is one transport per connected device/emulator. a "local transport"
149** connects through TCP (for the emulator), while a "usb transport" through
150** USB (for real devices)
151**
152** note that kTransportHost doesn't really correspond to a real transport
153** object, it's a special value used to indicate that a client wants to
154** connect to a service implemented within the ADB server itself.
155*/
156typedef enum transport_type {
157 kTransportUsb,
158 kTransportLocal,
159 kTransportAny,
160 kTransportHost,
161} transport_type;
162
163struct atransport
164{
165 atransport *next;
166 atransport *prev;
167
168 int (*read_from_remote)(apacket *p, atransport *t);
169 int (*write_to_remote)(apacket *p, atransport *t);
170 void (*close)(atransport *t);
171 void (*kick)(atransport *t);
172
173 int fd;
174 int transport_socket;
175 fdevent transport_fde;
176 int ref_count;
177 unsigned sync_token;
178 int connection_state;
179 transport_type type;
180
181 /* usb handle or socket fd as needed */
182 usb_handle *usb;
183 int sfd;
184
185 /* used to identify transports for clients */
186 char *serial;
187 char *product;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100188 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
190 /* a list of adisconnect callbacks called when the transport is kicked */
191 int kicked;
192 adisconnect disconnects;
193};
194
195
196/* A listener is an entity which binds to a local port
197** and, upon receiving a connection on that port, creates
198** an asocket to connect the new local connection to a
199** specific remote service.
200**
201** TODO: some listeners read from the new connection to
202** determine what exact service to connect to on the far
203** side.
204*/
205struct alistener
206{
207 alistener *next;
208 alistener *prev;
209
210 fdevent fde;
211 int fd;
212
213 const char *local_name;
214 const char *connect_to;
215 atransport *transport;
216 adisconnect disconnect;
217};
218
219
220void print_packet(const char *label, apacket *p);
221
222asocket *find_local_socket(unsigned id);
223void install_local_socket(asocket *s);
224void remove_socket(asocket *s);
225void close_all_sockets(atransport *t);
226
227#define LOCAL_CLIENT_PREFIX "emulator-"
228
229asocket *create_local_socket(int fd);
230asocket *create_local_service_socket(const char *destination);
231
232asocket *create_remote_socket(unsigned id, atransport *t);
233void connect_to_remote(asocket *s, const char *destination);
234void connect_to_smartsocket(asocket *s);
235
236void fatal(const char *fmt, ...);
237void fatal_errno(const char *fmt, ...);
238
239void handle_packet(apacket *p, atransport *t);
240void send_packet(apacket *p, atransport *t);
241
Alexey Tarasov31664102009-10-22 02:55:00 +1100242void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100243int launch_server(int server_port);
244int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245
246
247/* transports are ref-counted
248** get_device_transport does an acquire on your behalf before returning
249*/
250void init_transport_registration(void);
251int list_transports(char *buf, size_t bufsize);
252void update_transports(void);
253
254asocket* create_device_tracker(void);
255
256/* Obtain a transport from the available transports.
257** If state is != CS_ANY, only transports in that state are considered.
258** If serial is non-NULL then only the device with that serial will be chosen.
259** If no suitable transport is found, error is set.
260*/
261atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
262void add_transport_disconnect( atransport* t, adisconnect* dis );
263void remove_transport_disconnect( atransport* t, adisconnect* dis );
264void run_transport_disconnects( atransport* t );
265void kick_transport( atransport* t );
266
267/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100268#if ADB_HOST
269int get_available_local_transport_index();
270#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700271int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400272void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
274/* for MacOS X cleanup */
275void close_usb_devices();
276
277/* cause new transports to be init'd and added to the list */
Mike Lockwood2f38b692009-08-24 15:58:40 -0700278void register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400279
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400280/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400281void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400282void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400283
Mike Lockwood95b837d2009-08-08 12:37:44 -0400284void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
285
286/* this should only be used for transports with connection_state == CS_NOPERM */
287void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400289atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100290#if ADB_HOST
291atransport* find_emulator_transport_by_adb_port(int adb_port);
292#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400293
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294int service_to_fd(const char *name);
295#if ADB_HOST
296asocket *host_service_to_socket(const char* name, const char *serial);
297#endif
298
299#if !ADB_HOST
300int init_jdwp(void);
301asocket* create_jdwp_service_socket();
302asocket* create_jdwp_tracker_service_socket();
303int create_jdwp_connection_fd(int jdwp_pid);
304#endif
305
306#if !ADB_HOST
307void framebuffer_service(int fd, void *cookie);
308void log_service(int fd, void *cookie);
309void remount_service(int fd, void *cookie);
310char * get_log_file_path(const char * log_name);
311#endif
312
313/* packet allocator */
314apacket *get_apacket(void);
315void put_apacket(apacket *p);
316
317int check_header(apacket *p);
318int check_data(apacket *p);
319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
321
322#define ADB_TRACE 1
323
324/* IMPORTANT: if you change the following list, don't
325 * forget to update the corresponding 'tags' table in
326 * the adb_trace_init() function implemented in adb.c
327 */
328typedef enum {
JP Abgrall408fa572011-03-16 15:57:42 -0700329 TRACE_ADB = 0, /* 0x001 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 TRACE_SOCKETS,
331 TRACE_PACKETS,
332 TRACE_TRANSPORT,
JP Abgrall408fa572011-03-16 15:57:42 -0700333 TRACE_RWX, /* 0x010 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 TRACE_USB,
335 TRACE_SYNC,
336 TRACE_SYSDEPS,
JP Abgrall408fa572011-03-16 15:57:42 -0700337 TRACE_JDWP, /* 0x100 */
338 TRACE_SERVICES,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339} AdbTrace;
340
341#if ADB_TRACE
342
JP Abgrall408fa572011-03-16 15:57:42 -0700343 extern int adb_trace_mask;
344 extern unsigned char adb_trace_output_count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 void adb_trace_init(void);
346
347# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
348
349 /* you must define TRACE_TAG before using this macro */
JP Abgrall408fa572011-03-16 15:57:42 -0700350# define D(...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 do { \
JP Abgrall408fa572011-03-16 15:57:42 -0700352 if (ADB_TRACING) { \
353 int save_errno = errno; \
354 adb_mutex_lock(&D_lock); \
355 fprintf(stderr, "%s::%s():", \
356 __FILE__, __FUNCTION__); \
357 errno = save_errno; \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 fprintf(stderr, __VA_ARGS__ ); \
JP Abgrall408fa572011-03-16 15:57:42 -0700359 fflush(stderr); \
360 adb_mutex_unlock(&D_lock); \
361 errno = save_errno; \
362 } \
363 } while (0)
364# define DR(...) \
365 do { \
366 if (ADB_TRACING) { \
367 int save_errno = errno; \
368 adb_mutex_lock(&D_lock); \
369 errno = save_errno; \
370 fprintf(stderr, __VA_ARGS__ ); \
371 fflush(stderr); \
372 adb_mutex_unlock(&D_lock); \
373 errno = save_errno; \
374 } \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 } while (0)
376#else
377# define D(...) ((void)0)
JP Abgrall408fa572011-03-16 15:57:42 -0700378# define DR(...) ((void)0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379# define ADB_TRACING 0
380#endif
381
382
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383#if !TRACE_PACKETS
384#define print_packet(tag,p) do {} while (0)
385#endif
386
John Michelauc3188332010-09-23 17:08:34 -0500387#if ADB_HOST_ON_TARGET
388/* adb and adbd are coexisting on the target, so use 5038 for adb
389 * to avoid conflicting with adbd's usage of 5037
390 */
391# define DEFAULT_ADB_PORT 5038
392#else
393# define DEFAULT_ADB_PORT 5037
394#endif
395
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100396#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
Xavier Ducroheta481d092009-05-21 17:47:43 -0700398#define ADB_CLASS 0xff
399#define ADB_SUBCLASS 0x42
400#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700403void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100405int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406
407/* usb host/client interface */
408void usb_init();
409void usb_cleanup();
410int usb_write(usb_handle *h, const void *data, int len);
411int usb_read(usb_handle *h, void *data, int len);
412int usb_close(usb_handle *h);
413void usb_kick(usb_handle *h);
414
415/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700416#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700418#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419
420unsigned host_to_le32(unsigned n);
421int adb_commandline(int argc, char **argv);
422
423int connection_state(atransport *t);
424
425#define CS_ANY -1
426#define CS_OFFLINE 0
427#define CS_BOOTLOADER 1
428#define CS_DEVICE 2
429#define CS_HOST 3
430#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400431#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
433extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700434extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
436#define CHUNK_SIZE (64*1024)
437
438int sendfailmsg(int fd, const char *reason);
439int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
440
441#endif