在Mac上监听USB热插拔消息

要在Mac上监听USB热插拔消息,可以使用Apple提供的IOKit框架。以下是一个基本的示例代码:

#include <IOKit/IOKitLib.h>
#include <CoreFoundation/CoreFoundation.h>

// 回调函数,当有设备插入或拔出时被调用
void deviceAdded(void *refCon, io_iterator_t iterator) {
    kern_return_t result;
    io_service_t usbDevice;
    
    while ((usbDevice = IOIteratorNext(iterator))) {
        // 打印设备名
        CFStringRef deviceName = IORegistryEntryCreateCFProperty(usbDevice, CFSTR("USB Product Name"), kCFAllocatorDefault, 0);
        if (deviceName) {
            char name[128];
            if (CFStringGetCString(deviceName, name, sizeof(name), kCFStringEncodingUTF8)) {
                printf("Device added: %s\n", name);
            }
            CFRelease(deviceName);
        }
        
        // 释放设备
        IOObjectRelease(usbDevice);
    }
}

int main(int argc, const char * argv[]) {
    CFRunLoopSourceRef runLoopSource;
    io_iterator_t usbIterator;
    kern_return_t result;
    CFMutableDictionaryRef matchingDict;
    SInt32 vendorID, productID;
    
    // 创建匹配字典,指定要监听的设备的厂商ID和产品ID
    vendorID = 0x1234;
    productID = 0x5678;
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vendorID));
    CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID), CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &productID));
    
    // 创建异步通知端口,并将其添加到主运行循环中
    result = IOServiceAddMatchingNotification(kIOMasterPortDefault, kIOFirstMatchNotification, matchingDict, deviceAdded, NULL, &usbIterator);
    if (result != KERN_SUCCESS) {
        printf("Error: IOServiceAddMatchingNotification returned %d\n", result);
        return -1;
    }
    
    runLoopSource = IONotificationPortGetRunLoopSource(kIOMasterPortDefault, usbIterator);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
    
    // 进入主运行循环,等待设备插入或拔出事件
    CFRunLoopRun();
    
    return 0;
}

在上述代码中,我们使用IOServiceMatching函数创建了一个匹配字典,指定要监听的设备的厂商ID和产品ID。然后,我们使用IOServiceAddMatchingNotification函数将该字典注册为异步通知,当有符合条件的设备插入或拔出时,回调函数deviceAdded将被调用。

在回调函数中,我们首先使用IORegistryEntryCreateCFProperty函数获取设备名称,并打印出来。最后,我们释放了IO对象。

最后,在main函数中,我们将异步通知端口添加到主运行循环中,然后进入主运行循环

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值