歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 2.3 ADB源碼分析

Android 2.3 ADB源碼分析

日期:2017/3/1 10:40:06   编辑:Linux編程

ADB是Android debug bridge的縮寫,負責計算機與Android設備的幾乎所有通信和協作,可以認為是連接兩者的橋梁。

其完整源代碼路徑:system\core\adb

1、首先查看其Android.mk文件,確認會生成哪此文件。

可執行進程由兩部分組成:

LOCAL_MODULE := adb

include $(BUILD_HOST_EXECUTABLE)
adb或adb.exe,運行於PC端,包括Linux、Windows、Mac OS等系統之中,通常是x86架構上

LOCAL_MODULE := adbd
ifeq ($(TARGET_SIMULATOR),true)
include $(BUILD_HOST_EXECUTABLE)
else
include $(BUILD_EXECUTABLE)
endif
adbd,運行於Android設備的底層Linux平台上,或者運行於虛擬機平台上


盒子上如此運行:init.rc 腳本添加:
service adbd /sbin/adbd
disabled

利用ADB_HOST宏定義編譯不同的代碼

2、主體介紹一下
現在分析一下整個adb通訊由哪些模塊或組件構成呢?

一、adb server 服務端:
這是一個守護進程長期運行於後台(runs on the host machine),沒有控制台界面.
其主要工作有兩部分:

1):管理PC中的Android模擬器,以及通過USB線連接到PC的Android設備,負責維持運行於其中的
adbd進程與自身的數據通道;
2):實現PC與設備/模擬器之間的數據拷貝。

主要代碼文件:

二、adb client 客戶端:
提供給用戶的命令行工具,對用戶暴露了上述install、push、shell等接口,與用戶交互
其主要工作是解析這些命令的參數,做必要預處理,然後轉移為指令或數據,發送給adb服務端。
adb服務端再將指令數據轉發到模擬器或設備中,由adbd處理,產生結果,再通過adb服務端接收回來。


三、adb service 服務:
由此服務給adbd提供功能,即由這個模塊完成,主要分為Host Services及 Local Services兩類


四、ADB daemon (adbd) 守護進程
作為後台進程運行於Android device or emulated system,提供連接 ADB server的功能
(through USB for devices, through TCP for emulators)

其通訊典型結構如下:

3、以常用命令為實例
常用的指令如下:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> [<local>] - copy file/dir from device
adb shell - run remote shell interactively
adb install [-l] [-r] [-s] <file> - push this package file to the device and install it
adb kill-server - kill the server if it is running
connect <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number is specified.


所有這些cmd處理函數都在:\system\core\adb\Commandline.c 中:
int adb_commandline(int argc, char **argv)

為了更好的理解這些命令,有必須找到代碼理解一下這些命令的處理主要函數:

函數啟動點: adb.c 中的main函數,根據ADB_HOST決定執行哪些代碼:
int main(int argc, char **argv)
{
...
adb_trace_init();
#if ADB_HOST
adb_sysdeps_init();
return adb_commandline(argc - 1, argv + 1); //這裡運行PC端,用於命令發送
#else
start_device_log();
return adb_main(0, DEFAULT_ADB_PORT); //運行於android系統的盒子或設備上,用於命令接收及反饋
#endif
}

先分析PC端這邊:
a、首先建立adb server:
有兩種方式,
手工建立:adb fork-server server 調用:adb_main(is_daemon, server_port);
默認5037端口,也可以設置:service.adb.tcp.port 這個屬性獲取
自動建立:調用 launch_server(server_port),利用 CreateProcess 或者fork建立後台進程進行運行
// child process
int result = execl(path, "adb", "fork-server", "server", NULL);
這個進程利用fdevent_loop();處理所有數據及消息

b、ADB command-line client即發送命令界面:
主要處理函數:int adb_commandline(int argc, char **argv)
主要利用如下幾個函數:
adb_query 查詢
adb_connect 連接
adb_status 獲取狀態

命令發送格式:
1. A 4-byte hexadecimal string giving the length of the payload
2. Followed by the payload itself.


服務端收到後回復:
1. For success, the 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.
3. As a special exception, for 'host:version', a 4-byte
hex string corresponding to the server's internal version number

以上兩者通訊利用socket進行數據傳遞

然後分析設備服務端:
主要集中在函數:fdevent_loop();


主要分析一下數據transport這塊,文件Transport.c

void init_transport_registration(void)
{
adb_socketpair(s) //創建socket pair用於處理異步注冊事件

transport_registration_send = s[0];
transport_registration_recv = s[1];


// 在transport_registration_recv上安裝一個transport_registration_func異步事情回調函數
fdevent_install(&transport_registration_fde,
transport_registration_recv,
transport_registration_func,
0);

...
}

異步如何觸發的呢?
register_transport
-->transport_write_action(transport_registration_send, &m)

remove_transport
-->transport_write_action(transport_registration_send, &m)


此時會將事件寫入socket pair的寫入端,而接收端:

void fdevent_loop()
{
...

for(;;) {

while((fde = fdevent_plist_dequeue())) {
unsigned events = fde->events;
fde->events = 0;
fde->state &= (~FDE_PENDING);
dump_fde(fde, "callback");

//這個回調函數是在:fdevent_install 函數中注冊的:fde->func = func;
fde->func(fde->fd, events, fde->arg);
}
}
}

然後利用transport_read_action讀取異步事情,那麼數據處理在哪裡呢?

transport_registration_func
-->
/* don't create transport threads for inaccessible devices */
if (t->connection_state != CS_NOPERM) {

if(adb_thread_create(&input_thread_ptr, input_thread, t)){
fatal_errno("cannot create input thread");
}


if(adb_thread_create(&output_thread_ptr, output_thread, t)){
fatal_errno("cannot create output thread");
}
}

在這裡會創建兩個線程 output thread和 input thread用於做異步 IO,

Copyright © Linux教程網 All Rights Reserved