通过IOKit监听Mac电脑上的USB驱动事件

Mac电脑如何监听USB设备连接和断开呢?

首先需要区分是监听USB设备还是disk(U盘、SD卡等),如果为后者,则可以直接通过系统通知,而不需要调用IOKit监听,方法如下:

一、监听可装载的USB设备(U盘、SD卡等)

1.注册通知

// Notification for Mountingthe USB device

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(deviceMounted:)  name: NSWorkspaceDidMountNotification object: nil];

 // Notification for Un-Mountingthe USB device
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(deviceUnmounted:)  name: NSWorkspaceDidUnmountNotification object: nil];

2.设备挂载后收到通知

NSArray* devices = [[NSWorkspacesharedWorkspace] mountedRemovableMedia];

3.获取所有已挂载USB的卷路径之后,识别已挂载USB。

#import <IOKit/usb/IOUSBLib.h>
#import <IOKit/IOCFPlugIn.h>

// The following code will return an array having configured Ids and Name of all the mounted USB devices.
-(NSArray *) deviceAttributes {

    mach_port_t masterPort;
    CFMutableDictionaryRef matchingDict;
    NSMutableArray * devicesAttributes = [NSMutableArray array];
    kern_return_t kr;

    //Create a master port for communication with the I/O Kit
    kr = IOMasterPort (MACH_PORT_NULL, &masterPort);
    if (kr || !masterPort) {
        NSLog (@"Error: Couldn't create a master I/O Kit port(%08x)", kr);
        return devicesAttributes;
    }
    
    //Set up matching dictionary for class IOUSBDevice and its subclasses
    matchingDict = IOServiceMatching (kIOUSBDeviceClassName);
    if (!matchingDict) {
        NSLog (@"Error: Couldn't create a USB matching dictionary");
        mach_port_deallocate(mach_task_self(), masterPort);
        return devicesAttributes;
    }

io_iterator_t iterator;
IOServiceGetMatchingServices (kIOMasterPortDefault, matchingDict, &iterator);
io_service_t usbDevice;

//Iterate for USB devices
    while (usbDevice = IOIteratorNext (iterator)) {
IOCFPlugInInterface**plugInInterface = NULL;
SInt32 theScore;

//Create an intermediate plug-in
        kr = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &theScore);
        
        if ((kIOReturnSuccess != kr) || !plugInInterface)
            printf("Unable to create a plug-in (%08x)\n", kr);

IOUSBDeviceInterface182 **dev = NULL;

        //Create the device interface
HRESULT result = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID)&dev);

        if (result || !dev)
            printf("Couldn't create a device interface (%08x)\n", (int) result);
            
UInt16 vendorId;

UInt16 productId;

UInt16 releaseId;

        //Get configuration Ids of the device
        (*dev)->GetDeviceVendor(dev, &vendorId);
        (*dev)->GetDeviceProduct(dev, &productId);
        (*dev)->GetDeviceReleaseNumber(dev, &releaseId);
UInt8 stringIndex;

(*dev)->USBGetProductStringIndex(dev, &stringIndex);

IOUSBConfigurationDescriptorPtr descriptor;

        (*dev)->GetConfigurationDescriptorPtr(dev, stringIndex, &descriptor);
        
//Get Device name
io_name_t deviceName;

kr = IORegistryEntryGetName (usbDevice, deviceName);

if (kr != KERN_SUCCESS) {

NSLog (@"fail 0x%8x", kr);
deviceName[0] = '\0';
}
NSString * name = [NSStringstringWithCString:deviceName encoding:NSASCIIStringEncoding];

//data will be initialized only for USB storage devices.
//bsdName can be converted to mounted path of the device and vice-versa using DiskArbitration framework, hence we can identify the device through it's mounted path

CFTypeRef data = IORegistryEntrySearchCFProperty(usbDevice, kIOServicePlane, CFSTR("BSD Name"), kCFAllocatorDefault, kIORegistryIterateRecursively);

NSString* bsdName = [(NSString*)data substringToIndex:5];

NSString* attributeString = @"";
if(bsdName)
attributeString = [NSString stringWithFormat:@"%@,%@,0x%x,0x%x,0x%x", name, bsdName, vendorId, productId, releaseId];
else
attributeString = [NSString stringWithFormat:@"%@,0x%x,0x%x,0x%x", name, vendorId, productId, releaseId];

[devicesAttributes addObject:attributeString];
        IOObjectRelease(usbDevice);

        (*plugInInterface)->Release(plugInInterface);

        (*dev)->Release(dev);
    }

    //Finished with master port

    mach_port_deallocate(mach_task_self(), masterPort);

    masterPort = 0;

return devicesAttributes;
}

二、监听所有USB设备(包括键盘、鼠标等)

方法如下:

- (void)ListenUsbConnectEvent {
    //dictionary
    CFMutableDictionaryRef matchingDict = matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    
    //create notification
    IONotificationPortRef notificationObject; //notification object to listen
    mach_port_t masterPort = 0; //received from IOMasterPort
    notificationObject = IONotificationPortCreate(masterPort);
    
    //create run loop
    CFRunLoopSourceRef notificationRunLoopSource;
    
    //use notification obejct received from notificationPortCreate
    notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationObject);
    
    CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);
    io_iterator_t iter;
    
    CFRetain(matchingDict);
    IOServiceAddMatchingNotification(notificationObject, kIOFirstMatchNotification, matchingDict, isAttached, (__bridge void*)self, &iter);
    isAttached(NULL, iter);
    
    IOServiceAddMatchingNotification(notificationObject,kIOTerminatedNotification, matchingDict, isRemoved, (__bridge void*)self, &iter);
    isRemoved(NULL, iter);
}

void isAttached(void *refcon, io_iterator_t iterator) {
    
    io_service_t usbDevice;
    while((usbDevice = IOIteratorNext(iterator))) {
        io_name_t name;
        IORegistryEntryGetName(usbDevice, name);
        printf("\tName:\t\t%s\n", (char *)name);
        
        CFNumberRef idProduct = (CFNumberRef)IORegistryEntrySearchCFProperty(usbDevice, kIOServicePlane, CFSTR("idProduct"), kCFAllocatorDefault, 0);
        uint16_t PID;
        CFNumberGetValue(idProduct, kCFNumberSInt16Type, (void *)&PID);
        printf("\tidProduct:\t0x%x\n", PID);
        
        IOObjectRelease(usbDevice);
        CFRelease(idProduct);
    }
}

void isRemoved(void *refcon, io_iterator_t iterator) {
    
    io_service_t usbDevice;
    while((usbDevice = IOIteratorNext(iterator))) {
        io_name_t name;
        IORegistryEntryGetName(usbDevice, name);
        printf("\tName:\t\t%s\n", (char *)name);
        
        CFNumberRef idProduct = (CFNumberRef)IORegistryEntrySearchCFProperty(usbDevice, kIOServicePlane, CFSTR("idProduct"), kCFAllocatorDefault, 0);
        uint16_t PID;
        CFNumberGetValue(idProduct, kCFNumberSInt16Type, (void *)&PID);
        printf("\tidProduct:\t0x%x\n", PID);
        
        IOObjectRelease(usbDevice);
        CFRelease(idProduct);
    }
}

//Just for testing
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    //监听USB事件,需要在主线程中执行
    [self ListenUsbConnectEvent];
}

获取到设备后可根据自身需要获取其VID、Pid及序列号等,根据自己业务需要进行后续数据处理。

参考资料

How to detect and identify mounted and unmounted USB device on Mac using Cocoa
Objective c/c: Detect USB drive via IOKit only on Mac

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jarlen John

谢谢你给我一杯咖啡的温暖

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值