从 IOKit (CoreFoundation) 接收插入设备的通知时出现问题

发布于 2024-11-01 00:42:43 字数 1222 浏览 5 评论 0原文

我目前正在 10.6.7 上开发一个应用程序,当插入新的 USB 设备时,它应该收到通知。我发现有一个 IOKit 函数可以处理“IOServiceAddMatchingNotification”等内容。因为这个特定函数的返回值是 0,所以我认为问题可能出在我的匹配字典中,该字典被赋予这个函数。我这样声明字典:

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);

因为我不想收到每个设备的通知,所以我不知道这是否是创建这个特定字典的正确方法。

我的完整代码如下所示:

ioKitNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
notificationRunLoopSource = IONotificationPortGetRunLoopSource(ioKitNotificationPort);

CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);


addMatchingNotificationResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
                                                                 kIOPublishNotification,
                                                                 matchingDict,
                                                                 deviceAdded,
                                                                 NULL,

有谁知道为什么这不起作用? (注:Callback函数是一个static void c函数,其余的被包装在ins中 ide 是一个 Obj-C 类)。

感谢

Xcode 4、10.6.7

I'm currently developing an application on 10.6.7 which should receive notifications when a new usb device is plugged in. I found out that there is a IOKit function which handles such stuff 'IOServiceAddMatchingNotification'. Because the return value from this specific function is 0, I think that the problem perhaps is in my matching Dictionary, which is given into this function. I declare the Dictionary that way:

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);

Because I wan't to receive a notification for each device, I don't know if this is the right way to create this particular dictionary.

My complete code look like this:

ioKitNotificationPort = IONotificationPortCreate(kIOMasterPortDefault);
notificationRunLoopSource = IONotificationPortGetRunLoopSource(ioKitNotificationPort);

CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);


addMatchingNotificationResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
                                                                 kIOPublishNotification,
                                                                 matchingDict,
                                                                 deviceAdded,
                                                                 NULL,

Does anyone has a idea why this won't work?
(Note: The Callback function is a static void c function and the rest is wrapped ins
ide a Obj-C class).

Thanks

Xcode 4, 10.6.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-11-08 00:42:43

奇怪的是,在通知被武装之前,您必须清空 IOServiceAddMatchingNotification 返回的迭代器。我在提供的代码中没有看到这一点,因此这可能是问题所在。实际上,您需要保留该迭代器来保持通知运行。

io_iterator_t ioNotification;

addMatchingNotificationResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
                                                                 kIOPublishNotification,
                                                                 matchingDict,
                                                                 deviceAdded,
                                                                 NULL,
                                                                 &ioNotification);

while ((service = IOIteratorNext(ioNotification)))
{
    NSLog(@"Hey, I found a service!");

    IOObjectRelease(service); // yes, you have to release this
}

Weirdly, you must empty the iterator returned by IOServiceAddMatchingNotification before the notification will be armed. I don't see that in the code provided, so that could be in the issue. That iterator is actually what you need to keep around to keep the notification running.

io_iterator_t ioNotification;

addMatchingNotificationResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
                                                                 kIOPublishNotification,
                                                                 matchingDict,
                                                                 deviceAdded,
                                                                 NULL,
                                                                 &ioNotification);

while ((service = IOIteratorNext(ioNotification)))
{
    NSLog(@"Hey, I found a service!");

    IOObjectRelease(service); // yes, you have to release this
}
把回忆走一遍 2024-11-08 00:42:43

在我看来,你应该在 opensource.apple.com 上下载 IOUSBFamily 的源代码,然后找到 USB Prober 的代码,这个应用程序的作用与你描述的完全相同,监听 USB 设备(此外,USB Prober还获取通用设备和配置描述符,也许这也是您需要的东西。)

In my opinion, u should download the source code form IOUSBFamily on opensource.apple.com, and then find the code for USB Prober, this application does exactly the same thing as u described, listening the USB Device attachment.(Further, USB Prober also get the general device and configuration descriptor, maybe it is also the things u need.)

记忆里有你的影子 2024-11-08 00:42:43

您是否已将要查找的设备的 VID 和 PID 添加到匹配字典中?对于您拥有的字典,并且 VID= yourVid,PID= yourPid,这将是:

CFDictionaryAddValue(matchingDict, usbVendorId, yourVid);  
CFDictionaryAddValue(matchingDict, usbProductId, yourPid);  

另一件事 - 在成功调用 IOServiceAddMatchingNotification 后,您需要使用在调用中设置的迭代器来调用设备添加的处理程序。这将启动通知并检查现有设备。

Did you add the VID and PID of the device you wish to look for to your matching dictionary? For the dictionary you have, and VID= yourVid, PID= yourPid, it would be:

CFDictionaryAddValue(matchingDict, usbVendorId, yourVid);  
CFDictionaryAddValue(matchingDict, usbProductId, yourPid);  

Another thing - after the call to IOServiceAddMatchingNotification succeeds, you need to call your device-added handler with the iterator that was set in the call. That will arm the notification and check for existing devices.

淡淡の花香 2024-11-08 00:42:43

执行我认为您所描述的操作的最简单方法是连接到磁盘仲裁框架。 DA 对于 OSX 来说相对较新,允许用户态应用程序在设备连接时检查设备。它用于在连接 iPod 时打开 iTunes、在连接相机时启动 iPhoto 等等...如果您正在寻找的 USB 设备是存储设备,那么这将适合您。否则你将需要走匹配的字典路线......

the easiest way to do what i think you are describing is to hook into the DiskArbitration Framework. DA is relatively new to OSX and allows userland applications to examine devices as they get attached. it is what is used to open iTunes when an iPod is attached, launch iPhoto when a camera is attached, etc... if the USB device you are looking for is a storage device then this will work for you. otherwise you will need to go the matching dictionary route...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文