首页 > 解决方案 > 迭代 IOService 对象时,迭代器是否进入子节点?

问题描述

例如,如果我想查找所有 IOPCIDevice,我使用

CFMutableDictionaryRef dict = IOServiceMatching("IOPCIDevice");
IOServiceGetMatchingServices(kIOMasterPortDefault,matchDict,
                                     &iterator) == kIOReturnSuccess

在此之后,迭代器将指向一个节点,即 IOPCIDevice。

      iterator
         |
Parent (IOPCIDevice) <--- child of ---- Child(IOPCIDevice)
         |
         |
Another parent (IOPCIDevice)
         |
         ---------------- ...

我的问题是,这个迭代器会进入子节点吗?它如何迭代所有匹配的节点?

标签: objective-cmacosiokit

解决方案


我的问题是,这个迭代器会进入子节点吗?

是的,如果任何子节点也满足匹配条件。

它如何迭代所有匹配的节点?

如果这就是您所要求的,我认为无法保证对象将以哪种顺序返回。

使用以下代码很容易演示该行为:

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

int main(int argc, const char* argv[])
{
    CFMutableDictionaryRef matching_dict = IOServiceMatching("IOPCIDevice");
    io_iterator_t match_iter = IO_OBJECT_NULL;
    IOReturn result = IOServiceGetMatchingServices(
        kIOMasterPortDefault, matching_dict, &match_iter);
    if (result == kIOReturnSuccess)
    {
        io_service_t matched_service;
        while ((matched_service = IOIteratorNext(match_iter)))
        {
            io_name_t service_class = "";
            io_string_t service_path = "";
            IORegistryEntryGetPath(matched_service, kIOServicePlane, service_path);
            IOObjectGetClass(matched_service, service_class);
            printf("%s [%s]\n", service_path, service_class);
            IOObjectRelease(matched_service);
        }
        IOObjectRelease(match_iter);
    }

    return 0;
}

在我的系统上,一台 Mac Mini (2018) 与一些通过 Thunderbolt 连接的设备,我得到以下输出:

IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/MCHC@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG0@1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/IGPU@2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/pci8086,a379@12 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG0@1/IOPP/XGBE@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/pci8086,a36f@14,2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/IMEI@16 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP17@1B [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB1@1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP17@1B/IOPP/ANS2@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB1@1/IOPP/UPS0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB2@2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB4@4 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP17@1B/IOPP/IOBC@0,1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB1@1/IOPP/UPS0@0/IOPP/pci-bridge@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB1@1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP17@1B/IOPP/SEPM@0,2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB4@4/IOPP/UPS0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB1@1/IOPP/UPS0@0/IOPP/pci-bridge@0/IOPP/pci11c1,5901@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP17@1B/IOPP/ADIO@0,3 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB2@2 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB4@4 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB2@2/IOPP/XHC2@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB4@4/IOPP/UPS0@0/IOPP/pci-bridge@1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB0@0/IOPP/NHI0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB4@4/IOPP/UPS0@0/IOPP/pci-bridge@1/IOPP/display@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG1@1,1/IOPP/UPSB@0/IOPP/DSB4@4/IOPP/UPS0@0/IOPP/pci-bridge@1/IOPP/pci1002,aaf0@0,1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB2@2/IOPP/XHC3@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG2@1,2/IOPP/UPSB@0/IOPP/DSB0@0/IOPP/NHI0@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP01@1C [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP02@1C,1 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/URT0@1E [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/LPCB@1F [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/HDEF@1F,3 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/RP01@1C/IOPP/ARPT@0 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/SBUS@1F,4 [IOPCIDevice]
IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XSPI@1F,5 [IOPCIDevice]

你可以看到

IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG0@1

是一个父节点

IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/PEG0@1/IOPP/XGBE@0

推荐阅读