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