android adb源码分析(2)
2016-07-28 22:22
597 查看
本篇分析usb_linux_client.c中的usb_init(),它的代码如下:void usb_init()
{
if (access(USB_FFS_ADB_EP0, F_OK) == 0)
{
usb_ffs_init();
}
else{
usb_adb_init();
}
}
调用usb_adb_init():
它初始化了usb_handle,并把它作为参数创建usb_adb_open_thread()线程。这里不能打开/dev/android_adb_enable。h->fd的值会在线程usb_adb_open_thread中赋值,并把它做为h->write(), h->read(), h->kick()的文件句柄,h->kick()函数功能是把h->fd置为-1。usb_adb_open_thread的代码如下:static void *usb_adb_open_thread(void *x)
{
struct usb_handle *usb = (struct usb_handle *)x;
int fd;
while (1) {
// wait until the USB device needs opening
adb_mutex_lock(&usb->lock);
while (usb->fd != -1)
adb_cond_wait(&usb->notify, &usb->lock);
adb_mutex_unlock(&usb->lock);
D("[ usb_thread - opening device ]\n");
do {
/* XXX use inotify? */
fd = unix_open("/dev/android_adb", O_RDWR);
if (fd < 0) {
// to support older kernels
fd = unix_open("/dev/android", O_RDWR);
}
if (fd < 0) {
adb_sleep_ms(1000);
}
} while (fd < 0);
D("[ opening device succeeded ]\n");
close_on_exec(fd);
usb->fd = fd;
D("[ usb_thread - registering device ]\n");
register_usb_transport(usb, 0, 0, 1);
}
// never gets here
return 0;
}
这个线程的作用是一进入立即打开/dev/android_adb或/dev/android,如果成功,则调用register_usb_transport()后再次循环,并阻塞在以下代码处
while(usb->fd != -1)
adb_cond_wait(&usb->notify, &usb->lock);
当usb->kick()调用后fd的值被赋为-1,并发送cond唤醒上面的代码。
{
if (access(USB_FFS_ADB_EP0, F_OK) == 0)
{
usb_ffs_init();
}
else{
usb_adb_init();
}
}
调用usb_adb_init():
static void usb_adb_init() { usb_handle *h; adb_thread_t tid; int fd; h = calloc(1, sizeof(usb_handle)); h->write = usb_adb_write; h->read = usb_adb_read; h->kick = usb_adb_kick; h->fd = -1; adb_cond_init(&h->notify, 0); adb_mutex_init(&h->lock, 0); // Open the file /dev/android_adb_enable to trigger // the enabling of the adb USB function in the kernel. // We never touch this file again - just leave it open // indefinitely so the kernel will know when we are running // and when we are not. fd = unix_open("/dev/android_adb_enable", O_RDWR); if (fd < 0) { D("failed to open /dev/android_adb_enable\n"); } else { close_on_exec(fd); } D("[ usb_init - starting thread ]\n"); if(adb_thread_create(&tid, usb_adb_open_thread, h)){ fatal_errno("cannot create usb thread"); } }
它初始化了usb_handle,并把它作为参数创建usb_adb_open_thread()线程。这里不能打开/dev/android_adb_enable。h->fd的值会在线程usb_adb_open_thread中赋值,并把它做为h->write(), h->read(), h->kick()的文件句柄,h->kick()函数功能是把h->fd置为-1。usb_adb_open_thread的代码如下:static void *usb_adb_open_thread(void *x)
{
struct usb_handle *usb = (struct usb_handle *)x;
int fd;
while (1) {
// wait until the USB device needs opening
adb_mutex_lock(&usb->lock);
while (usb->fd != -1)
adb_cond_wait(&usb->notify, &usb->lock);
adb_mutex_unlock(&usb->lock);
D("[ usb_thread - opening device ]\n");
do {
/* XXX use inotify? */
fd = unix_open("/dev/android_adb", O_RDWR);
if (fd < 0) {
// to support older kernels
fd = unix_open("/dev/android", O_RDWR);
}
if (fd < 0) {
adb_sleep_ms(1000);
}
} while (fd < 0);
D("[ opening device succeeded ]\n");
close_on_exec(fd);
usb->fd = fd;
D("[ usb_thread - registering device ]\n");
register_usb_transport(usb, 0, 0, 1);
}
// never gets here
return 0;
}
这个线程的作用是一进入立即打开/dev/android_adb或/dev/android,如果成功,则调用register_usb_transport()后再次循环,并阻塞在以下代码处
while(usb->fd != -1)
adb_cond_wait(&usb->notify, &usb->lock);
当usb->kick()调用后fd的值被赋为-1,并发送cond唤醒上面的代码。
相关文章推荐
- Android如何判断时间是否在0到8点之间
- Android中过场动画
- Android 四大图片缓存原理,特性对比
- Android中Handler造成内存泄露解决方法
- Json数据格式介绍、使用Android自带类库完成Json生成与解析
- Android的四大组件之-Activity的状态和生命周期
- Android Toast优化,不看别后悔
- Android基本问题汇总
- 设置背景渐变色
- android轮播图的实现
- android连接天天模拟器方法
- android异常收集-关于fragment导入包出错的问题
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- Android动画效果设置
- android异常收集-java.lang.RuntimeException: Fail to get camera info
- 将当前界面的可视组件保存在png图像文件中
- Android_Spinner_example
- 如何保证Android与服务器的DES加密保持一致
- android异常收集-关于导入文件出现错误,运行不起来
- Android屏幕适配全攻略(最权威的官方适配指导)