Skip to content

Commit

Permalink
wip Move to an object oriented bindings layer
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed Aug 17, 2016
1 parent 9df0b10 commit a02b2b0
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 362 deletions.
111 changes: 35 additions & 76 deletions lib/bindings-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,25 @@ function MissingPortError(message) {
}
util.inherits(MissingPortError, Error);

function PortReader(options) {
this.port = options.port;
this.fd = options.fd;
this.write = options.write;
this.errorCallback = options.errorCallback;
this.reading = false;
}

PortReader.prototype.emitData = function(buffer){
this.availbleData = Buffer.concat(this.availbleData, buffer);
};

PortReader.prototype.startReading = function(bytes) {
if (this.reading) {
return;
}
this.reading = true;
};

PortReader.prototype.stopReading = function() {
this.reading = false;
};
var nextFd = 0;
var fds = {};
var ports = {};

function MockBindings() {
this.nextFd = 0;
this.fds = {};
this.ports = {};
var functions = [
'close',
'drain',
'flush',
'list',
'open',
'set',
'update',
'write'
];
functions.forEach(function(name){
this[name] = this[name].bind(this);
}.bind(this));
this.isOpen = false;
};

MockBindings.prototype.PortReader = function(opt){
// if this fd doesn't exist we shouldn't throw but
// instead rely on the disconnect and error callbacks
opt.port = this.fds[opt.fd];
return new PortReader(opt);
MockBindings.reset = function() {
ports = {};
fds = {};
nextFd = 0;
};

MockBindings.prototype.reset = function() {
this.ports = {};
this.fds = {};
this.nextFd = 0;
};

MockBindings.prototype.createPort = function(path) {
if (this.ports[path]) {
delete this.fds[this.ports[path].fd];
MockBindings.createPort = function(path) {
if (ports[path]) {
delete fds[ports[path].fd];
}
this.ports[path] = {
ports[path] = {
data: new Buffer(0),
lastWrite: null,
poller: null,
Expand All @@ -93,49 +52,50 @@ MockBindings.prototype.createPort = function(path) {
};
};

MockBindings.list = function(cb) {
var info = Object.keys(ports).map(function(path) {
return ports[path].info;
});
callLater(cb, null, info);
};

// TODO
MockBindings.prototype.emitData = function(path, data) {
var port = this.ports[path];
var port = ports[path];
port.data = Buffer.concat([port.data, data]);
// port.poller && port.poller.detectRead();
};

MockBindings.prototype.disconnect = function(path) {
var port = this.ports[path];
var port = ports[path];
var err = new Error('disconnected');
port.openOpt.disconnectedCallback(err);
};

MockBindings.prototype.list = function(cb) {
var ports = this.ports;
var info = Object.keys(ports).map(function(path) {
return ports[path].info;
});
callLater(cb, null, info);
};

MockBindings.prototype.open = function(path, opt, cb) {
var port = this.ports[path];
this.port
var port = ports[path];
if (!port) {
return cb(new MissingPortError(path));
}
if (port.fds.length > 0 && port.openOpt.lock) {
return cb(new Error('port is locked cannot open'));
}
port.openOpt = opt;
var fd = this.nextFd++;
var fd = nextFd++;
port.fds.push(fd);
this.fds[fd] = port;
fds[fd] = port;
callLater(cb, null, fd);
};

MockBindings.prototype.close = function(fd, cb) {
var port = this.fds[fd];
var port = fds[fd];
if (!port) {
return callLater(cb, new Error(fd + ' fd is already closed'));
}

// remove fd
delete this.fds[fd];
delete fds[fd];
port.fds = port.fds.filter(function(item) { return item !== fd });
callLater(cb, null);
};
Expand All @@ -145,14 +105,14 @@ MockBindings.prototype.update = function(fd, opt, cb) {
throw new Error('Missing baudRate');
}

if (!this.fds[fd]) {
if (!fds[fd]) {
return callLater(cb, new MissingPortError());
}
callLater(cb, null);
};

MockBindings.prototype.write = function(fd, buffer, cb) {
var port = this.fds[fd];
var port = fds[fd];
if (!port) {
return callLater(cb, new MissingPortError());
}
Expand All @@ -161,7 +121,7 @@ MockBindings.prototype.write = function(fd, buffer, cb) {
};

MockBindings.prototype.read = function(fd, bytes, cb) {
var port = this.fds[fd];
var port = fds[fd];
if (!port) {
return callLater(cb, new MissingPortError());
}
Expand All @@ -171,25 +131,24 @@ MockBindings.prototype.read = function(fd, bytes, cb) {
};

MockBindings.prototype.flush = function(fd, cb) {
if (!this.fds[fd]) {
if (!fds[fd]) {
return callLater(cb, new MissingPortError());
}
callLater(cb, null, undefined);
};

MockBindings.prototype.set = function(fd, options, cb) {
if (!this.fds[fd]) {
if (!fds[fd]) {
return callLater(cb, new MissingPortError());
}
callLater(cb, null, undefined);
};

MockBindings.prototype.drain = function(fd, cb) {
if (!this.fds[fd]) {
if (!fds[fd]) {
return callLater(cb, new MissingPortError());
}
callLater(cb, null, undefined);
};

var mockBindings = new MockBindings();
module.exports = mockBindings;
module.exports = MockBindings;
24 changes: 11 additions & 13 deletions lib/read-unix.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';
var fs = require('fs');
// var fs = require('fs');

module.export = function read(size) {
var push = this.push.bind(this);
var fd = this.fd;
// module.export = function read(size) {
// var push = this.push.bind(this);
// var fd = this.fd;
// };


};


bindings.ondata(fd, function(chunk) {
// if push() returns false, then stop reading from source
if (!this.push(chunk)) {
this._source.readStop();
}
});
// bindings.ondata(fd, function(chunk) {
// // if push() returns false, then stop reading from source
// if (!this.push(chunk)) {
// this._source.readStop();
// }
// });
32 changes: 18 additions & 14 deletions lib/serialport.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var defaultSetFlags = {

/**
* @typedef {Object} openOptions
* @property {module:serialport~bindings=} bindings The hardware access bindings, defaults to install time c++ bindings based upon installation node version and platform
* @property {module:serialport~bindings=} binding The hardware access bindings, defaults to install time c++ bindings based upon installation node version and platform
* @property {boolean} [autoOpen=true] Automatically opens the port on `nextTick`
* @property {boolean} [lock=true] Prevent other processes from opening the port. false is not currently supported on windows.
* @property {number} [baudRate=9600] The baud rate of the port to be opened. This should match one of commonly available baud rates, such as 110, 300, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200. There is no guarantee, that the device connected to the serial port will support the requested baud rate, even if the port itself supports that baud rate.
Expand Down Expand Up @@ -109,12 +109,16 @@ function SerialPort(path, options, callback) {
// highWaterMark: settings.highWaterMark
});

this.Bindings = settings.bindings || SerialPort.Bindings;
var Binding = settings.binding || SerialPort.Binding;

if (!this.Bindings) {
if (Binding) {
throw new TypeError('No bindings set, pass it as `options.bindings` or set it on `SerialPort.Bindings`');
}

this.binding = new Binding({
push: this.push
});

if (!path) {
throw new TypeError('No path specified');
}
Expand Down Expand Up @@ -150,7 +154,7 @@ function SerialPort(path, options, callback) {
Object.defineProperty(this, 'isOpen', {
enumerable: true,
get: function() {
return this.fd !== null;
return this.binding.isOpen;
}
});

Expand Down Expand Up @@ -206,7 +210,7 @@ SerialPort.prototype.open = function(callback) {

this.opening = true;

this.bindings.open(this.path, this.options, function(err, fd) {
this.binding.open(this.path, this.options, function(err, fd) {
this.opening = false;
if (err) {
debug('SerialPortBinding.open had an error', err);
Expand Down Expand Up @@ -234,7 +238,7 @@ SerialPort.prototype.update = function(options, callback) {
var settings = assign({}, defaultSettings, options);
this.options.baudRate = settings.baudRate;

this.bindings.update(this.fd, this.options, function(err) {
this.binding.update(this.fd, this.options, function(err) {
if (err) {
return this._error(err, callback);
}
Expand Down Expand Up @@ -263,7 +267,7 @@ SerialPort.prototype._write = function(data, encoding, callback) {
}

debug('_write ' + data.length + ' bytes of data');
this.bindings.write(this.fd, data, callback);
this.binding.write(this.fd, data, callback);
};

SerialPort.prototype._writev = function(data, callback) {
Expand All @@ -280,12 +284,12 @@ SerialPort.prototype._writev = function(data, callback) {
// TODO document stream.read() vs data events

SerialPort.prototype._read = function(bytes) {
if(!this.isOpen){
this.once('open', function(){
if(!this.isOpen) {
this.once('open', function() {
this._read(bytes);
});
}
this.bindings.read(bytes);
this.binding.read(bytes);
};

/**
Expand Down Expand Up @@ -321,7 +325,7 @@ SerialPort.prototype.close = function(callback) {
var fd = this.fd;
this.fd = null;

this.bindings.close(fd, function(err) {
this.binding.close(fd, function(err) {
if (err) {
debug('SerialPortBinding.close had an error', err);
return this._error(err, callback);
Expand Down Expand Up @@ -364,7 +368,7 @@ SerialPort.prototype.set = function(options, callback) {
}
}

this.bindings.set(this.fd, settings, function(err) {
this.binding.set(this.fd, settings, function(err) {
if (err) {
debug('SerialPortBinding.set had an error', err);
return this._error(err, callback);
Expand All @@ -383,7 +387,7 @@ SerialPort.prototype.flush = function(callback) {
return this._error(new Error('Port is not open'), callback);
}

this.bindings.flush(this.fd, function(err, result) {
this.binding.flush(this.fd, function(err, result) {
if (err) {
debug('SerialPortBinding.flush had an error', err);
return this._error(err, callback);
Expand Down Expand Up @@ -412,7 +416,7 @@ SerialPort.prototype.drain = function(callback) {
return this._error(new Error('Port is not open'), callback);
}

this.bindings.drain(this.fd, function(err) {
this.binding.drain(this.fd, function(err) {
if (err) {
debug('SerialPortBinding.drain had an error', err);
return this._error(err, callback);
Expand Down
Loading

0 comments on commit a02b2b0

Please sign in to comment.