1 /*
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>
21 #include <stdint.h>
22 #include <sys/types.h>
23
24 #include <string>
25
26 #include <android-base/macros.h>
27
28 #include "adb_trace.h"
29 #include "fdevent/fdevent.h"
30 #include "socket.h"
31 #include "types.h"
32
33 constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
34 constexpr size_t MAX_PAYLOAD = 1024 * 1024;
35 constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
36
37 constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
38
39 #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
45 #define A_AUTH 0x48545541
46 #define A_STLS 0x534C5453
47
48 // ADB protocol version.
49 // Version revision:
50 // 0x01000000: original
51 // 0x01000001: skip checksum (Dec 2017)
52 #define A_VERSION_MIN 0x01000000
53 #define A_VERSION_SKIP_CHECKSUM 0x01000001
54 #define A_VERSION 0x01000001
55
56 // Stream-based TLS protocol version
57 #define A_STLS_VERSION_MIN 0x01000000
58 #define A_STLS_VERSION 0x01000000
59
60 // Used for help/version information.
61 #define ADB_VERSION_MAJOR 1
62 #define ADB_VERSION_MINOR 0
63
64 std::string adb_version();
65
66 // Increment this when we want to force users to start a new adb server.
67 #define ADB_SERVER_VERSION 41
68
69 using TransportId = uint64_t;
70 class atransport;
71
72 uint32_t calculate_apacket_checksum(const apacket* packet);
73
74 /* the adisconnect structure is used to record a callback that
75 ** will be called whenever a transport is disconnected (e.g. by the user)
76 ** this should be used to cleanup objects that depend on the
77 ** transport (e.g. remote sockets, listeners, etc...)
78 */
79 struct adisconnect {
80 void (*func)(void* opaque, atransport* t);
81 void* opaque;
82 };
83
84 // A transport object models the connection to a remote device or emulator there
85 // is one transport per connected device/emulator. A "local transport" connects
86 // through TCP (for the emulator), while a "usb transport" through USB (for real
87 // devices).
88 //
89 // Note that kTransportHost doesn't really correspond to a real transport
90 // object, it's a special value used to indicate that a client wants to connect
91 // to a service implemented within the ADB server itself.
92 enum TransportType {
93 kTransportUsb,
94 kTransportLocal,
95 kTransportAny,
96 kTransportHost,
97 };
98
99 #define TOKEN_SIZE 20
100
101 enum ConnectionState {
102 kCsAny = -1,
103
104 kCsConnecting = 0, // Haven't received a response from the device yet.
105 kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
106 kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
107 kCsNoPerm, // Insufficient permissions to communicate with the device.
108 kCsOffline,
109
110 kCsBootloader,
111 kCsDevice,
112 kCsHost,
113 kCsRecovery,
114 kCsSideload,
115 kCsRescue,
116 };
117
ConnectionStateIsOnline(ConnectionState state)118 inline bool ConnectionStateIsOnline(ConnectionState state) {
119 switch (state) {
120 case kCsBootloader:
121 case kCsDevice:
122 case kCsHost:
123 case kCsRecovery:
124 case kCsSideload:
125 case kCsRescue:
126 return true;
127 default:
128 return false;
129 }
130 }
131
132 void print_packet(const char* label, apacket* p);
133
134 void handle_packet(apacket* p, atransport* t);
135
136 int launch_server(const std::string& socket_spec);
137 int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
138
139 /* initialize a transport object's func pointers and state */
140 int init_socket_transport(atransport* t, unique_fd s, int port, int local);
141
142 std::string getEmulatorSerialString(int console_port);
143 #if ADB_HOST
144 atransport* find_emulator_transport_by_adb_port(int adb_port);
145 atransport* find_emulator_transport_by_console_port(int console_port);
146 #endif
147
148 unique_fd service_to_fd(std::string_view name, atransport* transport);
149 #if !ADB_HOST
150 unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
151 #endif
152
153 #if ADB_HOST
154 asocket* host_service_to_socket(std::string_view name, std::string_view serial,
155 TransportId transport_id);
156 #endif
157
158 #if !ADB_HOST
159 asocket* daemon_service_to_socket(std::string_view name);
160 #endif
161
162 #if !ADB_HOST
163 unique_fd execute_abb_command(std::string_view command);
164 #endif
165
166 #if !ADB_HOST
167 int init_jdwp(void);
168 asocket* create_jdwp_service_socket();
169 asocket* create_jdwp_tracker_service_socket();
170 unique_fd create_jdwp_connection_fd(int jdwp_pid);
171 #endif
172
173 bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
174 bool handle_forward_request(const char* service,
175 std::function<atransport*(std::string* error)> transport_acquirer,
176 int reply_fd);
177
178 /* packet allocator */
179 apacket* get_apacket(void);
180 void put_apacket(apacket* p);
181
182 // Define it if you want to dump packets.
183 #define DEBUG_PACKETS 0
184
185 #if !DEBUG_PACKETS
186 #define print_packet(tag, p) \
187 do { \
188 } while (0)
189 #endif
190
191 #define DEFAULT_ADB_PORT 5037
192
193 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
194
195 #define ADB_CLASS 0xff
196 #define ADB_SUBCLASS 0x42
197 #define ADB_PROTOCOL 0x1
198
199 void local_init(const std::string& addr);
200 bool local_connect(int port);
201 int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
202
203 ConnectionState connection_state(atransport* t);
204
205 extern const char* adb_device_banner;
206
207 #define CHUNK_SIZE (64 * 1024)
208
209 // Argument delimeter for adb abb command.
210 #define ABB_ARG_DELIMETER ('\0')
211
212 #if !ADB_HOST
213 #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
214 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
215
216 #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
217 #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
218 #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
219 #endif
220
221 enum class HostRequestResult {
222 Handled,
223 SwitchedTransport,
224 Unhandled,
225 };
226
227 HostRequestResult handle_host_request(std::string_view service, TransportType type,
228 const char* serial, TransportId transport_id, int reply_fd,
229 asocket* s);
230
231 void handle_online(atransport* t);
232 void handle_offline(atransport* t);
233
234 void send_connect(atransport* t);
235 void send_tls_request(atransport* t);
236
237 void parse_banner(const std::string&, atransport* t);
238
239 // On startup, the adb server needs to wait until all of the connected devices are ready.
240 // To do this, we need to know when the scan has identified all of the potential new transports, and
241 // when each transport becomes ready.
242 // TODO: Do this for mDNS as well, instead of just USB?
243
244 // We've found all of the transports we potentially care about.
245 void adb_notify_device_scan_complete();
246
247 // One or more transports have changed status, check to see if we're ready.
248 void update_transport_status();
249
250 // Wait until device scan has completed and every transport is ready, or a timeout elapses.
251 void adb_wait_for_device_initialization();
252
253 void usb_init();
254 #endif
255