| xj | c929f53 | 2022-06-06 11:35:21 +0800 | [diff] [blame] | 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 | #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 |  | 
 | 46 | typedef struct amessage amessage; | 
 | 47 | typedef struct apacket apacket; | 
 | 48 | typedef struct asocket asocket; | 
 | 49 | typedef struct alistener alistener; | 
 | 50 | typedef struct aservice aservice; | 
 | 51 | typedef struct atransport atransport; | 
 | 52 | typedef struct adisconnect  adisconnect; | 
 | 53 | typedef struct usb_handle usb_handle; | 
 | 54 |  | 
 | 55 | struct 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 |  | 
 | 64 | struct 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 | */ | 
 | 79 | struct 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 | */ | 
 | 152 | struct  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 | */ | 
 | 170 | typedef enum transport_type { | 
 | 171 |         kTransportUsb, | 
 | 172 |         kTransportLocal, | 
 | 173 |         kTransportAny, | 
 | 174 |         kTransportHost, | 
 | 175 | } transport_type; | 
 | 176 |  | 
 | 177 | #define TOKEN_SIZE 20 | 
 | 178 |  | 
 | 179 | struct 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 | */ | 
 | 231 | struct 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 |  | 
 | 246 | void print_packet(const char *label, apacket *p); | 
 | 247 |  | 
 | 248 | asocket *find_local_socket(unsigned local_id, unsigned remote_id); | 
 | 249 | void install_local_socket(asocket *s); | 
 | 250 | void remove_socket(asocket *s); | 
 | 251 | void close_all_sockets(atransport *t); | 
 | 252 |  | 
 | 253 | #define  LOCAL_CLIENT_PREFIX  "emulator-" | 
 | 254 |  | 
 | 255 | asocket *create_local_socket(int fd); | 
 | 256 | asocket *create_local_service_socket(const char *destination); | 
 | 257 |  | 
 | 258 | asocket *create_remote_socket(unsigned id, atransport *t); | 
 | 259 | void connect_to_remote(asocket *s, const char *destination); | 
 | 260 | void connect_to_smartsocket(asocket *s); | 
 | 261 |  | 
 | 262 | void fatal(const char *fmt, ...); | 
 | 263 | void fatal_errno(const char *fmt, ...); | 
 | 264 |  | 
 | 265 | void handle_packet(apacket *p, atransport *t); | 
 | 266 | void send_packet(apacket *p, atransport *t); | 
 | 267 |  | 
 | 268 | void get_my_path(char *s, size_t maxLen); | 
 | 269 | int launch_server(int server_port); | 
 | 270 | int 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 | */ | 
 | 276 | void init_transport_registration(void); | 
 | 277 | int  list_transports(char *buf, size_t  bufsize, int long_listing); | 
 | 278 | void update_transports(void); | 
 | 279 |  | 
 | 280 | asocket*  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 | */ | 
 | 287 | atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out); | 
 | 288 | void   add_transport_disconnect( atransport*  t, adisconnect*  dis ); | 
 | 289 | void   remove_transport_disconnect( atransport*  t, adisconnect*  dis ); | 
 | 290 | void   run_transport_disconnects( atransport*  t ); | 
 | 291 | void   kick_transport( atransport*  t ); | 
 | 292 |  | 
 | 293 | /* initialize a transport object's func pointers and state */ | 
 | 294 | #if ADB_HOST | 
 | 295 | int get_available_local_transport_index(); | 
 | 296 | #endif | 
 | 297 | int  init_socket_transport(atransport *t, int s, int port, int local); | 
 | 298 | void init_usb_transport(atransport *t, usb_handle *usb, int state); | 
 | 299 |  | 
 | 300 | /* for MacOS X cleanup */ | 
 | 301 | void close_usb_devices(); | 
 | 302 |  | 
 | 303 | /* cause new transports to be init'd and added to the list */ | 
 | 304 | int register_socket_transport(int s, const char *serial, int port, int local); | 
 | 305 |  | 
 | 306 | /* these should only be used for the "adb disconnect" command */ | 
 | 307 | void unregister_transport(atransport *t); | 
 | 308 | void unregister_all_tcp_transports(); | 
 | 309 |  | 
 | 310 | void register_usb_transport(usb_handle *h, const char *serial, const char *devpath, unsigned writeable); | 
 | 311 | void init_pcie_transport(atransport *t, struct pcie_handle *h, const char *devpath, int state); | 
 | 312 | void 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 */ | 
 | 315 | void unregister_usb_transport(usb_handle *usb); | 
 | 316 |  | 
 | 317 | atransport *find_transport(const char *serial); | 
 | 318 | #if ADB_HOST | 
 | 319 | atransport* find_emulator_transport_by_adb_port(int adb_port); | 
 | 320 | #endif | 
 | 321 |  | 
 | 322 | int service_to_fd(const char *name); | 
 | 323 | #if ADB_HOST | 
 | 324 | asocket *host_service_to_socket(const char*  name, const char *serial); | 
 | 325 | #endif | 
 | 326 |  | 
 | 327 | #if !ADB_HOST | 
 | 328 | int       init_jdwp(void); | 
 | 329 | asocket*  create_jdwp_service_socket(); | 
 | 330 | asocket*  create_jdwp_tracker_service_socket(); | 
 | 331 | int       create_jdwp_connection_fd(int  jdwp_pid); | 
 | 332 | #endif | 
 | 333 |  | 
 | 334 | int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd); | 
 | 335 |  | 
 | 336 | #if !ADB_HOST | 
 | 337 | void framebuffer_service(int fd, void *cookie); | 
 | 338 | void remount_service(int fd, void *cookie); | 
 | 339 | #endif | 
 | 340 |  | 
 | 341 | /* packet allocator */ | 
 | 342 | apacket *get_apacket(void); | 
 | 343 | void put_apacket(apacket *p); | 
 | 344 |  | 
 | 345 | int check_header(apacket *p); | 
 | 346 | int 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 |  | 
 | 368 | void local_init(int port); | 
 | 369 | int  local_connect(int  port); | 
 | 370 | int  local_connect_arbitrary_ports(int console_port, int adb_port); | 
 | 371 |  | 
 | 372 | /* usb host/client interface */ | 
 | 373 | void usb_init(); | 
 | 374 | void usb_cleanup(); | 
 | 375 | int usb_write(usb_handle *h, const void *data, int len); | 
 | 376 | int usb_read(usb_handle *h, void *data, int len); | 
 | 377 | int usb_close(usb_handle *h); | 
 | 378 | void usb_kick(usb_handle *h); | 
 | 379 |  | 
 | 380 | /* used for USB device detection */ | 
 | 381 | #if ADB_HOST | 
 | 382 | int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); | 
 | 383 | #endif | 
 | 384 |  | 
 | 385 | unsigned host_to_le32(unsigned n); | 
 | 386 | int adb_commandline(int argc, char **argv); | 
 | 387 |  | 
 | 388 | int 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 |  | 
 | 400 | extern int HOST; | 
 | 401 | extern int SHELL_EXIT_NOTIFY_FD; | 
 | 402 |  | 
 | 403 | typedef 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 |  | 
 | 431 | int sendfailmsg(int fd, const char *reason); | 
 | 432 | int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); | 
 | 433 |  | 
 | 434 | void pcie_init(void); | 
 | 435 | void pcie_host_init(void); | 
 | 436 |  | 
 | 437 | #endif |