设为首页 加入收藏

TOP

Android 2.3 ADB源码分析(一)
2014-11-24 14:36:54 来源: 作者: 【 】 浏览:6
Tags:Android 2.3 ADB 源码 分析

其完整源代码路径: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 - copy file/dir to device
adb pull [] - copy file/dir from device
adb shell - run remote shell interactively
adb install [-l] [-r] [-s] - push this package file to the device and install it
adb kill-server - kill the server if it is running
connect [:] - 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);

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇浅谈Android开发的技术层次 下一篇Android 图形系统加速学习系列

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: