OSX:如何从IOUSBDeviceInterface或位置ID获取卷名(或bsd名称) [英] OSX: How to get a volume name (or bsd name) from a IOUSBDeviceInterface or location id

查看:499
本文介绍了OSX:如何从IOUSBDeviceInterface或位置ID获取卷名(或bsd名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将(USB大容量存储设备的)特定USB字符串描述符与其卷或bsd名称相关联的应用程序.

I'm trying to write an app that associates a particular USB string descriptor (of a USB mass storage device) with its volume or bsd name.

因此,代码将遍历所有已连接的USB设备,获取字符串描述符并从其中一个中提取信息.我想获取这些USB设备的卷名.我找不到合适的API来做到这一点. 我尝试这样做:

So the code goes through all the connected USB devices, gets the string descriptors and extracts information from one of them. I would like to get the volume name of those USB devices. I can't find the right API to do that. I have tried to do that:

DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DADiskRef disk_ref = DADiskCreateFromIOMedia(kCFAllocatorDefault, 
                                             session,
                                             usb_device_ref);
const char* name = DADiskGetBSDName(disk_ref);

但是DADiskCreateFromIOMedia函数返回nil,我假设我传递的usb_device_ref与该函数期望的io_service_t不兼容.

But the DADiskCreateFromIOMedia function returned nil, I assume the usb_device_ref that I passed was not compatible with the io_service_t that the function is expecting.

那么我如何获得USB设备的卷名?

So how can I get the volume name of a USB device?

我可以使用位置ID来做到这一点吗?

Could I use the location id to do that?

感谢您的阅读. -L

Thanks for reading. -L

FOO_Result              result = FOO_SUCCESS;
mach_port_t             master_port;
kern_return_t           k_result;
io_iterator_t           iterator = 0;
io_service_t            usb_device_ref;
CFMutableDictionaryRef  matching_dictionary = NULL; 

// first create a master_port
if (FOO_FAILED(k_result = IOMasterPort(MACH_PORT_NULL, &master_port))) {
    fprintf(stderr, "could not create master port, err = %d\n", k_result);
    goto cleanup;
}
if ((matching_dictionary = IOServiceMatching(kIOUSBDeviceClassName)) == NULL)
{
    fprintf(stderr, "could not create matching dictionary, err = %d\n", k_result);
    goto cleanup;
}
if (FOO_FAILED(k_result = IOServiceGetMatchingServices(master_port, matching_dictionary, &iterator))) {
    fprintf(stderr, "could not find any matching services, err = %d\n", k_result);
    goto cleanup;
}
while (usb_device_ref = IOIteratorNext(iterator))
{       
    IOReturn                        err;
    IOCFPlugInInterface             **iodev;        // requires <IOKit/IOCFPlugIn.h>
    IOUSBDeviceInterface            **dev;
    SInt32                          score;

    err = IOCreatePlugInInterfaceForService(usb_device_ref, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
    if (err || !iodev)
    {
        printf("dealWithDevice: unable to create plugin. ret = %08x, iodev = %p\n", err, iodev);
        return;
    }
    err = (*iodev)->QueryInterface(iodev, 
                                   CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), 
                                   (LPVOID*)&dev);
    (*iodev)->Release(iodev);               // done with this


    FOO_String string_value;
    UInt8 string_index = 0x1;
    FOO_Result result = FOO_SUCCESS;
    CFStringRef device_name_as_cf_string;
    do {
        if (FOO_SUCCEEDED(result = FOO_GetStringDescriptor(dev, string_index, 0, string_value))) {
            printf("String at index %i is %s\n", string_index, string_value.GetChars());
            // extract the command code if it is the FOO string
            if (string_value.CompareN("FOO:", 4) == 0) {
                FOO_Byte code = 0;
                FOO_HexToByte(string_value.GetChars() + 4, code);
                // Get other relevant information from the device
                io_name_t        device_name;
                UInt32           location_id = 0;

                // Get the USB device's name.
                err = IORegistryEntryGetName(usb_device_ref, device_name);
                device_name_as_cf_string = CFStringCreateWithCString(kCFAllocatorDefault, device_name, 
                                                                     kCFStringEncodingASCII); 

                err = (*dev)->GetLocationID(dev, &location_id);

                // TODO: get volume or BSD name 

                // add the device to the list

                break;
            }
        }
        string_index++;
    } while (FOO_SUCCEEDED(result));


    err = (*dev)->USBDeviceClose(dev);
    if (err)
    {
        printf("dealWithDevice: error closing device - %08x\n", err);
        (*dev)->Release(dev);
        return;
    }
    err = (*dev)->Release(dev);
    if (err)
    {
        printf("dealWithDevice: error releasing device - %08x\n", err);
        return;
    }

    IOObjectRelease(usb_device_ref);            // no longer need this reference
}

推荐答案

一旦有了io_service_t,就可以通过IORegistry递归查找BSD名称.您无需实际创建接口.

Once you have the io_service_t, you can recurse through the IORegistry to find the BSD Name. You do not need to actually create an interface.

#import <IOKit/IOBSD.h>

CFStringRef bsdName = ( CFStringRef ) IORegistryEntrySearchCFProperty ( usbDevice,
                                                                       kIOServicePlane,
                                                                       CFSTR ( kIOBSDNameKey ),
                                                                       kCFAllocatorDefault,
                                                                       kIORegistryIterateRecursively );

这篇关于OSX:如何从IOUSBDeviceInterface或位置ID获取卷名(或bsd名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆