IOServiceGetMatchingServices 在某些 Mac 上找不到某些设备

发布于 2024-11-09 20:03:06 字数 1164 浏览 3 评论 0原文

我在使用监控 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 技术交流群。

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

发布评论

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

评论(3

眼眸印温柔 2024-11-16 20:03:06

你匹配的东西完全错误。您要与 Mac 中使用的物理控制器(EHCIUHCIOHCI)进行匹配。每当他们发明新的控制器标准时,例如 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 as XHCI 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 the Deva sample code.

何时共饮酒 2024-11-16 20:03:06

事实证明,您需要轮询所有总线类型:

for(i=0;i<3;i++){

if(i==0)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBEHCI"), &io_objects);
if(i==1)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBOHCI"), &io_objects);
if(i==2)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBUHCI"), &io_objects);

if(kr != KERN_SUCCESS)
    exit(1);

It turns out that you need to poll all the bus types:

for(i=0;i<3;i++){

if(i==0)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBEHCI"), &io_objects);
if(i==1)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBOHCI"), &io_objects);
if(i==2)kr = IOServiceGetMatchingServices(myMasterPort,
                                      IOServiceNameMatching("AppleUSBUHCI"), &io_objects);

if(kr != KERN_SUCCESS)
    exit(1);
左耳近心 2024-11-16 20:03:06

我同意 Jeremy 的观点:除了 AppleUSBEHCI 之外,还有其他可能性。

也许您可能知道(以防万一):

ioreg -l | grep AppleUSB

应该显示您计算机上的活动 USB 状态

I agree with Jeremy : there are other possibilities than AppleUSBEHCI

Maybe you probably know that (just in case):

ioreg -l | grep AppleUSB

should show the alive USB status on your machine

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