adb.h 14 KB

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