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