USB Interaction

Discussion in 'Mac Programming' started by Blakeasd, Oct 13, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello!
    How can I detect in code if a USB storage device is connected to the computer? I need to be able to move files off of the device and move files on the device. I looked at IOKit, but the App Documentation said it was used for writing device drivers, which is not what I want to do. It would be helpful if someone could point me to some documentation about the task I would like to achieve. This will be done in Objective-C.
    Thanks!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    A USB storage device should appear as a removable disk. So hook into the mechanisms for handling disk mount events, and for working with removable disks.

    Start with the NSWorkspace class, and look at the notifications related to disk mounts: NSWorkspaceDidMountNotification etc.

    Also see the DiskArbitration framework, the hdiutil command-line tool, and the FSMegaInfo sample code.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    Is there anything higher level than DiskArbitration? It seems to be all in C and it's very confusing..
    Thanks!
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    NSWorkspace is higher-level. I did say you should start there.

    I also specifically gave the name of a notification which indicates that a disk was mounted. There are other methods in NSWorkspace for getting removable disks, which a USB storage device is almost certainly going to fall into.

    If you already read NSWorkspace's class docs and didn't find a feature you need, then post exactly what feature you need. Otherwise start with the NSWorkspace class.
     
  5. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    I am also interested in discovery attributes about the USB device. Such as its ID
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Exactly which ID do you mean?
     
  7. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #7
    Just general information about the device such as its manufacturer and available space.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Manufacturer is the USB Vendor ID. You'll need IOKit to get it.

    If the programming docs are too difficult, see the 'ioreg' command's man page.

    You can get the source for 'ioreg' from Apple's open source page:
    http://www.opensource.apple.com/

    It's under IOKitTools for a particular OS release, e.g.:
    http://www.opensource.apple.com/source/IOKitTools/IOKitTools-78/


    "Available space" is ambiguous. Do you mean total space of the storage device, or free space after subtracting space used by current files? Either way, you can get that using the statfs() function or getattrlist(), both C functions, which have man pages. Or see the FSMegaInfo sample code I previously linked to.

    You can also use NSTask (an Obj-C class) or popen() (a C function) to run the command:
    Code:
    diskutil info -plist /path/of/usb-disk-here
    
    The path of the disk is provided as one of the values in the disk-mount event (see NSWorkspace docs previously referenced).
     
  9. Sydde, Oct 14, 2011
    Last edited: Oct 14, 2011

    Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #9
    Use the NSFileManager method -attributesOfFileSystemForPath:error: to get size and free space on a drive.
     

Share This Page