Comparing version 0.2.2 to 0.2.21
const util = require('util') | ||
const fs = require('fs') | ||
const EventEmitter = require('events').EventEmitter | ||
@@ -23,2 +24,14 @@ const SerialPort = require('serialport') | ||
function BusPirate(options) { | ||
// throw if no port is given | ||
if (!options || !options.port) { | ||
throw new Error('Port required in options object') | ||
} | ||
// throw if port does not exist | ||
fs.stat(options.port, (err) => { | ||
if (err) { | ||
throw new Error('Port not found') | ||
} | ||
}) | ||
EventEmitter.call(this) | ||
@@ -35,13 +48,12 @@ | ||
baudRate: 115200, | ||
autoOpen: false | ||
} | ||
) | ||
// TODO: change these to () => syntax | ||
this.port.on('open', function() { this.emit('open') }.bind(this)) | ||
this.port.on('open', () => { this.emit('open') }) | ||
this.port.on('data', function(data) { | ||
this.port.on('data', (data) => { | ||
data = Buffer.from(data).toString() | ||
this.inputQueue.push(data) | ||
console.log('Queue: ', this.inputQueue) | ||
}.bind(this)) | ||
}) | ||
} | ||
@@ -61,4 +73,21 @@ | ||
*/ | ||
BusPirate.prototype.reset = function() { | ||
this.port.write([0x0F]) | ||
BusPirate.prototype.reset = function(cb) { | ||
let exitReady = false | ||
this.port.write([0x00]) | ||
async.until( | ||
() => exitReady, | ||
(cb) => { | ||
if (this.inputQueue.length === 0) { | ||
this.port.write([0x00], () => { setTimeout(cb, 10) }) | ||
} else { | ||
let message = this.inputQueue.shift() | ||
if (message.indexOf('BBIO1') !== -1) { | ||
this.port.write([0x0F]) | ||
exitReady = true | ||
this._flush() | ||
} | ||
cb(null) | ||
} | ||
} | ||
) | ||
} | ||
@@ -76,12 +105,13 @@ | ||
BusPirate.prototype.start = function() { | ||
async.until( | ||
() => this._ready, | ||
(cb) => { | ||
if (this.inputQueue.length === 0) { | ||
this.port.write([0x00], () => { setTimeout(cb, 100) }) | ||
} else { | ||
let message = this.inputQueue.shift() | ||
if (message.indexOf('BBIO1') !== -1) { | ||
console.log('ready') | ||
this._ready = true | ||
this.port.open(() => { | ||
async.until( | ||
() => this._ready, | ||
(cb) => { | ||
if (this.inputQueue.length === 0) { | ||
this.port.write([0x00], () => { setTimeout(cb, 10) }) | ||
} else { | ||
let message = this.inputQueue.shift() | ||
if (message.indexOf('BBIO1') !== -1) { | ||
this._ready = true | ||
/** | ||
@@ -92,11 +122,12 @@ * Ready event -- signals the bus pirate is ready to recieve commands | ||
*/ | ||
this.emit('ready') | ||
this._flush() | ||
this.emit('ready') | ||
this._flush() | ||
} | ||
cb(null) | ||
} | ||
cb(null) | ||
} | ||
} | ||
) | ||
) | ||
}) | ||
} | ||
module.exports = BusPirate |
@@ -7,5 +7,3 @@ var BusPirate = require('../BusPirate') | ||
busPirate.on('open', () => { | ||
busPirate.start() | ||
}) | ||
busPirate.start() | ||
@@ -12,0 +10,0 @@ busPirate.on('ready', () => { |
@@ -31,3 +31,3 @@ const async = require('async') | ||
this._flush() | ||
this.port.write([0x02], () => { setTimeout(cb, 100) }) | ||
this.port.write([0x02], () => { setTimeout(cb, 10) }) | ||
this.i2c = false | ||
@@ -181,2 +181,2 @@ } else if (this.inputQueue.join('').indexOf('I2C1') !== -1) { | ||
module.exports = i2c | ||
module.exports = i2c |
{ | ||
"name": "bus-pirate", | ||
"version": "0.2.2", | ||
"description": "", | ||
"version": "0.2.21", | ||
"description": "Control Bus Pirate hardware with Node.JS", | ||
"main": "BusPirate.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "./node_modules/.bin/mocha test/BusPirate.js", | ||
"gen_docs": "./node_modules/.bin/jsdoc -c conf.json --readme README.md", | ||
@@ -9,0 +9,0 @@ "exampleUart": "node --use_strict examples/uart.js" |
// Need to test | ||
// constructor -- validation and success | ||
// start -- success and failure | ||
// | ||
const assert = require('chai').assert | ||
const sinon = require('sinon') | ||
const fs = require('fs') | ||
const BusPirate = require('../BusPirate.js') | ||
const MockPort = require('./fixtures/hardware-mock.js') | ||
function stubPort(busPirate) { | ||
busPirate.port = new MockPort() | ||
busPirate.port.on('open', () => { | ||
busPirate.emit('open') | ||
}) | ||
busPirate.port.on('data', (data) => { | ||
busPirate.inputQueue.push(data) | ||
}) | ||
} | ||
describe('Main BusPirate module', () => { | ||
let busPirate, fsStub | ||
before(() => { | ||
fsStub = sinon.stub(fs, 'stat') | ||
fsStub.withArgs('/dev/tty.usbserial-xxxx').yields() | ||
fsStub.withArgs('/dev/tty.usbserial-yyyy').throws(new Error('Port not found')) | ||
}) | ||
beforeEach(() => { | ||
busPirate = undefined; | ||
}) | ||
after(() => { | ||
fs.stat.restore() | ||
}) | ||
describe('constructor', () => { | ||
it('should construct when a port that exists is passed', () => { | ||
busPirate = new BusPirate({ | ||
port: '/dev/tty.usbserial-xxxx' | ||
}) | ||
assert(busPirate instanceof BusPirate) | ||
}) | ||
it('should fail when a non-existant port is passed', () => { | ||
let fn = () => { | ||
busPirate = new BusPirate({ | ||
port: '/dev/tty.usbserial-yyyy' | ||
}) | ||
} | ||
assert.throws(fn, /Port not found/, 'BusPirate constructor did not throw an error') | ||
}) | ||
it('should fail when no port is passed', () => { | ||
let fn = () => { | ||
busPirate = new BusPirate({}) | ||
} | ||
assert.throws(fn, /^Port required/, 'BusPirate constructor did not throw an error') | ||
}) | ||
}) | ||
describe('start()', () => { | ||
it('should fire the ready event when the bus pirate sends BBIO1', (done) => { | ||
busPirate = new BusPirate({ | ||
port: '/dev/tty.usbserial-xxxx' | ||
}) | ||
stubPort(busPirate) | ||
let eventHandler = sinon.spy() | ||
busPirate.start() | ||
busPirate.on('ready', () => { | ||
eventHandler() | ||
}) | ||
setTimeout(() => { busPirate.port.emit('data', 'BBIO1') }, 5) | ||
setTimeout(() => { | ||
assert(eventHandler.called, 'Ready event handler was not called') | ||
done() | ||
}, 20) | ||
}) | ||
}) | ||
describe('reset()', () => { | ||
it('should fire a 0x00, wait for BBIO1, then fire 0x0F', (done) => { | ||
busPirate = new BusPirate({ | ||
port: '/dev/tty.usbserial-xxxx' | ||
}) | ||
stubPort(busPirate) | ||
busPirate.start() | ||
setTimeout(() => { busPirate.port.emit('data', 'BBIO1') }, 5) | ||
busPirate.on('ready', () => { | ||
let writeSpy = sinon.spy(busPirate.port, 'write') | ||
busPirate.reset(() => {}) | ||
setTimeout(() => { busPirate.port.emit('data', 'BBIO1') }, 5) | ||
setTimeout(() => { | ||
assert(writeSpy.called, 'reset() did not call port.write') | ||
assert(writeSpy.getCall(0).args[0][0] == 0x00, 'reset() did not call port.write with 0x00 first') | ||
assert(writeSpy.lastCall.args[0][0] == 0x0F, 'reset() did not call port.write with 0x0F second') | ||
done() | ||
}, 15) | ||
}) | ||
}) | ||
}) | ||
describe('data queue', () => { | ||
}) | ||
}) |
@@ -1,6 +0,25 @@ | ||
// Need to add a mock that | ||
// stubs opening a serial port | ||
// stubs serial port writes | ||
// responds with sucess codes for various scenarios | ||
// responds with junk | ||
// responds with failure codes | ||
const EventEmitter = require('events').EventEmitter | ||
const util = require('util') | ||
// mock port | ||
// stubs all the functions of the serial port | ||
function MockPort() { | ||
if (!this instanceof MockPort) { | ||
throw new Error('MockPort is a constructor') | ||
} | ||
EventEmitter.call(this) | ||
} | ||
util.inherits(MockPort, EventEmitter) | ||
MockPort.prototype.open = function(cb) { | ||
this.emit('open') | ||
cb() | ||
} | ||
MockPort.prototype.write = function(dataArray, cb) { | ||
if (cb) { cb(null) } | ||
} | ||
module.exports = MockPort |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
20830
621
2
3