node-serialport-bindings-cpp/lib/bindings/linux.js
Francis Gulotta a98e92c898 Switch to arrow functions when possible
- Rename the linux-list test
- restore the require cache after proxyquire
2017-01-07 18:29:55 -05:00

144 lines
3 KiB
JavaScript

'use strict';
const binding = require('bindings')('serialport.node');
const defaultBindingOptions = {
vmin: 1,
vtime: 0
};
function asyncError(cb, err) {
process.nextTick(() => {
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('./linux-list');
LinuxBinding.prototype.read = require('./unix-read');
LinuxBinding.prototype.open = function(path, options, cb) {
if (!path) {
throw new TypeError('"path" is not a valid port');
}
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 = Object.assign({}, options, defaultBindingOptions, this.bindingOptions);
binding.open(path, options, (err, fd) => {
if (err) {
return cb(err);
}
this.fd = fd;
cb(null);
});
};
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.close();
this.readPoller = null;
}
binding.close(this.fd, (err) => {
if (err) {
return cb(err);
}
this.fd = null;
cb(null);
});
};
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);
};
const commonMethods = [
'drain',
'flush',
'update',
'get'
];
commonMethods.map((methodName) => {
LinuxBinding.prototype[methodName] = function() {
const args = Array.prototype.slice.apply(arguments);
const 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() {
return this.fd !== null;
}
});
module.exports = LinuxBinding;