blob: 7cbf3d33b7ef28d719f8ad48372b6756cbd01529 [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
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 Lockwood505bd6e2010-02-19 17:45:57 -050036#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 -080037
38typedef struct amessage amessage;
39typedef struct apacket apacket;
40typedef struct asocket asocket;
41typedef struct alistener alistener;
42typedef struct aservice aservice;
43typedef struct atransport atransport;
44typedef struct adisconnect adisconnect;
45typedef struct usb_handle usb_handle;
46
47struct 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
56struct 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*/
71struct 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
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800126 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 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*/
136struct 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*/
154typedef enum transport_type {
155 kTransportUsb,
156 kTransportLocal,
157 kTransportAny,
158 kTransportHost,
159} transport_type;
160
161struct 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;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100186 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
188 /* a list of adisconnect callbacks called when the transport is kicked */
189 int kicked;
190 adisconnect disconnects;
191};
192
193
194/* A listener is an entity which binds to a local port
195** and, upon receiving a connection on that port, creates
196** an asocket to connect the new local connection to a
197** specific remote service.
198**
199** TODO: some listeners read from the new connection to
200** determine what exact service to connect to on the far
201** side.
202*/
203struct alistener
204{
205 alistener *next;
206 alistener *prev;
207
208 fdevent fde;
209 int fd;
210
211 const char *local_name;
212 const char *connect_to;
213 atransport *transport;
214 adisconnect disconnect;
215};
216
217
218void print_packet(const char *label, apacket *p);
219
220asocket *find_local_socket(unsigned id);
221void install_local_socket(asocket *s);
222void remove_socket(asocket *s);
223void close_all_sockets(atransport *t);
224
225#define LOCAL_CLIENT_PREFIX "emulator-"
226
227asocket *create_local_socket(int fd);
228asocket *create_local_service_socket(const char *destination);
229
230asocket *create_remote_socket(unsigned id, atransport *t);
231void connect_to_remote(asocket *s, const char *destination);
232void connect_to_smartsocket(asocket *s);
233
234void fatal(const char *fmt, ...);
235void fatal_errno(const char *fmt, ...);
236
237void handle_packet(apacket *p, atransport *t);
238void send_packet(apacket *p, atransport *t);
239
Alexey Tarasov31664102009-10-22 02:55:00 +1100240void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100241int launch_server(int server_port);
242int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
244
245/* transports are ref-counted
246** get_device_transport does an acquire on your behalf before returning
247*/
248void init_transport_registration(void);
249int list_transports(char *buf, size_t bufsize);
250void update_transports(void);
251
252asocket* create_device_tracker(void);
253
254/* Obtain a transport from the available transports.
255** If state is != CS_ANY, only transports in that state are considered.
256** If serial is non-NULL then only the device with that serial will be chosen.
257** If no suitable transport is found, error is set.
258*/
259atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
260void add_transport_disconnect( atransport* t, adisconnect* dis );
261void remove_transport_disconnect( atransport* t, adisconnect* dis );
262void run_transport_disconnects( atransport* t );
263void kick_transport( atransport* t );
264
265/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100266#if ADB_HOST
267int get_available_local_transport_index();
268#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700269int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400270void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272/* for MacOS X cleanup */
273void close_usb_devices();
274
275/* cause new transports to be init'd and added to the list */
Mike Lockwood2f38b692009-08-24 15:58:40 -0700276void register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400277
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400278/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400279void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400280void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400281
Mike Lockwood95b837d2009-08-08 12:37:44 -0400282void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
283
284/* this should only be used for transports with connection_state == CS_NOPERM */
285void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400287atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100288#if ADB_HOST
289atransport* find_emulator_transport_by_adb_port(int adb_port);
290#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400291
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292int service_to_fd(const char *name);
293#if ADB_HOST
294asocket *host_service_to_socket(const char* name, const char *serial);
295#endif
296
297#if !ADB_HOST
298int init_jdwp(void);
299asocket* create_jdwp_service_socket();
300asocket* create_jdwp_tracker_service_socket();
301int create_jdwp_connection_fd(int jdwp_pid);
302#endif
303
304#if !ADB_HOST
305void framebuffer_service(int fd, void *cookie);
306void log_service(int fd, void *cookie);
307void remount_service(int fd, void *cookie);
308char * get_log_file_path(const char * log_name);
309#endif
310
311/* packet allocator */
312apacket *get_apacket(void);
313void put_apacket(apacket *p);
314
315int check_header(apacket *p);
316int check_data(apacket *p);
317
318/* convenience wrappers around read/write that will retry on
319** EINTR and/or short read/write. Returns 0 on success, -1
320** on error or EOF.
321*/
322int readx(int fd, void *ptr, size_t len);
323int writex(int fd, const void *ptr, size_t len);
324
325/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
326
327#define ADB_TRACE 1
328
329/* IMPORTANT: if you change the following list, don't
330 * forget to update the corresponding 'tags' table in
331 * the adb_trace_init() function implemented in adb.c
332 */
333typedef enum {
334 TRACE_ADB = 0,
335 TRACE_SOCKETS,
336 TRACE_PACKETS,
337 TRACE_TRANSPORT,
338 TRACE_RWX,
339 TRACE_USB,
340 TRACE_SYNC,
341 TRACE_SYSDEPS,
342 TRACE_JDWP,
343} AdbTrace;
344
345#if ADB_TRACE
346
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800347// TODO(jpa): remove this after fixing fprintf() multithreading issue {
348// Can't include sysdeps.h, because framebuffer_service.c wants a different
349// close() than what sysdeps overrides when linking adbd.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800351// Assume Linux.
352#ifndef _ADB_SYSDEPS_H
353#include <pthread.h>
354typedef pthread_mutex_t adb_mutex_t;
355#define ADB_MUTEX(x) extern adb_mutex_t x;
356#include "mutex_list.h"
357#endif
358
359// }
360
361 extern int adb_trace_mask;
362 extern unsigned char adb_trace_output_count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 void adb_trace_init(void);
364
365# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
366
367 /* you must define TRACE_TAG before using this macro */
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800368# define D(...) \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 do { \
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800370 if (ADB_TRACING) { \
371 adb_mutex_lock(&D_lock); \
372 fprintf(stderr, "%s::%s():", \
373 __FILE__, __FUNCTION__); \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 fprintf(stderr, __VA_ARGS__ ); \
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800375 fflush(stderr); \
376 adb_mutex_unlock(&D_lock); \
377 } \
378 } while (0)
379# define DR(...) \
380 do { \
381 if (ADB_TRACING) { \
382 adb_mutex_lock(&D_lock); \
383 fprintf(stderr, __VA_ARGS__ ); \
384 fflush(stderr); \
385 adb_mutex_unlock(&D_lock); \
386 } \
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 } while (0)
388#else
389# define D(...) ((void)0)
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800390# define DR(...) ((void)0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391# define ADB_TRACING 0
392#endif
393
394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395#if !TRACE_PACKETS
396#define print_packet(tag,p) do {} while (0)
397#endif
398
John Michelauc3188332010-09-23 17:08:34 -0500399#if ADB_HOST_ON_TARGET
400/* adb and adbd are coexisting on the target, so use 5038 for adb
401 * to avoid conflicting with adbd's usage of 5037
402 */
403# define DEFAULT_ADB_PORT 5038
404#else
405# define DEFAULT_ADB_PORT 5037
406#endif
407
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100408#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409
Xavier Ducroheta481d092009-05-21 17:47:43 -0700410#define ADB_CLASS 0xff
411#define ADB_SUBCLASS 0x42
412#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700413
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700415void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100417int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418
419/* usb host/client interface */
420void usb_init();
421void usb_cleanup();
422int usb_write(usb_handle *h, const void *data, int len);
423int usb_read(usb_handle *h, void *data, int len);
424int usb_close(usb_handle *h);
425void usb_kick(usb_handle *h);
426
427/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700428#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700430#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431
432unsigned host_to_le32(unsigned n);
433int adb_commandline(int argc, char **argv);
434
435int connection_state(atransport *t);
436
437#define CS_ANY -1
438#define CS_OFFLINE 0
439#define CS_BOOTLOADER 1
440#define CS_DEVICE 2
441#define CS_HOST 3
442#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400443#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444
445extern int HOST;
446
447#define CHUNK_SIZE (64*1024)
448
449int sendfailmsg(int fd, const char *reason);
450int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
451
452#endif