blob: 84d55d8157525760bfb9d9ae7ead605ddcc3dc91 [file] [log] [blame]
xj123f7cc2022-06-06 11:35:21 +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>
21
22#include "adb_trace.h"
23#include "transport.h" /* readx(), writex() */
24
25#if ADB_OVER_PCIE
26#define MAX_PAYLOAD 3456
27#else
28#define MAX_PAYLOAD 4096
29#endif
30
31#define A_SYNC 0x434e5953
32#define A_CNXN 0x4e584e43
33#define A_OPEN 0x4e45504f
34#define A_OKAY 0x59414b4f
35#define A_CLSE 0x45534c43
36#define A_WRTE 0x45545257
37#define A_AUTH 0x48545541
38
39#define A_VERSION 0x01000000 // ADB protocol version
40
41#define ADB_VERSION_MAJOR 1 // Used for help/version information
42#define ADB_VERSION_MINOR 0 // Used for help/version information
43
44#define ADB_SERVER_VERSION 32 // Increment this when we want to force users to start a new adb server
45
46typedef struct amessage amessage;
47typedef struct apacket apacket;
48typedef struct asocket asocket;
49typedef struct alistener alistener;
50typedef struct aservice aservice;
51typedef struct atransport atransport;
52typedef struct adisconnect adisconnect;
53typedef struct usb_handle usb_handle;
54
55struct amessage {
56 unsigned command; /* command identifier constant */
57 unsigned arg0; /* first argument */
58 unsigned arg1; /* second argument */
59 unsigned data_length; /* length of payload (0 is allowed) */
60 unsigned data_check; /* checksum of data payload */
61 unsigned magic; /* command ^ 0xffffffff */
62};
63
64struct apacket
65{
66 apacket *next;
67
68 unsigned len;
69 unsigned char *ptr;
70
71 amessage msg;
72 unsigned char data[MAX_PAYLOAD];
73};
74
75/* An asocket represents one half of a connection between a local and
76** remote entity. A local asocket is bound to a file descriptor. A
77** remote asocket is bound to the protocol engine.
78*/
79struct asocket {
80 /* chain pointers for the local/remote list of
81 ** asockets that this asocket lives in
82 */
83 asocket *next;
84 asocket *prev;
85
86 /* the unique identifier for this asocket
87 */
88 unsigned id;
89
90 /* flag: set when the socket's peer has closed
91 ** but packets are still queued for delivery
92 */
93 int closing;
94
95 /* flag: quit adbd when both ends close the
96 ** local service socket
97 */
98 int exit_on_close;
99
100 /* the asocket we are connected to
101 */
102
103 asocket *peer;
104
105 /* For local asockets, the fde is used to bind
106 ** us to our fd event system. For remote asockets
107 ** these fields are not used.
108 */
109 fdevent fde;
110 int fd;
111
112 /* queue of apackets waiting to be written
113 */
114 apacket *pkt_first;
115 apacket *pkt_last;
116
117 /* enqueue is called by our peer when it has data
118 ** for us. It should return 0 if we can accept more
119 ** data or 1 if not. If we return 1, we must call
120 ** peer->ready() when we once again are ready to
121 ** receive data.
122 */
123 int (*enqueue)(asocket *s, apacket *pkt);
124
125 /* ready is called by the peer when it is ready for
126 ** us to send data via enqueue again
127 */
128 void (*ready)(asocket *s);
129
130 /* shutdown is called by the peer before it goes away.
131 ** the socket should not do any further calls on its peer.
132 ** Always followed by a call to close. Optional, i.e. can be NULL.
133 */
134 void (*shutdown)(asocket *s);
135
136 /* close is called by the peer when it has gone away.
137 ** we are not allowed to make any further calls on the
138 ** peer once our close method is called.
139 */
140 void (*close)(asocket *s);
141
142 /* A socket is bound to atransport */
143 atransport *transport;
144};
145
146
147/* the adisconnect structure is used to record a callback that
148** will be called whenever a transport is disconnected (e.g. by the user)
149** this should be used to cleanup objects that depend on the
150** transport (e.g. remote sockets, listeners, etc...)
151*/
152struct adisconnect
153{
154 void (*func)(void* opaque, atransport* t);
155 void* opaque;
156 adisconnect* next;
157 adisconnect* prev;
158};
159
160
161/* a transport object models the connection to a remote device or emulator
162** there is one transport per connected device/emulator. a "local transport"
163** connects through TCP (for the emulator), while a "usb transport" through
164** USB (for real devices)
165**
166** note that kTransportHost doesn't really correspond to a real transport
167** object, it's a special value used to indicate that a client wants to
168** connect to a service implemented within the ADB server itself.
169*/
170typedef enum transport_type {
171 kTransportUsb,
172 kTransportLocal,
173 kTransportAny,
174 kTransportHost,
175} transport_type;
176
177#define TOKEN_SIZE 20
178
179struct atransport
180{
181 atransport *next;
182 atransport *prev;
183
184 int (*read_from_remote)(apacket *p, atransport *t);
185 int (*write_to_remote)(apacket *p, atransport *t);
186 void (*close)(atransport *t);
187 void (*kick)(atransport *t);
188
189 int fd;
190 int transport_socket;
191 fdevent transport_fde;
192 int ref_count;
193 unsigned sync_token;
194 int connection_state;
195 int online;
196 transport_type type;
197
198 /* usb handle or socket fd as needed */
199 struct usb_handle *usb;
200 struct pcie_handle *pcie;
201 int sfd;
202
203 /* used to identify transports for clients */
204 char *serial;
205 char *product;
206 char *model;
207 char *device;
208 char *devpath;
209 int adb_port; // Use for emulators (local transport)
210
211 /* a list of adisconnect callbacks called when the transport is kicked */
212 int kicked;
213 adisconnect disconnects;
214
215 void *key;
216 unsigned char token[TOKEN_SIZE];
217 fdevent auth_fde;
218 unsigned failed_auth_attempts;
219};
220
221
222/* A listener is an entity which binds to a local port
223** and, upon receiving a connection on that port, creates
224** an asocket to connect the new local connection to a
225** specific remote service.
226**
227** TODO: some listeners read from the new connection to
228** determine what exact service to connect to on the far
229** side.
230*/
231struct alistener
232{
233 alistener *next;
234 alistener *prev;
235
236 fdevent fde;
237 int fd;
238
239 const char *local_name;
240 const char *connect_to;
241 atransport *transport;
242 adisconnect disconnect;
243};
244
245
246void print_packet(const char *label, apacket *p);
247
248asocket *find_local_socket(unsigned local_id, unsigned remote_id);
249void install_local_socket(asocket *s);
250void remove_socket(asocket *s);
251void close_all_sockets(atransport *t);
252
253#define LOCAL_CLIENT_PREFIX "emulator-"
254
255asocket *create_local_socket(int fd);
256asocket *create_local_service_socket(const char *destination);
257
258asocket *create_remote_socket(unsigned id, atransport *t);
259void connect_to_remote(asocket *s, const char *destination);
260void connect_to_smartsocket(asocket *s);
261
262void fatal(const char *fmt, ...);
263void fatal_errno(const char *fmt, ...);
264
265void handle_packet(apacket *p, atransport *t);
266void send_packet(apacket *p, atransport *t);
267
268void get_my_path(char *s, size_t maxLen);
269int launch_server(int server_port);
270int adb_main(int is_daemon, int server_port);
271
272
273/* transports are ref-counted
274** get_device_transport does an acquire on your behalf before returning
275*/
276void init_transport_registration(void);
277int list_transports(char *buf, size_t bufsize, int long_listing);
278void update_transports(void);
279
280asocket* create_device_tracker(void);
281
282/* Obtain a transport from the available transports.
283** If state is != CS_ANY, only transports in that state are considered.
284** If serial is non-NULL then only the device with that serial will be chosen.
285** If no suitable transport is found, error is set.
286*/
287atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
288void add_transport_disconnect( atransport* t, adisconnect* dis );
289void remove_transport_disconnect( atransport* t, adisconnect* dis );
290void run_transport_disconnects( atransport* t );
291void kick_transport( atransport* t );
292
293/* initialize a transport object's func pointers and state */
294#if ADB_HOST
295int get_available_local_transport_index();
296#endif
297int init_socket_transport(atransport *t, int s, int port, int local);
298void init_usb_transport(atransport *t, usb_handle *usb, int state);
299
300/* for MacOS X cleanup */
301void close_usb_devices();
302
303/* cause new transports to be init'd and added to the list */
304int register_socket_transport(int s, const char *serial, int port, int local);
305
306/* these should only be used for the "adb disconnect" command */
307void unregister_transport(atransport *t);
308void unregister_all_tcp_transports();
309
310void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable);
311void init_pcie_transport(atransport *t, struct pcie_handle *h, const char *devpath, int state);
312void register_pcie_transport(struct pcie_handle *h, const char *devpath, unsigned writeable);
313
314/* this should only be used for transports with connection_state == CS_NOPERM */
315void unregister_usb_transport(usb_handle *usb);
316
317atransport *find_transport(const char *serial);
318#if ADB_HOST
319atransport* find_emulator_transport_by_adb_port(int adb_port);
320#endif
321
322int service_to_fd(const char *name);
323#if ADB_HOST
324asocket *host_service_to_socket(const char* name, const char *serial);
325#endif
326
327#if !ADB_HOST
328int init_jdwp(void);
329asocket* create_jdwp_service_socket();
330asocket* create_jdwp_tracker_service_socket();
331int create_jdwp_connection_fd(int jdwp_pid);
332#endif
333
334int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
335
336#if !ADB_HOST
337void framebuffer_service(int fd, void *cookie);
338void remount_service(int fd, void *cookie);
339#endif
340
341/* packet allocator */
342apacket *get_apacket(void);
343void put_apacket(apacket *p);
344
345int check_header(apacket *p);
346int check_data(apacket *p);
347
348#if !DEBUG_PACKETS
349#define print_packet(tag,p) do {} while (0)
350#endif
351
352#if ADB_HOST_ON_TARGET
353/* adb and adbd are coexisting on the target, so use 5038 for adb
354 * to avoid conflicting with adbd's usage of 5037
355 */
356# define DEFAULT_ADB_PORT 5038
357#else
358# define DEFAULT_ADB_PORT 5037
359#endif
360
361#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
362
363#define ADB_CLASS 0xff
364#define ADB_SUBCLASS 0x42
365#define ADB_PROTOCOL 0x1
366
367
368void local_init(int port);
369int local_connect(int port);
370int local_connect_arbitrary_ports(int console_port, int adb_port);
371
372/* usb host/client interface */
373void usb_init();
374void usb_cleanup();
375int usb_write(usb_handle *h, const void *data, int len);
376int usb_read(usb_handle *h, void *data, int len);
377int usb_close(usb_handle *h);
378void usb_kick(usb_handle *h);
379
380/* used for USB device detection */
381#if ADB_HOST
382int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
383#endif
384
385unsigned host_to_le32(unsigned n);
386int adb_commandline(int argc, char **argv);
387
388int connection_state(atransport *t);
389
390#define CS_ANY -1
391#define CS_OFFLINE 0
392#define CS_BOOTLOADER 1
393#define CS_DEVICE 2
394#define CS_HOST 3
395#define CS_RECOVERY 4
396#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
397#define CS_SIDELOAD 6
398#define CS_UNAUTHORIZED 7
399
400extern int HOST;
401extern int SHELL_EXIT_NOTIFY_FD;
402
403typedef enum {
404 SUBPROC_PTY = 0,
405 SUBPROC_RAW = 1,
406} subproc_mode;
407
408#define CHUNK_SIZE (64*1024)
409
410#if !ADB_HOST
411#define USB_ADB_PATH "/dev/android_adb"
412
413#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
414#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
415
416#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
417#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
418#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
419#endif
420
421/* PCIe interface */
422#if ADB_OVER_PCIE
423#if ADB_HOST
424#define PCIE_ADB_PATH "/dev/ccci_sap_adb"
425#else
426#define PCIE_ADB_PATH "/dev/ccci_hsapif_adb"
427#define PCIE_INFO_ADB_PATH "/dev/pcie_dipc1"
428#endif /* ADB_HOST */
429#endif /* ADB_OVER_PCIE */
430
431int sendfailmsg(int fd, const char *reason);
432int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
433
434void pcie_init(void);
435void pcie_host_init(void);
436
437#endif