From 39e559fc1127ae4bc93ffd3ef072a03a007d772d Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Mon, 3 Sep 2012 17:15:52 -0700 Subject: [PATCH] Add fifo queue support to serialportWrite, fix test to detect arduinos under Linux --- src/serialport.cpp | 40 +++++++++++++++++++++++++++++++++++----- src/serialport.h | 7 +++++++ src/serialport_unix.cpp | 3 ++- src/serialport_win.cpp | 3 ++- test/{ => echo}/echo.ino | 0 test/echoTest.js | 10 ++++++++-- 6 files changed, 54 insertions(+), 9 deletions(-) rename test/{ => echo}/echo.ino (100%) diff --git a/src/serialport.cpp b/src/serialport.cpp index 3c00334..d1430b8 100644 --- a/src/serialport.cpp +++ b/src/serialport.cpp @@ -5,9 +5,15 @@ #define strcasecmp stricmp #endif +uv_mutex_t write_queue_mutex; +ngx_queue_t write_queue; + v8::Handle Open(const v8::Arguments& args) { v8::HandleScope scope; + uv_mutex_init(&write_queue_mutex); + ngx_queue_init(&write_queue); + // path if(!args[0]->IsString()) { return scope.Close(v8::ThrowException(v8::Exception::TypeError(v8::String::New("First argument must be a string")))); @@ -97,15 +103,28 @@ v8::Handle Write(const v8::Arguments& args) { baton->bufferLength = bufferLength; baton->callback = v8::Persistent::New(callback); - uv_work_t* req = new uv_work_t(); - req->data = baton; - uv_queue_work(uv_default_loop(), req, EIO_Write, EIO_AfterWrite); + QueuedWrite* queuedWrite = new QueuedWrite(); + memset(queuedWrite, 0, sizeof(QueuedWrite)); + ngx_queue_init(&queuedWrite->queue); + queuedWrite->baton = baton; + queuedWrite->req.data = queuedWrite; + + uv_mutex_lock(&write_queue_mutex); + bool empty = ngx_queue_empty(&write_queue); + + ngx_queue_insert_tail(&write_queue, &queuedWrite->queue); + + if (empty) { + uv_queue_work(uv_default_loop(), &queuedWrite->req, EIO_Write, EIO_AfterWrite); + } + uv_mutex_unlock(&write_queue_mutex); return scope.Close(v8::Undefined()); } void EIO_AfterWrite(uv_work_t* req) { - WriteBaton* data = static_cast(req->data); + QueuedWrite* queuedWrite = static_cast(req->data); + WriteBaton* data = static_cast(queuedWrite->baton); v8::Handle argv[2]; if(data->errorString[0]) { @@ -117,10 +136,21 @@ void EIO_AfterWrite(uv_work_t* req) { } v8::Function::Cast(*data->callback)->Call(v8::Context::GetCurrent()->Global(), 2, argv); + uv_mutex_lock(&write_queue_mutex); + ngx_queue_remove(&queuedWrite->queue); + + if (!ngx_queue_empty(&write_queue)) { + // Always pull the next work item from the head of the queue + ngx_queue_t* head = ngx_queue_head(&write_queue); + QueuedWrite* nextQueuedWrite = ngx_queue_data(head, QueuedWrite, queue); + uv_queue_work(uv_default_loop(), &nextQueuedWrite->req, EIO_Write, EIO_AfterWrite); + } + uv_mutex_unlock(&write_queue_mutex); + data->buffer.Dispose(); data->callback.Dispose(); delete data; - delete req; + delete queuedWrite; } v8::Handle Close(const v8::Arguments& args) { diff --git a/src/serialport.h b/src/serialport.h index e019272..67b227c 100644 --- a/src/serialport.h +++ b/src/serialport.h @@ -77,6 +77,13 @@ public: char errorString[1024]; }; +struct QueuedWrite { +public: + uv_work_t req; + ngx_queue_t queue; + WriteBaton* baton; +}; + struct CloseBaton { public: int fd; diff --git a/src/serialport_unix.cpp b/src/serialport_unix.cpp index 132e148..2b5d268 100644 --- a/src/serialport_unix.cpp +++ b/src/serialport_unix.cpp @@ -203,7 +203,8 @@ void EIO_Open(uv_work_t* req) { } void EIO_Write(uv_work_t* req) { - WriteBaton* data = static_cast(req->data); + QueuedWrite* queuedWrite = static_cast(req->data); + WriteBaton* data = static_cast(queuedWrite->baton); int bytesWritten = write(data->fd, data->bufferData, data->bufferLength); diff --git a/src/serialport_win.cpp b/src/serialport_win.cpp index 2abd869..af57360 100644 --- a/src/serialport_win.cpp +++ b/src/serialport_win.cpp @@ -222,7 +222,8 @@ void AfterOpenSuccess(int fd, v8::Handle dataCallback, v8::Handle(req->data); + QueuedWrite* queuedWrite = static_cast(req->data); + WriteBaton* data = static_cast(queuedWrite->baton); OVERLAPPED ov = {0}; ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); diff --git a/test/echo.ino b/test/echo/echo.ino similarity index 100% rename from test/echo.ino rename to test/echo/echo.ino diff --git a/test/echoTest.js b/test/echoTest.js index 99d9c24..2a03e86 100644 --- a/test/echoTest.js +++ b/test/echoTest.js @@ -17,6 +17,7 @@ describe('Echo', function() { for (var i = 0; i < results.length; i++) { var item = results[i]; + // Under Windows this catches any Arduino that is loaded using the Teensyduino INF. // Hardcoded to detect a Teensy INF device for now. Teensy INF works with Leonardo too. if (item.manufacturer && item.manufacturer.indexOf('PJRC') != -1) { portName = results[i].comName; @@ -28,6 +29,12 @@ describe('Echo', function() { portName = results[i].comName; break; } + + // Under Ubuntu 12.04 this catches a Teensy. + if (item.pnpId && item.pnpId.indexOf('Teensy') != -1) { + portName = results[i].comName; + break; + } } console.log("Arduino found on " + portName); @@ -89,13 +96,12 @@ describe('Echo', function() { sendFrame('abc'); sendFrame('def'); - sendFrame('ghi'); + sendFrame('ghi'); } sp.on('open', function () { setInterval(loop, 200); }); - }); }); }); \ No newline at end of file