So I've been working on a project to work with Amazon Kindle's.
These devices show in MacOS as USB Mass Storage devices, and what I
need is the ability to query for how many are attached, the mount
point, and the USB Serial Number values. I have an exmaple that does
the attach / detach which I will include here. The problem is now how
do I assertain where the device is mounted? Thanks for any ideas!
The file has been posted with the name KIDeviceMonitor.m
> These devices show in MacOS as USB Mass Storage devices, and what I
> need is the ability to query for how many are attached, the mount
> point, and the USB Serial Number values. I have an exmaple that does
> the attach / detach which I will include here. The problem is now how
> do I assertain where the device is mounted? Thanks for any ideas!
No idea what these devices are, but if you know the USB vendor/product ID
(or some other means of identifying them), use IOServiceGetMatchingServices
to search the IORegistry for your device. This gives you back an io_object_t
object, then call IORegistryEntrySearchCFProperty for a "kIOBSDNameKey" to
get the BSD device name. Use IORegistryExplorer to see what keys might be of
interest to you.
The following is based on sample code that I got from developer.apple.com:
static CFTypeRef FindProp( io_registry_entry_t e, CFStringRef key, bool up )
{
IOOptionBits bits = kIORegistryIterateRecursively;
if( up )
{
bits |= kIORegistryIterateParents;
}
return IORegistryEntrySearchCFProperty( e, kIOServicePlane, key, NULL,
bits );
}
CFStringRef FindBSDDeviceName(unsigned int vendorID, unsigned int productID)
{
CFMutableDictionaryRef matchingDict;
kern_return_t kr;
io_iterator_t iter;
CFStringRef deviceNameAsCFString = NULL;
CFNumberRef reqdVID = CFNumberCreate(kCFAllocatorDefault,
kCFNumberIntType, &vendorID);
CFNumberRef reqdPID = CFNumberCreate(kCFAllocatorDefault,
kCFNumberIntType, &productID);
fprintf(stderr, "Looking for devices matching vendor ID=%u and product
ID=%u.\n", vendorID, productID);
// Set up the matching criteria for the devices we're interested in.
matchingDict = IOServiceMatching(kIOMediaClass); //
Interested in instances of class
if (matchingDict == NULL)
{
fprintf(stderr, "IOServiceMatching returned NULL.\n");
}
else
{
// We are looking for a mass storage device that has our VID/PID
CFDictionarySetValue( matchingDict, CFSTR(kIOMediaLeafKey),
kCFBooleanTrue );
CFDictionarySetValue( matchingDict, CFSTR(kIOMediaEjectableKey),
kCFBooleanTrue );
CFDictionarySetValue( matchingDict, CFSTR(kIOMediaWritableKey),
kCFBooleanTrue );
kr = IOServiceGetMatchingServices(kIOMasterPortDefault,
matchingDict, &iter);
if (kr == KERN_SUCCESS)
{
io_object_t device;
while ((device = IOIteratorNext(iter)) && deviceNameAsCFString
== NULL)
{
CFNumberRef vid = (CFNumberRef)FindProp(device,
CFSTR(kUSBVendorID), true);
CFNumberRef pid = (CFNumberRef)FindProp(device,
CFSTR(kUSBProductID), true);
if ( vid != NULL && pid != NULL &&
CFNumberCompare(vid, reqdVID, NULL) ==
kCFCompareEqualTo &&
CFNumberCompare(pid, reqdPID, NULL) ==
kCFCompareEqualTo)
{
// Found one: get the BSD device name
deviceNameAsCFString = (CFStringRef)FindProp(device,
CFSTR(kIOBSDNameKey), false);
}
// Done with this USB device; release the reference added by
IOServiceGetMatchingService
kr = IOObjectRelease(device);
}
}
}
return deviceNameAsCFString;
}
--
Sak Wathanasin
Network Analysis Limited
http://www.network-analysis.ltd.uk