blob: 35d8a4d1097862fac58317193a11e4da26c9e2f0 [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"
JP Abgrall408fa572011-03-16 15:57:42 -070023#include "transport.h" /* readx(), writex() */
24
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#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
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070033#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#define A_VERSION 0x01000000 // ADB protocol version
36
37#define ADB_VERSION_MAJOR 1 // Used for help/version information
38#define ADB_VERSION_MINOR 0 // Used for help/version information
39
Doug Zongker71fe5842014-06-26 15:35:36 -070040#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 -080041
42typedef struct amessage amessage;
43typedef struct apacket apacket;
44typedef struct asocket asocket;
45typedef struct alistener alistener;
46typedef struct aservice aservice;
47typedef struct atransport atransport;
48typedef struct adisconnect adisconnect;
49typedef struct usb_handle usb_handle;
50
51struct amessage {
52 unsigned command; /* command identifier constant */
53 unsigned arg0; /* first argument */
54 unsigned arg1; /* second argument */
55 unsigned data_length; /* length of payload (0 is allowed) */
56 unsigned data_check; /* checksum of data payload */
57 unsigned magic; /* command ^ 0xffffffff */
58};
59
60struct apacket
61{
62 apacket *next;
63
64 unsigned len;
65 unsigned char *ptr;
66
67 amessage msg;
68 unsigned char data[MAX_PAYLOAD];
69};
70
71/* An asocket represents one half of a connection between a local and
72** remote entity. A local asocket is bound to a file descriptor. A
73** remote asocket is bound to the protocol engine.
74*/
75struct asocket {
76 /* chain pointers for the local/remote list of
77 ** asockets that this asocket lives in
78 */
79 asocket *next;
80 asocket *prev;
81
82 /* the unique identifier for this asocket
83 */
84 unsigned id;
85
86 /* flag: set when the socket's peer has closed
87 ** but packets are still queued for delivery
88 */
89 int closing;
90
Benoit Gobyf366b362012-03-16 14:50:07 -070091 /* flag: quit adbd when both ends close the
92 ** local service socket
93 */
94 int exit_on_close;
95
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 /* the asocket we are connected to
97 */
98
99 asocket *peer;
100
101 /* For local asockets, the fde is used to bind
102 ** us to our fd event system. For remote asockets
103 ** these fields are not used.
104 */
105 fdevent fde;
106 int fd;
107
108 /* queue of apackets waiting to be written
109 */
110 apacket *pkt_first;
111 apacket *pkt_last;
112
113 /* enqueue is called by our peer when it has data
114 ** for us. It should return 0 if we can accept more
115 ** data or 1 if not. If we return 1, we must call
116 ** peer->ready() when we once again are ready to
117 ** receive data.
118 */
119 int (*enqueue)(asocket *s, apacket *pkt);
120
121 /* ready is called by the peer when it is ready for
122 ** us to send data via enqueue again
123 */
124 void (*ready)(asocket *s);
125
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100126 /* shutdown is called by the peer before it goes away.
127 ** the socket should not do any further calls on its peer.
128 ** Always followed by a call to close. Optional, i.e. can be NULL.
129 */
130 void (*shutdown)(asocket *s);
131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 /* close is called by the peer when it has gone away.
133 ** we are not allowed to make any further calls on the
134 ** peer once our close method is called.
135 */
136 void (*close)(asocket *s);
137
Benoit Goby9470c2f2013-02-20 15:04:53 -0800138 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 atransport *transport;
140};
141
142
143/* the adisconnect structure is used to record a callback that
144** will be called whenever a transport is disconnected (e.g. by the user)
145** this should be used to cleanup objects that depend on the
146** transport (e.g. remote sockets, listeners, etc...)
147*/
148struct adisconnect
149{
150 void (*func)(void* opaque, atransport* t);
151 void* opaque;
152 adisconnect* next;
153 adisconnect* prev;
154};
155
156
157/* a transport object models the connection to a remote device or emulator
158** there is one transport per connected device/emulator. a "local transport"
159** connects through TCP (for the emulator), while a "usb transport" through
160** USB (for real devices)
161**
162** note that kTransportHost doesn't really correspond to a real transport
163** object, it's a special value used to indicate that a client wants to
164** connect to a service implemented within the ADB server itself.
165*/
166typedef enum transport_type {
167 kTransportUsb,
168 kTransportLocal,
169 kTransportAny,
170 kTransportHost,
171} transport_type;
172
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700173#define TOKEN_SIZE 20
174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175struct atransport
176{
177 atransport *next;
178 atransport *prev;
179
180 int (*read_from_remote)(apacket *p, atransport *t);
181 int (*write_to_remote)(apacket *p, atransport *t);
182 void (*close)(atransport *t);
183 void (*kick)(atransport *t);
184
185 int fd;
186 int transport_socket;
187 fdevent transport_fde;
188 int ref_count;
189 unsigned sync_token;
190 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700191 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 transport_type type;
193
194 /* usb handle or socket fd as needed */
195 usb_handle *usb;
196 int sfd;
197
198 /* used to identify transports for clients */
199 char *serial;
200 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700201 char *model;
202 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700203 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100204 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
206 /* a list of adisconnect callbacks called when the transport is kicked */
207 int kicked;
208 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700209
210 void *key;
211 unsigned char token[TOKEN_SIZE];
212 fdevent auth_fde;
213 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214};
215
216
217/* A listener is an entity which binds to a local port
218** and, upon receiving a connection on that port, creates
219** an asocket to connect the new local connection to a
220** specific remote service.
221**
222** TODO: some listeners read from the new connection to
223** determine what exact service to connect to on the far
224** side.
225*/
226struct alistener
227{
228 alistener *next;
229 alistener *prev;
230
231 fdevent fde;
232 int fd;
233
234 const char *local_name;
235 const char *connect_to;
236 atransport *transport;
237 adisconnect disconnect;
238};
239
240
241void print_packet(const char *label, apacket *p);
242
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100243asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244void install_local_socket(asocket *s);
245void remove_socket(asocket *s);
246void close_all_sockets(atransport *t);
247
248#define LOCAL_CLIENT_PREFIX "emulator-"
249
250asocket *create_local_socket(int fd);
251asocket *create_local_service_socket(const char *destination);
252
253asocket *create_remote_socket(unsigned id, atransport *t);
254void connect_to_remote(asocket *s, const char *destination);
255void connect_to_smartsocket(asocket *s);
256
257void fatal(const char *fmt, ...);
258void fatal_errno(const char *fmt, ...);
259
260void handle_packet(apacket *p, atransport *t);
261void send_packet(apacket *p, atransport *t);
262
Alexey Tarasov31664102009-10-22 02:55:00 +1100263void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100264int launch_server(int server_port);
265int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266
267
268/* transports are ref-counted
269** get_device_transport does an acquire on your behalf before returning
270*/
271void init_transport_registration(void);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700272int list_transports(char *buf, size_t bufsize, int long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273void update_transports(void);
274
275asocket* create_device_tracker(void);
276
277/* Obtain a transport from the available transports.
278** If state is != CS_ANY, only transports in that state are considered.
279** If serial is non-NULL then only the device with that serial will be chosen.
280** If no suitable transport is found, error is set.
281*/
282atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
283void add_transport_disconnect( atransport* t, adisconnect* dis );
284void remove_transport_disconnect( atransport* t, adisconnect* dis );
285void run_transport_disconnects( atransport* t );
286void kick_transport( atransport* t );
287
288/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100289#if ADB_HOST
290int get_available_local_transport_index();
291#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700292int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400293void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294
295/* for MacOS X cleanup */
296void close_usb_devices();
297
298/* cause new transports to be init'd and added to the list */
Benoit Goby1c45ee92013-03-29 18:22:36 -0700299int register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400300
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400301/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400302void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400303void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400304
Scott Andersone109d262012-04-20 11:21:14 -0700305void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400306
307/* this should only be used for transports with connection_state == CS_NOPERM */
308void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400310atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100311#if ADB_HOST
312atransport* find_emulator_transport_by_adb_port(int adb_port);
313#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400314
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315int service_to_fd(const char *name);
316#if ADB_HOST
317asocket *host_service_to_socket(const char* name, const char *serial);
318#endif
319
320#if !ADB_HOST
321int init_jdwp(void);
322asocket* create_jdwp_service_socket();
323asocket* create_jdwp_tracker_service_socket();
324int create_jdwp_connection_fd(int jdwp_pid);
325#endif
326
David 'Digit' Turner25258692013-03-21 21:07:42 +0100327int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329#if !ADB_HOST
330void framebuffer_service(int fd, void *cookie);
Paul Lawrence7c442e12014-12-03 15:31:57 -0800331// Allow enable-verity to write to system and vendor block devices
332int make_system_and_vendor_block_devices_writable();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333void remount_service(int fd, void *cookie);
Paul Lawrence7c442e12014-12-03 15:31:57 -0800334void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335#endif
336
337/* packet allocator */
338apacket *get_apacket(void);
339void put_apacket(apacket *p);
340
341int check_header(apacket *p);
342int check_data(apacket *p);
343
leozwangadb09fa2014-08-15 09:51:27 -0700344// Define it if you want to dump packets.
345#define DEBUG_PACKETS 0
346
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700347#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348#define print_packet(tag,p) do {} while (0)
349#endif
350
John Michelauc3188332010-09-23 17:08:34 -0500351#if ADB_HOST_ON_TARGET
352/* adb and adbd are coexisting on the target, so use 5038 for adb
353 * to avoid conflicting with adbd's usage of 5037
354 */
355# define DEFAULT_ADB_PORT 5038
356#else
357# define DEFAULT_ADB_PORT 5037
358#endif
359
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100360#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361
Xavier Ducroheta481d092009-05-21 17:47:43 -0700362#define ADB_CLASS 0xff
363#define ADB_SUBCLASS 0x42
364#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700367void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100369int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370
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 */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700380#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700382#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384int adb_commandline(int argc, char **argv);
385
386int connection_state(atransport *t);
387
388#define CS_ANY -1
389#define CS_OFFLINE 0
390#define CS_BOOTLOADER 1
391#define CS_DEVICE 2
392#define CS_HOST 3
393#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400394#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800395#define CS_SIDELOAD 6
Benoit Goby77e8e582013-01-15 12:36:47 -0800396#define CS_UNAUTHORIZED 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
398extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700399extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700401typedef enum {
402 SUBPROC_PTY = 0,
403 SUBPROC_RAW = 1,
404} subproc_mode;
405
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406#define CHUNK_SIZE (64*1024)
407
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100408#if !ADB_HOST
409#define USB_ADB_PATH "/dev/android_adb"
410
411#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
412#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
413
414#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
415#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
416#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
417#endif
418
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419int sendfailmsg(int fd, const char *reason);
420int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
421
422#endif