2015-11-06 126 views
1

我需要为我的USB设备找到IO Kit服务。我期待像这样所有的USB设备:无法在Mac OS X上获得真正的IOKit条目10.11

CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName); 

IONotificationPortRef notificationObject = IONotificationPortCreate(kIOMasterPortDefault); 

CFRunLoopSourceRef notificationRunLoopSource; 
//Use the notification object received from IONotificationPortCreate 
notificationRunLoopSource = 
IONotificationPortGetRunLoopSource(notificationObject); 

CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, 
        kCFRunLoopDefaultMode); 

io_iterator_t iterator; 
kern_return_t kr; 
kr = IOServiceAddMatchingNotification(notificationObject, kIOFirstPublishNotification, matchingDict, 
             DeviceAppearedCallback, nil, &iterator); 
if (kIOReturnSuccess == kr) 
    DeviceAppearedCallback(nil, iterator); 

在10.10和更早的时候,我找IOUSBDevices我的AppleACPIPlatformExpert类下回服务。

> ioreg -r -n MyDevice -i -t 
+-o Root <class IORegistryEntry, id 0x100000100, retain 9> 
    +-o MacBookPro11,2 <class IORegistryEntry:IOService:IOPlatformExpertDevice, id 0x100000112, registered, matched, active, busy 0 (1191243 ms), retain 42> 
    +-o AppleACPIPlatformExpert <class IORegistryEntry:IOService:IOPlatformExpert:IODTPlatformExpert:IOACPIPlatformExpert:AppleACPIPlatformExpert, id 0x100000113, registered, matched, active, busy 0 (1174729 ms), retain 44> 
     +-o [email protected] <class IORegistryEntry:IOService:IOPlatformDevice:IOACPIPlatformDevice, id 0x10000013c, registered, matched, active, busy 0 (1171678 ms), retain 40> 
     +-o AppleACPIPCI <class IORegistryEntry:IOService:IOPCIBridge:AppleACPIPCI, id 0x100000244, registered, matched, active, busy 0 (1171663 ms), retain 33> 
      +-o [email protected] <class IORegistryEntry:IOService:IOPCIDevice, id 0x10000023a, registered, matched, active, busy 0 (1167511 ms), retain 10> 
      +-o AppleUSBXHCI <class IORegistryEntry:IOService:IOUSBBus:IOUSBController:IOUSBControllerV2:IOUSBControllerV3:AppleUSBXHCI, id 0x100000269, registered, matched, active, busy 0 (1166957 ms), retain 30> 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x1000503ee, registered, matched, active, busy 0 (316 ms), retain 16> 
       | { 
       | "sessionID" = 262856664715859 
       | "idProduct" = 555 
       | "bNumConfigurations" = 1 
       | "iManufacturer" = 1 
       | "bcdDevice" = 1280 
       | "Bus Power Available" = 250 
       | "bMaxPacketSize0" = 8 
       | "USB Product Name" = "MyDevice" 
       | "iProduct" = 2 
       | "iSerialNumber" = 3 
       | "bDeviceClass" = 0 
       | "IOUserClientClass" = "IOUSBDeviceUserClientV2" 
       | "bDeviceSubClass" = 0 
       | "USB Address" = 19 
       | "bcdUSB" = 512 
       | "locationID" = 340947248 
       | "PortNum" = 3 
       | "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       | "bDeviceProtocol" = 0 
       | "USB Vendor Name" = "FTDI" 
       | "Device Speed" = 1 
       | "idVendor" = 1027 
       | "USB Serial Number" = "sn123456" 
       | "IOGeneralInterest" = "IOCommand is not serializable" 
       | "Requested Power" = 45 
       | "Low Power Displayed" = No 
       | } 
       | 
       +-o IOUSBDeviceUserClientV2 <class IORegistryEntry:IOService:IOUserClient:IOUSBDeviceUserClientV2, id 0x1000503ef, !registered, !matched, active, busy 0, retain 7> 
       +-o IOUSBCompositeDriver <class IORegistryEntry:IOService:IOUSBCompositeDriver, id 0x1000503f3, !registered, !matched, active, busy 0, retain 4> 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x1000503f4, registered, matched, active, busy 0 (125 ms), retain 9> 
       | | { 
       | | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       | | "bcdDevice" = 1280 
       | | "IOUserClientClass" = "IOUSBInterfaceUserClientV3" 
       | | "idProduct" = 555 
       | | "bInterfaceSubClass" = 255 
       | | "bConfigurationValue" = 1 
       | | "locationID" = 340947248 
       | | "USB Interface Name" = "MyDevice" 
       | | "idVendor" = 1027 
       | | "iInterface" = 2 
       | | "bAlternateSetting" = 0 
       | | "bInterfaceProtocol" = 255 
       | | "bInterfaceNumber" = 0 
       | | "bInterfaceClass" = 255 
       | | "bNumEndpoints" = 2 
       | | } 
       | | 
       | +-o AppleUSBFTDI <class IORegistryEntry:IOService:IOSerialDriverSync:AppleUSBFTDI, id 0x1000503fa, !registered, !matched, active, busy 0 (1 ms), retain 6> 
       | +-o IORS232SerialStreamSync <class IORegistryEntry:IOService:IOSerialStreamSync:IORS232SerialStreamSync, id 0x1000503fb, registered, matched, active, busy 0 (1 ms), retain 6> 
       |  +-o IOSerialBSDClient <class IORegistryEntry:IOService:IOSerialBSDClient, id 0x1000503fd, registered, matched, active, busy 0 (0 ms), retain 5> 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x1000503f7, registered, matched, active, busy 0 (12 ms), retain 9> 
       | | { 
       | | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       | | "bcdDevice" = 1280 
       | | "IOUserClientClass" = "IOUSBInterfaceUserClientV3" 
       | | "idProduct" = 555 
       | | "bInterfaceSubClass" = 255 
       | | "bConfigurationValue" = 1 
       | | "locationID" = 340947248 
       | | "USB Interface Name" = "MyDevice" 
       | | "idVendor" = 1027 
       | | "iInterface" = 2 
       | | "bAlternateSetting" = 0 
       | | "bInterfaceProtocol" = 255 
       | | "bInterfaceNumber" = 1 
       | | "bInterfaceClass" = 255 
       | | "bNumEndpoints" = 2 
       | | } 
       | | 
       | +-o AppleUSBFTDI <class IORegistryEntry:IOService:IOSerialDriverSync:AppleUSBFTDI, id 0x1000503fe, !registered, !matched, active, busy 0 (0 ms), retain 6> 
       | +-o IORS232SerialStreamSync <class IORegistryEntry:IOService:IOSerialStreamSync:IORS232SerialStreamSync, id 0x1000503ff, registered, matched, active, busy 0 (0 ms), retain 6> 
       |  +-o IOSerialBSDClient <class IORegistryEntry:IOService:IOSerialBSDClient, id 0x100050401, registered, matched, active, busy 0 (0 ms), retain 5> 
       +-o IOUSBDeviceUserClientV2 <class IORegistryEntry:IOService:IOUserClient:IOUSBDeviceUserClientV2, id 0x100050403, !registered, !matched, active, busy 0, retain 7> 
       +-o IOUSBDeviceUserClientV2 <class IORegistryEntry:IOService:IOUserClient:IOUSBDeviceUserClientV2, id 0x100050404, !registered, !matched, active, busy 0, retain 7> 

在10.11,当我看使用名为ioreg或IORegistryExplorer我不仅AppleACPIPlatformExpert类之下,又下的IOResources类返回服务IOUSBDevices。

$ ioreg -r -n MyDevice -t -i 
+-o Root <class IORegistryEntry, id 0x100000100, retain 14> 
    +-o Macmini7,1 <class IORegistryEntry:IOService:IOPlatformExpertDevice, id 0x100000112, registered, matched, active, busy 0 (23064 ms), retain 39> 
    +-o AppleACPIPlatformExpert <class IORegistryEntry:IOService:IOPlatformExpert:IODTPlatformExpert:IOACPIPlatformExpert:AppleACPIPlatformExpert, id 0x100000113, registered, matched, active, busy 0 (9879 ms), retain 37> 
     +-o [email protected] <class IORegistryEntry:IOService:IOPlatformDevice:IOACPIPlatformDevice, id 0x10000013c, registered, matched, active, busy 0 (9850 ms), retain 42> 
     +-o AppleACPIPCI <class IORegistryEntry:IOService:IOPCIBridge:AppleACPIPCI, id 0x10000024b, registered, matched, active, busy 0 (9836 ms), retain 35> 
      +-o [email protected] <class IORegistryEntry:IOService:IOPCIDevice, id 0x10000023e, registered, matched, active, busy 0 (5991 ms), retain 13> 
      +-o [email protected] <class IORegistryEntry:IOService:AppleUSBHostController:AppleUSBXHCI:AppleUSBXHCIPCI:AppleUSBXHCILPT, id 0x100000267, registered, matched, active, busy 0 (5067 ms), retain 183> 
       +-o [email protected] <class IORegistryEntry:IOService:AppleUSBHostPort:AppleUSBXHCIPort:AppleUSB20XHCIPort, id 0x1000002d9, registered, matched, active, busy 0 (99 ms), retain 14> 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x100000a95, registered, matched, active, busy 0 (7 ms), retain 26> 
        | { 
        | "sessionID" = 145657060387527 
        | "USBSpeed" = 1 
        | "idProduct" = 555 
        | "bDeviceClass" = 0 
        | "IOPowerManagement" = {"PowerOverrideOn"=Yes,"CapabilityFlags"=32768,"MaxPowerState"=2,"DevicePowerState"=2,"ChildrenPowerState"=1,"DriverPowerState"=0,"CurrentPowerState"=2} 
        | "bcdDevice" = 1280 
        | "USB Product Name" = "MyDevice" 
        | "AppleUSBAlternateServiceRegistryID" = 4294970007 
        | "locationID" = 336592896 
        | "kUSBSerialNumberString" = "54df18d4" 
        | "bDeviceSubClass" = 0 
        | "kUSBCurrentConfiguration" = 1 
        | "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
        | "bDeviceProtocol" = 0 
        | "USBPortType" = 0 
        | "USB Vendor Name" = "FTDI" 
        | "idVendor" = 1027 
        | "USB Serial Number" = "54df18d4" 
        | "IOGeneralInterest" = "IOCommand is not serializable" 
        | "kUSBVendorString" = "FTDI" 
        | "IOClassNameOverride" = "IOUSBDevice" 
        | } 
        | 
        +-o AppleUSBHostLegacyClient <class IORegistryEntry:IOService:AppleUSBHostLegacyClient, id 0x100000a98, !registered, !matched, active, busy 0, retain 8> 
        +-o AppleUSBHostCompositeDevice <class IORegistryEntry:IOService:AppleUSBHostCompositeDevice, id 0x100000aa0, !registered, !matched, active, busy 0, retain 4> 
        +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x100000aa1, registered, matched, active, busy 0 (2 ms), retain 11> 
        | | { 
        | | "USBPortType" = 0 
        | | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
        | | "bcdDevice" = 1280 
        | | "USBSpeed" = 1 
        | | "idProduct" = 555 
        | | "bConfigurationValue" = 1 
        | | "bInterfaceSubClass" = 255 
        | | "locationID" = 336592896 
        | | "IOGeneralInterest" = "IOCommand is not serializable" 
        | | "IOClassNameOverride" = "IOUSBInterface" 
        | | "AppleUSBAlternateServiceRegistryID" = 4294970019 
        | | "idVendor" = 1027 
        | | "bInterfaceProtocol" = 255 
        | | "bAlternateSetting" = 0 
        | | "bInterfaceNumber" = 0 
        | | "bInterfaceClass" = 255 
        | | } 
        | | 
        | +-o AppleUSBFTDI <class IORegistryEntry:IOService:IOSerialDriverSync:AppleUSBFTDI, id 0x100000aa8, !registered, !matched, active, busy 0 (0 ms), retain 5> 
        | +-o IORS232SerialStreamSync <class IORegistryEntry:IOService:IOSerialStreamSync:IORS232SerialStreamSync, id 0x100000aa9, registered, matched, active, busy 0 (0 ms), retain 6> 
        |  +-o IOSerialBSDClient <class IORegistryEntry:IOService:IOSerialBSDClient, id 0x100000aaa, registered, matched, active, busy 0 (0 ms), retain 5> 
        +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x100000aa2, registered, matched, active, busy 0 (2 ms), retain 11> 
        | { 
        | "USBPortType" = 0 
        | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
        | "bcdDevice" = 1280 
        | "USBSpeed" = 1 
        | "idProduct" = 555 
        | "bConfigurationValue" = 1 
        | "bInterfaceSubClass" = 255 
        | "locationID" = 336592896 
        | "IOGeneralInterest" = "IOCommand is not serializable" 
        | "IOClassNameOverride" = "IOUSBInterface" 
        | "AppleUSBAlternateServiceRegistryID" = 4294970023 
        | "idVendor" = 1027 
        | "bInterfaceProtocol" = 255 
        | "bAlternateSetting" = 0 
        | "bInterfaceNumber" = 1 
        | "bInterfaceClass" = 255 
        | } 
        | 
        +-o AppleUSBFTDI <class IORegistryEntry:IOService:IOSerialDriverSync:AppleUSBFTDI, id 0x100000aac, !registered, !matched, active, busy 0 (0 ms), retain 5> 
         +-o IORS232SerialStreamSync <class IORegistryEntry:IOService:IOSerialStreamSync:IORS232SerialStreamSync, id 0x100000aad, registered, matched, active, busy 0 (0 ms), retain 6> 
         +-o IOSerialBSDClient <class IORegistryEntry:IOService:IOSerialBSDClient, id 0x100000aae, registered, matched, active, busy 0 (0 ms), retain 5> 

+-o Root <class IORegistryEntry, id 0x100000100, retain 14> 
    +-o Macmini7,1 <class IORegistryEntry:IOService:IOPlatformExpertDevice, id 0x100000112, registered, matched, active, busy 0 (23064 ms), retain 39> 
    +-o IOResources <class IORegistryEntry:IOService:IOResources, id 0x100000115, registered, matched, active, busy 0 (4082 ms), retain 33> 
     +-o AppleUSBHostResources <class IORegistryEntry:IOService:AppleUSBHostResources, id 0x100000257, registered, matched, active, busy 0 (1530 ms), retain 37> 
     +-o AppleUSBLegacyRoot <class IORegistryEntry:IOService:AppleUSBHostResourcesClient:AppleUSBLegacyRoot, id 0x10000025a, registered, matched, active, busy 0 (1529 ms), retain 26> 
      +-o [email protected] <class IORegistryEntry:IOService:IOUSBBus:IOUSBController:IOUSBControllerV2:IOUSBControllerV3:AppleUSBController, id 0x1000002ff, registered, matched, active, busy 0 (1529 ms), retain 26> 
      +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBDevice, id 0x100000a97, registered, matched, active, busy 0 (7 ms), retain 16> 
       | { 
       | "sessionID" = 145657060387527 
       | "iManufacturer" = 1 
       | "bNumConfigurations" = 1 
       | "Built-In" = No 
       | "bcdDevice" = 1280 
       | "Bus Power Available" = 500 
       | "idProduct" = 555 
       | "bMaxPacketSize0" = 8 
       | "iProduct" = 2 
       | "iSerialNumber" = 3 
       | "USB Address" = 18 
       | "bDeviceClass" = 0 
       | "locationID" = 336592896 
       | "bDeviceSubClass" = 0 
       | "bcdUSB" = 512 
       | "USB Product Name" = "MyDevice" 
       | "non-removable" = "no" 
       | "IOCFPlugInTypes" = {"9dc7b780-9ec0-11d4-a54f-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       | "IOUserClientClass" = "IOUSBDeviceUserClientV2" 
       | "bDeviceProtocol" = 0 
       | "IOPowerManagement" = {"DevicePowerState"=0,"CurrentPowerState"=3,"CapabilityFlags"=65536,"MaxPowerState"=4,"DriverPowerState"=3} 
       | "Device Speed" = 1 
       | "USB Vendor Name" = "FTDI" 
       | "idVendor" = 1027 
       | "IOGeneralInterest" = "IOCommand is not serializable" 
       | "USB Serial Number" = "54df18d4" 
       | "IOClassNameOverride" = "IOUSBDevice" 
       | } 
       | 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x100000aa3, registered, matched, active, busy 0 (2 ms), retain 5> 
       | { 
       |  "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       |  "bcdDevice" = 1280 
       |  "IOUserClientClass" = "IOUSBInterfaceUserClientV3" 
       |  "idProduct" = 555 
       |  "bInterfaceSubClass" = 255 
       |  "bConfigurationValue" = 1 
       |  "locationID" = 336592896 
       |  "USB Interface Name" = "MyDevice" 
       |  "IOClassNameOverride" = "IOUSBInterface" 
       |  "bInterfaceProtocol" = 255 
       |  "idVendor" = 1027 
       |  "bAlternateSetting" = 0 
       |  "iInterface" = 2 
       |  "bInterfaceNumber" = 0 
       |  "bInterfaceClass" = 255 
       |  "bNumEndpoints" = 2 
       | } 
       | 
       +-o [email protected] <class IORegistryEntry:IOService:IOUSBNub:IOUSBInterface, id 0x100000aa7, registered, matched, active, busy 0 (2 ms), retain 5> 
       | { 
       |  "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} 
       |  "bcdDevice" = 1280 
       |  "IOUserClientClass" = "IOUSBInterfaceUserClientV3" 
       |  "idProduct" = 555 
       |  "bInterfaceSubClass" = 255 
       |  "bConfigurationValue" = 1 
       |  "locationID" = 336592896 
       |  "USB Interface Name" = "MyDevice" 
       |  "IOClassNameOverride" = "IOUSBInterface" 
       |  "bInterfaceProtocol" = 255 
       |  "idVendor" = 1027 
       |  "bAlternateSetting" = 0 
       |  "iInterface" = 2 
       |  "bInterfaceNumber" = 1 
       |  "bInterfaceClass" = 255 
       |  "bNumEndpoints" = 2 
       | } 

然而,在10.11,当我使用上面的代码搜索类了IOUSBDevice的设备,我只得到的IOResources/AppleUSBLegacyRoot供应商下的结果。这意味着当我查找IOSerialBSDClient类的子对象时,无法找到它。

任何想法如何我可以得到10.11“真实”的条目?

+0

请注意,IOKit注册表不是一棵树,它是一个无环的有向图。每个服务可以有多个父节点(提供者),因此可以有多条从根节点到节点的路径,现在可以使用这两个并行的USB服务图完成此操作。你确定有两个实例吗? (为了增加混淆,OSX 10.11在USB类名称中插入了“主机”一词,为了兼容性,这些名称被一些API删除。) – pmdj

回答

3

在10.11上,您必须查找IOUSBHostDevice而不是IOUSBDevice,以获得与10.10及更早版本相同的结果。

NSOperatingSystemVersion needHostDeviceVersion = {10,11,0}; 
    if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:needHostDeviceVersion]) 
     matchingDict = IOServiceMatching("IOUSBHostDevice"); 
    else 
     matchingDict = IOServiceMatching(kIOUSBDeviceClassName);