blob: 86d205c98c4f6968b8894b528c549c45fc540f1f [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
17#ifndef __ADB_H
18#define __ADB_H
19
20#include <limits.h>
Josh Gao67ac3792016-10-06 13:31:44 -070021#include <stdint.h>
Dan Albertb302d122015-02-24 15:51:19 -080022#include <sys/types.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023
Elliott Hughes43df1092015-07-23 17:12:58 -070024#include <string>
25
Elliott Hughesf55ead92015-12-04 22:00:26 -080026#include <android-base/macros.h>
Dan Albertbe8e54b2015-05-18 13:06:53 -070027
leozwangf25a6a42014-07-29 12:50:02 -070028#include "adb_trace.h"
Josh Gaob51193a2019-06-28 13:50:37 -070029#include "fdevent/fdevent.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070030#include "socket.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080031#include "types.h"
Josh Gaob7366922016-09-28 12:32:45 -070032#include "usb.h"
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070033
Tamas Berghammera1c60c02015-07-13 19:12:28 +010034constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070035constexpr size_t MAX_PAYLOAD = 1024 * 1024;
Michael Groover02b74272019-04-25 18:33:35 -070036constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080037
Jerry Zhangf0e239c2017-02-10 17:45:27 -080038constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
39
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080040#define A_SYNC 0x434e5953
41#define A_CNXN 0x4e584e43
42#define A_OPEN 0x4e45504f
43#define A_OKAY 0x59414b4f
44#define A_CLSE 0x45534c43
45#define A_WRTE 0x45545257
Benoit Goby2cc19e42012-04-12 12:23:49 -070046#define A_AUTH 0x48545541
Joshua Duong64fab752020-01-21 13:19:42 -080047#define A_STLS 0x534C5453
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048
Dan Albertb302d122015-02-24 15:51:19 -080049// ADB protocol version.
Tim Murrayee7b44d2017-12-07 11:40:00 -080050// Version revision:
51// 0x01000000: original
52// 0x01000001: skip checksum (Dec 2017)
53#define A_VERSION_MIN 0x01000000
54#define A_VERSION_SKIP_CHECKSUM 0x01000001
55#define A_VERSION 0x01000001
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056
Joshua Duong64fab752020-01-21 13:19:42 -080057// Stream-based TLS protocol version
58#define A_STLS_VERSION_MIN 0x01000000
59#define A_STLS_VERSION 0x01000000
60
Dan Albertb302d122015-02-24 15:51:19 -080061// Used for help/version information.
62#define ADB_VERSION_MAJOR 1
63#define ADB_VERSION_MINOR 0
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064
Elliott Hughesb9f01642015-08-12 08:32:10 -070065std::string adb_version();
66
Dan Albertb302d122015-02-24 15:51:19 -080067// Increment this when we want to force users to start a new adb server.
Josh Gao070e8ba2019-02-22 15:26:15 -080068#define ADB_SERVER_VERSION 41
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069
Josh Gaob39e4152017-08-16 16:57:01 -070070using TransportId = uint64_t;
Dan Albertecce5032015-05-18 16:46:31 -070071class atransport;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080072
Josh Gao67ac3792016-10-06 13:31:44 -070073uint32_t calculate_apacket_checksum(const apacket* packet);
74
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080075/* the adisconnect structure is used to record a callback that
76** will be called whenever a transport is disconnected (e.g. by the user)
77** this should be used to cleanup objects that depend on the
78** transport (e.g. remote sockets, listeners, etc...)
79*/
Mark Salyzynd4bf94d2017-10-04 15:05:40 -070080struct adisconnect {
81 void (*func)(void* opaque, atransport* t);
82 void* opaque;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080083};
84
Dan Albertecce5032015-05-18 16:46:31 -070085// A transport object models the connection to a remote device or emulator there
86// is one transport per connected device/emulator. A "local transport" connects
87// through TCP (for the emulator), while a "usb transport" through USB (for real
88// devices).
89//
90// Note that kTransportHost doesn't really correspond to a real transport
91// object, it's a special value used to indicate that a client wants to connect
92// to a service implemented within the ADB server itself.
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070093enum TransportType {
Dan Albertecce5032015-05-18 16:46:31 -070094 kTransportUsb,
95 kTransportLocal,
96 kTransportAny,
97 kTransportHost,
Elliott Hughesfe7ff812015-04-17 09:47:42 -070098};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099
Benoit Goby2cc19e42012-04-12 12:23:49 -0700100#define TOKEN_SIZE 20
101
Dan Albert9a50f4c2015-05-18 16:43:57 -0700102enum ConnectionState {
103 kCsAny = -1,
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700104
105 kCsConnecting = 0, // Haven't received a response from the device yet.
106 kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
107 kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
108 kCsNoPerm, // Insufficient permissions to communicate with the device.
109 kCsOffline,
110
Dan Albert9a50f4c2015-05-18 16:43:57 -0700111 kCsBootloader,
112 kCsDevice,
113 kCsHost,
114 kCsRecovery,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700115 kCsSideload,
Tao Bao81e9c422019-04-07 23:24:03 -0700116 kCsRescue,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700117};
118
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700119inline bool ConnectionStateIsOnline(ConnectionState state) {
120 switch (state) {
121 case kCsBootloader:
122 case kCsDevice:
123 case kCsHost:
124 case kCsRecovery:
125 case kCsSideload:
Tao Bao81e9c422019-04-07 23:24:03 -0700126 case kCsRescue:
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700127 return true;
128 default:
129 return false;
130 }
131}
132
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700133void print_packet(const char* label, apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700135void handle_packet(apacket* p, atransport* t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800136
Josh Gaobb4f8602016-08-25 16:00:22 -0700137int launch_server(const std::string& socket_spec);
138int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800139
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140/* initialize a transport object's func pointers and state */
Josh Gaofa3107e2018-07-25 17:21:49 -0700141int init_socket_transport(atransport* t, unique_fd s, int port, int local);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800142void init_usb_transport(atransport* t, usb_handle* usb);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700144std::string getEmulatorSerialString(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100145#if ADB_HOST
146atransport* find_emulator_transport_by_adb_port(int adb_port);
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700147atransport* find_emulator_transport_by_console_port(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100148#endif
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400149
Josh Gaoc2705962019-01-23 15:36:42 -0800150unique_fd service_to_fd(std::string_view name, atransport* transport);
Josh Gao5607e922018-07-25 18:15:52 -0700151#if !ADB_HOST
Josh Gaof0fa1e42018-12-13 13:06:03 -0800152unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
Josh Gao5607e922018-07-25 18:15:52 -0700153#endif
154
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800155#if ADB_HOST
Josh Gao0565ae02019-02-22 13:41:55 -0800156asocket* host_service_to_socket(std::string_view name, std::string_view serial,
157 TransportId transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158#endif
159
160#if !ADB_HOST
Josh Gao3edf8072018-11-16 15:40:16 -0800161asocket* daemon_service_to_socket(std::string_view name);
162#endif
163
164#if !ADB_HOST
Alex Buynytskyy4f3fa052019-02-21 14:22:51 -0800165unique_fd execute_abb_command(std::string_view command);
Alex Buynytskyyef34f012018-12-12 10:48:50 -0800166#endif
167
168#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700169int init_jdwp(void);
170asocket* create_jdwp_service_socket();
171asocket* create_jdwp_tracker_service_socket();
Josh Gao5607e922018-07-25 18:15:52 -0700172unique_fd create_jdwp_connection_fd(int jdwp_pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800173#endif
174
Josh Gaodd655ec2018-07-30 18:49:03 -0700175bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
176bool handle_forward_request(const char* service,
177 std::function<atransport*(std::string* error)> transport_acquirer,
178 int reply_fd);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100179
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800180/* packet allocator */
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700181apacket* get_apacket(void);
182void put_apacket(apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183
leozwang1be54622014-08-15 09:51:27 -0700184// Define it if you want to dump packets.
185#define DEBUG_PACKETS 0
186
Benoit Goby2cc19e42012-04-12 12:23:49 -0700187#if !DEBUG_PACKETS
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700188#define print_packet(tag, p) \
189 do { \
190 } while (0)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191#endif
192
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700193#define DEFAULT_ADB_PORT 5037
John Michelau87337522010-09-23 17:08:34 -0500194
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100195#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700197#define ADB_CLASS 0xff
198#define ADB_SUBCLASS 0x42
199#define ADB_PROTOCOL 0x1
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200
Jason Jeremy Iman84613872019-07-19 12:44:39 +0900201void local_init(const std::string& addr);
Yabin Cuif401ead2016-04-29 16:53:52 -0700202bool local_connect(int port);
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700203int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800204
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700205ConnectionState connection_state(atransport* t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206
Dan Albertbe8e54b2015-05-18 13:06:53 -0700207extern const char* adb_device_banner;
208
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700209#define CHUNK_SIZE (64 * 1024)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210
Alex Buynytskyyef34f012018-12-12 10:48:50 -0800211// Argument delimeter for adb abb command.
212#define ABB_ARG_DELIMETER ('\0')
213
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100214#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700215#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
216#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100217
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700218#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
219#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
220#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100221#endif
222
Josh Gaob13f3cd2019-02-20 20:37:26 -0800223enum class HostRequestResult {
224 Handled,
225 SwitchedTransport,
226 Unhandled,
227};
228
229HostRequestResult handle_host_request(std::string_view service, TransportType type,
230 const char* serial, TransportId transport_id, int reply_fd,
231 asocket* s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800232
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700233void handle_online(atransport* t);
234void handle_offline(atransport* t);
Dan Albert056ad0e2015-02-18 17:47:33 -0800235
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700236void send_connect(atransport* t);
Joshua Duong64fab752020-01-21 13:19:42 -0800237void send_tls_request(atransport* t);
Dan Albert056ad0e2015-02-18 17:47:33 -0800238
Dan Albertbe8e54b2015-05-18 13:06:53 -0700239void parse_banner(const std::string&, atransport* t);
240
Josh Gao1e3bf732017-05-03 22:37:10 -0700241// On startup, the adb server needs to wait until all of the connected devices are ready.
242// To do this, we need to know when the scan has identified all of the potential new transports, and
243// when each transport becomes ready.
244// TODO: Do this for mDNS as well, instead of just USB?
245
246// We've found all of the transports we potentially care about.
247void adb_notify_device_scan_complete();
248
249// One or more transports have changed status, check to see if we're ready.
250void update_transport_status();
251
252// Wait until device scan has completed and every transport is ready, or a timeout elapses.
253void adb_wait_for_device_initialization();
254
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800255#endif