blob: 08ee989d6f47e4a94b411e35b85683658af23a2a [file] [log] [blame]
Doug Zongker9270a202012-01-09 15:16:13 -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#include "transport.h" /* readx(), writex() */
23#include "fdevent.h"
24
25#define MAX_PAYLOAD 4096
26
27#define A_SYNC 0x434e5953
28#define A_CNXN 0x4e584e43
29#define A_OPEN 0x4e45504f
30#define A_OKAY 0x59414b4f
31#define A_CLSE 0x45534c43
32#define A_WRTE 0x45545257
33
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
39#define ADB_SERVER_VERSION 29 // Increment this when we want to force users to start a new adb server
40
41typedef struct amessage amessage;
42typedef struct apacket apacket;
43typedef struct asocket asocket;
Doug Zongker9270a202012-01-09 15:16:13 -080044typedef 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
128 /* A socket is bound to atransport */
129 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
Doug Zongker098d1f62012-03-20 09:40:04 -0700136** transport (e.g. remote sockets, etc...)
Doug Zongker9270a202012-01-09 15:16:13 -0800137*/
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;
188 int adb_port; // Use for emulators (local transport)
189
190 /* a list of adisconnect callbacks called when the transport is kicked */
191 int kicked;
192 adisconnect disconnects;
193};
194
195
Doug Zongker9270a202012-01-09 15:16:13 -0800196void print_packet(const char *label, apacket *p);
197
198asocket *find_local_socket(unsigned id);
199void install_local_socket(asocket *s);
200void remove_socket(asocket *s);
201void close_all_sockets(atransport *t);
202
203#define LOCAL_CLIENT_PREFIX "emulator-"
204
205asocket *create_local_socket(int fd);
206asocket *create_local_service_socket(const char *destination);
207
208asocket *create_remote_socket(unsigned id, atransport *t);
209void connect_to_remote(asocket *s, const char *destination);
210void connect_to_smartsocket(asocket *s);
211
212void fatal(const char *fmt, ...);
213void fatal_errno(const char *fmt, ...);
214
215void handle_packet(apacket *p, atransport *t);
216void send_packet(apacket *p, atransport *t);
217
218void get_my_path(char *s, size_t maxLen);
219int launch_server(int server_port);
Dees_Troy9a4b5692012-09-19 15:09:45 -0400220int adb_main(const char* path);
Doug Zongker9270a202012-01-09 15:16:13 -0800221
222
223/* transports are ref-counted
224** get_device_transport does an acquire on your behalf before returning
225*/
226void init_transport_registration(void);
227int list_transports(char *buf, size_t bufsize);
228void update_transports(void);
229
230asocket* create_device_tracker(void);
231
232/* Obtain a transport from the available transports.
233** If state is != CS_ANY, only transports in that state are considered.
234** If serial is non-NULL then only the device with that serial will be chosen.
235** If no suitable transport is found, error is set.
236*/
237atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
238void add_transport_disconnect( atransport* t, adisconnect* dis );
239void remove_transport_disconnect( atransport* t, adisconnect* dis );
240void run_transport_disconnects( atransport* t );
241void kick_transport( atransport* t );
242
243/* initialize a transport object's func pointers and state */
244#if ADB_HOST
245int get_available_local_transport_index();
246#endif
Doug Zongker9270a202012-01-09 15:16:13 -0800247void init_usb_transport(atransport *t, usb_handle *usb, int state);
248
249/* for MacOS X cleanup */
250void close_usb_devices();
251
Doug Zongker9270a202012-01-09 15:16:13 -0800252/* these should only be used for the "adb disconnect" command */
253void unregister_transport(atransport *t);
254void unregister_all_tcp_transports();
255
256void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
257
258/* this should only be used for transports with connection_state == CS_NOPERM */
259void unregister_usb_transport(usb_handle *usb);
260
261atransport *find_transport(const char *serial);
262#if ADB_HOST
263atransport* find_emulator_transport_by_adb_port(int adb_port);
264#endif
265
266int service_to_fd(const char *name);
267#if ADB_HOST
268asocket *host_service_to_socket(const char* name, const char *serial);
269#endif
270
271#if !ADB_HOST
272typedef enum {
273 BACKUP,
274 RESTORE
275} BackupOperation;
276int backup_service(BackupOperation operation, char* args);
277void framebuffer_service(int fd, void *cookie);
278void log_service(int fd, void *cookie);
279void remount_service(int fd, void *cookie);
280char * get_log_file_path(const char * log_name);
281#endif
282
283/* packet allocator */
284apacket *get_apacket(void);
285void put_apacket(apacket *p);
286
287int check_header(apacket *p);
288int check_data(apacket *p);
289
290/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
291
292#define ADB_TRACE 1
293
294/* IMPORTANT: if you change the following list, don't
295 * forget to update the corresponding 'tags' table in
296 * the adb_trace_init() function implemented in adb.c
297 */
298typedef enum {
299 TRACE_ADB = 0, /* 0x001 */
300 TRACE_SOCKETS,
301 TRACE_PACKETS,
302 TRACE_TRANSPORT,
303 TRACE_RWX, /* 0x010 */
304 TRACE_USB,
305 TRACE_SYNC,
306 TRACE_SYSDEPS,
307 TRACE_JDWP, /* 0x100 */
308 TRACE_SERVICES,
309} AdbTrace;
310
311#if ADB_TRACE
312
313 extern int adb_trace_mask;
314 extern unsigned char adb_trace_output_count;
315 void adb_trace_init(void);
316
317# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
318
319 /* you must define TRACE_TAG before using this macro */
320# define D(...) \
321 do { \
322 if (ADB_TRACING) { \
323 int save_errno = errno; \
324 adb_mutex_lock(&D_lock); \
325 fprintf(stderr, "%s::%s():", \
326 __FILE__, __FUNCTION__); \
327 errno = save_errno; \
328 fprintf(stderr, __VA_ARGS__ ); \
329 fflush(stderr); \
330 adb_mutex_unlock(&D_lock); \
331 errno = save_errno; \
332 } \
333 } while (0)
334# define DR(...) \
335 do { \
336 if (ADB_TRACING) { \
337 int save_errno = errno; \
338 adb_mutex_lock(&D_lock); \
339 errno = save_errno; \
340 fprintf(stderr, __VA_ARGS__ ); \
341 fflush(stderr); \
342 adb_mutex_unlock(&D_lock); \
343 errno = save_errno; \
344 } \
345 } while (0)
346#else
347# define D(...) ((void)0)
348# define DR(...) ((void)0)
349# define ADB_TRACING 0
350#endif
351
352
353#if !TRACE_PACKETS
354#define print_packet(tag,p) do {} while (0)
355#endif
356
357#if ADB_HOST_ON_TARGET
358/* adb and adbd are coexisting on the target, so use 5038 for adb
359 * to avoid conflicting with adbd's usage of 5037
360 */
361# define DEFAULT_ADB_PORT 5038
362#else
363# define DEFAULT_ADB_PORT 5037
364#endif
365
366#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
367
368#define ADB_CLASS 0xff
369#define ADB_SUBCLASS 0x42
370#define ADB_PROTOCOL 0x1
371
372
373void local_init(int port);
374int local_connect(int port);
375int local_connect_arbitrary_ports(int console_port, int adb_port);
376
377/* usb host/client interface */
378void usb_init();
379void usb_cleanup();
380int usb_write(usb_handle *h, const void *data, int len);
381int usb_read(usb_handle *h, void *data, int len);
382int usb_close(usb_handle *h);
383void usb_kick(usb_handle *h);
384
385/* used for USB device detection */
386#if ADB_HOST
387int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
388#endif
389
390unsigned host_to_le32(unsigned n);
391int adb_commandline(int argc, char **argv);
392
393int connection_state(atransport *t);
394
395#define CS_ANY -1
396#define CS_OFFLINE 0
397#define CS_BOOTLOADER 1
398#define CS_DEVICE 2
399#define CS_HOST 3
400#define CS_RECOVERY 4
401#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
402#define CS_SIDELOAD 6
Doug Zongker075ad802014-06-26 15:35:51 -0700403#define CS_UNAUTHORIZED 7
Doug Zongker9270a202012-01-09 15:16:13 -0800404
405extern int HOST;
406extern int SHELL_EXIT_NOTIFY_FD;
407
408#define CHUNK_SIZE (64*1024)
409
Da Zhoud432deb2014-01-17 01:04:07 -0800410#if !ADB_HOST
411#define USB_ADB_PATH "/dev/android_adb"
412
413#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
414#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
415
416#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
417#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
418#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
419#endif
420
Doug Zongker9270a202012-01-09 15:16:13 -0800421int sendfailmsg(int fd, const char *reason);
422int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
423
Dees_Troy9a4b5692012-09-19 15:09:45 -0400424//#define ADB_SIDELOAD_FILENAME "/tmp/update.zip"
425extern char ADB_SIDELOAD_FILENAME[255];
Doug Zongker9270a202012-01-09 15:16:13 -0800426
427#endif