linux应用程序检测usb热插拔事件

#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
#include <sys/socket.h>
 
/* Kernel Netlink */
int CUSBListener_initSock()
{
    const int buffersize = UEVENT_BUFFER_SIZE;
    int ret;
    int on = 1;
 
    struct sockaddr_nl snl;
    bzero(&snl, sizeof(struct sockaddr_nl));
    snl.nl_family = AF_NETLINK;
    snl.nl_pid = getpid();
    //snl.nl_groups = 1|RTNLGRP_LINK;
    //snl.nl_groups = 1;
    snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
 
    int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if (s == -1)
    {
		return -1;
    }
    if (setsockopt(s, SOL_SOCKET, /*SO_RCVBUFFORCE*/ SO_RCVBUF, &buffersize, sizeof(buffersize)) < 0)
    {
		perror("setsockopet error\n");
		return -1;
    }
    if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))<0)
    {
		perror("setsockopet error\n");
		return -1;
    }
 
    ret = bind(s, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
    if (ret < 0)
    {
		return -2;
    }
    return s;
}
 
void CUSBListener_onUSB(const char *msg)
{
    if (!memcmp(msg, "add@", 4))
    {
		// 识别 U盘
		if (!memcmp(&msg[strlen(msg)-5], "/sd", 3)) {
			printf("Found U Disk\n");
			printf("%s\n",&msg[strlen(msg) - 5]);
		} 
    } 
    else  if (!memcmp(msg,"remove@",7))
    {
		if (!memcmp(&msg[strlen(msg) - 5],"/sd",3)) {
			printf("remove U Disk\n");
			printf("%s\n",&msg[strlen(msg) - 5]);
		} 
    }
}
 
/* listener for USB Event message*/
void CUSBListener_run()
{
    char buf[UEVENT_BUFFER_SIZE * 2] = {0};
 
    int sock = CUSBListener_initSock();
 
    while(sock > 0)
    {
		/* Netlink message buffer */
		int b = recv(sock, &buf, sizeof(buf),0);
		if(b > 0)
		{
			//printf("%s\n", buf);
			CUSBListener_onUSB(buf);
		}
    }
    perror("Create Netlink failed:" );
}

接收到的内核的报文内容

插入

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/scsi_host/host6

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0

add@/devices/virtual/bdi/8:0

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/block/sda

add@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/block/sda/sda1

Found U Disk

/sda1

add@/devices/virtual/bdi/8:1-fuseblk

add@/devices/virtual/bdi/0:28

add@/devices/virtual/bdi/0:29

add@/devices/virtual/bdi/0:30

——————

删除

——————

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/block/sda/sda1

remove U Disk

/sda1

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0/block/sda

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0/6:0:0:0

remove@/devices/virtual/bdi/8:0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/target6:0:0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6/scsi_host/host6

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host6

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4/1-1.4:1.0

remove@/devices/platform/fe3c0000.usb/usb1/1-1/1-1.4

remove@/devices/virtual/bdi/0:28

remove@/devices/virtual/bdi/0:29

remove@/devices/virtual/bdi/0:30

remove@/devices/virtual/bdi/8:1-fuseblk
————————————————

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Qt中实现USB热插拔检测,可以使用QDeviceWatcher类。QDeviceWatcher是一个用于监测设备插入和移除事件的类。 首先,确保在项目文件(.pro)中添加以下内容以启用Qt的udev库: ```cpp LIBS += -ludev ``` 然后,可以按照以下步骤在Qt中实现USB热插拔检测: 1. 在头文件中包含必要的头文件: ```cpp #include <QObject> #include <QDeviceWatcher> ``` 2. 创建一个类并继承自QObject: ```cpp class USBWatcher : public QObject { Q_OBJECT public: explicit USBWatcher(QObject *parent = nullptr); private slots: void deviceAdded(const QString &devPath); void deviceRemoved(const QString &devPath); private: QDeviceWatcher *m_deviceWatcher; }; ``` 3. 在实现文件中定义构造函数,并在构造函数中初始化QDeviceWatcher对象,并连接相应的槽函数: ```cpp USBWatcher::USBWatcher(QObject *parent) : QObject(parent) { m_deviceWatcher = new QDeviceWatcher(this); connect(m_deviceWatcher, SIGNAL(deviceAdded(QString)), this, SLOT(deviceAdded(QString))); connect(m_deviceWatcher, SIGNAL(deviceRemoved(QString)), this, SLOT(deviceRemoved(QString))); m_deviceWatcher->start(); } ``` 4. 实现设备插入和移除的槽函数: ```cpp void USBWatcher::deviceAdded(const QString &devPath) { // 处理设备插入事件 qDebug() << "Device added: " << devPath; } void USBWatcher::deviceRemoved(const QString &devPath) { // 处理设备移除事件 qDebug() << "Device removed: " << devPath; } ``` 5. 在你的应用程序中实例化USBWatcher对象,并将其保持活动状态: ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); USBWatcher usbWatcher; return app.exec(); } ``` 通过以上步骤,你现在可以在`deviceAdded()`和`deviceRemoved()`槽函数中处理设备插入和移除事件。你可以根据需求来执行一些自定义的操作,比如更新UI或者执行特定的任务。 请注意,USB热插拔检测需要在具有相应权限的操作系统上运行,如Linux。在不同的操作系统和平台上,可能需要使用不同的方法来实现设备监测。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值