blob: 5a74b7b9aab703f238573f74a3cd68419e1a6482 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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
36#define ADB_SERVER_VERSION 20 // Increment this when we want to force users to start a new adb server
37
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 /* the asocket we are connected to
83 */
84
85 asocket *peer;
86
87 /* For local asockets, the fde is used to bind
88 ** us to our fd event system. For remote asockets
89 ** these fields are not used.
90 */
91 fdevent fde;
92 int fd;
93
94 /* queue of apackets waiting to be written
95 */
96 apacket *pkt_first;
97 apacket *pkt_last;
98
99 /* enqueue is called by our peer when it has data
100 ** for us. It should return 0 if we can accept more
101 ** data or 1 if not. If we return 1, we must call
102 ** peer->ready() when we once again are ready to
103 ** receive data.
104 */
105 int (*enqueue)(asocket *s, apacket *pkt);
106
107 /* ready is called by the peer when it is ready for
108 ** us to send data via enqueue again
109 */
110 void (*ready)(asocket *s);
111
112 /* close is called by the peer when it has gone away.
113 ** we are not allowed to make any further calls on the
114 ** peer once our close method is called.
115 */
116 void (*close)(asocket *s);
117
118 /* socket-type-specific extradata */
119 void *extra;
120
121 /* A socket is bound to atransport */
122 atransport *transport;
123};
124
125
126/* the adisconnect structure is used to record a callback that
127** will be called whenever a transport is disconnected (e.g. by the user)
128** this should be used to cleanup objects that depend on the
129** transport (e.g. remote sockets, listeners, etc...)
130*/
131struct adisconnect
132{
133 void (*func)(void* opaque, atransport* t);
134 void* opaque;
135 adisconnect* next;
136 adisconnect* prev;
137};
138
139
140/* a transport object models the connection to a remote device or emulator
141** there is one transport per connected device/emulator. a "local transport"
142** connects through TCP (for the emulator), while a "usb transport" through
143** USB (for real devices)
144**
145** note that kTransportHost doesn't really correspond to a real transport
146** object, it's a special value used to indicate that a client wants to
147** connect to a service implemented within the ADB server itself.
148*/
149typedef enum transport_type {
150 kTransportUsb,
151 kTransportLocal,
152 kTransportAny,
153 kTransportHost,
154} transport_type;
155
156struct atransport
157{
158 atransport *next;
159 atransport *prev;
160
161 int (*read_from_remote)(apacket *p, atransport *t);
162 int (*write_to_remote)(apacket *p, atransport *t);
163 void (*close)(atransport *t);
164 void (*kick)(atransport *t);
165
166 int fd;
167 int transport_socket;
168 fdevent transport_fde;
169 int ref_count;
170 unsigned sync_token;
171 int connection_state;
172 transport_type type;
173
174 /* usb handle or socket fd as needed */
175 usb_handle *usb;
176 int sfd;
177
178 /* used to identify transports for clients */
179 char *serial;
180 char *product;
181
182 /* a list of adisconnect callbacks called when the transport is kicked */
183 int kicked;
184 adisconnect disconnects;
185};
186
187
188/* A listener is an entity which binds to a local port
189** and, upon receiving a connection on that port, creates
190** an asocket to connect the new local connection to a
191** specific remote service.
192**
193** TODO: some listeners read from the new connection to
194** determine what exact service to connect to on the far
195** side.
196*/
197struct alistener
198{
199 alistener *next;
200 alistener *prev;
201
202 fdevent fde;
203 int fd;
204
205 const char *local_name;
206 const char *connect_to;
207 atransport *transport;
208 adisconnect disconnect;
209};
210
211
212void print_packet(const char *label, apacket *p);
213
214asocket *find_local_socket(unsigned id);
215void install_local_socket(asocket *s);
216void remove_socket(asocket *s);
217void close_all_sockets(atransport *t);
218
219#define LOCAL_CLIENT_PREFIX "emulator-"
220
221asocket *create_local_socket(int fd);
222asocket *create_local_service_socket(const char *destination);
223
224asocket *create_remote_socket(unsigned id, atransport *t);
225void connect_to_remote(asocket *s, const char *destination);
226void connect_to_smartsocket(asocket *s);
227
228void fatal(const char *fmt, ...);
229void fatal_errno(const char *fmt, ...);
230
231void handle_packet(apacket *p, atransport *t);
232void send_packet(apacket *p, atransport *t);
233
234void get_my_path(char s[PATH_MAX]);
235int launch_server();
236int adb_main(int is_daemon);
237
238
239/* transports are ref-counted
240** get_device_transport does an acquire on your behalf before returning
241*/
242void init_transport_registration(void);
243int list_transports(char *buf, size_t bufsize);
244void update_transports(void);
245
246asocket* create_device_tracker(void);
247
248/* Obtain a transport from the available transports.
249** If state is != CS_ANY, only transports in that state are considered.
250** If serial is non-NULL then only the device with that serial will be chosen.
251** If no suitable transport is found, error is set.
252*/
253atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
254void add_transport_disconnect( atransport* t, adisconnect* dis );
255void remove_transport_disconnect( atransport* t, adisconnect* dis );
256void run_transport_disconnects( atransport* t );
257void kick_transport( atransport* t );
258
259/* initialize a transport object's func pointers and state */
260int init_socket_transport(atransport *t, int s, int port);
261void init_usb_transport(atransport *t, usb_handle *usb);
262
263/* for MacOS X cleanup */
264void close_usb_devices();
265
266/* cause new transports to be init'd and added to the list */
267void register_socket_transport(int s, const char *serial, int port);
268void register_usb_transport(usb_handle *h, const char *serial);
269
270int service_to_fd(const char *name);
271#if ADB_HOST
272asocket *host_service_to_socket(const char* name, const char *serial);
273#endif
274
275#if !ADB_HOST
276int init_jdwp(void);
277asocket* create_jdwp_service_socket();
278asocket* create_jdwp_tracker_service_socket();
279int create_jdwp_connection_fd(int jdwp_pid);
280#endif
281
282#if !ADB_HOST
283void framebuffer_service(int fd, void *cookie);
284void log_service(int fd, void *cookie);
285void remount_service(int fd, void *cookie);
286char * get_log_file_path(const char * log_name);
287#endif
288
289/* packet allocator */
290apacket *get_apacket(void);
291void put_apacket(apacket *p);
292
293int check_header(apacket *p);
294int check_data(apacket *p);
295
296/* convenience wrappers around read/write that will retry on
297** EINTR and/or short read/write. Returns 0 on success, -1
298** on error or EOF.
299*/
300int readx(int fd, void *ptr, size_t len);
301int writex(int fd, const void *ptr, size_t len);
302
303/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
304
305#define ADB_TRACE 1
306
307/* IMPORTANT: if you change the following list, don't
308 * forget to update the corresponding 'tags' table in
309 * the adb_trace_init() function implemented in adb.c
310 */
311typedef enum {
312 TRACE_ADB = 0,
313 TRACE_SOCKETS,
314 TRACE_PACKETS,
315 TRACE_TRANSPORT,
316 TRACE_RWX,
317 TRACE_USB,
318 TRACE_SYNC,
319 TRACE_SYSDEPS,
320 TRACE_JDWP,
321} AdbTrace;
322
323#if ADB_TRACE
324
325 int adb_trace_mask;
326
327 void adb_trace_init(void);
328
329# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
330
331 /* you must define TRACE_TAG before using this macro */
332 #define D(...) \
333 do { \
334 if (ADB_TRACING) \
335 fprintf(stderr, __VA_ARGS__ ); \
336 } while (0)
337#else
338# define D(...) ((void)0)
339# define ADB_TRACING 0
340#endif
341
342
343/* set this to log to /data/adb/adb_<time>.txt on the device.
344 * has no effect if the /data/adb/ directory does not exist.
345 */
346#define ADB_DEVICE_LOG 0
347
348#if !TRACE_PACKETS
349#define print_packet(tag,p) do {} while (0)
350#endif
351
352#define ADB_PORT 5037
353#define ADB_LOCAL_TRANSPORT_PORT 5555
354
355// Google's USB Vendor ID
356#define VENDOR_ID_GOOGLE 0x18d1
357// HTC's USB Vendor ID
358#define VENDOR_ID_HTC 0x0bb4
359
360// products for VENDOR_ID_GOOGLE
361#define PRODUCT_ID_SOONER 0xd00d // Sooner bootloader
362#define PRODUCT_ID_SOONER_COMP 0xdeed // Sooner composite device
363
364// products for VENDOR_ID_HTC
365#define PRODUCT_ID_DREAM 0x0c01 // Dream bootloader
366#define PRODUCT_ID_DREAM_COMP 0x0c02 // Dream composite device
367
368void local_init();
369int local_connect(int port);
370
371/* usb host/client interface */
372void usb_init();
373void usb_cleanup();
374int usb_write(usb_handle *h, const void *data, int len);
375int usb_read(usb_handle *h, void *data, int len);
376int usb_close(usb_handle *h);
377void usb_kick(usb_handle *h);
378
379/* used for USB device detection */
380int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
381
382unsigned host_to_le32(unsigned n);
383int adb_commandline(int argc, char **argv);
384
385int connection_state(atransport *t);
386
387#define CS_ANY -1
388#define CS_OFFLINE 0
389#define CS_BOOTLOADER 1
390#define CS_DEVICE 2
391#define CS_HOST 3
392#define CS_RECOVERY 4
393#define CS_ERROR 5
394
395extern int HOST;
396
397#define CHUNK_SIZE (64*1024)
398
399int sendfailmsg(int fd, const char *reason);
400int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
401
402#endif