blob: 1be83d7ae156d7f4d3ac97659a3852c260a2d402 [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
Dan Albertc7915a32015-05-18 16:46:31 -070023#include <base/macros.h>
24
leozwangd3fc15f2014-07-29 12:50:02 -070025#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080026#include "fdevent.h"
JP Abgrall408fa572011-03-16 15:57:42 -070027
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#define MAX_PAYLOAD 4096
29
30#define A_SYNC 0x434e5953
31#define A_CNXN 0x4e584e43
32#define A_OPEN 0x4e45504f
33#define A_OKAY 0x59414b4f
34#define A_CLSE 0x45534c43
35#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070036#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Dan Albert76649012015-02-24 15:51:19 -080038// ADB protocol version.
39#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Dan Albert76649012015-02-24 15:51:19 -080041// Used for help/version information.
42#define ADB_VERSION_MAJOR 1
43#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Dan Albert76649012015-02-24 15:51:19 -080045// Increment this when we want to force users to start a new adb server.
46#define ADB_SERVER_VERSION 32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Dan Albertc7915a32015-05-18 16:46:31 -070048class atransport;
Elliott Hughes2d4121c2015-04-17 09:47:42 -070049struct usb_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
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
Dan Albertc7915a32015-05-18 16:46:31 -0700157// A transport object models the connection to a remote device or emulator there
158// is one transport per connected device/emulator. A "local transport" connects
159// through TCP (for the emulator), while a "usb transport" through USB (for real
160// 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 connect
164// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700165enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -0700166 kTransportUsb,
167 kTransportLocal,
168 kTransportAny,
169 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700170};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700172#define TOKEN_SIZE 20
173
Dan Albertdcd78a12015-05-18 16:43:57 -0700174enum ConnectionState {
175 kCsAny = -1,
176 kCsOffline = 0,
177 kCsBootloader,
178 kCsDevice,
179 kCsHost,
180 kCsRecovery,
181 kCsNoPerm, // Insufficient permissions to communicate with the device.
182 kCsSideload,
183 kCsUnauthorized,
184};
185
Dan Albertc7915a32015-05-18 16:46:31 -0700186class atransport {
187public:
188 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
189 // historically just a struct, but making the whole thing a more idiomatic
190 // class in one go is a very large change. Given how bad our testing is,
191 // it's better to do this piece by piece.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Dan Albertc7915a32015-05-18 16:46:31 -0700193 atransport() {
194 auth_fde = {};
195 transport_fde = {};
196 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
Dan Albertc7915a32015-05-18 16:46:31 -0700198 virtual ~atransport() {}
199
200 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
201 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
202 void (*close)(atransport* t) = nullptr;
203 void (*kick)(atransport* t) = nullptr;
204
205 int fd = -1;
206 int transport_socket = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 fdevent transport_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700208 int ref_count = 0;
209 uint32_t sync_token = 0;
210 ConnectionState connection_state = kCsOffline;
211 bool online = false;
212 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
Dan Albertc7915a32015-05-18 16:46:31 -0700214 // USB handle or socket fd as needed.
215 usb_handle* usb = nullptr;
216 int sfd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217
Dan Albertc7915a32015-05-18 16:46:31 -0700218 // Used to identify transports for clients.
219 char* serial = nullptr;
220 char* product = nullptr;
221 char* model = nullptr;
222 char* device = nullptr;
223 char* devpath = nullptr;
224 int adb_port = -1; // Use for emulators (local transport)
225 bool kicked = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
Dan Albertc7915a32015-05-18 16:46:31 -0700227 // A list of adisconnect callbacks called when the transport is kicked.
228 adisconnect disconnects = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700229
Dan Albertc7915a32015-05-18 16:46:31 -0700230 void* key = nullptr;
231 unsigned char token[TOKEN_SIZE] = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700232 fdevent auth_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700233 size_t failed_auth_attempts = 0;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700234
235 const char* connection_state_name() const;
Dan Albertc7915a32015-05-18 16:46:31 -0700236
237private:
238 DISALLOW_COPY_AND_ASSIGN(atransport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239};
240
241
242/* A listener is an entity which binds to a local port
243** and, upon receiving a connection on that port, creates
244** an asocket to connect the new local connection to a
245** specific remote service.
246**
247** TODO: some listeners read from the new connection to
248** determine what exact service to connect to on the far
249** side.
250*/
251struct alistener
252{
253 alistener *next;
254 alistener *prev;
255
256 fdevent fde;
257 int fd;
258
Dan Albertbac34742015-02-25 17:51:28 -0800259 char *local_name;
260 char *connect_to;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 atransport *transport;
262 adisconnect disconnect;
263};
264
265
266void print_packet(const char *label, apacket *p);
267
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100268asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269void install_local_socket(asocket *s);
270void remove_socket(asocket *s);
271void close_all_sockets(atransport *t);
272
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273asocket *create_local_socket(int fd);
274asocket *create_local_service_socket(const char *destination);
275
276asocket *create_remote_socket(unsigned id, atransport *t);
277void connect_to_remote(asocket *s, const char *destination);
278void connect_to_smartsocket(asocket *s);
279
280void fatal(const char *fmt, ...);
281void fatal_errno(const char *fmt, ...);
282
283void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284
Alexey Tarasov31664102009-10-22 02:55:00 +1100285void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100286int launch_server(int server_port);
287int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289/* 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);
Dan Albertdcd78a12015-05-18 16:43:57 -0700294void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100296#if ADB_HOST
297atransport* find_emulator_transport_by_adb_port(int adb_port);
298#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400299
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300int service_to_fd(const char *name);
301#if ADB_HOST
302asocket *host_service_to_socket(const char* name, const char *serial);
303#endif
304
305#if !ADB_HOST
306int init_jdwp(void);
307asocket* create_jdwp_service_socket();
308asocket* create_jdwp_tracker_service_socket();
309int create_jdwp_connection_fd(int jdwp_pid);
310#endif
311
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700312int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100313
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314#if !ADB_HOST
315void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800316void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317#endif
318
319/* packet allocator */
320apacket *get_apacket(void);
321void put_apacket(apacket *p);
322
leozwangcbf02672014-08-15 09:51:27 -0700323// Define it if you want to dump packets.
324#define DEBUG_PACKETS 0
325
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700326#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327#define print_packet(tag,p) do {} while (0)
328#endif
329
John Michelauc3188332010-09-23 17:08:34 -0500330#if ADB_HOST_ON_TARGET
331/* adb and adbd are coexisting on the target, so use 5038 for adb
332 * to avoid conflicting with adbd's usage of 5037
333 */
334# define DEFAULT_ADB_PORT 5038
335#else
336# define DEFAULT_ADB_PORT 5037
337#endif
338
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100339#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340
Xavier Ducroheta481d092009-05-21 17:47:43 -0700341#define ADB_CLASS 0xff
342#define ADB_SUBCLASS 0x42
343#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700346void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100348int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349
350/* usb host/client interface */
351void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352int usb_write(usb_handle *h, const void *data, int len);
353int usb_read(usb_handle *h, void *data, int len);
354int usb_close(usb_handle *h);
355void usb_kick(usb_handle *h);
356
357/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700358#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700360#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361
Dan Albertbac34742015-02-25 17:51:28 -0800362int adb_commandline(int argc, const char **argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363
Dan Albertdcd78a12015-05-18 16:43:57 -0700364ConnectionState connection_state(atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365
Dan Albertbd0b7502015-02-18 18:22:45 -0800366extern const char *adb_device_banner;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367extern int HOST;
JP Abgrall408fa572011-03-16 15:57:42 -0700368extern int SHELL_EXIT_NOTIFY_FD;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369
370#define CHUNK_SIZE (64*1024)
371
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100372#if !ADB_HOST
373#define USB_ADB_PATH "/dev/android_adb"
374
375#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
376#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
377
378#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
379#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
380#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
381#endif
382
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700383int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Dan Albertba3a2512015-02-18 17:47:33 -0800385void handle_online(atransport *t);
386void handle_offline(atransport *t);
387
388void send_connect(atransport *t);
389
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390#endif