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