blob: 9a68871adaaf7ccf27e94923a6ee7d5c0fc8f6a5 [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
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
Dan Albert76649012015-02-24 15:51:19 -080040// ADB protocol version.
41#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Dan Albert76649012015-02-24 15:51:19 -080043// Used for help/version information.
44#define ADB_VERSION_MAJOR 1
45#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Dan Albert76649012015-02-24 15:51:19 -080047// Increment this when we want to force users to start a new adb server.
48#define ADB_SERVER_VERSION 32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50typedef struct amessage amessage;
51typedef struct apacket apacket;
52typedef struct asocket asocket;
53typedef struct alistener alistener;
54typedef struct aservice aservice;
55typedef struct atransport atransport;
56typedef struct adisconnect adisconnect;
57typedef struct usb_handle usb_handle;
58
59struct amessage {
60 unsigned command; /* command identifier constant */
61 unsigned arg0; /* first argument */
62 unsigned arg1; /* second argument */
63 unsigned data_length; /* length of payload (0 is allowed) */
64 unsigned data_check; /* checksum of data payload */
65 unsigned magic; /* command ^ 0xffffffff */
66};
67
68struct apacket
69{
70 apacket *next;
71
72 unsigned len;
73 unsigned char *ptr;
74
75 amessage msg;
76 unsigned char data[MAX_PAYLOAD];
77};
78
79/* An asocket represents one half of a connection between a local and
80** remote entity. A local asocket is bound to a file descriptor. A
81** remote asocket is bound to the protocol engine.
82*/
83struct asocket {
84 /* chain pointers for the local/remote list of
85 ** asockets that this asocket lives in
86 */
87 asocket *next;
88 asocket *prev;
89
90 /* the unique identifier for this asocket
91 */
92 unsigned id;
93
94 /* flag: set when the socket's peer has closed
95 ** but packets are still queued for delivery
96 */
97 int closing;
98
Benoit Gobyf366b362012-03-16 14:50:07 -070099 /* flag: quit adbd when both ends close the
100 ** local service socket
101 */
102 int exit_on_close;
103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 /* the asocket we are connected to
105 */
106
107 asocket *peer;
108
109 /* For local asockets, the fde is used to bind
110 ** us to our fd event system. For remote asockets
111 ** these fields are not used.
112 */
113 fdevent fde;
114 int fd;
115
116 /* queue of apackets waiting to be written
117 */
118 apacket *pkt_first;
119 apacket *pkt_last;
120
121 /* enqueue is called by our peer when it has data
122 ** for us. It should return 0 if we can accept more
123 ** data or 1 if not. If we return 1, we must call
124 ** peer->ready() when we once again are ready to
125 ** receive data.
126 */
127 int (*enqueue)(asocket *s, apacket *pkt);
128
129 /* ready is called by the peer when it is ready for
130 ** us to send data via enqueue again
131 */
132 void (*ready)(asocket *s);
133
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100134 /* shutdown is called by the peer before it goes away.
135 ** the socket should not do any further calls on its peer.
136 ** Always followed by a call to close. Optional, i.e. can be NULL.
137 */
138 void (*shutdown)(asocket *s);
139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 /* close is called by the peer when it has gone away.
141 ** we are not allowed to make any further calls on the
142 ** peer once our close method is called.
143 */
144 void (*close)(asocket *s);
145
Benoit Goby9470c2f2013-02-20 15:04:53 -0800146 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 atransport *transport;
148};
149
150
151/* the adisconnect structure is used to record a callback that
152** will be called whenever a transport is disconnected (e.g. by the user)
153** this should be used to cleanup objects that depend on the
154** transport (e.g. remote sockets, listeners, etc...)
155*/
156struct adisconnect
157{
158 void (*func)(void* opaque, atransport* t);
159 void* opaque;
160 adisconnect* next;
161 adisconnect* prev;
162};
163
164
165/* a transport object models the connection to a remote device or emulator
166** there is one transport per connected device/emulator. a "local transport"
167** connects through TCP (for the emulator), while a "usb transport" through
168** USB (for real devices)
169**
170** note that kTransportHost doesn't really correspond to a real transport
171** object, it's a special value used to indicate that a client wants to
172** connect to a service implemented within the ADB server itself.
173*/
174typedef enum transport_type {
175 kTransportUsb,
176 kTransportLocal,
177 kTransportAny,
178 kTransportHost,
179} transport_type;
180
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700181#define TOKEN_SIZE 20
182
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183struct atransport
184{
185 atransport *next;
186 atransport *prev;
187
188 int (*read_from_remote)(apacket *p, atransport *t);
189 int (*write_to_remote)(apacket *p, atransport *t);
190 void (*close)(atransport *t);
191 void (*kick)(atransport *t);
192
193 int fd;
194 int transport_socket;
195 fdevent transport_fde;
196 int ref_count;
197 unsigned sync_token;
198 int connection_state;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700199 int online;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 transport_type type;
201
202 /* usb handle or socket fd as needed */
203 usb_handle *usb;
204 int sfd;
205
206 /* used to identify transports for clients */
207 char *serial;
208 char *product;
Scott Andersone82c2db2012-05-25 14:10:02 -0700209 char *model;
210 char *device;
Scott Andersone109d262012-04-20 11:21:14 -0700211 char *devpath;
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100212 int adb_port; // Use for emulators (local transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
214 /* a list of adisconnect callbacks called when the transport is kicked */
215 int kicked;
216 adisconnect disconnects;
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700217
218 void *key;
219 unsigned char token[TOKEN_SIZE];
220 fdevent auth_fde;
221 unsigned failed_auth_attempts;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222};
223
224
225/* A listener is an entity which binds to a local port
226** and, upon receiving a connection on that port, creates
227** an asocket to connect the new local connection to a
228** specific remote service.
229**
230** TODO: some listeners read from the new connection to
231** determine what exact service to connect to on the far
232** side.
233*/
234struct alistener
235{
236 alistener *next;
237 alistener *prev;
238
239 fdevent fde;
240 int fd;
241
242 const char *local_name;
243 const char *connect_to;
244 atransport *transport;
245 adisconnect disconnect;
246};
247
248
249void print_packet(const char *label, apacket *p);
250
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100251asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252void install_local_socket(asocket *s);
253void remove_socket(asocket *s);
254void close_all_sockets(atransport *t);
255
256#define LOCAL_CLIENT_PREFIX "emulator-"
257
258asocket *create_local_socket(int fd);
259asocket *create_local_service_socket(const char *destination);
260
261asocket *create_remote_socket(unsigned id, atransport *t);
262void connect_to_remote(asocket *s, const char *destination);
263void connect_to_smartsocket(asocket *s);
264
265void fatal(const char *fmt, ...);
266void fatal_errno(const char *fmt, ...);
267
268void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
Alexey Tarasov31664102009-10-22 02:55:00 +1100270void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100271int launch_server(int server_port);
272int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100275#if ADB_HOST
276int get_available_local_transport_index();
277#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700278int init_socket_transport(atransport *t, int s, int port, int local);
Mike Lockwood95b837d2009-08-08 12:37:44 -0400279void init_usb_transport(atransport *t, usb_handle *usb, int state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100281#if ADB_HOST
282atransport* find_emulator_transport_by_adb_port(int adb_port);
283#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400284
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285int service_to_fd(const char *name);
286#if ADB_HOST
287asocket *host_service_to_socket(const char* name, const char *serial);
288#endif
289
290#if !ADB_HOST
291int init_jdwp(void);
292asocket* create_jdwp_service_socket();
293asocket* create_jdwp_tracker_service_socket();
294int create_jdwp_connection_fd(int jdwp_pid);
295#endif
296
David 'Digit' Turner25258692013-03-21 21:07:42 +0100297int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
298
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299#if !ADB_HOST
300void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800301// Allow enable-verity to write to system and vendor block devices
Sami Tolvanen13449cd2015-01-02 13:30:50 +0000302int make_block_device_writable(const char* dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303void remount_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800304void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305#endif
306
307/* packet allocator */
308apacket *get_apacket(void);
309void put_apacket(apacket *p);
310
leozwangcbf02672014-08-15 09:51:27 -0700311// Define it if you want to dump packets.
312#define DEBUG_PACKETS 0
313
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700314#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315#define print_packet(tag,p) do {} while (0)
316#endif
317
John Michelauc3188332010-09-23 17:08:34 -0500318#if ADB_HOST_ON_TARGET
319/* adb and adbd are coexisting on the target, so use 5038 for adb
320 * to avoid conflicting with adbd's usage of 5037
321 */
322# define DEFAULT_ADB_PORT 5038
323#else
324# define DEFAULT_ADB_PORT 5037
325#endif
326
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100327#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328
Xavier Ducroheta481d092009-05-21 17:47:43 -0700329#define ADB_CLASS 0xff
330#define ADB_SUBCLASS 0x42
331#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700332
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700334void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100336int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337
338/* usb host/client interface */
339void usb_init();
340void usb_cleanup();
341int usb_write(usb_handle *h, const void *data, int len);
342int usb_read(usb_handle *h, void *data, int len);
343int usb_close(usb_handle *h);
344void usb_kick(usb_handle *h);
345
346/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700347#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700349#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351int adb_commandline(int argc, char **argv);
352
353int connection_state(atransport *t);
354
355#define CS_ANY -1
356#define CS_OFFLINE 0
357#define CS_BOOTLOADER 1
358#define CS_DEVICE 2
359#define CS_HOST 3
360#define CS_RECOVERY 4
Mike Lockwood95b837d2009-08-08 12:37:44 -0400361#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
Doug Zongker447f0612012-01-09 14:54:53 -0800362#define CS_SIDELOAD 6
Benoit Goby77e8e582013-01-15 12:36:47 -0800363#define CS_UNAUTHORIZED 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364
Dan Albertbd0b7502015-02-18 18:22:45 -0800365extern const char *adb_device_banner;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700367extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
Jeff Sharkey5d9d4342014-05-26 18:30:43 -0700369typedef enum {
370 SUBPROC_PTY = 0,
371 SUBPROC_RAW = 1,
372} subproc_mode;
373
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374#define CHUNK_SIZE (64*1024)
375
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100376#if !ADB_HOST
377#define USB_ADB_PATH "/dev/android_adb"
378
379#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
380#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
381
382#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
383#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
384#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
385#endif
386
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387int sendfailmsg(int fd, const char *reason);
388int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
389
Dan Albertba3a2512015-02-18 17:47:33 -0800390void handle_online(atransport *t);
391void handle_offline(atransport *t);
392
393void send_connect(atransport *t);
394
Dan Albert818fb4b2015-02-18 00:18:25 -0800395#ifdef __cplusplus
396}
397#endif
398
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399#endif