OVERVIEW.TXT 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. Implementation notes regarding ADB.
  2. I. General Overview:
  3. The Android Debug Bridge (ADB) is used to:
  4. - keep track of all Android devices and emulators instances
  5. connected to or running on a given host developer machine
  6. - implement various control commands (e.g. "adb shell", "adb pull", etc..)
  7. for the benefit of clients (command-line users, or helper programs like
  8. DDMS). These commands are what is called a 'service' in ADB.
  9. As a whole, everything works through the following components:
  10. 1. The ADB server
  11. This is a background process that runs on the host machine. Its purpose
  12. if to sense the USB ports to know when devices are attached/removed,
  13. as well as when emulator instances start/stop.
  14. It thus maintains a list of "connected devices" and assigns a 'state'
  15. to each one of them: OFFLINE, BOOTLOADER, RECOVERY or ONLINE (more on
  16. this below).
  17. The ADB server is really one giant multiplexing loop whose purpose is
  18. to orchestrate the exchange of data (packets, really) between clients,
  19. services and devices.
  20. 2. The ADB daemon (adbd)
  21. The 'adbd' program runs as a background process within an Android device
  22. or emulated system. Its purpose is to connect to the ADB server
  23. (through USB for devices, through TCP for emulators) and provide a
  24. few services for clients that run on the host.
  25. The ADB server considers that a device is ONLINE when it has successfully
  26. connected to the adbd program within it. Otherwise, the device is OFFLINE,
  27. meaning that the ADB server detected a new device/emulator, but could not
  28. connect to the adbd daemon.
  29. the BOOTLOADER and RECOVERY states correspond to alternate states of
  30. devices when they are in the bootloader or recovery mode.
  31. 3. The ADB command-line client
  32. The 'adb' command-line program is used to run adb commands from a shell
  33. or a script. It first tries to locate the ADB server on the host machine,
  34. and will start one automatically if none is found.
  35. then, the client sends its service requests to the ADB server. It doesn't
  36. need to know.
  37. Currently, a single 'adb' binary is used for both the server and client.
  38. this makes distribution and starting the server easier.
  39. 4. Services
  40. There are essentially two kinds of services that a client can talk to.
  41. Host Services:
  42. these services run within the ADB Server and thus do not need to
  43. communicate with a device at all. A typical example is "adb devices"
  44. which is used to return the list of currently known devices and their
  45. state. They are a few couple other services though.
  46. Local Services:
  47. these services either run within the adbd daemon, or are started by
  48. it on the device. The ADB server is used to multiplex streams
  49. between the client and the service running in adbd. In this case
  50. its role is to initiate the connection, then of being a pass-through
  51. for the data.
  52. II. Protocol details:
  53. 1. Client <-> Server protocol:
  54. This details the protocol used between ADB clients and the ADB
  55. server itself. The ADB server listens on TCP:localhost:5037.
  56. A client sends a request using the following format:
  57. 1. A 4-byte hexadecimal string giving the length of the payload
  58. 2. Followed by the payload itself.
  59. For example, to query the ADB server for its internal version number,
  60. the client will do the following:
  61. 1. Connect to tcp:localhost:5037
  62. 2. Send the string "000Chost:version" to the corresponding socket
  63. The 'host:' prefix is used to indicate that the request is addressed
  64. to the server itself (we will talk about other kinds of requests later).
  65. The content length is encoded in ASCII for easier debugging.
  66. The server should answer a request with one of the following:
  67. 1. For success, the 4-byte "OKAY" string
  68. 2. For failure, the 4-byte "FAIL" string, followed by a
  69. 4-byte hex length, followed by a string giving the reason
  70. for failure.
  71. 3. As a special exception, for 'host:version', a 4-byte
  72. hex string corresponding to the server's internal version number
  73. Note that the connection is still alive after an OKAY, which allows the
  74. client to make other requests. But in certain cases, an OKAY will even
  75. change the state of the connection.
  76. For example, the case of the 'host:transport:<serialnumber>' request,
  77. where '<serialnumber>' is used to identify a given device/emulator; after
  78. the "OKAY" answer, all further requests made by the client will go
  79. directly to the corresponding adbd daemon.
  80. The file SERVICES.TXT lists all services currently implemented by ADB.
  81. 2. Transports:
  82. An ADB transport models a connection between the ADB server and one device
  83. or emulator. There are currently two kinds of transports:
  84. - USB transports, for physical devices through USB
  85. - Local transports, for emulators running on the host, connected to
  86. the server through TCP
  87. In theory, it should be possible to write a local transport that proxies
  88. a connection between an ADB server and a device/emulator connected to/
  89. running on another machine. This hasn't been done yet though.
  90. Each transport can carry one or more multiplexed streams between clients
  91. and the device/emulator they point to. The ADB server must handle
  92. unexpected transport disconnections (e.g. when a device is physically
  93. unplugged) properly.