此文主要针对嵌入式板测USB端口需求,当然linux PC设备也同样适用。
目的是 检测当前 设备的USB端口是否正常,插入相应设备是否能够正常挂载。
作者主要想到了两种办法,第一种通过脚本命令,第二种则是通过C代码来实现检测USB的热插拔。
先来说第一种方法—-lsusb命令
———-华丽分割线———
通过lsusb命令我们可以实现某个USB端口的设备检测,一般嵌入式设备通常有几个USB总线端口,例如001 002 003总线,而通过下面的命令则可以查看001总线下的USB设备:
// 命令行 lsusb -s 002: //运行结果: root@ubuntu:~# lsusb -s 001: Bus 001 Device 039: ID 203a:fffa Bus 001 Device 038: ID 203a:fffa Bus 001 Device 037: ID 203a:fff9 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub //当我们在该总线USB端口插入一个USB无线网卡,再次执行该命令,结果如下: root@ubuntu:~# lsusb -s 001: Bus 001 Device 043: ID 0bda:8179 Realtek Semiconductor Corp. RTL8188EUS 802.11n Wireless Network Adapter Bus 001 Device 039: ID 203a:fffa Bus 001 Device 038: ID 203a:fffa Bus 001 Device 037: ID 203a:fff9 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub //从结果可以看到多出一个043的USB设备,描述可以看出是一个型号为RTL8188EUS的无线网卡
以上就是第一种方法,而通过这个命令我们也可以用代码封装,解析返回结果,分析端口信息等,这里就不多做描述了,大家自己探索吧。
———-华丽分割线———
第二种方法,是通过 文件描述法 也就是常常了解的套接字,代码如下:
// // USB.c // // // Created by huangea on 2018/4/20. // #include#include #include #include #include #include int main(void) { struct sockaddr_nl clt; struct timeval t1; fd_set fd; int buf_size = 2048, len, ret, usb_test; //创建套接字 usb_test = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT); memset(&clt, 0, sizeof(clt)); clt.nl_pid = getpid(); clt.nl_family = AF_NETLINK; clt.nl_groups = 1; setsockopt(usb_test, SOL_SOCKET, SO_RCVBUF, &buf_size, sizeof(buf_size)); //绑定套接字 bind(usb_test, (struct sockaddr*)&clt, sizeof(clt)); while (1) { char buf[2048]; memset(&buf,0,sizeof(buf)); FD_ZERO(&fd); FD_SET(usb_test, &fd); //监测频率周期 t1.tv_sec = 0; t1.tv_usec = 50 * 1000; //监测 ret = select(usb_test + 1, &fd, NULL, NULL, &t1); if(ret < 0) continue; if(!(ret > 0 && FD_ISSET(usb_test, &fd))) continue; len = recv(usb_test, &buf, sizeof(buf), 0); if (len > 0) { printf("%s\n", buf);//打印收到的数据 } } close(usb_test); return 0; }
测试以上代码,当插入及拔出USB设备,程序输出结果如下:
root@ubuntu:~# ./usb.out //此时插入USB无线网卡 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/rx-0 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/rx-1 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/rx-2 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/rx-3 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/tx-0 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/tx-1 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/tx-2 add@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlan0/queues/tx-3 move@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008 //此时移除USB网卡 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/rx-3 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/rx-2 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/rx-1 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/rx-0 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/tx-3 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/tx-2 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/tx-1 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008/queues/tx-0 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0/net/wlx0013ef800008 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4:1.0 remove@/devices/pci0000:00/0000:00:1d.7/usb1/1-4
通过以上实验,进行结果分析,依然可以检测当前USB总线是否正常。