blob: 4922040a52a45ac84a2e4c11cc9c696355a694b8 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Albertb302d122015-02-24 15:51:19 -080021#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022
Elliott Hughes43df1092015-07-23 17:12:58 -070023#include <string>
24
Dan Albertbe8e54b2015-05-18 13:06:53 -070025#include <base/macros.h>
26
leozwangf25a6a42014-07-29 12:50:02 -070027#include "adb_trace.h"
Dan Albertbbbbea62014-11-24 23:34:35 -080028#include "fdevent.h"
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070029
Tamas Berghammera1c60c02015-07-13 19:12:28 +010030constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
31constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
32constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080033
34#define A_SYNC 0x434e5953
35#define A_CNXN 0x4e584e43
36#define A_OPEN 0x4e45504f
37#define A_OKAY 0x59414b4f
38#define A_CLSE 0x45534c43
39#define A_WRTE 0x45545257
Benoit Goby2cc19e42012-04-12 12:23:49 -070040#define A_AUTH 0x48545541
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080041
Dan Albertb302d122015-02-24 15:51:19 -080042// ADB protocol version.
43#define A_VERSION 0x01000000
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080044
Dan Albertb302d122015-02-24 15:51:19 -080045// Used for help/version information.
46#define ADB_VERSION_MAJOR 1
47#define ADB_VERSION_MINOR 0
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048
Elliott Hughesb9f01642015-08-12 08:32:10 -070049std::string adb_version();
50
Dan Albertb302d122015-02-24 15:51:19 -080051// Increment this when we want to force users to start a new adb server.
52#define ADB_SERVER_VERSION 32
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
Dan Albertecce5032015-05-18 16:46:31 -070054class atransport;
Elliott Hughesfe7ff812015-04-17 09:47:42 -070055struct usb_handle;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056
57struct amessage {
58 unsigned command; /* command identifier constant */
59 unsigned arg0; /* first argument */
60 unsigned arg1; /* second argument */
61 unsigned data_length; /* length of payload (0 is allowed) */
62 unsigned data_check; /* checksum of data payload */
63 unsigned magic; /* command ^ 0xffffffff */
64};
65
66struct apacket
67{
68 apacket *next;
69
70 unsigned len;
71 unsigned char *ptr;
72
73 amessage msg;
74 unsigned char data[MAX_PAYLOAD];
75};
76
77/* An asocket represents one half of a connection between a local and
78** remote entity. A local asocket is bound to a file descriptor. A
79** remote asocket is bound to the protocol engine.
80*/
81struct asocket {
82 /* chain pointers for the local/remote list of
83 ** asockets that this asocket lives in
84 */
85 asocket *next;
86 asocket *prev;
87
88 /* the unique identifier for this asocket
89 */
90 unsigned id;
91
92 /* flag: set when the socket's peer has closed
93 ** but packets are still queued for delivery
94 */
95 int closing;
96
Benoit Goby88468f32012-03-16 14:50:07 -070097 /* flag: quit adbd when both ends close the
98 ** local service socket
99 */
100 int exit_on_close;
101
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 /* the asocket we are connected to
103 */
104
105 asocket *peer;
106
107 /* For local asockets, the fde is used to bind
108 ** us to our fd event system. For remote asockets
109 ** these fields are not used.
110 */
111 fdevent fde;
112 int fd;
113
114 /* queue of apackets waiting to be written
115 */
116 apacket *pkt_first;
117 apacket *pkt_last;
118
119 /* enqueue is called by our peer when it has data
120 ** for us. It should return 0 if we can accept more
121 ** data or 1 if not. If we return 1, we must call
122 ** peer->ready() when we once again are ready to
123 ** receive data.
124 */
125 int (*enqueue)(asocket *s, apacket *pkt);
126
127 /* ready is called by the peer when it is ready for
128 ** us to send data via enqueue again
129 */
130 void (*ready)(asocket *s);
131
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100132 /* shutdown is called by the peer before it goes away.
133 ** the socket should not do any further calls on its peer.
134 ** Always followed by a call to close. Optional, i.e. can be NULL.
135 */
136 void (*shutdown)(asocket *s);
137
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800138 /* close is called by the peer when it has gone away.
139 ** we are not allowed to make any further calls on the
140 ** peer once our close method is called.
141 */
142 void (*close)(asocket *s);
143
Benoit Goby12dc3692013-02-20 15:04:53 -0800144 /* A socket is bound to atransport */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145 atransport *transport;
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100146
147 size_t get_max_payload() const;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148};
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;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160};
161
162
Dan Albertecce5032015-05-18 16:46:31 -0700163// A transport object models the connection to a remote device or emulator there
164// is one transport per connected device/emulator. A "local transport" connects
165// through TCP (for the emulator), while a "usb transport" through USB (for real
166// devices).
167//
168// Note that kTransportHost doesn't really correspond to a real transport
169// object, it's a special value used to indicate that a client wants to connect
170// to a service implemented within the ADB server itself.
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700171enum TransportType {
Dan Albertecce5032015-05-18 16:46:31 -0700172 kTransportUsb,
173 kTransportLocal,
174 kTransportAny,
175 kTransportHost,
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700176};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177
Benoit Goby2cc19e42012-04-12 12:23:49 -0700178#define TOKEN_SIZE 20
179
Dan Albert9a50f4c2015-05-18 16:43:57 -0700180enum ConnectionState {
181 kCsAny = -1,
182 kCsOffline = 0,
183 kCsBootloader,
184 kCsDevice,
185 kCsHost,
186 kCsRecovery,
187 kCsNoPerm, // Insufficient permissions to communicate with the device.
188 kCsSideload,
189 kCsUnauthorized,
190};
191
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192/* A listener is an entity which binds to a local port
193** and, upon receiving a connection on that port, creates
194** an asocket to connect the new local connection to a
195** specific remote service.
196**
197** TODO: some listeners read from the new connection to
198** determine what exact service to connect to on the far
199** side.
200*/
201struct alistener
202{
203 alistener *next;
204 alistener *prev;
205
206 fdevent fde;
207 int fd;
208
Dan Albertf30d73c2015-02-25 17:51:28 -0800209 char *local_name;
210 char *connect_to;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800211 atransport *transport;
212 adisconnect disconnect;
213};
214
215
216void print_packet(const char *label, apacket *p);
217
David 'Digit' Turnere92344d2013-12-13 14:09:44 +0100218asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800219void install_local_socket(asocket *s);
220void remove_socket(asocket *s);
221void close_all_sockets(atransport *t);
222
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800223asocket *create_local_socket(int fd);
David Pursell8da19a42015-08-31 10:42:13 -0700224asocket *create_local_service_socket(const char* destination,
225 const atransport* transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800226
227asocket *create_remote_socket(unsigned id, atransport *t);
228void connect_to_remote(asocket *s, const char *destination);
229void connect_to_smartsocket(asocket *s);
230
Elliott Hughesdd01a372015-08-28 19:13:10 -0700231void fatal(const char *fmt, ...) __attribute__((noreturn));
232void fatal_errno(const char *fmt, ...) __attribute__((noreturn));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800233
234void handle_packet(apacket *p, atransport *t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800235
Alexey Tarasov857f17a2009-10-22 02:55:00 +1100236void get_my_path(char *s, size_t maxLen);
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100237int launch_server(int server_port);
Siva Velusamya55dbd82015-08-07 10:10:29 -0700238int adb_main(int is_daemon, int server_port, int ack_reply_fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800239
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240/* initialize a transport object's func pointers and state */
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100241#if ADB_HOST
242int get_available_local_transport_index();
243#endif
Mike Lockwoodb51ae572009-08-24 15:58:40 -0700244int init_socket_transport(atransport *t, int s, int port, int local);
Dan Albert9a50f4c2015-05-18 16:43:57 -0700245void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800246
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100247#if ADB_HOST
248atransport* find_emulator_transport_by_adb_port(int adb_port);
249#endif
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400250
David Pursell8da19a42015-08-31 10:42:13 -0700251int service_to_fd(const char* name, const atransport* transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800252#if ADB_HOST
253asocket *host_service_to_socket(const char* name, const char *serial);
254#endif
255
256#if !ADB_HOST
257int init_jdwp(void);
258asocket* create_jdwp_service_socket();
259asocket* create_jdwp_tracker_service_socket();
260int create_jdwp_connection_fd(int jdwp_pid);
261#endif
262
Elliott Hughes9d07fee2015-05-07 21:38:41 -0700263int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100264
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800265#if !ADB_HOST
266void framebuffer_service(int fd, void *cookie);
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800267void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268#endif
269
270/* packet allocator */
271apacket *get_apacket(void);
272void put_apacket(apacket *p);
273
leozwang1be54622014-08-15 09:51:27 -0700274// Define it if you want to dump packets.
275#define DEBUG_PACKETS 0
276
Benoit Goby2cc19e42012-04-12 12:23:49 -0700277#if !DEBUG_PACKETS
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278#define print_packet(tag,p) do {} while (0)
279#endif
280
John Michelau87337522010-09-23 17:08:34 -0500281#if ADB_HOST_ON_TARGET
282/* adb and adbd are coexisting on the target, so use 5038 for adb
283 * to avoid conflicting with adbd's usage of 5037
284 */
285# define DEFAULT_ADB_PORT 5038
286#else
287# define DEFAULT_ADB_PORT 5037
288#endif
289
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100290#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291
Xavier Ducrohet30a40332009-05-21 17:47:43 -0700292#define ADB_CLASS 0xff
293#define ADB_SUBCLASS 0x42
294#define ADB_PROTOCOL 0x1
Dima Zavin3e824912009-05-08 18:25:58 -0700295
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296
Mike Lockwooda8b38752009-08-26 12:50:22 -0700297void local_init(int port);
Elliott Hughes43df1092015-07-23 17:12:58 -0700298void local_connect(int port);
299int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300
301/* usb host/client interface */
302void usb_init();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303int usb_write(usb_handle *h, const void *data, int len);
304int usb_read(usb_handle *h, void *data, int len);
305int usb_close(usb_handle *h);
306void usb_kick(usb_handle *h);
307
308/* used for USB device detection */
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -0700309#if ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800310int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducrohet2ef5fc22009-05-20 17:33:53 -0700311#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312
Dan Albertf30d73c2015-02-25 17:51:28 -0800313int adb_commandline(int argc, const char **argv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800314
Dan Albert9a50f4c2015-05-18 16:43:57 -0700315ConnectionState connection_state(atransport *t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800316
Dan Albertbe8e54b2015-05-18 13:06:53 -0700317extern const char* adb_device_banner;
318
Alex Vallée436fa6b2015-07-17 15:30:59 -0400319#if !ADB_HOST
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700320extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée436fa6b2015-07-17 15:30:59 -0400321#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322
323#define CHUNK_SIZE (64*1024)
324
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100325#if !ADB_HOST
326#define USB_ADB_PATH "/dev/android_adb"
327
328#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
329#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
330
331#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
332#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
333#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
334#endif
335
Elliott Hughes9d07fee2015-05-07 21:38:41 -0700336int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337
Dan Albert056ad0e2015-02-18 17:47:33 -0800338void handle_online(atransport *t);
339void handle_offline(atransport *t);
340
341void send_connect(atransport *t);
342
Dan Albertbe8e54b2015-05-18 13:06:53 -0700343void parse_banner(const std::string&, atransport* t);
344
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800345#endif