Factored out package helper class for examples

This commit is contained in:
Rob Moran
2018-01-06 20:23:34 +00:00
parent 3a42b3245c
commit 7cd41ba48b
3 changed files with 119 additions and 82 deletions

View File

@@ -28,14 +28,14 @@ var http = require("http");
var https = require("https");
var readline = require("readline");
var crc = require("crc-32");
var JSZip = require("jszip");
var progress = require("progress");
var webbluetooth = require("webbluetooth");
var Package = require("./package");
var SecureDfu = require("../");
process.stdin.setEncoding("utf8");
// Determine manifest URL or file path
// Determine package URL or file path
function getFileName() {
return new Promise((resolve) => {
if (process.argv[2]) {
@@ -124,44 +124,6 @@ function downloadFile(url) {
});
}
// Firmware zip file wrapper
function Firmware(zipFile) {
this.zipFile = zipFile;
}
Firmware.prototype.load = function() {
try {
return this.zipFile.file("manifest.json").async("string")
.then(content => {
this.manifest = JSON.parse(content).manifest;
return this;
});
} catch(e) {
throw new Error("Unable to find manifest, is this a proper DFU package?");
}
};
Firmware.prototype.getImage = function(types) {
for (var type of types) {
if (this.manifest[type]) {
var entry = this.manifest[type];
var result = {
type: type,
initFile: entry.dat_file,
imageFile: entry.bin_file
};
return this.zipFile.file(result.initFile).async("arraybuffer")
.then(data => {
result.initData = data;
return this.zipFile.file(result.imageFile).async("arraybuffer")
})
.then(data => {
result.imageData = data;
return result;
});
}
}
}
// Update device using image containing init packet and data
function updateFirmware(dfu, device, image) {
console.log(`Using firmware: ${image.imageFile}`);
@@ -183,7 +145,7 @@ function updateFirmware(dfu, device, image) {
}
function update() {
var firmware = null;
var package = null;
var device = null;
var dfu = null;
@@ -194,11 +156,8 @@ function update() {
return loadFile(fileName);
})
.then(file => {
return JSZip.loadAsync(file);
})
.then(zipFile => {
firmware = new Firmware(zipFile);
return firmware.load();
package = new Package(file);
return package.load();
})
.then(() => {
console.log("Scanning for DFU devices...");
@@ -219,12 +178,12 @@ function update() {
})
.then(selectedDevice => {
device = selectedDevice;
return firmware.getImage(["softdevice", "bootloader", "softdevice_bootloader"]);
return package.getBaseImage();
})
.then(image => {
if (image) return updateFirmware(dfu, device, image);
})
.then(() => firmware.getImage(["application"]))
.then(() => package.getAppImage())
.then(image => {
if (image) return updateFirmware(dfu, device, image);
})

95
examples/package.js Normal file
View File

@@ -0,0 +1,95 @@
/*
* Web Bluetooth DFU
* Copyright (c) 2018 Rob Moran
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// https://github.com/umdjs/umd
(function (root, factory) {
if (typeof define === "function" && define.amd) {
// AMD. Register as an anonymous module.
define(["jszip"], factory);
} else if (typeof exports === "object") {
// Node. Does not work with strict CommonJS
module.exports = factory(require("jszip"));
} else {
// Browser globals with support for web workers (root is window)
root.SecureDfuPackage = factory(root.JSZip);
}
}(this, function(JSZip) {
"use strict";
function Package(buffer) {
this.buffer = buffer;
this.zipFile = null;
this.manifest = null;
};
Package.prototype.load = function() {
try {
return JSZip.loadAsync(this.buffer)
.then(zipFile => {
this.zipFile = zipFile;
return this.zipFile.file("manifest.json").async("string");
})
.then(content => {
this.manifest = JSON.parse(content).manifest;
return this;
});
} catch(e) {
throw new Error("Unable to find manifest, is this a proper DFU package?");
}
};
Package.prototype.getImage = function(types) {
for (var type of types) {
if (this.manifest[type]) {
var entry = this.manifest[type];
var result = {
type: type,
initFile: entry.dat_file,
imageFile: entry.bin_file
};
return this.zipFile.file(result.initFile).async("arraybuffer")
.then(data => {
result.initData = data;
return this.zipFile.file(result.imageFile).async("arraybuffer")
})
.then(data => {
result.imageData = data;
return result;
});
}
}
};
Package.prototype.getBaseImage = function() {
return this.getImage(["softdevice", "bootloader", "softdevice_bootloader"]);
};
Package.prototype.getAppImage = function() {
return this.getImage(["application"]);
};
return Package;
}));

View File

@@ -157,6 +157,7 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crc-32/1.0.2/crc32.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="./package.js"></script>
<script src="../dist/secure-dfu.js"></script>
<script>
@@ -170,7 +171,6 @@
let barEl = document.getElementById("bar");
let package = null;
let manifest = null;
function setStatus(state) {
labelEl.textContent = state;
@@ -187,20 +187,13 @@
transferEl.textContent = `${state.currentBytes}/${state.totalBytes} ${state.object} bytes written`;
}
// Load a firmware package
function setPackage(file) {
if (!file) return;
JSZip.loadAsync(file)
.then(zipFile => {
try {
package = zipFile;
return zipFile.file("manifest.json").async("string");
} catch(e) {
throw new Error("Unable to find manifest, is this a proper DFU package?");
}
})
.then(content => {
manifest = JSON.parse(content).manifest;
package = new SecureDfuPackage(file);
package.load()
.then(() => {
setStatus(`Firmware package: ${file.name}`);
selectEl.style.visibility = "visible";
})
@@ -211,6 +204,7 @@
});
}
// Choose a device
function selectDevice() {
setStatus("Selecting device...");
setTransfer();
@@ -237,20 +231,23 @@
});
}
// Update a device with all firmware from a package
function update(dfu, device) {
if (!package) return;
Promise.resolve()
.then(() => {
for (type of ["softdevice", "bootloader", "softdevice_bootloader"]) {
if (manifest[type]) {
return updateFirmware(device, dfu, manifest[type], type);
}
.then(() => package.getBaseImage())
.then(image => {
if (image) {
setStatus(`Updating ${image.type}: ${image.imageFile}...`);
return dfu.update(device, image.initData, image.imageData);
}
})
.then(() => {
if (manifest.application) {
return updateFirmware(device, dfu, manifest.application, "application");
.then(() => package.getAppImage())
.then(image => {
if (image) {
setStatus(`Updating ${image.type}: ${image.imageFile}...`);
return dfu.update(device, image.initData, image.imageData);
}
})
.then(() => {
@@ -263,20 +260,6 @@
});
}
function updateFirmware(device, dfu, manifest, type) {
let init = null;
return package.file(manifest.dat_file).async("arraybuffer")
.then(data => {
init = data;
return package.file(manifest.bin_file).async("arraybuffer");
})
.then(data => {
setStatus(`Updating ${type}: ${manifest.bin_file}...`);
return dfu.update(device, init, data);
});
}
fileEl.addEventListener("change", event => {
setPackage(event.target.files[0]);
});