blob: 6ac47312ab6223f8461c33a5f2d8c0d482d63723 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#define MAX_PAYLOAD 4096
27
28#define A_SYNC 0x434e5953
29#define A_CNXN 0x4e584e43
30#define A_OPEN 0x4e45504f
31#define A_OKAY 0x59414b4f
32#define A_CLSE 0x45534c43
33#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070034#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36#define A_VERSION 0x01000000 // ADB protocol version
37
38#define ADB_VERSION_MAJOR 1 // Used for help/version information
39#define ADB_VERSION_MINOR 0 // Used for help/version information
40
Doug Zongker71fe5842014-06-26 15:35:36 -070041#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 -080042
43typedef struct amessage amessage;
44typedef struct apacket apacket;
45typedef struct asocket asocket;
46typedef struct alistener alistener;
47typedef struct aservice aservice;
48typedef struct atransport atransport;
49typedef struct adisconnect adisconnect;
50typedef struct usb_handle usb_handle;
51
52struct amessage {
53 unsigned command; /* command identifier constant */
54 unsigned arg0; /* first argument */
55 unsigned arg1; /* second argument */
56 unsigned data_length; /* length of payload (0 is allowed) */
57 unsigned data_check; /* checksum of data payload */
58 unsigned magic; /* command ^ 0xffffffff */
59};
60
61struct apacket
62{
63 apacket *next;
64
65 unsigned len;
66 unsigned char *ptr;
67
68 amessage msg;
69 unsigned char data[MAX_PAYLOAD];
70};
71
72/* An asocket represents one half of a connection between a local and
73** remote entity. A local asocket is bound to a file descriptor. A
74** remote asocket is bound to the protocol engine.
75*/
76struct asocket {
77 /* chain pointers for the local/remote list of
78 ** asockets that this asocket lives in
79 */
80 asocket *next;
81 asocket *prev;
82
83 /* the unique identifier for this asocket
84 */
85 unsigned id;
86
87 /* flag: set when the socket's peer has closed
88 ** but packets are still queued for delivery
89 */
90 int closing;
91
Benoit Gobyf366b362012-03-16 14:50:07 -070092 /* flag: quit adbd when both ends close the
93 ** local service socket
94 */
95 int exit_on_close;
96
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 /* the asocket we are connected to
98 */
99
100 asocket *peer;
101
102 /* For local asockets, the fde is used to bind
103 ** us to our fd event system. For remote asockets
104 ** these fields are not used.
105 */
106 fdevent fde;
107 int fd;
108
109 /* queue of apackets waiting to be written
110 */
111 apacket *pkt_first;
112 apacket *pkt_last;
113
114 /* enqueue is called by our peer when it has data
115 ** for us. It should return 0 if we can accept more
116 ** data or 1 if not. If we return 1, we must call
117 ** peer->ready() when we once again are ready to
118 ** receive data.
119 */
120 int (*enqueue)(asocket *s, apacket *pkt);
121
122 /* ready is called by the peer when it is ready for
123 ** us to send data via enqueue again
124 */
125 void (*ready)(asocket *s);
126
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100127 /* shutdown is called by the peer before it goes away.
128 ** the socket should not do any further calls on its peer.
129 ** Always followed by a call to close. Optional, i.e. can be NULL.
130 */
131 void (*shutdown)(asocket *s);
132
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 /* close is called by the peer when it has gone away.
134 ** we are not allowed to make any further calls on the
135 ** peer once our close method is called.
136 */
137 void (*close)(asocket *s);
138
Benoit Goby9470c2f2013-02-20 15:04:53 -0800139 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 atransport *transport;
141};
142
143
144/* the adisconnect structure is used to record a callback that
145** will be called whenever a transport is disconnected (e.g. by the user)
146** this should be used to cleanup objects that depend on the
147** transport (e.g. remote sockets, listeners, etc...)
148*/
149struct adisconnect
150{
151 void (*func)(void* opaque, atransport* t);
152 void* opaque;
153 adisconnect* next;
154 adisconnect* prev;
155};
156
157
158/* a transport object models the connection to a remote device or emulator
159** there is one transport per connected device/emulator. a "local transport"
160** connects through TCP (for the emulator), while a "usb transport" through
161** USB (for real devices)
162**
163** note that kTransportHost doesn't really correspond to a real transport
164** object, it's a special value used to indicate that a client wants to
165** connect to a service implemented within the ADB server itself.
166*/
167typedef enum transport_type {
168 kTransportUsb,
169 kTransportLocal,
170 kTransportAny,
171 kTransportHost,
172} transport_type;
173
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700174#define TOKEN_SIZE 20
175
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176struct atransport
177{
178 atransport *next;
179 atransport *prev;
180
181 int (*read_from_remote)(apacket *p, atransport *t);
182 int (*write_to_remote)(apacket *p, atransport *t);
183 void (*close)(atransport *t);
184 void (*kick)(atransport *t);
185
186 int fd;
187 int transport_socket;
188 fdevent transport_fde;
189 int ref_count;
190 unsigned sync_token;
191 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700192 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 transport_type type;
194
195 /* usb handle or socket fd as needed */
196 usb_handle *usb;
197 int sfd;
198
199 /* used to identify transports for clients */
200 char *serial;
201 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700202 char *model;
203 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700204 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100205 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206
207 /* a list of adisconnect callbacks called when the transport is kicked */
208 int kicked;
209 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700210
211 void *key;
212 unsigned char token[TOKEN_SIZE];
213 fdevent auth_fde;
214 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215};
216
217
218/* A listener is an entity which binds to a local port
219** and, upon receiving a connection on that port, creates
220** an asocket to connect the new local connection to a
221** specific remote service.
222**
223** TODO: some listeners read from the new connection to
224** determine what exact service to connect to on the far
225** side.
226*/
227struct alistener
228{
229 alistener *next;
230 alistener *prev;
231
232 fdevent fde;
233 int fd;
234
235 const char *local_name;
236 const char *connect_to;
237 atransport *transport;
238 adisconnect disconnect;
239};
240
241
242void print_packet(const char *label, apacket *p);
243
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100244asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245void install_local_socket(asocket *s);
246void remove_socket(asocket *s);
247void close_all_sockets(atransport *t);
248
249#define LOCAL_CLIENT_PREFIX "emulator-"
250
251asocket *create_local_socket(int fd);
252asocket *create_local_service_socket(const char *destination);
253
254asocket *create_remote_socket(unsigned id, atransport *t);
255void connect_to_remote(asocket *s, const char *destination);
256void connect_to_smartsocket(asocket *s);
257
258void fatal(const char *fmt, ...);
259void fatal_errno(const char *fmt, ...);
260
261void handle_packet(apacket *p, atransport *t);
262void send_packet(apacket *p, atransport *t);
263
Alexey Tarasov31664102009-10-22 02:55:00 +1100264void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100265int launch_server(int server_port);
266int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267
268
269/* transports are ref-counted
270** get_device_transport does an acquire on your behalf before returning
271*/
272void init_transport_registration(void);
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700273int list_transports(char *buf, size_t bufsize, int long_listing);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274void update_transports(void);
275
276asocket* create_device_tracker(void);
277
278/* Obtain a transport from the available transports.
279** If state is != CS_ANY, only transports in that state are considered.
280** If serial is non-NULL then only the device with that serial will be chosen.
281** If no suitable transport is found, error is set.
282*/
283atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
284void add_transport_disconnect( atransport* t, adisconnect* dis );
285void remove_transport_disconnect( atransport* t, adisconnect* dis );
286void run_transport_disconnects( atransport* t );
287void kick_transport( atransport* t );
288
289/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100290#if ADB_HOST
291int get_available_local_transport_index();
292#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700293int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400294void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295
296/* for MacOS X cleanup */
297void close_usb_devices();
298
299/* cause new transports to be init'd and added to the list */
Benoit Goby1c45ee92013-03-29 18:22:36 -0700300int register_socket_transport(int s, const char *serial, int port, int local);
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400301
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400302/* these should only be used for the "adb disconnect" command */
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400303void unregister_transport(atransport *t);
Mike Lockwoodcbbe79a2010-05-24 10:44:35 -0400304void unregister_all_tcp_transports();
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400305
Scott Andersone109d262012-04-20 11:21:14 -0700306void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400307
308/* this should only be used for transports with connection_state == CS_NOPERM */
309void unregister_usb_transport(usb_handle *usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400311atransport *find_transport(const char *serial);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100312#if ADB_HOST
313atransport* find_emulator_transport_by_adb_port(int adb_port);
314#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400315
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316int service_to_fd(const char *name);
317#if ADB_HOST
318asocket *host_service_to_socket(const char* name, const char *serial);
319#endif
320
321#if !ADB_HOST
322int init_jdwp(void);
323asocket* create_jdwp_service_socket();
324asocket* create_jdwp_tracker_service_socket();
325int create_jdwp_connection_fd(int jdwp_pid);
326#endif
327
David 'Digit' Turner25258692013-03-21 21:07:42 +0100328int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330#if !ADB_HOST
331void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800332// Allow enable-verity to write to system and vendor block devices
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000333int make_block_device_writable(const char* dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334void remount_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800335void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336#endif
337
338/* packet allocator */
339apacket *get_apacket(void);
340void put_apacket(apacket *p);
341
342int check_header(apacket *p);
343int check_data(apacket *p);
344
leozwangcbf02672014-08-15 09:51:27 -0700345// Define it if you want to dump packets.
346#define DEBUG_PACKETS 0
347
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700348#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349#define print_packet(tag,p) do {} while (0)
350#endif
351
John Michelauc3188332010-09-23 17:08:34 -0500352#if ADB_HOST_ON_TARGET
353/* adb and adbd are coexisting on the target, so use 5038 for adb
354 * to avoid conflicting with adbd's usage of 5037
355 */
356# define DEFAULT_ADB_PORT 5038
357#else
358# define DEFAULT_ADB_PORT 5037
359#endif
360
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100361#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362
Xavier Ducroheta481d092009-05-21 17:47:43 -0700363#define ADB_CLASS 0xff
364#define ADB_SUBCLASS 0x42
365#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700366
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700368void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100370int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371
372/* usb host/client interface */
373void usb_init();
374void usb_cleanup();
375int usb_write(usb_handle *h, const void *data, int len);
376int usb_read(usb_handle *h, void *data, int len);
377int usb_close(usb_handle *h);
378void usb_kick(usb_handle *h);
379
380/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700381#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700383#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385int adb_commandline(int argc, char **argv);
386
387int connection_state(atransport *t);
388
389#define CS_ANY -1
390#define CS_OFFLINE 0
391#define CS_BOOTLOADER 1
392#define CS_DEVICE 2
393#define CS_HOST 3
394#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400395#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800396#define CS_SIDELOAD 6
Benoit Goby77e8e582013-01-15 12:36:47 -0800397#define CS_UNAUTHORIZED 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
399extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700400extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700402typedef enum {
403 SUBPROC_PTY = 0,
404 SUBPROC_RAW = 1,
405} subproc_mode;
406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407#define CHUNK_SIZE (64*1024)
408
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100409#if !ADB_HOST
410#define USB_ADB_PATH "/dev/android_adb"
411
412#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
413#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
414
415#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
416#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
417#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
418#endif
419
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420int sendfailmsg(int fd, const char *reason);
421int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
422
423#endif