From 3be9418902b43df2cf681f2ed84f630f9b6d9b7b Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sun, 31 Jul 2016 22:49:37 -0600 Subject: [PATCH] Convert flash to return a promise --- lib/flash.js | 76 +++++++++++++++++++++++-------------------- lib/tessel/restore.js | 14 ++++---- 2 files changed, 46 insertions(+), 44 deletions(-) diff --git a/lib/flash.js b/lib/flash.js index 73e57651..40624c3a 100644 --- a/lib/flash.js +++ b/lib/flash.js @@ -25,7 +25,7 @@ function factoryPartition(mac1, mac2) { ); } -function transaction(usbConnection, write, read, status_poll, wren, next) { +function transaction (usbConnection, write, read, status_poll, wren, next) { read = read || 0; status_poll = status_poll || false; wren = wren || false; @@ -69,8 +69,8 @@ function eraseChip(usbConnection, next) { } // Poll for the WIP bit in the status register to go low -function waitTransactionComplete(usbConnection, next) { - setTimeout(function onWait() { +function waitTransactionComplete (usbConnection, next) { + setTimeout(function onWait () { getStatus(usbConnection, (err, status) => { if (err) { return next(err); @@ -138,39 +138,43 @@ function randbyte() { return randint(0, 255); } -function asyncLog(msg, next) { - log.info(msg); - next(); +function asyncLog(msg) { + return function onAsyncLog(next) { + log.info(msg); + next(); + } } -module.exports = function flashDevice(usbConnection, ubootBuffer, squashfsBuffer, next) { - var uid = [randbyte(), randbyte(), randbyte(), randbyte()]; - var mac1 = [0x02, 0xa3].concat(uid); - var mac2 = [0x02, 0xa4].concat(uid); - - async.waterfall([ - asyncLog.bind(null, 'Checking the chip id...'), - readChipId.bind(null, usbConnection), - checkChipId, - - asyncLog.bind(null, 'Erasing the chip...'), - setWriteEnabled.bind(null, usbConnection), - eraseChip.bind(null, usbConnection), - waitTransactionComplete.bind(null, usbConnection), - - asyncLog.bind(null, 'Writing uboot...'), - write.bind(null, usbConnection, 0, ubootBuffer), - waitTransactionComplete.bind(null, usbConnection), - - asyncLog.bind(null, 'Writing mediatek factory partition...'), - write.bind(null, usbConnection, 0x40000, factoryPartition(mac1, mac2)), - waitTransactionComplete.bind(null, usbConnection), - - asyncLog.bind(null, 'Writing squashfs...'), - write.bind(null, usbConnection, 0x50000, squashfsBuffer), - waitTransactionComplete.bind(null, usbConnection), - asyncLog.bind(null, 'The update was successful.'), - asyncLog.bind(null, 'Please power cycle your Tessel.') - - ], next); +module.exports = function flashDevice(usbConnection, ubootBuffer, squashfsBuffer) { + return new Promise((resolve, reject)=> { + var uid = [randbyte(), randbyte(), randbyte(), randbyte()]; + var mac1 = [0x02, 0xa3].concat(uid); + var mac2 = [0x02, 0xa4].concat(uid); + + async.waterfall([ + asyncLog('Checking the chip id...'), + readChipId.bind(null, usbConnection), + checkChipId, + + asyncLog('Erasing the chip...'), + setWriteEnabled.bind(null, usbConnection), + eraseChip.bind(null, usbConnection), + waitTransactionComplete.bind(null, usbConnection), + + asyncLog('Writing uboot...'), + write.bind(null, usbConnection, 0, ubootBuffer), + waitTransactionComplete.bind(null, usbConnection), + + asyncLog('Writing mediatek factory partition...'), + write.bind(null, usbConnection, 0x40000, factoryPartition(mac1, mac2)), + waitTransactionComplete.bind(null, usbConnection), + + asyncLog('Writing squashfs...'), + write.bind(null, usbConnection, 0x50000, squashfsBuffer), + waitTransactionComplete.bind(null, usbConnection), + asyncLog('The update was successful.'), + asyncLog('Please power cycle your Tessel.') + + ], (err) => err ? reject(err) : resolve()); + }) }; diff --git a/lib/tessel/restore.js b/lib/tessel/restore.js index 715c6e64..8aea9954 100644 --- a/lib/tessel/restore.js +++ b/lib/tessel/restore.js @@ -10,21 +10,19 @@ var update = require('../update-fetch'); var log = require('../log'); var flash = require('../flash'); -Tessel.prototype.restore = function restore() { +Tessel.prototype.restore = function restore () { var usbConnection = this.connection; return new Promise((resolve, reject) => { log.info('Proceeding with updating OpenWrt...'); // download images - return update.fetchRestore() + return update + .fetchRestore() .then((result) => { - flash(usbConnection, result.uboot, result.squashfs, (err) => { - if (err) { - return reject(err); - } - resolve(); - }); + flash(usbConnection, result.uboot, result.squashfs) + .then(resolve) + .catch(reject) }); }); };