OSX : iousbdeviceinterface 또는 location id에서 볼륨 이름 (또는 BSD 이름)을 얻는 방법

StackOverflow https://stackoverflow.com/questions/2459414

  •  20-09-2019
  •  | 
  •  

문제

나는 특정 USB 문자열 디스크립터 (USB 질량 저장 장치)를 볼륨 또는 BSD 이름과 연관시키는 앱을 작성하려고합니다.

따라서 코드는 연결된 모든 연결된 USB 장치를 통해 문자열 디스크립터를 가져오고 그 중 하나에서 정보를 추출합니다. USB 장치의 볼륨 이름을 얻고 싶습니다. 그렇게 할 올바른 API를 찾을 수 없습니다. 나는 그렇게하려고 노력했다 :

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

그러나 dadiskcreatefromiemedia 함수는 NIL을 반환했습니다. 통과 한 USB_DEVICE_REF는 IO_SERVICE_T와 호환되지 않는다고 가정합니다.

그렇다면 USB 장치의 볼륨 이름을 어떻게 얻을 수 있습니까?

위치 ID를 사용하여 그렇게 할 수 있습니까?

읽어 주셔서 감사합니다. -엘

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 이름을 찾을 수 있습니다. 실제로 인터페이스를 만들 필요는 없습니다.

#import <IOKit/IOBSD.h>

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

다른 팁

또한보십시오:

USBDEVS- Mac OS X의 목록 장착 USB 장치 및 볼륨 경로

https://gist.github.com/etr000/621561

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top