blob: e2911e81e9c148ddacaccd8d9f42d9e2e25ff771 [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 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;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036
Jerry Zhangf0e239c2017-02-10 17:45:27 -080037constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
38
The Android Open Source Project9ca14dc2009-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 Goby2cc19e42012-04-12 12:23:49 -070045#define A_AUTH 0x48545541
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
Dan Albertb302d122015-02-24 15:51:19 -080047// ADB protocol version.
Tim Murrayee7b44d2017-12-07 11:40:00 -080048// Version revision:
49// 0x01000000: original
50// 0x01000001: skip checksum (Dec 2017)
51#define A_VERSION_MIN 0x01000000
52#define A_VERSION_SKIP_CHECKSUM 0x01000001
53#define A_VERSION 0x01000001
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054
Dan Albertb302d122015-02-24 15:51:19 -080055// Used for help/version information.
56#define ADB_VERSION_MAJOR 1
57#define ADB_VERSION_MINOR 0
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058
Elliott Hughesb9f01642015-08-12 08:32:10 -070059std::string adb_version();
60
Dan Albertb302d122015-02-24 15:51:19 -080061// Increment this when we want to force users to start a new adb server.
Josh Gao8496bf52018-11-01 13:16:15 -070062#define ADB_SERVER_VERSION 40
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080063
Josh Gaob39e4152017-08-16 16:57:01 -070064using TransportId = uint64_t;
Dan Albertecce5032015-05-18 16:46:31 -070065class atransport;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066
Josh Gao67ac3792016-10-06 13:31:44 -070067uint32_t calculate_apacket_checksum(const apacket* packet);
68
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069/* the adisconnect structure is used to record a callback that
70** will be called whenever a transport is disconnected (e.g. by the user)
71** this should be used to cleanup objects that depend on the
72** transport (e.g. remote sockets, listeners, etc...)
73*/
Mark Salyzynd4bf94d2017-10-04 15:05:40 -070074struct adisconnect {
75 void (*func)(void* opaque, atransport* t);
76 void* opaque;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077};
78
Dan Albertecce5032015-05-18 16:46:31 -070079// A transport object models the connection to a remote device or emulator there
80// is one transport per connected device/emulator. A "local transport" connects
81// through TCP (for the emulator), while a "usb transport" through USB (for real
82// devices).
83//
84// Note that kTransportHost doesn't really correspond to a real transport
85// object, it's a special value used to indicate that a client wants to connect
86// to a service implemented within the ADB server itself.
Elliott Hughes3aec2ba2015-05-05 13:10:43 -070087enum TransportType {
Dan Albertecce5032015-05-18 16:46:31 -070088 kTransportUsb,
89 kTransportLocal,
90 kTransportAny,
91 kTransportHost,
Elliott Hughesfe7ff812015-04-17 09:47:42 -070092};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080093
Benoit Goby2cc19e42012-04-12 12:23:49 -070094#define TOKEN_SIZE 20
95
Dan Albert9a50f4c2015-05-18 16:43:57 -070096enum ConnectionState {
97 kCsAny = -1,
Josh Gao7a7c5cb2018-05-04 16:04:49 -070098
99 kCsConnecting = 0, // Haven't received a response from the device yet.
100 kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
101 kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
102 kCsNoPerm, // Insufficient permissions to communicate with the device.
103 kCsOffline,
104
Dan Albert9a50f4c2015-05-18 16:43:57 -0700105 kCsBootloader,
106 kCsDevice,
107 kCsHost,
108 kCsRecovery,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700109 kCsSideload,
Dan Albert9a50f4c2015-05-18 16:43:57 -0700110};
111
Josh Gao7a7c5cb2018-05-04 16:04:49 -0700112inline bool ConnectionStateIsOnline(ConnectionState state) {
113 switch (state) {
114 case kCsBootloader:
115 case kCsDevice:
116 case kCsHost:
117 case kCsRecovery:
118 case kCsSideload:
119 return true;
120 default:
121 return false;
122 }
123}
124
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700125void print_packet(const char* label, apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800126
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700127void handle_packet(apacket* p, atransport* t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800128
Josh Gaobb4f8602016-08-25 16:00:22 -0700129int launch_server(const std::string& socket_spec);
130int 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 -0800131
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800132/* initialize a transport object's func pointers and state */
Josh Gaofa3107e2018-07-25 17:21:49 -0700133int init_socket_transport(atransport* t, unique_fd s, int port, int local);
Yabin Cui3cf1b362017-03-10 16:01:01 -0800134void init_usb_transport(atransport* t, usb_handle* usb);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700136std::string getEmulatorSerialString(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100137#if ADB_HOST
138atransport* find_emulator_transport_by_adb_port(int adb_port);
Lingfeng Yang5edb12b2016-10-06 12:22:55 -0700139atransport* find_emulator_transport_by_console_port(int console_port);
Stefan Hilzinger1ec03422010-04-26 10:17:43 +0100140#endif
Mike Lockwood5f2c4a12009-10-11 23:04:18 -0400141
Josh Gaoebc1c312018-04-13 12:17:03 -0700142int service_to_fd(const char* name, atransport* transport);
Josh Gao5607e922018-07-25 18:15:52 -0700143#if !ADB_HOST
144unique_fd daemon_service_to_fd(const char* name, atransport* transport);
145#endif
146
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147#if ADB_HOST
Josh Gaob39e4152017-08-16 16:57:01 -0700148asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800149#endif
150
151#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700152int init_jdwp(void);
153asocket* create_jdwp_service_socket();
154asocket* create_jdwp_tracker_service_socket();
Josh Gao5607e922018-07-25 18:15:52 -0700155unique_fd create_jdwp_connection_fd(int jdwp_pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800156#endif
157
Josh Gaodd655ec2018-07-30 18:49:03 -0700158bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
159bool handle_forward_request(const char* service,
160 std::function<atransport*(std::string* error)> transport_acquirer,
161 int reply_fd);
David 'Digit' Turner963a4492013-03-21 21:07:42 +0100162
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800163/* packet allocator */
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700164apacket* get_apacket(void);
165void put_apacket(apacket* p);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800166
leozwang1be54622014-08-15 09:51:27 -0700167// Define it if you want to dump packets.
168#define DEBUG_PACKETS 0
169
Benoit Goby2cc19e42012-04-12 12:23:49 -0700170#if !DEBUG_PACKETS
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700171#define print_packet(tag, p) \
172 do { \
173 } while (0)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174#endif
175
John Michelau87337522010-09-23 17:08:34 -0500176#if ADB_HOST_ON_TARGET
177/* adb and adbd are coexisting on the target, so use 5038 for adb
178 * to avoid conflicting with adbd's usage of 5037
179 */
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700180#define DEFAULT_ADB_PORT 5038
John Michelau87337522010-09-23 17:08:34 -0500181#else
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700182#define DEFAULT_ADB_PORT 5037
John Michelau87337522010-09-23 17:08:34 -0500183#endif
184
Stefan Hilzinger92ca4fa2010-04-19 12:21:12 +0100185#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700187#define ADB_CLASS 0xff
188#define ADB_SUBCLASS 0x42
189#define ADB_PROTOCOL 0x1
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800190
Mike Lockwooda8b38752009-08-26 12:50:22 -0700191void local_init(int port);
Yabin Cuif401ead2016-04-29 16:53:52 -0700192bool local_connect(int port);
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700193int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800194
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700195ConnectionState connection_state(atransport* t);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800196
Dan Albertbe8e54b2015-05-18 13:06:53 -0700197extern const char* adb_device_banner;
198
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700199#define CHUNK_SIZE (64 * 1024)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100201#if !ADB_HOST
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700202#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
203#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100204
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700205#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
206#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
207#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
Andrzej Pietrasiewiczd7270f22012-01-13 15:13:46 +0100208#endif
209
Josh Gaod3067472018-08-07 14:14:21 -0700210bool handle_host_request(const char* service, TransportType type, const char* serial,
211 TransportId transport_id, int reply_fd, asocket* s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800212
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700213void handle_online(atransport* t);
214void handle_offline(atransport* t);
Dan Albert056ad0e2015-02-18 17:47:33 -0800215
Mark Salyzynd4bf94d2017-10-04 15:05:40 -0700216void send_connect(atransport* t);
Dan Albert056ad0e2015-02-18 17:47:33 -0800217
Dan Albertbe8e54b2015-05-18 13:06:53 -0700218void parse_banner(const std::string&, atransport* t);
219
Josh Gao1e3bf732017-05-03 22:37:10 -0700220// On startup, the adb server needs to wait until all of the connected devices are ready.
221// To do this, we need to know when the scan has identified all of the potential new transports, and
222// when each transport becomes ready.
223// TODO: Do this for mDNS as well, instead of just USB?
224
225// We've found all of the transports we potentially care about.
226void adb_notify_device_scan_complete();
227
228// One or more transports have changed status, check to see if we're ready.
229void update_transport_status();
230
231// Wait until device scan has completed and every transport is ready, or a timeout elapses.
232void adb_wait_for_device_initialization();
233
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800234#endif