Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bs2-programmer

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bs2-programmer - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

examples/blink.js

87

examples/hi.js
/*eslint-disable no-process-exit */
'use strict';
var com = require('serialport');
var when = require('when');
var nodefn = require('when/node');
var bs2 = require('../');
var Bs2SerialProtocol = require('bs2-serial-protocol');
var hex = new Buffer([0xFF, 0x00, 0x00, 0x00, 0x00, 0x30, 0xA0, 0xC7, 0x92, 0x66, 0x48, 0x13, 0x84, 0x4C, 0x35, 0x07, 0xC0, 0x4B]);
function upload(path, done){
function bootload(path, done){
var serialPort = new com.SerialPort(path, {
baudrate: 9600
}, false);
var protocol = new Bs2SerialProtocol({ path: path });
function setDtr(){
return when.promise(function(resolve, reject) {
serialPort.set({dtr: false, brk: true}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
var bs2Options = {
protocol: protocol,
revision: 'bs2'
};
function setBrk(){
return when.promise(function(resolve, reject) {
serialPort.set({dtr: true, brk: true}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
function clear(){
return when.promise(function(resolve, reject) {
serialPort.set({dtr: true, brk: false}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
function bootload(){
return bs2.bootload(serialPort, bs2.revisions.bs2, hex, 1000);
}
function close(){
return when.promise(function(resolve, reject) {
serialPort.on('error', function(err){
return reject(err);
});
serialPort.on('close', function(){
return resolve();
});
serialPort.close();
});
}
var promise = nodefn.lift(serialPort.open.bind(serialPort))()
.then(setDtr)
.delay(2)
.then(setBrk)
.delay(45)
.then(clear)
.then(bootload)
.finally(close);
return nodefn.bindCallback(promise, done);
return bs2.bootload(hex, bs2Options, done);
}
if(process && process.argv && process.argv[2])
{
upload(process.argv[2], function(error){
if(error)
{
console.log('error ', error);
}else{
if(process && process.argv && process.argv[2]){
bootload(process.argv[2], function(error){
if(error){
console.log(error);
} else {
console.log('success');

@@ -88,6 +30,5 @@ }

});
}else
{
} else {
console.log('call with a path like /dev/tty.something');
process.exit(0);
}
/*eslint-disable no-process-exit */
'use strict';
var com = require('serialport');
var when = require('when');
var most = require('most');
var nodefn = require('when/node');
var bs2 = require('../');
var Bs2SerialProtocol = require('bs2-serial-protocol');
// tries all revisions against the board
// async series wont work here to my knownledge because its kind of
// backwards, if error we need to continue to another board
// if success stop and give result..
// soo ... infinite promise chain instead? or bach?
function identify(stream, rev){
function search(path, done){
function setDtr(){
return when.promise(function(resolve, reject) {
stream.set({dtr: false, brk: true}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
var protocol = new Bs2SerialProtocol({ path: path });
function setBrk(){
return when.promise(function(resolve, reject) {
stream.set({dtr: true, brk: true}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
var revs = Object.keys(bs2.revisions);
function clear(){
return when.promise(function(resolve, reject) {
stream.set({dtr: true, brk: false}, function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
var stream = most.unfold(function(idx){
var bs2Options = {
protocol: protocol,
revision: revs[idx]
};
function id(){
return bs2.identify(stream, rev);
}
function close(){
return when.promise(function(resolve, reject) {
stream.on('error', function(err){
return reject(err);
// TODO: this needs a better abstraction because it is leaking
// implementation details
// maybe this could be a `search` method on programmer????
return protocol.enterProgramming()
.then(function(){
return bs2.identify(bs2Options);
})
.then(function(content){
return { value: content, done: true };
})
.catch(function(){
return { value: null, seed: idx + 1 };
})
.finally(function(){
return protocol.exitProgramming();
});
}, 0);
stream.on('close', function(){
return resolve();
});
var promise = stream
.skipWhile(function(val){
return val == null;
})
.drain();
stream.close();
});
}
return nodefn.lift(stream.open.bind(stream))()
.then(setDtr)
.delay(2)
.then(setBrk)
.delay(45)
.then(clear)
.then(id)
.finally(close);
nodefn.bindCallback(promise, done);
}
function search(path, done){
var serialPort = new com.SerialPort(path, {
baudrate: 9600
}, false);
var promise = identify(serialPort, bs2.revisions.bs2);
return nodefn.bindCallback(promise, done);
}
if(process && process.argv && process.argv[2])
{
if(process && process.argv && process.argv[2]){
search(process.argv[2], function(error, board){
if(error)
{
if(error){
console.log('error ', error);
}else{
} else {
console.log('found ', board);

@@ -96,6 +60,5 @@ }

});
}else
{
} else {
console.log('call with a path like /dev/tty.something');
process.exit(0);
}
'use strict';
var send = require('./lib/sendData');
var revisions = require('./lib/revisions');
var Programmer = require('./lib/programmer');
var when = require('when');
var nodefn = require('when/node');
function identify(options, cb){
var programmer = new Programmer(options);
var TimeoutError = require('./lib/timeouterror.js');
function challenge(stream, options, cb){
if(!options.hasOwnProperty('challenge')){
return nodefn.bindCallback(when(0), cb);
}
function unspool(index) {
return [index, index + 1];
}
function predicate(index) {
return index >= options.challenge.length;
}
function handler(index) {
return send(stream, 1000, options.challenge.slice(index, index + 1))
.then(function(response){
if(response !== options.response[index]){
throw new Error('Incorrect Response: ' + response + '. Board might not be a ' + options.name);
}
});
}
var promise = when.unfold(unspool, predicate, handler, 0);
return nodefn.bindCallback(promise, cb);
return programmer.identify(cb);
}
function identify(stream, options, cb) {
function challenge(options, cb){
var programmer = new Programmer(options);
function version(){
return send(stream, 1000, options.version);
}
var promise = challenge(stream, options)
.then(version)
.then(options.lookup)
// rewrite timeout errors
.catch(function(e){
if(e instanceof TimeoutError)
{
throw new Error(options.name + ' did not respond. Check power, connection, or maybe this is not a ' + options.name);
}
throw e;
});
return nodefn.bindCallback(promise, cb);
return programmer.challenge(cb);
}
function bootload(stream, type, hex, timeout, cb){
var pageSize = 18;
function bootload(hex, options, cb){
var programmer = new Programmer(options);
if(!hex || hex.length % pageSize !== 0){
throw new Error('Data must be in multiples of 18 bytes');
}
function unspool(index) {
return index + pageSize;
}
function predicate(index) {
return index >= hex.length;
}
function handler(index) {
return send(stream, timeout, hex.slice(index, index + pageSize))
.then(function(response){
if(response){
throw new Error('Board nacked packet ' + (index / pageSize) + ' with code: ' + response);
}
});
}
function upload(){
return when.iterate(unspool, predicate, handler, 0);
}
function signoff(){
return when.promise(function(resolve, reject) {
stream.write(new Buffer([0]), function(err){
if(err){ return reject(err); }
return resolve();
});
});
}
var promise = identify(stream, type)
.then(upload)
.then(signoff);
return nodefn.bindCallback(promise, cb);
return programmer.bootload(hex, cb);
}

@@ -104,4 +25,6 @@

identify: identify,
challenge: challenge,
bootload: bootload,
revisions: revisions
revisions: Programmer.revisions,
Programmer: Programmer
};

@@ -25,4 +25,3 @@ 'use strict';

},
bs2e:
{
bs2e: {
name: 'BS2e',

@@ -44,4 +43,3 @@ version: new Buffer([0x65]),

},
bs2sx:
{
bs2sx: {
name: 'BS2sx',

@@ -68,4 +66,3 @@ version: new Buffer([0x58]),

},
bs2p:
{
bs2p: {
name: 'BS2p',

@@ -107,4 +104,3 @@ version: new Buffer([0x50]),

},
bs2pe:
{
bs2pe: {
name: 'BS2pe',

@@ -111,0 +107,0 @@ version: new Buffer([0x49]),

{
"name": "bs2-programmer",
"version": "1.0.0",
"version": "2.0.0",
"description": "Upload tokenized hex to Basic Stamp 2",

@@ -10,6 +10,7 @@ "main": "index.js",

"dependencies": {
"most": "^0.13.0",
"when": "^3.7.2"
},
"devDependencies": {
"serialport": "^1.6.1",
"bs2-serial-protocol": "^0.1.2",
"code": "^1.3.0",

@@ -16,0 +17,0 @@ "lab": "^5.2.1"

@@ -8,345 +8,91 @@ 'use strict';

var bs2 = require('../');
var hardware = require('../mock/hardware');
var Protocol = require('../mock/protocol');
var hi = new Buffer([0xFF, 0x00, 0x00, 0x00, 0x00, 0x30, 0xA0, 0xC7, 0x92, 0x66, 0x48, 0x13, 0x84, 0x4C, 0x35, 0x07, 0xC0, 0x4B]);
var blink = new Buffer([0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x14, 0x20, 0x8c, 0x0e, 0xd8, 0xc8, 0x7c, 0xff, 0x0e, 0x60, 0x4a, 0xae, 0xe8, 0x9f, 0x49, 0xc1, 0x50, 0xc3, 0x6f, 0x8d, 0xd1, 0x03, 0x07, 0xc0, 0x60]);
var hi = require('./fixtures/hi');
lab.experiment('bs2', function () {
lab.experiment('bs2', function(){
var hw;
lab.beforeEach(function (done) {
hw = hardware();
var protocol;
var options;
lab.beforeEach(function(done){
protocol = new Protocol();
protocol._setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10]));
options = {
protocol: protocol,
revision: 'bs2'
};
done();
});
lab.test('times out', function (done) {
lab.experiment('challenge', function () {
bs2.identify(hw, bs2.revisions.bs2, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('BS2 did not respond. Check power, connection, or maybe this is not a BS2');
done();
lab.test('works without creating a new programmer instance first', function(done){
bs2.challenge(options, function(err){
Code.expect(err).to.not.exist();
done();
});
});
});
lab.test('fails on incorrect response', function (done) {
hw.setData(new Buffer([0xC8, 0xAD, 0xCE, 0x10]));
bs2.identify(hw, bs2.revisions.bs2, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Incorrect Response: 200. Board might not be a BS2');
done();
lab.test('supports the promise pattern', function(done){
bs2.challenge(options)
.then(function(){
done();
})
.catch(function(err){
Code.expect(err).to.not.exist();
})
.done();
});
});
lab.test('bs2 1.0', function (done) {
lab.experiment('identify', function(){
hw.setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10]));
bs2.identify(hw, bs2.revisions.bs2, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2', version: '1.0'});
done();
lab.test('works without creating a new programmer instance first', function(done){
bs2.identify(options, function(err, result){
Code.expect(err).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2', version: '1.0'});
done();
});
});
});
lab.test('bs2 less than one character', function (done) {
hw.setData(new Buffer([0xBE, 0xAD, 0xCE, 0x00]));
bs2.identify(hw, bs2.revisions.bs2, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2', version: '0'});
done();
lab.test('supports the promise pattern', function(done){
bs2.identify(options)
.then(function(result){
Code.expect(result).to.deep.equal({name: 'BS2', version: '1.0'});
done();
})
.catch(function(err){
Code.expect(err).to.not.exist();
})
.done();
});
});
lab.test('bs2e 1.0', function (done) {
lab.experiment('bootload', function(){
hw.setData(new Buffer([0x65]));
bs2.identify(hw, bs2.revisions.bs2e, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2e', version: '1.0'});
lab.beforeEach(function(done){
//send bs2 response bytes, then the success byte
protocol._setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10, 0x00]));
done();
});
});
lab.test('bs2e unknown', function (done) {
hw.setData(new Buffer([0x02]));
bs2.identify(hw, bs2.revisions.bs2e, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Unknown: 2');
done();
lab.test('works without creating a new programmer instance first', function(done){
bs2.bootload(hi, options, function(err){
Code.expect(err).to.not.exist();
done();
});
});
});
lab.test('bs2sx 1.0', function (done) {
hw.setData(new Buffer([0x58]));
bs2.identify(hw, bs2.revisions.bs2sx, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2sx', version: '1.0'});
done();
lab.test('supports the promise pattern', function(done){
bs2.bootload(hi, options)
.then(function(){
done();
})
.catch(function(err){
Code.expect(err).to.not.exist();
})
.done();
});
});
lab.test('bs2sx 1.1', function (done) {
hw.setData(new Buffer([0x59]));
bs2.identify(hw, bs2.revisions.bs2sx, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2sx', version: '1.1'});
done();
});
});
lab.test('bs2sx 1.2', function (done) {
hw.setData(new Buffer([0x60]));
bs2.identify(hw, bs2.revisions.bs2sx, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2sx', version: '1.2'});
done();
});
});
lab.test('bs2sx unknown', function (done) {
hw.setData(new Buffer([0x02]));
bs2.identify(hw, bs2.revisions.bs2sx, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Unknown: 2');
done();
});
});
lab.test('bs2p 1.0', function (done) {
hw.setData(new Buffer([0x70]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p24', version: '1.0'});
done();
});
});
lab.test('bs2p 1.1', function (done) {
hw.setData(new Buffer([0x71]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p24', version: '1.1'});
done();
});
});
lab.test('bs2p 1.2', function (done) {
hw.setData(new Buffer([0x72]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p24', version: '1.2'});
done();
});
});
lab.test('bs2p 1.3', function (done) {
hw.setData(new Buffer([0x73]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p24', version: '1.3'});
done();
});
});
lab.test('bs2p 1.0', function (done) {
hw.setData(new Buffer([0x50]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p40', version: '1.0'});
done();
});
});
lab.test('bs2p 1.1', function (done) {
hw.setData(new Buffer([0x51]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p40', version: '1.1'});
done();
});
});
lab.test('bs2p 1.2', function (done) {
hw.setData(new Buffer([0x52]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p40', version: '1.2'});
done();
});
});
lab.test('bs2p 1.3', function (done) {
hw.setData(new Buffer([0x53]));
bs2.identify(hw, bs2.revisions.bs2p, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2p40', version: '1.3'});
done();
});
});
lab.test('bs2p unknown', function (done) {
hw.setData(new Buffer([0x02]));
bs2.identify(hw, bs2.revisions.bs2p, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Unknown: 2');
done();
});
});
lab.test('bs2pe 1.0', function (done) {
hw.setData(new Buffer([0x69]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe24', version: '1.0'});
done();
});
});
lab.test('bs2pe 1.1', function (done) {
hw.setData(new Buffer([0x70]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe24', version: '1.1'});
done();
});
});
lab.test('bs2pe 1.2', function (done) {
hw.setData(new Buffer([0x71]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe24', version: '1.2'});
done();
});
});
lab.test('bs2pe 1.0', function (done) {
hw.setData(new Buffer([0x49]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe40', version: '1.0'});
done();
});
});
lab.test('bs2pe 1.1', function (done) {
hw.setData(new Buffer([0x50]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe40', version: '1.1'});
done();
});
});
lab.test('bs2pe 1.2', function (done) {
hw.setData(new Buffer([0x51]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error, result){
Code.expect(error).to.not.exist();
Code.expect(result).to.deep.equal({name: 'BS2pe40', version: '1.2'});
done();
});
});
lab.test('bs2pe unknown', function (done) {
hw.setData(new Buffer([0x02]));
bs2.identify(hw, bs2.revisions.bs2pe, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Unknown: 2');
done();
});
});
lab.test('bootload throws with non multiple of 18 byte data', function (done) {
var throws = function () {
bs2.bootload(hw, bs2.revisions.bs2, new Buffer([0x00, 0x01, 0x02, 0x03]), 1000, function(){});
};
Code.expect(throws).to.throw(Error, 'Data must be in multiples of 18 bytes');
done();
});
lab.test('bootload bs2 with 18 byte packet', function (done) {
//send bs2 response bytes, then the success byte
hw.setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10, 0x00]));
bs2.bootload(hw, bs2.revisions.bs2, hi, 1000, function(error){
Code.expect(error).to.not.exist();
done();
});
});
lab.test('bootload bs2 with 36 byte packet', function (done) {
//send bs2 response bytes, then 2 success bytes, 1 for each packet
hw.setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10, 0x00, 0x00]));
bs2.bootload(hw, bs2.revisions.bs2, blink, 1000, function(error){
Code.expect(error).to.not.exist();
done();
});
});
lab.test('bootload bs2 error byte', function (done) {
//send bs2 response bytes, then the error byte which should stop bootload at first 18 byte packet
hw.setData(new Buffer([0xBE, 0xAD, 0xCE, 0x10, 0x01]));
bs2.bootload(hw, bs2.revisions.bs2, blink, 1000, function(error){
Code.expect(error).to.exist();
Code.expect(error.message).to.equal('Board nacked packet 0 with code: 1');
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc