Permalink
Browse files

Added examples

- a simple lsusb port for node.js
- USB vendor ids can be loaded from linux-usb.org and tranformed to JSON (create_usb_ids.js)
  • Loading branch information...
1 parent a63e8a8 commit 119e4651c006c8352a396b50c6bd7b953d2bcf21 Christopher Klein committed Nov 11, 2010
Showing with 54,850 additions and 7 deletions.
  1. +1 −1 .gitignore
  2. +9 −0 .lock-wscript
  3. +3 −0 Makefile
  4. +9 −1 README
  5. +69 −0 examples/lsusb.js
  6. +71 −0 lsusb.js
  7. +1 −1 src/bindings.cc
  8. +4 −4 tests/node-usb-test.js
  9. +54,620 −0 usb_ids.js
  10. +63 −0 util/create_usb_ids.js
View
@@ -1,3 +1,3 @@
build/*
usb_bindings.node
-**/.lock*
+./.lock-wscript
View
@@ -0,0 +1,9 @@
+argv = ['/home/ckl/local/bin/node-waf', 'configure', 'clean', 'build']
+blddir = '/home/ckl/dev/app/node-usb/build'
+commands = {'dist': 0, 'configure': True, 'distcheck': 0, 'install': 0, 'build': True, 'clean': True, 'distclean': 0, 'check': 0, 'uninstall': 0}
+cwd = '/home/ckl/dev/app/node-usb'
+environ = {'USERNAME': 'ckl', 'WINDOWID': '81799326', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'LOGNAME': 'ckl', 'USER': 'ckl', 'GNOME_KEYRING_CONTROL': '/tmp/keyring-VWj2ef', 'HOME': '/home/ckl', 'DISPLAY': ':0.0', 'MAKEFLAGS': '', 'SSH_AGENT_PID': '2146', 'LANG': 'de_DE.utf8', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'XDG_SESSION_COOKIE': 'eaa6acd2a6d8a10cb2d49e344ba557f3-1289502408.135740-1015499594', 'SESSION_MANAGER': 'local/notebook:@/tmp/.ICE-unix/2112,unix/notebook:/tmp/.ICE-unix/2112', 'SHLVL': '1', 'MANDATORY_PATH': '/usr/share/gconf/gnome.mandatory.path', 'WINDOWPATH': '7', 'ORBIT_SOCKETDIR': '/tmp/orbit-ckl', 'MFLAGS': '', 'GDM_KEYBOARD_LAYOUT': 'de', 'SSH_AUTH_SOCK': '/tmp/keyring-VWj2ef/ssh', 'PATH': '/home/ckl/local/bin:/usr/local/cuda/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games', 'DEFAULTS_PATH': '/usr/share/gconf/gnome.default.path', 'XDG_DATA_DIRS': '/usr/share/gnome:/usr/local/share/:/usr/share/', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-gnome:/etc/xdg', 'DBUS_SESSION_BUS_ADDRESS': 'unix:abstract=/tmp/dbus-6sf7fMaANW,guid=de8dc3c944334ff6b09d569e0000001f', '_': '/usr/bin/make', 'XAUTHORITY': '/var/run/gdm/auth-for-ckl-yTnIrO/database', 'GDMSESSION': 'gnome', 'DESKTOP_SESSION': 'gnome', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'GNOME_KEYRING_PID': '2093', 'OLDPWD': '/home/ckl', 'GDM_LANG': 'de_DE.utf8', 'GTK_MODULES': 'canberra-gtk-module', 'SPEECHD_PORT': '7560', 'MAKELEVEL': '1', 'PWD': '/home/ckl/dev/app/node-usb', 'COLORTERM': 'gnome-terminal', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:'}
+files = []
+hash = 0
+options = {'compile_targets': None, 'force': False, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'destdir': '', 'keep': False, 'zones': '', 'blddir': '', 'prefix': '/usr/local/', 'jobs': 2, 'srcdir': '', 'check_cxx_compiler': 'g++ icpc sunc++', 'check_c_compiler': 'gcc icc suncc'}
+srcdir = '/home/ckl/dev/app/node-usb'
View
@@ -1,2 +1,5 @@
make:
node-waf configure clean build; node tests/node-usb-test.js
+
+create-usb-ids:
+ rm usb_ids.js; node util/create_usb_ids.js >> usb_ids.js
View
10 README
@@ -9,10 +9,18 @@ Installation
==============================
Make sure you have installed libusb-1.0-0-dev (Ubuntu: sudo apt-get install libusb-1.0-0-dev).
Just run
- make
+ make setup
in current directory and wait. "Unit tests" can be executed with
node tests/node-usb-test.js
+If you want to use the USB vendor ids, execute
+ make create-usb-ids
+
+Examples
+=============================
+A simple version of lsusb can be executed by typing
+ node examples/lsusb.js
+
TODO
=============================
View
@@ -0,0 +1,69 @@
+var usb_driver = require("../usb.js");
+var usb_ids = require("../usb_ids.js");
+var instance = usb_driver.create()
+var devices = instance.getDevices();
+
+function toHex(i) {
+ return Number(i).toString(16);
+}
+
+function toByteArray(_i) {
+ var byteString = Number(_i).toString(2);
+ var array = new Array();
+ var bitCounter = 0;
+ var byte = "";
+
+ for (var i = (byteString.length - 1); i >= 0; i--) {
+ byte = byteString[i] + byte;
+ bitCounter++;
+
+ if ((bitCounter == 8) || ((i - 1) < 0)) {
+ array.push(parseInt(byte, 2));
+ byte = "";
+ bitCounter = 0;
+ }
+ }
+
+ return array.reverse();
+}
+
+
+function pad(v, len, fill) {
+ var s = v.toString();
+ for (var i = s.length; i < len; i++) {
+ s = fill + s;
+ }
+
+ return s;
+}
+
+for (var i = 0; i < devices.length; i++) {
+ var device = devices[i];
+ var dd = device.getDeviceDescriptor();
+
+ console.log("Bus " + pad(device.busNumber, 3, "0") + " Device " + pad(device.deviceAddress, 3, "0") + " ID " + pad(toHex(dd.idVendor), 4, "0") + ":" + pad(toHex(dd.idProduct), 4, "0"));
+ console.log("Device Descriptor:");
+ console.log(" bLength " + dd.bLength);
+ console.log(" bDescriptorType " + dd.bDescriptorType);
+ // 2 bcdUSB: 2 bytes
+ var bcdUSBArr = toByteArray(dd.bcdUSB);
+ console.log(" bcdUSB " + bcdUSBArr[0] + "." + pad(bcdUSBArr[1], 2, "0"));
+ console.log(" bDeviceClass " + dd.bDeviceClass);
+ console.log(" bDeviceSubClass " + dd.bDeviceSubClass);
+ console.log(" bDeviceProtocol " + dd.bDeviceProtocol);
+ console.log(" bMaxPacketSize0 " + dd.bMaxPacketSize0);
+ var idVendor = pad(toHex(dd.idVendor), 4, "0");
+ console.log(" idVendor " + "0x" + idVendor); // + " " + usb_ids[idVendor].name);
+ var idProduct = pad(toHex(dd.idProduct), 4, "0")
+ console.log(" idProduct " + "0x" + idProduct); // + " " + usb_ids[idVendor].products[idProduct].name);
+ // 2 bcdDevice: 2 bytes
+ var bcdDeviceArr = toByteArray(dd.bcdDevice);
+ console.log(" bcdDevice " + bcdDeviceArr[0] + "." + pad(bcdDeviceArr[1], 2, "0"));
+ console.log(" iManufacturer " + dd.iManufacturer);
+ // iSerialNumber is default of libusb-1.0
+ console.log(" iSerial " + dd.iSerialNumber);
+ console.log(" bNumConfigurations " + dd.bNumConfigurations);
+}
+
+instance.close();
+
View
@@ -0,0 +1,71 @@
+var usb_driver = require("./usb.js");
+var usb_ids = require("./usb_ids.js");
+var instance = usb_driver.create()
+var devices = instance.getDevices();
+
+function toHex(i) {
+ return Number(i).toString(16);
+}
+
+function toByteArray(_i) {
+ var byteString = Number(_i).toString(2);
+ var array = new Array();
+ var bitCounter = 0;
+ var byte = "";
+
+ for (var i = (byteString.length - 1); i >= 0; i--) {
+ byte = byteString[i] + byte;
+ bitCounter++;
+
+ if ((bitCounter == 8) || ((i - 1) < 0)) {
+ array.push(parseInt(byte, 2));
+ byte = "";
+ bitCounter = 0;
+ }
+ }
+
+ return array.reverse();
+}
+
+
+function pad(v, len, fill) {
+ var s = v.toString();
+ for (var i = s.length; i < len; i++) {
+ s = fill + s;
+ }
+
+ return s;
+}
+
+for (var i = 0; i < devices.length; i++) {
+ var device = devices[i];
+ var dd = device.getDeviceDescriptor();
+
+ console.log("Bus " + pad(device.busNumber, 3, "0") + " Device " + pad(device.deviceAddress, 3, "0") + " ID " + pad(toHex(dd.idVendor), 4, "0") + ":" + pad(toHex(dd.idProduct), 4, "0"));
+ console.log("Device Descriptor:");
+ console.log(" bLength " + dd.bLength);
+ console.log(" bDescriptorType " + dd.bDescriptorType);
+ // 2 bcdUSB: 2 bytes
+ var bcdUSBArr = toByteArray(dd.bcdUSB);
+ console.log(" bcdUSB " + bcdUSBArr[0] + "." + pad(bcdUSBArr[1], 2, "0"));
+ console.log(" bDeviceClass " + dd.bDeviceClass);
+ console.log(" bDeviceSubClass " + dd.bDeviceSubClass);
+ console.log(" bDeviceProtocol " + dd.bDeviceProtocol);
+ console.log(" bMaxPacketSize0 " + dd.bMaxPacketSize0);
+ var idVendor = pad(toHex(dd.idVendor), 4, "0");
+ console.log(" idVendor " + "0x" + idVendor + " " + usb_ids[idVendor].name);
+ var idProduct = pad(toHex(dd.idProduct), 4, "0")
+ console.log(" idProduct " + "0x" + idProduct + " " + usb_ids[idVendor].products[idProduct].name);
+ // 2 bcdDevice: 2 bytes
+ var bcdDeviceArr = toByteArray(dd.bcdDevice);
+ console.log(" bcdDevice " + bcdDeviceArr[0] + "." + pad(bcdDeviceArr[1], 2, "0"));
+ console.log(" iManufacturer " + dd.iManufacturer);
+ console.log(" iProduct " + dd.iProduct);
+ // iSerialNumber is default of libusb-1.0
+ console.log(" iSerial " + dd.iSerialNumber);
+ console.log(" bNumConfigurations " + dd.bNumConfigurations);
+}
+
+instance.close();
+
+console.log(usb_ids.064e);
View
@@ -300,8 +300,8 @@ namespace NodeUsb {
// no constants at the moment
// Properties
- instance_template->SetAccessor(V8STR("busNumber"), Device::BusNumberGetter);
instance_template->SetAccessor(V8STR("deviceAddress"), Device::DeviceAddressGetter);
+ instance_template->SetAccessor(V8STR("busNumber"), Device::BusNumberGetter);
// Bindings to nodejs
NODE_SET_PROTOTYPE_METHOD(t, "open", Device::Open); // TODO Stream!
View
@@ -22,8 +22,8 @@ for (var i = 0; i < devices.length; i++) {
console.log(device);
assert.ok((device.busNumber > 0), "busNumber must be larger than 0");
assert.ok((device.deviceAddress > 0), "deviceAddress must be larger than 0");
- var id = device.busNumber + ":" + device.deviceAddress;
- console.log("working on " + id);
+// var id = device.busNumber + ":" + device.deviceAddress;
+// console.log("working on " + id);
assert.equal(device.close(), false, "close() must be false because device is not opened");
try {
@@ -34,8 +34,8 @@ for (var i = 0; i < devices.length; i++) {
console.log("failed to open device [" + id + "]: " + e.message);
}
- console.log(device.getDeviceDescriptor());
- console.log(device.getConfigDescriptor());
+// console.log(device.getDeviceDescriptor());
+// console.log(device.getConfigDescriptor());
}
assert.ok(instance.close());
Oops, something went wrong.

0 comments on commit 119e465

Please sign in to comment.