无法在Mac OS X 10.11上获得真正的IOKit条目

时间:2015-11-06 02:27:07

标签: macos usb osx-elcapitan iokit

我需要找到USB设备的IO Kit服务。我寻找所有这样的USB设备:

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 PCI0@0  <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 XHC1@14  <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 MyDevice@14527130  <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 MyDevice@0  <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 MyDevice@1  <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查找IOUSBDevices时,我不仅在AppleACPIPlatformExpert类下返回服务,而且在IOResources类下返回服务。

$ 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 PCI0@0  <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 XHC1@14  <class IORegistryEntry:IOService:IOPCIDevice, id 0x10000023e, registered, matched, active, busy 0 (5991 ms), retain 13>
            +-o XHC1@14000000  <class IORegistryEntry:IOService:AppleUSBHostController:AppleUSBXHCI:AppleUSBXHCIPCI:AppleUSBXHCILPT, id 0x100000267, registered, matched, active, busy 0 (5067 ms), retain 183>
              +-o HS01@14100000  <class IORegistryEntry:IOService:AppleUSBHostPort:AppleUSBXHCIPort:AppleUSB20XHCIPort, id 0x1000002d9, registered, matched, active, busy 0 (99 ms), retain 14>
                +-o MyDevice@14100000  <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 MyDevice@0  <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 MyDevice@1  <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 AppleUSBXHCI@14000000  <class IORegistryEntry:IOService:IOUSBBus:IOUSBController:IOUSBControllerV2:IOUSBControllerV3:AppleUSBController, id 0x1000002ff, registered, matched, active, busy 0 (1529 ms), retain 26>
            +-o MyDevice@14100000  <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 MyDevice@0  <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 MyDevice@1  <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类的子对象时,找不到它。

我知道如何才能获得真实的&#34; 10.11上的条目?

1 个答案:

答案 0 :(得分:3)

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

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