diff --git a/Gruntfile.js b/Gruntfile.js index 9b891c8..4daacfa 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -9,7 +9,7 @@ module.exports = function(grunt) { } }, eslint: { - src: ['*.js', 'test/**/*.js', 'arduinoTest/**/*.js'] + src: ['*.js', 'test/**/*.js', 'test_mocks/**/*.js', 'arduinoTest/**/*.js'] } }); diff --git a/package.json b/package.json index 011d9db..726bee1 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "license": "MIT", "scripts": { "install": "node-pre-gyp install --fallback-to-build", + "rebuild": "node-pre-gyp rebuild", "test": "grunt --verbose" } } diff --git a/serialport.js b/serialport.js index deff874..5f4a5d0 100644 --- a/serialport.js +++ b/serialport.js @@ -242,7 +242,7 @@ function SerialPortFactory(_spfOptions) { // verification code above SerialPort.prototype.update = function (options, callback) { var self = this; - if (!this.fd) { + if (!this.isOpen()) { debug('Update attempted, but serialport not available - FD is not set'); var err = new Error('Serialport not open.'); if (callback) { @@ -271,13 +271,13 @@ function SerialPortFactory(_spfOptions) { }; SerialPort.prototype.isOpen = function() { - return !!this.fd; + return this.fd !== null; }; SerialPort.prototype.write = function (buffer, callback) { var self = this; - if (!this.fd) { - debug('Write attempted, but serialport not available - FD is not set'); + if (!this.isOpen()) { + debug('Write attempted, but serialport is not open'); var err = new Error('Serialport not open.'); if (callback) { callback(err); @@ -305,9 +305,7 @@ function SerialPortFactory(_spfOptions) { if (process.platform !== 'win32') { SerialPort.prototype._read = function () { var self = this; - - // console.log(">>READ"); - if (!self.readable || self.paused || self.reading) { + if (!self.readable || self.paused || self.reading || this.closing) { return; } @@ -315,10 +313,7 @@ function SerialPortFactory(_spfOptions) { if (!self.pool || self.pool.length - self.pool.used < kMinPoolSpace) { // discard the old pool. Can't add to the free list because - // users might have refernces to slices on it. - self.pool = null; - - // alloc new pool + // users might have references to slices on it. self.pool = new Buffer(kPoolSize); self.pool.used = 0; } @@ -333,15 +328,16 @@ function SerialPortFactory(_spfOptions) { self.reading = false; if (err) { if (err.code && err.code === 'EAGAIN') { - if (self.fd >= 0) { + if (self.isOpen()) { self.serialPoller.start(); } - } else if (err.code && (err.code === 'EBADF' || err.code === 'ENXIO' || (err.errno === -1 || err.code === 'UNKNOWN'))) { // handle edge case were mac/unix doesn't clearly know the error. + // handle edge case were mac/unix doesn't clearly know the error. + } else if (err.code && (err.code === 'EBADF' || err.code === 'ENXIO' || (err.errno === -1 || err.code === 'UNKNOWN'))) { self.disconnected(err); } else { self.fd = null; - self.emit('error', err); self.readable = false; + self.emit('error', err); } } else { // Since we will often not read the number of bytes requested, @@ -400,7 +396,7 @@ function SerialPortFactory(_spfOptions) { } // No longer open? - if (self.fd === null) { + if (!this.isOpen()) { return; } @@ -438,9 +434,11 @@ function SerialPortFactory(_spfOptions) { debug('Disconnect completed with an exception: ' + JSON.stringify(e)); } + // TODO THIS IS CRAZY TOWN self.removeAllListeners(); + self.closing = false; - self.fd = 0; + self.fd = null; if (process.platform !== 'win32') { self.readable = false; @@ -450,13 +448,12 @@ function SerialPortFactory(_spfOptions) { SerialPort.prototype.close = function (callback) { var self = this; - var fd = self.fd; if (self.closing) { return; } - if (!fd) { + if (!this.isOpen()) { var err = new Error('Serialport not open.'); if (callback) { callback(err); @@ -489,7 +486,7 @@ function SerialPortFactory(_spfOptions) { self.emit('close'); self.removeAllListeners(); self.closing = false; - self.fd = 0; + self.fd = null; if (callback) { callback(); @@ -585,7 +582,7 @@ function SerialPortFactory(_spfOptions) { var self = this; var fd = self.fd; - if (!fd) { + if (!this.isOpen()) { var err = new Error('Serialport not open.'); if (callback) { callback(err); @@ -628,7 +625,7 @@ function SerialPortFactory(_spfOptions) { options.brk = _options.brk; } - if (!fd) { + if (!this.isOpen()) { var err = new Error('Serialport not open.'); if (callback) { callback(err); @@ -651,7 +648,7 @@ function SerialPortFactory(_spfOptions) { var self = this; var fd = this.fd; - if (!fd) { + if (!this.isOpen()) { var err = new Error('Serialport not open.'); if (callback) { callback(err); diff --git a/test/serialport-basic.js b/test/serialport-basic.js index 89412df..4b0a014 100644 --- a/test/serialport-basic.js +++ b/test/serialport-basic.js @@ -33,9 +33,7 @@ describe('SerialPort', function () { it('emits the open event', function (done) { var port = new SerialPort('/dev/exists'); - port.on('open', function() { - done(); - }); + port.on('open', done); }); it('emits an error on the factory when erroring without a callback', function (done) { @@ -146,6 +144,49 @@ describe('SerialPort', function () { done(); }); }); + + it('write should consider 0 to be a valid fd', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + port.write(new Buffer(0), done); + }); + }); + + it('flush should consider 0 to be a valid fd', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + port.flush(done); + }); + }); + + it('set should consider 0 to be a valid fd', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + port.set({}, done); + }); + }); + + it('drain should consider 0 to be a valid fd', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + port.drain(done); + }); + }); + + it('update should consider 0 a valid file descriptor', function() { + var port = new SerialPort('/dev/exists', function(done) { + expect(port.fd).to.equal(0); + port.update({}, done); + }); + }); + + it('isOpen should consider 0 a valid file descriptor', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + expect(port.isOpen()).to.be.true; + done(); + }); + }); }); describe('reading data', function () { @@ -178,8 +219,10 @@ describe('SerialPort', function () { it('passes the port to the bindings', function (done) { var openSpy = sandbox.spy(MockedSerialPort.SerialPortBinding, 'open'); var port = new SerialPort('/dev/exists', {}, false); + expect(port.isOpen()).to.be.false; port.open(function (err) { expect(err).to.not.be.ok; + expect(port.isOpen()).to.be.true; expect(openSpy.calledWith('/dev/exists')); done(); }); @@ -215,6 +258,7 @@ describe('SerialPort', function () { port.on('close', closeSpy); port.close(); expect(closeSpy.calledOnce); + expect(port.isOpen()).to.be.false; done(); }); }); @@ -233,10 +277,18 @@ describe('SerialPort', function () { it('emits a close event', function (done) { var port = new SerialPort('/dev/exists', function () { - port.on('close', function () { + port.on('close', done); + port.close(); + }); + }); + + it('should consider 0 a valid fd', function(done) { + var port = new SerialPort('/dev/exists', function() { + expect(port.fd).to.equal(0); + port.close(function () { + expect(port.fd).to.be.null; done(); }); - port.close(); }); }); }); @@ -244,18 +296,26 @@ describe('SerialPort', function () { describe('disconnect', function () { it('fires a disconnect event', function (done) { this.port = new SerialPort('/dev/exists', { - disconnectedCallback: function (err) { - expect(err).to.not.be.ok; - done(); - } + disconnectedCallback: done }, function () { hardware.disconnect('/dev/exists'); }); }); + it('disconnected closes port', function(done) { + var port = new SerialPort('/dev/exists', function () { + expect(port.fd).to.equal(0); + port.disconnected(); + expect(port.fd).to.be.null; + expect(port.isOpen()).to.be.false; + done(); + }); + }); + it('emits a disconnect event', function (done) { var port = new SerialPort('/dev/exists', function () { - port.on('disconnect', function () { + port.on('disconnect', function(err) { + expect(err).to.be.ok; done(); }); hardware.disconnect('/dev/exists'); diff --git a/test_mocks/linux-hardware.js b/test_mocks/linux-hardware.js index 28ecafa..9ad5d7c 100644 --- a/test_mocks/linux-hardware.js +++ b/test_mocks/linux-hardware.js @@ -1,12 +1,12 @@ // This takes a serial-port factory and mocks the shit out of it in complete isolation per require of this file -"use strict"; +'use strict'; var mockSerialportPoller = function (hardware) { - var Poller = function (path, cb) { - this.port = hardware.ports[path]; + var Poller = function (fd, cb) { + this.port = hardware.fds[fd]; if (!this.port) { - throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + throw new Error(fd + ' does not exist - please call hardware.createPort(path) first'); } this.port.poller = this; this.polling = null; @@ -25,22 +25,33 @@ var mockSerialportPoller = function (hardware) { }; var Hardware = function () { + this.nextFd = 0; + this.fds = {}; this.ports = {}; this.mockBinding = { list: this.list.bind(this), open: this.open.bind(this), + update: this.update.bind(this), write: this.write.bind(this), close: this.close.bind(this), flush: this.flush.bind(this), + set: this.set.bind(this), + drain: this.drain.bind(this), SerialportPoller: mockSerialportPoller(this) }; }; Hardware.prototype.reset = function () { this.ports = {}; + this.fds = {}; + this.nextFd = 0; }; Hardware.prototype.createPort = function (path) { + if (this.ports[path]) { + delete this.fds[this.ports[path].fd]; + } + var fd = this.nextFd++; this.ports[path] = { data: new Buffer(0), lastWrite: null, @@ -54,14 +65,16 @@ Hardware.prototype.createPort = function (path) { locationId: '', vendorId: '', productId: '' - } + }, + fd: fd }; + this.fds[fd] = this.ports[path]; }; Hardware.prototype.emitData = function (path, data) { var port = this.ports[path]; - if(!port) { - throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + if (!port) { + throw new Error(path + ' does not exist - please call hardware.createPort(path) first'); } port.data = Buffer.concat([port.data, data]); port.poller && port.poller.detectRead(); @@ -70,9 +83,9 @@ Hardware.prototype.emitData = function (path, data) { Hardware.prototype.disconnect = function (path) { var port = this.ports[path]; if (!port) { - throw new Error(path + " does not exist - please call hardware.createPort(path) first"); + throw new Error(path + ' does not exist - please call hardware.createPort(path) first'); } - port.openOpt.disconnectedCallback(); + port.openOpt.disconnectedCallback && port.openOpt.disconnectedCallback(); }; Hardware.prototype.list = function (cb) { @@ -86,49 +99,73 @@ Hardware.prototype.list = function (cb) { Hardware.prototype.open = function (path, opt, cb) { var port = this.ports[path]; if (!port) { - return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + return cb(new Error(path + ' does not exist - please call hardware.createPort(path) first')); } port.open = true; port.openOpt = opt; - cb && cb(null, path); // using path as the fd for convience + cb && cb(null, port.fd); }; -Hardware.prototype.write = function (path, buffer, cb) { - var port = this.ports[path]; +Hardware.prototype.update = function(fd, opt, cb) { + var port = this.fds[fd]; if (!port) { - return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); } - port.lastWrite = new Buffer(buffer); //copy + cb && cb(); +}; + +Hardware.prototype.write = function (fd, buffer, cb) { + var port = this.fds[fd]; + if (!port) { + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); + } + port.lastWrite = new Buffer(buffer); // copy cb && cb(null, buffer.length); }; -Hardware.prototype.close = function (path, cb) { - var port = this.ports[path]; +Hardware.prototype.close = function (fd, cb) { + var port = this.fds[fd]; if (!port) { - return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); } port.open = false; cb && cb(null); }; -Hardware.prototype.flush = function (path, cb) { - var port = this.ports[path]; +Hardware.prototype.flush = function (fd, cb) { + var port = this.fds[fd]; if (!port) { - return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); } cb && cb(null, undefined); }; -Hardware.prototype.fakeRead = function (path, buffer, offset, length, position, cb) { - var port = this.ports[path]; +Hardware.prototype.set = function (fd, options, cb) { + var port = this.fds[fd]; if (!port) { - return cb(new Error(path + " does not exist - please call hardware.createPort(path) first")); + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); + } + cb && cb(null, undefined); +}; + +Hardware.prototype.drain = function (fd, cb) { + var port = this.fds[fd]; + if (!port) { + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); + } + cb && cb(null, undefined); +}; + +Hardware.prototype.fakeRead = function (fd, buffer, offset, length, position, cb) { + var port = this.fds[fd]; + if (!port) { + return cb(new Error(fd + ' does not exist - please call hardware.createPort(path) first')); } if (port.data.length === 0) { return cb(null, 0, buffer); } if ((offset + length) > buffer.length) { - throw new Error("Length extends beyond buffer"); + throw new Error('Length extends beyond buffer'); } // node v0.8 doesn't like a slice that is bigger then available data