ADB是Android debug bridge的缩写,负责计算机与Android设备的几乎所有通信和协作,可以认为是连接两者的桥梁。其完整源代码路径:systemcoreadb1、首先查看其Android.mk文件,确认会生成哪此文件。可执行进程由两部分组成:LOCAL_MODULE := adbinclude $(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处理函数都在:systemcoreadbCommandline.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.cvoid init_transport_registration(void) { adb_socketpair(s) //创建socket pair用于处理异步注册事件