blob: 39bdedb0ef83a2cf14cd8def34cbf5282cf5a0c6 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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
Josh Gaobea8f3c2020-04-06 12:07:56 -070017#pragma once
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080018
19#include <limits.h>
Josh Gao67ac3792016-10-06 13:31:44 -070020#include <stdint.h>
Dan Albertb302d122015-02-24 15:51:19 -080021#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022
Elliott Hughes43df1092015-07-23 17:12:58 -070023#include <string>
24
Elliott Hughesf55ead92015-12-04 22:00:26 -080025#include <android-base/macros.h>
Dan Albertbe8e54b2015-05-18 13:06:53 -070026
leozwangf25a6a42014-07-29 12:50:02 -070027#include "adb_trace.h"
Josh Gaob51193a2019-06-28 13:50:37 -070028#include "fdevent/fdevent.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070029#include "socket.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080030#include "types.h"
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070031
Tamas Berghammera1c60c02015-07-13 19:12:28 +010032constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070033constexpr size_t MAX_PAYLOAD = 1024 * 1024;
Michael Groover02b74272019-04-25 18:33:35 -070034constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035
Josh Gaobd1f3002021-12-29 21:15:12 -080036// When delayed acks are supported, the initial number of unacknowledged bytes we're willing to
37// receive on a socket before the other side should block.
38constexpr size_t INITIAL_DELAYED_ACK_BYTES = 32 * 1024 * 1024;
39
Jerry Zhangf0e239c2017-02-10 17:45:27 -080040constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
41
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#define A_SYNC 0x434e5953
43#define A_CNXN 0x4e584e43
44#define A_OPEN 0x4e45504f
45#define A_OKAY 0x59414b4f
46#define A_CLSE 0x45534c43
47#define A_WRTE 0x45545257
Benoit Goby2cc19e42012-04-12 12:23:49 -070048#define A_AUTH 0x48545541
Joshua Duong64fab752020-01-21 13:19:42 -080049#define A_STLS 0x534C5453
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050
Dan Albertb302d122015-02-24 15:51:19 -080051// ADB protocol version.
Tim Murrayee7b44d2017-12-07 11:40:00 -080052// Version revision:
53// 0x01000000: original
54// 0x01000001: skip checksum (Dec 2017)
55#define A_VERSION_MIN 0x01000000
56#define A_VERSION_SKIP_CHECKSUM 0x01000001
57#define A_VERSION 0x01000001
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058
Joshua Duong64fab752020-01-21 13:19:42 -080059// Stream-based TLS protocol version
60#define A_STLS_VERSION_MIN 0x01000000
61#define A_STLS_VERSION 0x01000000
62
Dan Albertb302d122015-02-24 15:51:19 -080063// Used for help/version information.
64#define ADB_VERSION_MAJOR 1
65#define ADB_VERSION_MINOR 0
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066
Elliott Hughesb9f01642015-08-12 08:32:10 -070067std::string adb_version();
68
Dan Albertb302d122015-02-24 15:51:19 -080069// Increment this when we want to force users to start a new adb server.
Josh Gao070e8ba2019-02-22 15:26:15 -080070#define ADB_SERVER_VERSION 41
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071
Josh Gaob39e4152017-08-16 16:57:01 -070072using TransportId = uint64_t;
Dan Albertecce5032015-05-18 16:46:31 -070073class atransport;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074
Josh Gao67ac3792016-10-06 13:31:44 -070075uint32_t calculate_apacket_checksum(const apacket* packet);
76
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077/* the adisconnect structure is used to record a callback that
78** will be called whenever a transport is disconnected (e.g. by the user)
79** this should be used to cleanup objects that depend on the
80** transport (e.g. remote sockets, listeners, etc...)
81*/
Mark Salyzynd4bf94d2017-10-04 15:05:40 -070082struct adisconnect {
83 void (*func)(void* opaque, atransport* t);
84 void* opaque;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085};
86
Dan Albertecce5032015-05-18 16:46:31 -070087// A transport object models the connection to a remote device or emulator there
88// is one transport per connected device/emulator. A "local transport" connects
89// through TCP (for the emulator), while a "usb transport" through USB (for real
90// devices).
91//
92// Note that kTransportHost doesn't really correspond to a real transport
93// object, it's a special value used to indicate that a client wants to connect
94// to a service implemented within the ADB server itself.
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070095enum TransportType {
Dan Albertecce5032015-05-18 16:46:31 -070096 kTransportUsb,
97 kTransportLocal,
98 kTransportAny,
99 kTransportHost,
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700100};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101
Benoit Goby2cc19e42012-04-12 12:23:49 -0700102#define TOKEN_SIZE 20
103
Dan Albert9a50f4c2015-05-18 16:43:57 -0700104enum ConnectionState {
105 kCsAny = -1,
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700106
107 kCsConnecting = 0, // Haven't received a response from the device yet.
108 kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
109 kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
110 kCsNoPerm, // Insufficient permissions to communicate with the device.
Josh Gao8e7c9722021-06-17 04:19:45 -0700111 kCsDetached, // USB device that's detached from the adb server.
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700112 kCsOffline,
113
Dan Albert9a50f4c2015-05-18 16:43:57 -0700114 kCsBootloader,
115 kCsDevice,
116 kCsHost,
117 kCsRecovery,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700118 kCsSideload,
Tao Bao81e9c422019-04-07 23:24:03 -0700119 kCsRescue,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700120};
121
Josh Gao6256d992021-05-20 18:28:13 -0700122std::string to_string(ConnectionState state);
123
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700124inline bool ConnectionStateIsOnline(ConnectionState state) {
125 switch (state) {
126 case kCsBootloader:
127 case kCsDevice:
128 case kCsHost:
129 case kCsRecovery:
130 case kCsSideload:
Tao Bao81e9c422019-04-07 23:24:03 -0700131 case kCsRescue:
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700132 return true;
133 default:
134 return false;
135 }
136}
137
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700138void print_packet(const char* label, apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800139
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700140void handle_packet(apacket* p, atransport* t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141
Vince Harron5703eb32021-11-12 12:40:58 -0800142int launch_server(const std::string& socket_spec, const char* one_device);
143int adb_server_main(int is_daemon, const std::string& socket_spec, const char* one_device,
144 int ack_reply_fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800146/* initialize a transport object's func pointers and state */
Josh Gaofa3107e2018-07-25 17:21:49 -0700147int init_socket_transport(atransport* t, unique_fd s, int port, int local);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800148
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700149std::string getEmulatorSerialString(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100150#if ADB_HOST
151atransport* find_emulator_transport_by_adb_port(int adb_port);
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700152atransport* find_emulator_transport_by_console_port(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100153#endif
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400154
Josh Gaoc2705962019-01-23 15:36:42 -0800155unique_fd service_to_fd(std::string_view name, atransport* transport);
Josh Gao5607e922018-07-25 18:15:52 -0700156#if !ADB_HOST
Josh Gaof0fa1e42018-12-13 13:06:03 -0800157unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
Josh Gao5607e922018-07-25 18:15:52 -0700158#endif
159
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160#if ADB_HOST
Josh Gao0565ae02019-02-22 13:41:55 -0800161asocket* host_service_to_socket(std::string_view name, std::string_view serial,
162 TransportId transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800163#endif
164
165#if !ADB_HOST
Josh Gaobd1f3002021-12-29 21:15:12 -0800166asocket* daemon_service_to_socket(std::string_view name, atransport* transport);
Josh Gao3edf8072018-11-16 15:40:16 -0800167#endif
168
169#if !ADB_HOST
Alex Buynytskyy4f3fa052019-02-21 14:22:51 -0800170unique_fd execute_abb_command(std::string_view command);
Alex Buynytskyyef34f012018-12-12 10:48:50 -0800171#endif
172
173#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700174int init_jdwp(void);
175asocket* create_jdwp_service_socket();
176asocket* create_jdwp_tracker_service_socket();
Shukang Zhou420ad552020-02-13 17:01:39 -0800177asocket* create_app_tracker_service_socket();
Josh Gao5607e922018-07-25 18:15:52 -0700178unique_fd create_jdwp_connection_fd(int jdwp_pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800179#endif
180
Josh Gaodd655ec2018-07-30 18:49:03 -0700181bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
182bool handle_forward_request(const char* service,
183 std::function<atransport*(std::string* error)> transport_acquirer,
184 int reply_fd);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100185
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186/* packet allocator */
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700187apacket* get_apacket(void);
188void put_apacket(apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800189
leozwang1be54622014-08-15 09:51:27 -0700190// Define it if you want to dump packets.
191#define DEBUG_PACKETS 0
192
Benoit Goby2cc19e42012-04-12 12:23:49 -0700193#if !DEBUG_PACKETS
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700194#define print_packet(tag, p) \
195 do { \
196 } while (0)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197#endif
198
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700199#define DEFAULT_ADB_PORT 5037
John Michelau87337522010-09-23 17:08:34 -0500200
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100201#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700203#define ADB_CLASS 0xff
204#define ADB_SUBCLASS 0x42
205#define ADB_PROTOCOL 0x1
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900207void local_init(const std::string& addr);
Yabin Cuif401ead2016-04-29 16:53:52 -0700208bool local_connect(int port);
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700209int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210
Dan Albertbe8e54b2015-05-18 13:06:53 -0700211extern const char* adb_device_banner;
212
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700213#define CHUNK_SIZE (64 * 1024)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214
Alex Buynytskyyef34f012018-12-12 10:48:50 -0800215// Argument delimeter for adb abb command.
216#define ABB_ARG_DELIMETER ('\0')
217
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100218#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700219#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
220#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100221
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700222#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
223#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
224#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100225#endif
226
Josh Gaob13f3cd2019-02-20 20:37:26 -0800227enum class HostRequestResult {
228 Handled,
229 SwitchedTransport,
230 Unhandled,
231};
232
233HostRequestResult handle_host_request(std::string_view service, TransportType type,
234 const char* serial, TransportId transport_id, int reply_fd,
235 asocket* s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800236
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700237void handle_online(atransport* t);
238void handle_offline(atransport* t);
Dan Albert056ad0e2015-02-18 17:47:33 -0800239
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700240void send_connect(atransport* t);
Joshua Duong64fab752020-01-21 13:19:42 -0800241void send_tls_request(atransport* t);
Josh Gaobd1f3002021-12-29 21:15:12 -0800242void send_ready(unsigned local, unsigned remote, atransport* t, uint32_t ack_bytes);
Dan Albert056ad0e2015-02-18 17:47:33 -0800243
Dan Albertbe8e54b2015-05-18 13:06:53 -0700244void parse_banner(const std::string&, atransport* t);
245
Josh Gaobea8f3c2020-04-06 12:07:56 -0700246#if ADB_HOST
Josh Gao1e3bf732017-05-03 22:37:10 -0700247// On startup, the adb server needs to wait until all of the connected devices are ready.
248// To do this, we need to know when the scan has identified all of the potential new transports, and
249// when each transport becomes ready.
250// TODO: Do this for mDNS as well, instead of just USB?
251
252// We've found all of the transports we potentially care about.
253void adb_notify_device_scan_complete();
254
255// One or more transports have changed status, check to see if we're ready.
256void update_transport_status();
257
258// Wait until device scan has completed and every transport is ready, or a timeout elapses.
259void adb_wait_for_device_initialization();
Josh Gaobea8f3c2020-04-06 12:07:56 -0700260
Josh Gaobea8f3c2020-04-06 12:07:56 -0700261// When ssh-forwarding to a remote adb server, kill-server is almost never what you actually want,
262// and unfortunately, many other tools issue it. This adds a knob to reject kill-servers.
263void adb_set_reject_kill_server(bool reject);
264#endif
Josh Gao1e3bf732017-05-03 22:37:10 -0700265
Josh Gao6b55e752020-03-27 18:09:56 -0700266void usb_init();