blob: b8c6156a7a2575e493705243366a7cb7cae1536e [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>
Dan Albert76649012015-02-24 15:51:19 -080021#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
leozwangd3fc15f2014-07-29 12:50:02 -070023#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080024#include "fdevent.h"
JP Abgrall408fa572011-03-16 15:57:42 -070025
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
Dan Albert76649012015-02-24 15:51:19 -080036// ADB protocol version.
37#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Dan Albert76649012015-02-24 15:51:19 -080039// Used for help/version information.
40#define ADB_VERSION_MAJOR 1
41#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Dan Albert76649012015-02-24 15:51:19 -080043// Increment this when we want to force users to start a new adb server.
44#define ADB_SERVER_VERSION 32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Elliott Hughes2d4121c2015-04-17 09:47:42 -070046struct atransport;
47struct usb_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
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
Benoit Gobyf366b362012-03-16 14:50:07 -070089 /* flag: quit adbd when both ends close the
90 ** local service socket
91 */
92 int exit_on_close;
93
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 /* the asocket we are connected to
95 */
96
97 asocket *peer;
98
99 /* For local asockets, the fde is used to bind
100 ** us to our fd event system. For remote asockets
101 ** these fields are not used.
102 */
103 fdevent fde;
104 int fd;
105
106 /* queue of apackets waiting to be written
107 */
108 apacket *pkt_first;
109 apacket *pkt_last;
110
111 /* enqueue is called by our peer when it has data
112 ** for us. It should return 0 if we can accept more
113 ** data or 1 if not. If we return 1, we must call
114 ** peer->ready() when we once again are ready to
115 ** receive data.
116 */
117 int (*enqueue)(asocket *s, apacket *pkt);
118
119 /* ready is called by the peer when it is ready for
120 ** us to send data via enqueue again
121 */
122 void (*ready)(asocket *s);
123
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100124 /* shutdown is called by the peer before it goes away.
125 ** the socket should not do any further calls on its peer.
126 ** Always followed by a call to close. Optional, i.e. can be NULL.
127 */
128 void (*shutdown)(asocket *s);
129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 /* close is called by the peer when it has gone away.
131 ** we are not allowed to make any further calls on the
132 ** peer once our close method is called.
133 */
134 void (*close)(asocket *s);
135
Benoit Goby9470c2f2013-02-20 15:04:53 -0800136 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 atransport *transport;
138};
139
140
141/* the adisconnect structure is used to record a callback that
142** will be called whenever a transport is disconnected (e.g. by the user)
143** this should be used to cleanup objects that depend on the
144** transport (e.g. remote sockets, listeners, etc...)
145*/
146struct adisconnect
147{
148 void (*func)(void* opaque, atransport* t);
149 void* opaque;
150 adisconnect* next;
151 adisconnect* prev;
152};
153
154
155/* a transport object models the connection to a remote device or emulator
156** there is one transport per connected device/emulator. a "local transport"
157** connects through TCP (for the emulator), while a "usb transport" through
158** USB (for real devices)
159**
160** note that kTransportHost doesn't really correspond to a real transport
161** object, it's a special value used to indicate that a client wants to
162** connect to a service implemented within the ADB server itself.
163*/
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700164enum transport_type {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 kTransportUsb,
166 kTransportLocal,
167 kTransportAny,
168 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700169};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700171#define TOKEN_SIZE 20
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173struct atransport
174{
175 atransport *next;
176 atransport *prev;
177
178 int (*read_from_remote)(apacket *p, atransport *t);
179 int (*write_to_remote)(apacket *p, atransport *t);
180 void (*close)(atransport *t);
181 void (*kick)(atransport *t);
182
183 int fd;
184 int transport_socket;
185 fdevent transport_fde;
186 int ref_count;
187 unsigned sync_token;
188 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700189 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 transport_type type;
191
192 /* usb handle or socket fd as needed */
193 usb_handle *usb;
194 int sfd;
195
196 /* used to identify transports for clients */
197 char *serial;
198 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700199 char *model;
200 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700201 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100202 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
204 /* a list of adisconnect callbacks called when the transport is kicked */
205 int kicked;
206 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700207
208 void *key;
209 unsigned char token[TOKEN_SIZE];
210 fdevent auth_fde;
211 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212};
213
214
215/* A listener is an entity which binds to a local port
216** and, upon receiving a connection on that port, creates
217** an asocket to connect the new local connection to a
218** specific remote service.
219**
220** TODO: some listeners read from the new connection to
221** determine what exact service to connect to on the far
222** side.
223*/
224struct alistener
225{
226 alistener *next;
227 alistener *prev;
228
229 fdevent fde;
230 int fd;
231
Dan Albertbac34742015-02-25 17:51:28 -0800232 char *local_name;
233 char *connect_to;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 atransport *transport;
235 adisconnect disconnect;
236};
237
238
239void print_packet(const char *label, apacket *p);
240
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100241asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242void install_local_socket(asocket *s);
243void remove_socket(asocket *s);
244void close_all_sockets(atransport *t);
245
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246asocket *create_local_socket(int fd);
247asocket *create_local_service_socket(const char *destination);
248
249asocket *create_remote_socket(unsigned id, atransport *t);
250void connect_to_remote(asocket *s, const char *destination);
251void connect_to_smartsocket(asocket *s);
252
253void fatal(const char *fmt, ...);
254void fatal_errno(const char *fmt, ...);
255
256void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
Alexey Tarasov31664102009-10-22 02:55:00 +1100258void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100259int launch_server(int server_port);
260int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100263#if ADB_HOST
264int get_available_local_transport_index();
265#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700266int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400267void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100269#if ADB_HOST
270atransport* find_emulator_transport_by_adb_port(int adb_port);
271#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400272
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273int service_to_fd(const char *name);
274#if ADB_HOST
275asocket *host_service_to_socket(const char* name, const char *serial);
276#endif
277
278#if !ADB_HOST
279int init_jdwp(void);
280asocket* create_jdwp_service_socket();
281asocket* create_jdwp_tracker_service_socket();
282int create_jdwp_connection_fd(int jdwp_pid);
283#endif
284
David 'Digit' Turner25258692013-03-21 21:07:42 +0100285int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
286
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287#if !ADB_HOST
288void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800289void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290#endif
291
292/* packet allocator */
293apacket *get_apacket(void);
294void put_apacket(apacket *p);
295
leozwangcbf02672014-08-15 09:51:27 -0700296// Define it if you want to dump packets.
297#define DEBUG_PACKETS 0
298
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700299#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300#define print_packet(tag,p) do {} while (0)
301#endif
302
John Michelauc3188332010-09-23 17:08:34 -0500303#if ADB_HOST_ON_TARGET
304/* adb and adbd are coexisting on the target, so use 5038 for adb
305 * to avoid conflicting with adbd's usage of 5037
306 */
307# define DEFAULT_ADB_PORT 5038
308#else
309# define DEFAULT_ADB_PORT 5037
310#endif
311
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100312#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313
Xavier Ducroheta481d092009-05-21 17:47:43 -0700314#define ADB_CLASS 0xff
315#define ADB_SUBCLASS 0x42
316#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700317
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700319void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100321int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
323/* usb host/client interface */
324void usb_init();
325void usb_cleanup();
326int usb_write(usb_handle *h, const void *data, int len);
327int usb_read(usb_handle *h, void *data, int len);
328int usb_close(usb_handle *h);
329void usb_kick(usb_handle *h);
330
331/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700332#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700334#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335
Dan Albertbac34742015-02-25 17:51:28 -0800336int adb_commandline(int argc, const char **argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337
338int connection_state(atransport *t);
339
340#define CS_ANY -1
341#define CS_OFFLINE 0
342#define CS_BOOTLOADER 1
343#define CS_DEVICE 2
344#define CS_HOST 3
345#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400346#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800347#define CS_SIDELOAD 6
Benoit Goby77e8e582013-01-15 12:36:47 -0800348#define CS_UNAUTHORIZED 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
Dan Albertbd0b7502015-02-18 18:22:45 -0800350extern const char *adb_device_banner;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700352extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700354enum subproc_mode {
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700355 SUBPROC_PTY = 0,
356 SUBPROC_RAW = 1,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700357} ;
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700358
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359#define CHUNK_SIZE (64*1024)
360
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100361#if !ADB_HOST
362#define USB_ADB_PATH "/dev/android_adb"
363
364#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
365#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
366
367#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
368#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
369#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
370#endif
371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372int sendfailmsg(int fd, const char *reason);
373int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
374
Dan Albertba3a2512015-02-18 17:47:33 -0800375void handle_online(atransport *t);
376void handle_offline(atransport *t);
377
378void send_connect(atransport *t);
379
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380#endif