blob: 9ff830ee5488e198d4b660643d30dbd141da3664 [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
Elliott Hughes381cfa92015-07-23 17:12:58 -070025#include <string>
26
leozwangd3fc15f2014-07-29 12:50:02 -070027#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080028#include "fdevent.h"
JP Abgrall408fa572011-03-16 15:57:42 -070029
Tamas Berghammer3d2904c2015-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 Projectdd7bc332009-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 Gobyd5fcafa2012-04-12 12:23:49 -070040#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Dan Albert76649012015-02-24 15:51:19 -080042// ADB protocol version.
43#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Dan Albert76649012015-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 Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes42ae2602015-08-12 08:32:10 -070049std::string adb_version();
50
Dan Albert76649012015-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 Projectdd7bc332009-03-03 19:32:55 -080053
Dan Albertc7915a32015-05-18 16:46:31 -070054class atransport;
Elliott Hughes2d4121c2015-04-17 09:47:42 -070055struct usb_handle;
The Android Open Source Projectdd7bc332009-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 Gobyf366b362012-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 Projectdd7bc332009-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' Turner818d6412013-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 Projectdd7bc332009-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 Goby9470c2f2013-02-20 15:04:53 -0800144 /* A socket is bound to atransport */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 atransport *transport;
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100146
147 size_t get_max_payload() const;
The Android Open Source Projectdd7bc332009-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;
160 adisconnect* next;
161 adisconnect* prev;
162};
163
164
Dan Albertc7915a32015-05-18 16:46:31 -0700165// A transport object models the connection to a remote device or emulator there
166// is one transport per connected device/emulator. A "local transport" connects
167// through TCP (for the emulator), while a "usb transport" through USB (for real
168// 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 connect
172// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700173enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -0700174 kTransportUsb,
175 kTransportLocal,
176 kTransportAny,
177 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700178};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700180#define TOKEN_SIZE 20
181
Dan Albertdcd78a12015-05-18 16:43:57 -0700182enum ConnectionState {
183 kCsAny = -1,
184 kCsOffline = 0,
185 kCsBootloader,
186 kCsDevice,
187 kCsHost,
188 kCsRecovery,
189 kCsNoPerm, // Insufficient permissions to communicate with the device.
190 kCsSideload,
191 kCsUnauthorized,
192};
193
Dan Albertc7915a32015-05-18 16:46:31 -0700194class atransport {
195public:
196 // TODO(danalbert): We expose waaaaaaay too much stuff because this was
197 // historically just a struct, but making the whole thing a more idiomatic
198 // class in one go is a very large change. Given how bad our testing is,
199 // it's better to do this piece by piece.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Dan Albertc7915a32015-05-18 16:46:31 -0700201 atransport() {
202 auth_fde = {};
203 transport_fde = {};
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100204 protocol_version = A_VERSION;
205 max_payload = MAX_PAYLOAD;
Dan Albertc7915a32015-05-18 16:46:31 -0700206 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Dan Albertc7915a32015-05-18 16:46:31 -0700208 virtual ~atransport() {}
209
210 int (*read_from_remote)(apacket* p, atransport* t) = nullptr;
211 int (*write_to_remote)(apacket* p, atransport* t) = nullptr;
212 void (*close)(atransport* t) = nullptr;
213 void (*kick)(atransport* t) = nullptr;
214
215 int fd = -1;
216 int transport_socket = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 fdevent transport_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700218 int ref_count = 0;
219 uint32_t sync_token = 0;
220 ConnectionState connection_state = kCsOffline;
221 bool online = false;
222 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
Dan Albertc7915a32015-05-18 16:46:31 -0700224 // USB handle or socket fd as needed.
225 usb_handle* usb = nullptr;
226 int sfd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Dan Albertc7915a32015-05-18 16:46:31 -0700228 // Used to identify transports for clients.
229 char* serial = nullptr;
230 char* product = nullptr;
231 char* model = nullptr;
232 char* device = nullptr;
233 char* devpath = nullptr;
234 int adb_port = -1; // Use for emulators (local transport)
235 bool kicked = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236
Dan Albertc7915a32015-05-18 16:46:31 -0700237 // A list of adisconnect callbacks called when the transport is kicked.
238 adisconnect disconnects = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700239
Dan Albertc7915a32015-05-18 16:46:31 -0700240 void* key = nullptr;
241 unsigned char token[TOKEN_SIZE] = {};
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700242 fdevent auth_fde;
Dan Albertc7915a32015-05-18 16:46:31 -0700243 size_t failed_auth_attempts = 0;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700244
245 const char* connection_state_name() const;
Dan Albertc7915a32015-05-18 16:46:31 -0700246
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100247 void update_version(int version, size_t payload);
248 int get_protocol_version() const;
249 size_t get_max_payload() const;
250
Dan Albertc7915a32015-05-18 16:46:31 -0700251private:
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100252 int protocol_version;
253 size_t max_payload;
254
Dan Albertc7915a32015-05-18 16:46:31 -0700255 DISALLOW_COPY_AND_ASSIGN(atransport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256};
257
258
259/* A listener is an entity which binds to a local port
260** and, upon receiving a connection on that port, creates
261** an asocket to connect the new local connection to a
262** specific remote service.
263**
264** TODO: some listeners read from the new connection to
265** determine what exact service to connect to on the far
266** side.
267*/
268struct alistener
269{
270 alistener *next;
271 alistener *prev;
272
273 fdevent fde;
274 int fd;
275
Dan Albertbac34742015-02-25 17:51:28 -0800276 char *local_name;
277 char *connect_to;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 atransport *transport;
279 adisconnect disconnect;
280};
281
282
283void print_packet(const char *label, apacket *p);
284
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100285asocket *find_local_socket(unsigned local_id, unsigned remote_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286void install_local_socket(asocket *s);
287void remove_socket(asocket *s);
288void close_all_sockets(atransport *t);
289
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290asocket *create_local_socket(int fd);
291asocket *create_local_service_socket(const char *destination);
292
293asocket *create_remote_socket(unsigned id, atransport *t);
294void connect_to_remote(asocket *s, const char *destination);
295void connect_to_smartsocket(asocket *s);
296
Dan Albert286bb6d2015-07-09 20:35:09 +0000297void fatal(const char *fmt, ...);
298void fatal_errno(const char *fmt, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299
300void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301
Alexey Tarasov31664102009-10-22 02:55:00 +1100302void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100303int launch_server(int server_port);
Siva Velusamy9f2d1a92015-08-07 10:10:29 -0700304int adb_main(int is_daemon, int server_port, int ack_reply_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100307#if ADB_HOST
308int get_available_local_transport_index();
309#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700310int init_socket_transport(atransport *t, int s, int port, int local);
Dan Albertdcd78a12015-05-18 16:43:57 -0700311void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100313#if ADB_HOST
314atransport* find_emulator_transport_by_adb_port(int adb_port);
315#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400316
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317int service_to_fd(const char *name);
318#if ADB_HOST
319asocket *host_service_to_socket(const char* name, const char *serial);
320#endif
321
322#if !ADB_HOST
323int init_jdwp(void);
324asocket* create_jdwp_service_socket();
325asocket* create_jdwp_tracker_service_socket();
326int create_jdwp_connection_fd(int jdwp_pid);
327#endif
328
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700329int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100330
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331#if !ADB_HOST
332void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800333void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334#endif
335
336/* packet allocator */
337apacket *get_apacket(void);
338void put_apacket(apacket *p);
339
leozwangcbf02672014-08-15 09:51:27 -0700340// Define it if you want to dump packets.
341#define DEBUG_PACKETS 0
342
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700343#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344#define print_packet(tag,p) do {} while (0)
345#endif
346
John Michelauc3188332010-09-23 17:08:34 -0500347#if ADB_HOST_ON_TARGET
348/* adb and adbd are coexisting on the target, so use 5038 for adb
349 * to avoid conflicting with adbd's usage of 5037
350 */
351# define DEFAULT_ADB_PORT 5038
352#else
353# define DEFAULT_ADB_PORT 5037
354#endif
355
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100356#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
Xavier Ducroheta481d092009-05-21 17:47:43 -0700358#define ADB_CLASS 0xff
359#define ADB_SUBCLASS 0x42
360#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700363void local_init(int port);
Elliott Hughes381cfa92015-07-23 17:12:58 -0700364void local_connect(int port);
365int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
367/* usb host/client interface */
368void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369int usb_write(usb_handle *h, const void *data, int len);
370int usb_read(usb_handle *h, void *data, int len);
371int usb_close(usb_handle *h);
372void usb_kick(usb_handle *h);
373
374/* used for USB device detection */
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700375#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700377#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Dan Albertbac34742015-02-25 17:51:28 -0800379int adb_commandline(int argc, const char **argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380
Dan Albertdcd78a12015-05-18 16:43:57 -0700381ConnectionState connection_state(atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
Dan Albertbd0b7502015-02-18 18:22:45 -0800383extern const char *adb_device_banner;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400384#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700385extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400386#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387
388#define CHUNK_SIZE (64*1024)
389
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100390#if !ADB_HOST
391#define USB_ADB_PATH "/dev/android_adb"
392
393#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
394#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
395
396#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
397#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
398#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
399#endif
400
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700401int 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 -0800402
Dan Albertba3a2512015-02-18 17:47:33 -0800403void handle_online(atransport *t);
404void handle_offline(atransport *t);
405
406void send_connect(atransport *t);
407
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408#endif