blob: 357be513fcd9c28d585b88d909dc3d25d0c6f159 [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
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010028constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
29constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
30constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
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
Dan Albertc7915a32015-05-18 16:46:31 -070050class atransport;
Elliott Hughes2d4121c2015-04-17 09:47:42 -070051struct usb_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
53struct amessage {
54 unsigned command; /* command identifier constant */
55 unsigned arg0; /* first argument */
56 unsigned arg1; /* second argument */
57 unsigned data_length; /* length of payload (0 is allowed) */
58 unsigned data_check; /* checksum of data payload */
59 unsigned magic; /* command ^ 0xffffffff */
60};
61
62struct apacket
63{
64 apacket *next;
65
66 unsigned len;
67 unsigned char *ptr;
68
69 amessage msg;
70 unsigned char data[MAX_PAYLOAD];
71};
72
73/* An asocket represents one half of a connection between a local and
74** remote entity. A local asocket is bound to a file descriptor. A
75** remote asocket is bound to the protocol engine.
76*/
77struct asocket {
78 /* chain pointers for the local/remote list of
79 ** asockets that this asocket lives in
80 */
81 asocket *next;
82 asocket *prev;
83
84 /* the unique identifier for this asocket
85 */
86 unsigned id;
87
88 /* flag: set when the socket's peer has closed
89 ** but packets are still queued for delivery
90 */
91 int closing;
92
Benoit Gobyf366b362012-03-16 14:50:07 -070093 /* flag: quit adbd when both ends close the
94 ** local service socket
95 */
96 int exit_on_close;
97
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 /* the asocket we are connected to
99 */
100
101 asocket *peer;
102
103 /* For local asockets, the fde is used to bind
104 ** us to our fd event system. For remote asockets
105 ** these fields are not used.
106 */
107 fdevent fde;
108 int fd;
109
110 /* queue of apackets waiting to be written
111 */
112 apacket *pkt_first;
113 apacket *pkt_last;
114
115 /* enqueue is called by our peer when it has data
116 ** for us. It should return 0 if we can accept more
117 ** data or 1 if not. If we return 1, we must call
118 ** peer->ready() when we once again are ready to
119 ** receive data.
120 */
121 int (*enqueue)(asocket *s, apacket *pkt);
122
123 /* ready is called by the peer when it is ready for
124 ** us to send data via enqueue again
125 */
126 void (*ready)(asocket *s);
127
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100128 /* shutdown is called by the peer before it goes away.
129 ** the socket should not do any further calls on its peer.
130 ** Always followed by a call to close. Optional, i.e. can be NULL.
131 */
132 void (*shutdown)(asocket *s);
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 /* close is called by the peer when it has gone away.
135 ** we are not allowed to make any further calls on the
136 ** peer once our close method is called.
137 */
138 void (*close)(asocket *s);
139
Benoit Goby9470c2f2013-02-20 15:04:53 -0800140 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 atransport *transport;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100142
143 size_t get_max_payload() const;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144};
145
146
147/* the adisconnect structure is used to record a callback that
148** will be called whenever a transport is disconnected (e.g. by the user)
149** this should be used to cleanup objects that depend on the
150** transport (e.g. remote sockets, listeners, etc...)
151*/
152struct adisconnect
153{
154 void (*func)(void* opaque, atransport* t);
155 void* opaque;
156 adisconnect* next;
157 adisconnect* prev;
158};
159
160
Dan Albertc7915a32015-05-18 16:46:31 -0700161// A transport object models the connection to a remote device or emulator there
162// is one transport per connected device/emulator. A "local transport" connects
163// through TCP (for the emulator), while a "usb transport" through USB (for real
164// devices).
165//
166// Note that kTransportHost doesn't really correspond to a real transport
167// object, it's a special value used to indicate that a client wants to connect
168// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700169enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -0700170 kTransportUsb,
171 kTransportLocal,
172 kTransportAny,
173 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700174};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700176#define TOKEN_SIZE 20
177
Dan Albertdcd78a12015-05-18 16:43:57 -0700178enum ConnectionState {
179 kCsAny = -1,
180 kCsOffline = 0,
181 kCsBootloader,
182 kCsDevice,
183 kCsHost,
184 kCsRecovery,
185 kCsNoPerm, // Insufficient permissions to communicate with the device.
186 kCsSideload,
187 kCsUnauthorized,
188};
189
Dan Albertc7915a32015-05-18 16:46:31 -0700190class atransport {
191public:
192 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
193 // historically just a struct, but making the whole thing a more idiomatic
194 // class in one go is a very large change. Given how bad our testing is,
195 // it's better to do this piece by piece.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Dan Albertc7915a32015-05-18 16:46:31 -0700197 atransport() {
198 auth_fde = {};
199 transport_fde = {};
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100200 protocol_version = A_VERSION;
201 max_payload = MAX_PAYLOAD;
Dan Albertc7915a32015-05-18 16:46:31 -0700202 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Dan Albertc7915a32015-05-18 16:46:31 -0700204 virtual ~atransport() {}
205
206 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
207 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
208 void (*close)(atransport* t) = nullptr;
209 void (*kick)(atransport* t) = nullptr;
210
211 int fd = -1;
212 int transport_socket = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 fdevent transport_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700214 int ref_count = 0;
215 uint32_t sync_token = 0;
216 ConnectionState connection_state = kCsOffline;
217 bool online = false;
218 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
Dan Albertc7915a32015-05-18 16:46:31 -0700220 // USB handle or socket fd as needed.
221 usb_handle* usb = nullptr;
222 int sfd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
Dan Albertc7915a32015-05-18 16:46:31 -0700224 // Used to identify transports for clients.
225 char* serial = nullptr;
226 char* product = nullptr;
227 char* model = nullptr;
228 char* device = nullptr;
229 char* devpath = nullptr;
230 int adb_port = -1; // Use for emulators (local transport)
231 bool kicked = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232
Dan Albertc7915a32015-05-18 16:46:31 -0700233 // A list of adisconnect callbacks called when the transport is kicked.
234 adisconnect disconnects = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700235
Dan Albertc7915a32015-05-18 16:46:31 -0700236 void* key = nullptr;
237 unsigned char token[TOKEN_SIZE] = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700238 fdevent auth_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700239 size_t failed_auth_attempts = 0;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700240
241 const char* connection_state_name() const;
Dan Albertc7915a32015-05-18 16:46:31 -0700242
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100243 void update_version(int version, size_t payload);
244 int get_protocol_version() const;
245 size_t get_max_payload() const;
246
Dan Albertc7915a32015-05-18 16:46:31 -0700247private:
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100248 int protocol_version;
249 size_t max_payload;
250
Dan Albertc7915a32015-05-18 16:46:31 -0700251 DISALLOW_COPY_AND_ASSIGN(atransport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252};
253
254
255/* A listener is an entity which binds to a local port
256** and, upon receiving a connection on that port, creates
257** an asocket to connect the new local connection to a
258** specific remote service.
259**
260** TODO: some listeners read from the new connection to
261** determine what exact service to connect to on the far
262** side.
263*/
264struct alistener
265{
266 alistener *next;
267 alistener *prev;
268
269 fdevent fde;
270 int fd;
271
Dan Albertbac34742015-02-25 17:51:28 -0800272 char *local_name;
273 char *connect_to;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 atransport *transport;
275 adisconnect disconnect;
276};
277
278
279void print_packet(const char *label, apacket *p);
280
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100281asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282void install_local_socket(asocket *s);
283void remove_socket(asocket *s);
284void close_all_sockets(atransport *t);
285
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286asocket *create_local_socket(int fd);
287asocket *create_local_service_socket(const char *destination);
288
289asocket *create_remote_socket(unsigned id, atransport *t);
290void connect_to_remote(asocket *s, const char *destination);
291void connect_to_smartsocket(asocket *s);
292
Dan Albert286bb6d2015-07-09 20:35:09 +0000293void fatal(const char *fmt, ...);
294void fatal_errno(const char *fmt, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295
296void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297
Alexey Tarasov31664102009-10-22 02:55:00 +1100298void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100299int launch_server(int server_port);
300int adb_main(int is_daemon, int server_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100303#if ADB_HOST
304int get_available_local_transport_index();
305#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700306int init_socket_transport(atransport *t, int s, int port, int local);
Dan Albertdcd78a12015-05-18 16:43:57 -0700307void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100309#if ADB_HOST
310atransport* find_emulator_transport_by_adb_port(int adb_port);
311#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313int service_to_fd(const char *name);
314#if ADB_HOST
315asocket *host_service_to_socket(const char* name, const char *serial);
316#endif
317
318#if !ADB_HOST
319int init_jdwp(void);
320asocket* create_jdwp_service_socket();
321asocket* create_jdwp_tracker_service_socket();
322int create_jdwp_connection_fd(int jdwp_pid);
323#endif
324
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700325int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100326
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327#if !ADB_HOST
328void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800329void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330#endif
331
332/* packet allocator */
333apacket *get_apacket(void);
334void put_apacket(apacket *p);
335
leozwangcbf02672014-08-15 09:51:27 -0700336// Define it if you want to dump packets.
337#define DEBUG_PACKETS 0
338
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700339#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340#define print_packet(tag,p) do {} while (0)
341#endif
342
John Michelauc3188332010-09-23 17:08:34 -0500343#if ADB_HOST_ON_TARGET
344/* adb and adbd are coexisting on the target, so use 5038 for adb
345 * to avoid conflicting with adbd's usage of 5037
346 */
347# define DEFAULT_ADB_PORT 5038
348#else
349# define DEFAULT_ADB_PORT 5037
350#endif
351
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100352#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
Xavier Ducroheta481d092009-05-21 17:47:43 -0700354#define ADB_CLASS 0xff
355#define ADB_SUBCLASS 0x42
356#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700357
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700359void local_init(int port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360int local_connect(int port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100361int local_connect_arbitrary_ports(int console_port, int adb_port);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362
363/* usb host/client interface */
364void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365int usb_write(usb_handle *h, const void *data, int len);
366int usb_read(usb_handle *h, void *data, int len);
367int usb_close(usb_handle *h);
368void usb_kick(usb_handle *h);
369
370/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700371#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700373#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
Dan Albertbac34742015-02-25 17:51:28 -0800375int adb_commandline(int argc, const char **argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Dan Albertdcd78a12015-05-18 16:43:57 -0700377ConnectionState connection_state(atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Dan Albertbd0b7502015-02-18 18:22:45 -0800379extern const char *adb_device_banner;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380extern int HOST;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400381#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700382extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400383#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
385#define CHUNK_SIZE (64*1024)
386
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100387#if !ADB_HOST
388#define USB_ADB_PATH "/dev/android_adb"
389
390#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
391#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
392
393#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
394#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
395#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
396#endif
397
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700398int 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 -0800399
Dan Albertba3a2512015-02-18 17:47:33 -0800400void handle_online(atransport *t);
401void handle_offline(atransport *t);
402
403void send_connect(atransport *t);
404
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405#endif