node-serialport-bindings-cpp/lib/bindings-linux.js
Francis Gulotta 39d0103937 Streams rewrite
This is a major change.  To reading, the poller and the bindings layer, there isn’t a great way to do this piecemeal. The goal here is to make bindings a plugin layer, our c++ bindings isolated and tested, and make Serialport a streams object.

- Move all bindings into platform specific modules DarwinBinding LinuxBinding, and WindowsBinding
- Separate out the bindings from the SerialPort class so it can be required by itself.
- Provide mock bindings as a tested first class bindings layer
- Mixin stream methods with `pushBindingWrap`, inspired from fs.readStream
- All integration tests now require an Arduino 
- Ditch sandboxed modules as we don't need to be that heavy handed to test anymore

TODO

- windows binding
- performance test
- memory leak test
- 🎉
2016-11-04 21:03:35 -04:00

145 lines
3.1 KiB
JavaScript

'use strict';
var binding = require('bindings')('serialport.node');
var assign = require('object.assign').getPolyfill();
var defaultBindingOptions = {
vmin: 1,
vtime: 0
};
function asyncError(cb, err) {
process.nextTick(function() {
cb(err);
});
}
function LinuxBinding(opt) {
if (typeof opt !== 'object') {
throw new TypeError('options is not an object');
}
if (typeof opt.disconnect !== 'function') {
throw new TypeError('options.disconnect is not a function');
}
this.disconnect = opt.disconnect;
this.bindingOptions = opt.bindingOptions || {};
this.fd = null;
};
LinuxBinding.list = require('./list-linux');
LinuxBinding.prototype.read = require('./read-unix');
LinuxBinding.prototype.open = function(path, options, cb) {
if (typeof path !== 'string') {
throw new TypeError('path is not a string');
}
if (typeof options !== 'object') {
throw new TypeError('options is not an object');
}
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
if (this.isOpen) {
return asyncError(cb, new Error('Already open'));
}
options = assign({}, options, defaultBindingOptions, this.bindingOptions);
binding.open(path, options, function(err, fd) {
if (err) {
return cb(err);
}
this.fd = fd;
cb(null);
}.bind(this));
};
LinuxBinding.prototype.close = function(cb) {
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
if (this.readPoller) {
this.readPoller.stop();
this.readPoller = null;
}
binding.close(this.fd, function(err) {
if (err) {
return cb(err);
}
this.fd = null;
cb(null);
}.bind(this));
};
LinuxBinding.prototype.set = function(options, cb) {
if (typeof options !== 'object') {
throw new TypeError('options is not an object');
}
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
binding.set(this.fd, options, cb);
};
LinuxBinding.prototype.write = function(buffer, cb) {
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('buffer is not a Buffer');
}
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
binding.write(this.fd, buffer, cb);
};
var commonMethods = [
'drain',
'flush',
'update',
'get'
];
commonMethods.map(function(methodName) {
LinuxBinding.prototype[methodName] = function() {
var args = Array.prototype.slice.apply(arguments);
var cb = args[args.length - 1];
if (typeof cb !== 'function') {
throw new TypeError('cb is not a function');
}
if (!this.isOpen) {
return asyncError(cb, new Error('Port is not open'));
}
args.unshift(this.fd);
binding[methodName].apply(binding, args);
};
});
Object.defineProperty(LinuxBinding.prototype, 'isOpen', {
enumerable: true,
get: function() {
return this.fd !== null;
}
});
module.exports = LinuxBinding;