blob: 8064c175c3edc9571ec5efa269266956a0aacae2 [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>
Josh Gao06d61d42016-10-06 13:31:44 -070021#include <stdint.h>
Dan Albert76649012015-02-24 15:51:19 -080022#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
Elliott Hughes381cfa92015-07-23 17:12:58 -070024#include <string>
25
Elliott Hughes4f713192015-12-04 22:00:26 -080026#include <android-base/macros.h>
Dan Albert1792c232015-05-18 13:06:53 -070027
leozwangd3fc15f2014-07-29 12:50:02 -070028#include "adb_trace.h"
Dan Albert630b9af2014-11-24 23:34:35 -080029#include "fdevent.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070030#include "socket.h"
JP Abgrall408fa572011-03-16 15:57:42 -070031
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010032constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
33constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024;
34constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
36#define A_SYNC 0x434e5953
37#define A_CNXN 0x4e584e43
38#define A_OPEN 0x4e45504f
39#define A_OKAY 0x59414b4f
40#define A_CLSE 0x45534c43
41#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070042#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Dan Albert76649012015-02-24 15:51:19 -080044// ADB protocol version.
45#define A_VERSION 0x01000000
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Dan Albert76649012015-02-24 15:51:19 -080047// Used for help/version information.
48#define ADB_VERSION_MAJOR 1
49#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
Elliott Hughes42ae2602015-08-12 08:32:10 -070051std::string adb_version();
52
Dan Albert76649012015-02-24 15:51:19 -080053// Increment this when we want to force users to start a new adb server.
David Purselleaae97e2016-04-07 11:25:48 -070054#define ADB_SERVER_VERSION 37
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Dan Albertc7915a32015-05-18 16:46:31 -070056class atransport;
Elliott Hughes2d4121c2015-04-17 09:47:42 -070057struct usb_handle;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
59struct amessage {
Josh Gao06d61d42016-10-06 13:31:44 -070060 uint32_t command; /* command identifier constant */
61 uint32_t arg0; /* first argument */
62 uint32_t arg1; /* second argument */
63 uint32_t data_length; /* length of payload (0 is allowed) */
64 uint32_t data_check; /* checksum of data payload */
65 uint32_t magic; /* command ^ 0xffffffff */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066};
67
68struct apacket
69{
70 apacket *next;
71
Josh Gao06d61d42016-10-06 13:31:44 -070072 size_t len;
73 char* ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
75 amessage msg;
Josh Gao06d61d42016-10-06 13:31:44 -070076 char data[MAX_PAYLOAD];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077};
78
Josh Gao06d61d42016-10-06 13:31:44 -070079uint32_t calculate_apacket_checksum(const apacket* packet);
80
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081/* the adisconnect structure is used to record a callback that
82** will be called whenever a transport is disconnected (e.g. by the user)
83** this should be used to cleanup objects that depend on the
84** transport (e.g. remote sockets, listeners, etc...)
85*/
86struct adisconnect
87{
88 void (*func)(void* opaque, atransport* t);
89 void* opaque;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090};
91
92
Dan Albertc7915a32015-05-18 16:46:31 -070093// A transport object models the connection to a remote device or emulator there
94// is one transport per connected device/emulator. A "local transport" connects
95// through TCP (for the emulator), while a "usb transport" through USB (for real
96// devices).
97//
98// Note that kTransportHost doesn't really correspond to a real transport
99// object, it's a special value used to indicate that a client wants to connect
100// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700101enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -0700102 kTransportUsb,
103 kTransportLocal,
104 kTransportAny,
105 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700106};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700108#define TOKEN_SIZE 20
109
Dan Albertdcd78a12015-05-18 16:43:57 -0700110enum ConnectionState {
111 kCsAny = -1,
112 kCsOffline = 0,
113 kCsBootloader,
114 kCsDevice,
115 kCsHost,
116 kCsRecovery,
117 kCsNoPerm, // Insufficient permissions to communicate with the device.
118 kCsSideload,
119 kCsUnauthorized,
120};
121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122
123void print_packet(const char *label, apacket *p);
124
Josh Gao56e9bb92016-01-15 15:17:37 -0800125// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
126// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
127void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
128void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
130void handle_packet(apacket *p, atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131
Josh Gao9c869b52016-08-25 16:00:22 -0700132int launch_server(const std::string& socket_spec);
133int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135/* initialize a transport object's func pointers and state */
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100136#if ADB_HOST
137int get_available_local_transport_index();
138#endif
Mike Lockwood2f38b692009-08-24 15:58:40 -0700139int init_socket_transport(atransport *t, int s, int port, int local);
Dan Albertdcd78a12015-05-18 16:43:57 -0700140void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100142#if ADB_HOST
143atransport* find_emulator_transport_by_adb_port(int adb_port);
144#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400145
David Pursell0955c662015-08-31 10:42:13 -0700146int service_to_fd(const char* name, const atransport* transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147#if ADB_HOST
148asocket *host_service_to_socket(const char* name, const char *serial);
149#endif
150
151#if !ADB_HOST
152int init_jdwp(void);
153asocket* create_jdwp_service_socket();
154asocket* create_jdwp_tracker_service_socket();
155int create_jdwp_connection_fd(int jdwp_pid);
156#endif
157
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700158int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160#if !ADB_HOST
161void framebuffer_service(int fd, void *cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800162void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163#endif
164
165/* packet allocator */
166apacket *get_apacket(void);
167void put_apacket(apacket *p);
168
leozwangcbf02672014-08-15 09:51:27 -0700169// Define it if you want to dump packets.
170#define DEBUG_PACKETS 0
171
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700172#if !DEBUG_PACKETS
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173#define print_packet(tag,p) do {} while (0)
174#endif
175
John Michelauc3188332010-09-23 17:08:34 -0500176#if ADB_HOST_ON_TARGET
177/* adb and adbd are coexisting on the target, so use 5038 for adb
178 * to avoid conflicting with adbd's usage of 5037
179 */
180# define DEFAULT_ADB_PORT 5038
181#else
182# define DEFAULT_ADB_PORT 5037
183#endif
184
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100185#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Xavier Ducroheta481d092009-05-21 17:47:43 -0700187#define ADB_CLASS 0xff
188#define ADB_SUBCLASS 0x42
189#define ADB_PROTOCOL 0x1
Dima Zavin3fd82b82009-05-08 18:25:58 -0700190
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700192void local_init(int port);
Yabin Cuib74c6492016-04-29 16:53:52 -0700193bool local_connect(int port);
Elliott Hughes381cfa92015-07-23 17:12:58 -0700194int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
David Purselld2acbd12015-12-02 15:14:31 -0800196// USB host/client interface.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198int usb_write(usb_handle *h, const void *data, int len);
199int usb_read(usb_handle *h, void *data, int len);
200int usb_close(usb_handle *h);
201void usb_kick(usb_handle *h);
202
David Purselld2acbd12015-12-02 15:14:31 -0800203// USB device detection.
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700204#if ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700206#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Dan Albertdcd78a12015-05-18 16:43:57 -0700208ConnectionState connection_state(atransport *t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Dan Albert1792c232015-05-18 13:06:53 -0700210extern const char* adb_device_banner;
211
Alex Vallée947cb3e2015-07-17 15:30:59 -0400212#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700213extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée947cb3e2015-07-17 15:30:59 -0400214#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
216#define CHUNK_SIZE (64*1024)
217
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100218#if !ADB_HOST
219#define USB_ADB_PATH "/dev/android_adb"
220
221#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
222#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
223
224#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
225#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
226#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
227#endif
228
Elliott Hughesaee80fb2015-05-07 21:38:41 -0700229int 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 -0800230
Dan Albertba3a2512015-02-18 17:47:33 -0800231void handle_online(atransport *t);
232void handle_offline(atransport *t);
233
234void send_connect(atransport *t);
235
Dan Albert1792c232015-05-18 13:06:53 -0700236void parse_banner(const std::string&, atransport* t);
237
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238#endif