Over a million developers have joined DZone.

Raw access to USB device under Mac OS X 10.6

DZone's Guide to

Raw access to USB device under Mac OS X 10.6

Free Resource
Some time ago I had to implement Java applet for web page to access IrDA USB dongle under Windows, Linux and Mac OS X.
Windows and Linux both have IrSock implementations, so they are out of this article scope. Mac OS X differs in a way that they dropped IrDA support in 10.4. This limitation was the reason for my decision to use library JIR as 100% Java IrDA stack implementation, and access to underlying IrDA dongle hardware (chip) using native IOKit API.
IOKit API is available for C and C++ language only, and uses COM concept. I found very little samples on Internet, and they were mostly on synchronous bulk write to USB. While working with IR port employs asynchronous reading and writing, taking delays into account, putting things of puzzle together (Java interface; JIR; native methods) and making it work, took decent amount of my time. Finally, I got a clean enough Java USB access layer for Mac, so it can be reused for other USB dongle devices which don't have Mac driver such as IrDA dongles, TV tuners, custom boards, etc.


My library consists of 3 modules/layers:


1. usb-interface module (Java):
It provides abstraction for usb device modelling and its usage by client code.
Classes are:
usb.driver.DeviceDriver - base abstract class for device driver implementation;
usb.dongle.Device - model for raw USB device with methods for input-output to USB pipe;
devicemodel.* (SerialLink, SIRLink)- interfaces with contract for serial port abstraction;
usb.driver.impl.irda.MSC7780 - 100% Java device driver for Moschip MSC 7780- based IrDA dongle, based on Linux kernel C driver code.


2. darwin-usb module (Java):
It provides implementation classes with native methods:
usb.dongle.UsbDevice - base abstract class for USB device modelling by native wrappers for IOKit API;
usb.dongle.UsbDeviceListener - contract on raw callback listener (extended by usb.driver.DeviceDriver- subclasses);
usb.dongle.FactoryActor - singleton owner for background loop, which dispatches events from USB port hardware to device driver objects.


3. darwin-jni module (C):
C modules with native methods from darwin-usb Java classes.


The archive also contains irda-client module with entry-point main functions to test, and JIR module, which is essentially a customised version of JIR library (contains additional classes for IrCOMM protocol and necessary fixes).


I hope my classes could save some time and be a good start point and hardware abstraction layer for other Java developers who happen to communicate with raw USB device from Java app under Mac OS X.


Download darwin-usb library source code

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}