Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new autoOpen option, defaults to true #178

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions nodehid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ function HID() {
//Inherit from EventEmitter
EventEmitter.call(this);

var options = {};
var argumentsLength = arguments.length;

if (typeof(arguments[argumentsLength - 1]) === "object") {
// last argument contains options
options = arguments[argumentsLength - 1];
argumentsLength--;
}

/* We also want to inherit from `binding.HID`, but unfortunately,
it's not so easy for native Objects. For example, the
following won't work since `new` keyword isn't used:
Expand All @@ -21,9 +30,9 @@ function HID() {

So... we do this craziness instead...
*/
var thisPlusArgs = new Array(arguments.length + 1);
var thisPlusArgs = new Array(argumentsLength + 1);
thisPlusArgs[0] = null;
for(var i = 0; i < arguments.length; i++)
for(var i = 0; i < argumentsLength; i++)
thisPlusArgs[i + 1] = arguments[i];
this._raw = new (Function.prototype.bind.apply(binding.HID,
thisPlusArgs) )();
Expand All @@ -34,15 +43,22 @@ function HID() {
`this._raw`
*/
for(var i in binding.HID.prototype)
if(i != "close" && i != "read")
if(i != "open" && i != "close" && i != "read")
this[i] = binding.HID.prototype[i].bind(this._raw);

this._closed = true;
this._paused = true;

if (typeof(options.autoOpen) === "undefined" || options.autoOpen) {
// default auto open to true, if not provided
this.open();
}

/* We are now done inheriting from `binding.HID` and EventEmitter.

Now upon adding a new listener for "data" events, we start
polling the HID device using `read(...)`
See `resume()` for more details. */
this._paused = true;
var self = this;
self.on("newListener", function(eventName, listener) {
if(eventName == "data")
Expand All @@ -53,6 +69,12 @@ function HID() {
util.inherits(HID, EventEmitter);
//Don't inherit from `binding.HID`; that's done above already!

HID.prototype.open = function open() {
this._raw.open();
this._closed = false;
this.resume();
};

HID.prototype.close = function close() {
this._closing = true;
this.removeAllListeners();
Expand All @@ -74,7 +96,7 @@ HID.prototype.read = function read(callback) {

HID.prototype.resume = function resume() {
var self = this;
if(self._paused && self.listeners("data").length > 0)
if(!self._closed && self._paused && self.listeners("data").length > 0)
{
//Start polling & reading loop
self._paused = false;
Expand Down
65 changes: 52 additions & 13 deletions src/HID.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class HID

typedef vector<unsigned char> databuf_t;

void open()
throw(JSException);
void write(const databuf_t& message)
throw(JSException);
void close();
Expand All @@ -77,6 +79,7 @@ class HID
~HID() { close(); }

static NAN_METHOD(New);
static NAN_METHOD(open);
static NAN_METHOD(read);
static NAN_METHOD(write);
static NAN_METHOD(close);
Expand Down Expand Up @@ -115,28 +118,49 @@ class HID

void readResultsToJSCallbackArguments(ReceiveIOCB* iocb, Local<Value> argv[]);

hid_device* _hidHandle;
int32_t _productId = -1;
int32_t _vendorId = -1;
wstring _serialNumber;
string _path;
hid_device* _hidHandle = NULL;
};

HID::HID(unsigned short vendorId, unsigned short productId, wchar_t* serialNumber)
{
_hidHandle = hid_open(vendorId, productId, serialNumber);

if (!_hidHandle) {
ostringstream os;
os << "cannot open device with vendor id 0x" << hex << vendorId << " and product id 0x" << productId;
throw JSException(os.str());
}
_vendorId = vendorId;
_productId = productId;
_serialNumber = serialNumber;
}

HID::HID(const char* path)
{
_hidHandle = hid_open_path(path);
_path = path;
}

void
HID::open()
throw(JSException)
{
if (_hidHandle != NULL) {
throw JSException("Device already open.");
}

if (_path.length() > 0) {
_hidHandle = hid_open_path(_path.c_str());

if (!_hidHandle) {
ostringstream os;
os << "cannot open device with path " << _path;
throw JSException(os.str());
}
} else {
_hidHandle = hid_open(_vendorId, _productId, _serialNumber.c_str());

if (!_hidHandle) {
ostringstream os;
os << "cannot open device with path " << path;
throw JSException(os.str());
if (!_hidHandle) {
ostringstream os;
os << "cannot open device with vendor id 0x" << hex << _vendorId << " and product id 0x" << _productId;
throw JSException(os.str());
}
}
}

Expand Down Expand Up @@ -415,6 +439,20 @@ NAN_METHOD(HID::New)
}
}

NAN_METHOD(HID::open)
{
Nan::HandleScope scope;

try {
HID* hid = Nan::ObjectWrap::Unwrap<HID>(info.This());
hid->open();
return;
}
catch (const JSException& e) {
e.asV8Exception();
}
}

NAN_METHOD(HID::close)
{
Nan::HandleScope scope;
Expand Down Expand Up @@ -598,6 +636,7 @@ HID::Initialize(Local<Object> target)
hidTemplate->InstanceTemplate()->SetInternalFieldCount(1);
hidTemplate->SetClassName(Nan::New<String>("HID").ToLocalChecked());

Nan::SetPrototypeMethod(hidTemplate, "open", open);
Nan::SetPrototypeMethod(hidTemplate, "close", close);
Nan::SetPrototypeMethod(hidTemplate, "read", read);
Nan::SetPrototypeMethod(hidTemplate, "write", write);
Expand Down