Saturday, December 6, 2008

Detecting whether a USB device is plugged in, via a Makefile or script

I needed a quick (5 minute) solution that would check that my USB dongle was inserted before running the scripted build. The dongle contains the license key for the compiler.

I found a small application supplied by Microsoft called DEVCON.EXE. You can get it from this blog or from Microsoft. The two key commands are 'devcon status *' or 'devcon find ...'. First you need to figure out what the Dongle is called in device language.
Using Cygwin, I ran devcon twice, once with the dongle inserted and once without.

devcon status * > a
devcon status * > b
diff a b
That got me the crucial VID and PID values "USB\VID_04B9&PID_0300" and saved me from paging through hundreds of devices. I used the * wildcard to ignore everything after the PID. When you run 'devcon find', it will also give more information about the device. I use this additional information to make sure that I have the right device. In my case, the string "SafeNet" appears.

Then I constructed this target in the Makefile that would make sure that the dongle was inserted, or else fail the build immediately. I used the result of 'grep' (success if it finds a match, fail if it doesn't) to conditional run the "echo" command.

all: donglecheck
make otherstuff

donglecheck:
@./common/tools/devcon find 'USB\VID_04B9&PID_0300*' | grep 'SafeNet' || ( echo "*** Please Insert IAR Dongle for EWARM ***" ; false )


The reason for this is that the IAR compiler will hang for 5 to 10 minutes before timing-out if there is no dongle, and I can't wait that long.

No comments: