views:

1715

answers:

5

Hi

I am trying to write a Cocoa program which detects iPods connected to Mac OS. I am listening to NSWorkspaceDidMountNotification and NSWorkspaceDidUnmountNotification for the USB device mount and unmount notifications. I can get the device path of the mounted device using NSString *path = [[notif userInfo] objectForKey:@"NSDevicePath"]; but I also need t know the Device Id, Vendor Id, Product Id etc to check whether the mounted device is an iPod. I think the way forward is IOKit. But I have a feeling that it for low level programming. Is there any other way to find these? Also, if it is IO kit is there any sample program which will give the Ids when I provide mount path?

Thanks a lot.

+2  A: 

I am listening to NSWorkspaceDidMountNotification and NSWorkspaceDidUnmountNotification for the USB device mount and unmount notifications.

That's not what those notifications are for. They are volume mount and unmount notifications, and a volume can come from something that isn't a USB device. Disk images, FireWire devices, optical discs, and flash memory cards are all devices that are not USB devices. (The card may be in a USB card reader, but the card is not the reader.)

I think the way forward is IOKit.

Correct.

But I have a feeling that it for low level programming.

Correct.

Is there any other way to find these?

You can't cut out I/O Kit completely, but there is a shortcut that may save you some work. It's the Disk Arbitration framework.

Register a disk-appeared callback and a disk-disappeared callback. Each callback function, which you implement, takes a DADiskRef. You can pass this to the DADiskCopyIOMedia function to get a service port to the I/O Kit media object for the disk.

I have no idea of what to do then, except that you will need to release the service port as described in that documentation. Also, you will still need to filter out non-USB devices, but at least you'll have the I/O Kit media object to do it with.

One other thing: This solution, and the NSWorkspace notifications you're currently using, probably both will not work if the iPod is not set to use, or does not support, disk mode. iPhones and iPods touch are the biggest current example. If that's the case, then you are just going to have to use I/O Kit from start to finish—neither DiskArb nor NSWorkspace will do the job for you.

Peter Hosey
Thanks a lot for the answer. I am very much new to Mac and IOKit. I think for a generic solution I will use IO Kit from start. Does it work even if iPod is not mounted? Can you please provide some leads like where to look for more information and IO Kit examples.
sbabu
Of course it *works*, as in provides you access to the device tree. But the iPod won't be in the device tree if it isn't connected, because the device tree is only those devices that are connected. As for examples: http://developer.apple.com/mac/
Peter Hosey
+1  A: 

A good example to get started with USB device notifications: http://developer.apple.com/Mac/library/samplecode/USBPrivateDataSample/listing2.html

I used the above example as the base for my USB detection code.

Another example that I've seen but not used, so cannot verify how well it works is: http://snipplr.com/view/1645/given-a-mount-path-retrieve-a-usb-device-name/

Joubert Nel
A: 

There's also the HIDUtilites sample from Apple. Take a read through the headers -- while there's not great documentation for this framework, I think there's a sample app or something with it.

RyanWilcox
+1  A: 

Here is a tutorial for doing it: http://joubert.posterous.com/notification-when-usb-storage-device-is-conne

Joubert Nel
A: 

For windows how can i get vendor id and product id

is it possible by using any functions in windows api

Thank you

barbgal