Adb overview and Protocol Reference, adb overview Protocol Reference
Original article: https://github.com/android/platform_system_core/blob/master/adb/OVERVIEW.TXT)
Implementation notes regarding ADB.
ADB implementation Annotation
1. General Overview:
1 Overview
The Android Debug Bridge (ADB) is used:
ADB is used in the following scenarios:
- Keep track of all Android devices and emulators instances connected to or running on a given host developer machine
- Track and manage all android real machines and Simulators connected to the development Machine
- Implement various control commands (e.g. "adb shell", "adb pull", etc ..) for the benefit of clients (command-line users, or helper programs like DDMS ). these commands are what is called a 'service' in ADB.
- A large number of control commands (such as "adb shell" and "adb pull") are implemented for your convenience (including command line users and helper programs such as ddms ), these commands are often called a 'service' in adb '.
As a whole, everything works through the following components:
All in all, everything is centered on the following modules:
1.1 The ADB server
1.1 ADB Server
This is a background process that runs on the host machine. Its purpose if to sense the USB ports to know when devices are attached/removed, as well as when emulator instances start/stop.
This is inRunning on host devices (PCS/development machines)A background process. It is used to sniff when a device is mounted/removed on the USB port of the host, and when the simulator is enabled/disabled.
It thus maintains a list of "connected devices" and assigns a 'State' to each one of them: OFFLINE, BOOTLOADER, RECOVERY or ONLINE (more on this below ).
Therefore, it maintains a list of connected devices and specifies a 'status' for each device: OFFLINE, BOOTLOADER, RECOVERY, or ONLINE (as detailed below ).
The ADB server is really one giant multiplexing loop whose purpose is to orchestrate the exchange of data (packets, really) between clients, services and devices.
The ADB server can be called a powerful multi-path routing. It aims to coordinate the organization of clients and exchange data (packets, real data) between various services and devices ).
1.2 The ADB daemon (adbd)1.2 ADB daemon (adbd)
The 'adbd 'program runs as a background process within an Android device or emulated system. its purpose is to connect to the ADB server (through USB for devices, through TCP for emulators) and provide a few services for clients that run on the host.
Adbd is a background servo program running on a real android machine or simulator. It aims to connect the pc-side adb server (the real machine uses usb and the simulator uses tcp protocol, this article is not updated in time) and provides some services for the adb client applications running on the host pc.
The ADB server considers that a device is ONLINE when it has successfully connected to the adbd program within it. otherwise, the device is OFFLINE, meaning that the ADB server detected a new device/emulator, but cocould not connect to the adbd daemon.
When the adb server successfully connects to the adbd servo program on the android machine, it will think that the device is online. Otherwise, it will think that the device is offline, the adb server detects a new device connection, but fails to connect to the device's adbd.
The BOOTLOADER and RECOVERY states correspond to alternate states of devices when they are in the bootloader or recovery mode.
Both BOOTLOADER and RECOVERY indicate the available status of the android device in bootloader or recovery mode.
1.3. The ADB command-line client1.3 ADB command line Client
The 'adb' command-line program is used to run adb commands from a shell or a script. it first tries to locate the ADB server on the host machine, and will start one automatically if none is found.
The adb command line client calls shell or script to run various adb commands. First, it will try to find the adb server running on the host pc. If not, it will automatically start an adb server.
Then, the client sends its service requests to the ADB server. It doesn' t need to know.
Then the adb command line client will send service requests to the adb server, which is unknown to the adb server.
Currently, a single 'adb' binary is used for both the server and client. this makes distribution and starting the server easier.
Currently, the adb server and the adb client use the same binary file, which makes it easier to publish and start the server.
1.4. Services1.4. Service
There are essential two kinds of services that a client can talk.
Essentially, an adb command line client will communicate with two types of services.
Host Services: these services run within the ADB Server and thus do not need to communicate with a device at all. A typical example is "adb devices" which is used to return the list of currently known devices and their state. they are a few couple other services though.
Host services: these services run internally on the adb server, so they do not need to interact with any android devices. A typical command is the "adb devices" command used to list all currently connected android devices and statuses ". Of course there are some other services.
Command |
Explanation |
Host: version |
|
Host: kill |
Stop server |
Host: devices |
|
Host: track-devies |
|
Host: emulator: <port> |
|
Host: transport: <serial-number> |
Connect the specified serial-number device or Simulator |
Host: transport-usb |
If there is more than one device on the usb, the connection will fail. |
Host: transport-local |
Connect to the simulator through tcp. If multiple simulators are running, they will fail. |
Host: transport-any |
You can connect to a usb device or simulator. However, if there is more than one device or simulator, it will fail. |
Host-serial: <serial-number >:< request> Host-usb: <request> Host-local: <request> |
Sends a specific request to the specified device. If multiple devices conflict with each other, the operation fails. |
Host: <request> |
Send a request to the currently connected Device |
<Host-prefix>: get-serialno |
Obtain the serial-number of the device. |
<Host-prefix>: get-state |
Get device status |
<Host-prefix >:forward: <local >;< remote> |
|
Local Services: these services either run within the adbd daemon, or are started by it on the device. the ADB server is used to multiplex streams between the client and the service running in adbd. in this case its role is to initiate the connection, then of being a pass-through for the data.
Local services: these services run inside the adbd daemon, or are started and run by the daemon. The adb server routes data between the client and the services running in the adbd. In this case, the adb server plays the role of Initializing Various connections and data tunnels.
Command |
Explanation |
Shell: command arg1 arg2... |
Execute Command Line Operation on the device |
Shell: |
See interactive_shell () in commandline. c () |
Remount: |
Load the file system of the device in read/write mode |
Dev: <path> |
Open a specific path on the device for the client for read and write issues. It may fail due to permission issues. |
Tcp: <port> |
Try to connect a tcp port of the host from the device |
Tcp: <port >:< server-name> |
Try to connect a tcp port from a device to a specific host name |
Local: <path> |
Try to connect to a specific path on the device in the form of a UNIX domain name |
Localreserved: <path> Localabstract: <path> Localfilesystem: <path> |
Try to connect to a specific path on the device. |
Log: <name> |
Open a specific log file on the device to read logs. |
Framebuffer: |
Try to get the snapshot of framebuffer. The screen snapshot of the pen. |
Dns: <server-name> |
The specified device name is parsed by serer. |
Recover: <size> |
Update the recovery image of a device |
Jdwp: <pid> |
Connect the JDWP thread above a specific VM Process |
Track-jdwp |
|
Sync: |
Synchronize files on devices and hosts |
(Note: The above two tables from the netizens arm-linux: http://www.cnblogs.com/armlinux/archive/2011/02/16/2396845.html)
2 Protocol details:
2. protocol details
2.1 Client <-> Server protocol:
2.1 Client <---> Server
This details the protocols used between ADB clients and the ADB server itself. The ADB server listens on TCP: localhost: 5037.
The following details describe the protocols used for communications between the adb client and the adb server in the host pc. The adb server listens to TCP: localhost: 5037.
A client sends a request using the following format:
The client sends a request in the following protocol format:
- 1. A 4-byte hexadecimal string giving the length of the payload
- 1. A 4-byte hexadecimal format is used to specify the length of the Request command.
- 2. Followed by the payload itself.
- 2. Follow the Request command content
For example, to query the ADB server for its internal version number, the client will do the following:
For example, to obtain the internal version number of the adb server, the client performs the following actions:
- 1. Connect to tcp: localhost: 5037
- 1. Connect to tcp: localhost: 5037
- 2. Send the string "000 Chost: version" to the corresponding socket
- 2. Send the string "000 Chost: version" to the corresponding socket (Translator's note: The hexadecimal 000C is 12 in decimal format and "host: version" is exactly 12 bytes)
The 'host: 'prefix is used to indicate that the request is addressed to the server itself (we will talk about other kinds of requests later ). the content length is encoded in ASCII for easier debugging.
The prefix 'host' is used to specify that the request is sent to the server itself (we will talk about other request types later). For convenience of debugging, the request content length is encoded in ASCII format.
The server shocould answer a request with one of the following:
The server will respond in one of the following ways:
- 1. For success, the 4-byte "OKAY" string
- 1. Success: Answer a 4-Byte "OKAY" String
- 2. For failure, the 4-byte "FAIL" string, followed by a 4-byte hex length, followed by a string giving the reason for failure.
- 2. Failed: Answer a 4-Byte "FAIL" string followed by a 4-byte hexadecimal description of the error description content length, followed by a description of the error content string.
- 3. As a special exception, for 'host: version', a 4-byte hex string corresponding to the server's internal version number
- 3. exception: the return value of 'host: version' is a 4-byte string representing the internal version number of the server.
Note that the connection is still alive after an OKAY, which allows the client to make other requests. But in certain cases, an OKAY will even change the state of the connection.
Note that the connection between the client and the server will continue after receiving the OKAY response, so that the client can continue with other requests. However, in some specific situations, the OKAY response changes the connection status.
For example, the case of the 'host: transport: <serialnumber> 'request, where '<serialnumber>' is used to identify a given device/emulator; after the "OKAY" answer, all further requests made by the client will go directly to the corresponding adbd daemon.
For example, if you use the command 'host: transport: <serialnumber> 'as an example (' <serialnumber> 'is used to specify a specified device/simulator), after receiving the 'okay' response, all subsequent requests from the client will be sent directly to the adbd daemon of the corresponding device/simulator.
The file SERVICES. TXT lists all services currently implemented by ADB.
The file SERVICES. TXT lists all the SERVICES that adb has implemented currently ).
2.2. Transports:
An ADB transport models a connection between the ADB server and one device or emulator. There are currently two kinds of transports:
Adb transmission refers to the connection model between the adb server and a device/simulator. There are currently two transmission models:
- USB transports, for physical devices through USB
- USB transmission: when a real machine is connected via usb
- Local transports, for emulators running on the host, connected to the server through TCP
- Local Transmission: When the simulator on the local machine connects to the adb server through tcp
In theory, it shoshould be possible to write a local transport that proxies a connection between an ADB server and a device/emulator connected to/running on another machine. this hasn't been done yet though.
Theoretically, we can write a local transmission proxy to process the connection between the adb server and the device/simulator that connects/runs on another host pc, but this is not yet implemented.
Each transport can carry one or more multiplexed streams between clients and the device/emulator they point to. The ADB server must handle unexpected transport disconnections (e.g. when a device is physically unplugged) properly.
Each transmission mode can transmit data streams between multiple clients and their specified devices/simulators. The adb server must properly handle transmission disconnection and other exceptions (for example, when a device is unplugged from a pc host)
How to execute the adb command in an android Application
The ADB interface is mainly used to allow computers and other devices to control the Android system. Therefore, it is called "Intermediate bridge ";
It is not used by Android, but can be directly executed as SHELL, which has nothing to do with ADB.
Therefore, Android JAVA does not necessarily have encapsulated ADB classes. There are ADB service programs on the computer, port 5037,
It is an intermediate program that communicates with Daemon on the Android system.
If you want to execute the adb command on your mobile phone, you should directly follow the daemon process
(Daemon) communication. The methods that Baidu can find are not satisfactory.
The landlord uses exec to execute the CMD command. This is no longer an ADB interface. This is the SHELL of the system !!!
I do not know the effect of directly sending commands using socket/tcp. The address is 127.0.0.1, and the port of the android daemon process is used.
5555 is the start of an odd number.
... . Baidu can find the ADB dialog protocol. We recommend you try it.
The upstairs actually requires SHELL, not ADB. I found an article, but I have not tried it,
Do you need ROOT? I don't know. Attach it. Please try again.
If you are satisfied, you can adopt it!
What is an adblog file?
The adb log file generated by the system for debugging reference by developers.
This file can be deleted if it is not a developer.