IOServiceGetMatchingServices 在某些 Mac 上找不到某些设备
我在使用监控 USB 总线更改的应用程序时遇到问题。具体来说,如果我想获取连接的 USB 服务的列表,我将使用下面的代码。奇怪的是,一些用户(当然不是我的机器)看不到一两个设备。 这些设备确实显示在 IORegistryExplorer 中,并显示已注册。
此功能不起作用的机器也运行 10.6 并且也是 MacBook Pro。
CFMutableDictionaryRef service_properties = CFDictionaryCreateMutable(NULL, 0, NULL,NULL); CFMutableDictionaryRef child_props = CFDictionaryCreateMutable(NULL, 0, NULL,NULL);
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceNameMatching("AppleUSBEHCI"), &io_objects);
if(kr != KERN_SUCCESS)
exit(1);
while((io_service= IOIteratorNext(io_objects)))
{
kr = IORegistryEntryCreateCFProperties(io_service, &service_properties, kCFAllocatorDefault, kNilOptions);
io_iterator_t iter;
kr = IORegistryEntryGetChildIterator(io_service, kIOServicePlane, &iter);
io_registry_entry_t child;
while( (child = IOIteratorNext( iter )))
{
kr = IORegistryEntryCreateCFProperties(child, &child_props, kCFAllocatorDefault, kNilOptions );
NSLog(@"%@",child_props);
}
IOObjectRelease(io_service);
}
I have an issue with an app that monitors the USB bus for changes. Specifically if I want to get a list of attached USB sevices, I am using the code below. What is strange is that some users (and nautally not my machines) do not see one or two devices.
Those devices DO show up in the IORegistryExplorer, and show registered.
The machines for which this is not working are also running 10.6 and are also MacBook Pros.
CFMutableDictionaryRef service_properties = CFDictionaryCreateMutable(NULL, 0, NULL,NULL);
CFMutableDictionaryRef child_props = CFDictionaryCreateMutable(NULL, 0, NULL,NULL);
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceNameMatching("AppleUSBEHCI"), &io_objects);
if(kr != KERN_SUCCESS)
exit(1);
while((io_service= IOIteratorNext(io_objects)))
{
kr = IORegistryEntryCreateCFProperties(io_service, &service_properties, kCFAllocatorDefault, kNilOptions);
io_iterator_t iter;
kr = IORegistryEntryGetChildIterator(io_service, kIOServicePlane, &iter);
io_registry_entry_t child;
while( (child = IOIteratorNext( iter )))
{
kr = IORegistryEntryCreateCFProperties(child, &child_props, kCFAllocatorDefault, kNilOptions );
NSLog(@"%@",child_props);
}
IOObjectRelease(io_service);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你匹配的东西完全错误。您要与 Mac 中使用的物理控制器(
EHCI
、UHCI
、OHCI
)进行匹配。每当他们发明新的控制器标准时,例如 2012 年以来新 Mac 中的 XHCI(用于 USB 3 兼容性),这都会失败。您可能想要做的是匹配 IOUSBDevice,这就是当我想要匹配系统中的每个设备时我所做的。这也是
Deva
示例代码中的完成方式。You're matching on entirely the wrong thing. You're matching against the physical controller used in the Mac (
EHCI
,UHCI
,OHCI
). This will fail whenever they invent a new controller standard, such asXHCI
in new Macs since 2012 (for USB 3 compatibility).What you probably want to do is to match against
IOUSBDevice
, that what I do when I want to match every device in the system. This is also how its done in theDeva
sample code.事实证明,您需要轮询所有总线类型:
It turns out that you need to poll all the bus types:
我同意 Jeremy 的观点:除了
AppleUSBEHCI
之外,还有其他可能性。也许您可能知道(以防万一):
应该显示您计算机上的活动 USB 状态
I agree with Jeremy : there are other possibilities than
AppleUSBEHCI
Maybe you probably know that (just in case):
should show the alive USB status on your machine