2015-10-17 40 views
0

我們一直使用OSX: How to get a volume name (or bsd name) from a IOUSBDeviceInterface or location id中描述的解決方案將USB設備從IOKit映射到其對應的BSD設備位置。代碼如下:OSX:獲取IOUSBDevice的BSD名稱的解決方法

CFTypeRef name = IORegistryEntrySearchCFProperty(usbDevice, 
               kIOServicePlane, 
               CFSTR(kIOBSDNameKey), 
               kCFAllocatorDefault, 
               kIORegistryIterateRecursively); 

由於測試版本中引入了迴歸,此解決方案不再適用於El Capitan。根據https://forums.developer.apple.com/thread/7974的線索,Apple已經確認了該錯誤,但尚未發佈修復程序,所以我需要一個解決方法。到目前爲止,我知道的唯一一個涉及從根解析整個I/O註冊表並尋找我的特定設備。

有誰知道更簡單的解決方法?

+1

您是否嘗試了磁盤仲裁方法? – pmdj

+0

看起來像磁盤仲裁會讓我們有能力識別USB設備何時連接。任何想法,如果它提供了找到特定的IO服務USB設備條目的能力?我們需要獲得關於USB設備的特定屬性,所以只需要安裝點就夠了。 – Cobar

+0

在e上您有一個DADisk對象,您可以通過DADiskCopyIOMedia獲得對其IOMedia對象的引用。從那裏,你應該能夠找到你所需要的。 – pmdj

回答

1

最近也遇到了這個問題。這個問題並沒有開始爲我過濾bsdName,它實際上與首先從USB查詢獲得結果有關。在El Capitan發現IOUSBDevice有點贊成IOUSBHostDevice。以下是我所需要做的:

// -- Begin Relevant Changes Part! --- 
// Get current version 
auto current_supported_version = __MAC_OS_X_VERSION_MAX_ALLOWED; 
// El Capitan is 101100 (in AvailabilityInternal.h) 
auto el_capitan = 101100; 

// IOUSBDevice has become IOUSBHostDevice in El Capitan... 
auto service_matcher = current_supported_version < el_capitan ? "IOUSBDevice" : "IOUSBHostDevice"; 

// Create matching dict to search 
CFMutableDictionaryRef matchingDict = IOServiceMatching(service_matcher); 
// -- End Relevant Changes Part! --- 


// The rest of this is just example implementation code. 
if (matchingDict == NULL) 
{ 
    // IOServiceMatching Failed 
} 

// Fill it with other stuff if necessary (vendor id, etc...) 

// Do actual search 
io_iterator_t iter; 
kern_return_t kr; 
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); 
if (kr != KERN_SUCCESS) 
{ 
    // IOServiceGetMatchingServices Failed 
} 

io_service_t service; 
while ((service = IOIteratorNext(iter))) 
{ 
    // Ought to work now, regardless of version of OSX being ran. 
    CFStringRef bsdName = (CFStringRef) IORegistryEntrySearchCFProperty(
     service, 
     kIOServicePlane, 
     CFSTR(kIOBSDNameKey), 
     kCFAllocatorDefault, 
     kIORegistryIterateRecursively 
     ); 
}