blob: 59644d43e9e0beb1bb5d1505b8ad1878634e76dc [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
Elliott Hughes381cfa92015-07-23 17:12:58 -070023#include <string>
24
Elliott Hughes4f713192015-12-04 22:00:26 -080025#include <android-base/macros.h>
Dan Albert1792c232015-05-18 13:06:53 -070026
leozwangd3fc15f2014-07-29 12:50:02 -070027#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080028#include "fdevent.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070029#include "socket.h"
JP Abgrall408fa572011-03-16 15:57:42 -070030
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010031constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
32constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
33constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#define A_SYNC 0x434e5953
36#define A_CNXN 0x4e584e43
37#define A_OPEN 0x4e45504f
38#define A_OKAY 0x59414b4f
39#define A_CLSE 0x45534c43
40#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070041#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Dan Albert76649012015-02-24 15:51:19 -080043// ADB protocol version.
44#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Dan Albert76649012015-02-24 15:51:19 -080046// Used for help/version information.
47#define ADB_VERSION_MAJOR 1
48#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Elliott Hughes42ae2602015-08-12 08:32:10 -070050std::string adb_version();
51
Dan Albert76649012015-02-24 15:51:19 -080052// Increment this when we want to force users to start a new adb server.
Todd Kennedy6fa848a2015-11-03 16:53:08 -080053#define ADB_SERVER_VERSION 35
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Dan Albertc7915a32015-05-18 16:46:31 -070055class atransport;
Elliott Hughes2d4121c2015-04-17 09:47:42 -070056struct usb_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
58struct amessage {
59 unsigned command; /* command identifier constant */
60 unsigned arg0; /* first argument */
61 unsigned arg1; /* second argument */
62 unsigned data_length; /* length of payload (0 is allowed) */
63 unsigned data_check; /* checksum of data payload */
64 unsigned magic; /* command ^ 0xffffffff */
65};
66
67struct apacket
68{
69 apacket *next;
70
71 unsigned len;
72 unsigned char *ptr;
73
74 amessage msg;
75 unsigned char data[MAX_PAYLOAD];
76};
77
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078/* the adisconnect structure is used to record a callback that
79** will be called whenever a transport is disconnected (e.g. by the user)
80** this should be used to cleanup objects that depend on the
81** transport (e.g. remote sockets, listeners, etc...)
82*/
83struct adisconnect
84{
85 void (*func)(void* opaque, atransport* t);
86 void* opaque;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087};
88
89
Dan Albertc7915a32015-05-18 16:46:31 -070090// A transport object models the connection to a remote device or emulator there
91// is one transport per connected device/emulator. A "local transport" connects
92// through TCP (for the emulator), while a "usb transport" through USB (for real
93// devices).
94//
95// Note that kTransportHost doesn't really correspond to a real transport
96// object, it's a special value used to indicate that a client wants to connect
97// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -070098enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -070099 kTransportUsb,
100 kTransportLocal,
101 kTransportAny,
102 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700103};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700105#define TOKEN_SIZE 20
106
Dan Albertdcd78a12015-05-18 16:43:57 -0700107enum ConnectionState {
108 kCsAny = -1,
109 kCsOffline = 0,
110 kCsBootloader,
111 kCsDevice,
112 kCsHost,
113 kCsRecovery,
114 kCsNoPerm, // Insufficient permissions to communicate with the device.
115 kCsSideload,
116 kCsUnauthorized,
117};
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119/* A listener is an entity which binds to a local port
120** and, upon receiving a connection on that port, creates
121** an asocket to connect the new local connection to a
122** specific remote service.
123**
124** TODO: some listeners read from the new connection to
125** determine what exact service to connect to on the far
126** side.
127*/
128struct alistener
129{
130 alistener *next;
131 alistener *prev;
132
133 fdevent fde;
134 int fd;
135
Dan Albertbac34742015-02-25 17:51:28 -0800136 char *local_name;
137 char *connect_to;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 atransport *transport;
139 adisconnect disconnect;
140};
141
142
143void print_packet(const char *label, apacket *p);
144
Josh Gao56e9bb92016-01-15 15:17:37 -0800145// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
146// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
147void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
148void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
150void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
Alexey Tarasov31664102009-10-22 02:55:00 +1100152void get_my_path(char *s, size_t maxLen);
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100153int launch_server(int server_port);
Elliott Hughes65433da2015-11-18 18:07:48 -0800154int adb_server_main(int is_daemon, int server_port, int ack_reply_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100157#if ADB_HOST
158int get_available_local_transport_index();
159#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700160int init_socket_transport(atransport *t, int s, int port, int local);
Dan Albertdcd78a12015-05-18 16:43:57 -0700161void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100163#if ADB_HOST
164atransport* find_emulator_transport_by_adb_port(int adb_port);
165#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400166
David Pursell0955c662015-08-31 10:42:13 -0700167int service_to_fd(const char* name, const atransport* transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168#if ADB_HOST
169asocket *host_service_to_socket(const char* name, const char *serial);
170#endif
171
172#if !ADB_HOST
173int init_jdwp(void);
174asocket* create_jdwp_service_socket();
175asocket* create_jdwp_tracker_service_socket();
176int create_jdwp_connection_fd(int jdwp_pid);
177#endif
178
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700179int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100180
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181#if !ADB_HOST
182void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800183void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184#endif
185
186/* packet allocator */
187apacket *get_apacket(void);
188void put_apacket(apacket *p);
189
leozwangcbf02672014-08-15 09:51:27 -0700190// Define it if you want to dump packets.
191#define DEBUG_PACKETS 0
192
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700193#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194#define print_packet(tag,p) do {} while (0)
195#endif
196
John Michelauc3188332010-09-23 17:08:34 -0500197#if ADB_HOST_ON_TARGET
198/* adb and adbd are coexisting on the target, so use 5038 for adb
199 * to avoid conflicting with adbd's usage of 5037
200 */
201# define DEFAULT_ADB_PORT 5038
202#else
203# define DEFAULT_ADB_PORT 5037
204#endif
205
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100206#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Xavier Ducroheta481d092009-05-21 17:47:43 -0700208#define ADB_CLASS 0xff
209#define ADB_SUBCLASS 0x42
210#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700211
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700213void local_init(int port);
Elliott Hughes381cfa92015-07-23 17:12:58 -0700214void local_connect(int port);
215int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
David Purselld2acbd12015-12-02 15:14:31 -0800217// USB host/client interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219int usb_write(usb_handle *h, const void *data, int len);
220int usb_read(usb_handle *h, void *data, int len);
221int usb_close(usb_handle *h);
222void usb_kick(usb_handle *h);
223
David Purselld2acbd12015-12-02 15:14:31 -0800224// USB device detection.
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700225#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700227#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228
Dan Albertbac34742015-02-25 17:51:28 -0800229int adb_commandline(int argc, const char **argv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230
Dan Albertdcd78a12015-05-18 16:43:57 -0700231ConnectionState connection_state(atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232
Dan Albert1792c232015-05-18 13:06:53 -0700233extern const char* adb_device_banner;
234
Alex Vallée947cb3e2015-07-17 15:30:59 -0400235#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700236extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400237#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
239#define CHUNK_SIZE (64*1024)
240
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100241#if !ADB_HOST
242#define USB_ADB_PATH "/dev/android_adb"
243
244#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
245#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
246
247#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
248#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
249#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
250#endif
251
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700252int 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 -0800253
Dan Albertba3a2512015-02-18 17:47:33 -0800254void handle_online(atransport *t);
255void handle_offline(atransport *t);
256
257void send_connect(atransport *t);
258
Dan Albert1792c232015-05-18 13:06:53 -0700259void parse_banner(const std::string&, atransport* t);
260
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261#endif