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