Posted By

nunofgs on 11/10/06


Tagged

cocoa usb iokit carbon


Versions (?)

Given a mount path, retrieve a USB device name


 / Published in: Objective C
 

This is a bit of a hack, but it works. If someone knows of a better way, please let me know!

  1. - (NSString *) deviceNameWithMountPath:(NSString *)mountPath
  2. {
  3. OSStatus err;
  4. FSRef ref;
  5. FSVolumeRefNum actualVolume;
  6. err = FSPathMakeRef ( (const UInt8 *) [mountPath fileSystemRepresentation], &ref, NULL );
  7.  
  8. // get a FSVolumeRefNum from mountPath
  9. if ( noErr == err ) {
  10. FSCatalogInfo catalogInfo;
  11. err = FSGetCatalogInfo ( &ref,
  12. kFSCatInfoVolume,
  13. &catalogInfo,
  14. NULL,
  15. NULL,
  16. NULL
  17. );
  18. if ( noErr == err ) {
  19. actualVolume = catalogInfo.volume;
  20. }
  21. }
  22.  
  23. // now let's get the device name
  24. GetVolParmsInfoBuffer volumeParms;
  25. HParamBlockRec pb;
  26.  
  27. // Use the volume reference number to retrieve the volume parameters. See the documentation
  28. // on PBHGetVolParmsSync for other possible ways to specify a volume.
  29. pb.ioParam.ioNamePtr = NULL;
  30. pb.ioParam.ioVRefNum = actualVolume;
  31. pb.ioParam.ioBuffer = (Ptr) &volumeParms;
  32. pb.ioParam.ioReqCount = sizeof(volumeParms);
  33.  
  34. // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
  35. // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
  36. err = PBHGetVolParmsSync(&pb);
  37.  
  38. NSString *bsdName = [NSString stringWithCString:(char *)volumeParms.vMDeviceID];
  39.  
  40. // after this we traverse the IORegistry to try to find the device that has the same BDS name
  41. NSString *deviceName=nil;
  42. CFDictionaryRef matchingDict = NULL;
  43. io_iterator_t devIter = nil;
  44. io_service_t ioDeviceObj = nil;
  45. IOReturn kr;
  46. CFTypeRef data;
  47.  
  48. matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
  49. if (matchingDict == NULL) {
  50. NSLog(@"BusProber: error in -refresh at IOServiceMatching() - dictionary was NULL");
  51. return nil;
  52. }
  53.  
  54. kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &devIter);
  55. if (kr != kIOReturnSuccess) {
  56. NSLog(@"BusProber: error in -refresh at IOServiceGetMatchingServices()");
  57. return nil;
  58. }
  59.  
  60. while (ioDeviceObj = IOIteratorNext(devIter))
  61. {
  62.  
  63. data = IORegistryEntrySearchCFProperty(ioDeviceObj, kIOServicePlane, CFSTR("BSD Name"), kCFAllocatorDefault, kIORegistryIterateRecursively);
  64.  
  65. if ([[(NSString*)data substringToIndex:5] isEqualToString:[bsdName substringToIndex:5]]) {
  66. io_name_t devName;
  67. IORegistryEntryGetName(ioDeviceObj, devName);
  68. deviceName = [NSString stringWithCString:devName];
  69.  
  70. IOObjectRelease(ioDeviceObj);
  71. break;
  72. }
  73.  
  74. IOObjectRelease(ioDeviceObj);
  75. }
  76.  
  77. return deviceName;
  78. }

Report this snippet  

You need to login to post a comment.