利用NETLINK检测USB热插拔的C语言实现

Posted by 老沙
8月 20 2017

做嵌入式开发,尤其在网关、路由器或者其他支持USB设备的终端上,为了提高用户体验,我们常常需要支持自动识别并挂载USB设备功能。某些应用程序,在使用USB设备的过程中,也希望能够侦测到USB断开事件,不至于某些工作因为USB已经不存在而白做。在Linux下,我们主要有两种办法检测USB热插拔。
第一种便是定时检查/proc/scsi/scsi文件,该文件内会按照标准格式保存着当前设备内挂载的存储介质基本信息,如果在PC端,除了硬盘(ATA)、光驱(CD-ROM)外,就是USB设备(Direct-Access)了,轮询该scsi文件,检查文件内是否新增或减少数据便可实现自动侦测USB热插拔的效果。但是这种方法对于热插拔(hotplug)设备,如U盘,效果就没那么理想了,因为我们不知道设备什么时候插上,又是什么时候被拔掉了,只能验证当前是否已经插上或者已经拔除的事实。于是便有了另一种办法,我们采用一种特殊类的的文件描述符(套结字)专门用于Linux内核跟用户空间之间的异步通信,这种技术通常被成为NETLINK。
由于NETLINK是linux内置功能,所以使用起来很简单:创建一个AF_NETLINK协议族下NETLINK_KOBJECT_UEVENT类型的特殊文件描述符(套结字)CppLive,然后利用setsocketopt允许该文件描述符(套结字)复用其他端口,再利用band函数将自身进程绑定到特殊文件描述符(套结字)CppLive,最后利用select在while循环内监听CppLive是否可读,如果可读则调用recv接收Linux系统内核传递过来的数据并打印出来,这些输出便是USB热插拔信息。当然你也可以个性化地处理来自内核的热插拔信息,让程序变得更加智能以及人性化。

利用NETLINK检测USB热插拔的C语言实现代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/socket.h>
  6. #include <linux/netlink.h>
  7. #define UEVENT_BUFFER_SIZE 2048
  8. int main(void)
  9. {
  10.     struct sockaddr_nl client;
  11.     struct timeval tv;
  12.     int CppLive, rcvlen, ret;
  13.     fd_set fds;
  14.     int buffersize = 1024;
  15.     CppLive = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
  16.     memset(&client, 0, sizeof(client));
  17.     client.nl_family = AF_NETLINK;
  18.     client.nl_pid = getpid();
  19.     client.nl_groups = 1; /* receive broadcast message*/
  20.     setsockopt(CppLive, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
  21.     bind(CppLive, (struct sockaddr*)&client, sizeof(client));
  22.     while (1) {
  23.         char buf[UEVENT_BUFFER_SIZE] = { 0 };
  24.         FD_ZERO(&fds);
  25.         FD_SET(CppLive, &fds);
  26.         tv.tv_sec = 0;
  27.         tv.tv_usec = 100 * 1000;
  28.         ret = select(CppLive + 1, &fds, NULL, NULL, &tv);
  29.         if(ret < 0)
  30.             continue;
  31.         if(!(ret > 0 && FD_ISSET(CppLive, &fds)))
  32.             continue;
  33.         /* receive data */
  34.         rcvlen = recv(CppLive, &buf, sizeof(buf), 0);
  35.         if (rcvlen > 0) {
  36.             printf(“%s\n”, buf);
  37.             /*You can do something here to make the program more perfect!!!*/
  38.         }
  39.     }
  40.     close(CppLive);
  41.     return 0;
  42. }

运行程序,测试U盘插入/拔除,输出如下:

  1. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1
  2. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0
  3. add@/module/usb_storage
  4. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6
  5. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/scsi_host/host6
  6. add@/bus/usb/drivers/usb-storage
  7. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0
  8. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0
  9. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0
  10. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0
  11. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg2
  12. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0
  13. change@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0
  14. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/block/sdb
  15. add@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb4
  16. add@/devices/virtual/bdi/8:16
  17. add@/module/fat
  18. add@/kernel/slab/fat_cache
  19. add@/kernel/slab/fat_inode_cache
  20. add@/module/vfat
  21. add@/module/nls_cp437
  22. add@/module/nls_iso8859_1
  23. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0
  24. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg2
  25. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0
  26. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0
  27. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb4
  28. remove@/devices/virtual/bdi/8:16
  29. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0/block/sdb
  30. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/target6:0:0/6:0:0:0
  31. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6/scsi_host/host6
  32. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host6
  33. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0
  34. remove@/devices/pci0000:00/0000:00:1d.7/usb2/2-1
  35. remove@/host6/target6:0:0

Trackback URL for this entry