Switched to using eslint

This commit is contained in:
Rob Moran
2017-05-21 23:21:19 +01:00
parent cb728b7efb
commit 824ccfea21
9 changed files with 330 additions and 41 deletions

265
examples/backup.html Normal file
View File

@@ -0,0 +1,265 @@
<!DOCTYPE html>
<html>
<head>
<title>Web Bluetooth Secure DFU</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link href="https://fonts.googleapis.com/css?family=Raleway:400,600" rel="stylesheet">
<style>
body {
font-family: 'Raleway', sans-serif;
color: #d7ecfb;
background-color: #072b44;
text-align: center;
}
h1 {
font-weight: 400;
}
strong {
font-weight: 600;
}
#drop {
position: relative;
margin: 40px auto;
max-width: 680px;
background-color: rgba(255, 255, 255, 0.10);
padding: 120px 0px 100px;
outline: 2px dashed #072b44;
outline-offset: -10px;
}
#drop.hover {
outline-offset: -10px;
background-color: rgba(255, 255, 255, 0.15);
}
#icon {
width: 100%;
fill: #d7ecfb;
margin-bottom: 30px;
}
#file {
width: 0.1px;
height: 0.1px;
opacity: 0;
z-index: -1;
}
#label {
cursor: pointer;
}
#label:hover strong {
color: #8bb5ba;
}
#update {
position: absolute;
width: 100%;
visibility: hidden;
}
#button {
font-size: 12px;
color: inherit;
margin: 20px auto;
border: 0px;
background-color: #1b679c;
height: 30px;
padding: 0 30px;
border-radius: 4px;
text-transform: uppercase;
cursor: pointer;
outline: none;
}
#button:hover {
background: #2674ab;
}
#status {
margin: 20px auto;
border: 1px solid #d7ecfb;
width: 400px;
height: 24px;
visibility: hidden;
}
#bar {
background: #2674ab;
width: 0%;
height: 100%;
}
#transfer {
width: 100%;
line-height: 24px;
margin-top: -24px;
}
</style>
</head>
<body>
<h1>Web Bluetooth Secure Device Firmware Update</h1>
<div id="drop">
<svg id="icon" xmlns="http://www.w3.org/2000/svg" width="50" height="43" viewBox="0 0 50 43">
<path d="M48.4 26.5c-.9 0-1.7.7-1.7 1.7v11.6h-43.3v-11.6c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v13.2c0 .9.7 1.7 1.7 1.7h46.7c.9 0 1.7-.7 1.7-1.7v-13.2c0-1-.7-1.7-1.7-1.7zm-24.5 6.1c.3.3.8.5 1.2.5.4 0 .9-.2 1.2-.5l10-11.6c.7-.7.7-1.7 0-2.4s-1.7-.7-2.4 0l-7.1 8.3v-25.3c0-.9-.7-1.7-1.7-1.7s-1.7.7-1.7 1.7v25.3l-7.1-8.3c-.7-.7-1.7-.7-2.4 0s-.7 1.7 0 2.4l10 11.6z"/>
</svg>
<input id="file" type="file" />
<label id="label" for="file">
<strong>Choose a firmware package</strong>
<span>or drag it here</span>
</label>
<div id="update">
<button id="button">Update</button>
</div>
<div id="status">
<div id="bar"></div>
<div id="transfer"></div>
</div>
</div>
<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="/dist/secure-dfu.js"></script>
<script>
let dropEl = document.getElementById("drop");
let fileEl = document.getElementById("file");
let updateEl = document.getElementById("update");
let labelEl = document.getElementById("label");
let statusEl = document.getElementById("status");
let transferEl = document.getElementById("transfer");
let barEl = document.getElementById("bar");
let package = null;
let manifest = null;
function updateStatus(state) {
labelEl.textContent = state;
}
function updateTransfer(state) {
if (!state) {
statusEl.style.visibility = "hidden";
return;
}
updateEl.style.visibility = "hidden";
statusEl.style.visibility = "visible";
barEl.style.width = state.currentBytes / state.totalBytes * 100 + '%';
transferEl.textContent = `${state.currentBytes}/${state.totalBytes} bytes written`;
}
function updatePackage(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;
updateStatus(`Package: ${file.name}`);
updateEl.style.visibility = "visible";
})
.catch(error => {
updateEl.style.visibility = "hidden";
statusEl.style.visibility = "hidden";
updateStatus(error);
});
}
function transfer() {
if (!package) return;
updateStatus(`Selecting device...`);
updateTransfer();
let device = null;
const dfu = new SecureDfu(CRC32.buf);
dfu.addEventListener("log", event => {
console.log(event.message);
});
dfu.addEventListener("progress", event => {
updateTransfer(event);
});
dfu.requestDevice()
.then(selectedDevice => {
device = selectedDevice;
for (type of ["softdevice", "bootloader", "softdevice_bootloader"]) {
if (manifest[type]) {
return transferImage(device, dfu, manifest[type])
.then(() => {
if (manifest.application) {
return new Promise((resolve, reject) => {
device.addEventListener("gattserverdisconnected", event => {
resolve();
});
});
}
});
}
}
})
.then(() => {
if (manifest.application) {
return transferImage(device, dfu, manifest.application);
}
})
.then(() => {
updateStatus("Update complete!");
updateTransfer();
})
.catch(error => {
statusEl.style.visibility = "hidden";
updateStatus(error);
});
}
function transferImage(device, dfu, manifest) {
return dfu.connect(device)
.then(() => {
return package.file(manifest.dat_file).async("arraybuffer");
})
.then(data => {
updateStatus(`Transferring init: ${manifest.dat_file}...`);
return dfu.transferInit(data);
})
.then(() => {
return package.file(manifest.bin_file).async("arraybuffer");
})
.then(data => {
updateStatus(`Transferring firmware: ${manifest.bin_file}...`);
return dfu.transferFirmware(data);
});
}
fileEl.addEventListener("change", event => {
updatePackage(event.target.files[0]);
});
dropEl.addEventListener("drop", event => {
updatePackage(event.dataTransfer.files[0]);
});
updateEl.addEventListener("click", transfer);
["drag", "dragstart", "dragend", "dragover", "dragenter", "dragleave", "drop"].forEach(eventName => {
dropEl.addEventListener(eventName, event => {
event.preventDefault();
event.stopPropagation();
});
});
["dragover", "dragenter"].forEach(eventName => {
dropEl.addEventListener(eventName, event => {
dropEl.classList.add("hover");
});
});
["dragleave", "dragend", "drop"].forEach(eventName => {
dropEl.addEventListener(eventName, event => {
dropEl.classList.remove("hover");
});
});
</script>
</body>
</html>

View File

@@ -30,10 +30,10 @@ switch(deviceType) {
dfu.findDevice({ services: [0x180D] })
.then(device => {
fileName = fileMask.replace("{0}", device.name === "Hi_Rob" ? "bye" : "hi");
log("found device: " + device.name);
log("found device: " + device.name);
log("using file name: " + fileName);
return dfu.writeMode(device);
return dfu.writeMode(device);
})
.then(() => dfu.findDevice({ name: "DfuTarg" }))
.then(device => {

View File

@@ -17,7 +17,7 @@ function logError(error) {
}
function getFileName() {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
if (process.argv[2]) {
return resolve(process.argv[2]);
}
@@ -55,7 +55,7 @@ function downloadFile(url) {
}
function loadFile(fileName) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
var file = fs.readFileSync(fileName);
resolve(new Uint8Array(file).buffer);
});
@@ -97,6 +97,7 @@ function updateFirmware(dfu, package, manifest, device, type) {
}
function update() {
var device = null;
var dfu = null;
var package = null;
var manifest = null;
@@ -144,7 +145,7 @@ function update() {
console.log("DFU mode set");
return bluetooth.requestDevice({
filters: [{ services: [secureDfu.SERVICE_UUID] }],
deviceFound: device => {
deviceFound: () => {
// Select first device found with correct service
return true;
}
@@ -153,7 +154,7 @@ function update() {
.then(selectedDevice => {
device = selectedDevice;
for (type of ["softdevice", "bootloader", "softdevice_bootloader"]) {
for (var type of ["softdevice", "bootloader", "softdevice_bootloader"]) {
if (manifest[type]) {
return updateFirmware(dfu, package, manifest[type], device, type);
}