要在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函数中,我们将异步通知端口添加到主运行循环中,然后进入主运行循环