打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android adb源码分析(2)
版权声明:☆☆☆原创文章,转载请注明来自: https://blog.csdn.net/xgbing/article/details/52059755

本篇分析usb_linux_client.c中的usb_init(),它的代码如下:

  1. void usb_init()
  2. {
  3. if (access(USB_FFS_ADB_EP0, F_OK) == 0)
  4. {
  5. usb_ffs_init();
  6. }
  7. else{
  8. usb_adb_init();
  9. }
  10. }

调用usb_adb_init():
  1. static void usb_adb_init()
  2. {
  3. usb_handle *h;
  4. adb_thread_t tid;
  5. int fd;
  6. h = calloc(1, sizeof(usb_handle));
  7. h->write = usb_adb_write;
  8. h->read = usb_adb_read;
  9. h->kick = usb_adb_kick;
  10. h->fd = -1;
  11. adb_cond_init(&h->notify, 0);
  12. adb_mutex_init(&h->lock, 0);
  13. // Open the file /dev/android_adb_enable to trigger
  14. // the enabling of the adb USB function in the kernel.
  15. // We never touch this file again - just leave it open
  16. // indefinitely so the kernel will know when we are running
  17. // and when we are not.
  18. fd = unix_open("/dev/android_adb_enable", O_RDWR);
  19. if (fd < 0) {
  20. D("failed to open /dev/android_adb_enable\n");
  21. } else {
  22. close_on_exec(fd);
  23. }
  24. D("[ usb_init - starting thread ]\n");
  25. if(adb_thread_create(&tid, usb_adb_open_thread, h)){
  26. fatal_errno("cannot create usb thread");
  27. }
  28. }

它初始化了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的代码如下:

  1. static void *usb_adb_open_thread(void *x)
  2. {
  3. struct usb_handle *usb = (struct usb_handle *)x;
  4. int fd;
  5. while (1) {
  6. // wait until the USB device needs opening
  7. adb_mutex_lock(&usb->lock);
  8. while (usb->fd != -1)
  9. adb_cond_wait(&usb->notify, &usb->lock);
  10. adb_mutex_unlock(&usb->lock);
  11. D("[ usb_thread - opening device ]\n");
  12. do {
  13. /* XXX use inotify? */
  14. fd = unix_open("/dev/android_adb", O_RDWR);
  15. if (fd < 0) {
  16. // to support older kernels
  17. fd = unix_open("/dev/android", O_RDWR);
  18. }
  19. if (fd < 0) {
  20. adb_sleep_ms(1000);
  21. }
  22. } while (fd < 0);
  23. D("[ opening device succeeded ]\n");
  24. close_on_exec(fd);
  25. usb->fd = fd;
  26. D("[ usb_thread - registering device ]\n");
  27. register_usb_transport(usb, 0, 0, 1);
  28. }
  29. // never gets here
  30. return 0;
  31. }

这个线程的作用是一进入立即打开/dev/android_adb或/dev/android,如果成功,则调用register_usb_transport()后再次循环,并阻塞在以下代码处

while(usb->fd != -1)

       adb_cond_wait(&usb->notify, &usb->lock);

当usb->kick()调用后fd的值被赋为-1,并发送cond唤醒上面的代码。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
打造N台云手机
Android 让adb logcat打印内核调试信息
Android模拟器中安装busybox的最简方法
Android内核开发:如何统计系统的启动时间
adb打开关闭
Ubuntu 开启 Android 的 USB 调试模式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服