blob: 7bc60fca7af22c43d1b7700287df065de635f55e [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"
Josh Gao57e09b12019-06-28 13:50:37 -070029#include "fdevent/fdevent.h"
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070030#include "socket.h"
Josh Gao1ce99572018-03-07 16:52:28 -080031#include "types.h"
JP Abgrall408fa572011-03-16 15:57:42 -070032
Tamas Berghammer3d2904c2015-07-13 19:12:28 +010033constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
Jerry Zhangecee4342017-07-18 14:07:57 -070034constexpr size_t MAX_PAYLOAD = 1024 * 1024;
Michael Groover7eeda6b2019-04-25 18:33:35 -070035constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080037constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
38
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039#define A_SYNC 0x434e5953
40#define A_CNXN 0x4e584e43
41#define A_OPEN 0x4e45504f
42#define A_OKAY 0x59414b4f
43#define A_CLSE 0x45534c43
44#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070045#define A_AUTH 0x48545541
Joshua Duong5cf78682020-01-21 13:19:42 -080046#define A_STLS 0x534C5453
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Dan Albert76649012015-02-24 15:51:19 -080048// ADB protocol version.
Tim Murrayde471942017-12-07 11:40:00 -080049// Version revision:
50// 0x01000000: original
51// 0x01000001: skip checksum (Dec 2017)
52#define A_VERSION_MIN 0x01000000
53#define A_VERSION_SKIP_CHECKSUM 0x01000001
54#define A_VERSION 0x01000001
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Joshua Duong5cf78682020-01-21 13:19:42 -080056// Stream-based TLS protocol version
57#define A_STLS_VERSION_MIN 0x01000000
58#define A_STLS_VERSION 0x01000000
59
Dan Albert76649012015-02-24 15:51:19 -080060// Used for help/version information.
61#define ADB_VERSION_MAJOR 1
62#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Elliott Hughes42ae2602015-08-12 08:32:10 -070064std::string adb_version();
65
Dan Albert76649012015-02-24 15:51:19 -080066// Increment this when we want to force users to start a new adb server.
Josh Gaoaa4f31a2019-02-22 15:26:15 -080067#define ADB_SERVER_VERSION 41
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Josh Gaob122b172017-08-16 16:57:01 -070069using TransportId = uint64_t;
Dan Albertc7915a32015-05-18 16:46:31 -070070class atransport;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Josh Gao06d61d42016-10-06 13:31:44 -070072uint32_t calculate_apacket_checksum(const apacket* packet);
73
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074/* the adisconnect structure is used to record a callback that
75** will be called whenever a transport is disconnected (e.g. by the user)
76** this should be used to cleanup objects that depend on the
77** transport (e.g. remote sockets, listeners, etc...)
78*/
Mark Salyzyn0a78cc12017-10-04 15:05:40 -070079struct adisconnect {
80 void (*func)(void* opaque, atransport* t);
81 void* opaque;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082};
83
Dan Albertc7915a32015-05-18 16:46:31 -070084// A transport object models the connection to a remote device or emulator there
85// is one transport per connected device/emulator. A "local transport" connects
86// through TCP (for the emulator), while a "usb transport" through USB (for real
87// devices).
88//
89// Note that kTransportHost doesn't really correspond to a real transport
90// object, it's a special value used to indicate that a client wants to connect
91// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -070092enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -070093 kTransportUsb,
94 kTransportLocal,
95 kTransportAny,
96 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -070097};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070099#define TOKEN_SIZE 20
100
Dan Albertdcd78a12015-05-18 16:43:57 -0700101enum ConnectionState {
102 kCsAny = -1,
Josh Gao704494b2018-05-04 16:04:49 -0700103
104 kCsConnecting = 0, // Haven't received a response from the device yet.
105 kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
106 kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
107 kCsNoPerm, // Insufficient permissions to communicate with the device.
108 kCsOffline,
109
Dan Albertdcd78a12015-05-18 16:43:57 -0700110 kCsBootloader,
111 kCsDevice,
112 kCsHost,
113 kCsRecovery,
Dan Albertdcd78a12015-05-18 16:43:57 -0700114 kCsSideload,
Tao Bao25fe1262019-04-07 23:24:03 -0700115 kCsRescue,
Dan Albertdcd78a12015-05-18 16:43:57 -0700116};
117
Josh Gao704494b2018-05-04 16:04:49 -0700118inline bool ConnectionStateIsOnline(ConnectionState state) {
119 switch (state) {
120 case kCsBootloader:
121 case kCsDevice:
122 case kCsHost:
123 case kCsRecovery:
124 case kCsSideload:
Tao Bao25fe1262019-04-07 23:24:03 -0700125 case kCsRescue:
Josh Gao704494b2018-05-04 16:04:49 -0700126 return true;
127 default:
128 return false;
129 }
130}
131
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700132void print_packet(const char* label, apacket* p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700134void handle_packet(apacket* p, atransport* t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135
Josh Gao9c869b52016-08-25 16:00:22 -0700136int launch_server(const std::string& socket_spec);
137int 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 -0800138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139/* initialize a transport object's func pointers and state */
Josh Gao56300c92018-07-25 17:21:49 -0700140int init_socket_transport(atransport* t, unique_fd s, int port, int local);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
Lingfeng Yang11979522016-10-06 12:22:55 -0700142std::string getEmulatorSerialString(int console_port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100143#if ADB_HOST
144atransport* find_emulator_transport_by_adb_port(int adb_port);
Lingfeng Yang11979522016-10-06 12:22:55 -0700145atransport* find_emulator_transport_by_console_port(int console_port);
Stefan Hilzingerd9d1ca42010-04-26 10:17:43 +0100146#endif
Mike Lockwood74d7ff82009-10-11 23:04:18 -0400147
Josh Gao74ccdf92019-01-23 15:36:42 -0800148unique_fd service_to_fd(std::string_view name, atransport* transport);
Josh Gao997cfac2018-07-25 18:15:52 -0700149#if !ADB_HOST
Josh Gaoe2615412018-12-13 13:06:03 -0800150unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
Josh Gao997cfac2018-07-25 18:15:52 -0700151#endif
152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153#if ADB_HOST
Josh Gao0ecc4022019-02-22 13:41:55 -0800154asocket* host_service_to_socket(std::string_view name, std::string_view serial,
155 TransportId transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156#endif
157
158#if !ADB_HOST
Josh Gao6eb78822018-11-16 15:40:16 -0800159asocket* daemon_service_to_socket(std::string_view name);
160#endif
161
162#if !ADB_HOST
Alex Buynytskyy05626c12019-02-21 14:22:51 -0800163unique_fd execute_abb_command(std::string_view command);
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800164#endif
165
166#if !ADB_HOST
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700167int init_jdwp(void);
168asocket* create_jdwp_service_socket();
169asocket* create_jdwp_tracker_service_socket();
Shukang Zhouf4ffae12020-02-13 17:01:39 -0800170asocket* create_app_tracker_service_socket();
Josh Gao997cfac2018-07-25 18:15:52 -0700171unique_fd create_jdwp_connection_fd(int jdwp_pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172#endif
173
Josh Gao19062432018-07-30 18:49:03 -0700174bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
175bool handle_forward_request(const char* service,
176 std::function<atransport*(std::string* error)> transport_acquirer,
177 int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100178
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179/* packet allocator */
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700180apacket* get_apacket(void);
181void put_apacket(apacket* p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
leozwangcbf02672014-08-15 09:51:27 -0700183// Define it if you want to dump packets.
184#define DEBUG_PACKETS 0
185
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700186#if !DEBUG_PACKETS
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700187#define print_packet(tag, p) \
188 do { \
189 } while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190#endif
191
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700192#define DEFAULT_ADB_PORT 5037
John Michelauc3188332010-09-23 17:08:34 -0500193
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100194#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700196#define ADB_CLASS 0xff
197#define ADB_SUBCLASS 0x42
198#define ADB_PROTOCOL 0x1
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Jason Jeremy Iman8bde1912019-07-19 12:44:39 +0900200void local_init(const std::string& addr);
Yabin Cuib74c6492016-04-29 16:53:52 -0700201bool local_connect(int port);
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700202int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700204ConnectionState connection_state(atransport* t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
Dan Albert1792c232015-05-18 13:06:53 -0700206extern const char* adb_device_banner;
207
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700208#define CHUNK_SIZE (64 * 1024)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800210// Argument delimeter for adb abb command.
211#define ABB_ARG_DELIMETER ('\0')
212
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100213#if !ADB_HOST
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700214#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
215#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100216
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700217#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
218#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
219#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100220#endif
221
Josh Gao79797ec2019-02-20 20:37:26 -0800222enum class HostRequestResult {
223 Handled,
224 SwitchedTransport,
225 Unhandled,
226};
227
228HostRequestResult handle_host_request(std::string_view service, TransportType type,
229 const char* serial, TransportId transport_id, int reply_fd,
230 asocket* s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700232void handle_online(atransport* t);
233void handle_offline(atransport* t);
Dan Albertba3a2512015-02-18 17:47:33 -0800234
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700235void send_connect(atransport* t);
Joshua Duong5cf78682020-01-21 13:19:42 -0800236void send_tls_request(atransport* t);
Dan Albertba3a2512015-02-18 17:47:33 -0800237
Dan Albert1792c232015-05-18 13:06:53 -0700238void parse_banner(const std::string&, atransport* t);
239
Josh Gaofd713e52017-05-03 22:37:10 -0700240// On startup, the adb server needs to wait until all of the connected devices are ready.
241// To do this, we need to know when the scan has identified all of the potential new transports, and
242// when each transport becomes ready.
243// TODO: Do this for mDNS as well, instead of just USB?
244
245// We've found all of the transports we potentially care about.
246void adb_notify_device_scan_complete();
247
248// One or more transports have changed status, check to see if we're ready.
249void update_transport_status();
250
251// Wait until device scan has completed and every transport is ready, or a timeout elapses.
252void adb_wait_for_device_initialization();
253
Josh Gao08718242020-03-27 18:09:56 -0700254void usb_init();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255#endif