blob: 58818a996fb338eb9817a9ccbd97b8d149795d05 [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
Dan Albertd808d212015-02-18 10:21:54 -080025#ifdef __cplusplus
26extern "C" {
27#endif
28
Doug Zongker9270a202012-01-09 15:16:13 -080029#define MAX_PAYLOAD 4096
30
31#define A_SYNC 0x434e5953
32#define A_CNXN 0x4e584e43
33#define A_OPEN 0x4e45504f
34#define A_OKAY 0x59414b4f
35#define A_CLSE 0x45534c43
36#define A_WRTE 0x45545257
37
38#define A_VERSION 0x01000000 // ADB protocol version
39
40#define ADB_VERSION_MAJOR 1 // Used for help/version information
41#define ADB_VERSION_MINOR 0 // Used for help/version information
42
43#define ADB_SERVER_VERSION 29 // Increment this when we want to force users to start a new adb server
44
45typedef struct amessage amessage;
46typedef struct apacket apacket;
47typedef struct asocket asocket;
Doug Zongker9270a202012-01-09 15:16:13 -080048typedef struct aservice aservice;
49typedef struct atransport atransport;
50typedef struct adisconnect adisconnect;
51typedef struct usb_handle usb_handle;
52
53struct amessage {
54 unsigned command; /* command identifier constant */
55 unsigned arg0; /* first argument */
56 unsigned arg1; /* second argument */
57 unsigned data_length; /* length of payload (0 is allowed) */
58 unsigned data_check; /* checksum of data payload */
59 unsigned magic; /* command ^ 0xffffffff */
60};
61
62struct apacket
63{
64 apacket *next;
65
66 unsigned len;
67 unsigned char *ptr;
68
69 amessage msg;
70 unsigned char data[MAX_PAYLOAD];
71};
72
73/* An asocket represents one half of a connection between a local and
74** remote entity. A local asocket is bound to a file descriptor. A
75** remote asocket is bound to the protocol engine.
76*/
77struct asocket {
78 /* chain pointers for the local/remote list of
79 ** asockets that this asocket lives in
80 */
81 asocket *next;
82 asocket *prev;
83
84 /* the unique identifier for this asocket
85 */
86 unsigned id;
87
88 /* flag: set when the socket's peer has closed
89 ** but packets are still queued for delivery
90 */
91 int closing;
92
93 /* the asocket we are connected to
94 */
95
96 asocket *peer;
97
98 /* For local asockets, the fde is used to bind
99 ** us to our fd event system. For remote asockets
100 ** these fields are not used.
101 */
102 fdevent fde;
103 int fd;
104
105 /* queue of apackets waiting to be written
106 */
107 apacket *pkt_first;
108 apacket *pkt_last;
109
110 /* enqueue is called by our peer when it has data
111 ** for us. It should return 0 if we can accept more
112 ** data or 1 if not. If we return 1, we must call
113 ** peer->ready() when we once again are ready to
114 ** receive data.
115 */
116 int (*enqueue)(asocket *s, apacket *pkt);
117
118 /* ready is called by the peer when it is ready for
119 ** us to send data via enqueue again
120 */
121 void (*ready)(asocket *s);
122
123 /* close is called by the peer when it has gone away.
124 ** we are not allowed to make any further calls on the
125 ** peer once our close method is called.
126 */
127 void (*close)(asocket *s);
128
129 /* socket-type-specific extradata */
130 void *extra;
131
132 /* A socket is bound to atransport */
133 atransport *transport;
134};
135
136
137/* the adisconnect structure is used to record a callback that
138** will be called whenever a transport is disconnected (e.g. by the user)
139** this should be used to cleanup objects that depend on the
Doug Zongker098d1f62012-03-20 09:40:04 -0700140** transport (e.g. remote sockets, etc...)
Doug Zongker9270a202012-01-09 15:16:13 -0800141*/
142struct adisconnect
143{
144 void (*func)(void* opaque, atransport* t);
145 void* opaque;
146 adisconnect* next;
147 adisconnect* prev;
148};
149
150
151/* a transport object models the connection to a remote device or emulator
152** there is one transport per connected device/emulator. a "local transport"
153** connects through TCP (for the emulator), while a "usb transport" through
154** USB (for real devices)
155**
156** note that kTransportHost doesn't really correspond to a real transport
157** object, it's a special value used to indicate that a client wants to
158** connect to a service implemented within the ADB server itself.
159*/
160typedef enum transport_type {
161 kTransportUsb,
162 kTransportLocal,
163 kTransportAny,
164 kTransportHost,
165} transport_type;
166
167struct atransport
168{
169 atransport *next;
170 atransport *prev;
171
172 int (*read_from_remote)(apacket *p, atransport *t);
173 int (*write_to_remote)(apacket *p, atransport *t);
174 void (*close)(atransport *t);
175 void (*kick)(atransport *t);
176
177 int fd;
178 int transport_socket;
179 fdevent transport_fde;
180 int ref_count;
181 unsigned sync_token;
182 int connection_state;
183 transport_type type;
184
185 /* usb handle or socket fd as needed */
186 usb_handle *usb;
187 int sfd;
188
189 /* used to identify transports for clients */
190 char *serial;
191 char *product;
192 int adb_port; // Use for emulators (local transport)
193
194 /* a list of adisconnect callbacks called when the transport is kicked */
195 int kicked;
196 adisconnect disconnects;
197};
198
199
Doug Zongker9270a202012-01-09 15:16:13 -0800200void print_packet(const char *label, apacket *p);
201
202asocket *find_local_socket(unsigned id);
203void install_local_socket(asocket *s);
204void remove_socket(asocket *s);
205void close_all_sockets(atransport *t);
206
207#define LOCAL_CLIENT_PREFIX "emulator-"
208
209asocket *create_local_socket(int fd);
210asocket *create_local_service_socket(const char *destination);
211
212asocket *create_remote_socket(unsigned id, atransport *t);
213void connect_to_remote(asocket *s, const char *destination);
214void connect_to_smartsocket(asocket *s);
215
216void fatal(const char *fmt, ...);
217void fatal_errno(const char *fmt, ...);
218
219void handle_packet(apacket *p, atransport *t);
220void send_packet(apacket *p, atransport *t);
221
222void get_my_path(char *s, size_t maxLen);
223int launch_server(int server_port);
224int adb_main();
225
226
227/* transports are ref-counted
228** get_device_transport does an acquire on your behalf before returning
229*/
230void init_transport_registration(void);
231int list_transports(char *buf, size_t bufsize);
232void update_transports(void);
233
234asocket* create_device_tracker(void);
235
236/* Obtain a transport from the available transports.
237** If state is != CS_ANY, only transports in that state are considered.
238** If serial is non-NULL then only the device with that serial will be chosen.
239** If no suitable transport is found, error is set.
240*/
241atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
242void add_transport_disconnect( atransport* t, adisconnect* dis );
243void remove_transport_disconnect( atransport* t, adisconnect* dis );
244void run_transport_disconnects( atransport* t );
245void kick_transport( atransport* t );
246
247/* initialize a transport object's func pointers and state */
248#if ADB_HOST
249int get_available_local_transport_index();
250#endif
Doug Zongker9270a202012-01-09 15:16:13 -0800251void init_usb_transport(atransport *t, usb_handle *usb, int state);
252
253/* for MacOS X cleanup */
254void close_usb_devices();
255
Doug Zongker9270a202012-01-09 15:16:13 -0800256/* these should only be used for the "adb disconnect" command */
257void unregister_transport(atransport *t);
258void unregister_all_tcp_transports();
259
Dan Albert8f1bfea2014-11-25 10:55:50 -0800260void register_usb_transport(usb_handle *h, const char *serial,
261 const char* dev_path, unsigned writeable);
Doug Zongker9270a202012-01-09 15:16:13 -0800262
263/* this should only be used for transports with connection_state == CS_NOPERM */
264void unregister_usb_transport(usb_handle *usb);
265
266atransport *find_transport(const char *serial);
267#if ADB_HOST
268atransport* find_emulator_transport_by_adb_port(int adb_port);
269#endif
270
271int service_to_fd(const char *name);
272#if ADB_HOST
273asocket *host_service_to_socket(const char* name, const char *serial);
274#endif
275
276#if !ADB_HOST
277typedef enum {
278 BACKUP,
279 RESTORE
280} BackupOperation;
281int backup_service(BackupOperation operation, char* args);
282void framebuffer_service(int fd, void *cookie);
283void log_service(int fd, void *cookie);
284void remount_service(int fd, void *cookie);
285char * get_log_file_path(const char * log_name);
286#endif
287
288/* packet allocator */
289apacket *get_apacket(void);
290void put_apacket(apacket *p);
291
292int check_header(apacket *p);
293int check_data(apacket *p);
294
295/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */
296
297#define ADB_TRACE 1
298
299/* IMPORTANT: if you change the following list, don't
300 * forget to update the corresponding 'tags' table in
301 * the adb_trace_init() function implemented in adb.c
302 */
303typedef enum {
304 TRACE_ADB = 0, /* 0x001 */
305 TRACE_SOCKETS,
306 TRACE_PACKETS,
307 TRACE_TRANSPORT,
308 TRACE_RWX, /* 0x010 */
309 TRACE_USB,
310 TRACE_SYNC,
311 TRACE_SYSDEPS,
312 TRACE_JDWP, /* 0x100 */
313 TRACE_SERVICES,
314} AdbTrace;
315
316#if ADB_TRACE
317
318 extern int adb_trace_mask;
319 extern unsigned char adb_trace_output_count;
320 void adb_trace_init(void);
321
322# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
323
324 /* you must define TRACE_TAG before using this macro */
325# define D(...) \
326 do { \
327 if (ADB_TRACING) { \
328 int save_errno = errno; \
329 adb_mutex_lock(&D_lock); \
330 fprintf(stderr, "%s::%s():", \
331 __FILE__, __FUNCTION__); \
332 errno = save_errno; \
333 fprintf(stderr, __VA_ARGS__ ); \
334 fflush(stderr); \
335 adb_mutex_unlock(&D_lock); \
336 errno = save_errno; \
337 } \
338 } while (0)
339# define DR(...) \
340 do { \
341 if (ADB_TRACING) { \
342 int save_errno = errno; \
343 adb_mutex_lock(&D_lock); \
344 errno = save_errno; \
345 fprintf(stderr, __VA_ARGS__ ); \
346 fflush(stderr); \
347 adb_mutex_unlock(&D_lock); \
348 errno = save_errno; \
349 } \
350 } while (0)
351#else
352# define D(...) ((void)0)
353# define DR(...) ((void)0)
354# define ADB_TRACING 0
355#endif
356
357
358#if !TRACE_PACKETS
359#define print_packet(tag,p) do {} while (0)
360#endif
361
362#if ADB_HOST_ON_TARGET
363/* adb and adbd are coexisting on the target, so use 5038 for adb
364 * to avoid conflicting with adbd's usage of 5037
365 */
366# define DEFAULT_ADB_PORT 5038
367#else
368# define DEFAULT_ADB_PORT 5037
369#endif
370
371#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
372
373#define ADB_CLASS 0xff
374#define ADB_SUBCLASS 0x42
375#define ADB_PROTOCOL 0x1
376
377
378void local_init(int port);
379int local_connect(int port);
380int local_connect_arbitrary_ports(int console_port, int adb_port);
381
382/* usb host/client interface */
383void usb_init();
384void usb_cleanup();
385int usb_write(usb_handle *h, const void *data, int len);
386int usb_read(usb_handle *h, void *data, int len);
387int usb_close(usb_handle *h);
388void usb_kick(usb_handle *h);
389
390/* used for USB device detection */
391#if ADB_HOST
392int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
393#endif
394
395unsigned host_to_le32(unsigned n);
396int adb_commandline(int argc, char **argv);
397
398int connection_state(atransport *t);
399
400#define CS_ANY -1
401#define CS_OFFLINE 0
402#define CS_BOOTLOADER 1
403#define CS_DEVICE 2
404#define CS_HOST 3
405#define CS_RECOVERY 4
406#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
407#define CS_SIDELOAD 6
Doug Zongker075ad802014-06-26 15:35:51 -0700408#define CS_UNAUTHORIZED 7
Doug Zongker9270a202012-01-09 15:16:13 -0800409
410extern int HOST;
411extern int SHELL_EXIT_NOTIFY_FD;
412
413#define CHUNK_SIZE (64*1024)
414
Da Zhou65ad9282014-01-17 01:04:07 -0800415#if !ADB_HOST
416#define USB_ADB_PATH "/dev/android_adb"
417
418#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
419#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
420
421#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
422#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
423#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
424#endif
425
Doug Zongker9270a202012-01-09 15:16:13 -0800426int sendfailmsg(int fd, const char *reason);
427int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
428
Dan Albertd808d212015-02-18 10:21:54 -0800429#ifdef __cplusplus
430}
431#endif
432
Doug Zongker9270a202012-01-09 15:16:13 -0800433#endif