adb.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <stdint.h>
  20. #include <sys/types.h>
  21. #include <string>
  22. #include <android-base/macros.h>
  23. #include "adb_trace.h"
  24. #include "fdevent.h"
  25. #include "socket.h"
  26. #include "types.h"
  27. #include "usb.h"
  28. constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
  29. constexpr size_t MAX_PAYLOAD = 1024 * 1024;
  30. constexpr size_t MAX_FRAMEWORK_PAYLOAD = 64 * 1024;
  31. constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304;
  32. #define A_SYNC 0x434e5953
  33. #define A_CNXN 0x4e584e43
  34. #define A_OPEN 0x4e45504f
  35. #define A_OKAY 0x59414b4f
  36. #define A_CLSE 0x45534c43
  37. #define A_WRTE 0x45545257
  38. #define A_AUTH 0x48545541
  39. // ADB protocol version.
  40. // Version revision:
  41. // 0x01000000: original
  42. // 0x01000001: skip checksum (Dec 2017)
  43. #define A_VERSION_MIN 0x01000000
  44. #define A_VERSION_SKIP_CHECKSUM 0x01000001
  45. #define A_VERSION 0x01000001
  46. // Used for help/version information.
  47. #define ADB_VERSION_MAJOR 1
  48. #define ADB_VERSION_MINOR 0
  49. std::string adb_version();
  50. // Increment this when we want to force users to start a new adb server.
  51. #define ADB_SERVER_VERSION 41
  52. using TransportId = uint64_t;
  53. class atransport;
  54. uint32_t calculate_apacket_checksum(const apacket* packet);
  55. /* the adisconnect structure is used to record a callback that
  56. ** will be called whenever a transport is disconnected (e.g. by the user)
  57. ** this should be used to cleanup objects that depend on the
  58. ** transport (e.g. remote sockets, listeners, etc...)
  59. */
  60. struct adisconnect {
  61. void (*func)(void* opaque, atransport* t);
  62. void* opaque;
  63. };
  64. // A transport object models the connection to a remote device or emulator there
  65. // is one transport per connected device/emulator. A "local transport" connects
  66. // through TCP (for the emulator), while a "usb transport" through USB (for real
  67. // devices).
  68. //
  69. // Note that kTransportHost doesn't really correspond to a real transport
  70. // object, it's a special value used to indicate that a client wants to connect
  71. // to a service implemented within the ADB server itself.
  72. enum TransportType {
  73. kTransportUsb,
  74. kTransportLocal,
  75. kTransportAny,
  76. kTransportHost,
  77. };
  78. #define TOKEN_SIZE 20
  79. enum ConnectionState {
  80. kCsAny = -1,
  81. kCsConnecting = 0, // Haven't received a response from the device yet.
  82. kCsAuthorizing, // Authorizing with keys from ADB_VENDOR_KEYS.
  83. kCsUnauthorized, // ADB_VENDOR_KEYS exhausted, fell back to user prompt.
  84. kCsNoPerm, // Insufficient permissions to communicate with the device.
  85. kCsOffline,
  86. kCsBootloader,
  87. kCsDevice,
  88. kCsHost,
  89. kCsRecovery,
  90. kCsSideload,
  91. kCsRescue,
  92. };
  93. inline bool ConnectionStateIsOnline(ConnectionState state) {
  94. switch (state) {
  95. case kCsBootloader:
  96. case kCsDevice:
  97. case kCsHost:
  98. case kCsRecovery:
  99. case kCsSideload:
  100. case kCsRescue:
  101. return true;
  102. default:
  103. return false;
  104. }
  105. }
  106. void print_packet(const char* label, apacket* p);
  107. void handle_packet(apacket* p, atransport* t);
  108. int launch_server(const std::string& socket_spec);
  109. int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd);
  110. /* initialize a transport object's func pointers and state */
  111. int init_socket_transport(atransport* t, unique_fd s, int port, int local);
  112. void init_usb_transport(atransport* t, usb_handle* usb);
  113. std::string getEmulatorSerialString(int console_port);
  114. #if ADB_HOST
  115. atransport* find_emulator_transport_by_adb_port(int adb_port);
  116. atransport* find_emulator_transport_by_console_port(int console_port);
  117. #endif
  118. unique_fd service_to_fd(std::string_view name, atransport* transport);
  119. #if !ADB_HOST
  120. unique_fd daemon_service_to_fd(std::string_view name, atransport* transport);
  121. #endif
  122. #if ADB_HOST
  123. asocket* host_service_to_socket(std::string_view name, std::string_view serial,
  124. TransportId transport_id);
  125. #endif
  126. #if !ADB_HOST
  127. asocket* daemon_service_to_socket(std::string_view name);
  128. #endif
  129. #if !ADB_HOST
  130. unique_fd execute_abb_command(std::string_view command);
  131. #endif
  132. #if !ADB_HOST
  133. int init_jdwp(void);
  134. asocket* create_jdwp_service_socket();
  135. asocket* create_jdwp_tracker_service_socket();
  136. unique_fd create_jdwp_connection_fd(int jdwp_pid);
  137. #endif
  138. bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
  139. bool handle_forward_request(const char* service,
  140. std::function<atransport*(std::string* error)> transport_acquirer,
  141. int reply_fd);
  142. /* packet allocator */
  143. apacket* get_apacket(void);
  144. void put_apacket(apacket* p);
  145. // Define it if you want to dump packets.
  146. #define DEBUG_PACKETS 0
  147. #if !DEBUG_PACKETS
  148. #define print_packet(tag, p) \
  149. do { \
  150. } while (0)
  151. #endif
  152. #if ADB_HOST_ON_TARGET
  153. /* adb and adbd are coexisting on the target, so use 5038 for adb
  154. * to avoid conflicting with adbd's usage of 5037
  155. */
  156. #define DEFAULT_ADB_PORT 5038
  157. #else
  158. #define DEFAULT_ADB_PORT 5037
  159. #endif
  160. #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
  161. #define ADB_CLASS 0xff
  162. #define ADB_SUBCLASS 0x42
  163. #define ADB_PROTOCOL 0x1
  164. void local_init(int port);
  165. bool local_connect(int port);
  166. int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
  167. ConnectionState connection_state(atransport* t);
  168. extern const char* adb_device_banner;
  169. #define CHUNK_SIZE (64 * 1024)
  170. // Argument delimeter for adb abb command.
  171. #define ABB_ARG_DELIMETER ('\0')
  172. #if !ADB_HOST
  173. #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"
  174. #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH #x
  175. //#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0)
  176. //#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1)
  177. //#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2)
  178. #define USB_FFS_ADB_EP0 (access("/.cell", F_OK) == 0?"/adb/ep0":"/dev/usb-ffs/adb/ep0")
  179. #define USB_FFS_ADB_OUT (access("/.cell", F_OK) == 0?"/adb/ep1":"/dev/usb-ffs/adb/ep1")
  180. #define USB_FFS_ADB_IN (access("/.cell", F_OK) == 0?"/adb/ep2":"/dev/usb-ffs/adb/ep2")
  181. #endif
  182. enum class HostRequestResult {
  183. Handled,
  184. SwitchedTransport,
  185. Unhandled,
  186. };
  187. HostRequestResult handle_host_request(std::string_view service, TransportType type,
  188. const char* serial, TransportId transport_id, int reply_fd,
  189. asocket* s);
  190. void handle_online(atransport* t);
  191. void handle_offline(atransport* t);
  192. void send_connect(atransport* t);
  193. void parse_banner(const std::string&, atransport* t);
  194. // On startup, the adb server needs to wait until all of the connected devices are ready.
  195. // To do this, we need to know when the scan has identified all of the potential new transports, and
  196. // when each transport becomes ready.
  197. // TODO: Do this for mDNS as well, instead of just USB?
  198. // We've found all of the transports we potentially care about.
  199. void adb_notify_device_scan_complete();
  200. // One or more transports have changed status, check to see if we're ready.
  201. void update_transport_status();
  202. // Wait until device scan has completed and every transport is ready, or a timeout elapses.
  203. void adb_wait_for_device_initialization();
  204. #endif