Skip to content

Instantly share code, notes, and snippets.

@diego-carrera
Created November 28, 2014 16:45
Show Gist options
  • Save diego-carrera/4e77edfee983969c2e60 to your computer and use it in GitHub Desktop.
Save diego-carrera/4e77edfee983969c2e60 to your computer and use it in GitHub Desktop.
node-hid readsync readtimeout
diff --git a/src/HID.cc b/src/HID.cc
index 49deec5..e46a03d 100644
--- a/src/HID.cc
+++ b/src/HID.cc
@@ -81,6 +81,8 @@ private:
static Handle<Value> getFeatureReport(const Arguments& args);
static Handle<Value> sendFeatureReport(const Arguments& args);
+ static Handle<Value> readSync(const Arguments& args);
+ static Handle<Value> readTimeout(const Arguments& args);
static void recvAsync(uv_work_t* req);
@@ -133,7 +135,7 @@ HID::HID(const char* path)
os << "cannot open device with path " << path;
throw JSException(os.str());
}
-}
+}
void
HID::close()
@@ -179,7 +181,7 @@ HID::recvAsync(uv_work_t* req)
ReceiveIOCB* iocb = static_cast<ReceiveIOCB*>(req->data);
HID* hid = iocb->_hid;
- unsigned char buf[1024];
+ unsigned char buf[64];
int len = hid_read(hid->_hidHandle, buf, sizeof buf);
if (len < 0) {
iocb->_error = new JSException("could not read from HID device");
@@ -291,6 +293,92 @@ HID::getFeatureReport(const Arguments& args)
return retval;
}
+Handle<Value>
+HID::readSync(const Arguments& args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 1 ||
+ args[0]->ToUint32()->Value() == 0 ) {
+ return ThrowException(String::New("specify the buffer length to read"));
+ }
+
+ HID* hid = ObjectWrap::Unwrap<HID>(args.This());
+ const int bufSize = args[0]->ToUint32()->Value();
+
+ unsigned char* buf = new unsigned char[bufSize];
+ int returnedLength = hid_read(hid->_hidHandle, buf, bufSize);
+
+ if (returnedLength < 0) {
+ delete[] buf;
+ return ThrowException(String::New("Could not read from device"));
+ }
+#if 0
+ Local<Array> retval = Array::New();
+ for (int i = 0; i < returnedLength; i++) {
+ retval->Set(i, Integer::New(buf[i]));
+ }
+#else // Rinie try return buffer like read
+ //Get "fast" node Buffer constructor
+ Local<Function> nodeBufConstructor = Local<Function>::Cast(
+ Context::GetCurrent()->Global()->Get(String::New("Buffer") )
+ );
+ //Construct a new Buffer
+ Handle<Value> nodeBufferArgs[1] = { Integer::New(returnedLength) };
+ Local<Object> retval = nodeBufConstructor->NewInstance(1, nodeBufferArgs);
+ char* data = Buffer::Data(retval);
+ for (int i = 0; i < returnedLength; i++) {
+ data[i] = buf[i];
+ }
+#endif
+ delete[] buf;
+ return retval;
+}
+
+
+Handle<Value>
+HID::readTimeout(const Arguments& args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 2 ||
+ args[0]->ToUint32()->Value() == 0 ||
+ args[1]->ToUint32()->Value() == 0) {
+ return ThrowException(String::New("specify the buffer length to read and timeout"));
+ }
+
+ HID* hid = ObjectWrap::Unwrap<HID>(args.This());
+ const int bufSize = args[0]->ToUint32()->Value();
+ const int timeout = args[1]->ToUint32()->Value();
+
+ unsigned char* buf = new unsigned char[bufSize];
+ int returnedLength = hid_read_timeout(hid->_hidHandle, buf, bufSize, timeout);
+
+ if (returnedLength < 0) {
+ delete[] buf;
+ return ThrowException(String::New("Could not read from device"));
+ }
+#if 0
+ Local<Array> retval = Array::New();
+ for (int i = 0; i < returnedLength; i++) {
+ retval->Set(i, Integer::New(buf[i]));
+ }
+#else // Rinie try return buffer like read
+ //Get "fast" node Buffer constructor
+ Local<Function> nodeBufConstructor = Local<Function>::Cast(
+ Context::GetCurrent()->Global()->Get(String::New("Buffer") )
+ );
+ //Construct a new Buffer
+ Handle<Value> nodeBufferArgs[1] = { Integer::New(returnedLength) };
+ Local<Object> retval = nodeBufConstructor->NewInstance(1, nodeBufferArgs);
+ char* data = Buffer::Data(retval);
+ for (int i = 0; i < returnedLength; i++) {
+ data[i] = buf[i];
+ }
+#endif
+ delete[] buf;
+ return retval;
+}
Handle<Value>
HID::sendFeatureReport(const Arguments& args)
@@ -323,7 +411,7 @@ HID::sendFeatureReport(const Arguments& args)
int returnedLength = hid_send_feature_report(hid->_hidHandle, buf, message.size());
delete[] buf;
- if (returnedLength == -1) { // Not sure if there would ever be a valid return value of 0.
+ if (returnedLength == -1) { // Not sure if there would ever be a valid return value of 0.
return ThrowException(String::New("could not send feature report to device"));
}
@@ -459,7 +547,7 @@ HID::devices(const Arguments& args)
catch (JSException& e) {
return e.asV8Exception();
}
-
+
hid_device_info* devs = hid_enumerate(vendorId, productId);
Local<Array> retval = Array::New();
int count = 0;
@@ -520,6 +608,8 @@ HID::Initialize(Handle<Object> target)
NODE_SET_PROTOTYPE_METHOD(hidTemplate, "getFeatureReport", getFeatureReport);
NODE_SET_PROTOTYPE_METHOD(hidTemplate, "sendFeatureReport", sendFeatureReport);
NODE_SET_PROTOTYPE_METHOD(hidTemplate, "setNonBlocking", setNonBlocking);
+ NODE_SET_PROTOTYPE_METHOD(hidTemplate, "readSync", readSync);
+ NODE_SET_PROTOTYPE_METHOD(hidTemplate, "readTimeout", readTimeout);
target->Set(String::NewSymbol("HID"), hidTemplate->GetFunction());
@@ -528,11 +618,11 @@ HID::Initialize(Handle<Object> target)
extern "C" {
-
+
static void init (Handle<Object> target)
{
HandleScope handleScope;
-
+
HID::Initialize(target);
}
Copy link

ghost commented Nov 28, 2014

Looking good :-) Thanks. Once you push it to your branch, I can try it out :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment