blob: 48f2a25019b071850b8144da0c60ce5158161cf5 [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
leozwangd3fc15f2014-07-29 12:50:02 -070022#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080023#include "fdevent.h"
JP Abgrall408fa572011-03-16 15:57:42 -070024#include "transport.h" /* readx(), writex() */
25
Dan Albert818fb4b2015-02-18 00:18:25 -080026#ifdef __cplusplus
27extern "C" {
28#endif
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#define MAX_PAYLOAD 4096
31
32#define A_SYNC 0x434e5953
33#define A_CNXN 0x4e584e43
34#define A_OPEN 0x4e45504f
35#define A_OKAY 0x59414b4f
36#define A_CLSE 0x45534c43
37#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070038#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
40#define A_VERSION 0x01000000 // ADB protocol version
41
42#define ADB_VERSION_MAJOR 1 // Used for help/version information
43#define ADB_VERSION_MINOR 0 // Used for help/version information
44
Doug Zongker71fe5842014-06-26 15:35:36 -070045#define ADB_SERVER_VERSION 32 // Increment this when we want to force users to start a new adb server
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
47typedef struct amessage amessage;
48typedef struct apacket apacket;
49typedef struct asocket asocket;
50typedef struct alistener alistener;
51typedef struct aservice aservice;
52typedef struct atransport atransport;
53typedef struct adisconnect adisconnect;
54typedef struct usb_handle usb_handle;
55
56struct amessage {
57 unsigned command; /* command identifier constant */
58 unsigned arg0; /* first argument */
59 unsigned arg1; /* second argument */
60 unsigned data_length; /* length of payload (0 is allowed) */
61 unsigned data_check; /* checksum of data payload */
62 unsigned magic; /* command ^ 0xffffffff */
63};
64
65struct apacket
66{
67 apacket *next;
68
69 unsigned len;
70 unsigned char *ptr;
71
72 amessage msg;
73 unsigned char data[MAX_PAYLOAD];
74};
75
76/* An asocket represents one half of a connection between a local and
77** remote entity. A local asocket is bound to a file descriptor. A
78** remote asocket is bound to the protocol engine.
79*/
80struct asocket {
81 /* chain pointers for the local/remote list of
82 ** asockets that this asocket lives in
83 */
84 asocket *next;
85 asocket *prev;
86
87 /* the unique identifier for this asocket
88 */
89 unsigned id;
90
91 /* flag: set when the socket's peer has closed
92 ** but packets are still queued for delivery
93 */
94 int closing;
95
Benoit Gobyf366b362012-03-16 14:50:07 -070096 /* flag: quit adbd when both ends close the
97 ** local service socket
98 */
99 int exit_on_close;
100
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 /* the asocket we are connected to
102 */
103
104 asocket *peer;
105
106 /* For local asockets, the fde is used to bind
107 ** us to our fd event system. For remote asockets
108 ** these fields are not used.
109 */
110 fdevent fde;
111 int fd;
112
113 /* queue of apackets waiting to be written
114 */
115 apacket *pkt_first;
116 apacket *pkt_last;
117
118 /* enqueue is called by our peer when it has data
119 ** for us. It should return 0 if we can accept more
120 ** data or 1 if not. If we return 1, we must call
121 ** peer->ready() when we once again are ready to
122 ** receive data.
123 */
124 int (*enqueue)(asocket *s, apacket *pkt);
125
126 /* ready is called by the peer when it is ready for
127 ** us to send data via enqueue again
128 */
129 void (*ready)(asocket *s);
130
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100131 /* shutdown is called by the peer before it goes away.
132 ** the socket should not do any further calls on its peer.
133 ** Always followed by a call to close. Optional, i.e. can be NULL.
134 */
135 void (*shutdown)(asocket *s);
136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 /* close is called by the peer when it has gone away.
138 ** we are not allowed to make any further calls on the
139 ** peer once our close method is called.
140 */
141 void (*close)(asocket *s);
142
Benoit Goby9470c2f2013-02-20 15:04:53 -0800143 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 atransport *transport;
145};
146
147
148/* the adisconnect structure is used to record a callback that
149** will be called whenever a transport is disconnected (e.g. by the user)
150** this should be used to cleanup objects that depend on the
151** transport (e.g. remote sockets, listeners, etc...)
152*/
153struct adisconnect
154{
155 void (*func)(void* opaque, atransport* t);
156 void* opaque;
157 adisconnect* next;
158 adisconnect* prev;
159};
160
161
162/* a transport object models the connection to a remote device or emulator
163** there is one transport per connected device/emulator. a "local transport"
164** connects through TCP (for the emulator), while a "usb transport" through
165** USB (for real devices)
166**
167** note that kTransportHost doesn't really correspond to a real transport
168** object, it's a special value used to indicate that a client wants to
169** connect to a service implemented within the ADB server itself.
170*/
171typedef enum transport_type {
172 kTransportUsb,
173 kTransportLocal,
174 kTransportAny,
175 kTransportHost,
176} transport_type;
177
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700178#define TOKEN_SIZE 20
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180struct atransport
181{
182 atransport *next;
183 atransport *prev;
184
185 int (*read_from_remote)(apacket *p, atransport *t);
186 int (*write_to_remote)(apacket *p, atransport *t);
187 void (*close)(atransport *t);
188 void (*kick)(atransport *t);
189
190 int fd;
191 int transport_socket;
192 fdevent transport_fde;
193 int ref_count;
194 unsigned sync_token;
195 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700196 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 transport_type type;
198
199 /* usb handle or socket fd as needed */
200 usb_handle *usb;
201 int sfd;
202
203 /* used to identify transports for clients */
204 char *serial;
205 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700206 char *model;
207 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700208 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100209 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
211 /* a list of adisconnect callbacks called when the transport is kicked */
212 int kicked;
213 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700214
215 void *key;
216 unsigned char token[TOKEN_SIZE];
217 fdevent auth_fde;
218 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219};
220
221
222/* A listener is an entity which binds to a local port
223** and, upon receiving a connection on that port, creates
224** an asocket to connect the new local connection to a
225** specific remote service.
226**
227** TODO: some listeners read from the new connection to
228** determine what exact service to connect to on the far
229** side.
230*/
231struct alistener
232{
233 alistener *next;
234 alistener *prev;
235
236 fdevent fde;
237 int fd;
238
239 const char *local_name;
240 const char *connect_to;
241 atransport *transport;
242 adisconnect disconnect;
243};
244
245
246void print_packet(const char *label, apacket *p);
247
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100248asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249void install_local_socket(asocket *s);
250void remove_socket(asocket *s);
251void close_all_sockets(atransport *t);
252
253#define LOCAL_CLIENT_PREFIX "emulator-"
254
255asocket *create_local_socket(int fd);
256asocket *create_local_service_socket(const char *destination);
257
258asocket *create_remote_socket(unsigned id, atransport *t);
259void connect_to_remote(asocket *s, const char *destination);
260void connect_to_smartsocket(asocket *s);
261
262void fatal(const char *fmt, ...);
263void fatal_errno(const char *fmt, ...);
264
265void handle_packet(apacket *p, atransport *t);
266void send_packet(apacket *p, atransport *t);
267
Alexey Tarasov31664102009-10-22 02:55:00 +1100268void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100269int launch_server(int server_port);
270int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
272
273/* transports are ref-counted
274** get_device_transport does an acquire on your behalf before returning
275*/
276void init_transport_registration(void);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700277int list_transports(char *buf, size_t bufsize, int long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278void update_transports(void);
279
280asocket* create_device_tracker(void);
281
282/* Obtain a transport from the available transports.
283** If state is != CS_ANY, only transports in that state are considered.
284** If serial is non-NULL then only the device with that serial will be chosen.
285** If no suitable transport is found, error is set.
286*/
287atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
288void add_transport_disconnect( atransport* t, adisconnect* dis );
289void remove_transport_disconnect( atransport* t, adisconnect* dis );
290void run_transport_disconnects( atransport* t );
291void kick_transport( atransport* t );
292
293/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100294#if ADB_HOST
295int get_available_local_transport_index();
296#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700297int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400298void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299
300/* for MacOS X cleanup */
301void close_usb_devices();
302
303/* cause new transports to be init'd and added to the list */
Benoit Goby1c45ee92013-03-29 18:22:36 -0700304int register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400305
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400306/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400307void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400308void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400309
Scott Andersone109d262012-04-20 11:21:14 -0700310void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400311
312/* this should only be used for transports with connection_state == CS_NOPERM */
313void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400315atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100316#if ADB_HOST
317atransport* find_emulator_transport_by_adb_port(int adb_port);
318#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320int service_to_fd(const char *name);
321#if ADB_HOST
322asocket *host_service_to_socket(const char* name, const char *serial);
323#endif
324
325#if !ADB_HOST
326int init_jdwp(void);
327asocket* create_jdwp_service_socket();
328asocket* create_jdwp_tracker_service_socket();
329int create_jdwp_connection_fd(int jdwp_pid);
330#endif
331
David 'Digit' Turner25258692013-03-21 21:07:42 +0100332int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334#if !ADB_HOST
335void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800336// Allow enable-verity to write to system and vendor block devices
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000337int make_block_device_writable(const char* dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338void remount_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800339void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340#endif
341
342/* packet allocator */
343apacket *get_apacket(void);
344void put_apacket(apacket *p);
345
346int check_header(apacket *p);
347int check_data(apacket *p);
348
leozwangcbf02672014-08-15 09:51:27 -0700349// Define it if you want to dump packets.
350#define DEBUG_PACKETS 0
351
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700352#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353#define print_packet(tag,p) do {} while (0)
354#endif
355
John Michelauc3188332010-09-23 17:08:34 -0500356#if ADB_HOST_ON_TARGET
357/* adb and adbd are coexisting on the target, so use 5038 for adb
358 * to avoid conflicting with adbd's usage of 5037
359 */
360# define DEFAULT_ADB_PORT 5038
361#else
362# define DEFAULT_ADB_PORT 5037
363#endif
364
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100365#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
Xavier Ducroheta481d092009-05-21 17:47:43 -0700367#define ADB_CLASS 0xff
368#define ADB_SUBCLASS 0x42
369#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700372void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100374int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375
376/* usb host/client interface */
377void usb_init();
378void usb_cleanup();
379int usb_write(usb_handle *h, const void *data, int len);
380int usb_read(usb_handle *h, void *data, int len);
381int usb_close(usb_handle *h);
382void usb_kick(usb_handle *h);
383
384/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700385#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700387#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389int adb_commandline(int argc, char **argv);
390
391int connection_state(atransport *t);
392
393#define CS_ANY -1
394#define CS_OFFLINE 0
395#define CS_BOOTLOADER 1
396#define CS_DEVICE 2
397#define CS_HOST 3
398#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400399#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800400#define CS_SIDELOAD 6
Benoit Goby77e8e582013-01-15 12:36:47 -0800401#define CS_UNAUTHORIZED 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
403extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700404extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700406typedef enum {
407 SUBPROC_PTY = 0,
408 SUBPROC_RAW = 1,
409} subproc_mode;
410
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411#define CHUNK_SIZE (64*1024)
412
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100413#if !ADB_HOST
414#define USB_ADB_PATH "/dev/android_adb"
415
416#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
417#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
418
419#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
420#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
421#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
422#endif
423
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424int sendfailmsg(int fd, const char *reason);
425int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
426
Dan Albertba3a2512015-02-18 17:47:33 -0800427void handle_online(atransport *t);
428void handle_offline(atransport *t);
429
430void send_connect(atransport *t);
431
Dan Albert818fb4b2015-02-18 00:18:25 -0800432#ifdef __cplusplus
433}
434#endif
435
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436#endif