Skip to content

Commit 05cb63d

Browse files
committed
Use EventEmitter stream for Endpoint Stream API
1 parent 3da9fdb commit 05cb63d

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

src/bindings.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Local<v8::Value> makeBuffer(const uint8_t* buf, unsigned length){
1010
return scope.Close(actualBuffer);
1111
}
1212

13-
void NodeUsb::doTransferCallback(Handle<Function> v8callback, libusb_transfer_status status, uint8_t* buffer, unsigned length){
13+
void NodeUsb::doTransferCallback(Handle<Function> v8callback, Handle<Object> v8this, libusb_transfer_status status, uint8_t* buffer, unsigned length){
1414
HandleScope scope;
1515
Local<Value> cbvalue = Local<Value>::New(Undefined());
1616
Local<Value> cberror = Local<Value>::New(Undefined());
@@ -25,7 +25,7 @@ void NodeUsb::doTransferCallback(Handle<Function> v8callback, libusb_transfer_st
2525

2626
Local<Value> argv[2] = {cbvalue, cberror};
2727
TryCatch try_catch;
28-
v8callback->Call(Context::GetCurrent()->Global(), 2, argv);
28+
v8callback->Call(v8this, 2, argv);
2929
if (try_catch.HasCaught()) {
3030
FatalException(try_catch);
3131
}

src/bindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace NodeUsb {
123123
class Transfer;
124124
class Stream;
125125

126-
void doTransferCallback(Handle<Function> v8callback, libusb_transfer_status status, uint8_t* buffer, unsigned length);
126+
void doTransferCallback(Handle<Function> v8callback, Handle<Object> v8this, libusb_transfer_status status, uint8_t* buffer, unsigned length);
127127

128128
struct nodeusb_endpoint_selection {
129129
int interface_number;

src/stream.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extern "C" void LIBUSB_CALL usbThreadStreamCb(libusb_transfer *t){
9090
}
9191

9292
void Stream::handleCompletion(CompletionData c){
93-
doTransferCallback(c.stream->v8callback, c.status, c.data, c.length);
93+
doTransferCallback(c.stream->v8callback, c.stream->v8endpoint, c.status, c.data, c.length);
9494
free(c.data);
9595

9696
if (c.dead){

src/transfer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void Transfer::handleCompletion(Transfer* t){
8080
}
8181
}
8282

83-
doTransferCallback(t->v8callback, t->transfer->status, buffer, length);
83+
doTransferCallback(t->v8callback, Context::GetCurrent()->Global(), t->transfer->status, buffer, length);
8484

8585
uv_unref(uv_default_loop());
8686
delete t;

tests/node-usb-test.coffee

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,19 @@ test "Write to OUT endpoint", ->
123123
test "Stream from IN endpoint", ->
124124
pkts = 0
125125

126-
inEndpoint.__stream_data_cb = (d, e) ->
127-
console.log("Stream callback", d, e)
126+
inEndpoint.on 'data', (d) ->
127+
console.log("Stream callback", d)
128128
pkts++
129129

130130
if pkts == 10
131131
inEndpoint.stopStream()
132132
console.log("Stopping stream")
133133

134-
inEndpoint.__stream_stop_cb = () ->
134+
inEndpoint.on 'error', (e) ->
135+
console.log("Stream error", e)
136+
assert.equal(e, 3)
137+
138+
inEndpoint.on 'end', ->
135139
console.log("Stream stopped")
136140
next()
137141

usb.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
22
* Expose complete node-usb binding to node.js
33
*/
4-
var exports = module.exports = require("./usb_bindings");
4+
var usb = exports = module.exports = require("./usb_bindings");
5+
var events = require('events');
56

67
var devices = undefined;
78

89
// singleton
910
exports.getDevices = function() {
1011
if (devices == undefined) {
11-
devices = this._getDevices();
12+
devices = usb._getDevices();
1213

1314
for (var i = 0, m = devices.length; i < m; i++) {
1415
var device = devices[i];
@@ -32,3 +33,22 @@ exports.find_by_vid_and_pid = function(vid, pid) {
3233

3334
return r;
3435
}
36+
37+
function inherits(target, source) {
38+
for (var k in source.prototype)
39+
target.prototype[k] = source.prototype[k];
40+
}
41+
42+
inherits(usb.Endpoint, events.EventEmitter);
43+
44+
usb.Endpoint.prototype.__stream_data_cb = function stream_data_cb(data, error){
45+
if (!error){
46+
this.emit("data", data)
47+
}else{
48+
this.emit("error", error)
49+
}
50+
}
51+
52+
usb.Endpoint.prototype.__stream_stop_cb = function stream_stop_cb(data, error){
53+
this.emit("end")
54+
}

0 commit comments

Comments
 (0)