PageRenderTime 29ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/minadbd/adb.h

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