Permalink
Browse files

- Readme file renamed

- Removed semicolons from line end
- Added check for pkg-config as suggested in #9 (thanks to geuis)
  • Loading branch information...
1 parent e7b2fe4 commit f0747e6b9ae329693b3b1536dc29ec0c998e7116 Christopher Klein committed Apr 1, 2012
Showing with 87 additions and 67 deletions.
  1. +27 −9 README → Readme.md
  2. +59 −58 tests/node-usb-test.js
  3. +1 −0 wscript
View
@@ -1,35 +1,43 @@
libusb-1.0 bindings for Node.js
-==============================
+===============================
node-usb binds libusb-1.0 to Node.js
Tested with Node version 0.6.6/Linux.
Brew/OSX port of libusb-1.0 has still an issue which results in a segfault. Try using the latest development branch from http://git.libusb.org
Installation
-==============================
+============
Make sure you have installed libusb-1.0-0-dev (Ubuntu: sudo apt-get install libusb-1.0-0-dev).
Just run
+
make
+
in current directory and wait. "Unit tests" are automatically executed by default target.
You can although execute the tests by typing:
+
node tests/node-usb-test.js
If you want to use the USB vendor ids, execute
+
make create-usb-ids
For creating debug output you have to compile node-usb with
+
make debug
+
or
+
node-waf clean configure --debug=true build
API
-=============================
+===
returned values can always be an error object, so make sure that no error occurs!
The device/interface/endpoint descriptor values are always injected into the proper object. I do not list them.
Usb
+---
.LIBUSB_* : const
constant properties from libusb
.isLibusbInitalized : boolean
@@ -43,7 +51,7 @@ Usb
.close() : undefined
closes node-usb
-Device
+ Device
.deviceAddress : int
property for getting device address
.busNumber : int
@@ -64,7 +72,10 @@ Device
* Buffer object, then controlTransfer works in write mode (write data TO USB device)
_timeout parameter is optional.
afterTransfer contains a Buffer object with the data read from device.
+
Interface
+---------
+
.__idxInterface : int
property for internal interface index
.__idxAltSetting : int
@@ -87,6 +98,8 @@ Interface
returns byte array with extra data from interface descriptor
Endpoint
+--------
+
.__endpointType : int
property for getting the endpoint type (IN/OUT), check with Usb.LIBUSB_ENDPOINT_TYPE constants
.__transferType : int
@@ -109,23 +122,28 @@ Endpoint
.submitNative(mixed read|write, function after(data) [, int _timeout, int _iso_packets, int _flags]) : undefined
[async] bulkTransfer, controlTransfer and interruptTransfer are using the EV library for threading. submitNative() uses the libusb asynchronous interface. This is not really tested now.
submitNative() will be the only function to handle isochronous transfer mode and detects the endpoint type automatically.
- First parameter can be either (I'll bet, you know it already):
+ First parameter can be either (you know it already):
* int, the function will be work in read mode (read _int_ bytes FROM USB device)
- * Array, the function will work in write mode (write byte array TO USB device)
+ * Buffer object, the function will work in write mode (write byte array TO USB device)
The parameter _iso_packets and _flags can be used, if the endpoint is working in isochronous mode otherwise they are useless.
+
Examples
-=============================
+========
A simple port of lsusb can be executed by typing
+
node examples/lsusb/lsusb.js
-If you own a Microsoft Kinect, you can manipulate the LED and motor tilt by typing
+If you own a Microsoft Kinect, you can manipulate the LED, set the angle of the device and read the axis of the accelerometer
+
node examples/node-usb-kinect.js
+
Browse to localhost:8080 and see the magic.
+
If you have permission issues, you have to add the proper rights to the USB device or execute the examples as root (NOT RECOMMENDED!)
More information
-==============================
+=================
Christopher Klein <schakkonator[at]googlemail[dot]com>
http://twitter.com/schakko
http://wap.ecw.de
View
@@ -1,104 +1,105 @@
-var assert = require('assert');
-var usb_driver = require("../usb.js");
+var assert = require('assert')
+var usb_driver = require("../usb.js")
var instance = usb_driver.create()
-assert.notEqual(instance, undefined, "instance must be undefined");
-assert.ok((instance.LIBUSB_CLASS_PER_INTERFACE != undefined), "Constants must be defined");
-assert.ok((instance.LIBUSB_ENDPOINT_IN == 128));
-assert.notEqual(instance.revision, "unknown", "Revision should not unknown");
-assert.equal(instance.isLibusbInitalized, false, "isLibusbInitalized must be false");
-assert.equal(instance.close(), false, "close() must be false because driver is not opened");
+console.log("---- Failed tests does NOT mean, node-usb is working. It depends on your USB infrastructure that all tests pass. Try the examples/kinect/kinect.js for proper working")
+assert.notEqual(instance, undefined, "instance must be undefined")
+assert.ok((instance.LIBUSB_CLASS_PER_INTERFACE != undefined), "Constants must be defined")
+assert.ok((instance.LIBUSB_ENDPOINT_IN == 128))
+assert.notEqual(instance.revision, "unknown", "Revision should not unknown")
+assert.equal(instance.isLibusbInitalized, false, "isLibusbInitalized must be false")
+assert.equal(instance.close(), false, "close() must be false because driver is not opened")
-assert.ok(instance.refresh(), "refresh() must be true");
-assert.ok(instance.isLibusbInitalized, "isLibusbInitalized must be true after refresh()");
+assert.ok(instance.refresh(), "refresh() must be true")
+assert.ok(instance.isLibusbInitalized, "isLibusbInitalized must be true after refresh()")
-assert.throws(function() { instance.setDebugLevel(); }, TypeError);
-assert.throws(function() { instance.setDebugLevel(-1); }, TypeError);
-assert.throws(function() { instance.setDebugLevel(4); }, TypeError);
-assert.doesNotThrow(function() { instance.setDebugLevel(0); });
+assert.throws(function() { instance.setDebugLevel(); }, TypeError)
+assert.throws(function() { instance.setDebugLevel(-1); }, TypeError)
+assert.throws(function() { instance.setDebugLevel(4); }, TypeError)
+assert.doesNotThrow(function() { instance.setDebugLevel(0)})
-var devices = instance.get_devices();
-assert.notEqual(devices, undefined, "getDevices() must not be undefined");
-assert.ok((devices.length > 0), "getDevices() must be larger than 0 (assume that at least one host controller is available)");
+var devices = instance.get_devices()
+assert.notEqual(devices, undefined, "getDevices() must not be undefined")
+assert.ok((devices.length > 0), "getDevices() must be larger than 0 (assume that at least one host controller is available)")
function assert_extra_length(obj) {
- var r = obj.getExtraData();
- assert.ok((r.length == obj.extra_length), "getExtraLength() (length is: " + r.length + ") + must be equal to .extra_length (is: " + obj.extra_length + ")");
+ var r = obj.getExtraData()
+ assert.ok((r.length == obj.extra_length), "getExtraLength() (length is: " + r.length + ") + must be equal to .extra_length (is: " + obj.extra_length + ")")
}
for (var i = 0; i < devices.length; i++) {
- var device = devices[i];
- var deviceDesc = undefined;
- var deviceConfigDesc = undefined;
- 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;
- assert.ok(((deviceDesc = device.getDeviceDescriptor()) != undefined), "getDeviceDescriptor() must return an object");
- assert.ok(((deviceConfigDesc = device.getConfigDescriptor()) != undefined), "getConfigDescriptor() must return an object");
-
- var arr = instance.find_by_vid_and_pid(deviceDesc.idVendor, deviceDesc.idProduct);
- assert.ok((arr != undefined), "usb.find_by_vid_and_pid() must return array");
- assert.ok((arr.length > 0), "usb.find_by_vid_and_pid() must return array with length > 0");
- var found = false;
+ var device = devices[i]
+ var deviceDesc = undefined
+ var deviceConfigDesc = undefined
+ 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
+ assert.ok(((deviceDesc = device.getDeviceDescriptor()) != undefined), "getDeviceDescriptor() must return an object")
+ assert.ok(((deviceConfigDesc = device.getConfigDescriptor()) != undefined), "getConfigDescriptor() must return an object")
+
+ var arr = instance.find_by_vid_and_pid(deviceDesc.idVendor, deviceDesc.idProduct)
+ assert.ok((arr != undefined), "usb.find_by_vid_and_pid() must return array")
+ assert.ok((arr.length > 0), "usb.find_by_vid_and_pid() must return array with length > 0")
+ var found = false
for (var j = 0; j < arr.length; j++) {
if ((arr[j].deviceAddress == device.deviceAddress) && (arr[j].busNumber == device.busNumber)) {
- found = true;
- break;
+ found = true
+ break
}
}
- assert.ok(found, "could not find USB interface with find_by_vid_and_pid with equal busNumber and deviceAddress");
- assert_extra_length(device);
+ assert.ok(found, "could not find USB interface with find_by_vid_and_pid with equal busNumber and deviceAddress")
+ assert_extra_length(device)
- var interfaces = device.getInterfaces();
+ var interfaces = device.getInterfaces()
- assert.ok((interfaces != undefined), "Device.getInterfaces() must return an array");
- assert.ok((interfaces.length >= 0), "Device.getInterfaces() must return an array with length >= 0");
+ assert.ok((interfaces != undefined), "Device.getInterfaces() must return an array")
+ assert.ok((interfaces.length >= 0), "Device.getInterfaces() must return an array with length >= 0")
for (var j = 0; j < interfaces.length; j++) {
- var interface = interfaces[j];
- assert_extra_length(interface);
+ var interface = interfaces[j]
+ assert_extra_length(interface)
- var endpoints = interface.getEndpoints();
- assert.ok((endpoints != undefined), "Device.getEndpoints() must return an array");
- assert.ok((endpoints.length >= 0), "Device.getEndpoints() must return an array with length >= 0");
+ var endpoints = interface.getEndpoints()
+ assert.ok((endpoints != undefined), "Device.getEndpoints() must return an array")
+ assert.ok((endpoints.length >= 0), "Device.getEndpoints() must return an array with length >= 0")
// if we do not claim the interface, we'll get some pthrad_mutex_lock errors from libusb/glibc
- var r = interface.claim();
+ var r = interface.claim()
if (r != undefined) {
- console.log("Failed to claim endpoint; error: " + r.errno);
- continue;
+ console.log("Failed to claim endpoint; error: " + r.errno)
+ continue
}
for (k = 0; k < endpoints.length; k++) {
- var endpoint = endpoints[k];
- assert_extra_length(endpoint);
+ var endpoint = endpoints[k]
+ assert_extra_length(endpoint)
- assert.throws(function() { endpoint.submitNative(); });
- assert.throws(function() { endpoint.submitNative(1,2); });
+ assert.throws(function() { endpoint.submitNative()})
+ assert.throws(function() { endpoint.submitNative(1,2)})
if (endpoint.__endpointType == instance.LIBUSB_ENDPOINT_OUT) {
// wrong usage of endpoint. Endpoint is OUT, usage is IN
- assert.throws(function() { endpoint.submitNative(100, function(_stat) {}, 0, 0); });
- var param = new Buffer(new Array(0x01));
- assert.ok(endpoint.submitNative(param, function (_stat, _data) {}, 10, 10));
+ assert.throws(function() { endpoint.submitNative(100, function(_stat) {}, 0, 0)})
+ var param = new Buffer(new Array(0x01))
+ assert.ok(endpoint.submitNative(param, function (_stat, _data) {}, 10, 10))
}
else {
// endoint is IN, usage is OUT
var param = new Buffer(new Array(0x01));
- assert.throws(function() { endpoint.submitNative(param, function (_stat, _data) {}, 0, 0); });
- assert.ok(endpoint.submitNative(100, function (stat, _data) {}, 10, 10));
+ assert.throws(function() { endpoint.submitNative(param, function (_stat, _data) {}, 0, 0)})
+ assert.ok(endpoint.submitNative(100, function (stat, _data) {}, 10, 10))
}
}
}
// assert.equal(device.close(), true, "close() must be true because device is opened by prior
}
-assert.ok(instance.close());
+assert.ok(instance.close())
/*
endpoint.write(new Buffer(0x01, 0x01), function(status) {
@@ -108,5 +109,5 @@ endpoint.read(100./^bytes$/, function(status, bytes) {
});
*/
-console.log("Tests were successful :-)");
+console.log("Tests were successful :-)")
View
@@ -17,6 +17,7 @@ def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool("compiler_cc")
conf.check_tool('node_addon')
+ conf.find_program('pkg-config', mandatory=True)
conf.check_cfg(package='libusb-1.0', uselib_store='USB10', mandatory=1, args='--cflags --libs')
conf.env.append_unique('CPPFLAGS', ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"])

0 comments on commit f0747e6

Please sign in to comment.