blob: 6a9897f6327deaf57078c23d209744466bf53c61 [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"
Dan Albertbbbbea62014-11-24 23:34:35 -080029#include "fdevent.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070030#include "socket.h"
Josh Gaob7366922016-09-28 12:32:45 -070031#include "usb.h"
JP Abgrall2e5dd6e2011-03-16 15:57:42 -070032
Tamas Berghammera1c60c02015-07-13 19:12:28 +010033constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
Jerry Zhanga2cb8de2017-07-18 14:07:57 -070034constexpr size_t MAX_PAYLOAD = 1024 * 1024;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080035
Jerry Zhangf0e239c2017-02-10 17:45:27 -080036constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
37
The Android Open Source Project9ca14dc2009-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 Goby2cc19e42012-04-12 12:23:49 -070044#define A_AUTH 0x48545541
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080045
Dan Albertb302d122015-02-24 15:51:19 -080046// ADB protocol version.
47#define A_VERSION 0x01000000
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048
Dan Albertb302d122015-02-24 15:51:19 -080049// Used for help/version information.
50#define ADB_VERSION_MAJOR 1
51#define ADB_VERSION_MINOR 0
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080052
Elliott Hughesb9f01642015-08-12 08:32:10 -070053std::string adb_version();
54
Dan Albertb302d122015-02-24 15:51:19 -080055// Increment this when we want to force users to start a new adb server.
Josh Gao210b63f2017-02-22 17:07:01 -080056#define ADB_SERVER_VERSION 39
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057
Josh Gaob39e4152017-08-16 16:57:01 -070058using TransportId = uint64_t;
Dan Albertecce5032015-05-18 16:46:31 -070059class atransport;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
61struct amessage {
Josh Gao67ac3792016-10-06 13:31:44 -070062 uint32_t command; /* command identifier constant */
63 uint32_t arg0; /* first argument */
64 uint32_t arg1; /* second argument */
65 uint32_t data_length; /* length of payload (0 is allowed) */
66 uint32_t data_check; /* checksum of data payload */
67 uint32_t magic; /* command ^ 0xffffffff */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080068};
69
70struct apacket
71{
72 apacket *next;
73
Josh Gao67ac3792016-10-06 13:31:44 -070074 size_t len;
75 char* ptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080076
77 amessage msg;
Josh Gao67ac3792016-10-06 13:31:44 -070078 char data[MAX_PAYLOAD];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080079};
80
Josh Gao67ac3792016-10-06 13:31:44 -070081uint32_t calculate_apacket_checksum(const apacket* packet);
82
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080083/* the adisconnect structure is used to record a callback that
84** will be called whenever a transport is disconnected (e.g. by the user)
85** this should be used to cleanup objects that depend on the
86** transport (e.g. remote sockets, listeners, etc...)
87*/
88struct adisconnect
89{
90 void (*func)(void* opaque, atransport* t);
91 void* opaque;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092};
93
94
Dan Albertecce5032015-05-18 16:46:31 -070095// A transport object models the connection to a remote device or emulator there
96// is one transport per connected device/emulator. A "local transport" connects
97// through TCP (for the emulator), while a "usb transport" through USB (for real
98// devices).
99//
100// Note that kTransportHost doesn't really correspond to a real transport
101// object, it's a special value used to indicate that a client wants to connect
102// to a service implemented within the ADB server itself.
Elliott Hughes3aec2ba2015-05-05 13:10:43 -0700103enum TransportType {
Dan Albertecce5032015-05-18 16:46:31 -0700104 kTransportUsb,
105 kTransportLocal,
106 kTransportAny,
107 kTransportHost,
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700108};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109
Benoit Goby2cc19e42012-04-12 12:23:49 -0700110#define TOKEN_SIZE 20
111
Dan Albert9a50f4c2015-05-18 16:43:57 -0700112enum ConnectionState {
113 kCsAny = -1,
114 kCsOffline = 0,
115 kCsBootloader,
116 kCsDevice,
117 kCsHost,
118 kCsRecovery,
119 kCsNoPerm, // Insufficient permissions to communicate with the device.
120 kCsSideload,
121 kCsUnauthorized,
122};
123
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800124
125void print_packet(const char *label, apacket *p);
126
Josh Gao2930cdc2016-01-15 15:17:37 -0800127// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
128// shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
129void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
130void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131
132void handle_packet(apacket *p, atransport *t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800133
Josh Gaobb4f8602016-08-25 16:00:22 -0700134int launch_server(const std::string& socket_spec);
135int 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 -0800136
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137/* initialize a transport object's func pointers and state */
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100138#if ADB_HOST
139int get_available_local_transport_index();
140#endif
Mike Lockwoodb51ae572009-08-24 15:58:40 -0700141int init_socket_transport(atransport *t, int 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
David Pursell8da19a42015-08-31 10:42:13 -0700150int service_to_fd(const char* name, const atransport* transport);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151#if ADB_HOST
Josh Gaob39e4152017-08-16 16:57:01 -0700152asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153#endif
154
155#if !ADB_HOST
156int init_jdwp(void);
157asocket* create_jdwp_service_socket();
158asocket* create_jdwp_tracker_service_socket();
159int create_jdwp_connection_fd(int jdwp_pid);
160#endif
161
Josh Gaob39e4152017-08-16 16:57:01 -0700162int handle_forward_request(const char* service, TransportType type, const char* serial,
163 TransportId transport_id, int reply_fd);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100164
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800165#if !ADB_HOST
166void framebuffer_service(int fd, void *cookie);
Paul Lawrence8ba2b022014-12-03 15:31:57 -0800167void set_verity_enabled_state_service(int fd, void* cookie);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800168#endif
169
170/* packet allocator */
171apacket *get_apacket(void);
172void put_apacket(apacket *p);
173
leozwang1be54622014-08-15 09:51:27 -0700174// Define it if you want to dump packets.
175#define DEBUG_PACKETS 0
176
Benoit Goby2cc19e42012-04-12 12:23:49 -0700177#if !DEBUG_PACKETS
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178#define print_packet(tag,p) do {} while (0)
179#endif
180
John Michelau87337522010-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 */
185# define DEFAULT_ADB_PORT 5038
186#else
187# define DEFAULT_ADB_PORT 5037
188#endif
189
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100190#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191
Xavier Ducrohet30a40332009-05-21 17:47:43 -0700192#define ADB_CLASS 0xff
193#define ADB_SUBCLASS 0x42
194#define ADB_PROTOCOL 0x1
Dima Zavin3e824912009-05-08 18:25:58 -0700195
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196
Mike Lockwooda8b38752009-08-26 12:50:22 -0700197void local_init(int port);
Yabin Cuif401ead2016-04-29 16:53:52 -0700198bool local_connect(int port);
Elliott Hughes43df1092015-07-23 17:12:58 -0700199int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200
Dan Albert9a50f4c2015-05-18 16:43:57 -0700201ConnectionState connection_state(atransport *t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202
Dan Albertbe8e54b2015-05-18 13:06:53 -0700203extern const char* adb_device_banner;
204
Alex Vallée436fa6b2015-07-17 15:30:59 -0400205#if !ADB_HOST
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700206extern int SHELL_EXIT_NOTIFY_FD;
Alex Vallée436fa6b2015-07-17 15:30:59 -0400207#endif // !ADB_HOST
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800208
209#define CHUNK_SIZE (64*1024)
210
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100211#if !ADB_HOST
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100212#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
213#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
214
215#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
216#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
217#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
218#endif
219
Josh Gaob39e4152017-08-16 16:57:01 -0700220int handle_host_request(const char* service, TransportType type, const char* serial,
221 TransportId transport_id, int reply_fd, asocket* s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800222
Dan Albert056ad0e2015-02-18 17:47:33 -0800223void handle_online(atransport *t);
224void handle_offline(atransport *t);
225
226void send_connect(atransport *t);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800227#if ADB_HOST
228void SendConnectOnHost(atransport* t);
229#endif
Dan Albert056ad0e2015-02-18 17:47:33 -0800230
Dan Albertbe8e54b2015-05-18 13:06:53 -0700231void parse_banner(const std::string&, atransport* t);
232
Josh Gao1e3bf732017-05-03 22:37:10 -0700233// On startup, the adb server needs to wait until all of the connected devices are ready.
234// To do this, we need to know when the scan has identified all of the potential new transports, and
235// when each transport becomes ready.
236// TODO: Do this for mDNS as well, instead of just USB?
237
238// We've found all of the transports we potentially care about.
239void adb_notify_device_scan_complete();
240
241// One or more transports have changed status, check to see if we're ready.
242void update_transport_status();
243
244// Wait until device scan has completed and every transport is ready, or a timeout elapses.
245void adb_wait_for_device_initialization();
246
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800247#endif