blob: b5d6bcbf0a6889d8dbc5f0a1ddc4e73334181ceb [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"
Josh Gao1c70e1b2016-09-28 12:32:45 -070031#include "usb.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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Jerry Zhang2f8c60b2017-02-10 17:45:27 -080036constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
37
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038#define A_SYNC 0x434e5953
39#define A_CNXN 0x4e584e43
40#define A_OPEN 0x4e45504f
41#define A_OKAY 0x59414b4f
42#define A_CLSE 0x45534c43
43#define A_WRTE 0x45545257
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070044#define A_AUTH 0x48545541
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
Dan Albert76649012015-02-24 15:51:19 -080046// ADB protocol version.
Tim Murrayde471942017-12-07 11:40:00 -080047// Version revision:
48// 0x01000000: original
49// 0x01000001: skip checksum (Dec 2017)
50#define A_VERSION_MIN 0x01000000
51#define A_VERSION_SKIP_CHECKSUM 0x01000001
52#define A_VERSION 0x01000001
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Dan Albert76649012015-02-24 15:51:19 -080054// Used for help/version information.
55#define ADB_VERSION_MAJOR 1
56#define ADB_VERSION_MINOR 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes42ae2602015-08-12 08:32:10 -070058std::string adb_version();
59
Dan Albert76649012015-02-24 15:51:19 -080060// Increment this when we want to force users to start a new adb server.
Tim Murrayde471942017-12-07 11:40:00 -080061#define ADB_SERVER_VERSION 40
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Josh Gaob122b172017-08-16 16:57:01 -070063using TransportId = uint64_t;
Dan Albertc7915a32015-05-18 16:46:31 -070064class atransport;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66struct amessage {
Josh Gao06d61d42016-10-06 13:31:44 -070067 uint32_t command; /* command identifier constant */
68 uint32_t arg0; /* first argument */
69 uint32_t arg1; /* second argument */
70 uint32_t data_length; /* length of payload (0 is allowed) */
71 uint32_t data_check; /* checksum of data payload */
72 uint32_t magic; /* command ^ 0xffffffff */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073};
74
Mark Salyzyn0a78cc12017-10-04 15:05:40 -070075struct apacket {
76 apacket* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Josh Gao06d61d42016-10-06 13:31:44 -070078 size_t len;
79 char* ptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
81 amessage msg;
Josh Gao06d61d42016-10-06 13:31:44 -070082 char data[MAX_PAYLOAD];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083};
84
Josh Gao06d61d42016-10-06 13:31:44 -070085uint32_t calculate_apacket_checksum(const apacket* packet);
86
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087/* the adisconnect structure is used to record a callback that
88** will be called whenever a transport is disconnected (e.g. by the user)
89** this should be used to cleanup objects that depend on the
90** transport (e.g. remote sockets, listeners, etc...)
91*/
Mark Salyzyn0a78cc12017-10-04 15:05:40 -070092struct adisconnect {
93 void (*func)(void* opaque, atransport* t);
94 void* opaque;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095};
96
Dan Albertc7915a32015-05-18 16:46:31 -070097// A transport object models the connection to a remote device or emulator there
98// is one transport per connected device/emulator. A "local transport" connects
99// through TCP (for the emulator), while a "usb transport" through USB (for real
100// devices).
101//
102// Note that kTransportHost doesn't really correspond to a real transport
103// object, it's a special value used to indicate that a client wants to connect
104// to a service implemented within the ADB server itself.
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700105enum TransportType {
Dan Albertc7915a32015-05-18 16:46:31 -0700106 kTransportUsb,
107 kTransportLocal,
108 kTransportAny,
109 kTransportHost,
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700110};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700112#define TOKEN_SIZE 20
113
Dan Albertdcd78a12015-05-18 16:43:57 -0700114enum ConnectionState {
115 kCsAny = -1,
116 kCsOffline = 0,
117 kCsBootloader,
118 kCsDevice,
119 kCsHost,
120 kCsRecovery,
121 kCsNoPerm, // Insufficient permissions to communicate with the device.
122 kCsSideload,
123 kCsUnauthorized,
124};
125
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700126void print_packet(const char* label, apacket* p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127
Josh Gao56e9bb92016-01-15 15:17:37 -0800128// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
129// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
130void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
131void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700133void handle_packet(apacket* p, atransport* t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
Josh Gao9c869b52016-08-25 16:00:22 -0700135int launch_server(const std::string& socket_spec);
136int 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 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138/* initialize a transport object's func pointers and state */
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700139int init_socket_transport(atransport* t, int s, int port, int local);
Yabin Cuib5e11412017-03-10 16:01:01 -0800140void init_usb_transport(atransport* t, usb_handle* usb);
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
David Pursell0955c662015-08-31 10:42:13 -0700148int service_to_fd(const char* name, const atransport* transport);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700150asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151#endif
152
153#if !ADB_HOST
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700154int init_jdwp(void);
155asocket* create_jdwp_service_socket();
156asocket* create_jdwp_tracker_service_socket();
157int create_jdwp_connection_fd(int jdwp_pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158#endif
159
Josh Gaob122b172017-08-16 16:57:01 -0700160int handle_forward_request(const char* service, TransportType type, const char* serial,
161 TransportId transport_id, int reply_fd);
David 'Digit' Turner25258692013-03-21 21:07:42 +0100162
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163#if !ADB_HOST
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700164void framebuffer_service(int fd, void* cookie);
Paul Lawrence982089d2014-12-03 15:31:57 -0800165void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166#endif
167
168/* packet allocator */
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700169apacket* get_apacket(void);
170void put_apacket(apacket* p);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
leozwangcbf02672014-08-15 09:51:27 -0700172// Define it if you want to dump packets.
173#define DEBUG_PACKETS 0
174
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700175#if !DEBUG_PACKETS
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700176#define print_packet(tag, p) \
177 do { \
178 } while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179#endif
180
John Michelauc3188332010-09-23 17:08:34 -0500181#if ADB_HOST_ON_TARGET
182/* adb and adbd are coexisting on the target, so use 5038 for adb
183 * to avoid conflicting with adbd's usage of 5037
184 */
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700185#define DEFAULT_ADB_PORT 5038
John Michelauc3188332010-09-23 17:08:34 -0500186#else
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700187#define DEFAULT_ADB_PORT 5037
John Michelauc3188332010-09-23 17:08:34 -0500188#endif
189
Stefan Hilzingera84a42e2010-04-19 12:21:12 +0100190#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700192#define ADB_CLASS 0xff
193#define ADB_SUBCLASS 0x42
194#define ADB_PROTOCOL 0x1
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Mike Lockwoodcef31a02009-08-26 12:50:22 -0700196void local_init(int port);
Yabin Cuib74c6492016-04-29 16:53:52 -0700197bool local_connect(int port);
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700198int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700200ConnectionState connection_state(atransport* t);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Dan Albert1792c232015-05-18 13:06:53 -0700202extern const char* adb_device_banner;
203
Alex Vallée947cb3e2015-07-17 15:30:59 -0400204#if !ADB_HOST
JP Abgrall408fa572011-03-16 15:57:42 -0700205extern int SHELL_EXIT_NOTIFY_FD;
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700206#endif // !ADB_HOST
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700208#define CHUNK_SIZE (64 * 1024)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100210#if !ADB_HOST
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700211#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
212#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100213
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700214#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
215#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
216#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
Andrzej Pietrasiewiczfd96db12012-01-13 15:13:46 +0100217#endif
218
Josh Gaob122b172017-08-16 16:57:01 -0700219int handle_host_request(const char* service, TransportType type, const char* serial,
220 TransportId transport_id, int reply_fd, asocket* s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700222void handle_online(atransport* t);
223void handle_offline(atransport* t);
Dan Albertba3a2512015-02-18 17:47:33 -0800224
Mark Salyzyn0a78cc12017-10-04 15:05:40 -0700225void send_connect(atransport* t);
Dan Albertba3a2512015-02-18 17:47:33 -0800226
Dan Albert1792c232015-05-18 13:06:53 -0700227void parse_banner(const std::string&, atransport* t);
228
Josh Gaofd713e52017-05-03 22:37:10 -0700229// On startup, the adb server needs to wait until all of the connected devices are ready.
230// To do this, we need to know when the scan has identified all of the potential new transports, and
231// when each transport becomes ready.
232// TODO: Do this for mDNS as well, instead of just USB?
233
234// We've found all of the transports we potentially care about.
235void adb_notify_device_scan_complete();
236
237// One or more transports have changed status, check to see if we're ready.
238void update_transport_status();
239
240// Wait until device scan has completed and every transport is ready, or a timeout elapses.
241void adb_wait_for_device_initialization();
242
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243#endif