linux查看USB运行状态,Linux下获取USB设备插拔状态的通知

最近遇到一个需求,要求浏览器的内嵌keyboard与USB keyboard状态同步。

主要就是几个特殊的功能键,shift、alt、ctrl、caps、tab等等,借鉴了一个博客:http://www.voidcn.com/article/p-sjjkjzze-cy.html,其中提供了一段获取USB设备的代码,自己运行过后,有一定的帮助,具体的还需要深究。代码如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define UEVENT_BUFFER_SIZE 2048

static int init_hotplug_sock() {

const int buffersize = 1024;

int ret;

struct sockaddr_nl snl;

bzero(&snl, sizeof(struct sockaddr_nl));

snl.nl_family = AF_NETLINK;

snl.nl_pid = getpid();

snl.nl_groups = 1;

int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);

if (s == -1) {

perror("socket");

return -1;

}

setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));

ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));

if (ret < 0) {

perror("bind");

close(s);

return -1;

}

return s;

}

int main(int argc, char* argv[]) {

int hotplug_sock = init_hotplug_sock();

while(1) {

/* Netlink message buffer */

char buf[UEVENT_BUFFER_SIZE * 2] = {0};

recv(hotplug_sock, &buf, sizeof(buf), 0); printf("%s\n", buf);

/* USB 设备的插拔会出现字符信息,通过比较不同的信息确定特定设备的插拔,在这添加比较代码 */

}

return 0;

}

编译之后,运行的结果如下:

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:046D:C31C.0006

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input6

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input6/event1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:046D:C31C.0006/hidraw/hidraw0

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/0003:046D:C31C.0007

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/input/input7

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/input/input7/event2

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/0003:046D:C31C.0007/hidraw/hidraw1

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input6/event1

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input6

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:046D:C31C.0006/hidraw/hidraw0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:046D:C31C.0006

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/input/input7/event2

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/input/input7

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/0003:046D:C31C.0007/hidraw/hidraw1

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1/0003:046D:C31C.0007

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.1

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:03F0:1F4A.0008

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8/mouse0

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8/event1

add@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:03F0:1F4A.0008/hidraw/hidraw0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8/mouse0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8/event1

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/input/input8

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:03F0:1F4A.0008/hidraw/hidraw0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0/0003:03F0:1F4A.0008

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1/2-1:1.0

remove@/devices/platform/xxxxx-ehci-1.0/usb2/2-1

前面的add和remove是USB Keyboard的,后面是USB Mouse的,从中可以看到有Mouse的信息,其中xxxxx是平台相关定义的,打印出来,但是Keyboard插拔时,没有能够体现Keyboard的信息。

还需要继续深究,先做一个记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值